Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

SparseBitmatrix.cpp

Go to the documentation of this file.
00001 /*
00002  * SparseBitmatrix.cpp
00003  *
00004  * Time-stamp: <05/05/30 10:04:56 lars>
00005  *
00006  * history: 2004/04/01  1.0  LST created.
00007  */
00008 
00009 #include "common/debug.hpp"
00010 #include "SparseBitmatrix.hpp"
00011 // #include "Sorting.h"
00012 
00013 #include <iostream>
00014 using namespace std;
00015 
00016 SparseBitmatrix::SparseBitmatrix(int numRows) {
00017 #if DEBUG_LEVEL>=20
00018   cerr << endl << "!SBc " << numRows << " x ?" << endl;
00019 #endif
00020 
00021   this->numRows = numRows;
00022   rows = new (int*)[numRows];
00023   this->rowLengths = new int[numRows];
00024   this->rowLabels = new ROW_T[numRows];
00025   rowSizes = new int[numRows];
00026   this->m_maxRowLength = 0;
00027   sizeRows = numRows;
00028 #if USE_DYNAMIC_MEMORY_NODE_EXIT
00029   rowshift = 0;
00030 #endif
00031 }
00032 
00033 
00034 SparseBitmatrix::SparseBitmatrix(int numRows, int rowCapacity) {
00035 #if DEBUG_LEVEL>=20
00036   cerr << endl << "!SBc " << numRows << " x " << rowCapacity << endl;
00037 #endif
00038 
00039   this->numRows = numRows;
00040   rows = new (int*)[numRows];
00041   this->rowLengths = new int[numRows];
00042   this->rowLabels = new ROW_T[numRows];
00043   rowSizes = new int[numRows];
00044   for (int i = 0; i < numRows;  i++) {
00045     int len = rowCapacity;
00046     // this->rowLengths[i] = len;
00047     this->rowLengths[i] = 0;
00048     rowSizes[i] = len;
00049     this->rowLabels[i] = i; // -1; 
00050     rows[i] = new int[len];
00051   }
00052   // this->m_maxRowLength = rowLength;
00053   this->m_maxRowLength = 0;
00054   sizeRows = numRows;
00055 #if USE_DYNAMIC_MEMORY_NODE_EXIT
00056   rowshift = 0;
00057 #endif
00058 
00059 }
00060 
00061 SparseBitmatrix::SparseBitmatrix(int numRows, int* rowCapacities) {
00062 #if DEBUG_LEVEL>=20
00063   cerr << endl << "!SBdyn " << numRows << " x ";
00064   int min = numRows > 0 ? rowCapacities[0] : 0,
00065     max = min, sum = min;
00066   for (int i = 1; i < numRows; i++) {
00067     sum += rowCapacities[i];
00068     if (rowCapacities[i] < min)
00069       min = rowCapacities[i];
00070     else if (rowCapacities[i] > max)
00071       max = rowCapacities[i];
00072   }
00073   double mean = numRows > 0 ? sum * 1.0 / numRows : 0;
00074   cerr << "(" << min << "-" << max << "; " << mean << ")" << endl;
00075 #endif
00076 
00077   this->numRows = numRows;
00078   rows = new (int*)[numRows];
00079   this->rowLengths = new int[numRows];
00080   this->rowLabels = new ROW_T[numRows];
00081   rowSizes = new int[numRows];
00082   this->m_maxRowLength = 0;
00083   for (int i = 0; i < numRows;  i++) {
00084     int len = rowCapacities[i];
00085     // this->rowLengths[i] = len;
00086     this->rowLengths[i] = 0;
00087     rowSizes[i] = len;
00088     this->rowLabels[i] = i; // -1; 
00089     rows[i] = new int[len];
00090     // if (len > m_maxRowLength)
00091     //  this->m_maxRowLength = len;
00092   }
00093   sizeRows = numRows;
00094 #if USE_DYNAMIC_MEMORY_NODE_EXIT
00095   rowshift = 0;
00096 #endif
00097 }
00098 
00099 SparseBitmatrix::SparseBitmatrix(int numRows, int* rowCapacities, int* rowLabels) {
00100 #if DEBUG_LEVEL>=20
00101   cerr << endl << "!SBdyn " << numRows << " x ";
00102   int min = numRows > 0 ? rowCapacities[0] : 0,
00103     max = min, sum = min;
00104   for (int i = 1; i < numRows; i++) {
00105     sum += rowCapacities[i];
00106     if (rowCapacities[i] < min)
00107       min = rowCapacities[i];
00108     else if (rowCapacities[i] > max)
00109       max = rowCapacities[i];
00110   }
00111   double mean = numRows > 0 ? sum * 1.0 / numRows : 0;
00112   cerr << "(" << min << "-" << max << "; " << mean << ")" << endl;
00113 #endif
00114 
00115   this->numRows = numRows;
00116   rows = new (int*)[numRows];
00117   this->rowLengths = new int[numRows];
00118   this->rowLabels = new ROW_T[numRows];
00119   rowSizes = new int[numRows];
00120   this->m_maxRowLength = 0;
00121   for (int i = 0; i < numRows;  i++) {
00122     int len = rowCapacities[i];
00123     rows[i] = new int[len];
00124     rowSizes[i] = len;
00125     // this->rowLengths[i] = len;
00126     this->rowLengths[i] = 0;
00127     this->rowLabels[i] = rowLabels[i];
00128     // if (len > m_maxRowLength)
00129     //  this->m_maxRowLength = len;
00130   }
00131   sizeRows = numRows;
00132 #if USE_DYNAMIC_MEMORY_NODE_EXIT
00133   rowshift = 0;
00134 #endif
00135 }
00136 
00137 SparseBitmatrix::~SparseBitmatrix() {
00138 #if DEBUG_LEVEL >= 20
00139   cerr << endl << "~SB ... "; cerr.flush();
00140   cerr << " rows[" << sizeRows << "] "; cerr.flush();
00141 #endif
00142   for (int i = 0; i < sizeRows; i++) {
00143     assert(rows[i] != NULL);
00144 #if DEBUG_LEVEL >= 20
00145     cerr << i << " "; cerr.flush();
00146 #endif
00147     delete[] rows[i];
00148   }
00149 #if USE_DYNAMIC_MEMORY_NODE_EXIT && false
00150 #if DEBUG_LEVEL >= 20
00151   cerr << " rows "; cerr.flush();
00152 #endif
00153   delete[] &(rows[-rowshift]);
00154 #if DEBUG_LEVEL >= 20
00155   cerr << " rowLengths "; cerr.flush();
00156 #endif
00157   delete[] &(rowLengths[-rowshift]);
00158 #if DEBUG_LEVEL >= 20
00159   cerr << " rowLabels "; cerr.flush();
00160 #endif
00161   delete[] &(rowLabels[-rowshift]);
00162 #if DEBUG_LEVEL >= 20
00163   cerr << " rowSizes "; cerr.flush();
00164 #endif
00165   delete[] &(rowSizes[-rowshift]);
00166 #else
00167 #if DEBUG_LEVEL >= 20
00168   cerr << " rows "; cerr.flush();
00169 #endif
00170   assert(rows != NULL);
00171   delete[] rows;
00172 #if DEBUG_LEVEL >= 20
00173   cerr << " rowLengths "; cerr.flush();
00174 #endif
00175   assert(rowLengths != NULL);
00176   delete[] rowLengths;
00177 #if DEBUG_LEVEL >= 20
00178   cerr << " rowLabels "; cerr.flush();
00179 #endif
00180   delete[] rowLabels;
00181 #if DEBUG_LEVEL >= 20
00182   cerr << " rowSizes "; cerr.flush();
00183 #endif
00184   delete[] rowSizes;
00185 #endif
00186 #if DEBUG_LEVEL >= 20
00187   cerr << " - done" << endl;
00188 #endif
00189 }
00190 
00191 /*
00192 void SparseBitmatrix::sortRows() {
00193   for (int i = 0; i < numRows; i++)
00194     quickSort(rows[i], 0, rowLengths[i]);
00195 }
00196 */
00197 
00198 
00199 void SparseBitmatrix::dump() const {
00200   cout << endl << "dump " << numRows << " x ";
00201   int min = numRows > 0 ? rowLengths[0] : 0,
00202     max = min, sum = min;
00203   for (int i = 1; i < numRows; i++) {
00204     sum += rowLengths[i];
00205     if (rowLengths[i] < min)
00206       min = rowLengths[i];
00207     else if (rowLengths[i] > max)
00208       max = rowLengths[i];
00209   }
00210   double mean = numRows > 0 ? sum * 1.0 / numRows : 0;
00211 
00212   int smin = sizeRows > 0 ? rowSizes[0] : 0,
00213     smax = smin, ssum = smin;
00214   for (int i = 1; i < sizeRows; i++) {
00215     ssum += rowSizes[i];
00216     if (rowSizes[i] < smin)
00217       smin = rowSizes[i];
00218     else if (rowSizes[i] > smax)
00219       smax = rowSizes[i];
00220   }
00221   double smean = sizeRows > 0 ? ssum * 1.0 / sizeRows : 0;
00222   cout << "(" << min << "-" << max << "; " << mean << " : "
00223        << sizeRows << " x " << smin << "-" << smax << "; " << smean << ")" << endl;
00224 
00225   for (int i = 0; i < numRows; i++) {
00226     cout << i << " \"" << rowLabels[i] << "\":";
00227     int* row = rows[i];
00228     int len = rowLengths[i];
00229     for (int j = 0; j < len; j++)
00230       cout << " " << row[j];
00231     cout << "\tlen= " << rowLengths[i] << ", size= " << rowSizes[i] << endl;
00232   }
00233 
00234   if (numRows == 0) {
00235     cout << "  sizes: ";
00236     for (int i = 0; i < sizeRows; i++)
00237       cout << rowSizes[i] << " ";
00238     cout << endl;
00239   }
00240 }
00241 
00242 
00243 void SparseBitmatrix::dumpStructure() const {
00244   cout << endl << "dump " << numRows << " x ";
00245   int min = numRows > 0 ? rowLengths[0] : 0,
00246     max = min, sum = min;
00247   for (int i = 1; i < numRows; i++) {
00248     sum += rowLengths[i];
00249     if (rowLengths[i] < min)
00250       min = rowLengths[i];
00251     else if (rowLengths[i] > max)
00252       max = rowLengths[i];
00253   }
00254   double mean = numRows > 0 ? sum * 1.0 / numRows : 0;
00255 
00256   int smin = sizeRows > 0 ? rowSizes[0] : 0,
00257     smax = smin, ssum = smin;
00258   for (int i = 1; i < sizeRows; i++) {
00259     ssum += rowSizes[i];
00260     if (rowSizes[i] < smin)
00261       smin = rowSizes[i];
00262     else if (rowSizes[i] > smax)
00263       smax = rowSizes[i];
00264   }
00265   double smean = sizeRows > 0 ? ssum * 1.0 / sizeRows : 0;
00266   cout << "(" << min << "-" << max << "; " << mean << " : "
00267        << sizeRows << " x " << smin << "-" << smax << "; " << smean << ")" << endl;
00268 }
00269 
00270 
00271 
00272 void SparseBitmatrix::computeEmpiricalDensity(long& numCells, long& numUsedCells) const {
00273   // 0. search for non-empty row:
00274   int i = 0; 
00275   while (i < numRows && rowLengths[i] == 0)
00276     ++i;
00277   if (i == numRows) {
00278     numCells = 0;
00279     numUsedCells = 0;
00280     return;
00281   }
00282 
00283   // 1. compute #used cells and range of values stored.
00284   numUsedCells += rowLengths[i];
00285   int min = rows[i][0], max = rows[i][rowLengths[i]-1]; // assume rows are sorted in ascending order.
00286   i++;
00287   for ( ; i < numRows; i++) {
00288     numUsedCells += rowLengths[i];
00289     if (rows[i][0] < min)
00290       min = rows[i][0];
00291     if (rows[i][rowLengths[i]-1] > max)
00292       max = rows[i][rowLengths[i]-1];
00293   }
00294 
00295   numCells += numRows * (max - min + 1);
00296 }
00297 
00298 
00299 
00305 int SparseBitmatrix::filterMinimalRowLength(int minRowLength) {
00306   int toi = 0;
00307   m_maxRowLength = 0;
00308   for (int i = 0; i < numRows; i++) {
00309     int rowlen = rowLengths[i];
00310     if (rowlen >= minRowLength) {
00311       assert(rowSizes[toi] >= rowlen);
00312       rowLabels[toi] = rowLabels[i];
00313       rowLengths[toi] = rowlen;
00314       int* rowFrom = rows[i];
00315       int* rowTo = rows[toi];
00316       for (int j = 0; j < rowlen; j++)
00317         rowTo[j] = rowFrom[j];
00318       toi++;
00319       if (rowlen > m_maxRowLength)
00320         m_maxRowLength = rowlen; 
00321     }
00322   }
00323   numRows = toi;
00324   return toi;
00325 }
00326 
00327 
00335 int SparseBitmatrix::filterMinimalRowLength_destroyStructure(int minRowLength) {
00336   int toi = 0;
00337   m_maxRowLength = 0;
00338   for (int i = 0; i < numRows; i++) {
00339     int rowlen = rowLengths[i];
00340     if (rowlen >= minRowLength) {
00341       rows[toi] = rows[i];
00342       rowLengths[toi] = rowLengths[i];
00343       rowSizes[toi] = rowSizes[i];
00344       rowLabels[toi] = rowLabels[i];
00345       toi++;
00346       if (rowlen > m_maxRowLength)
00347         m_maxRowLength = rowlen; 
00348     } else {
00349       delete[] rows[i];
00350     }
00351   }
00352   for (int i = toi; i < numRows; i++) 
00353     rows[i] = NULL; // added 31.8.2004
00354   numRows = toi;
00355   // sizeRows = toi; // added 31.8.2004
00356   return toi;
00357 }
00358 
00363 /*
00364 void SparseBitmatrix::shiftRowStorage(int firstRow) {
00365   assert(firstRow == 1);
00366 #if USE_DYNAMIC_MEMORY_NODE_EXIT
00367   cout << "shift [" << rowshift << " + " << firstRow << "] ... "; cout.flush();
00368 
00369   // a. delete all rows[]:
00370   for (int i = 0; i < sizeRows; i++) {
00371     assert(rows[i] != NULL);
00372     delete[] rows[i];
00373   }
00374 
00375   // b. reallocate all rows[]:
00376   for (int i = 0; i < sizeRows-1; i++) {
00377     rowSizes[i] = rowSizes[i+1];
00378     rows[i] = new int[rowSizes[i]];
00379     rowLengths[i] = 0;
00380   }
00381   rows[sizeRows-1] = new int[0];
00382   rowSizes[sizeRows-1] = 0;
00383   rowLengths[sizeRows-1] = 0;
00384   numRows = 0;
00385 
00386   cout << "ok" << endl;
00387 #endif
00388 }
00389 */

Generated on Sun Sep 17 17:50:40 2006 for FIM environment by  doxygen 1.4.4