Bitset.hpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006 #ifndef BITSET_HPP
00007 #define BITSET_HPP
00008
00009 #include <cassert>
00010 #include <set>
00011 #include <string>
00012 #include <vector>
00013
00014 #include "Benzene.hpp"
00015 #include "Types.hpp"
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #define USE_HOMEBREWED_BITSET 1
00026
00027 #if USE_HOMEBREWED_BITSET
00028 #include "BenzeneBitset.hpp"
00029 #else
00030 #include <bitset>
00031 #endif
00032
00033 _BEGIN_BENZENE_NAMESPACE_
00034
00035
00036
00037
00038
00039
00040
00041 #if defined(SUPPORT_19x19)
00042
00043
00044 static const int BITSETSIZE = 384;
00045
00046 #elif defined(SUPPORT_14x14)
00047
00048
00049 static const int BITSETSIZE = 224;
00050
00051 #elif defined(SUPPORT_13x13)
00052
00053
00054 static const int BITSETSIZE = 192;
00055
00056 #else
00057
00058
00059 static const int BITSETSIZE = 128;
00060
00061 #endif
00062
00063
00064
00065
00066 typedef benzene_bitset<BITSETSIZE> bitset_t;
00067
00068
00069 static const bitset_t EMPTY_BITSET;
00070
00071
00072
00073
00074 namespace BitsetUtil
00075 {
00076
00077 void BitsetToBytes(const bitset_t& b, byte* out, int numbits);
00078
00079
00080 bitset_t BytesToBitset(const byte* bytes, int numbits);
00081
00082
00083 std::string BitsetToHex(const bitset_t& b, int numbits);
00084
00085
00086 bitset_t HexToBitset(const std::string& str);
00087
00088
00089
00090
00091 bitset_t Subtract(const bitset_t& b1, const bitset_t& b2);
00092
00093
00094
00095
00096 bool SubtractIfLeavesAny(bitset_t& removeFrom, const bitset_t& remove);
00097
00098
00099 bool IsSubsetOf(const bitset_t& b1, const bitset_t& b2);
00100
00101
00102
00103 bool IsLessThan(const bitset_t& b1, const bitset_t& b2);
00104
00105
00106
00107
00108
00109
00110 template<typename INT>
00111 void BitsetToVector(const bitset_t& b, std::vector<INT>& indices);
00112
00113
00114
00115
00116 template<typename INT>
00117 bitset_t SetToBitset(const std::set<INT>& indices);
00118
00119
00120
00121
00122 int FindSetBit(const bitset_t& b);
00123
00124
00125 int FirstSetBit(const bitset_t& b);
00126 }
00127
00128
00129
00130 template<typename INT>
00131 void BitsetUtil::BitsetToVector(const bitset_t& b,
00132 std::vector<INT>& indices)
00133 {
00134 indices.clear();
00135 for (int i=0; i<BITSETSIZE; i++) {
00136 if (b.test(i))
00137 indices.push_back(static_cast<INT>(i));
00138 }
00139 assert(b.count() == indices.size());
00140 }
00141
00142 template<typename INT>
00143 bitset_t BitsetUtil::SetToBitset(const std::set<INT>& indices)
00144 {
00145 bitset_t ret;
00146 typedef typename std::set<INT>::const_iterator const_iterator;
00147 for (const_iterator it=indices.begin();
00148 it != indices.end();
00149 ++it)
00150 {
00151 ret.set(static_cast<int>(*it));
00152 }
00153 return ret;
00154 }
00155
00156
00157
00158 inline bool BitsetUtil::IsSubsetOf(const bitset_t& b1, const bitset_t& b2)
00159 {
00160 #if USE_HOMEBREWED_BITSET
00161 return b1.is_subset_of(b2);
00162 #else
00163 return ((b1 & b2) == b1);
00164 #endif
00165 }
00166
00167 inline bool BitsetUtil::IsLessThan(const bitset_t& b1, const bitset_t& b2)
00168 {
00169 #if USE_HOMEBREWED_BITSET
00170 return b1.is_less_than(b2);
00171 #else
00172
00173 for (int i = 0; i < BITSETSIZE; ++i)
00174 {
00175 if (b1[i] != b2[i])
00176 return !b1[i];
00177 }
00178 #endif
00179 }
00180
00181
00182
00183
00184 bitset_t operator-(const bitset_t& b1, const bitset_t& b2);
00185
00186
00187
00188 _END_BENZENE_NAMESPACE_
00189
00190 #endif // BITSET_HPP