00001 00007 #ifndef BOOL_VECTOR_HH 00008 #define BOOL_VECTOR_HH 00009 00018 namespace my_lib { 00019 00028 struct bit_reference{ 00029 private: 00030 unsigned char & _byte; 00031 unsigned char _mask; 00032 public: 00033 bit_reference(unsigned char& byte, unsigned char position); 00034 operator bool() const; 00035 bit_reference& operator=(bool b); 00036 void swap(); 00037 }; 00038 00039 00047 class bool_vector_t { 00048 private: 00049 00050 unsigned char * _content; //le tableau qui stocke les valeurs 00051 unsigned _size; //la taille apparente du vecteur 00052 unsigned _capacity; //la taille réelle du tableau en octets : _capacity*8 >= _size 00053 00054 public: 00061 bool_vector_t (unsigned size); 00067 ~bool_vector_t (); 00068 00078 bit_reference operator[] (unsigned i) ; 00085 const bit_reference operator[] (unsigned i) const; 00086 00094 inline 00095 unsigned size() const { 00096 return _size; 00097 } 00098 00110 inline 00111 unsigned capacity() const { 00112 return _capacity*8; 00113 } 00114 00122 void resize(unsigned new_size); 00131 void reserve(unsigned new_capacity); 00132 00133 00138 struct const_iterator{ 00139 protected: 00140 unsigned char* _content; 00141 unsigned _position; 00142 public: 00143 const_iterator(unsigned char* content, unsigned position) : 00144 _position(position) , _content(content) {} 00145 inline 00146 bool operator==(const const_iterator& it) const{ 00147 return _content==it._content && _position==it._position; 00148 } 00149 inline 00150 bool operator!=(const const_iterator& it) const{ 00151 return _content!=it._content || _position!=it._position; 00152 } 00157 inline 00158 const_iterator& operator++() { 00159 ++_position; 00160 return *this; 00161 } 00162 00168 const bit_reference operator*(); 00169 }; 00170 00177 struct iterator : public const_iterator{ 00178 public: 00179 iterator(unsigned char* content, unsigned position) : const_iterator(content, position) {} 00180 inline 00181 iterator& operator++() { 00182 ++_position; 00183 return *this; 00184 } 00185 00186 bit_reference operator*(); 00187 }; 00188 00193 iterator begin() ; 00194 00199 const_iterator begin() const ; 00200 00205 iterator end() ; 00206 00211 const_iterator end() const ; 00212 }; 00213 00214 00215 00216 } // fin namespace my_lib 00217 00218 00219 #endif //BOOL_VECTOR_HH