7 #ifndef PS_FILEBUFFER_HXX
19 PS_BitSet(
const bool _bias,
const long _max_index,
const long _capacity)
22 max_index(_max_index),
36 virtual bool Get(
const long _index);
37 virtual bool Set(
const long _index,
const bool _value);
39 virtual void setTrue(
const long _index);
40 virtual void setFalse(
const long _index);
45 void print(FILE *out,
const bool _header,
const long _fill_index);
50 virtual bool reserve(
const long _capacity);
52 explicit PS_BitSet(
const bool _bias,
const long _capacity) : bias(_bias), max_index(-1), capacity(0) {
59 load(_file, _fill_index);
73 PS_BitSet_Fast(
const PS_BitSet_Fast&);
79 bool Set(
const long _index,
const bool _value)
OVERRIDE;
88 load(_file, _fill_index);
105 unsigned char byte =
data[byte_index++];
106 for (
int i = 0; i<8 && index<=
max_index; ++i) {
107 if (!(byte&1)) _index_set.insert(index);
115 for (; (index <= _fill_index); ++index) {
116 _index_set.insert(index);
119 return _index_set.size();
129 unsigned char byte =
data[byte_index++];
130 for (
int i = 0; i<8 && index<=
max_index; ++i) {
131 if (byte&1) _index_set.insert(index);
139 for (; (index <= _fill_index); ++index) {
140 _index_set.insert(index);
143 return _index_set.size();
153 unsigned char byte =
data[byte_index++];
154 for (
int i = 0; i<8 && index<=
max_index; ++i) {
172 bool previous_value = (((
data[_index/8] >> (_index % 8)) & 1) == 1);
174 data[_index/8] |= 1 << (_index % 8);
177 data[_index/8] &= ~(1 << (_index % 8));
180 return previous_value;
187 data[_index/8] &= 1 << (_index % 8);
195 data[_index/8] &= ~(1 << (_index % 8));
203 return ((
data[_index/8] >> (_index % 8)) & 1) == 1;
217 unsigned char *new_data;
218 long new_capacity_bytes = (_capacity/8)+1;
219 long old_capacity_bytes = (
capacity/8)+1;
221 if (new_capacity_bytes <= old_capacity_bytes)
return true;
223 new_capacity_bytes = ((new_capacity_bytes / 32)+1)*32;
224 new_data = (
unsigned char *)malloc(new_capacity_bytes);
225 if (!new_data)
return false;
226 memset(new_data,
bias ? 0xFF : 0, new_capacity_bytes);
227 if (
capacity > 0) memcpy(new_data,
data, old_capacity_bytes);
236 for (
long i = 0; i <
capacity/8; ++i) {
243 for (
long i = 0; i <
capacity/8; ++i) {
250 if (_header) fprintf(out,
"PS_BitSet: bias(%1i) max_index(%6li) capacity(%6li) ",
bias,
max_index,
capacity);
254 for (
long i =
max_index+1; i <= _fill_index; ++i) {
274 _file->
put(&(
data[i]), (bytes-i));
294 _file->
get(&(
data[i]), (bytes-i));
299 bool PS_BitSet_Fast::Get(
const long _index) {
300 if (_index >= capacity) {
301 printf(
"PS_BitSet_Fast::get( %li ) exceeds capacity %li\n", _index, capacity);
305 if (_index > max_index) max_index = _index;
306 return ((data[_index/8] >> (_index % 8)) & 1) == 1;
310 bool PS_BitSet_Fast::Set(
const long _index,
const bool _value) {
311 if (_index >= capacity) {
312 printf(
"PS_BitSet_Fast::set( %li,%1i ) exceeds capacity %li\n", _index, _value, capacity);
316 bool previous_value = (((data[_index/8] >> (_index % 8)) & 1) == 1);
318 data[_index/8] |= 1 << (_index % 8);
321 data[_index/8] &= ~(1 << (_index % 8));
323 if (_index > max_index) max_index = _index;
324 return previous_value;
328 void PS_BitSet_Fast::setTrue(
const long _index) {
329 if (_index >= capacity) {
330 printf(
"PS_BitSet_Fast::setTrue( %li ) exceeds capacity %li\n", _index, capacity);
334 data[_index/8] |= 1 << (_index % 8);
335 if (_index > max_index) max_index = _index;
339 void PS_BitSet_Fast::setFalse(
const long _index) {
340 if (_index >= capacity) printf(
"PS_BitSet_Fast::setFalse( %li ) exceeds capacity %li\n", _index, capacity);
341 data[_index/8] &= ~(1 << (_index % 8));
342 if (_index > max_index) max_index = _index;
349 bool PS_BitSet_Fast::reserve(
const long _capacity) {
350 unsigned char *new_data;
351 long new_capacity_bytes = (_capacity/8)+1;
352 long old_capacity_bytes = (capacity/8)+1;
354 if (new_capacity_bytes <= old_capacity_bytes)
return true;
356 new_data = (
unsigned char *)malloc(new_capacity_bytes);
357 if (!new_data)
return false;
358 memset(new_data, bias ? 0xFF : 0, new_capacity_bytes);
359 if (capacity > 0) memcpy(new_data, data, old_capacity_bytes);
360 if (data) free(data);
362 capacity = new_capacity_bytes*8;
366 #error ps_bitset.hxx included twice
void get(void *_data, int _length)
long getTrueIndices(IndexSet &_index_set, const long _fill_index)
bool copy(const PS_BitSet *_other_bitset)
void x_or(const PS_BitSet *_other_set)
void put_char(unsigned char _c)
void get_char(unsigned char &_c)
long getMaxUsedIndex() const
void put_long(long int _l)
virtual bool reserve(const long _capacity)
void get_long(long int &_l)
void print(FILE *out, const bool _header, const long _fill_index)
PS_BitSet(const bool _bias, const long _max_index, const long _capacity)
void put(const void *_data, int _length)
bool load(PS_FileBuffer *_file, const long _fill_index)
PS_BitSet(const bool _bias, const long _capacity)
long getFalseIndices(IndexSet &_index_set, const long _fill_index)
static const int BUFFER_SIZE
PS_BitSet(PS_FileBuffer *_file, const long _fill_index=-1)
virtual bool Set(const long _index, const bool _value)
virtual bool Get(const long _index)
long getCountOfTrues(const long _fill_index=-1)
bool save(PS_FileBuffer *_file)
virtual void setTrue(const long _index)
PS_BitSet_Fast(bool _bias, long _capacity)
PS_BitSet_Fast(PS_FileBuffer *_file, const long _fill_index=-1)
virtual void setFalse(const long _index)