Go to the documentation of this file.00001
00006 #include<my_lib/bool_vector.hh>
00007 #include<cstdlib>
00008
00009 namespace my_lib {
00010
00011
00012 bool_vector_t::bool_handler_t::bool_handler_t
00013 (unsigned char position, unsigned char* byte):
00014 _mask(1<<position), _byte(byte) {}
00015
00016 bool_vector_t::bool_handler_t& bool_vector_t::bool_handler_t::operator=
00017 (const bool& x) {
00018 if(x)
00019 *_byte|=_mask;
00020 else
00021 *_byte&=~_mask;
00022 return *this;
00023 }
00024
00025
00026 bool_vector_t::bool_handler_t::operator bool() const {
00027 return *_byte&_mask;
00028 }
00029
00030
00031
00032 const bool_vector_t::bool_handler_t bool_vector_t::const_iterator::operator*() {
00033 return bool_handler_t(_position%8, _content+_position/8);
00034 }
00035
00036 bool_vector_t::bool_handler_t bool_vector_t::iterator::operator*() {
00037 return bool_handler_t(_position%8, _content+_position/8);
00038 }
00039
00040
00041
00042 bool_vector_t::bool_vector_t(unsigned int size): _size(size), _capacity((size+7)/8) {
00043 _content = (unsigned char*)std::calloc(_capacity,1);
00044 }
00045
00046 const bool_vector_t::bool_handler_t bool_vector_t::operator[](unsigned i) const{
00047 return bool_handler_t(i%8, _content+i/8);
00048 }
00049
00050 bool_vector_t::bool_handler_t bool_vector_t::operator[](unsigned i) {
00051 return bool_handler_t(i%8, _content+i/8);
00052 }
00053
00054 bool_vector_t::iterator bool_vector_t::begin() {
00055 return iterator(_content,0);
00056 }
00057 bool_vector_t::const_iterator bool_vector_t::begin() const{
00058 return const_iterator(_content,0);
00059 }
00060
00061 bool_vector_t::iterator bool_vector_t::end() {
00062 return iterator(_content,_size);
00063 }
00064 bool_vector_t::const_iterator bool_vector_t::end() const{
00065 return const_iterator(_content,_size);
00066 }
00067
00068
00069 bool_vector_t::~bool_vector_t () {
00070 std::free(_content);
00071 }
00072
00073 void bool_vector_t::resize(unsigned new_size) {
00074 reserve(new_size);
00075 _size = new_size;
00076 }
00077
00078 void bool_vector_t::reserve(unsigned new_capacity) {
00079 unsigned nc =(7+new_capacity)/8;
00080 if(nc > _capacity) {
00081 _capacity = nc;
00082 _content = (unsigned char*)realloc(_content, nc);
00083 }
00084 }
00085
00086 }