00001
00002
00003
00004
00005
00006
00007
00008 #ifndef __SPARSE_BITVECTOR__
00009 #define __SPARSE_BITVECTOR__ t
00010
00011 #include <iostream>
00012 using namespace std;
00013
00014 #include "common/debug.hpp"
00015
00022 class SparseBitvector {
00023 public:
00028 #if DEBUG_LEVEL > 0
00029 SparseBitvector(int* values, int& length, int capacity) : m_values(values), m_length(length), m_capacity(capacity) {}
00030 #else
00031 SparseBitvector(int* values, int& length) : m_values(values), m_length(length) {}
00032 #endif
00033
00034 int length() const { return m_length; }
00035 int lengthpp() { return m_length++; }
00036 void setLength(int length) {
00037 assert(length <= m_capacity);
00038 m_length = length;
00039 }
00040 void resize(int length) { setLength(length); }
00041 int operator[] (int position) const {
00042 assert(position >= 0);
00043 assert(position < m_length);
00044 return m_values[position];
00045 }
00046 void setElement(int position, int value) const {
00047 assert(position >= 0);
00048 assert(position < m_capacity);
00049 m_values[position] = value;
00050 }
00051 void push_back(int column) {
00052 assert(m_length < m_capacity);
00053 assert(m_length == 0 || column > m_values[m_length-1]);
00054 m_values[m_length++] = column;
00055 }
00056 int* values() const { return m_values; }
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089 #if DEBUG_LEVEL > 0
00090
00091 class iterator {
00092 public:
00093 iterator(int* const values, int& size, int index) : m_values(values), m_size(size), m_index(index) {}
00094 int& operator* () const {
00095 assert(m_index >= 0);
00096 assert(m_index < m_size);
00097 return m_values[m_index];
00098 }
00099 iterator& operator++ () {
00100 assert(m_index < m_size);
00101 m_index++;
00102 return *this;
00103 }
00104 iterator operator++ (int) {
00105 assert(m_index < m_size);
00106 iterator iter = *this;
00107 m_index++;
00108 return iter;
00109 }
00110 iterator& operator-- () {
00111 assert(m_index > 0);
00112 m_index--;
00113 return *this;
00114 }
00115 iterator operator-- (int) {
00116 assert(m_index > 0);
00117 iterator iter = *this;
00118 m_index--;
00119 return iter;
00120 }
00121 void operator+= (int index) {
00122 assert(m_index + index <= m_size);
00123 m_index += index;
00124 }
00125 void operator-= (int index) {
00126 assert(m_index >= index);
00127 m_index -= index;
00128 }
00129 bool operator!= (iterator& iter) const {
00130 assert(m_values == iter.m_values);
00131 assert(m_size == iter.m_size);
00132 return m_index != iter.m_index;
00133 }
00134 bool operator== (iterator& iter) const {
00135 assert(m_values == iter.m_values);
00136 assert(m_size == iter.m_size);
00137 return m_index == iter.m_index;
00138 }
00139 bool operator< (iterator& iter) const {
00140 assert(m_values == iter.m_values);
00141 assert(m_size == iter.m_size);
00142 return m_index < iter.m_index;
00143 }
00144 bool operator<= (iterator& iter) const {
00145 assert(m_values == iter.m_values);
00146 assert(m_size == iter.m_size);
00147 return m_index <= iter.m_index;
00148 }
00149 bool operator> (iterator& iter) const {
00150 assert(m_values == iter.m_values);
00151 assert(m_size == iter.m_size);
00152 return m_index > iter.m_index;
00153 }
00154 bool operator>= (iterator& iter) const {
00155 assert(m_values == iter.m_values);
00156 assert(m_size == iter.m_size);
00157 return m_index >= iter.m_index;
00158 }
00159 int operator- (const iterator& iter) const {
00160 assert(m_values == iter.m_values);
00161 assert(m_size == iter.m_size);
00162 return m_index - iter.m_index;
00163 }
00164 iterator operator- (const int size) const {
00165 assert(m_index - size >= 0);
00166 assert(m_index - size <= m_size);
00167 return iterator(m_values, m_size, m_index - size);
00168 }
00169 iterator operator+ (const int size) const {
00170 assert(m_index + size >= 0);
00171 assert(m_index + size <= m_size);
00172 return iterator(m_values, m_size, m_index + size);
00173 }
00174 iterator operator= (iterator& iter) {
00175 assert(m_values == iter.m_values);
00176 assert(m_size == iter.m_size);
00177 m_index = iter.m_index;
00178 return *this;
00179 }
00180 int index() const { return m_index; }
00181
00182 int * const m_values;
00183 int& m_size;
00184 int m_index;
00185 };
00186
00187 class const_iterator {
00188 public:
00189 const_iterator(int const * const values, int& size, int index) : m_values(values), m_size(size), m_index(index) {}
00190 int operator* () const {
00191 assert(m_index >= 0);
00192 assert(m_index < m_size);
00193 return m_values[m_index];
00194 }
00195 const_iterator& operator++ () {
00196 assert(m_index < m_size);
00197 m_index++;
00198 return *this;
00199 }
00200 const_iterator operator++ (int) {
00201 assert(m_index < m_size);
00202 const_iterator iter = *this;
00203 m_index++;
00204 return iter;
00205 }
00206 const_iterator& operator-- () {
00207 assert(m_index > 0);
00208 m_index--;
00209 return *this;
00210 }
00211 const_iterator operator-- (int) {
00212 assert(m_index > 0);
00213 const_iterator iter = *this;
00214 m_index--;
00215 return iter;
00216 }
00217 void operator-= (int index) {
00218 assert(m_index >= index);
00219 m_index -= index;
00220 }
00221 void operator+= (int index) {
00222 assert(m_index + index <= m_size);
00223 m_index += index;
00224 }
00225 bool operator!= (const_iterator& iter) const {
00226 assert(m_values == iter.m_values);
00227 assert(m_size == iter.m_size);
00228 return m_index != iter.m_index;
00229 }
00230 bool operator== (const_iterator& iter) const {
00231 assert(m_values == iter.m_values);
00232 assert(m_size == iter.m_size);
00233 return m_index == iter.m_index;
00234 }
00235 bool operator< (const_iterator& iter) const {
00236 assert(m_values == iter.m_values);
00237 assert(m_size == iter.m_size);
00238 return m_index < iter.m_index;
00239 }
00240 bool operator<= (const_iterator& iter) const {
00241 assert(m_values == iter.m_values);
00242 assert(m_size == iter.m_size);
00243 return m_index <= iter.m_index;
00244 }
00245 bool operator> (const_iterator& iter) const {
00246 assert(m_values == iter.m_values);
00247 assert(m_size == iter.m_size);
00248 return m_index > iter.m_index;
00249 }
00250 bool operator>= (const_iterator& iter) const {
00251 assert(m_values == iter.m_values);
00252 assert(m_size == iter.m_size);
00253 return m_index >= iter.m_index;
00254 }
00255 int operator- (const const_iterator& iter) const {
00256 assert(m_values == iter.m_values);
00257 assert(m_size == iter.m_size);
00258 return m_index - iter.m_index;
00259 }
00260 const_iterator operator- (const int size) const {
00261 assert(m_index - size >= 0);
00262 assert(m_index - size <= m_size);
00263 return const_iterator(m_values, m_size, m_index - size);
00264 }
00265 const_iterator operator+ (const int size) const {
00266 assert(m_index + size >= 0);
00267 assert(m_index + size <= m_size);
00268 return const_iterator(m_values, m_size, m_index + size);
00269 }
00270 const_iterator operator= (const_iterator& iter) {
00271 assert(m_values == iter.m_values);
00272 assert(m_size == iter.m_size);
00273 m_index = iter.m_index;
00274 return *this;
00275 }
00276 int index() const { return m_index; }
00277 private:
00278 int const * const m_values;
00279 int& m_size;
00280 int m_index;
00281 };
00282
00283 iterator begin() { return iterator(m_values, m_length, 0); }
00284 iterator end() { return iterator(m_values, m_length, m_length); }
00285 const_iterator begin() const { return const_iterator(m_values, m_length, 0); }
00286 const_iterator end() const { return const_iterator(m_values, m_length, m_length); }
00287 iterator beginUnbounded() { return iterator(m_values, m_capacity, 0); }
00288 iterator endUnbounded() { return iterator(m_values, m_capacity, m_length); }
00289
00290 void resizeToEndOf(iterator& iter) { resize(iter.index()); }
00291 void resizeToEndOf(const_iterator& iter) { resize(iter.index()); }
00292
00293 #else
00294
00295 typedef int* iterator;
00296 typedef int const * const_iterator;
00297
00298 iterator begin() { return m_values; }
00299 iterator end() { return m_values + m_length; }
00300 const_iterator begin() const { return m_values; }
00301 const_iterator end() const { return m_values + m_length; }
00302 iterator beginUnbounded() { return m_values; }
00303 iterator endUnbounded() { return m_values + m_length; }
00304
00305 void resizeToEndOf(iterator& iter) { resize(iter - m_values); }
00306 void resizeToEndOf(const_iterator& iter) { resize(iter - m_values); }
00307
00308 #endif
00309
00310
00311 #if DEBUG_LEVEL > 0
00312 int capacity() const {
00313 return m_capacity;
00314 }
00315 #endif
00316
00317 private:
00318 int* const m_values;
00319 int& m_length;
00320 #if DEBUG_LEVEL > 0
00321 int m_capacity;
00322 #endif
00323 };
00324
00325 ostream& operator<< (ostream& out, SparseBitvector const & vector);
00326
00327
00328
00329
00330
00331 #if DEBUG_LEVEL > 0
00332 #define GET_ROW(vector, matrix, row) SparseBitvector vector ( matrix .rowValues( row ), matrix .rowLength( row ), matrix .capacity( row ))
00333 #else
00334 #define GET_ROW(vector, matrix, row) SparseBitvector vector ( matrix .rowValues( row ), matrix .rowLength( row ))
00335 #endif
00336
00337 #endif