20 virtual bool do_reserve(
const long _capacity,
const bool _init_sets);
22 PS_BitMap(
const bool _bias,
const long _max_row,
const long _capacity)
35 virtual bool get(
const long _row,
const long _col);
36 virtual bool set(
const long _row,
const long _col,
const bool _value);
38 virtual void setTrue(
const long _row,
const long _col) = 0;
39 virtual void setFalse(
const long _row,
const long _col) = 0;
44 bool triangle_set(
const long _row,
const long _col,
const bool _value);
49 virtual void print(FILE *out);
55 virtual bool reserve(
const long _capacity);
57 explicit PS_BitMap(
const bool _bias,
const long _capacity) : bias(_bias), max_row(0), capacity(_capacity) {
59 for (
long i = 0; i <
capacity; ++i) {
62 printf(
"PS_BitMap( %s,%li ) : error while init. data[%li]\n", bias ?
"true" :
"false", capacity, i);
74 for (
long i = 0; i <
capacity; ++i) {
75 if (data[i])
delete data[i];
95 PS_BitMap_Fast(
const PS_BitMap_Fast&);
100 bool get(
const long _row,
const long _col) OVERRIDE;
101 bool set(
const long _row,
const long _col,
const bool _value)
OVERRIDE;
107 bool copy(
const PS_BitMap_Fast *_other_bitmap);
112 for (
long i = 0; i <
capacity; ++i) {
113 data[i] =
new PS_BitSet_Fast(bias, capacity);
115 printf(
"PS_BitMap_Fast( %s,%li ) : error while init. data[%li]\n", bias ?
"true" :
"false", capacity, i);
142 long *count_true_per_index;
147 PS_BitMap_Counted(
const PS_BitMap_Counted&);
152 void recalcCounters();
153 long getCountFor(
const long _index) {
return count_true_per_index[_index]; }
161 bool get(const
long _row, const
long _col) OVERRIDE;
162 bool set(const
long _row, const
long _col, const
bool _value) OVERRIDE;
163 void setTrue(const
long _row, const
long _col) OVERRIDE;
164 void setFalse(const
long _row, const
long _col) OVERRIDE;
166 void print(FILE *out) OVERRIDE;
167 void printGNUplot(const
char *_title,
char *_buffer,
PS_FileBuffer *_file) OVERRIDE;
170 bool copy(const PS_BitMap_Counted *_other_bitmap);
171 bool reserve(const
long _capacity) OVERRIDE;
175 count_true_per_index =
NULp;
181 for (
long i = 0; i <
capacity; ++i) {
182 data[i] =
new PS_BitSet_Fast(bias, capacity);
184 printf(
"PS_BitMap_Counted( %s,%li ) : error while init. data[%li]\n", bias ?
"true" :
"false", capacity, i);
189 count_true_per_index = (
long *)malloc(capacity *
sizeof(
long));
192 memset(count_true_per_index, (bias) ? capacity : 0, capacity *
sizeof(
long));
196 if (count_true_per_index) free(count_true_per_index);
205 for (
long row = _index+1; row <=
max_row; ++row) {
206 if (
data[row]->Get(_index)) _index_set.insert(row);
208 return _index_set.size();
216 for (
long row = _index+1; row <=
max_row; ++row) {
217 if (!(
data[row]->Get(_index))) _index_set.insert(row);
219 return _index_set.size();
226 for (
long row = 0; row <=
max_row; ++row) {
236 return data[_row]->
Set(_col, _value);
248 return set(_col, _row, _value);
251 return set(_row, _col, _value);
258 return get(_col, _row);
261 return get(_row, _col);
269 for (
long i = 0; i <
capacity; ++i) {
271 if (!
data[i]->
copy(_other_bitmap->
data[i]))
return false;
283 long new_capacity_bytes = _capacity *
sizeof(
PS_BitSet*);
285 if (_capacity <=
capacity)
return true;
286 new_data = (
PS_BitSet **)malloc(new_capacity_bytes);
287 if (!new_data)
return false;
288 if (
capacity > 0) memcpy(new_data,
data, old_capacity_bytes);
292 for (
long i =
capacity; i < _capacity; ++i) {
294 if (!
data[i])
return false;
303 for (
long i = 0; i <=
max_row; ++i) {
310 for (
long i = 0; i <=
max_row; ++i) {
317 fprintf(out,
"PS_BitMap : bias(%1i) max_row(%6li) capacity(%6li)\n",
bias,
max_row,
capacity);
318 for (
long i = 0; i <
capacity; ++i) {
319 fprintf(out,
"[%5li] ", i);
328 size = sprintf(_buffer,
"# %s\n#PS_BitMap : bias(%1i) max_row(%li) capacity(%li)\n#col row\n", _title,
bias,
max_row,
capacity);
329 _file->
put(_buffer, size);
337 for (PS_BitSet::IndexSet::iterator col = trues.begin();
340 size = sprintf(_buffer,
"%li %li\n", *col, row);
341 _file->
put(_buffer, size);
356 for (
long i = 0; i <=
max_row; ++i) {
371 for (
long i = 0; i <=
max_row; ++i) {
381 inline bool PS_BitMap_Fast::set(
const long _row,
const long _col,
const bool _value) {
382 if (_row > max_row) max_row = _row;
383 return data[_row]->Set(_col, _value);
387 inline bool PS_BitMap_Fast::get(
const long _row,
const long _col) {
388 if (_row > max_row) max_row = _row;
389 return data[_row]->Get(_col);
393 inline void PS_BitMap_Fast::setTrue(
const long _row,
const long _col) {
394 if (_row > max_row) max_row = _row;
395 data[_row]->setTrue(_col);
399 inline void PS_BitMap_Fast::setFalse(
const long _row,
const long _col) {
400 if (_row > max_row) max_row = _row;
401 data[_row]->setFalse(_col);
412 if (!do_reserve(max_row+1,
false))
return false;
413 for (
long i = 0; i <= max_row; ++i) {
417 data[i] =
new PS_BitSet_Fast(_file);
423 bias = _other_bitmap->bias;
424 if (!do_reserve(_other_bitmap->capacity,
false))
return false;
425 for (
long i = 0; i < capacity; ++i) {
426 if (!data[i]) data[i] =
new PS_BitSet_Fast(bias, _other_bitmap->data[i]->getCapacity());
427 if (!data[i]->
copy(_other_bitmap->data[i]))
return false;
429 max_row = _other_bitmap->max_row;
433 bool PS_BitMap_Fast::reserve(
const long _capacity) {
434 return do_reserve(_capacity,
true);
436 bool PS_BitMap_Fast::do_reserve(
const long _capacity,
const bool _init_sets) {
437 if (_capacity <= capacity)
return true;
439 long new_capacity_bytes = _capacity *
sizeof(
PS_BitSet*);
440 long old_capacity_bytes = capacity *
sizeof(
PS_BitSet*);
441 new_data = (
PS_BitSet **)malloc(new_capacity_bytes);
442 if (!new_data)
return false;
443 if (capacity > 0) memcpy(new_data, data, old_capacity_bytes);
444 if (data) free(data);
447 for (
long i = capacity; i < _capacity; ++i) {
448 data[i] =
new PS_BitSet_Fast(bias, 1);
449 if (!data[i])
return false;
452 capacity = _capacity;
457 long PS_BitMap_Counted::getTrueIndicesFor(
const long _index,
PS_BitSet::IndexSet &_index_set) {
459 unsigned long total_count_trues = count_true_per_index[_index];
461 data[_index]->getTrueIndices(_index_set, _index);
463 for (
long row = _index+1;
464 ((row <= max_row) && (_index_set.size() < total_count_trues));
466 if (data[row]->Get(_index)) _index_set.insert(row);
468 return _index_set.size();
472 long PS_BitMap_Counted::getFalseIndicesFor(
const long _index,
PS_BitSet::IndexSet &_index_set) {
474 unsigned long total_count_falses = capacity - count_true_per_index[_index];
476 data[_index]->getFalseIndices(_index_set, _index);
478 for (
long row = _index+1;
479 ((row <= max_row) && (_index_set.size() < total_count_falses));
481 if (!(data[row]->Get(_index))) _index_set.insert(row);
483 return _index_set.size();
487 long PS_BitMap_Counted::getTrueIndicesForRow(
const long _row,
PS_BitSet::IndexSet &_index_set) {
489 data[_row]->getTrueIndices(_index_set, _row);
490 return _index_set.size();
494 long PS_BitMap_Counted::getFalseIndicesForRow(
const long _row,
PS_BitSet::IndexSet &_index_set) {
496 data[_row]->getFalseIndices(_index_set, _row);
497 return _index_set.size();
501 long PS_BitMap_Counted::getCountOfTrues() {
504 for (
long row = 0; row <= max_row; ++row) {
505 count += data[row]->getCountOfTrues(row);
511 bool PS_BitMap_Counted::set(
const long _row,
const long _col,
const bool _value) {
512 if (_col > _row) printf(
"PS_BitMap_Counted::set( %li,%li,%1i ) not allowed\n", _row, _col, _value);
513 if (_row > max_row) max_row = _row;
514 bool previous_value = data[_row]->Set(_col, _value);
515 if (_value && !previous_value) {
516 ++count_true_per_index[_row];
517 ++count_true_per_index[_col];
519 else if (!_value && previous_value) {
520 --count_true_per_index[_row];
521 --count_true_per_index[_col];
523 return previous_value;
527 inline bool PS_BitMap_Counted::get(
const long _row,
const long _col) {
528 if (_col > _row) printf(
"PS_BitMap_Counted::get( %li,%li ) not allowed\n", _row, _col);
529 if (_row > max_row) max_row = _row;
530 return data[_row]->Get(_col);
534 inline void PS_BitMap_Counted::setTrue(
const long _row,
const long _col) {
535 if (_col > _row) printf(
"PS_BitMap_Counted::setTrue( %li,%li ) not allowed\n", _row, _col);
536 if (_row > max_row) max_row = _row;
537 data[_row]->setTrue(_col);
541 inline void PS_BitMap_Counted::setFalse(
const long _row,
const long _col) {
542 if (_col > _row) printf(
"PS_BitMap_Counted::setFalse( %li,%li ) not allowed\n", _row, _col);
543 if (_row > max_row) max_row = _row;
544 data[_row]->setFalse(_col);
555 if (!do_reserve(max_row+1,
false))
return false;
556 for (
long i = 0; i <= max_row; ++i) {
560 data[i] =
new PS_BitSet_Fast(_file, i);
568 bias = _other_bitmap->bias;
569 if (!do_reserve(_other_bitmap->capacity,
false))
return false;
570 memcpy(count_true_per_index, _other_bitmap->count_true_per_index, (capacity *
sizeof(
long)));
571 for (
long i = 0; i < capacity; ++i) {
572 if (!data[i]) data[i] =
new PS_BitSet_Fast(bias, _other_bitmap->data[i]->getCapacity());
573 if (!data[i]->
copy(_other_bitmap->data[i]))
return false;
575 max_row = _other_bitmap->max_row;
580 bool PS_BitMap_Counted::reserve(
const long _capacity) {
581 return do_reserve(_capacity,
true);
583 bool PS_BitMap_Counted::do_reserve(
const long _capacity,
const bool _init_sets) {
586 if (_capacity <= capacity)
return true;
587 long new_data_bytes = _capacity *
sizeof(
PS_BitSet*);
588 long old_data_bytes = capacity *
sizeof(
PS_BitSet*);
589 long new_counts_bytes = _capacity *
sizeof(
long);
590 long old_counts_bytes = capacity *
sizeof(
long);
594 new_data = (
PS_BitSet **)malloc(new_data_bytes);
595 new_counts = (
long *)malloc(new_counts_bytes);
599 if (!new_data || !new_counts) {
601 if (new_data) free(new_data);
602 if (new_counts) free(new_counts);
608 if (capacity > 0) memcpy(new_data, data, old_data_bytes);
609 if (data) free(data);
612 for (
long i = capacity; i < _capacity; ++i) {
613 data[i] =
new PS_BitSet_Fast(bias, i+1);
614 if (!data[i])
return false;
618 for (
long i = capacity; i < _capacity; ++i) {
625 if (capacity > 0) memcpy(new_counts, count_true_per_index, old_counts_bytes);
626 memset(new_counts+old_counts_bytes, 0, new_counts_bytes-old_counts_bytes);
627 if (count_true_per_index) free(count_true_per_index);
628 count_true_per_index = new_counts;
630 capacity = _capacity;
636 fprintf(out,
"PS_BitMap_Counted : bias(%1i) max_row(%6li) capacity(%6li)\n", bias, max_row, capacity);
637 for (
long i = 0; i < capacity; ++i) {
638 fprintf(out,
"[%5li] %6li ", i, count_true_per_index[i]);
639 data[i]->print(out,
false);
644 void PS_BitMap_Counted::printGNUplot(
const char *_title,
char *_buffer,
PS_FileBuffer *_file) {
647 size = sprintf(_buffer,
"# %s\n#PS_BitMap_Counted : bias(%1i) max_row(%li) capacity(%li)\n#col row - index of a true\n", _title, bias, max_row, capacity);
648 _file->
put(_buffer, size);
655 data[row]->getTrueIndices(trues);
656 for (PS_BitSet::IndexSet::iterator col = trues.begin();
659 size = sprintf(_buffer,
"%li %li\n", *col, row);
660 _file->
put(_buffer, size);
665 size = sprintf(_buffer,
"\n\n# speciesID count (of trues)\n");
666 _file->
put(_buffer, size);
669 map<long, long> species_per_count;
673 size = sprintf(_buffer,
"%li %li\n", row, count_true_per_index[row]);
674 _file->
put(_buffer, size);
675 ++species_per_count[count_true_per_index[row]];
679 size = sprintf(_buffer,
"\n\n# count (of trues) count (of species)\n");
680 _file->
put(_buffer, size);
681 for (map<long, long>::iterator count = species_per_count.begin();
682 count != species_per_count.end();
684 size = sprintf(_buffer,
"%li %li\n", count->first, count->second);
685 _file->
put(_buffer, size);
692 void PS_BitMap_Counted::recalcCounters() {
693 printf(
"PS_BitMap_Counted::recalcCounters()\n");
694 memset(count_true_per_index, 0, capacity *
sizeof(
long));
695 for (
long row = 0; row <= max_row; ++row) {
699 if (row_data->
Get(col)) {
700 ++count_true_per_index[col];
701 if (row != col) ++count_true_per_index[row];
708 #error ps_bitmap.hxx included twice
virtual long getCountOfTrues()
long getTrueIndices(IndexSet &_index_set, const long _fill_index)
void x_or(const PS_BitSet *_other_set)
void put_char(unsigned char _c)
virtual void setTrue(const long _row, const long _col)=0
virtual void printGNUplot(const char *_title, char *_buffer, PS_FileBuffer *_file)
virtual void setFalse(const long _row, const long _col)=0
void get_char(unsigned char &_c)
long getMaxUsedIndex() const
void put_long(long int _l)
bool copy(const PS_BitMap *_other_bitmap)
void x_or(const PS_BitMap *_other_map)
void get_long(long int &_l)
void print(FILE *out, const bool _header, const long _fill_index)
long getCountFor(const long _index)
bool triangle_get(const long _row, const long _col)
virtual void print(FILE *out)
virtual bool set(const long _row, const long _col, const bool _value)
void put(const void *_data, int _length)
virtual long getFalseIndicesFor(const long _index, PS_BitSet::IndexSet &_index_set)
long getFalseIndices(IndexSet &_index_set, const long _fill_index)
virtual bool load(PS_FileBuffer *_file)
virtual bool reserve(const long _capacity)
static void copy(double **i, double **j)
virtual bool get(const long _row, const long _col)
PS_BitMap(const bool _bias, const long _capacity)
virtual bool Set(const long _index, const bool _value)
virtual bool Get(const long _index)
virtual long getTrueIndicesFor(const long _index, PS_BitSet::IndexSet &_index_set)
PS_BitMap(PS_FileBuffer *_file)
~PS_BitMap_Counted() OVERRIDE
PS_BitMap_Fast(const bool _bias, const long _capacity)
long getCountOfTrues(const long _fill_index=-1)
PS_BitMap_Counted(const bool _bias, const long _capacity)
bool save(PS_FileBuffer *_file)
bool save(PS_FileBuffer *_file)
PS_BitMap(const bool _bias, const long _max_row, const long _capacity)
bool triangle_set(const long _row, const long _col, const bool _value)
virtual bool do_reserve(const long _capacity, const bool _init_sets)