bitset.icc
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
00044 namespace Gecode { namespace Int { namespace Extensional {
00045
00047 class BitSet {
00049 typedef unsigned int Base;
00051 Base* data;
00053 unsigned int size;
00054 public:
00056 BitSet(void);
00058 BitSet(Space* home, int s, bool value = false);
00060 BitSet(Space* home, const BitSet& bs);
00062 void init(Space *home, int s, bool value=false);
00064 bool get(unsigned int i);
00066 void set(unsigned int i, bool value=true);
00067 };
00068
00069 forceinline void
00070 BitSet::init(Space* home, int s, bool value) {
00071 size = static_cast<int>(std::ceil(static_cast<double>(s)
00072 /(CHAR_BIT*sizeof(Base))));
00073 data = static_cast<Base*>(home->alloc(size*sizeof(Base)));
00074 Base ival = value ? ~0 : 0;
00075 for (int i = size; i--; ) data[i] = ival;
00076 }
00077
00078 forceinline
00079 BitSet::BitSet(void) : data(NULL), size(0) {}
00080
00081 forceinline
00082 BitSet::BitSet(Space *home, int s, bool value)
00083 : data(NULL), size(0) {
00084 init(home, s, value);
00085 }
00086 forceinline
00087 BitSet::BitSet(Space *home, const BitSet& bs)
00088 : data(static_cast<Base*>(home->alloc(bs.size*sizeof(Base)))),
00089 size(bs.size) {
00090 for (int i = size; i--; ) data[i] = bs.data[i];
00091 }
00092 forceinline bool
00093 BitSet::get(unsigned int i) {
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);
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