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 bit_reference::bit_reference
00013 (unsigned char& byte, unsigned char position):
00014 _mask(1<<position), _byte(byte) {}
00015
00016 bit_reference& bit_reference::operator=
00017 (bool x) {
00018 if(x)
00019 _byte|=_mask;
00020 else
00021 _byte&=~_mask;
00022 return *this;
00023 }
00024
00025
00026 bit_reference::operator bool() const {
00027 return (_byte&_mask)!=0;
00028 }
00029
00030 void bit_reference::swap() {
00031 _byte^=_mask;
00032 }
00033
00034
00035
00036 const bit_reference bool_vector_t::const_iterator::operator*() {
00037 return bit_reference(_content[_position/8], _position%8);
00038 }
00039
00040 bit_reference bool_vector_t::iterator::operator*() {
00041 return bit_reference(_content[_position/8], _position%8);
00042 }
00043
00044
00045
00046 bool_vector_t::bool_vector_t(unsigned int size): _size(size), _capacity((size+7)/8) {
00047 _content = (unsigned char*)std::calloc(_capacity,1);
00048 }
00049
00050 const bit_reference bool_vector_t::operator[](unsigned i) const{
00051 return bit_reference(_content[i/8], i%8);
00052 }
00053
00054 bit_reference bool_vector_t::operator[](unsigned i) {
00055 return bit_reference(_content[i/8], i%8);
00056 }
00057
00058 bool_vector_t::iterator bool_vector_t::begin() {
00059 return iterator(_content,0);
00060 }
00061 bool_vector_t::const_iterator bool_vector_t::begin() const{
00062 return const_iterator(_content,0);
00063 }
00064
00065 bool_vector_t::iterator bool_vector_t::end() {
00066 return iterator(_content,_size);
00067 }
00068 bool_vector_t::const_iterator bool_vector_t::end() const{
00069 return const_iterator(_content,_size);
00070 }
00071
00072
00073 bool_vector_t::~bool_vector_t () {
00074 std::free(_content);
00075 }
00076
00077 void bool_vector_t::resize(unsigned new_size) {
00078 reserve(new_size);
00079 _size = new_size;
00080 }
00081
00082 void bool_vector_t::reserve(unsigned new_capacity) {
00083 unsigned nc =(7+new_capacity)/8;
00084 if(nc > _capacity) {
00085 _capacity = nc;
00086 _content = (unsigned char*)realloc(_content, nc);
00087 }
00088 }
00089
00090 }