The Original CHomP Software
bitfield.h
Go to the documentation of this file.
1
3
21
22// Copyright (C) 1997-2020 by Pawel Pilarczyk.
23//
24// This file is part of my research software package. This is free software:
25// you can redistribute it and/or modify it under the terms of the GNU
26// General Public License as published by the Free Software Foundation,
27// either version 3 of the License, or (at your option) any later version.
28//
29// This software is distributed in the hope that it will be useful,
30// but WITHOUT ANY WARRANTY; without even the implied warranty of
31// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32// GNU General Public License for more details.
33//
34// You should have received a copy of the GNU General Public License
35// along with this software; see the file "license.txt". If not,
36// please, see <https://www.gnu.org/licenses/>.
37
38// Started in April 1999. Last revision: May 24, 2010.
39
40
41#ifndef _CHOMP_STRUCT_BITFIELD_H_
42#define _CHOMP_STRUCT_BITFIELD_H_
43
44#include "chomp/system/config.h"
45
46#include <iostream>
47#include <cstdlib>
48
49namespace chomp {
50namespace homology {
51
52
53// classes defined within this header file:
54class BitField;
55class SetOfBitFields;
56
59
62
63
64// --------------------------------------------------
65// ------------------- BitField ---------------------
66// --------------------------------------------------
67
73{
74public:
76 BitField ();
77
79 ~BitField ();
80
84 bool defined () const;
85
89 void define (unsigned char *buf, int_t length);
90
96 void allocate (int_t length);
97
101 void free ();
102
104 void set (int_t n);
105
107 void clear (int_t n);
108
110 int test (int_t n) const;
111
115 void takebits (const BitField &from, int_t length);
116
119 void clearall (int_t length);
120
125 int find (int_t first, int_t length) const;
126
130 int_t hashkey (int_t length) const;
131
135 int_t hashadd (int_t length) const;
136
139 friend bool thesame (const BitField &b1, const BitField &b2,
140 int_t length);
141
144 friend void int2bits (int bits, int_t length, BitField &field);
145
148 friend int bits2int (const BitField &field, int_t length);
149
150private:
155 unsigned char *table;
156
157}; /* BitField */
158
159// --------------------------------------------------
160
162{
163 table = NULL;
164 return;
165} /* BitField::BitField */
166
167inline bool BitField::defined () const
168{
169 return !!table;
170} /* BitField::defined */
171
172inline void BitField::free ()
173{
174 delete [] table;
175 table = NULL;
176 return;
177} /* BitField::free */
178
180{
181 return;
182} /* BitField::~BitField */
183
184inline void BitField::set (int_t n)
185{
186 table [n >> 3] |= static_cast<unsigned char> (0x01 << (n & 0x07));
187 return;
188} /* BitField::set */
189
190inline void BitField::clear (int_t n)
191{
192 table [n >> 3] &= static_cast<unsigned char> (~(0x01 << (n & 0x07)));
193 return;
194} /* BitField::clear */
195
196inline int BitField::test (int_t n) const
197{
198 return !!(table [n >> 3] & (0x01 << (n & 0x07)));
199} /* BitField::test */
200
201inline void int2bits (int bits, int_t length, BitField &field)
202{
203 unsigned char *tab = field. table;
204 if (!tab)
205 throw "Trying to set values to an undefined bitfield.";
206 while (length >= 0)
207 {
208 *(tab ++) = static_cast<unsigned char> (bits & 0xFF);
209 bits >>= 8;
210 length -= 8;
211 }
212 return;
213} /* int2bits */
214
215inline int bits2int (const BitField &field, int_t length)
216{
217 const unsigned char *tab = field. table;
218 if (!tab)
219 throw "Trying to set values to an undefined bitfield.";
220 int n = 0;
221 int shiftvalue = 0;
222 while (length >= 8)
223 {
224 n |= (*(tab ++)) << shiftvalue;
225 length -= 8;
226 shiftvalue += 8;
227 }
228 const int bitmasks [] = {0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF};
229 if (length > 0)
230 n |= ((*(tab ++)) & bitmasks [length]) << shiftvalue;
231 return n;
232} /* bits2int */
233
234
235// --------------------------------------------------
236// ----------------- SetOfBitFields -----------------
237// --------------------------------------------------
238
245{
246public:
249 SetOfBitFields (int_t len, int_t maxelem);
250
253
257 int add (const BitField &b, int value);
258
261 int check (const BitField &b) const;
262
264 int used (void) const;
265
267 void forget ();
268
269private:
271 int avail;
272
275
278
281
284
287
289 unsigned char *buf;
290
292 unsigned char *bufcur;
293
294}; /* class SetOfBitFields */
295
296// --------------------------------------------------
297
299{
300 if (bf)
301 delete [] bf;
302 if (buf)
303 delete [] buf;
304 if (length)
305 values. free ();
306 return;
307} /* SetOfBitFields::~SetOfBitFields */
308
309inline int SetOfBitFields::used () const
310{
311 return usedcount;
312} /* SetOfBitFields::used */
313
315{
316 if (bf)
317 delete [] bf;
318 bf = NULL;
319 if (buf)
320 delete [] buf;
321 buf = NULL;
322 if (length)
323 values. free ();
324 length = 0;
325 size = 0;
326 avail = 0;
327 return;
328} /* SetOfBitFields::forget */
329
330
331} // namespace homology
332} // namespace chomp
333
334#endif // _CHOMP_STRUCT_BITFIELD_H_
335
337
This class defines a bit field that is part of some larger array or that uses an allocated piece of m...
Definition: bitfield.h:73
friend bool thesame(const BitField &b1, const BitField &b2, int_t length)
Compares two bit fields of the giben length.
friend int bits2int(const BitField &field, int_t length)
Converts the bits of a bit field of the given length into an integer.
Definition: bitfield.h:215
bool defined() const
Returns true if the bit field has already been defined, that is, it is bound with some memory piece,...
Definition: bitfield.h:167
int_t hashadd(int_t length) const
Returns the second key for hashing.
BitField()
The constructor of an undefined bit field.
Definition: bitfield.h:161
unsigned char * table
The table of 8-bit cells which store the subsequent bits.
Definition: bitfield.h:155
void allocate(int_t length)
Allocates memory for a bit field.
void clearall(int_t length)
Clears all the bits in the entire bit field of specified length.
int test(int_t n) const
Tests the given bit. Returns 0 or 1.
Definition: bitfield.h:196
int find(int_t first, int_t length) const
Finds the first bit that is set to 1, beginning at the given one.
void set(int_t n)
Sets the given bit to 1.
Definition: bitfield.h:184
void takebits(const BitField &from, int_t length)
Copies all the bits from the given bitfield.
void clear(int_t n)
Clears the given bit (sets it to 0).
Definition: bitfield.h:190
void free()
Releases the memory allocated for the bit field.
Definition: bitfield.h:172
friend void int2bits(int bits, int_t length, BitField &field)
Converts an integer into the bits of a bit field of the given length.
Definition: bitfield.h:201
int_t hashkey(int_t length) const
Returns the first key for hashing.
~BitField()
The destructor which actually does nothing.
Definition: bitfield.h:179
void define(unsigned char *buf, int_t length)
Define the bit field as a piece of a larger memory buffer.
This class defines a set of bit fields of the same length which are to be stored in a contiguous piec...
Definition: bitfield.h:245
void forget()
Forgets all the bit fields and deallocates the memory.
Definition: bitfield.h:314
int used(void) const
Returns the number of bit fields contained in the set.
Definition: bitfield.h:309
int check(const BitField &b) const
Returns the value of the given bit field value or return -1 if the bit field is not in the set.
int add(const BitField &b, int value)
Adds a bit field to the set.
int_t length
The length of bit fields.
Definition: bitfield.h:280
~SetOfBitFields()
The destructor.
Definition: bitfield.h:298
SetOfBitFields(int_t len, int_t maxelem)
The constructor of a set of a fixed maximal number of bit fields, each of the same length.
unsigned char * bufcur
The current position in the buffer.
Definition: bitfield.h:292
BitField * bf
The table of bit fields.
Definition: bitfield.h:283
int avail
The number of bit fields that still can be stored.
Definition: bitfield.h:271
int_t size
The actual size of the table.
Definition: bitfield.h:277
unsigned char * buf
The memory buffer used for bit fields.
Definition: bitfield.h:289
int usedcount
The number of bit fields used.
Definition: bitfield.h:274
BitField values
The values of bit fields.
Definition: bitfield.h:286
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
int bits2int(const BitField &field, int_t length)
Definition: bitfield.h:215
SetOfBitFields bitfieldset
A lower-case version of the name of a bit field set [deprecated].
Definition: bitfield.h:61
BitField bitfield
A lower-case version of the name of a bit field [deprecated].
Definition: bitfield.h:58
void int2bits(int bits, int_t length, BitField &field)
Definition: bitfield.h:201
This namespace contains the entire CHomP library interface.
Definition: bitmaps.h:51