Chris@16
|
1 // -----------------------------------------------------------
|
Chris@16
|
2 //
|
Chris@16
|
3 // Copyright (c) 2001-2002 Chuck Allison and Jeremy Siek
|
Chris@16
|
4 // Copyright (c) 2003-2006, 2008 Gennaro Prota
|
Chris@16
|
5 //
|
Chris@16
|
6 // Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
7 // (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
8 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
9 //
|
Chris@16
|
10 // -----------------------------------------------------------
|
Chris@16
|
11
|
Chris@16
|
12 #ifndef BOOST_DYNAMIC_BITSET_DYNAMIC_BITSET_HPP
|
Chris@16
|
13 #define BOOST_DYNAMIC_BITSET_DYNAMIC_BITSET_HPP
|
Chris@16
|
14
|
Chris@16
|
15 #include <assert.h>
|
Chris@16
|
16 #include <string>
|
Chris@16
|
17 #include <stdexcept>
|
Chris@16
|
18 #include <algorithm>
|
Chris@16
|
19 #include <vector>
|
Chris@16
|
20 #include <climits> // for CHAR_BIT
|
Chris@16
|
21
|
Chris@16
|
22 #include "boost/dynamic_bitset/config.hpp"
|
Chris@16
|
23
|
Chris@16
|
24 #ifndef BOOST_NO_STD_LOCALE
|
Chris@16
|
25 # include <locale>
|
Chris@16
|
26 #endif
|
Chris@16
|
27
|
Chris@16
|
28 #if defined(BOOST_OLD_IOSTREAMS)
|
Chris@16
|
29 # include <iostream.h>
|
Chris@16
|
30 # include <ctype.h> // for isspace
|
Chris@16
|
31 #else
|
Chris@16
|
32 # include <istream>
|
Chris@16
|
33 # include <ostream>
|
Chris@16
|
34 #endif
|
Chris@16
|
35
|
Chris@16
|
36 #include "boost/dynamic_bitset_fwd.hpp"
|
Chris@16
|
37 #include "boost/detail/dynamic_bitset.hpp"
|
Chris@16
|
38 #include "boost/detail/iterator.hpp" // used to implement append(Iter, Iter)
|
Chris@16
|
39 #include "boost/static_assert.hpp"
|
Chris@16
|
40 #include "boost/limits.hpp"
|
Chris@16
|
41 #include "boost/pending/lowest_bit.hpp"
|
Chris@16
|
42
|
Chris@16
|
43
|
Chris@16
|
44 namespace boost {
|
Chris@16
|
45
|
Chris@16
|
46 template <typename Block, typename Allocator>
|
Chris@16
|
47 class dynamic_bitset
|
Chris@16
|
48 {
|
Chris@16
|
49 // Portability note: member function templates are defined inside
|
Chris@16
|
50 // this class definition to avoid problems with VC++. Similarly,
|
Chris@16
|
51 // with the member functions of nested classes.
|
Chris@16
|
52 //
|
Chris@16
|
53 // [October 2008: the note above is mostly historical; new versions
|
Chris@16
|
54 // of VC++ are likely able to digest a more drinking form of the
|
Chris@16
|
55 // code; but changing it now is probably not worth the risks...]
|
Chris@16
|
56
|
Chris@16
|
57 BOOST_STATIC_ASSERT((bool)detail::dynamic_bitset_impl::allowed_block_type<Block>::value);
|
Chris@16
|
58
|
Chris@16
|
59 public:
|
Chris@16
|
60 typedef Block block_type;
|
Chris@16
|
61 typedef Allocator allocator_type;
|
Chris@16
|
62 typedef std::size_t size_type;
|
Chris@16
|
63 typedef block_type block_width_type;
|
Chris@16
|
64
|
Chris@16
|
65 BOOST_STATIC_CONSTANT(block_width_type, bits_per_block = (std::numeric_limits<Block>::digits));
|
Chris@16
|
66 BOOST_STATIC_CONSTANT(size_type, npos = static_cast<size_type>(-1));
|
Chris@16
|
67
|
Chris@16
|
68
|
Chris@16
|
69 public:
|
Chris@16
|
70
|
Chris@16
|
71 // A proxy class to simulate lvalues of bit type.
|
Chris@16
|
72 //
|
Chris@16
|
73 class reference
|
Chris@16
|
74 {
|
Chris@16
|
75 friend class dynamic_bitset<Block, Allocator>;
|
Chris@16
|
76
|
Chris@16
|
77
|
Chris@16
|
78 // the one and only non-copy ctor
|
Chris@16
|
79 reference(block_type & b, block_type pos)
|
Chris@16
|
80 :m_block(b),
|
Chris@16
|
81 m_mask( (assert(pos < bits_per_block),
|
Chris@16
|
82 block_type(1) << pos )
|
Chris@16
|
83 )
|
Chris@16
|
84 { }
|
Chris@16
|
85
|
Chris@16
|
86 void operator&(); // left undefined
|
Chris@16
|
87
|
Chris@16
|
88 public:
|
Chris@16
|
89
|
Chris@16
|
90 // copy constructor: compiler generated
|
Chris@16
|
91
|
Chris@16
|
92 operator bool() const { return (m_block & m_mask) != 0; }
|
Chris@16
|
93 bool operator~() const { return (m_block & m_mask) == 0; }
|
Chris@16
|
94
|
Chris@16
|
95 reference& flip() { do_flip(); return *this; }
|
Chris@16
|
96
|
Chris@16
|
97 reference& operator=(bool x) { do_assign(x); return *this; } // for b[i] = x
|
Chris@16
|
98 reference& operator=(const reference& rhs) { do_assign(rhs); return *this; } // for b[i] = b[j]
|
Chris@16
|
99
|
Chris@16
|
100 reference& operator|=(bool x) { if (x) do_set(); return *this; }
|
Chris@16
|
101 reference& operator&=(bool x) { if (!x) do_reset(); return *this; }
|
Chris@16
|
102 reference& operator^=(bool x) { if (x) do_flip(); return *this; }
|
Chris@16
|
103 reference& operator-=(bool x) { if (x) do_reset(); return *this; }
|
Chris@16
|
104
|
Chris@16
|
105 private:
|
Chris@16
|
106 block_type & m_block;
|
Chris@16
|
107 const block_type m_mask;
|
Chris@16
|
108
|
Chris@16
|
109 void do_set() { m_block |= m_mask; }
|
Chris@16
|
110 void do_reset() { m_block &= ~m_mask; }
|
Chris@16
|
111 void do_flip() { m_block ^= m_mask; }
|
Chris@16
|
112 void do_assign(bool x) { x? do_set() : do_reset(); }
|
Chris@16
|
113 };
|
Chris@16
|
114
|
Chris@16
|
115 typedef bool const_reference;
|
Chris@16
|
116
|
Chris@16
|
117 // constructors, etc.
|
Chris@16
|
118 explicit
|
Chris@16
|
119 dynamic_bitset(const Allocator& alloc = Allocator());
|
Chris@16
|
120
|
Chris@16
|
121 explicit
|
Chris@16
|
122 dynamic_bitset(size_type num_bits, unsigned long value = 0,
|
Chris@16
|
123 const Allocator& alloc = Allocator());
|
Chris@16
|
124
|
Chris@16
|
125
|
Chris@16
|
126 // WARNING: you should avoid using this constructor.
|
Chris@16
|
127 //
|
Chris@16
|
128 // A conversion from string is, in most cases, formatting,
|
Chris@16
|
129 // and should be performed by using operator>>.
|
Chris@16
|
130 //
|
Chris@16
|
131 // NOTE:
|
Chris@16
|
132 // Leave the parentheses around std::basic_string<CharT, Traits, Alloc>::npos.
|
Chris@16
|
133 // g++ 3.2 requires them and probably the standard will - see core issue 325
|
Chris@16
|
134 // NOTE 2:
|
Chris@16
|
135 // split into two constructors because of bugs in MSVC 6.0sp5 with STLport
|
Chris@16
|
136
|
Chris@16
|
137 template <typename CharT, typename Traits, typename Alloc>
|
Chris@16
|
138 dynamic_bitset(const std::basic_string<CharT, Traits, Alloc>& s,
|
Chris@16
|
139 typename std::basic_string<CharT, Traits, Alloc>::size_type pos,
|
Chris@16
|
140 typename std::basic_string<CharT, Traits, Alloc>::size_type n,
|
Chris@16
|
141 size_type num_bits = npos,
|
Chris@16
|
142 const Allocator& alloc = Allocator())
|
Chris@16
|
143
|
Chris@16
|
144 :m_bits(alloc),
|
Chris@16
|
145 m_num_bits(0)
|
Chris@16
|
146 {
|
Chris@16
|
147 init_from_string(s, pos, n, num_bits);
|
Chris@16
|
148 }
|
Chris@16
|
149
|
Chris@16
|
150 template <typename CharT, typename Traits, typename Alloc>
|
Chris@16
|
151 explicit
|
Chris@16
|
152 dynamic_bitset(const std::basic_string<CharT, Traits, Alloc>& s,
|
Chris@16
|
153 typename std::basic_string<CharT, Traits, Alloc>::size_type pos = 0)
|
Chris@16
|
154
|
Chris@16
|
155 :m_bits(Allocator()),
|
Chris@16
|
156 m_num_bits(0)
|
Chris@16
|
157 {
|
Chris@16
|
158 init_from_string(s, pos, (std::basic_string<CharT, Traits, Alloc>::npos),
|
Chris@16
|
159 npos);
|
Chris@16
|
160 }
|
Chris@16
|
161
|
Chris@16
|
162 // The first bit in *first is the least significant bit, and the
|
Chris@16
|
163 // last bit in the block just before *last is the most significant bit.
|
Chris@16
|
164 template <typename BlockInputIterator>
|
Chris@16
|
165 dynamic_bitset(BlockInputIterator first, BlockInputIterator last,
|
Chris@16
|
166 const Allocator& alloc = Allocator())
|
Chris@16
|
167
|
Chris@16
|
168 :m_bits(alloc),
|
Chris@16
|
169 m_num_bits(0)
|
Chris@16
|
170 {
|
Chris@16
|
171 using boost::detail::dynamic_bitset_impl::value_to_type;
|
Chris@16
|
172 using boost::detail::dynamic_bitset_impl::is_numeric;
|
Chris@16
|
173
|
Chris@16
|
174 const value_to_type<
|
Chris@16
|
175 is_numeric<BlockInputIterator>::value> selector;
|
Chris@16
|
176
|
Chris@16
|
177 dispatch_init(first, last, selector);
|
Chris@16
|
178 }
|
Chris@16
|
179
|
Chris@16
|
180 template <typename T>
|
Chris@16
|
181 void dispatch_init(T num_bits, unsigned long value,
|
Chris@16
|
182 detail::dynamic_bitset_impl::value_to_type<true>)
|
Chris@16
|
183 {
|
Chris@16
|
184 init_from_unsigned_long(static_cast<size_type>(num_bits), value);
|
Chris@16
|
185 }
|
Chris@16
|
186
|
Chris@16
|
187 template <typename T>
|
Chris@16
|
188 void dispatch_init(T first, T last,
|
Chris@16
|
189 detail::dynamic_bitset_impl::value_to_type<false>)
|
Chris@16
|
190 {
|
Chris@16
|
191 init_from_block_range(first, last);
|
Chris@16
|
192 }
|
Chris@16
|
193
|
Chris@16
|
194 template <typename BlockIter>
|
Chris@16
|
195 void init_from_block_range(BlockIter first, BlockIter last)
|
Chris@16
|
196 {
|
Chris@16
|
197 assert(m_bits.size() == 0);
|
Chris@16
|
198 m_bits.insert(m_bits.end(), first, last);
|
Chris@16
|
199 m_num_bits = m_bits.size() * bits_per_block;
|
Chris@16
|
200 }
|
Chris@16
|
201
|
Chris@16
|
202 // copy constructor
|
Chris@16
|
203 dynamic_bitset(const dynamic_bitset& b);
|
Chris@16
|
204
|
Chris@16
|
205 ~dynamic_bitset();
|
Chris@16
|
206
|
Chris@16
|
207 void swap(dynamic_bitset& b);
|
Chris@16
|
208 dynamic_bitset& operator=(const dynamic_bitset& b);
|
Chris@16
|
209
|
Chris@16
|
210 allocator_type get_allocator() const;
|
Chris@16
|
211
|
Chris@16
|
212 // size changing operations
|
Chris@16
|
213 void resize(size_type num_bits, bool value = false);
|
Chris@16
|
214 void clear();
|
Chris@16
|
215 void push_back(bool bit);
|
Chris@16
|
216 void append(Block block);
|
Chris@16
|
217
|
Chris@16
|
218 template <typename BlockInputIterator>
|
Chris@16
|
219 void m_append(BlockInputIterator first, BlockInputIterator last, std::input_iterator_tag)
|
Chris@16
|
220 {
|
Chris@16
|
221 std::vector<Block, Allocator> v(first, last);
|
Chris@16
|
222 m_append(v.begin(), v.end(), std::random_access_iterator_tag());
|
Chris@16
|
223 }
|
Chris@16
|
224 template <typename BlockInputIterator>
|
Chris@16
|
225 void m_append(BlockInputIterator first, BlockInputIterator last, std::forward_iterator_tag)
|
Chris@16
|
226 {
|
Chris@16
|
227 assert(first != last);
|
Chris@16
|
228 block_width_type r = count_extra_bits();
|
Chris@16
|
229 std::size_t d = boost::detail::distance(first, last);
|
Chris@16
|
230 m_bits.reserve(num_blocks() + d);
|
Chris@16
|
231 if (r == 0) {
|
Chris@16
|
232 for( ; first != last; ++first)
|
Chris@16
|
233 m_bits.push_back(*first); // could use vector<>::insert()
|
Chris@16
|
234 }
|
Chris@16
|
235 else {
|
Chris@16
|
236 m_highest_block() |= (*first << r);
|
Chris@16
|
237 do {
|
Chris@16
|
238 Block b = *first >> (bits_per_block - r);
|
Chris@16
|
239 ++first;
|
Chris@16
|
240 m_bits.push_back(b | (first==last? 0 : *first << r));
|
Chris@16
|
241 } while (first != last);
|
Chris@16
|
242 }
|
Chris@16
|
243 m_num_bits += bits_per_block * d;
|
Chris@16
|
244 }
|
Chris@16
|
245 template <typename BlockInputIterator>
|
Chris@16
|
246 void append(BlockInputIterator first, BlockInputIterator last) // strong guarantee
|
Chris@16
|
247 {
|
Chris@16
|
248 if (first != last) {
|
Chris@16
|
249 typename detail::iterator_traits<BlockInputIterator>::iterator_category cat;
|
Chris@16
|
250 m_append(first, last, cat);
|
Chris@16
|
251 }
|
Chris@16
|
252 }
|
Chris@16
|
253
|
Chris@16
|
254
|
Chris@16
|
255 // bitset operations
|
Chris@16
|
256 dynamic_bitset& operator&=(const dynamic_bitset& b);
|
Chris@16
|
257 dynamic_bitset& operator|=(const dynamic_bitset& b);
|
Chris@16
|
258 dynamic_bitset& operator^=(const dynamic_bitset& b);
|
Chris@16
|
259 dynamic_bitset& operator-=(const dynamic_bitset& b);
|
Chris@16
|
260 dynamic_bitset& operator<<=(size_type n);
|
Chris@16
|
261 dynamic_bitset& operator>>=(size_type n);
|
Chris@16
|
262 dynamic_bitset operator<<(size_type n) const;
|
Chris@16
|
263 dynamic_bitset operator>>(size_type n) const;
|
Chris@16
|
264
|
Chris@16
|
265 // basic bit operations
|
Chris@16
|
266 dynamic_bitset& set(size_type n, bool val = true);
|
Chris@16
|
267 dynamic_bitset& set();
|
Chris@16
|
268 dynamic_bitset& reset(size_type n);
|
Chris@16
|
269 dynamic_bitset& reset();
|
Chris@16
|
270 dynamic_bitset& flip(size_type n);
|
Chris@16
|
271 dynamic_bitset& flip();
|
Chris@16
|
272 bool test(size_type n) const;
|
Chris@16
|
273 bool any() const;
|
Chris@16
|
274 bool none() const;
|
Chris@16
|
275 dynamic_bitset operator~() const;
|
Chris@16
|
276 size_type count() const;
|
Chris@16
|
277
|
Chris@16
|
278 // subscript
|
Chris@16
|
279 reference operator[](size_type pos) {
|
Chris@16
|
280 return reference(m_bits[block_index(pos)], bit_index(pos));
|
Chris@16
|
281 }
|
Chris@16
|
282 bool operator[](size_type pos) const { return test(pos); }
|
Chris@16
|
283
|
Chris@16
|
284 unsigned long to_ulong() const;
|
Chris@16
|
285
|
Chris@16
|
286 size_type size() const;
|
Chris@16
|
287 size_type num_blocks() const;
|
Chris@16
|
288 size_type max_size() const;
|
Chris@16
|
289 bool empty() const;
|
Chris@16
|
290
|
Chris@16
|
291 bool is_subset_of(const dynamic_bitset& a) const;
|
Chris@16
|
292 bool is_proper_subset_of(const dynamic_bitset& a) const;
|
Chris@16
|
293 bool intersects(const dynamic_bitset & a) const;
|
Chris@16
|
294
|
Chris@16
|
295 // lookup
|
Chris@16
|
296 size_type find_first() const;
|
Chris@16
|
297 size_type find_next(size_type pos) const;
|
Chris@16
|
298
|
Chris@16
|
299
|
Chris@16
|
300 #if !defined BOOST_DYNAMIC_BITSET_DONT_USE_FRIENDS
|
Chris@16
|
301 // lexicographical comparison
|
Chris@16
|
302 template <typename B, typename A>
|
Chris@16
|
303 friend bool operator==(const dynamic_bitset<B, A>& a,
|
Chris@16
|
304 const dynamic_bitset<B, A>& b);
|
Chris@16
|
305
|
Chris@16
|
306 template <typename B, typename A>
|
Chris@16
|
307 friend bool operator<(const dynamic_bitset<B, A>& a,
|
Chris@16
|
308 const dynamic_bitset<B, A>& b);
|
Chris@16
|
309
|
Chris@16
|
310
|
Chris@16
|
311 template <typename B, typename A, typename BlockOutputIterator>
|
Chris@16
|
312 friend void to_block_range(const dynamic_bitset<B, A>& b,
|
Chris@16
|
313 BlockOutputIterator result);
|
Chris@16
|
314
|
Chris@16
|
315 template <typename BlockIterator, typename B, typename A>
|
Chris@16
|
316 friend void from_block_range(BlockIterator first, BlockIterator last,
|
Chris@16
|
317 dynamic_bitset<B, A>& result);
|
Chris@16
|
318
|
Chris@16
|
319
|
Chris@16
|
320 template <typename CharT, typename Traits, typename B, typename A>
|
Chris@16
|
321 friend std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& is,
|
Chris@16
|
322 dynamic_bitset<B, A>& b);
|
Chris@16
|
323
|
Chris@16
|
324 template <typename B, typename A, typename stringT>
|
Chris@16
|
325 friend void to_string_helper(const dynamic_bitset<B, A> & b, stringT & s, bool dump_all);
|
Chris@16
|
326
|
Chris@16
|
327
|
Chris@16
|
328 #endif
|
Chris@16
|
329
|
Chris@16
|
330
|
Chris@16
|
331 private:
|
Chris@16
|
332 BOOST_STATIC_CONSTANT(block_width_type, ulong_width = std::numeric_limits<unsigned long>::digits);
|
Chris@16
|
333 typedef std::vector<block_type, allocator_type> buffer_type;
|
Chris@16
|
334
|
Chris@16
|
335 void m_zero_unused_bits();
|
Chris@16
|
336 bool m_check_invariants() const;
|
Chris@16
|
337
|
Chris@16
|
338 size_type m_do_find_from(size_type first_block) const;
|
Chris@16
|
339
|
Chris@16
|
340 block_width_type count_extra_bits() const { return bit_index(size()); }
|
Chris@16
|
341 static size_type block_index(size_type pos) { return pos / bits_per_block; }
|
Chris@16
|
342 static block_width_type bit_index(size_type pos) { return static_cast<block_width_type>(pos % bits_per_block); }
|
Chris@16
|
343 static Block bit_mask(size_type pos) { return Block(1) << bit_index(pos); }
|
Chris@16
|
344
|
Chris@16
|
345 template <typename CharT, typename Traits, typename Alloc>
|
Chris@16
|
346 void init_from_string(const std::basic_string<CharT, Traits, Alloc>& s,
|
Chris@16
|
347 typename std::basic_string<CharT, Traits, Alloc>::size_type pos,
|
Chris@16
|
348 typename std::basic_string<CharT, Traits, Alloc>::size_type n,
|
Chris@16
|
349 size_type num_bits)
|
Chris@16
|
350 {
|
Chris@16
|
351 assert(pos <= s.size());
|
Chris@16
|
352
|
Chris@16
|
353 typedef typename std::basic_string<CharT, Traits, Alloc> StrT;
|
Chris@16
|
354 typedef typename StrT::traits_type Tr;
|
Chris@16
|
355
|
Chris@16
|
356 const typename StrT::size_type rlen = (std::min)(n, s.size() - pos);
|
Chris@16
|
357 const size_type sz = ( num_bits != npos? num_bits : rlen);
|
Chris@16
|
358 m_bits.resize(calc_num_blocks(sz));
|
Chris@16
|
359 m_num_bits = sz;
|
Chris@16
|
360
|
Chris@16
|
361
|
Chris@16
|
362 BOOST_DYNAMIC_BITSET_CTYPE_FACET(CharT, fac, std::locale());
|
Chris@16
|
363 const CharT one = BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '1');
|
Chris@16
|
364
|
Chris@16
|
365 const size_type m = num_bits < rlen ? num_bits : rlen;
|
Chris@16
|
366 typename StrT::size_type i = 0;
|
Chris@16
|
367 for( ; i < m; ++i) {
|
Chris@16
|
368
|
Chris@16
|
369 const CharT c = s[(pos + m - 1) - i];
|
Chris@16
|
370
|
Chris@16
|
371 assert( Tr::eq(c, one)
|
Chris@16
|
372 || Tr::eq(c, BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '0')) );
|
Chris@16
|
373
|
Chris@16
|
374 if (Tr::eq(c, one))
|
Chris@16
|
375 set(i);
|
Chris@16
|
376
|
Chris@16
|
377 }
|
Chris@16
|
378
|
Chris@16
|
379 }
|
Chris@16
|
380
|
Chris@16
|
381 void init_from_unsigned_long(size_type num_bits,
|
Chris@16
|
382 unsigned long value/*,
|
Chris@16
|
383 const Allocator& alloc*/)
|
Chris@16
|
384 {
|
Chris@16
|
385
|
Chris@16
|
386 assert(m_bits.size() == 0);
|
Chris@16
|
387
|
Chris@16
|
388 m_bits.resize(calc_num_blocks(num_bits));
|
Chris@16
|
389 m_num_bits = num_bits;
|
Chris@16
|
390
|
Chris@16
|
391 typedef unsigned long num_type;
|
Chris@16
|
392 typedef boost::detail::dynamic_bitset_impl
|
Chris@16
|
393 ::shifter<num_type, bits_per_block, ulong_width> shifter;
|
Chris@16
|
394
|
Chris@16
|
395 //if (num_bits == 0)
|
Chris@16
|
396 // return;
|
Chris@16
|
397
|
Chris@16
|
398 // zero out all bits at pos >= num_bits, if any;
|
Chris@16
|
399 // note that: num_bits == 0 implies value == 0
|
Chris@16
|
400 if (num_bits < static_cast<size_type>(ulong_width)) {
|
Chris@16
|
401 const num_type mask = (num_type(1) << num_bits) - 1;
|
Chris@16
|
402 value &= mask;
|
Chris@16
|
403 }
|
Chris@16
|
404
|
Chris@16
|
405 typename buffer_type::iterator it = m_bits.begin();
|
Chris@16
|
406 for( ; value; shifter::left_shift(value), ++it) {
|
Chris@16
|
407 *it = static_cast<block_type>(value);
|
Chris@16
|
408 }
|
Chris@16
|
409
|
Chris@16
|
410 }
|
Chris@16
|
411
|
Chris@16
|
412
|
Chris@16
|
413
|
Chris@16
|
414 BOOST_DYNAMIC_BITSET_PRIVATE:
|
Chris@16
|
415
|
Chris@16
|
416 bool m_unchecked_test(size_type pos) const;
|
Chris@16
|
417 static size_type calc_num_blocks(size_type num_bits);
|
Chris@16
|
418
|
Chris@16
|
419 Block& m_highest_block();
|
Chris@16
|
420 const Block& m_highest_block() const;
|
Chris@16
|
421
|
Chris@16
|
422 buffer_type m_bits;
|
Chris@16
|
423 size_type m_num_bits;
|
Chris@16
|
424
|
Chris@16
|
425
|
Chris@16
|
426 class bit_appender;
|
Chris@16
|
427 friend class bit_appender;
|
Chris@16
|
428 class bit_appender {
|
Chris@16
|
429 // helper for stream >>
|
Chris@16
|
430 // Supplies to the lack of an efficient append at the less
|
Chris@16
|
431 // significant end: bits are actually appended "at left" but
|
Chris@16
|
432 // rearranged in the destructor. From the perspective of
|
Chris@16
|
433 // client code everything works *as if* dynamic_bitset<> had
|
Chris@16
|
434 // an append_at_right() function (eventually throwing the same
|
Chris@16
|
435 // exceptions as push_back) except that the function is in fact
|
Chris@16
|
436 // called bit_appender::do_append().
|
Chris@16
|
437 //
|
Chris@16
|
438 dynamic_bitset & bs;
|
Chris@16
|
439 size_type n;
|
Chris@16
|
440 Block mask;
|
Chris@16
|
441 Block * current;
|
Chris@16
|
442
|
Chris@16
|
443 // not implemented
|
Chris@16
|
444 bit_appender(const bit_appender &);
|
Chris@16
|
445 bit_appender & operator=(const bit_appender &);
|
Chris@16
|
446
|
Chris@16
|
447 public:
|
Chris@16
|
448 bit_appender(dynamic_bitset & r) : bs(r), n(0), mask(0), current(0) {}
|
Chris@16
|
449 ~bit_appender() {
|
Chris@16
|
450 // reverse the order of blocks, shift
|
Chris@16
|
451 // if needed, and then resize
|
Chris@16
|
452 //
|
Chris@16
|
453 std::reverse(bs.m_bits.begin(), bs.m_bits.end());
|
Chris@16
|
454 const block_width_type offs = bit_index(n);
|
Chris@16
|
455 if (offs)
|
Chris@16
|
456 bs >>= (bits_per_block - offs);
|
Chris@16
|
457 bs.resize(n); // doesn't enlarge, so can't throw
|
Chris@16
|
458 assert(bs.m_check_invariants());
|
Chris@16
|
459 }
|
Chris@16
|
460 inline void do_append(bool value) {
|
Chris@16
|
461
|
Chris@16
|
462 if (mask == 0) {
|
Chris@16
|
463 bs.append(Block(0));
|
Chris@16
|
464 current = &bs.m_highest_block();
|
Chris@16
|
465 mask = Block(1) << (bits_per_block - 1);
|
Chris@16
|
466 }
|
Chris@16
|
467
|
Chris@16
|
468 if(value)
|
Chris@16
|
469 *current |= mask;
|
Chris@16
|
470
|
Chris@16
|
471 mask /= 2;
|
Chris@16
|
472 ++n;
|
Chris@16
|
473 }
|
Chris@16
|
474 size_type get_count() const { return n; }
|
Chris@16
|
475 };
|
Chris@16
|
476
|
Chris@16
|
477 };
|
Chris@16
|
478
|
Chris@16
|
479 #if !defined BOOST_NO_INCLASS_MEMBER_INITIALIZATION
|
Chris@16
|
480
|
Chris@16
|
481 template <typename Block, typename Allocator>
|
Chris@16
|
482 const typename dynamic_bitset<Block, Allocator>::block_width_type
|
Chris@16
|
483 dynamic_bitset<Block, Allocator>::bits_per_block;
|
Chris@16
|
484
|
Chris@16
|
485 template <typename Block, typename Allocator>
|
Chris@16
|
486 const typename dynamic_bitset<Block, Allocator>::size_type
|
Chris@16
|
487 dynamic_bitset<Block, Allocator>::npos;
|
Chris@16
|
488
|
Chris@16
|
489 template <typename Block, typename Allocator>
|
Chris@16
|
490 const typename dynamic_bitset<Block, Allocator>::block_width_type
|
Chris@16
|
491 dynamic_bitset<Block, Allocator>::ulong_width;
|
Chris@16
|
492
|
Chris@16
|
493 #endif
|
Chris@16
|
494
|
Chris@16
|
495 // Global Functions:
|
Chris@16
|
496
|
Chris@16
|
497 // comparison
|
Chris@16
|
498 template <typename Block, typename Allocator>
|
Chris@16
|
499 bool operator!=(const dynamic_bitset<Block, Allocator>& a,
|
Chris@16
|
500 const dynamic_bitset<Block, Allocator>& b);
|
Chris@16
|
501
|
Chris@16
|
502 template <typename Block, typename Allocator>
|
Chris@16
|
503 bool operator<=(const dynamic_bitset<Block, Allocator>& a,
|
Chris@16
|
504 const dynamic_bitset<Block, Allocator>& b);
|
Chris@16
|
505
|
Chris@16
|
506 template <typename Block, typename Allocator>
|
Chris@16
|
507 bool operator>(const dynamic_bitset<Block, Allocator>& a,
|
Chris@16
|
508 const dynamic_bitset<Block, Allocator>& b);
|
Chris@16
|
509
|
Chris@16
|
510 template <typename Block, typename Allocator>
|
Chris@16
|
511 bool operator>=(const dynamic_bitset<Block, Allocator>& a,
|
Chris@16
|
512 const dynamic_bitset<Block, Allocator>& b);
|
Chris@16
|
513
|
Chris@16
|
514 // stream operators
|
Chris@16
|
515 #ifdef BOOST_OLD_IOSTREAMS
|
Chris@16
|
516 template <typename Block, typename Allocator>
|
Chris@16
|
517 std::ostream& operator<<(std::ostream& os,
|
Chris@16
|
518 const dynamic_bitset<Block, Allocator>& b);
|
Chris@16
|
519
|
Chris@16
|
520 template <typename Block, typename Allocator>
|
Chris@16
|
521 std::istream& operator>>(std::istream& is, dynamic_bitset<Block,Allocator>& b);
|
Chris@16
|
522 #else
|
Chris@16
|
523 template <typename CharT, typename Traits, typename Block, typename Allocator>
|
Chris@16
|
524 std::basic_ostream<CharT, Traits>&
|
Chris@16
|
525 operator<<(std::basic_ostream<CharT, Traits>& os,
|
Chris@16
|
526 const dynamic_bitset<Block, Allocator>& b);
|
Chris@16
|
527
|
Chris@16
|
528 template <typename CharT, typename Traits, typename Block, typename Allocator>
|
Chris@16
|
529 std::basic_istream<CharT, Traits>&
|
Chris@16
|
530 operator>>(std::basic_istream<CharT, Traits>& is,
|
Chris@16
|
531 dynamic_bitset<Block, Allocator>& b);
|
Chris@16
|
532 #endif
|
Chris@16
|
533
|
Chris@16
|
534 // bitset operations
|
Chris@16
|
535 template <typename Block, typename Allocator>
|
Chris@16
|
536 dynamic_bitset<Block, Allocator>
|
Chris@16
|
537 operator&(const dynamic_bitset<Block, Allocator>& b1,
|
Chris@16
|
538 const dynamic_bitset<Block, Allocator>& b2);
|
Chris@16
|
539
|
Chris@16
|
540 template <typename Block, typename Allocator>
|
Chris@16
|
541 dynamic_bitset<Block, Allocator>
|
Chris@16
|
542 operator|(const dynamic_bitset<Block, Allocator>& b1,
|
Chris@16
|
543 const dynamic_bitset<Block, Allocator>& b2);
|
Chris@16
|
544
|
Chris@16
|
545 template <typename Block, typename Allocator>
|
Chris@16
|
546 dynamic_bitset<Block, Allocator>
|
Chris@16
|
547 operator^(const dynamic_bitset<Block, Allocator>& b1,
|
Chris@16
|
548 const dynamic_bitset<Block, Allocator>& b2);
|
Chris@16
|
549
|
Chris@16
|
550 template <typename Block, typename Allocator>
|
Chris@16
|
551 dynamic_bitset<Block, Allocator>
|
Chris@16
|
552 operator-(const dynamic_bitset<Block, Allocator>& b1,
|
Chris@16
|
553 const dynamic_bitset<Block, Allocator>& b2);
|
Chris@16
|
554
|
Chris@16
|
555 // namespace scope swap
|
Chris@16
|
556 template<typename Block, typename Allocator>
|
Chris@16
|
557 void swap(dynamic_bitset<Block, Allocator>& b1,
|
Chris@16
|
558 dynamic_bitset<Block, Allocator>& b2);
|
Chris@16
|
559
|
Chris@16
|
560
|
Chris@16
|
561 template <typename Block, typename Allocator, typename stringT>
|
Chris@16
|
562 void
|
Chris@16
|
563 to_string(const dynamic_bitset<Block, Allocator>& b, stringT & s);
|
Chris@16
|
564
|
Chris@16
|
565 template <typename Block, typename Allocator, typename BlockOutputIterator>
|
Chris@16
|
566 void
|
Chris@16
|
567 to_block_range(const dynamic_bitset<Block, Allocator>& b,
|
Chris@16
|
568 BlockOutputIterator result);
|
Chris@16
|
569
|
Chris@16
|
570
|
Chris@16
|
571 template <typename BlockIterator, typename B, typename A>
|
Chris@16
|
572 inline void
|
Chris@16
|
573 from_block_range(BlockIterator first, BlockIterator last,
|
Chris@16
|
574 dynamic_bitset<B, A>& result)
|
Chris@16
|
575 {
|
Chris@16
|
576 // PRE: distance(first, last) <= numblocks()
|
Chris@16
|
577 std::copy (first, last, result.m_bits.begin());
|
Chris@16
|
578 }
|
Chris@16
|
579
|
Chris@16
|
580 //=============================================================================
|
Chris@16
|
581 // dynamic_bitset implementation
|
Chris@16
|
582
|
Chris@16
|
583
|
Chris@16
|
584 //-----------------------------------------------------------------------------
|
Chris@16
|
585 // constructors, etc.
|
Chris@16
|
586
|
Chris@16
|
587 template <typename Block, typename Allocator>
|
Chris@16
|
588 dynamic_bitset<Block, Allocator>::dynamic_bitset(const Allocator& alloc)
|
Chris@16
|
589 : m_bits(alloc), m_num_bits(0)
|
Chris@16
|
590 {
|
Chris@16
|
591
|
Chris@16
|
592 }
|
Chris@16
|
593
|
Chris@16
|
594 template <typename Block, typename Allocator>
|
Chris@16
|
595 dynamic_bitset<Block, Allocator>::
|
Chris@16
|
596 dynamic_bitset(size_type num_bits, unsigned long value, const Allocator& alloc)
|
Chris@16
|
597 : m_bits(alloc),
|
Chris@16
|
598 m_num_bits(0)
|
Chris@16
|
599 {
|
Chris@16
|
600 init_from_unsigned_long(num_bits, value);
|
Chris@16
|
601 }
|
Chris@16
|
602
|
Chris@16
|
603 // copy constructor
|
Chris@16
|
604 template <typename Block, typename Allocator>
|
Chris@16
|
605 inline dynamic_bitset<Block, Allocator>::
|
Chris@16
|
606 dynamic_bitset(const dynamic_bitset& b)
|
Chris@16
|
607 : m_bits(b.m_bits), m_num_bits(b.m_num_bits)
|
Chris@16
|
608 {
|
Chris@16
|
609
|
Chris@16
|
610 }
|
Chris@16
|
611
|
Chris@16
|
612 template <typename Block, typename Allocator>
|
Chris@16
|
613 inline dynamic_bitset<Block, Allocator>::
|
Chris@16
|
614 ~dynamic_bitset()
|
Chris@16
|
615 {
|
Chris@16
|
616 assert(m_check_invariants());
|
Chris@16
|
617 }
|
Chris@16
|
618
|
Chris@16
|
619 template <typename Block, typename Allocator>
|
Chris@16
|
620 inline void dynamic_bitset<Block, Allocator>::
|
Chris@16
|
621 swap(dynamic_bitset<Block, Allocator>& b) // no throw
|
Chris@16
|
622 {
|
Chris@16
|
623 std::swap(m_bits, b.m_bits);
|
Chris@16
|
624 std::swap(m_num_bits, b.m_num_bits);
|
Chris@16
|
625 }
|
Chris@16
|
626
|
Chris@16
|
627 template <typename Block, typename Allocator>
|
Chris@16
|
628 dynamic_bitset<Block, Allocator>& dynamic_bitset<Block, Allocator>::
|
Chris@16
|
629 operator=(const dynamic_bitset<Block, Allocator>& b)
|
Chris@16
|
630 {
|
Chris@16
|
631 m_bits = b.m_bits;
|
Chris@16
|
632 m_num_bits = b.m_num_bits;
|
Chris@16
|
633 return *this;
|
Chris@16
|
634 }
|
Chris@16
|
635
|
Chris@16
|
636 template <typename Block, typename Allocator>
|
Chris@16
|
637 inline typename dynamic_bitset<Block, Allocator>::allocator_type
|
Chris@16
|
638 dynamic_bitset<Block, Allocator>::get_allocator() const
|
Chris@16
|
639 {
|
Chris@16
|
640 return m_bits.get_allocator();
|
Chris@16
|
641 }
|
Chris@16
|
642
|
Chris@16
|
643 //-----------------------------------------------------------------------------
|
Chris@16
|
644 // size changing operations
|
Chris@16
|
645
|
Chris@16
|
646 template <typename Block, typename Allocator>
|
Chris@16
|
647 void dynamic_bitset<Block, Allocator>::
|
Chris@16
|
648 resize(size_type num_bits, bool value) // strong guarantee
|
Chris@16
|
649 {
|
Chris@16
|
650
|
Chris@16
|
651 const size_type old_num_blocks = num_blocks();
|
Chris@16
|
652 const size_type required_blocks = calc_num_blocks(num_bits);
|
Chris@16
|
653
|
Chris@16
|
654 const block_type v = value? ~Block(0) : Block(0);
|
Chris@16
|
655
|
Chris@16
|
656 if (required_blocks != old_num_blocks) {
|
Chris@16
|
657 m_bits.resize(required_blocks, v); // s.g. (copy)
|
Chris@16
|
658 }
|
Chris@16
|
659
|
Chris@16
|
660
|
Chris@16
|
661 // At this point:
|
Chris@16
|
662 //
|
Chris@16
|
663 // - if the buffer was shrunk, we have nothing more to do,
|
Chris@16
|
664 // except a call to m_zero_unused_bits()
|
Chris@16
|
665 //
|
Chris@16
|
666 // - if it was enlarged, all the (used) bits in the new blocks have
|
Chris@16
|
667 // the correct value, but we have not yet touched those bits, if
|
Chris@16
|
668 // any, that were 'unused bits' before enlarging: if value == true,
|
Chris@16
|
669 // they must be set.
|
Chris@16
|
670
|
Chris@16
|
671 if (value && (num_bits > m_num_bits)) {
|
Chris@16
|
672
|
Chris@16
|
673 const block_width_type extra_bits = count_extra_bits();
|
Chris@16
|
674 if (extra_bits) {
|
Chris@16
|
675 assert(old_num_blocks >= 1 && old_num_blocks <= m_bits.size());
|
Chris@16
|
676
|
Chris@16
|
677 // Set them.
|
Chris@16
|
678 m_bits[old_num_blocks - 1] |= (v << extra_bits);
|
Chris@16
|
679 }
|
Chris@16
|
680
|
Chris@16
|
681 }
|
Chris@16
|
682
|
Chris@16
|
683 m_num_bits = num_bits;
|
Chris@16
|
684 m_zero_unused_bits();
|
Chris@16
|
685
|
Chris@16
|
686 }
|
Chris@16
|
687
|
Chris@16
|
688 template <typename Block, typename Allocator>
|
Chris@16
|
689 void dynamic_bitset<Block, Allocator>::
|
Chris@16
|
690 clear() // no throw
|
Chris@16
|
691 {
|
Chris@16
|
692 m_bits.clear();
|
Chris@16
|
693 m_num_bits = 0;
|
Chris@16
|
694 }
|
Chris@16
|
695
|
Chris@16
|
696
|
Chris@16
|
697 template <typename Block, typename Allocator>
|
Chris@16
|
698 void dynamic_bitset<Block, Allocator>::
|
Chris@16
|
699 push_back(bool bit)
|
Chris@16
|
700 {
|
Chris@16
|
701 const size_type sz = size();
|
Chris@16
|
702 resize(sz + 1);
|
Chris@16
|
703 set(sz, bit);
|
Chris@16
|
704 }
|
Chris@16
|
705
|
Chris@16
|
706 template <typename Block, typename Allocator>
|
Chris@16
|
707 void dynamic_bitset<Block, Allocator>::
|
Chris@16
|
708 append(Block value) // strong guarantee
|
Chris@16
|
709 {
|
Chris@16
|
710 const block_width_type r = count_extra_bits();
|
Chris@16
|
711
|
Chris@16
|
712 if (r == 0) {
|
Chris@16
|
713 // the buffer is empty, or all blocks are filled
|
Chris@16
|
714 m_bits.push_back(value);
|
Chris@16
|
715 }
|
Chris@16
|
716 else {
|
Chris@16
|
717 m_bits.push_back(value >> (bits_per_block - r));
|
Chris@16
|
718 m_bits[m_bits.size() - 2] |= (value << r); // m_bits.size() >= 2
|
Chris@16
|
719 }
|
Chris@16
|
720
|
Chris@16
|
721 m_num_bits += bits_per_block;
|
Chris@16
|
722 assert(m_check_invariants());
|
Chris@16
|
723
|
Chris@16
|
724 }
|
Chris@16
|
725
|
Chris@16
|
726
|
Chris@16
|
727 //-----------------------------------------------------------------------------
|
Chris@16
|
728 // bitset operations
|
Chris@16
|
729 template <typename Block, typename Allocator>
|
Chris@16
|
730 dynamic_bitset<Block, Allocator>&
|
Chris@16
|
731 dynamic_bitset<Block, Allocator>::operator&=(const dynamic_bitset& rhs)
|
Chris@16
|
732 {
|
Chris@16
|
733 assert(size() == rhs.size());
|
Chris@16
|
734 for (size_type i = 0; i < num_blocks(); ++i)
|
Chris@16
|
735 m_bits[i] &= rhs.m_bits[i];
|
Chris@16
|
736 return *this;
|
Chris@16
|
737 }
|
Chris@16
|
738
|
Chris@16
|
739 template <typename Block, typename Allocator>
|
Chris@16
|
740 dynamic_bitset<Block, Allocator>&
|
Chris@16
|
741 dynamic_bitset<Block, Allocator>::operator|=(const dynamic_bitset& rhs)
|
Chris@16
|
742 {
|
Chris@16
|
743 assert(size() == rhs.size());
|
Chris@16
|
744 for (size_type i = 0; i < num_blocks(); ++i)
|
Chris@16
|
745 m_bits[i] |= rhs.m_bits[i];
|
Chris@16
|
746 //m_zero_unused_bits();
|
Chris@16
|
747 return *this;
|
Chris@16
|
748 }
|
Chris@16
|
749
|
Chris@16
|
750 template <typename Block, typename Allocator>
|
Chris@16
|
751 dynamic_bitset<Block, Allocator>&
|
Chris@16
|
752 dynamic_bitset<Block, Allocator>::operator^=(const dynamic_bitset& rhs)
|
Chris@16
|
753 {
|
Chris@16
|
754 assert(size() == rhs.size());
|
Chris@16
|
755 for (size_type i = 0; i < this->num_blocks(); ++i)
|
Chris@16
|
756 m_bits[i] ^= rhs.m_bits[i];
|
Chris@16
|
757 //m_zero_unused_bits();
|
Chris@16
|
758 return *this;
|
Chris@16
|
759 }
|
Chris@16
|
760
|
Chris@16
|
761 template <typename Block, typename Allocator>
|
Chris@16
|
762 dynamic_bitset<Block, Allocator>&
|
Chris@16
|
763 dynamic_bitset<Block, Allocator>::operator-=(const dynamic_bitset& rhs)
|
Chris@16
|
764 {
|
Chris@16
|
765 assert(size() == rhs.size());
|
Chris@16
|
766 for (size_type i = 0; i < num_blocks(); ++i)
|
Chris@16
|
767 m_bits[i] &= ~rhs.m_bits[i];
|
Chris@16
|
768 //m_zero_unused_bits();
|
Chris@16
|
769 return *this;
|
Chris@16
|
770 }
|
Chris@16
|
771
|
Chris@16
|
772 //
|
Chris@16
|
773 // NOTE:
|
Chris@16
|
774 // Note that the 'if (r != 0)' is crucial to avoid undefined
|
Chris@16
|
775 // behavior when the left hand operand of >> isn't promoted to a
|
Chris@16
|
776 // wider type (because rs would be too large).
|
Chris@16
|
777 //
|
Chris@16
|
778 template <typename Block, typename Allocator>
|
Chris@16
|
779 dynamic_bitset<Block, Allocator>&
|
Chris@16
|
780 dynamic_bitset<Block, Allocator>::operator<<=(size_type n)
|
Chris@16
|
781 {
|
Chris@16
|
782 if (n >= m_num_bits)
|
Chris@16
|
783 return reset();
|
Chris@16
|
784 //else
|
Chris@16
|
785 if (n > 0) {
|
Chris@16
|
786
|
Chris@16
|
787 size_type const last = num_blocks() - 1; // num_blocks() is >= 1
|
Chris@16
|
788 size_type const div = n / bits_per_block; // div is <= last
|
Chris@16
|
789 block_width_type const r = bit_index(n);
|
Chris@16
|
790 block_type * const b = &m_bits[0];
|
Chris@16
|
791
|
Chris@16
|
792 if (r != 0) {
|
Chris@16
|
793
|
Chris@16
|
794 block_width_type const rs = bits_per_block - r;
|
Chris@16
|
795
|
Chris@16
|
796 for (size_type i = last-div; i>0; --i) {
|
Chris@16
|
797 b[i+div] = (b[i] << r) | (b[i-1] >> rs);
|
Chris@16
|
798 }
|
Chris@16
|
799 b[div] = b[0] << r;
|
Chris@16
|
800
|
Chris@16
|
801 }
|
Chris@16
|
802 else {
|
Chris@16
|
803 for (size_type i = last-div; i>0; --i) {
|
Chris@16
|
804 b[i+div] = b[i];
|
Chris@16
|
805 }
|
Chris@16
|
806 b[div] = b[0];
|
Chris@16
|
807 }
|
Chris@16
|
808
|
Chris@16
|
809 // zero out div blocks at the less significant end
|
Chris@16
|
810 std::fill_n(b, div, static_cast<block_type>(0));
|
Chris@16
|
811
|
Chris@16
|
812 // zero out any 1 bit that flowed into the unused part
|
Chris@16
|
813 m_zero_unused_bits(); // thanks to Lester Gong
|
Chris@16
|
814
|
Chris@16
|
815 }
|
Chris@16
|
816
|
Chris@16
|
817 return *this;
|
Chris@16
|
818
|
Chris@16
|
819
|
Chris@16
|
820 }
|
Chris@16
|
821
|
Chris@16
|
822
|
Chris@16
|
823 //
|
Chris@16
|
824 // NOTE:
|
Chris@16
|
825 // see the comments to operator <<=
|
Chris@16
|
826 //
|
Chris@16
|
827 template <typename B, typename A>
|
Chris@16
|
828 dynamic_bitset<B, A> & dynamic_bitset<B, A>::operator>>=(size_type n) {
|
Chris@16
|
829 if (n >= m_num_bits) {
|
Chris@16
|
830 return reset();
|
Chris@16
|
831 }
|
Chris@16
|
832 //else
|
Chris@16
|
833 if (n>0) {
|
Chris@16
|
834
|
Chris@16
|
835 size_type const last = num_blocks() - 1; // num_blocks() is >= 1
|
Chris@16
|
836 size_type const div = n / bits_per_block; // div is <= last
|
Chris@16
|
837 block_width_type const r = bit_index(n);
|
Chris@16
|
838 block_type * const b = &m_bits[0];
|
Chris@16
|
839
|
Chris@16
|
840
|
Chris@16
|
841 if (r != 0) {
|
Chris@16
|
842
|
Chris@16
|
843 block_width_type const ls = bits_per_block - r;
|
Chris@16
|
844
|
Chris@16
|
845 for (size_type i = div; i < last; ++i) {
|
Chris@16
|
846 b[i-div] = (b[i] >> r) | (b[i+1] << ls);
|
Chris@16
|
847 }
|
Chris@16
|
848 // r bits go to zero
|
Chris@16
|
849 b[last-div] = b[last] >> r;
|
Chris@16
|
850 }
|
Chris@16
|
851
|
Chris@16
|
852 else {
|
Chris@16
|
853 for (size_type i = div; i <= last; ++i) {
|
Chris@16
|
854 b[i-div] = b[i];
|
Chris@16
|
855 }
|
Chris@16
|
856 // note the '<=': the last iteration 'absorbs'
|
Chris@16
|
857 // b[last-div] = b[last] >> 0;
|
Chris@16
|
858 }
|
Chris@16
|
859
|
Chris@16
|
860
|
Chris@16
|
861
|
Chris@16
|
862 // div blocks are zero filled at the most significant end
|
Chris@16
|
863 std::fill_n(b + (num_blocks()-div), div, static_cast<block_type>(0));
|
Chris@16
|
864 }
|
Chris@16
|
865
|
Chris@16
|
866 return *this;
|
Chris@16
|
867 }
|
Chris@16
|
868
|
Chris@16
|
869
|
Chris@16
|
870 template <typename Block, typename Allocator>
|
Chris@16
|
871 dynamic_bitset<Block, Allocator>
|
Chris@16
|
872 dynamic_bitset<Block, Allocator>::operator<<(size_type n) const
|
Chris@16
|
873 {
|
Chris@16
|
874 dynamic_bitset r(*this);
|
Chris@16
|
875 return r <<= n;
|
Chris@16
|
876 }
|
Chris@16
|
877
|
Chris@16
|
878 template <typename Block, typename Allocator>
|
Chris@16
|
879 dynamic_bitset<Block, Allocator>
|
Chris@16
|
880 dynamic_bitset<Block, Allocator>::operator>>(size_type n) const
|
Chris@16
|
881 {
|
Chris@16
|
882 dynamic_bitset r(*this);
|
Chris@16
|
883 return r >>= n;
|
Chris@16
|
884 }
|
Chris@16
|
885
|
Chris@16
|
886
|
Chris@16
|
887 //-----------------------------------------------------------------------------
|
Chris@16
|
888 // basic bit operations
|
Chris@16
|
889
|
Chris@16
|
890 template <typename Block, typename Allocator>
|
Chris@16
|
891 dynamic_bitset<Block, Allocator>&
|
Chris@16
|
892 dynamic_bitset<Block, Allocator>::set(size_type pos, bool val)
|
Chris@16
|
893 {
|
Chris@16
|
894 assert(pos < m_num_bits);
|
Chris@16
|
895
|
Chris@16
|
896 if (val)
|
Chris@16
|
897 m_bits[block_index(pos)] |= bit_mask(pos);
|
Chris@16
|
898 else
|
Chris@16
|
899 reset(pos);
|
Chris@16
|
900
|
Chris@16
|
901 return *this;
|
Chris@16
|
902 }
|
Chris@16
|
903
|
Chris@16
|
904 template <typename Block, typename Allocator>
|
Chris@16
|
905 dynamic_bitset<Block, Allocator>&
|
Chris@16
|
906 dynamic_bitset<Block, Allocator>::set()
|
Chris@16
|
907 {
|
Chris@16
|
908 std::fill(m_bits.begin(), m_bits.end(), ~Block(0));
|
Chris@16
|
909 m_zero_unused_bits();
|
Chris@16
|
910 return *this;
|
Chris@16
|
911 }
|
Chris@16
|
912
|
Chris@16
|
913 template <typename Block, typename Allocator>
|
Chris@16
|
914 dynamic_bitset<Block, Allocator>&
|
Chris@16
|
915 dynamic_bitset<Block, Allocator>::reset(size_type pos)
|
Chris@16
|
916 {
|
Chris@16
|
917 assert(pos < m_num_bits);
|
Chris@16
|
918 #if defined __MWERKS__ && BOOST_WORKAROUND(__MWERKS__, <= 0x3003) // 8.x
|
Chris@16
|
919 // CodeWarrior 8 generates incorrect code when the &=~ is compiled,
|
Chris@16
|
920 // use the |^ variation instead.. <grafik>
|
Chris@16
|
921 m_bits[block_index(pos)] |= bit_mask(pos);
|
Chris@16
|
922 m_bits[block_index(pos)] ^= bit_mask(pos);
|
Chris@16
|
923 #else
|
Chris@16
|
924 m_bits[block_index(pos)] &= ~bit_mask(pos);
|
Chris@16
|
925 #endif
|
Chris@16
|
926 return *this;
|
Chris@16
|
927 }
|
Chris@16
|
928
|
Chris@16
|
929 template <typename Block, typename Allocator>
|
Chris@16
|
930 dynamic_bitset<Block, Allocator>&
|
Chris@16
|
931 dynamic_bitset<Block, Allocator>::reset()
|
Chris@16
|
932 {
|
Chris@16
|
933 std::fill(m_bits.begin(), m_bits.end(), Block(0));
|
Chris@16
|
934 return *this;
|
Chris@16
|
935 }
|
Chris@16
|
936
|
Chris@16
|
937 template <typename Block, typename Allocator>
|
Chris@16
|
938 dynamic_bitset<Block, Allocator>&
|
Chris@16
|
939 dynamic_bitset<Block, Allocator>::flip(size_type pos)
|
Chris@16
|
940 {
|
Chris@16
|
941 assert(pos < m_num_bits);
|
Chris@16
|
942 m_bits[block_index(pos)] ^= bit_mask(pos);
|
Chris@16
|
943 return *this;
|
Chris@16
|
944 }
|
Chris@16
|
945
|
Chris@16
|
946 template <typename Block, typename Allocator>
|
Chris@16
|
947 dynamic_bitset<Block, Allocator>&
|
Chris@16
|
948 dynamic_bitset<Block, Allocator>::flip()
|
Chris@16
|
949 {
|
Chris@16
|
950 for (size_type i = 0; i < num_blocks(); ++i)
|
Chris@16
|
951 m_bits[i] = ~m_bits[i];
|
Chris@16
|
952 m_zero_unused_bits();
|
Chris@16
|
953 return *this;
|
Chris@16
|
954 }
|
Chris@16
|
955
|
Chris@16
|
956 template <typename Block, typename Allocator>
|
Chris@16
|
957 bool dynamic_bitset<Block, Allocator>::m_unchecked_test(size_type pos) const
|
Chris@16
|
958 {
|
Chris@16
|
959 return (m_bits[block_index(pos)] & bit_mask(pos)) != 0;
|
Chris@16
|
960 }
|
Chris@16
|
961
|
Chris@16
|
962 template <typename Block, typename Allocator>
|
Chris@16
|
963 bool dynamic_bitset<Block, Allocator>::test(size_type pos) const
|
Chris@16
|
964 {
|
Chris@16
|
965 assert(pos < m_num_bits);
|
Chris@16
|
966 return m_unchecked_test(pos);
|
Chris@16
|
967 }
|
Chris@16
|
968
|
Chris@16
|
969 template <typename Block, typename Allocator>
|
Chris@16
|
970 bool dynamic_bitset<Block, Allocator>::any() const
|
Chris@16
|
971 {
|
Chris@16
|
972 for (size_type i = 0; i < num_blocks(); ++i)
|
Chris@16
|
973 if (m_bits[i])
|
Chris@16
|
974 return true;
|
Chris@16
|
975 return false;
|
Chris@16
|
976 }
|
Chris@16
|
977
|
Chris@16
|
978 template <typename Block, typename Allocator>
|
Chris@16
|
979 inline bool dynamic_bitset<Block, Allocator>::none() const
|
Chris@16
|
980 {
|
Chris@16
|
981 return !any();
|
Chris@16
|
982 }
|
Chris@16
|
983
|
Chris@16
|
984 template <typename Block, typename Allocator>
|
Chris@16
|
985 dynamic_bitset<Block, Allocator>
|
Chris@16
|
986 dynamic_bitset<Block, Allocator>::operator~() const
|
Chris@16
|
987 {
|
Chris@16
|
988 dynamic_bitset b(*this);
|
Chris@16
|
989 b.flip();
|
Chris@16
|
990 return b;
|
Chris@16
|
991 }
|
Chris@16
|
992
|
Chris@16
|
993 template <typename Block, typename Allocator>
|
Chris@16
|
994 typename dynamic_bitset<Block, Allocator>::size_type
|
Chris@16
|
995 dynamic_bitset<Block, Allocator>::count() const
|
Chris@16
|
996 {
|
Chris@16
|
997 using detail::dynamic_bitset_impl::table_width;
|
Chris@16
|
998 using detail::dynamic_bitset_impl::access_by_bytes;
|
Chris@16
|
999 using detail::dynamic_bitset_impl::access_by_blocks;
|
Chris@16
|
1000 using detail::dynamic_bitset_impl::value_to_type;
|
Chris@16
|
1001
|
Chris@16
|
1002 #if BOOST_WORKAROUND(__GNUC__, == 4) && (__GNUC_MINOR__ == 3) && (__GNUC_PATCHLEVEL__ == 3)
|
Chris@16
|
1003 // NOTE: Explicit qualification of "bits_per_block"
|
Chris@16
|
1004 // breaks compilation on gcc 4.3.3
|
Chris@16
|
1005 enum { no_padding = bits_per_block == CHAR_BIT * sizeof(Block) };
|
Chris@16
|
1006 #else
|
Chris@16
|
1007 // NOTE: Explicitly qualifying "bits_per_block" to workaround
|
Chris@16
|
1008 // regressions of gcc 3.4.x
|
Chris@16
|
1009 enum { no_padding =
|
Chris@16
|
1010 dynamic_bitset<Block, Allocator>::bits_per_block
|
Chris@16
|
1011 == CHAR_BIT * sizeof(Block) };
|
Chris@16
|
1012 #endif
|
Chris@16
|
1013
|
Chris@16
|
1014 enum { enough_table_width = table_width >= CHAR_BIT };
|
Chris@16
|
1015
|
Chris@16
|
1016 enum { mode = (no_padding && enough_table_width)
|
Chris@16
|
1017 ? access_by_bytes
|
Chris@16
|
1018 : access_by_blocks };
|
Chris@16
|
1019
|
Chris@16
|
1020 return do_count(m_bits.begin(), num_blocks(), Block(0),
|
Chris@16
|
1021 static_cast<value_to_type<(bool)mode> *>(0));
|
Chris@16
|
1022 }
|
Chris@16
|
1023
|
Chris@16
|
1024
|
Chris@16
|
1025 //-----------------------------------------------------------------------------
|
Chris@16
|
1026 // conversions
|
Chris@16
|
1027
|
Chris@16
|
1028
|
Chris@16
|
1029 template <typename B, typename A, typename stringT>
|
Chris@16
|
1030 void to_string_helper(const dynamic_bitset<B, A> & b, stringT & s,
|
Chris@16
|
1031 bool dump_all)
|
Chris@16
|
1032 {
|
Chris@16
|
1033 typedef typename stringT::traits_type Tr;
|
Chris@16
|
1034 typedef typename stringT::value_type Ch;
|
Chris@16
|
1035
|
Chris@16
|
1036 BOOST_DYNAMIC_BITSET_CTYPE_FACET(Ch, fac, std::locale());
|
Chris@16
|
1037 const Ch zero = BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '0');
|
Chris@16
|
1038 const Ch one = BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '1');
|
Chris@16
|
1039
|
Chris@16
|
1040 // Note that this function may access (when
|
Chris@16
|
1041 // dump_all == true) bits beyond position size() - 1
|
Chris@16
|
1042
|
Chris@16
|
1043 typedef typename dynamic_bitset<B, A>::size_type size_type;
|
Chris@16
|
1044
|
Chris@16
|
1045 const size_type len = dump_all?
|
Chris@16
|
1046 dynamic_bitset<B, A>::bits_per_block * b.num_blocks():
|
Chris@16
|
1047 b.size();
|
Chris@16
|
1048 s.assign (len, zero);
|
Chris@16
|
1049
|
Chris@16
|
1050 for (size_type i = 0; i < len; ++i) {
|
Chris@16
|
1051 if (b.m_unchecked_test(i))
|
Chris@16
|
1052 Tr::assign(s[len - 1 - i], one);
|
Chris@16
|
1053
|
Chris@16
|
1054 }
|
Chris@16
|
1055
|
Chris@16
|
1056 }
|
Chris@16
|
1057
|
Chris@16
|
1058
|
Chris@16
|
1059 // A comment similar to the one about the constructor from
|
Chris@16
|
1060 // basic_string can be done here. Thanks to James Kanze for
|
Chris@16
|
1061 // making me (Gennaro) realize this important separation of
|
Chris@16
|
1062 // concerns issue, as well as many things about i18n.
|
Chris@16
|
1063 //
|
Chris@16
|
1064 template <typename Block, typename Allocator, typename stringT>
|
Chris@16
|
1065 inline void
|
Chris@16
|
1066 to_string(const dynamic_bitset<Block, Allocator>& b, stringT& s)
|
Chris@16
|
1067 {
|
Chris@16
|
1068 to_string_helper(b, s, false);
|
Chris@16
|
1069 }
|
Chris@16
|
1070
|
Chris@16
|
1071
|
Chris@16
|
1072 // Differently from to_string this function dumps out
|
Chris@16
|
1073 // every bit of the internal representation (may be
|
Chris@16
|
1074 // useful for debugging purposes)
|
Chris@16
|
1075 //
|
Chris@16
|
1076 template <typename B, typename A, typename stringT>
|
Chris@16
|
1077 inline void
|
Chris@16
|
1078 dump_to_string(const dynamic_bitset<B, A>& b, stringT& s)
|
Chris@16
|
1079 {
|
Chris@16
|
1080 to_string_helper(b, s, true /* =dump_all*/);
|
Chris@16
|
1081 }
|
Chris@16
|
1082
|
Chris@16
|
1083 template <typename Block, typename Allocator, typename BlockOutputIterator>
|
Chris@16
|
1084 inline void
|
Chris@16
|
1085 to_block_range(const dynamic_bitset<Block, Allocator>& b,
|
Chris@16
|
1086 BlockOutputIterator result)
|
Chris@16
|
1087 {
|
Chris@16
|
1088 // note how this copies *all* bits, including the
|
Chris@16
|
1089 // unused ones in the last block (which are zero)
|
Chris@16
|
1090 std::copy(b.m_bits.begin(), b.m_bits.end(), result);
|
Chris@16
|
1091 }
|
Chris@16
|
1092
|
Chris@16
|
1093 template <typename Block, typename Allocator>
|
Chris@16
|
1094 unsigned long dynamic_bitset<Block, Allocator>::
|
Chris@16
|
1095 to_ulong() const
|
Chris@16
|
1096 {
|
Chris@16
|
1097
|
Chris@16
|
1098 if (m_num_bits == 0)
|
Chris@16
|
1099 return 0; // convention
|
Chris@16
|
1100
|
Chris@16
|
1101 // Check for overflows. This may be a performance burden on very
|
Chris@16
|
1102 // large bitsets but is required by the specification, sorry
|
Chris@16
|
1103 if (find_next(ulong_width - 1) != npos)
|
Chris@16
|
1104 throw std::overflow_error("boost::dynamic_bitset::to_ulong overflow");
|
Chris@16
|
1105
|
Chris@16
|
1106
|
Chris@16
|
1107 // Ok, from now on we can be sure there's no "on" bit
|
Chris@16
|
1108 // beyond the "allowed" positions
|
Chris@16
|
1109 typedef unsigned long result_type;
|
Chris@16
|
1110
|
Chris@16
|
1111 const size_type maximum_size =
|
Chris@16
|
1112 (std::min)(m_num_bits, static_cast<size_type>(ulong_width));
|
Chris@16
|
1113
|
Chris@16
|
1114 const size_type last_block = block_index( maximum_size - 1 );
|
Chris@16
|
1115
|
Chris@16
|
1116 assert((last_block * bits_per_block) < static_cast<size_type>(ulong_width));
|
Chris@16
|
1117
|
Chris@16
|
1118 result_type result = 0;
|
Chris@16
|
1119 for (size_type i = 0; i <= last_block; ++i) {
|
Chris@16
|
1120 const size_type offset = i * bits_per_block;
|
Chris@16
|
1121 result |= (static_cast<result_type>(m_bits[i]) << offset);
|
Chris@16
|
1122 }
|
Chris@16
|
1123
|
Chris@16
|
1124 return result;
|
Chris@16
|
1125 }
|
Chris@16
|
1126
|
Chris@16
|
1127 template <typename Block, typename Allocator>
|
Chris@16
|
1128 inline typename dynamic_bitset<Block, Allocator>::size_type
|
Chris@16
|
1129 dynamic_bitset<Block, Allocator>::size() const
|
Chris@16
|
1130 {
|
Chris@16
|
1131 return m_num_bits;
|
Chris@16
|
1132 }
|
Chris@16
|
1133
|
Chris@16
|
1134 template <typename Block, typename Allocator>
|
Chris@16
|
1135 inline typename dynamic_bitset<Block, Allocator>::size_type
|
Chris@16
|
1136 dynamic_bitset<Block, Allocator>::num_blocks() const
|
Chris@16
|
1137 {
|
Chris@16
|
1138 return m_bits.size();
|
Chris@16
|
1139 }
|
Chris@16
|
1140
|
Chris@16
|
1141 template <typename Block, typename Allocator>
|
Chris@16
|
1142 inline typename dynamic_bitset<Block, Allocator>::size_type
|
Chris@16
|
1143 dynamic_bitset<Block, Allocator>::max_size() const
|
Chris@16
|
1144 {
|
Chris@16
|
1145 // Semantics of vector<>::max_size() aren't very clear
|
Chris@16
|
1146 // (see lib issue 197) and many library implementations
|
Chris@16
|
1147 // simply return dummy values, _unrelated_ to the underlying
|
Chris@16
|
1148 // allocator.
|
Chris@16
|
1149 //
|
Chris@16
|
1150 // Given these problems, I was tempted to not provide this
|
Chris@16
|
1151 // function at all but the user could need it if he provides
|
Chris@16
|
1152 // his own allocator.
|
Chris@16
|
1153 //
|
Chris@16
|
1154
|
Chris@16
|
1155 const size_type m = detail::dynamic_bitset_impl::
|
Chris@16
|
1156 vector_max_size_workaround(m_bits);
|
Chris@16
|
1157
|
Chris@16
|
1158 return m <= (size_type(-1)/bits_per_block) ?
|
Chris@16
|
1159 m * bits_per_block :
|
Chris@16
|
1160 size_type(-1);
|
Chris@16
|
1161 }
|
Chris@16
|
1162
|
Chris@16
|
1163 template <typename Block, typename Allocator>
|
Chris@16
|
1164 inline bool dynamic_bitset<Block, Allocator>::empty() const
|
Chris@16
|
1165 {
|
Chris@16
|
1166 return size() == 0;
|
Chris@16
|
1167 }
|
Chris@16
|
1168
|
Chris@16
|
1169 template <typename Block, typename Allocator>
|
Chris@16
|
1170 bool dynamic_bitset<Block, Allocator>::
|
Chris@16
|
1171 is_subset_of(const dynamic_bitset<Block, Allocator>& a) const
|
Chris@16
|
1172 {
|
Chris@16
|
1173 assert(size() == a.size());
|
Chris@16
|
1174 for (size_type i = 0; i < num_blocks(); ++i)
|
Chris@16
|
1175 if (m_bits[i] & ~a.m_bits[i])
|
Chris@16
|
1176 return false;
|
Chris@16
|
1177 return true;
|
Chris@16
|
1178 }
|
Chris@16
|
1179
|
Chris@16
|
1180 template <typename Block, typename Allocator>
|
Chris@16
|
1181 bool dynamic_bitset<Block, Allocator>::
|
Chris@16
|
1182 is_proper_subset_of(const dynamic_bitset<Block, Allocator>& a) const
|
Chris@16
|
1183 {
|
Chris@16
|
1184 assert(size() == a.size());
|
Chris@16
|
1185 assert(num_blocks() == a.num_blocks());
|
Chris@16
|
1186
|
Chris@16
|
1187 bool proper = false;
|
Chris@16
|
1188 for (size_type i = 0; i < num_blocks(); ++i) {
|
Chris@16
|
1189 const Block & bt = m_bits[i];
|
Chris@16
|
1190 const Block & ba = a.m_bits[i];
|
Chris@16
|
1191
|
Chris@16
|
1192 if (bt & ~ba)
|
Chris@16
|
1193 return false; // not a subset at all
|
Chris@16
|
1194 if (ba & ~bt)
|
Chris@16
|
1195 proper = true;
|
Chris@16
|
1196 }
|
Chris@16
|
1197 return proper;
|
Chris@16
|
1198 }
|
Chris@16
|
1199
|
Chris@16
|
1200 template <typename Block, typename Allocator>
|
Chris@16
|
1201 bool dynamic_bitset<Block, Allocator>::intersects(const dynamic_bitset & b) const
|
Chris@16
|
1202 {
|
Chris@16
|
1203 size_type common_blocks = num_blocks() < b.num_blocks()
|
Chris@16
|
1204 ? num_blocks() : b.num_blocks();
|
Chris@16
|
1205
|
Chris@16
|
1206 for(size_type i = 0; i < common_blocks; ++i) {
|
Chris@16
|
1207 if(m_bits[i] & b.m_bits[i])
|
Chris@16
|
1208 return true;
|
Chris@16
|
1209 }
|
Chris@16
|
1210 return false;
|
Chris@16
|
1211 }
|
Chris@16
|
1212
|
Chris@16
|
1213 // --------------------------------
|
Chris@16
|
1214 // lookup
|
Chris@16
|
1215
|
Chris@16
|
1216
|
Chris@16
|
1217 // look for the first bit "on", starting
|
Chris@16
|
1218 // from the block with index first_block
|
Chris@16
|
1219 //
|
Chris@16
|
1220 template <typename Block, typename Allocator>
|
Chris@16
|
1221 typename dynamic_bitset<Block, Allocator>::size_type
|
Chris@16
|
1222 dynamic_bitset<Block, Allocator>::m_do_find_from(size_type first_block) const
|
Chris@16
|
1223 {
|
Chris@16
|
1224 size_type i = first_block;
|
Chris@16
|
1225
|
Chris@16
|
1226 // skip null blocks
|
Chris@16
|
1227 while (i < num_blocks() && m_bits[i] == 0)
|
Chris@16
|
1228 ++i;
|
Chris@16
|
1229
|
Chris@16
|
1230 if (i >= num_blocks())
|
Chris@16
|
1231 return npos; // not found
|
Chris@16
|
1232
|
Chris@16
|
1233 return i * bits_per_block + boost::lowest_bit(m_bits[i]);
|
Chris@16
|
1234
|
Chris@16
|
1235 }
|
Chris@16
|
1236
|
Chris@16
|
1237
|
Chris@16
|
1238 template <typename Block, typename Allocator>
|
Chris@16
|
1239 typename dynamic_bitset<Block, Allocator>::size_type
|
Chris@16
|
1240 dynamic_bitset<Block, Allocator>::find_first() const
|
Chris@16
|
1241 {
|
Chris@16
|
1242 return m_do_find_from(0);
|
Chris@16
|
1243 }
|
Chris@16
|
1244
|
Chris@16
|
1245
|
Chris@16
|
1246 template <typename Block, typename Allocator>
|
Chris@16
|
1247 typename dynamic_bitset<Block, Allocator>::size_type
|
Chris@16
|
1248 dynamic_bitset<Block, Allocator>::find_next(size_type pos) const
|
Chris@16
|
1249 {
|
Chris@16
|
1250
|
Chris@16
|
1251 const size_type sz = size();
|
Chris@16
|
1252 if (pos >= (sz-1) || sz == 0)
|
Chris@16
|
1253 return npos;
|
Chris@16
|
1254
|
Chris@16
|
1255 ++pos;
|
Chris@16
|
1256
|
Chris@16
|
1257 const size_type blk = block_index(pos);
|
Chris@16
|
1258 const block_width_type ind = bit_index(pos);
|
Chris@16
|
1259
|
Chris@16
|
1260 // mask out bits before pos
|
Chris@16
|
1261 const Block fore = m_bits[blk] & ( ~Block(0) << ind );
|
Chris@16
|
1262
|
Chris@16
|
1263 return fore?
|
Chris@16
|
1264 blk * bits_per_block + lowest_bit(fore)
|
Chris@16
|
1265 :
|
Chris@16
|
1266 m_do_find_from(blk + 1);
|
Chris@16
|
1267
|
Chris@16
|
1268 }
|
Chris@16
|
1269
|
Chris@16
|
1270
|
Chris@16
|
1271
|
Chris@16
|
1272 //-----------------------------------------------------------------------------
|
Chris@16
|
1273 // comparison
|
Chris@16
|
1274
|
Chris@16
|
1275 template <typename Block, typename Allocator>
|
Chris@16
|
1276 bool operator==(const dynamic_bitset<Block, Allocator>& a,
|
Chris@16
|
1277 const dynamic_bitset<Block, Allocator>& b)
|
Chris@16
|
1278 {
|
Chris@16
|
1279 return (a.m_num_bits == b.m_num_bits)
|
Chris@16
|
1280 && (a.m_bits == b.m_bits);
|
Chris@16
|
1281 }
|
Chris@16
|
1282
|
Chris@16
|
1283 template <typename Block, typename Allocator>
|
Chris@16
|
1284 inline bool operator!=(const dynamic_bitset<Block, Allocator>& a,
|
Chris@16
|
1285 const dynamic_bitset<Block, Allocator>& b)
|
Chris@16
|
1286 {
|
Chris@16
|
1287 return !(a == b);
|
Chris@16
|
1288 }
|
Chris@16
|
1289
|
Chris@16
|
1290 template <typename Block, typename Allocator>
|
Chris@16
|
1291 bool operator<(const dynamic_bitset<Block, Allocator>& a,
|
Chris@16
|
1292 const dynamic_bitset<Block, Allocator>& b)
|
Chris@16
|
1293 {
|
Chris@16
|
1294 assert(a.size() == b.size());
|
Chris@16
|
1295 typedef typename dynamic_bitset<Block, Allocator>::size_type size_type;
|
Chris@16
|
1296
|
Chris@16
|
1297 //if (a.size() == 0)
|
Chris@16
|
1298 // return false;
|
Chris@16
|
1299
|
Chris@16
|
1300 // Since we are storing the most significant bit
|
Chris@16
|
1301 // at pos == size() - 1, we need to do the comparisons in reverse.
|
Chris@16
|
1302 //
|
Chris@16
|
1303 for (size_type ii = a.num_blocks(); ii > 0; --ii) {
|
Chris@16
|
1304 size_type i = ii-1;
|
Chris@16
|
1305 if (a.m_bits[i] < b.m_bits[i])
|
Chris@16
|
1306 return true;
|
Chris@16
|
1307 else if (a.m_bits[i] > b.m_bits[i])
|
Chris@16
|
1308 return false;
|
Chris@16
|
1309 }
|
Chris@16
|
1310 return false;
|
Chris@16
|
1311 }
|
Chris@16
|
1312
|
Chris@16
|
1313 template <typename Block, typename Allocator>
|
Chris@16
|
1314 inline bool operator<=(const dynamic_bitset<Block, Allocator>& a,
|
Chris@16
|
1315 const dynamic_bitset<Block, Allocator>& b)
|
Chris@16
|
1316 {
|
Chris@16
|
1317 return !(a > b);
|
Chris@16
|
1318 }
|
Chris@16
|
1319
|
Chris@16
|
1320 template <typename Block, typename Allocator>
|
Chris@16
|
1321 inline bool operator>(const dynamic_bitset<Block, Allocator>& a,
|
Chris@16
|
1322 const dynamic_bitset<Block, Allocator>& b)
|
Chris@16
|
1323 {
|
Chris@16
|
1324 return b < a;
|
Chris@16
|
1325 }
|
Chris@16
|
1326
|
Chris@16
|
1327 template <typename Block, typename Allocator>
|
Chris@16
|
1328 inline bool operator>=(const dynamic_bitset<Block, Allocator>& a,
|
Chris@16
|
1329 const dynamic_bitset<Block, Allocator>& b)
|
Chris@16
|
1330 {
|
Chris@16
|
1331 return !(a < b);
|
Chris@16
|
1332 }
|
Chris@16
|
1333
|
Chris@16
|
1334 //-----------------------------------------------------------------------------
|
Chris@16
|
1335 // stream operations
|
Chris@16
|
1336
|
Chris@16
|
1337 #ifdef BOOST_OLD_IOSTREAMS
|
Chris@16
|
1338 template < typename Block, typename Alloc>
|
Chris@16
|
1339 std::ostream&
|
Chris@16
|
1340 operator<<(std::ostream& os, const dynamic_bitset<Block, Alloc>& b)
|
Chris@16
|
1341 {
|
Chris@16
|
1342 // NOTE: since this is aimed at "classic" iostreams, exception
|
Chris@16
|
1343 // masks on the stream are not supported. The library that
|
Chris@16
|
1344 // ships with gcc 2.95 has an exceptions() member function but
|
Chris@16
|
1345 // nothing is actually implemented; not even the class ios::failure.
|
Chris@16
|
1346
|
Chris@16
|
1347 using namespace std;
|
Chris@16
|
1348
|
Chris@16
|
1349 const ios::iostate ok = ios::goodbit;
|
Chris@16
|
1350 ios::iostate err = ok;
|
Chris@16
|
1351
|
Chris@16
|
1352 if (os.opfx()) {
|
Chris@16
|
1353
|
Chris@16
|
1354 //try
|
Chris@16
|
1355 typedef typename dynamic_bitset<Block, Alloc>::size_type bitsetsize_type;
|
Chris@16
|
1356
|
Chris@16
|
1357 const bitsetsize_type sz = b.size();
|
Chris@16
|
1358 std::streambuf * buf = os.rdbuf();
|
Chris@16
|
1359 size_t npad = os.width() <= 0 // careful: os.width() is signed (and can be < 0)
|
Chris@16
|
1360 || (bitsetsize_type) os.width() <= sz? 0 : os.width() - sz;
|
Chris@16
|
1361
|
Chris@16
|
1362 const char fill_char = os.fill();
|
Chris@16
|
1363 const ios::fmtflags adjustfield = os.flags() & ios::adjustfield;
|
Chris@16
|
1364
|
Chris@16
|
1365 // if needed fill at left; pad is decresed along the way
|
Chris@16
|
1366 if (adjustfield != ios::left) {
|
Chris@16
|
1367 for (; 0 < npad; --npad)
|
Chris@16
|
1368 if (fill_char != buf->sputc(fill_char)) {
|
Chris@16
|
1369 err |= ios::failbit;
|
Chris@16
|
1370 break;
|
Chris@16
|
1371 }
|
Chris@16
|
1372 }
|
Chris@16
|
1373
|
Chris@16
|
1374 if (err == ok) {
|
Chris@16
|
1375 // output the bitset
|
Chris@16
|
1376 for (bitsetsize_type i = b.size(); 0 < i; --i) {
|
Chris@16
|
1377 const char dig = b.test(i-1)? '1' : '0';
|
Chris@16
|
1378 if (EOF == buf->sputc(dig)) {
|
Chris@16
|
1379 err |= ios::failbit;
|
Chris@16
|
1380 break;
|
Chris@16
|
1381 }
|
Chris@16
|
1382 }
|
Chris@16
|
1383 }
|
Chris@16
|
1384
|
Chris@16
|
1385 if (err == ok) {
|
Chris@16
|
1386 // if needed fill at right
|
Chris@16
|
1387 for (; 0 < npad; --npad) {
|
Chris@16
|
1388 if (fill_char != buf->sputc(fill_char)) {
|
Chris@16
|
1389 err |= ios::failbit;
|
Chris@16
|
1390 break;
|
Chris@16
|
1391 }
|
Chris@16
|
1392 }
|
Chris@16
|
1393 }
|
Chris@16
|
1394
|
Chris@16
|
1395 os.osfx();
|
Chris@16
|
1396 os.width(0);
|
Chris@16
|
1397
|
Chris@16
|
1398 } // if opfx
|
Chris@16
|
1399
|
Chris@16
|
1400 if(err != ok)
|
Chris@16
|
1401 os.setstate(err); // assume this does NOT throw
|
Chris@16
|
1402 return os;
|
Chris@16
|
1403
|
Chris@16
|
1404 }
|
Chris@16
|
1405 #else
|
Chris@16
|
1406
|
Chris@16
|
1407 template <typename Ch, typename Tr, typename Block, typename Alloc>
|
Chris@16
|
1408 std::basic_ostream<Ch, Tr>&
|
Chris@16
|
1409 operator<<(std::basic_ostream<Ch, Tr>& os,
|
Chris@16
|
1410 const dynamic_bitset<Block, Alloc>& b)
|
Chris@16
|
1411 {
|
Chris@16
|
1412
|
Chris@16
|
1413 using namespace std;
|
Chris@16
|
1414
|
Chris@16
|
1415 const ios_base::iostate ok = ios_base::goodbit;
|
Chris@16
|
1416 ios_base::iostate err = ok;
|
Chris@16
|
1417
|
Chris@16
|
1418 typename basic_ostream<Ch, Tr>::sentry cerberos(os);
|
Chris@16
|
1419 if (cerberos) {
|
Chris@16
|
1420
|
Chris@16
|
1421 BOOST_DYNAMIC_BITSET_CTYPE_FACET(Ch, fac, os.getloc());
|
Chris@16
|
1422 const Ch zero = BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '0');
|
Chris@16
|
1423 const Ch one = BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '1');
|
Chris@16
|
1424
|
Chris@16
|
1425 try {
|
Chris@16
|
1426
|
Chris@16
|
1427 typedef typename dynamic_bitset<Block, Alloc>::size_type bitsetsize_type;
|
Chris@16
|
1428 typedef basic_streambuf<Ch, Tr> buffer_type;
|
Chris@16
|
1429
|
Chris@16
|
1430 buffer_type * buf = os.rdbuf();
|
Chris@16
|
1431 size_t npad = os.width() <= 0 // careful: os.width() is signed (and can be < 0)
|
Chris@16
|
1432 || (bitsetsize_type) os.width() <= b.size()? 0 : os.width() - b.size();
|
Chris@16
|
1433
|
Chris@16
|
1434 const Ch fill_char = os.fill();
|
Chris@16
|
1435 const ios_base::fmtflags adjustfield = os.flags() & ios_base::adjustfield;
|
Chris@16
|
1436
|
Chris@16
|
1437 // if needed fill at left; pad is decresed along the way
|
Chris@16
|
1438 if (adjustfield != ios_base::left) {
|
Chris@16
|
1439 for (; 0 < npad; --npad)
|
Chris@16
|
1440 if (Tr::eq_int_type(Tr::eof(), buf->sputc(fill_char))) {
|
Chris@16
|
1441 err |= ios_base::failbit;
|
Chris@16
|
1442 break;
|
Chris@16
|
1443 }
|
Chris@16
|
1444 }
|
Chris@16
|
1445
|
Chris@16
|
1446 if (err == ok) {
|
Chris@16
|
1447 // output the bitset
|
Chris@16
|
1448 for (bitsetsize_type i = b.size(); 0 < i; --i) {
|
Chris@16
|
1449 typename buffer_type::int_type
|
Chris@16
|
1450 ret = buf->sputc(b.test(i-1)? one : zero);
|
Chris@16
|
1451 if (Tr::eq_int_type(Tr::eof(), ret)) {
|
Chris@16
|
1452 err |= ios_base::failbit;
|
Chris@16
|
1453 break;
|
Chris@16
|
1454 }
|
Chris@16
|
1455 }
|
Chris@16
|
1456 }
|
Chris@16
|
1457
|
Chris@16
|
1458 if (err == ok) {
|
Chris@16
|
1459 // if needed fill at right
|
Chris@16
|
1460 for (; 0 < npad; --npad) {
|
Chris@16
|
1461 if (Tr::eq_int_type(Tr::eof(), buf->sputc(fill_char))) {
|
Chris@16
|
1462 err |= ios_base::failbit;
|
Chris@16
|
1463 break;
|
Chris@16
|
1464 }
|
Chris@16
|
1465 }
|
Chris@16
|
1466 }
|
Chris@16
|
1467
|
Chris@16
|
1468
|
Chris@16
|
1469 os.width(0);
|
Chris@16
|
1470
|
Chris@16
|
1471 } catch (...) { // see std 27.6.1.1/4
|
Chris@16
|
1472 bool rethrow = false;
|
Chris@16
|
1473 try { os.setstate(ios_base::failbit); } catch (...) { rethrow = true; }
|
Chris@16
|
1474
|
Chris@16
|
1475 if (rethrow)
|
Chris@16
|
1476 throw;
|
Chris@16
|
1477 }
|
Chris@16
|
1478 }
|
Chris@16
|
1479
|
Chris@16
|
1480 if(err != ok)
|
Chris@16
|
1481 os.setstate(err); // may throw exception
|
Chris@16
|
1482 return os;
|
Chris@16
|
1483
|
Chris@16
|
1484 }
|
Chris@16
|
1485 #endif
|
Chris@16
|
1486
|
Chris@16
|
1487
|
Chris@16
|
1488 #ifdef BOOST_OLD_IOSTREAMS
|
Chris@16
|
1489
|
Chris@16
|
1490 // A sentry-like class that calls isfx in its destructor.
|
Chris@16
|
1491 // "Necessary" because bit_appender::do_append may throw.
|
Chris@16
|
1492 class pseudo_sentry {
|
Chris@16
|
1493 std::istream & m_r;
|
Chris@16
|
1494 const bool m_ok;
|
Chris@16
|
1495 public:
|
Chris@16
|
1496 explicit pseudo_sentry(std::istream & r) : m_r(r), m_ok(r.ipfx(0)) { }
|
Chris@16
|
1497 ~pseudo_sentry() { m_r.isfx(); }
|
Chris@16
|
1498 operator bool() const { return m_ok; }
|
Chris@16
|
1499 };
|
Chris@16
|
1500
|
Chris@16
|
1501 template <typename Block, typename Alloc>
|
Chris@16
|
1502 std::istream&
|
Chris@16
|
1503 operator>>(std::istream& is, dynamic_bitset<Block, Alloc>& b)
|
Chris@16
|
1504 {
|
Chris@16
|
1505
|
Chris@16
|
1506 // Extractor for classic IO streams (libstdc++ < 3.0)
|
Chris@16
|
1507 // ----------------------------------------------------//
|
Chris@16
|
1508 // It's assumed that the stream buffer functions, and
|
Chris@16
|
1509 // the stream's setstate() _cannot_ throw.
|
Chris@16
|
1510
|
Chris@16
|
1511
|
Chris@16
|
1512 typedef dynamic_bitset<Block, Alloc> bitset_type;
|
Chris@16
|
1513 typedef typename bitset_type::size_type size_type;
|
Chris@16
|
1514
|
Chris@16
|
1515 std::ios::iostate err = std::ios::goodbit;
|
Chris@16
|
1516 pseudo_sentry cerberos(is); // skips whitespaces
|
Chris@16
|
1517 if(cerberos) {
|
Chris@16
|
1518
|
Chris@16
|
1519 b.clear();
|
Chris@16
|
1520
|
Chris@16
|
1521 const std::streamsize w = is.width();
|
Chris@16
|
1522 const size_type limit = w > 0 && static_cast<size_type>(w) < b.max_size()
|
Chris@16
|
1523 ? w : b.max_size();
|
Chris@16
|
1524 typename bitset_type::bit_appender appender(b);
|
Chris@16
|
1525 std::streambuf * buf = is.rdbuf();
|
Chris@16
|
1526 for(int c = buf->sgetc(); appender.get_count() < limit; c = buf->snextc() ) {
|
Chris@16
|
1527
|
Chris@16
|
1528 if (c == EOF) {
|
Chris@16
|
1529 err |= std::ios::eofbit;
|
Chris@16
|
1530 break;
|
Chris@16
|
1531 }
|
Chris@16
|
1532 else if (char(c) != '0' && char(c) != '1')
|
Chris@16
|
1533 break; // non digit character
|
Chris@16
|
1534
|
Chris@16
|
1535 else {
|
Chris@16
|
1536 try {
|
Chris@16
|
1537 appender.do_append(char(c) == '1');
|
Chris@16
|
1538 }
|
Chris@16
|
1539 catch(...) {
|
Chris@16
|
1540 is.setstate(std::ios::failbit); // assume this can't throw
|
Chris@16
|
1541 throw;
|
Chris@16
|
1542 }
|
Chris@16
|
1543 }
|
Chris@16
|
1544
|
Chris@16
|
1545 } // for
|
Chris@16
|
1546 }
|
Chris@16
|
1547
|
Chris@16
|
1548 is.width(0);
|
Chris@16
|
1549 if (b.size() == 0)
|
Chris@16
|
1550 err |= std::ios::failbit;
|
Chris@16
|
1551 if (err != std::ios::goodbit)
|
Chris@16
|
1552 is.setstate (err); // may throw
|
Chris@16
|
1553
|
Chris@16
|
1554 return is;
|
Chris@16
|
1555 }
|
Chris@16
|
1556
|
Chris@16
|
1557 #else // BOOST_OLD_IOSTREAMS
|
Chris@16
|
1558
|
Chris@16
|
1559 template <typename Ch, typename Tr, typename Block, typename Alloc>
|
Chris@16
|
1560 std::basic_istream<Ch, Tr>&
|
Chris@16
|
1561 operator>>(std::basic_istream<Ch, Tr>& is, dynamic_bitset<Block, Alloc>& b)
|
Chris@16
|
1562 {
|
Chris@16
|
1563
|
Chris@16
|
1564 using namespace std;
|
Chris@16
|
1565
|
Chris@16
|
1566 typedef dynamic_bitset<Block, Alloc> bitset_type;
|
Chris@16
|
1567 typedef typename bitset_type::size_type size_type;
|
Chris@16
|
1568
|
Chris@16
|
1569 const streamsize w = is.width();
|
Chris@16
|
1570 const size_type limit = 0 < w && static_cast<size_type>(w) < b.max_size()?
|
Chris@16
|
1571 w : b.max_size();
|
Chris@16
|
1572
|
Chris@16
|
1573 ios_base::iostate err = ios_base::goodbit;
|
Chris@16
|
1574 typename basic_istream<Ch, Tr>::sentry cerberos(is); // skips whitespaces
|
Chris@16
|
1575 if(cerberos) {
|
Chris@16
|
1576
|
Chris@16
|
1577 // in accordance with prop. resol. of lib DR 303 [last checked 4 Feb 2004]
|
Chris@16
|
1578 BOOST_DYNAMIC_BITSET_CTYPE_FACET(Ch, fac, is.getloc());
|
Chris@16
|
1579 const Ch zero = BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '0');
|
Chris@16
|
1580 const Ch one = BOOST_DYNAMIC_BITSET_WIDEN_CHAR(fac, '1');
|
Chris@16
|
1581
|
Chris@16
|
1582 b.clear();
|
Chris@16
|
1583 try {
|
Chris@16
|
1584 typename bitset_type::bit_appender appender(b);
|
Chris@16
|
1585 basic_streambuf <Ch, Tr> * buf = is.rdbuf();
|
Chris@16
|
1586 typename Tr::int_type c = buf->sgetc();
|
Chris@16
|
1587 for( ; appender.get_count() < limit; c = buf->snextc() ) {
|
Chris@16
|
1588
|
Chris@16
|
1589 if (Tr::eq_int_type(Tr::eof(), c)) {
|
Chris@16
|
1590 err |= ios_base::eofbit;
|
Chris@16
|
1591 break;
|
Chris@16
|
1592 }
|
Chris@16
|
1593 else {
|
Chris@16
|
1594 const Ch to_c = Tr::to_char_type(c);
|
Chris@16
|
1595 const bool is_one = Tr::eq(to_c, one);
|
Chris@16
|
1596
|
Chris@16
|
1597 if (!is_one && !Tr::eq(to_c, zero))
|
Chris@16
|
1598 break; // non digit character
|
Chris@16
|
1599
|
Chris@16
|
1600 appender.do_append(is_one);
|
Chris@16
|
1601
|
Chris@16
|
1602 }
|
Chris@16
|
1603
|
Chris@16
|
1604 } // for
|
Chris@16
|
1605 }
|
Chris@16
|
1606 catch (...) {
|
Chris@16
|
1607 // catches from stream buf, or from vector:
|
Chris@16
|
1608 //
|
Chris@16
|
1609 // bits_stored bits have been extracted and stored, and
|
Chris@16
|
1610 // either no further character is extractable or we can't
|
Chris@16
|
1611 // append to the underlying vector (out of memory)
|
Chris@16
|
1612
|
Chris@16
|
1613 bool rethrow = false; // see std 27.6.1.1/4
|
Chris@16
|
1614 try { is.setstate(ios_base::badbit); }
|
Chris@16
|
1615 catch(...) { rethrow = true; }
|
Chris@16
|
1616
|
Chris@16
|
1617 if (rethrow)
|
Chris@16
|
1618 throw;
|
Chris@16
|
1619
|
Chris@16
|
1620 }
|
Chris@16
|
1621 }
|
Chris@16
|
1622
|
Chris@16
|
1623 is.width(0);
|
Chris@16
|
1624 if (b.size() == 0 /*|| !cerberos*/)
|
Chris@16
|
1625 err |= ios_base::failbit;
|
Chris@16
|
1626 if (err != ios_base::goodbit)
|
Chris@16
|
1627 is.setstate (err); // may throw
|
Chris@16
|
1628
|
Chris@16
|
1629 return is;
|
Chris@16
|
1630
|
Chris@16
|
1631 }
|
Chris@16
|
1632
|
Chris@16
|
1633
|
Chris@16
|
1634 #endif
|
Chris@16
|
1635
|
Chris@16
|
1636
|
Chris@16
|
1637 //-----------------------------------------------------------------------------
|
Chris@16
|
1638 // bitset operations
|
Chris@16
|
1639
|
Chris@16
|
1640 template <typename Block, typename Allocator>
|
Chris@16
|
1641 dynamic_bitset<Block, Allocator>
|
Chris@16
|
1642 operator&(const dynamic_bitset<Block, Allocator>& x,
|
Chris@16
|
1643 const dynamic_bitset<Block, Allocator>& y)
|
Chris@16
|
1644 {
|
Chris@16
|
1645 dynamic_bitset<Block, Allocator> b(x);
|
Chris@16
|
1646 return b &= y;
|
Chris@16
|
1647 }
|
Chris@16
|
1648
|
Chris@16
|
1649 template <typename Block, typename Allocator>
|
Chris@16
|
1650 dynamic_bitset<Block, Allocator>
|
Chris@16
|
1651 operator|(const dynamic_bitset<Block, Allocator>& x,
|
Chris@16
|
1652 const dynamic_bitset<Block, Allocator>& y)
|
Chris@16
|
1653 {
|
Chris@16
|
1654 dynamic_bitset<Block, Allocator> b(x);
|
Chris@16
|
1655 return b |= y;
|
Chris@16
|
1656 }
|
Chris@16
|
1657
|
Chris@16
|
1658 template <typename Block, typename Allocator>
|
Chris@16
|
1659 dynamic_bitset<Block, Allocator>
|
Chris@16
|
1660 operator^(const dynamic_bitset<Block, Allocator>& x,
|
Chris@16
|
1661 const dynamic_bitset<Block, Allocator>& y)
|
Chris@16
|
1662 {
|
Chris@16
|
1663 dynamic_bitset<Block, Allocator> b(x);
|
Chris@16
|
1664 return b ^= y;
|
Chris@16
|
1665 }
|
Chris@16
|
1666
|
Chris@16
|
1667 template <typename Block, typename Allocator>
|
Chris@16
|
1668 dynamic_bitset<Block, Allocator>
|
Chris@16
|
1669 operator-(const dynamic_bitset<Block, Allocator>& x,
|
Chris@16
|
1670 const dynamic_bitset<Block, Allocator>& y)
|
Chris@16
|
1671 {
|
Chris@16
|
1672 dynamic_bitset<Block, Allocator> b(x);
|
Chris@16
|
1673 return b -= y;
|
Chris@16
|
1674 }
|
Chris@16
|
1675
|
Chris@16
|
1676 //-----------------------------------------------------------------------------
|
Chris@16
|
1677 // namespace scope swap
|
Chris@16
|
1678
|
Chris@16
|
1679 template<typename Block, typename Allocator>
|
Chris@16
|
1680 inline void
|
Chris@16
|
1681 swap(dynamic_bitset<Block, Allocator>& left,
|
Chris@16
|
1682 dynamic_bitset<Block, Allocator>& right) // no throw
|
Chris@16
|
1683 {
|
Chris@16
|
1684 left.swap(right);
|
Chris@16
|
1685 }
|
Chris@16
|
1686
|
Chris@16
|
1687
|
Chris@16
|
1688 //-----------------------------------------------------------------------------
|
Chris@16
|
1689 // private (on conforming compilers) member functions
|
Chris@16
|
1690
|
Chris@16
|
1691
|
Chris@16
|
1692 template <typename Block, typename Allocator>
|
Chris@16
|
1693 inline typename dynamic_bitset<Block, Allocator>::size_type
|
Chris@16
|
1694 dynamic_bitset<Block, Allocator>::calc_num_blocks(size_type num_bits)
|
Chris@16
|
1695 {
|
Chris@16
|
1696 return num_bits / bits_per_block
|
Chris@16
|
1697 + static_cast<int>( num_bits % bits_per_block != 0 );
|
Chris@16
|
1698 }
|
Chris@16
|
1699
|
Chris@16
|
1700 // gives a reference to the highest block
|
Chris@16
|
1701 //
|
Chris@16
|
1702 template <typename Block, typename Allocator>
|
Chris@16
|
1703 inline Block& dynamic_bitset<Block, Allocator>::m_highest_block()
|
Chris@16
|
1704 {
|
Chris@16
|
1705 return const_cast<Block &>
|
Chris@16
|
1706 (static_cast<const dynamic_bitset *>(this)->m_highest_block());
|
Chris@16
|
1707 }
|
Chris@16
|
1708
|
Chris@16
|
1709 // gives a const-reference to the highest block
|
Chris@16
|
1710 //
|
Chris@16
|
1711 template <typename Block, typename Allocator>
|
Chris@16
|
1712 inline const Block& dynamic_bitset<Block, Allocator>::m_highest_block() const
|
Chris@16
|
1713 {
|
Chris@16
|
1714 assert(size() > 0 && num_blocks() > 0);
|
Chris@16
|
1715 return m_bits.back();
|
Chris@16
|
1716 }
|
Chris@16
|
1717
|
Chris@16
|
1718
|
Chris@16
|
1719 // If size() is not a multiple of bits_per_block
|
Chris@16
|
1720 // then not all the bits in the last block are used.
|
Chris@16
|
1721 // This function resets the unused bits (convenient
|
Chris@16
|
1722 // for the implementation of many member functions)
|
Chris@16
|
1723 //
|
Chris@16
|
1724 template <typename Block, typename Allocator>
|
Chris@16
|
1725 inline void dynamic_bitset<Block, Allocator>::m_zero_unused_bits()
|
Chris@16
|
1726 {
|
Chris@16
|
1727 assert (num_blocks() == calc_num_blocks(m_num_bits));
|
Chris@16
|
1728
|
Chris@16
|
1729 // if != 0 this is the number of bits used in the last block
|
Chris@16
|
1730 const block_width_type extra_bits = count_extra_bits();
|
Chris@16
|
1731
|
Chris@16
|
1732 if (extra_bits != 0)
|
Chris@16
|
1733 m_highest_block() &= ~(~static_cast<Block>(0) << extra_bits);
|
Chris@16
|
1734
|
Chris@16
|
1735 }
|
Chris@16
|
1736
|
Chris@16
|
1737 // check class invariants
|
Chris@16
|
1738 template <typename Block, typename Allocator>
|
Chris@16
|
1739 bool dynamic_bitset<Block, Allocator>::m_check_invariants() const
|
Chris@16
|
1740 {
|
Chris@16
|
1741 const block_width_type extra_bits = count_extra_bits();
|
Chris@16
|
1742 if (extra_bits > 0) {
|
Chris@16
|
1743 block_type const mask = (~static_cast<Block>(0) << extra_bits);
|
Chris@16
|
1744 if ((m_highest_block() & mask) != 0)
|
Chris@16
|
1745 return false;
|
Chris@16
|
1746 }
|
Chris@16
|
1747 if (m_bits.size() > m_bits.capacity() || num_blocks() != calc_num_blocks(size()))
|
Chris@16
|
1748 return false;
|
Chris@16
|
1749
|
Chris@16
|
1750 return true;
|
Chris@16
|
1751
|
Chris@16
|
1752 }
|
Chris@16
|
1753
|
Chris@16
|
1754
|
Chris@16
|
1755 } // namespace boost
|
Chris@16
|
1756
|
Chris@16
|
1757
|
Chris@16
|
1758 #undef BOOST_BITSET_CHAR
|
Chris@16
|
1759
|
Chris@16
|
1760 #endif // include guard
|
Chris@16
|
1761
|