bitset.hpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #include <climits>
00043 #include <cmath>
00044
00045 namespace Gecode { namespace Int { namespace Extensional {
00046
00048 class BitSet {
00050 typedef unsigned int Base;
00052 Base* data;
00054 unsigned int size;
00055 public:
00057 BitSet(void);
00059 BitSet(Space& home, unsigned int s, bool value = false);
00061 BitSet(Space& home, const BitSet& bs);
00063 void init(Space& home, unsigned int s, bool value=false);
00065 bool get(unsigned int i) const;
00067 void set(unsigned int i, bool value=true);
00068 };
00069
00070 forceinline void
00071 BitSet::init(Space& home, unsigned int s, bool value) {
00072 size = static_cast<unsigned int>(std::ceil(static_cast<double>(s)
00073 /(CHAR_BIT*sizeof(Base))));
00074 data = home.alloc<Base>(size);
00075 Base ival = value ? ~0 : 0;
00076 for (unsigned int i = size; i--; ) data[i] = ival;
00077 }
00078
00079 forceinline
00080 BitSet::BitSet(void) : data(NULL), size(0) {}
00081
00082 forceinline
00083 BitSet::BitSet(Space& home, unsigned int s, bool value)
00084 : data(NULL), size(0) {
00085 init(home, s, value);
00086 }
00087 forceinline
00088 BitSet::BitSet(Space& home, const BitSet& bs)
00089 : data(home.alloc<Base>(bs.size)), size(bs.size) {
00090 for (unsigned int i = size; i--; ) data[i] = bs.data[i];
00091 }
00092 forceinline bool
00093 BitSet::get(unsigned int i) const {
00094 unsigned int pos = i / (sizeof(Base)*CHAR_BIT);
00095 unsigned int bit = i % (sizeof(Base)*CHAR_BIT);
00096 assert(pos < size);
00097 return (data[pos] & ((Base)1 << bit)) != 0;
00098 }
00099 forceinline void
00100 BitSet::set(unsigned int i, bool value) {
00101 unsigned int pos = i / (sizeof(Base)*CHAR_BIT);
00102 unsigned int bit = i % (sizeof(Base)*CHAR_BIT);
00103 assert(pos < size);
00104 if (value)
00105 data[pos] |= 1 << bit;
00106 else
00107 data[pos] &= ~(1 << bit);
00108 }
00109
00110 }}}
00111
00112
00113