strusBase  0.17
dynamic_bitset.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Patrick P. Frey
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
9 #ifndef _STRUS_DYNAMIC_BITSET_HPP_INCLUDED
10 #define _STRUS_DYNAMIC_BITSET_HPP_INCLUDED
11 #include "strus/base/bitset.hpp"
12 
14 #if defined __GNUC__
15 #define STRUS_GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
16 #endif
17 
18 #undef STRUS_USE_STD_DYNAMIC_BITSET
19 #if __cplusplus >= 201103L
20 #if defined __clang__
21 #define STRUS_USE_STD_DYNAMIC_BITSET
22 #elif defined __GNUC__
23 #if STRUS_GCC_VERSION >= 40900
24 #define STRUS_USE_STD_DYNAMIC_BITSET
25 #endif // STRUS_GCC_VERSION
26 #endif // __clang__
27 #endif // __cplusplus
28 
29 #ifdef STRUS_USE_STD_DYNAMIC_BITSET
30 #include <vector>
31 #include <algorithm>
32 namespace strus {
33 
36 class dynamic_bitset
37 {
38 public:
39  dynamic_bitset( std::size_t size_)
40  :m_indices( (size_+(ElementDim-1)) / ElementDim, -1),m_elements(){}
42  :m_indices(o.m_indices),m_elements(o.m_elements){}
43  void set( std::size_t n, bool val = true)
44  {
45  int hi = n / ElementDim;
46  int li = n % ElementDim;
47  int idx = m_indices[ hi];
48  if (idx == -1)
49  {
50  idx = m_indices[ hi] = m_elements.size();
51  m_elements.push_back( bitset<ElementDim>());
52  }
53  m_elements[ idx].set( li, val);
54  }
55  bool test( std::size_t n) const
56  {
57  int hi = n / ElementDim;
58  int li = n % ElementDim;
59  int idx = m_indices[ hi];
60  if (idx == -1) return false;
61  return m_elements.at(idx).test( li);
62  }
63  void reset()
64  {
65  m_elements.clear();
66  std::fill( m_indices.begin(), m_indices.end(), -1);
67  }
68 
69 private:
70  enum {ElementDim=256};
71  std::vector<int> m_indices;
72  std::vector<bitset<ElementDim> > m_elements;
73 };
74 }//namespace
75 
76 #else //STRUS_USE_STD_DYNAMIC_BITSET
77 #include <boost/dynamic_bitset.hpp>
78 
79 namespace strus {
80 
82  :public boost::dynamic_bitset<>
83 {
84 public:
85  dynamic_bitset( std::size_t size_)
86  :boost::dynamic_bitset<>( size_){}
88  :boost::dynamic_bitset<>( o){}
89 };
90 
91 }//namespace
92 #endif //STRUS_USE_STD_DYNAMIC_BITSET
93 
94 #endif
95 
96 
dynamic_bitset(const dynamic_bitset &o)
Definition: dynamic_bitset.hpp:87
dynamic_bitset(std::size_t size_)
Definition: dynamic_bitset.hpp:85
Definition: dynamic_bitset.hpp:81