34 # include <tpl_dynArray.H> 51 unsigned int read_bit(
const unsigned int i)
const noexcept
65 default: assert(
false);
70 void write_bit(
const unsigned int i,
const unsigned int value) noexcept
77 case 0 : b0 = value;
break;
78 case 1 : b1 = value;
break;
79 case 2 : b2 = value;
break;
80 case 3 : b3 = value;
break;
81 case 4 : b4 = value;
break;
82 case 5 : b5 = value;
break;
83 case 6 : b6 = value;
break;
84 case 7 : b7 = value;
break;
85 default: assert(
false);
89 Byte() noexcept : b0(0), b1(0), b2(0), b3(0), b4(0), b5(0), b6(0), b7(0) {}
91 int get_int() const noexcept
93 const unsigned char *
const ptr = (
unsigned char*)
this;
97 void set_int(
int i) noexcept
99 unsigned char * ptr = (
unsigned char*)
this;
103 Byte & operator |= (
const Byte & rhs)
105 (
unsigned char&) *
this |= (
unsigned char&) rhs;
109 Byte & operator &= (
const Byte & rhs)
111 (
unsigned char&) *
this &= (
unsigned char&) rhs;
140 size_t get_num_bytes()
const noexcept
142 size_t div = current_size/8;
144 return (current_size % 8) != 0 ? div + 1 : div;
150 const size_t bit_index;
151 const size_t byte_index;
157 BitProxy(
BitArray & a,
const size_t i) noexcept
158 : index(i), bit_index(i % 8), byte_index(i/8), array(&a)
160 if (array->array_of_bytes.
exist(byte_index))
161 byte_ptr = &array->array_of_bytes.
access(byte_index);
166 operator int ()
const 168 if (index >= array->current_size)
169 throw std::out_of_range(
"Index out of range");
171 return byte_ptr !=
nullptr ? byte_ptr->read_bit(bit_index) : 0;
174 BitProxy & operator = (
const size_t value)
178 if (byte_ptr ==
nullptr)
179 byte_ptr = &array->array_of_bytes.
touch(byte_index);
181 if (index >= array->current_size)
182 array->current_size = index;
184 byte_ptr->write_bit(bit_index, value);
189 BitProxy & operator = (
const BitProxy & proxy)
191 if (byte_ptr ==
nullptr)
192 byte_ptr = &array->array_of_bytes.
touch(byte_index);
194 if (index >= array->current_size)
195 array->current_size = index;
197 byte_ptr->write_bit(bit_index, proxy.byte_ptr->read_bit(proxy.bit_index));
205 using Item_Type =
unsigned char;
214 : current_size(dim), array_of_bytes(get_num_bytes())
233 int num_bytes = dim/8;
237 array_of_bytes.
reserve(num_bytes);
241 size_t size() const noexcept {
return current_size; }
246 size_t array_size = sz / 8;
250 array_of_bytes.
adjust(array_size);
254 BitProxy operator [] (
const size_t i)
const noexcept
256 return BitProxy(const_cast<BitArray&>(*
this), i);
259 BitProxy operator [] (
const size_t i) noexcept {
return BitProxy(*
this, i); }
261 int read_bit_ne(
const size_t i)
const noexcept
263 const int bit_index = i % 8;
264 auto ptr = array_of_bytes.
test(i/8);
267 const Byte & byte = *ptr;
268 return byte.read_bit(bit_index);
282 if (i >= current_size)
283 throw std::out_of_range(
"index out of range");
284 const int bit_index = i % 8;
285 const Byte byte = array_of_bytes[i/8];
286 return byte.read_bit(bit_index);
289 int operator () (
const size_t i)
const {
return read_bit(i); }
298 void write_bit(
const size_t i,
const unsigned int value)
300 array_of_bytes.
touch(i/8).write_bit(i%8, value);
301 if (i >= current_size)
302 current_size = i + 1;
315 if (i >= current_size)
316 throw std::out_of_range(
"index out of range");
318 const int bit_index = i % 8;
319 return array_of_bytes.
access(i/8).read_bit(bit_index);
329 void write(
const size_t i,
const unsigned int value)
331 array_of_bytes.
access(i/8).write_bit(i%8, value);
332 if (i >= current_size)
333 current_size = i + 1;
336 int fast_read(
const size_t i)
const noexcept
338 return array_of_bytes.
access(i/8).read_bit(i%8);
341 void fast_write(
const size_t i,
const unsigned int value)
343 array_of_bytes.
access(i/8).write_bit(i%8, value);
347 void push(
const unsigned int value)
349 write_bit(current_size, value);
356 array_of_bytes.
cut(get_num_bytes());
363 array_of_bytes.
cut();
375 : current_size(array.current_size), array_of_bytes (array.array_of_bytes)
380 void swap(
BitArray & array) noexcept
382 std::swap(current_size, array.current_size);
383 array_of_bytes.
swap(array.array_of_bytes);
395 array_of_bytes.
cut();
404 for (
size_t i = 0; i < current_size; ++i)
405 ret_val.
append((
char) read_bit(i));
425 current_size = array.current_size;
426 array_of_bytes = array.array_of_bytes;
443 void save(std::ostream & output)
445 const size_t & num_bytes = array_of_bytes.
size();
447 output << num_bytes <<
" " << current_size << endl;
449 for (
size_t i = 0; i < num_bytes; i++)
450 if (array_of_bytes.
exist(i))
452 int byte = array_of_bytes.
access(i).get_int();
453 output << byte <<
" ";
471 void load(std::istream & input)
473 array_of_bytes.
cut();
476 size_t num_bytes = 0;
477 input >> num_bytes >> current_size;
478 for (
size_t i = 0; i < num_bytes; i++)
482 array_of_bytes.
touch(i).set_int(c);;
510 const size_t & num_bytes = array_of_bytes.
size();
512 output <<
"// " << current_size <<
" bits declaration" << endl
513 <<
"const unsigned char " << name <<
" [" << num_bytes <<
"] = {" 515 for (
size_t i = 0; i < num_bytes; i++)
517 if (array_of_bytes.
exist(i))
519 int byte = array_of_bytes.
access(i).get_int();
525 if (i != num_bytes - 1)
529 output << endl <<
" ";
531 output << endl <<
"};" << endl << endl;
546 const size_t num_bits)
548 array_of_bytes.
cut();
550 size_t num_bytes = num_bits/8;
552 if (num_bits % 8 != 0)
555 for (
size_t i = 0; i < num_bytes; i++)
556 array_of_bytes.
touch(i).set_int(str[i]);
558 current_size = num_bits;
570 const size_t real_n = std::min <size_t>(n, current_size);
572 for (
size_t i = 0; i < current_size - real_n; ++i)
573 write_bit(i, read_bit(i + real_n));
575 for (
size_t i = current_size - real_n; i < current_size; ++i)
588 const size_t real_n = std::min <size_t>(n, current_size);
590 for (
size_t i = current_size; i > real_n; --i)
591 write_bit(i - 1, read_bit(i - real_n - 1));
593 for (
size_t i = real_n; i > 0; --i)
606 for (
size_t i = 0; i < n; ++i)
619 if (n >= current_size)
628 for (
size_t i = 0; i < current_size - n; ++i)
643 const size_t real_n = n % current_size;
650 for (
size_t i = 0; i < real_n; ++i)
653 for (
size_t i = 0; i < current_size - real_n; ++i)
654 write_bit(i, read_bit(i + real_n));
656 for (
size_t i = 0; i < real_n; ++i)
657 write_bit(current_size - real_n + i, array.
read_bit(i));
669 const size_t real_n = n % current_size;
676 for (
size_t i = current_size - real_n; i < current_size; ++i)
677 array.
write_bit(i - (current_size - real_n), read_bit(i));
679 for (
size_t i = current_size - 1; i >= real_n; --i)
680 write_bit(i, read_bit(i - real_n));
682 for (
size_t i = 0; i < real_n; ++i)
686 template <
typename T>
690 const size_t num_bits =
sizeof(T) * 8;
692 for (
size_t i = 0; i < num_bits; ++i)
694 write_bit(current_size - i - 1, n & 1);
699 void set_num(
const char & c)
704 void set_num(
const short & c)
709 void set_num(
const int & c)
714 void set_num(
const long & c)
719 long get_num() noexcept
722 for (
size_t i = 0; i < current_size; ++i)
723 ret_val += read_bit(current_size - i - 1) << i;
727 void set_bit_str(
const std::string & str)
730 const size_t & str_size = str.size();
734 for (
size_t i = 0; i < str_size; ++i)
737 assert(c ==
'1' or c ==
'0');
738 write_bit(i, (c ==
'0' ? 0 : 1));
742 std::string get_bit_str()
const 745 for (
size_t i = 0; i < current_size; ++i)
746 ret_val.append(read_bit(i) == 0 ?
"0" :
"1");
751 friend ostream & operator << (ostream & out,
const BitArray & array)
753 for (
size_t i = 0; i < array.current_size; ++i)
760 BitArray(
const unsigned char str[],
const size_t num_bits)
762 load_from_array_of_chars(str, num_bits);
767 auto n = std::min(array_of_bytes.
size(), rhs.array_of_bytes.
size());
768 for (
size_t i = 0; i < n; ++i)
769 array_of_bytes(i) |= rhs.array_of_bytes(i);
773 auto i = array_of_bytes.
size();
774 set_size(rhs.
size());
775 for (; i < rhs.array_of_bytes.
size(); ++i)
776 array_of_bytes(i) = rhs.array_of_bytes(i);
784 set_size(std::min(size(), rhs.
size()));
785 for (
size_t i = 0; i < size(); ++i)
786 array_of_bytes(i) &= rhs.array_of_bytes(i);
807 template <
class Operation>
808 bool __traverse(Operation & operation) noexcept(noexcept(operation))
810 for (
size_t i = 0; i < current_size; ++i)
811 if (not operation(read_bit_ne(i)))
825 Iterator() noexcept { }
827 Iterator(
const BitArray & array) noexcept
828 : array_ptr(const_cast<BitArray*>(&array)), curr_idx(0)
833 bool has_curr()
const noexcept
835 return curr_idx >= 0 and curr_idx < array_ptr->
size();
838 unsigned int get_curr_ne()
const noexcept
840 return array_ptr->
read_bit(curr_idx);
843 unsigned int get_curr()
const 846 throw std::overflow_error(
"Iterator is at the end of the list");
847 return array_ptr->
read_bit(curr_idx);
850 long get_pos()
const noexcept {
return curr_idx; }
852 void next_ne() noexcept { ++curr_idx; }
856 if (curr_idx == array_ptr->
size())
857 throw std::overflow_error(
"not current item in iterator");
861 void prev_ne() noexcept { --curr_idx; }
865 throw std::underflow_error(
"not current item in iterator");
869 void reset_last() noexcept { curr_idx = array_ptr->
size() - 1; }
871 void end() noexcept { curr_idx = array_ptr->
size(); }
873 void reset_first() noexcept { curr_idx = 0; }
875 void reset() noexcept { reset_first(); }
878 template <
class Operation>
879 bool traverse(Operation & operation)
const noexcept(noexcept(operation))
881 return const_cast<BitArray&
>(*this).__traverse(operation);
884 template <
class Operation>
885 bool traverse(Operation & operation) noexcept(noexcept(operation))
887 return __traverse(operation);
890 template <
class Operation>
891 bool traverse(Operation && operation = Operation())
const 892 noexcept(noexcept(operation))
894 return traverse<Operation>(operation);
897 template <
class Operation>
898 bool traverse(Operation && operation = Operation())
899 noexcept(noexcept(operation))
901 return traverse<Operation>(operation);
904 Functional_Methods(
unsigned short);
906 Generic_Items(
unsigned char);
void write_bit(const size_t i, const unsigned int value)
Definition: bitArray.H:298
int read_bit(const size_t i) const
Definition: bitArray.H:280
void reserve(const size_t l, const size_t r)
Definition: tpl_dynArray.H:998
BitArray(const BitArray &array)
Definition: bitArray.H:374
void empty() noexcept
Elimina todos los bits insertados.
Definition: bitArray.H:360
int read(const size_t i) const
Definition: bitArray.H:313
void cut(const size_t new_dim=0)
Definition: tpl_dynArray.H:1104
void reserve(size_t dim)
Definition: bitArray.H:229
Definition: bitArray.H:135
T & access(const size_t i) const noexcept
Definition: tpl_dynArray.H:874
BitArray(const size_t dim=0)
Definition: bitArray.H:213
void dyn_left_shift(const size_t n=1)
Definition: bitArray.H:604
T & touch(const size_t i)
Definition: tpl_dynArray.H:958
size_t size() const noexcept
Retorna la dimensión del arreglo de bits.
Definition: bitArray.H:241
void swap(DynArray< T > &array) noexcept
Definition: tpl_dynArray.H:819
bool exist(const size_t i) const
Definition: tpl_dynArray.H:895
DynList< char > bits_list()
Lo convierte a una lista.
Definition: bitArray.H:401
BitArray(const unsigned char str[], const size_t num_bits)
Definition: bitArray.H:760
void load(std::istream &input)
Definition: bitArray.H:471
void dyn_right_shift(const size_t n=1)
Definition: bitArray.H:617
void circular_left_shift(const size_t n=1) noexcept
Definition: bitArray.H:641
void push(const unsigned int value)
Inserta al final del arreglo el valor value.
Definition: bitArray.H:347
T * test(const size_t i) const noexcept
Definition: tpl_dynArray.H:927
void adjust(const size_t dim)
Definition: tpl_dynArray.H:1119
void load_from_array_of_chars(const unsigned char str[], const size_t num_bits)
Definition: bitArray.H:545
void write(const size_t i, const unsigned int value)
Definition: bitArray.H:329
void circular_right_shift(const size_t n=1) noexcept
Definition: bitArray.H:667
void pop()
Elimina el último bit del arreglo.
Definition: bitArray.H:353
void save(std::ostream &output)
Definition: bitArray.H:443
void set_size(const size_t sz)
Reajusta la dimensión del arreglo.
Definition: bitArray.H:244
void save_in_array_of_chars(const string &name, std::ostream &output)
Definition: bitArray.H:508
size_t size() const noexcept
Definition: tpl_dynArray.H:641
void set_default_initial_value(const T &value) noexcept
Definition: tpl_dynArray.H:659
T & append(const T &item)
Definition: htlist.H:1471
void left_shift(const size_t n=1) noexcept
Definition: bitArray.H:568
BitArray(std::ifstream &input)
Definition: bitArray.H:488
void right_shift(const size_t n=1) noexcept
Definition: bitArray.H:586