/usr/include/lexertl/containers/bitvector.hpp is in libpuma-dev 1:2.1-2+b1.
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 | // bitvector.hpp
// Copyright (c) 2013-2015 Ben Hanson (http://www.benhanson.net/)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file licence_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef LEXERTL_BITVECTOR_HPP
#define LEXERTL_BITVECTOR_HPP
#include <vector>
namespace lexertl
{
template<typename T>
class basic_bitvector
{
public:
template<typename Ty>
class reference
{
public:
reference(Ty &block_, const std::size_t mask_) :
_block(block_),
_mask(mask_)
{
}
operator bool() const
{
return (_block & _mask) != 0;
}
reference<Ty> &operator =(const bool bit_)
{
if (bit_)
{
_block |= _mask;
}
else
{
_block &= ~_mask;
}
return *this;
}
reference<Ty> &operator =(reference<Ty> &rhs_)
{
if (rhs_)
{
_block |= _mask;
}
else
{
_block &= ~_mask;
}
}
private:
Ty &_block;
const std::size_t _mask;
};
basic_bitvector(const std::size_t size_) :
_vec(block(size_) + (bit(size_) ? 1 : 0), 0)
{
}
basic_bitvector(const basic_bitvector &rhs_) :
_vec(rhs_._vec)
{
}
basic_bitvector &operator =(const basic_bitvector &rhs_)
{
if (&rhs_ != this)
{
_vec = rhs_._vec;
}
return *this;
}
bool operator [](const std::size_t index_) const
{
return (_vec[block(index_)] & (1 << bit(index_))) != 0;
}
reference<T> operator [](const std::size_t index_)
{
return reference<T>(_vec[block(index_)], (1 << bit(index_)));
}
basic_bitvector<T> &operator |=(const basic_bitvector<T> &rhs_)
{
typename t_vector::iterator lhs_iter_ = _vec.begin();
typename t_vector::iterator lhs_end_ = _vec.end();
typename t_vector::const_iterator rhs_iter_ = rhs_._vec.begin();
typename t_vector::const_iterator rhs_end_ = rhs_._vec.end();
for (; lhs_iter_ != lhs_end_ && rhs_iter_ != rhs_end_;
++lhs_iter_, ++rhs_iter_)
{
*lhs_iter_ |= *rhs_iter_;
}
return *this;
}
basic_bitvector<T> &operator &=(const basic_bitvector<T> &rhs_)
{
typename t_vector::iterator lhs_iter_ = _vec.begin();
typename t_vector::iterator lhs_end_ = _vec.end();
typename t_vector::const_iterator rhs_iter_ = rhs_._vec.begin();
typename t_vector::const_iterator rhs_end_ = rhs_._vec.end();
for (; lhs_iter_ != lhs_end_ && rhs_iter_ != rhs_end_;
++lhs_iter_, ++rhs_iter_)
{
*lhs_iter_ &= *rhs_iter_;
}
return *this;
}
void clear()
{
typename t_vector::iterator iter_ = _vec.begin();
typename t_vector::iterator end_ = _vec.end();
for (; iter_ != end_; ++iter_)
{
*iter_ = 0;
}
}
bool any() const
{
typename t_vector::const_iterator iter_ = _vec.begin();
typename t_vector::const_iterator end_ = _vec.end();
for (; iter_ != end_; ++iter_)
{
if (*iter_) break;
}
return iter_ != end_;
}
void negate()
{
typename t_vector::iterator iter_ = _vec.begin();
typename t_vector::iterator end_ = _vec.end();
for (; iter_ != end_; ++iter_)
{
*iter_ = ~*iter_;
}
}
std::size_t find_first() const
{
return find_next(npos());
}
std::size_t find_next(const std::size_t index_) const
{
std::size_t ret_ = npos();
const std::size_t block_ = index_ == npos() ? 0 : block(index_ + 1);
std::size_t bit_ = index_ == npos() ? 0 : bit(index_ + 1);
typename t_vector::const_iterator iter_ = _vec.begin() + block_;
typename t_vector::const_iterator end_ = _vec.end();
for (std::size_t i_ = block_; iter_ != end_; ++iter_, ++i_)
{
const bool bits_ = (*iter_ & (~static_cast<T>(0) << bit_)) != 0;
if (bits_)
{
std::size_t j_ = bit_;
std::size_t b_ = 1 << bit_;
bool found_ = false;
for (; j_ < sizeof(T) * 8; ++j_, b_ <<= 1)
{
if (*iter_ & b_)
{
found_ = true;
break;
}
}
if (found_)
{
ret_ = i_ * sizeof(T) * 8 + j_;
break;
}
}
bit_ = 0;
}
return ret_;
}
std::size_t npos() const
{
return ~static_cast<std::size_t>(0);
}
private:
typedef std::vector<T> t_vector;
t_vector _vec;
std::size_t block(const std::size_t index_) const
{
return index_ / (sizeof(T) * 8);
}
std::size_t bit(const std::size_t index_) const
{
return index_ % (sizeof(T) * 8);
}
};
}
#endif
|