8 # include <tpl_dynArray.H>
25 unsigned int read_bit(
const unsigned int i)
const
39 default:
throw std::out_of_range (
"bit index greater than 7");
43 void write_bit(
const unsigned int i,
const unsigned int value)
50 case 0 : b0 = value;
break;
51 case 1 : b1 = value;
break;
52 case 2 : b2 = value;
break;
53 case 3 : b3 = value;
break;
54 case 4 : b4 = value;
break;
55 case 5 : b5 = value;
break;
56 case 6 : b6 = value;
break;
57 case 7 : b7 = value;
break;
58 default:
throw std::out_of_range (
"bit index greater than 7");
62 Byte() : b0(0), b1(0), b2(0), b3(0), b4(0), b5(0), b6(0), b7(0) {}
66 const unsigned char *
const ptr = (
unsigned char*)
this;
72 unsigned char * ptr = (
unsigned char*)
this;
101 size_t get_num_bytes()
const
103 size_t div = current_size/8;
105 return (current_size % 8) != 0 ? div + 1 : div;
111 const size_t bit_index;
112 const size_t byte_index;
118 BitProxy(
BitArray & a,
const size_t i) :
119 index(i), bit_index(i % 8), byte_index(i/8), array(&a)
121 if (array->array_of_bytes.exist(byte_index))
122 byte_ptr = &array->array_of_bytes.access(byte_index);
127 operator int ()
const
129 if (index >= array->current_size)
130 throw std::out_of_range(
"Index out of range");
132 return byte_ptr != NULL ? byte_ptr->read_bit(bit_index) : 0;
135 BitProxy & operator = (
const size_t value)
139 if (byte_ptr == NULL)
140 byte_ptr = &array->array_of_bytes.touch(byte_index);
142 if (index >= array->current_size)
143 array->current_size = index;
145 byte_ptr->write_bit(bit_index, value);
150 BitProxy & operator = (
const BitProxy & proxy)
152 if (byte_ptr == NULL)
153 byte_ptr = &array->array_of_bytes.touch(byte_index);
155 if (index >= array->current_size)
156 array->current_size = index;
158 byte_ptr->write_bit(bit_index, proxy.byte_ptr->read_bit(proxy.bit_index));
172 BitArray(
const size_t dim = 0) throw(std::exception, std::bad_alloc)
173 : current_size(dim), array_of_bytes(get_num_bytes())
192 int num_bytes = dim/8;
196 array_of_bytes.
reserve(num_bytes);
200 size_t size()
const {
return current_size; }
205 size_t array_size = sz / 8;
208 array_of_bytes.
cut(array_size);
212 BitProxy operator [] (
const size_t i)
const
214 return BitProxy(const_cast<BitArray&>(*
this), i);
217 BitProxy operator [] (
const size_t i) {
return BitProxy(*
this, i); }
227 if (i >= current_size)
228 throw std::out_of_range(
"index out of range");
230 const int bit_index = i % 8;
231 const Byte byte = array_of_bytes[i/8];
232 return byte.read_bit(bit_index);
242 void write_bit(
const size_t i,
const unsigned int value)
244 array_of_bytes.
touch(i/8).write_bit(i%8, value);
245 if (i >= current_size)
246 current_size = i + 1;
259 if (i >= current_size)
260 throw std::out_of_range(
"index out of range");
262 const int bit_index = i % 8;
263 return array_of_bytes.
access(i/8).read_bit(bit_index);
273 void write(
const size_t i,
const unsigned int value)
275 array_of_bytes.
access(i/8).write_bit(i%8, value);
276 if (i >= current_size)
277 current_size = i + 1;
281 void push(
const unsigned int value)
290 array_of_bytes.
cut(get_num_bytes());
297 array_of_bytes.
cut();
309 : current_size(array.current_size), array_of_bytes (array.array_of_bytes)
316 std::swap(current_size, array.current_size);
317 array_of_bytes.
swap(array.array_of_bytes);
329 array_of_bytes.
cut();
338 for (
size_t i = 0; i < current_size; ++i)
355 throw(std::exception, std::bad_alloc)
360 current_size = array.current_size;
361 array_of_bytes = array.array_of_bytes;
380 const size_t & num_bytes = array_of_bytes.
size();
382 output << num_bytes <<
" " << current_size << endl;
384 for (
size_t i = 0; i < num_bytes; i++)
385 if (array_of_bytes.
exist(i))
387 int byte = array_of_bytes.
access(i).get_int();
388 output << byte <<
" ";
408 array_of_bytes.
cut();
410 size_t num_bytes = 0;
411 input >> num_bytes >> current_size;
412 for (
size_t i = 0; i < num_bytes; i++)
416 array_of_bytes.
touch(i).set_int(c);;
444 const size_t & num_bytes = array_of_bytes.
size();
446 output <<
"// " << current_size <<
" bits declaration" << endl
447 <<
"const unsigned char " << name <<
" [" << num_bytes <<
"] = {"
449 for (
size_t i = 0; i < num_bytes; i++)
451 if (array_of_bytes.
exist(i))
453 int byte = array_of_bytes.
access(i).get_int();
459 if (i != num_bytes - 1)
463 output << endl <<
" ";
465 output << endl <<
"};" << endl << endl;
480 const size_t num_bits)
482 array_of_bytes.
cut();
484 size_t num_bytes = num_bits/8;
486 if (num_bits % 8 != 0)
489 for (
size_t i = 0; i < num_bytes; i++)
490 array_of_bytes.
touch(i).set_int(str[i]);
492 current_size = num_bits;
504 const size_t real_n = std::min <size_t>(n, current_size);
506 for (
size_t i = 0; i < current_size - real_n; ++i)
509 for (
size_t i = current_size - real_n; i < current_size; ++i)
522 const size_t real_n = std::min <size_t>(n, current_size);
524 for (
size_t i = current_size; i > real_n; --i)
527 for (
size_t i = real_n; i > 0; --i)
540 for (
size_t i = 0; i < n; ++i)
553 if (n >= current_size)
562 for (
size_t i = 0; i < current_size - n; ++i)
577 const size_t real_n = n % current_size;
584 for (
size_t i = 0; i < real_n; ++i)
587 for (
size_t i = 0; i < current_size - real_n; ++i)
590 for (
size_t i = 0; i < real_n; ++i)
603 const size_t real_n = n % current_size;
610 for (
size_t i = current_size - real_n; i < current_size; ++i)
613 for (
size_t i = current_size - 1; i >= real_n; --i)
616 for (
size_t i = 0; i < real_n; ++i)
620 template <
typename T>
624 const size_t num_bits =
sizeof(T) * 8;
626 for (
int i = 0; i < num_bits; ++i)
633 void set_num(
const char & c)
638 void set_num(
const short & c)
643 void set_num(
const int & c)
648 void set_num(
const long & c)
656 for (
int i = 0; i < current_size; ++i)
657 ret_val +=
read_bit(current_size - i - 1) << i;
661 void set_bit_str(
const std::string & str)
664 const size_t & str_size = str.size();
668 for (
size_t i = 0; i < str_size; ++i)
671 I(c ==
'1' or c ==
'0');
676 std::string get_bit_str()
const
679 for (
size_t i = 0; i < current_size; ++i)
680 ret_val.append(
read_bit(i) == 0 ?
"0" :
"1");
685 friend ostream & operator << (ostream & out,
const BitArray & array)
687 for (
size_t i = 0; i < array.current_size; ++i)
694 BitArray(
const unsigned char str[],
const size_t num_bits)
void write_bit(const size_t i, const unsigned int value)
Definition: bitArray.H:242
void reserve(const size_t l, const size_t r)
Definition: tpl_dynArray.H:960
BitArray(const BitArray &array)
Definition: bitArray.H:308
void save(ofstream &output)
Definition: bitArray.H:378
BitArray(ifstream &input)
Definition: bitArray.H:422
void cut(const size_t new_dim=0)
Definition: tpl_dynArray.H:1040
void reserve(size_t dim)
Definition: bitArray.H:188
size_t size() const
Retorna dimensión actual (más lejano índice escrito)
Definition: tpl_dynArray.H:523
Definition: bitArray.H:96
BitArray(const size_t dim=0)
Definition: bitArray.H:172
void dyn_left_shift(const size_t n=1)
Definition: bitArray.H:538
T & touch(const size_t i)
Definition: tpl_dynArray.H:915
void load(ifstream &input)
Definition: bitArray.H:406
void set_default_initial_value(const T &value)
Definition: tpl_dynArray.H:541
size_t size() const
Retorna la dimensión del arreglo de bits.
Definition: bitArray.H:200
void save_in_array_of_chars(const string &name, ofstream &output)
Definition: bitArray.H:442
bool exist(const size_t i) const
Definition: tpl_dynArray.H:854
void circular_right_shift(const size_t n=1)
Definition: bitArray.H:601
DynList< char > bits_list()
Lo convierte a una lista.
Definition: bitArray.H:335
BitArray(const unsigned char str[], const size_t num_bits)
Definition: bitArray.H:694
void dyn_right_shift(const size_t n=1)
Definition: bitArray.H:551
void push(const unsigned int value)
Inserta al final del arreglo el valor value.
Definition: bitArray.H:281
void left_shift(const size_t n=1)
Definition: bitArray.H:502
int read_bit(const size_t i) const
Definition: bitArray.H:225
void load_from_array_of_chars(const unsigned char str[], const size_t num_bits)
Definition: bitArray.H:479
void right_shift(const size_t n=1)
Definition: bitArray.H:520
void write(const size_t i, const unsigned int value)
Definition: bitArray.H:273
void pop()
Elimina el último bit del arreglo.
Definition: bitArray.H:287
int read(const size_t i) const
Definition: bitArray.H:257
void set_size(const size_t sz)
Reajusta la dimensión del arreglo.
Definition: bitArray.H:203
void empty()
Elimina todos los bits insertados.
Definition: bitArray.H:294
void circular_left_shift(const size_t n=1)
Definition: bitArray.H:575
T & access(const size_t i)
Definition: tpl_dynArray.H:793
T & append(const T &item)
Inserta un item como último elemento.
Definition: htlist.H:685
void swap(DynArray< T > &array)
Definition: tpl_dynArray.H:725
Definition: bitArray.H:12