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

SparseBitmatrix_intersection.hpp

Go to the documentation of this file.
00001 
00006 #ifndef __SPARSEBITMATRIX_INTERSECTION_HPP_
00007 #define __SPARSEBITMATRIX_INTERSECTION_HPP_ t
00008 
00009 #include "SparseBitvector_intersection.hpp"
00010 // #include "SparseBitmatrix.hpp"
00011 //#include "SparseBitmatrix_singleblock.hpp"
00012 #include "common/debug.hpp"
00013 #include <vector>
00014 
00029 template <bool EARLY_FILTERING, bool INTERSECTION_ROWSWAPPING, bool REMOVE_IDENTICAL_ROWS, class OutputIterator>
00030 int intersectSparseBitmatrix(const SparseBitmatrix& bmIn, const int row, SparseBitmatrix& bmOut, 
00031                              const int minimalLength, OutputIterator identicalRowsIterator) {
00032   assert(row >= 0 && row < bmIn.nRows());
00033 
00034   // 1. setup loop
00035   int tor = 0;
00036   int maxRowLength = 0;
00037   bmOut.setNRows(bmIn.nRows() - row - 1);
00038   const GET_ROW(row1, bmIn, row); // legacy: const SparseBitvector row1(bmIn.getRow(row));
00039 
00040   // 2. iterate over all other rows, i.e. otherrow > row.
00041   for (int otherRow = row+1; otherRow < bmIn.nRows(); otherRow++) {
00042     const GET_ROW(row2, bmIn, otherRow); // legacy: const SparseBitvector row2(bmIn.getRow(otherrow));
00043     GET_ROW(rowOut, bmOut, tor);         // legacy: SparseBitvector rowOut(bmOut.getRow(tor));
00044 
00045     bool hasMinimalLength = INTERSECTION_ROWSWAPPING && row1.length() < row2.length()
00046       ? intersection<EARLY_FILTERING>(rowOut, row2, row1, minimalLength)
00047       : intersection<EARLY_FILTERING>(rowOut, row1, row2, minimalLength);
00048 
00049     // intersect and only do anything if output row has at least minimalLength elements.
00050     if (hasMinimalLength) {
00051       if (REMOVE_IDENTICAL_ROWS && rowOut.length() == row1.length())
00052         // a. identical row: skip, but output to identicalRowsIterator.
00053         *identicalRowsIterator++ = bmIn.rowLabel(otherRow); 
00054       else {
00055         // b. retain row:
00056         // bmOut.setRowLength(tor, rowOut.length());
00057         bmOut.setRowLabel(tor, bmIn.rowLabel(otherRow));
00058         if (rowOut.length() > maxRowLength)
00059           maxRowLength = rowOut.length();
00060         tor++;
00061       }
00062     }
00063   }
00064 
00065   // 3. remember matrix structure.
00066   bmOut.setNRows(tor);
00067   bmOut.setMaxRowLength(maxRowLength);
00068 
00069   return tor;
00070 }
00071 
00072 
00088 template <bool EARLY_FILTERING, bool INTERSECTION_ROWSWAPPING, bool REMOVE_IDENTICAL_ROWS, class OutputIterator>
00089 int intersectSparseBitmatrix(const SparseBitmatrix& bmIn, const int row, const vector<int> otherRows, SparseBitmatrix& bmOut, 
00090                              const int minimalLength, OutputIterator identicalRowsIterator) {
00091   assert(row >= 0 && row < bmIn.nRows());
00092 
00093   // 1. setup loop
00094   int tor = 0;
00095   int maxRowLength = 0;
00096   bmOut.setNRows(otherRows.size());
00097   const GET_ROW(row1, bmIn, row);
00098 
00099   // 2. iterate over all other rows
00100   for (vector<int>::iterator iter = otherRows.begin(); iter != otherRows.end(); iter++ ) {
00101     int otherRow = *iter;
00102     const GET_ROW(row2, bmIn, otherRow);
00103     GET_ROW(rowOut, bmOut, tor);
00104 
00105     bool hasMinimalLength = INTERSECTION_ROWSWAPPING && row1.length() < row2.length()
00106       ? intersection<EARLY_FILTERING>(rowOut, row2, row1, minimalLength)
00107       : intersection<EARLY_FILTERING>(rowOut, row1, row2, minimalLength);
00108 
00109     // intersect and only do anything if output row has at least minimalLength elements.
00110     if (hasMinimalLength) {
00111       if (REMOVE_IDENTICAL_ROWS && rowOut.length() == row1.length())
00112         // a. identical row: skip, but output to identicalRowsIterator.
00113         *identicalRowsIterator++ = bmIn.rowLabel(otherRow); 
00114       else {
00115         // b. retain row:
00116         // bmOut.setRowLength(tor, rowOut.length());
00117         bmOut.setRowLabel(tor, bmIn.rowLabel(otherRow));
00118         if (rowOut.length() > maxRowLength)
00119           maxRowLength = rowOut.length();
00120         tor++;
00121       }
00122     }
00123   }
00124 
00125   // 3. remember matrix structure.
00126   bmOut.setNRows(tor);
00127   bmOut.setMaxRowLength(maxRowLength);
00128 
00129   return tor;
00130 }
00131 
00132 
00137 template <bool EARLY_FILTERING>
00138 int cardinalityIntersectionSparseBitmatrix_TwoRows(const SparseBitmatrix& bmIn, const int minimalLength) {
00139   const GET_ROW(row0, bmIn, 0); // legacy: const SparseBitvector row0(bmIn.getRow(0));
00140   const GET_ROW(row1, bmIn, 1); // legacy: const SparseBitvector row1(bmIn.getRow(1));
00141   return cardinalityOfIntersection<EARLY_FILTERING>(row0, row1, minimalLength);
00142 }
00143 
00144 
00145 #endif

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