source: tags/svn.1.5.4/PROBE_SET/ps_bitmap.hxx

Last change on this file was 7623, checked in by westram, 13 years ago
  • merge from dev [7450] [7452] [7456] [7457] [7458] [7459] [7460] [7461] [7464] [7465] [7466] [7467] [7468] [7469] [7482]
    • tweaked compiler options
      • activated -Weffc++
        • postfilter warnings where Scott Meyers' advices are too general.
          • base classes should not always have virtual destructors, since that renders tiny classes useless and
          • members should not always be initialized via initialization list, since that often violates the DRY principle
        • fix gcc's inability to detect that Noncopyable implements a private copy-ctor and op=
        • this slows down complete ARB recompilation by ~5%
    • added -Wold-style-cast (inactive)
    • removed -Wno-non-template-friend added in [7447]
  • postcompile.pl
    • added option —original to show unmodified compiler output
  • declared op= for classes which had a copy-ctor
  • moved op= macros to arbtools.h
  • derived classes containing pointers from Noncopyable (use Noncopyable virtually) or
  • made them copyable if needed (awt_mask_item, KnownDB, Code, AWT_registered_itemtype, GEN_gene, PosGene, PartialSequence, PlugIn, Range, Convaln_exception)
  • other related changes
    • user mask destruction working now
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.4 KB
Line 
1#ifndef PS_BITMAP_HXX
2#define PS_BITMAP_HXX
3
4#ifndef __MAP__
5#include <map>
6#endif
7#ifndef PS_BITSET_HXX
8#include "ps_bitset.hxx"
9#endif
10
11class PS_BitMap : virtual Noncopyable {
12protected:
13
14    PS_BitSet **data;                   // array of pointers to PS_BitSet
15    bool bias;                          // preset for bitmap
16    long max_row;                       // max used row index
17    long capacity;                      // number of allocated rows (and columns)
18
19    virtual bool do_reserve(const long _capacity, const bool _init_sets);
20
21    PS_BitMap(const PS_BitMap&);
22    PS_BitMap();
23    PS_BitMap(const bool _bias, const long _max_row, const long _capacity) {
24        bias     = _bias;
25        max_row  = _max_row;
26        capacity = _capacity;
27    }
28
29public:
30
31    virtual long getTrueIndicesFor(const long _index, PS_BitSet::IndexSet &_index_set);
32    virtual long getFalseIndicesFor(const long _index, PS_BitSet::IndexSet &_index_set);
33    virtual long getCountOfTrues();
34
35    virtual bool get(const long _row, const long _col);
36    virtual bool set(const long _row, const long _col, const bool _value);
37    // triangle_ - functions reverse _row and _col if _row is greater than _col
38    // the resulting map is only triangular
39    bool triangle_get(const long _row, const long _col);
40    bool triangle_set(const long _row, const long _col, const bool _value);
41
42    virtual void invert();
43    virtual void x_or(const PS_BitMap *_other_map);
44
45    virtual void print();
46    virtual void printGNUplot(const char *_title, char *_buffer, PS_FileBuffer *_file);
47    virtual bool save(PS_FileBuffer *_file);
48    virtual bool load(PS_FileBuffer *_file);
49
50            bool copy(const PS_BitMap *_other_bitmap);
51    virtual bool reserve(const long _capacity);
52
53    explicit PS_BitMap(const bool _bias, const long _capacity) : bias(_bias), max_row(0), capacity(_capacity) {
54        data = (PS_BitSet **)malloc(capacity * sizeof(PS_BitSet*));
55        for (long i = 0; i < capacity; ++i) {                        // init requested bitsets
56            data[i] = new PS_BitSet(bias, capacity);                 // init field
57            if (data[i] == 0) {
58                printf("PS_BitMap( %s,%li ) : error while init. data[%li]\n", bias ? "true" : "false", capacity, i);
59                exit(1);
60            }
61        }
62    }
63
64    explicit PS_BitMap(PS_FileBuffer *_file) : bias(false), max_row(0), capacity(0) {
65        data = 0;
66        load(_file);
67    }
68
69    virtual ~PS_BitMap() {
70        for (long i = 0; i < capacity; ++i) {
71            if (data[i]) delete data[i];
72        }
73        if (data) free(data);
74    }
75};
76
77
78//  ############################################################
79//  # PS_BitMap_Fast
80//  ############################################################
81//   No checks are made to assure that given row/col indices
82//   point to an allocated byte. Make sure you reserve the needed
83//   space either at creation time or before accessing the bits.
84//
85class PS_BitMap_Fast : public PS_BitMap {
86private:
87
88            bool copy(const PS_BitSet *_other_bitset);   // declared but not implemented
89    virtual bool do_reserve(const long _capacity, const bool _init_sets);
90
91    PS_BitMap_Fast(const PS_BitMap_Fast&);
92    PS_BitMap_Fast();
93
94public:
95
96    virtual bool get(const long _row, const long _col);
97    virtual bool set(const long _row, const long _col, const bool _value);
98    virtual void setTrue(const long _row, const long _col);
99    virtual void setFalse(const long _row, const long _col);
100
101    virtual bool load(PS_FileBuffer *_file);
102
103            bool copy(const PS_BitMap_Fast *_other_bitmap);
104    virtual bool reserve(const long _capacity);
105
106    explicit PS_BitMap_Fast(const bool _bias, const long _capacity) : PS_BitMap(_bias, 0, _capacity) {
107        data = (PS_BitSet **)malloc(capacity * sizeof(PS_BitSet*));
108        for (long i = 0; i < capacity; ++i) {                        // init requested bitsets
109            data[i] = new PS_BitSet_Fast(bias, capacity);            // init field
110            if (data[i] == 0) {
111                printf("PS_BitMap_Fast( %s,%li ) : error while init. data[%li]\n", bias ? "true" : "false", capacity, i);
112                exit(1);
113            }
114        }
115    }
116};
117
118
119//  ############################################################
120//  # PS_BitMap_Counted
121//  ############################################################
122//   PS_BitMap_Counted is ALWAYS triangular.
123//   This means that the max column index in a row is the row number.
124//   The resulting bitmap is the lower left half
125//     012
126//   0 .
127//   1 ..
128//   2 ...
129//   Therefore the row index must be higher or equal than the column index
130//   when you call set/get-functions, which do not check given row,col.
131//   If you cannot assure this use the triangleSet/Get-functions.
132//
133//   count_true_per_index counts TRUEs in a row + the TRUEs in the
134//   column of the same index.
135//   Only set() automatically updates count_true_per_index.
136//
137class PS_BitMap_Counted : public PS_BitMap { // derived from a Noncopyable
138    long *count_true_per_index;
139
140            bool copy(const PS_BitSet *_other_bitset);   // declared but not implemented because PS_BitSet has no count_true_per_index array
141    virtual bool do_reserve(const long _capacity, const bool _init_sets);
142
143    PS_BitMap_Counted(const PS_BitMap_Counted&);
144    PS_BitMap_Counted();
145
146public:
147
148    void recalcCounters();
149    long getCountFor(const long _index) { return count_true_per_index[_index]; }
150
151    virtual long getTrueIndicesFor(const long _index, PS_BitSet::IndexSet &_index_set);
152    virtual long getFalseIndicesFor(const long _index, PS_BitSet::IndexSet &_index_set);
153            long getTrueIndicesForRow(const long _row, PS_BitSet::IndexSet &_index_set);
154            long getFalseIndicesForRow(const long _row, PS_BitSet::IndexSet &_index_set);
155    virtual long getCountOfTrues();
156
157    virtual bool get(const long _row, const long _col);
158    virtual bool set(const long _row, const long _col, const bool _value);
159    virtual void setTrue(const long _row, const long _col);
160    virtual void setFalse(const long _row, const long _col);
161
162    virtual void print();
163    virtual void printGNUplot(const char *_title, char *_buffer, PS_FileBuffer *_file);
164    virtual bool load(PS_FileBuffer *_file);
165
166    virtual bool copy(const PS_BitMap_Counted *_other_bitmap);
167    virtual bool reserve(const long _capacity);
168
169    explicit PS_BitMap_Counted(PS_FileBuffer *_file) : PS_BitMap(false, 0, 0) {
170        data                 = 0;
171        count_true_per_index = 0;
172        load(_file);
173    }
174
175    explicit PS_BitMap_Counted(const bool _bias, const long _capacity) : PS_BitMap(_bias, 0, _capacity) {
176        data = (PS_BitSet **)malloc(capacity * sizeof(PS_BitSet*));
177        for (long i = 0; i < capacity; ++i) {                        // init requested bitsets
178            data[i] = new PS_BitSet_Fast(bias, capacity);            // init field
179            if (data[i] == 0) {
180                printf("PS_BitMap_Counted( %s,%li ) : error while init. data[%li]\n", bias ? "true" : "false", capacity, i);
181                exit(1);
182            }
183        }
184        // alloc memory for counters
185        count_true_per_index = (long *)malloc(capacity * sizeof(long));
186
187        // preset memory of counters
188        memset(count_true_per_index, (bias) ? capacity : 0, capacity * sizeof(long));
189    }
190
191    virtual ~PS_BitMap_Counted() {
192        if (count_true_per_index) free(count_true_per_index);
193    }
194};
195
196
197long PS_BitMap::getTrueIndicesFor(const long _index, PS_BitSet::IndexSet &_index_set) {
198    // get trues in the row
199    data[_index]->getTrueIndices(_index_set, _index);
200    // scan column _index in the remaining rows
201    for (long row = _index+1; row <= max_row; ++row) {
202        if (data[row]->Get(_index)) _index_set.insert(row);
203    }
204    return _index_set.size();
205}
206
207
208long PS_BitMap::getFalseIndicesFor(const long _index, PS_BitSet::IndexSet &_index_set) {
209    // get falses in the row
210    data[_index]->getFalseIndices(_index_set, _index);
211    // scan column _index in the remaining rows
212    for (long row = _index+1; row <= max_row; ++row) {
213        if (!(data[row]->Get(_index))) _index_set.insert(row);
214    }
215    return _index_set.size();
216}
217
218
219long PS_BitMap::getCountOfTrues() {
220    long count = 0;
221    // sum up trues in the rows
222    for (long row = 0; row <= max_row; ++row) {
223        count += data[row]->getCountOfTrues();
224    }
225    return count;
226}
227
228
229bool PS_BitMap::set(const long _row, const long _col, const bool _value) {
230    reserve(_row+1);
231    if (_row > max_row) max_row = _row;
232    return data[_row]->Set(_col, _value);
233}
234
235
236bool PS_BitMap::get(const long _row, const long _col) {
237    reserve(_row+1);
238    return data[_row]->Get(_col);
239}
240
241
242bool PS_BitMap::triangle_set(const long _row, const long _col, const bool _value) {
243    if (_row > _col) {
244        return set(_col, _row, _value);
245    }
246    else {
247        return set(_row, _col, _value);
248    }
249}
250
251
252bool PS_BitMap::triangle_get(const long _row, const long _col) {
253    if (_row > _col) {
254        return get(_col, _row);
255    }
256    else {
257        return get(_row, _col);
258    }
259}
260
261
262bool PS_BitMap::copy(const PS_BitMap *_other_bitmap) {
263    bias = _other_bitmap->bias;
264    if (!do_reserve(_other_bitmap->capacity, false)) return false;
265    for (long i = 0; i < capacity; ++i) {
266        if (!data[i]) data[i] = new PS_BitSet(bias, _other_bitmap->data[i]->capacity);
267        if (!data[i]->copy(_other_bitmap->data[i])) return false;
268    }
269    max_row = _other_bitmap->max_row;
270    return true;
271}
272
273
274bool PS_BitMap::reserve(const long _capacity) {
275    return do_reserve(_capacity, true);
276}
277bool PS_BitMap::do_reserve(const long _capacity, const bool _init_sets) {
278    PS_BitSet **new_data;
279    long new_capacity_bytes = _capacity * sizeof(PS_BitSet*);
280    long old_capacity_bytes =  capacity * sizeof(PS_BitSet*);
281    if (_capacity <= capacity) return true;                      // smaller or same size requested ?
282    new_data = (PS_BitSet **)malloc(new_capacity_bytes);         // get new memory for pointer array
283    if (new_data == 0) return false;
284    if (capacity > 0) memcpy(new_data, data, old_capacity_bytes); // copy old pointers
285    if (data) free(data);                                        // free old memory
286    data = new_data;
287    if (_init_sets) {
288        for (long i = capacity; i < _capacity; ++i) {            // init new requested bitsets
289            data[i] = new PS_BitSet(bias, 1);                    // init field
290            if (data[i] == 0) return false;                      // check success
291        }
292    }
293    capacity = _capacity;                                        // store new capacity
294    return true;
295}
296
297
298void PS_BitMap::invert() {
299    for (long i = 0; i <= max_row; ++i) {
300        data[i]->invert();
301    }
302}
303
304
305void PS_BitMap::x_or(const PS_BitMap *_other_map) {
306    for (long i = 0; i <= max_row; ++i) {
307        data[i]->x_or(_other_map->data[i]);
308    }
309}
310
311
312void PS_BitMap::print() {
313    printf("PS_BitMap : bias(%1i) max_row(%6li) capacity(%6li)\n", bias, max_row, capacity);
314    for (long i = 0; i < capacity; ++i) {
315        printf("[%5li] ", i);
316        data[i]->print(true, i);
317    }
318}
319
320
321void PS_BitMap::printGNUplot(const char *_title, char *_buffer, PS_FileBuffer *_file) {
322    // write title
323    long size;
324    size = sprintf(_buffer, "# %s\n#PS_BitMap : bias(%1i) max_row(%li) capacity(%li)\n#col row\n", _title, bias, max_row, capacity);
325    _file->put(_buffer, size);
326
327    // write indices of trues per row
328    PS_BitSet::IndexSet trues;
329    for (long row = 0;
330          row < capacity;
331          ++row) {
332        data[row]->getTrueIndices(trues);
333        for (PS_BitSet::IndexSet::iterator col = trues.begin();
334              col != trues.end();
335              ++col) {
336            size = sprintf(_buffer, "%li %li\n", *col, row);
337            _file->put(_buffer, size);
338        }
339    }
340
341    _file->flush();
342}
343
344
345bool PS_BitMap::save(PS_FileBuffer *_file) {
346    if (_file->isReadonly()) return false;
347    // save max_row
348    _file->put_long(max_row);
349    // save bias
350    _file->put_char((bias) ? '1' : '0');
351    // save bitsets
352    for (long i = 0; i <= max_row; ++i) {
353        data[i]->save(_file);
354    }
355    return true;
356}
357
358
359bool PS_BitMap::load(PS_FileBuffer *_file) {
360    if (!_file->isReadonly()) return false;
361    // load max_row
362    _file->get_long(max_row);
363    // load bias
364    bias = (_file->get_char() == '1');
365    // initialize bitmap
366    if (!do_reserve(max_row+1, false)) return false;
367    for (long i = 0; i <= max_row; ++i) {
368        if (data[i]) {
369            delete data[i];
370        }
371        data[i] = new PS_BitSet(_file);
372    }
373    return true;
374}
375
376
377inline bool PS_BitMap_Fast::set(const long _row, const long _col, const bool _value) {
378    if (_row > max_row) max_row = _row;
379    return data[_row]->Set(_col, _value);
380}
381
382
383inline bool PS_BitMap_Fast::get(const long _row, const long _col) {
384    if (_row > max_row) max_row = _row;
385    return data[_row]->Get(_col);
386}
387
388
389inline void PS_BitMap_Fast::setTrue(const long _row, const long _col) {
390    if (_row > max_row) max_row = _row;
391    data[_row]->setTrue(_col);
392}
393
394
395inline void PS_BitMap_Fast::setFalse(const long _row, const long _col) {
396    if (_row > max_row) max_row = _row;
397    data[_row]->setFalse(_col);
398}
399
400
401bool PS_BitMap_Fast::load(PS_FileBuffer *_file) {
402    if (!_file->isReadonly()) return false;
403    // load max_row
404    _file->get_long(max_row);
405    // load bias
406    bias = (_file->get_char() == '1');
407    // initialize bitmap
408    if (!do_reserve(max_row+1, false)) return false;
409    for (long i = 0; i <= max_row; ++i) {
410        if (data[i]) {
411            delete data[i];
412        }
413        data[i] = new PS_BitSet_Fast(_file);
414    }
415    return true;
416}
417
418bool PS_BitMap_Fast::copy(const PS_BitMap_Fast *_other_bitmap) {
419    bias = _other_bitmap->bias;
420    if (!do_reserve(_other_bitmap->capacity, false)) return false;
421    for (long i = 0; i < capacity; ++i) {
422        if (!data[i]) data[i] = new PS_BitSet_Fast(bias, _other_bitmap->data[i]->capacity);
423        if (!data[i]->copy(_other_bitmap->data[i])) return false;
424    }
425    max_row = _other_bitmap->max_row;
426    return true;
427}
428
429bool PS_BitMap_Fast::reserve(const long _capacity) {
430    return do_reserve(_capacity, true);
431}
432bool PS_BitMap_Fast::do_reserve(const long _capacity, const bool _init_sets) {
433    if (_capacity <= capacity) return true;                      // smaller or same size requested ?
434    PS_BitSet **new_data;
435    long new_capacity_bytes = _capacity * sizeof(PS_BitSet*);
436    long old_capacity_bytes =  capacity * sizeof(PS_BitSet*);
437    new_data = (PS_BitSet **)malloc(new_capacity_bytes);         // get new memory for pointer array
438    if (new_data == 0) return false;
439    if (capacity > 0) memcpy(new_data, data, old_capacity_bytes); // copy old pointers
440    if (data) free(data);                                        // free old memory
441    data = new_data;
442    if (_init_sets) {
443        for (long i = capacity; i < _capacity; ++i) {            // init new requested bitsets
444            data[i] = new PS_BitSet_Fast(bias, 1);               // init field
445            if (data[i] == 0) return false;                      // check success
446        }
447    }
448    capacity = _capacity;                                        // store new capacity
449    return true;
450}
451
452
453long PS_BitMap_Counted::getTrueIndicesFor(const long _index, PS_BitSet::IndexSet &_index_set) {
454    // get total number of trues
455    unsigned long total_count_trues = count_true_per_index[_index];
456    // get trues in the row
457    data[_index]->getTrueIndices(_index_set, _index);
458    // scan column _index in the remaining rows until all trues are found
459    for (long row = _index+1;
460         ((row <= max_row) && (_index_set.size() < total_count_trues));
461         ++row) {
462        if (data[row]->Get(_index)) _index_set.insert(row);
463    }
464    return _index_set.size();
465}
466
467
468long PS_BitMap_Counted::getFalseIndicesFor(const long _index, PS_BitSet::IndexSet &_index_set) {
469    // get total number of falses
470    unsigned long total_count_falses = capacity - count_true_per_index[_index];
471    // get falses in the row
472    data[_index]->getFalseIndices(_index_set, _index);
473    // scan column _index in the remaining rows until all falses are found
474    for (long row = _index+1;
475         ((row <= max_row) && (_index_set.size() < total_count_falses));
476         ++row) {
477        if (!(data[row]->Get(_index))) _index_set.insert(row);
478    }
479    return _index_set.size();
480}
481
482
483long PS_BitMap_Counted::getTrueIndicesForRow(const long _row, PS_BitSet::IndexSet &_index_set) {
484    // get trues in the row
485    data[_row]->getTrueIndices(_index_set, _row);
486    return _index_set.size();
487}
488
489
490long PS_BitMap_Counted::getFalseIndicesForRow(const long _row, PS_BitSet::IndexSet &_index_set) {
491    // get falses in the row
492    data[_row]->getFalseIndices(_index_set, _row);
493    return _index_set.size();
494}
495
496
497long PS_BitMap_Counted::getCountOfTrues() {
498    long count = 0;
499    // sum up trues in the rows
500    for (long row = 0; row <= max_row; ++row) {
501        count += data[row]->getCountOfTrues(row);
502    }
503    return count;
504}
505
506
507bool PS_BitMap_Counted::set(const long _row, const long _col, const bool _value) {
508    if (_col > _row) printf("PS_BitMap_Counted::set( %li,%li,%1i ) not allowed\n", _row, _col, _value);
509    if (_row > max_row) max_row = _row;
510    bool previous_value = data[_row]->Set(_col, _value);
511    if (_value && !previous_value) {
512        ++count_true_per_index[_row];
513        ++count_true_per_index[_col];
514    }
515    else if (!_value && previous_value) {
516        --count_true_per_index[_row];
517        --count_true_per_index[_col];
518    }
519    return previous_value;
520}
521
522
523inline bool PS_BitMap_Counted::get(const long _row, const long _col) {
524    if (_col > _row) printf("PS_BitMap_Counted::get( %li,%li ) not allowed\n", _row, _col);
525    if (_row > max_row) max_row = _row;
526    return data[_row]->Get(_col);
527}
528
529
530inline void PS_BitMap_Counted::setTrue(const long _row, const long _col) {
531    if (_col > _row) printf("PS_BitMap_Counted::setTrue( %li,%li ) not allowed\n", _row, _col);
532    if (_row > max_row) max_row = _row;
533    data[_row]->setTrue(_col);
534}
535
536
537inline void PS_BitMap_Counted::setFalse(const long _row, const long _col) {
538    if (_col > _row) printf("PS_BitMap_Counted::setFalse( %li,%li ) not allowed\n", _row, _col);
539    if (_row > max_row) max_row = _row;
540    data[_row]->setFalse(_col);
541}
542
543
544bool PS_BitMap_Counted::load(PS_FileBuffer *_file) {
545    if (!_file->isReadonly()) return false;
546    // load max_row
547    _file->get_long(max_row);
548    // load bias
549    bias = (_file->get_char() == '1');
550    // initialize bitmap
551    if (!do_reserve(max_row+1, false)) return false;
552    for (long i = 0; i <= max_row; ++i) {
553        if (data[i]) {
554            delete data[i];
555        }
556        data[i] = new PS_BitSet_Fast(_file, i);
557    }
558    recalcCounters();
559    return true;
560}
561
562
563bool PS_BitMap_Counted::copy(const PS_BitMap_Counted *_other_bitmap) {
564    bias = _other_bitmap->bias;
565    if (!do_reserve(_other_bitmap->capacity, false)) return false;
566    memcpy(count_true_per_index, _other_bitmap->count_true_per_index, (capacity * sizeof(long)));
567    for (long i = 0; i < capacity; ++i) {
568        if (!data[i]) data[i] = new PS_BitSet_Fast(bias, _other_bitmap->data[i]->capacity);
569        if (!data[i]->copy(_other_bitmap->data[i])) return false;
570    }
571    max_row = _other_bitmap->max_row;
572    return true;
573}
574
575
576bool PS_BitMap_Counted::reserve(const long _capacity) {
577    return do_reserve(_capacity, true);
578}
579bool PS_BitMap_Counted::do_reserve(const long _capacity, const bool _init_sets) {
580    PS_BitSet **new_data;
581    long *new_counts;
582    if (_capacity <= capacity) return true;                      // smaller or same size requested ?
583    long new_data_bytes   = _capacity * sizeof(PS_BitSet*);
584    long old_data_bytes   =  capacity * sizeof(PS_BitSet*);
585    long new_counts_bytes = _capacity * sizeof(long);
586    long old_counts_bytes =  capacity * sizeof(long);
587    //
588    // allocate new memory
589    //
590    new_data   = (PS_BitSet **)malloc(new_data_bytes);
591    new_counts = (long *)malloc(new_counts_bytes);
592    //
593    // test is we got the memory we wanted
594    //
595    if (!new_data || !new_counts) {
596        // failed to allocate all memory so give up the parts we got
597        if (new_data)   free(new_data);
598        if (new_counts) free(new_counts);
599        return false;
600    }
601    //
602    // initialize new data-array
603    //
604    if (capacity > 0) memcpy(new_data, data, old_data_bytes);    // copy old pointers
605    if (data) free(data);                                        // free old memory
606    data = new_data;
607    if (_init_sets) {
608        for (long i = capacity; i < _capacity; ++i) {            // init new requested bitsets
609            data[i] = new PS_BitSet_Fast(bias, i+1);             // init field
610            if (data[i] == 0) return false;                      // check success
611        }
612    }
613    //
614    // initialize new counts-arrays
615    //
616    if (capacity > 0) memcpy(new_counts, count_true_per_index, old_counts_bytes);
617    memset(new_counts+old_counts_bytes, 0, new_counts_bytes-old_counts_bytes);
618    if (count_true_per_index) free(count_true_per_index);
619    count_true_per_index = new_counts;
620
621    capacity = _capacity;                                        // store new capacity
622    return true;
623}
624
625
626void PS_BitMap_Counted::print() {
627    printf("PS_BitMap_Counted : bias(%1i) max_row(%6li) capacity(%6li)\n", bias, max_row, capacity);
628    for (long i = 0; i < capacity; ++i) {
629        printf("[%5li] %6li ", i, count_true_per_index[i]);
630        data[i]->print(false);
631    }
632}
633
634
635void PS_BitMap_Counted::printGNUplot(const char *_title, char *_buffer, PS_FileBuffer *_file) {
636    // write title and header of bitmap
637    long size;
638    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);
639    _file->put(_buffer, size);
640
641    // write indices of trues per row
642    PS_BitSet::IndexSet trues;
643    for (long row = 0;
644          row < capacity;
645          ++row) {
646        data[row]->getTrueIndices(trues);
647        for (PS_BitSet::IndexSet::iterator col = trues.begin();
648              col != trues.end();
649              ++col) {
650            size = sprintf(_buffer, "%li %li\n", *col, row);
651            _file->put(_buffer, size);
652        }
653    }
654
655    // write dataset separator and header of counters
656    size = sprintf(_buffer, "\n\n# speciesID count (of trues)\n");
657    _file->put(_buffer, size);
658
659    // write counters per species
660    map<long, long> species_per_count;
661    for (long row = 0;
662          row < capacity;
663          ++row) {
664        size = sprintf(_buffer, "%li %li\n", row, count_true_per_index[row]);
665        _file->put(_buffer, size);
666        ++species_per_count[count_true_per_index[row]];
667    }
668
669    // write dataset separator and header of counters
670    size = sprintf(_buffer, "\n\n# count (of trues) count (of species)\n");
671    _file->put(_buffer, size);
672    for (map<long, long>::iterator count = species_per_count.begin();
673          count != species_per_count.end();
674          ++count) {
675        size = sprintf(_buffer, "%li %li\n", count->first, count->second);
676        _file->put(_buffer, size);
677    }
678
679    _file->flush();
680}
681
682
683void PS_BitMap_Counted::recalcCounters() {
684    printf("PS_BitMap_Counted::recalcCounters()\n");
685    memset(count_true_per_index, 0, capacity * sizeof(long));
686    for (long row = 0; row <= max_row; ++row) {
687        PS_BitSet *row_data = data[row];
688        if (row_data->getMaxUsedIndex() > row) printf("row %4li 0..%li ??\n", row, row_data->getMaxUsedIndex());
689        for (long col = 0; col <= row_data->getMaxUsedIndex(); ++col) {
690            if (row_data->Get(col)) {
691                ++count_true_per_index[col];
692                if (row != col) ++count_true_per_index[row];   // don't count diagonal trues twice
693            }
694        }
695    }
696}
697
698#else
699#error ps_bitmap.hxx included twice
700#endif
Note: See TracBrowser for help on using the repository browser.