/usr/include/vigra/bit_array.hxx is in libvigraimpex-dev 1.9.0+dfsg-10+b2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | #ifndef VIGRA_BIT_ARRAY_HXX
#define VIGRA_BIT_ARRAY_HXX
#include <functional>
#include <ostream>
#include "metaprogramming.hxx"
namespace vigra {
template <class> // undefined class to provoke usable error messages
class vigra_error_BitArray_accepts_only_unsigned_underlying_types_and_no_;
template <unsigned SIZE, class X> // bitwise operators do not necessarily work for bool
struct EnableBitArray
: public enable_if<(HasMetaLog2<X>::value && !IsSameType<X, bool>::value && SIZE > 0)> {};
// BitArray: a minimal subset of std::bitset with the extension of compile-time
// access functions set<unsigned>(), test<unsigned>(), reset<unsigned>(), and
// flip<unsigned>(), plus all relational operators;
// furthermore, there are no range checks.
template <unsigned SIZE, class WORD_TYPE = unsigned, class = void>
class BitArray
: public
vigra_error_BitArray_accepts_only_unsigned_underlying_types_and_no_
<WORD_TYPE>
{};
template <unsigned SIZE, class WORD_TYPE>
class BitArray<SIZE, WORD_TYPE, typename EnableBitArray<SIZE, WORD_TYPE>::type>
{
// 'unsigned' will be the most efficent word type for most CPUs,
// since very long immediates such as a possible 64 bit 'unsigned long'
// are slower for many typical uses of BitArray
protected:
static const unsigned bit_size = SIZE;
static const unsigned word_len = MetaLog2<WORD_TYPE>::value;
static const unsigned array_len = (bit_size + word_len - 1) / word_len;
static const unsigned last_pos = array_len - 1;
template <unsigned pos>
struct bit_index
{
static const unsigned word_pos = pos / word_len;
static const unsigned bit_pos = pos % word_len;
static const WORD_TYPE bit_mask = WORD_TYPE(1) << bit_pos;
};
typedef bit_index<bit_size> size_index;
static const WORD_TYPE ones_mask = ~(WORD_TYPE(0));
static const unsigned border_pos = size_index::bit_pos;
static const WORD_TYPE last_mask = !border_pos ? 0
: size_index::bit_mask - 1;
static const bool does_fit = border_pos == 0;
unsigned word_pos(unsigned pos) const
{
return pos / word_len;
};
WORD_TYPE bit_mask(unsigned pos) const
{
return WORD_TYPE(1) << (pos % word_len); // the compiler knows as well..
};
WORD_TYPE set_bits[array_len];
public:
unsigned size()
{
return bit_size;
}
void clear()
{
for (unsigned i = 0; i != array_len; ++i)
set_bits[i] = 0;
}
BitArray()
{
clear();
}
template <unsigned pos>
void set()
{
typedef bit_index<pos> index;
set_bits[index::word_pos] |= index::bit_mask;
}
template <unsigned pos>
void reset()
{
typedef bit_index<pos> index;
set_bits[index::word_pos] &= ~index::bit_mask;
}
template <unsigned pos>
void flip()
{
typedef bit_index<pos> index;
set_bits[index::word_pos] ^= index::bit_mask;
}
template <unsigned pos>
bool test() const
{
typedef bit_index<pos> index;
return (set_bits[index::word_pos] & index::bit_mask) != 0;
}
BitArray & set(unsigned pos, bool value = true)
{
(set_bits[word_pos(pos)] &= ~bit_mask(pos))
|= value ? bit_mask(pos) : 0;
return *this;
}
BitArray & reset(unsigned pos)
{
set_bits[word_pos(pos)] &= ~bit_mask(pos);
return *this;
}
BitArray & flip(unsigned pos)
{
set_bits[word_pos(pos)] ^= bit_mask(pos);
return *this;
}
bool test(unsigned pos) const
{
return set_bits[word_pos(pos)] & bit_mask(pos);
}
bool operator[](unsigned pos) const
{
return test(pos);
}
BitArray & set()
{
for (unsigned i = 0; i != last_pos + does_fit; ++i)
set_bits[i] = ones_mask;
if (!does_fit)
set_bits[last_pos] = last_mask;
return *this;
}
BitArray & reset()
{
for (unsigned i = 0; i != array_len; ++i)
set_bits[i] = 0;
return *this;
}
BitArray & flip()
{
for (unsigned i = 0; i != last_pos + does_fit; ++i)
set_bits[i] ^= ones_mask;
if (!does_fit)
set_bits[last_pos] ^= last_mask;
return *this;
}
operator bool() const
{
for (unsigned i = 0; i != array_len; ++i)
if (set_bits[i] != 0)
return true;
return false;
}
bool operator!() const
{
return !bool(*this);
}
bool any() const
{
return *this;
}
bool none() const
{
return !*this;
}
bool all() const
{
for (unsigned i = 0; i != last_pos + does_fit; ++i)
if (set_bits[i] != ones_mask)
return false;
if (!does_fit)
return set_bits[last_pos] == last_mask;
return true;
}
BitArray operator~() const
{
BitArray x(*this);
x.flip();
return x;
}
protected:
template <class F>
bool mutual_compare(const BitArray & t, F f, bool if_equal = false) const
{
for (int i = last_pos; i >= 0; i--)
{
WORD_TYPE x = set_bits[i];
WORD_TYPE y = t.set_bits[i];
if (f(x, y))
return true;
if (f(y, x))
return false;
}
return if_equal;
}
typedef std::less<WORD_TYPE> less;
typedef std::greater<WORD_TYPE> greater;
public:
bool operator<(const BitArray & t) const
{
return mutual_compare(t, less());
}
bool operator>(const BitArray & t) const
{
return mutual_compare(t, greater());
}
bool operator<=(const BitArray & t) const
{
return mutual_compare(t, less(), true);
}
bool operator>=(const BitArray & t) const
{
return mutual_compare(t, greater(), true);
}
bool operator!=(const BitArray & t) const
{
for (unsigned i = 0; i != array_len; ++i)
if (set_bits[i] != t.set_bits[i])
return true;
return false;
}
bool operator==(const BitArray & t) const
{
return !operator!=(t);
}
protected:
struct bit_and_assign
{
static void assign(WORD_TYPE & a, WORD_TYPE b) { a &= b; }
};
struct exclusive_or_assign
{
static void assign(WORD_TYPE & a, WORD_TYPE b) { a ^= b; }
};
struct bit_or_assign
{
static void assign(WORD_TYPE & a, WORD_TYPE b) { a |= b; }
};
template <class A>
BitArray & assign_operator(const BitArray & x)
{
for (unsigned i = 0; i != array_len; ++i)
A::assign(set_bits[i], x.set_bits[i]);
return *this;
}
public:
BitArray & operator&=(const BitArray & x)
{
return assign_operator<bit_and_assign>(x);
}
BitArray & operator^=(const BitArray & x)
{
return assign_operator<exclusive_or_assign>(x);
}
BitArray & operator|=(const BitArray & x)
{
return assign_operator<bit_or_assign>(x);
}
protected:
template <class A>
BitArray & bit_operator(const BitArray & y) const
{
BitArray x(*this);
return x.assign_operator<A>(y);
}
public:
BitArray operator&(const BitArray & y) const
{
return bit_operator<bit_and_assign>(y);
}
BitArray operator^(const BitArray & y) const
{
return bit_operator<exclusive_or_assign>(y);
}
BitArray operator|(const BitArray & y) const
{
return bit_operator<bit_or_assign>(y);
}
bool operator&&(const BitArray & y) const
{
return *this && y;
}
bool operator||(const BitArray & y) const
{
return *this || y;
}
friend std::ostream & operator<<(std::ostream & os, const BitArray & z)
{
for (int i = bit_size - 1; i >= 0; i--)
os << (z[i] ? "1" : "0");
return os;
}
};
// work around GCC's zero-sized array extension
template <class WORD_TYPE>
class BitArray<0, WORD_TYPE>
{
// bool error[-(long int)sizeof(WORD_TYPE)];
void clear() {}
};
} // namespace vigra
#endif // VIGRA_BIT_ARRAY_HXX
|