The Original CHomP Software
bitsets.h
Go to the documentation of this file.
1
3
14
15// Copyright (C) 1997-2020 by Pawel Pilarczyk.
16//
17// This file is part of my research software package. This is free software:
18// you can redistribute it and/or modify it under the terms of the GNU
19// General Public License as published by the Free Software Foundation,
20// either version 3 of the License, or (at your option) any later version.
21//
22// This software is distributed in the hope that it will be useful,
23// but WITHOUT ANY WARRANTY; without even the implied warranty of
24// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25// GNU General Public License for more details.
26//
27// You should have received a copy of the GNU General Public License
28// along with this software; see the file "license.txt". If not,
29// please, see <https://www.gnu.org/licenses/>.
30
31// Started on February 22, 2007. Last revision: March 29, 2011.
32
33
34#ifndef _CHOMP_STRUCT_BITSETS_H_
35#define _CHOMP_STRUCT_BITSETS_H_
36
37#include "chomp/system/config.h"
38
39#include <iostream>
40#include <cstdlib>
41
42namespace chomp {
43namespace homology {
44
45
46// --------------------------------------------------
47// ------------------- BitSets ---------------------
48// --------------------------------------------------
49
58{
59 static const int_t longBits = sizeof (long) * 8;
60 static const int_t longBitMask = longBits - 1;
61 static const int_t longBitShift = (sizeof (long) == 8) ? 6 : 5;
62
63public:
68 BitSets (int_t _nsets, int_t _nelem);
69
71 BitSets (const BitSets &s);
72
74 BitSets &operator = (const BitSets &s);
75
77 ~BitSets ();
78
80 void add (int_t n, int_t e);
81
83 void remove (int_t n, int_t e);
84
86 bool check (int_t n, int_t e) const;
87
89 void addset (int_t n, int_t m);
90
93 int_t get (int_t n, int_t start = 0) const;
94
95private:
98
101
104
106 unsigned long *bits;
107
109 int_t arraySize ();
110
111}; /* BitSets */
112
113// --------------------------------------------------
114
116{
117 long size = static_cast<long> (nsets) * static_cast<long> (nunits);
118 if (static_cast<long> (static_cast<int_t> (size)) != size)
119 throw "Too large BitSets requested.";
120 return static_cast<int_t> (size);
121} /* BitSets::arraySize */
122
123inline BitSets::BitSets (int_t _nsets, int_t _nelem):
124 nsets (_nsets), nelem (_nelem),
125 nunits ((_nelem + (sizeof (unsigned long) << 3) - 1) /
126 (sizeof (unsigned long) << 3)), bits (0)
127{
128 int_t size = arraySize ();
129 bits = new unsigned long [size];
130 for (int_t i = 0; i < size; ++ i)
131 bits [i] = 0;
132 return;
133} /* BitSets::BitSets */
134
135inline BitSets::BitSets (const BitSets &s):
136 nsets (s. nsets), nelem (s. nelem), nunits (s. nunits), bits (0)
137{
138 int_t size = arraySize ();
139 bits = new unsigned long [size];
140 for (int_t i = 0; i < size; ++ i)
141 bits [i] = s. bits [i];
142 return;
143} /* BitSets::BitSets */
144
146{
147 if (this == &s)
148 return *this;
149 delete [] bits;
150 nsets = s. nsets;
151 nelem = s. nelem;
152 nunits = s. nunits;
153 int_t size = arraySize ();
154 bits = new unsigned long [size];
155 for (int_t i = 0; i < size; ++ i)
156 bits [i] = s. bits [i];
157 return *this;
158} /* BitSets::operator = */
159
161{
162 delete [] bits;
163 return;
164} /* BitSets::~BitSets */
165
166inline void BitSets::add (int_t n, int_t e)
167{
168// sbug << "Add " << e << " to " << n << ".\n";
169 unsigned long *buf = bits + n * nunits;
170 buf [e >> longBitShift] |= (1ul << (e & longBitMask));
171 return;
172} /* BitSets::add */
173
174inline void BitSets::remove (int_t n, int_t e)
175{
176 unsigned long *buf = bits + n * nunits;
177 buf [e >> longBitShift] &= ~(1ul << (e & longBitMask));
178 return;
179} /* BitSets::remove */
180
181inline bool BitSets::check (int_t n, int_t e) const
182{
183 unsigned long *buf = bits + n * nunits;
184 return !!(buf [e >> longBitShift] & (1ul << (e & longBitMask)));
185} /* BitSets::check */
186
187inline void BitSets::addset (int_t n, int_t m)
188{
189 unsigned long *nbuf = bits + n * nunits;
190 unsigned long *mbuf = bits + m * nunits;
191 for (int_t i = 0; i < nunits; ++ i)
192 *(nbuf ++) |= *(mbuf ++);
193 return;
194} /* BitSets::addset */
195
196inline int_t BitSets::get (int_t n, int_t start) const
197{
198 if (start >= nelem)
199 return -1;
200 unsigned long *buf = bits + n * nunits;
201 int_t offset = start >> longBitShift;
202 int bit = start & longBitMask;
203 if (buf [offset] & (~0ul << bit))
204 {
205 for (; bit < longBits; ++ bit)
206 {
207 if (buf [offset] & (1ul << bit))
208 return (offset << longBitShift) + bit;
209 }
210 throw "Wrong bit number in BitSets.";
211 }
212 for (++ offset; offset < nunits; ++ offset)
213 {
214 if (!buf [offset])
215 continue;
216 for (int bit = 0; bit < longBits; ++ bit)
217 {
218 if (buf [offset] & (1ul << bit))
219 return (offset << longBitShift) + bit;
220 }
221 throw "False bit in BitSets.";
222 }
223 return -1;
224} /* BitSets::get */
225
226
227} // namespace homology
228} // namespace chomp
229
230#endif // _CHOMP_STRUCT_BITSETS_H_
231
233
This class uses bit representation to store many small sets.
Definition: bitsets.h:58
int_t nunits
The number of array entries used for each set.
Definition: bitsets.h:103
unsigned long * bits
The memory buffer for storing the bits.
Definition: bitsets.h:106
void addset(int_t n, int_t m)
Adds the entire set 'm' to the set 'n'.
Definition: bitsets.h:187
void remove(int_t n, int_t e)
Removes an element from the given set.
Definition: bitsets.h:174
BitSets(int_t _nsets, int_t _nelem)
Constructor of a family of empty sets.
Definition: bitsets.h:123
int_t arraySize()
Computes the size of the memory array for storing the bits.
Definition: bitsets.h:115
~BitSets()
Destructor.
Definition: bitsets.h:160
int_t nelem
The maximal number of elements in each set.
Definition: bitsets.h:100
static const int_t longBitMask
Definition: bitsets.h:60
BitSets & operator=(const BitSets &s)
Assignment operator.
Definition: bitsets.h:145
static const int_t longBitShift
Definition: bitsets.h:61
bool check(int_t n, int_t e) const
Checks if an element belongs to the given set.
Definition: bitsets.h:181
static const int_t longBits
Definition: bitsets.h:59
void add(int_t n, int_t e)
Adds an element to the given set.
Definition: bitsets.h:166
int_t get(int_t n, int_t start=0) const
Retrieves an element >= 'start' in the given set.
Definition: bitsets.h:196
int_t nsets
The number of sets.
Definition: bitsets.h:97
This file contains some precompiler definitions which indicate the operating system and/or compiler u...
int int_t
Index type for indexing arrays, counting cubes, etc.
Definition: config.h:115
This namespace contains the entire CHomP library interface.
Definition: bitmaps.h:51