BitSet.hpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2019 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* https://www.oorexx.org/license.html */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 /******************************************************************************/
39 /* */
40 /* Handy format-portable template class for bit sets */
41 /* */
42 /******************************************************************************/
43 
44 #ifndef BitSet_Included
45 #define BitSet_Included
46 
47 /**
48  * This is a replacement for the std::bitset class that is more
49  * portable. For classes that are saved via rexxc compilation
50  * that use FlagSets, the different compiler implementations of
51  * the bitset class so not interoperate well. This ensures we
52  * have a single data format for saved bitsets across all of the
53  * interpreters.
54  *
55  * This is also simpler than the std bitset, limited to just 32
56  * bits in the set and only implementing the operations required
57  * by the interpreter.
58  */
59 template<size_t TMaxBits> class BitSet
60 { // store fixed-length sequence of Boolean elements
61  public:
62 
63  // default constructor...uses all false values
64  BitSet() : bits(0) { }
65 
67  {
68  bits = ~((uint32_t)0);
69  return (*this);
70  }
71 
72  inline BitSet& set(size_t pos, bool val = true)
73  {
74  // belt and braces, don't allow beyond the specified number of bits
75  if (pos <= TMaxBits)
76  {
77  if (val)
78  {
79  bits |= (uint32_t)1 << pos;
80  }
81  else
82  {
83  bits &= ~((uint32_t)1 << pos);
84  }
85  }
86 
87  return (*this);
88  }
89 
91  {
92  bits = 0;
93  return (*this);
94  }
95 
96  inline BitSet& reset(size_t pos)
97  {
98  return set(pos, false);
99  }
100 
102  {
103  // flip the bits individually so we don't turn on any bits out of range
104  for (size_t i = 0; i < TMaxBits; i++)
105  {
106  flip(i);
107  }
108  return (*this);
109  }
110 
111  inline BitSet& flip(size_t pos)
112  {
113  // belt and braces, don't allow beyond the specified number of bits
114  if (pos <= TMaxBits)
115  {
116  bits ^= (uint32_t)1 << pos;
117  }
118  return (*this);
119  }
120 
121  inline bool test(size_t pos) const
122  {
123  // belt and braces, don't allow beyond the specified number of bits
124  if (pos <= TMaxBits)
125  {
126  return (bits & ((uint32_t)1 << pos)) != 0;
127  }
128  else
129  {
130  return false;
131  }
132  }
133 
134  bool any() const
135  {
136  return bits != 0;
137  }
138 
139  bool none() const
140  {
141  return bits == 0;
142  }
143 
144  bool all() const
145  {
146  return count() == TMaxBits;
147  }
148 
149 
150  private:
151  size_t count() const
152  {
153  size_t c = 0;
154  for (size_t i = 0; i < TMaxBits; i++)
155  {
156  if (test(i))
157  {
158  c++;
159  }
160  }
161  return c;
162  }
163 
164 
165 
166  uint32_t bits; // the bits in our bitset
167 };
168 
169 
170 #endif
171 
bool test(size_t pos) const
Definition: BitSet.hpp:121
bool any() const
Definition: BitSet.hpp:134
size_t count() const
Definition: BitSet.hpp:151
bool none() const
Definition: BitSet.hpp:139
BitSet & reset()
Definition: BitSet.hpp:90
BitSet()
Definition: BitSet.hpp:64
bool all() const
Definition: BitSet.hpp:144
BitSet & set()
Definition: BitSet.hpp:66
BitSet & set(size_t pos, bool val=true)
Definition: BitSet.hpp:72
BitSet & flip()
Definition: BitSet.hpp:101
BitSet & reset(size_t pos)
Definition: BitSet.hpp:96
BitSet & flip(size_t pos)
Definition: BitSet.hpp:111
uint32_t bits
Definition: BitSet.hpp:166
unsigned int uint32_t