-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitArray.H
911 lines (717 loc) · 22.7 KB
/
bitArray.H
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
/* Aleph-w
/ \ | | ___ _ __ | |__ __ __
/ _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
/ ___ \| | __/ |_) | | | |_____\ V V / version 1.9c
/_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
|_|
This file is part of Aleph-w library
Copyright (c) 2002-2018 Leandro Rabindranath Leon
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
# ifndef BITARRAY_H
# define BITARRAY_H
# include <iostream>
# include <fstream>
# include <aleph.H>
# include <tpl_dynArray.H>
namespace Aleph {
class Byte
{
unsigned int b0 : 1;
unsigned int b1 : 1;
unsigned int b2 : 1;
unsigned int b3 : 1;
unsigned int b4 : 1;
unsigned int b5 : 1;
unsigned int b6 : 1;
unsigned int b7 : 1;
public:
unsigned int read_bit(const unsigned int i) const noexcept
{
assert(i < 8);
switch (i)
{
case 0 : return b0;
case 1 : return b1;
case 2 : return b2;
case 3 : return b3;
case 4 : return b4;
case 5 : return b5;
case 6 : return b6;
case 7 : return b7;
default: assert(false);
}
return 0; // never reached
}
void write_bit(const unsigned int i, const unsigned int value) noexcept
{
assert(i < 8);
assert(value <= 1);
switch (i)
{
case 0 : b0 = value; break;
case 1 : b1 = value; break;
case 2 : b2 = value; break;
case 3 : b3 = value; break;
case 4 : b4 = value; break;
case 5 : b5 = value; break;
case 6 : b6 = value; break;
case 7 : b7 = value; break;
default: assert(false);
}
}
Byte() noexcept : b0(0), b1(0), b2(0), b3(0), b4(0), b5(0), b6(0), b7(0) {}
int get_int() const noexcept
{
const unsigned char * const ptr = (unsigned char*) this;
return *ptr;
}
void set_int(int i) noexcept
{
unsigned char * ptr = (unsigned char*) this;
*ptr = i;
}
Byte & operator |= (const Byte & rhs)
{
(unsigned char&) *this |= (unsigned char&) rhs;
return *this;
}
Byte & operator &= (const Byte & rhs)
{
(unsigned char&) *this &= (unsigned char&) rhs;
return *this;
}
};
/** Arreglo contiguo de bits.
BitArray define una arreglo contiguo de bits. El consumo de
memoria es proporcional a la n/8; donde n es la cantidad de bits
que se desean manejar.
El arreglo es dinámico, lo que implica que éste puede expandirse
dinámicamente según la necesidad.
El acceso al i-ésimo bit puede realizarse mediante el operador
[]. Sin embargo, puesto que esta clase de acceso está
implementada mediante una clase proxy, el acceso puede fallar en
algunas expresiones donde el compilador no logre efectuar las
transformaciones de tipos adecuadas.
@ingroup Secuencias
@author Alejandro Mujica
@author Leandro Rabindranath Leon
*/
class BitArray
{
size_t current_size;
DynArray<Byte> array_of_bytes;
size_t get_num_bytes() const noexcept
{
size_t div = current_size/8;
return (current_size % 8) != 0 ? div + 1 : div;
}
class BitProxy
{
const size_t index;
const size_t bit_index;
const size_t byte_index;
BitArray * array;
Byte * byte_ptr;
public:
BitProxy(BitArray & a, const size_t i) noexcept
: index(i), bit_index(i % 8), byte_index(i/8), array(&a)
{
if (array->array_of_bytes.exist(byte_index))
byte_ptr = &array->array_of_bytes.access(byte_index);
else
byte_ptr = nullptr;
}
operator int () const
{
if (index >= array->current_size)
throw std::out_of_range("Index out of range");
return byte_ptr != nullptr ? byte_ptr->read_bit(bit_index) : 0;
}
BitProxy & operator = (const size_t value)
{
assert(value <= 1);
if (byte_ptr == nullptr)
byte_ptr = &array->array_of_bytes.touch(byte_index);
if (index >= array->current_size)
array->current_size = index;
byte_ptr->write_bit(bit_index, value);
return *this;
}
BitProxy & operator = (const BitProxy & proxy)
{
if (byte_ptr == nullptr)
byte_ptr = &array->array_of_bytes.touch(byte_index);
if (index >= array->current_size)
array->current_size = index;
byte_ptr->write_bit(bit_index, proxy.byte_ptr->read_bit(proxy.bit_index));
return *this;
}
};
public:
using Item_Type = unsigned char;
/** Constructor de arreglo de dim bits.
@param[in] dim cantidad de bits. Por omisión es 256.
@throw bad_alloc si no hay suficiente memoria para un bloque
continuo de dim/8 bytes.
*/
BitArray(const size_t dim = 0)
: current_size(dim), array_of_bytes(get_num_bytes())
{
array_of_bytes.set_default_initial_value(Byte());
}
/** Reserva por adelantado memoria para el arreglo de bits a la
dimensión dim.
reserve() aparta por adelantado la memoria suficiente para guardar
dim bits. De este modo, los accesos de escritura o lectura pueden
hacerse directa y más rápidamente mediante los métodos read() y
write().
@throw bad_alloc si no hay suficiente memoria
*/
void reserve(size_t dim)
{
current_size = dim;
int num_bytes = dim/8;
if (dim % 8)
++num_bytes;
array_of_bytes.reserve(num_bytes);
}
/// Retorna la dimensión del arreglo de bits
size_t size() const noexcept { return current_size; }
/// Reajusta la dimensión del arreglo
void set_size(const size_t sz)
{
size_t array_size = sz / 8;
if (array_size % 8)
++array_size;
array_of_bytes.adjust(array_size);
current_size = sz;
}
BitProxy operator [] (const size_t i) const noexcept
{
return BitProxy(const_cast<BitArray&>(*this), i);
}
BitProxy operator [] (const size_t i) noexcept { return BitProxy(*this, i); }
int read_bit_ne(const size_t i) const noexcept
{
const int bit_index = i % 8;
auto ptr = array_of_bytes.test(i/8);
if (ptr)
{
const Byte & byte = *ptr;
return byte.read_bit(bit_index);
}
else
return 0;
}
/** Lee el bit i.
@param[in] i índice del bit a ser leído.
@return el valor del bit i
@note No se verifica fuera de rango.
*/
int read_bit(const size_t i) const
{
if (i >= current_size)
throw std::out_of_range("index out of range");
const int bit_index = i % 8;
const Byte byte = array_of_bytes[i/8];
return byte.read_bit(bit_index);
}
int operator () (const size_t i) const { return read_bit(i); }
/** Escribe el bit i con el valor value
@param[in] i índice del bit a ser escrito.
@param[in] value valor a ser escrito en la posición i.
@note No se verifica fuera de rango y no verifica que value
tenga un valor binario.
*/
void write_bit(const size_t i, const unsigned int value)
{
array_of_bytes.touch(i/8).write_bit(i%8, value);
if (i >= current_size)
current_size = i + 1;
}
/** Lectura rápida del bit i.
Lee el bit i sin verificar memoria
@param[in] i índice del bit a ser leído.
@return el valor del bit i
@note No se verifica fuera de rango.
*/
int read(const size_t i) const
{
if (i >= current_size)
throw std::out_of_range("index out of range");
const int bit_index = i % 8;
return array_of_bytes.access(i/8).read_bit(bit_index);
}
/** Escribe el bit i con el valor value sin verificación de memoria.
@param[in] i índice del bit a ser escrito.
@param[in] value valor a ser escrito en la posición i.
@note No se verifica fuera de rango y no verifica que value
tenga un valor binario.
*/
void write(const size_t i, const unsigned int value)
{
array_of_bytes.access(i/8).write_bit(i%8, value);
if (i >= current_size)
current_size = i + 1;
}
int fast_read(const size_t i) const noexcept
{
return array_of_bytes.access(i/8).read_bit(i%8);
}
void fast_write(const size_t i, const unsigned int value)
{
array_of_bytes.access(i/8).write_bit(i%8, value);
}
/// Inserta al final del arreglo el valor value.
void push(const unsigned int value)
{
write_bit(current_size, value);
}
/// Elimina el último bit del arreglo.
void pop()
{
current_size--;
array_of_bytes.cut(get_num_bytes());
}
/// Elimina todos los bits insertados
void empty() noexcept
{
current_size = 0;
array_of_bytes.cut();
}
/** Constructor copia.
Instancia un arreglo de bits copia exacta de array.
@param[in] array el arreglo de bits a ser copiado.
@throw bad_alloc si no hay suficiente memoria para un bloque
continuo de dim/8 bytes.
*/
BitArray(const BitArray & array)
: current_size(array.current_size), array_of_bytes (array.array_of_bytes)
{
// empty
}
void swap(BitArray & array) noexcept
{
std::swap(current_size, array.current_size);
array_of_bytes.swap(array.array_of_bytes);
}
BitArray(BitArray && array) noexcept
: BitArray()
{
swap(array);
}
BitArray & operator = (BitArray && array) noexcept
{
current_size = 0;
array_of_bytes.cut();
swap(array);
return *this;
}
/// Lo convierte a una lista
DynList<char> bits_list()
{
DynList<char> ret_val;
for (size_t i = 0; i < current_size; ++i)
ret_val.append((char) read_bit(i));
return ret_val;
}
/** Asignación de arreglo de bits.
Libera toda la memoria de this, aparta nueva memoria para
albergar array y copia exactamente los bits de array a this.
@param[in] array el arreglo de bits a ser copiado.
@throw bad_alloc si no hay suficiente memoria para un bloque
continuo de dim/8 bytes.
@note Si ocurre esta excepción, el estado de this permanece el
mismo anterior a la asignación.
*/
BitArray & operator = (const BitArray & array)
{
if (this == &array)
return *this;
current_size = array.current_size;
array_of_bytes = array.array_of_bytes;
return *this;
}
/** Guarda en un archivo texto la secuencia de bits.
save() almacena en el archivo texto output, ya abierto, la secuencia
de bits en formato decimal.
La primera línea siempre guarda el tamaño en bytes y en
bits. Seguidamente se guardan en valor decimal los bytes secuenciales
correspondientes al arreglo.
@param[in] output stream de salida abierto donde se guardaría el
arreglo.
*/
void save(std::ostream & output)
{
const size_t & num_bytes = array_of_bytes.size();
output << num_bytes << " " << current_size << endl;
for (size_t i = 0; i < num_bytes; i++)
if (array_of_bytes.exist(i))
{
int byte = array_of_bytes.access(i).get_int();
output << byte << " ";
}
else
output << "0 ";
output << endl;
}
/** Carga desde un archivo un arreglo de bits.
load(input) toma el archivo abierto en input, previamente
guardado con el método load(), y restaura el arreglo al valro
guardado.
@param[in] input stream de entrada donde se encuentra el
arreglo y que debe haber sido previamente guardado con
save().
*/
void load(std::istream & input)
{
array_of_bytes.cut();
// TODO: bound num_bytes to a maximum according to available ram
size_t num_bytes = 0;
input >> num_bytes >> current_size;
for (size_t i = 0; i < num_bytes; i++)
{
int c;
input >> c;
array_of_bytes.touch(i).set_int(c);;
}
}
/// Construye un nuevo arreglo de bits a partir de un archivo
/// construido mediante el método save().
BitArray(std::ifstream & input)
{
load(input);
}
/** Guarda en un archivo texto una declaración estática de cadena de
carateres que representa el arreglo.
save_in_array_of_chars(name, output) genera una declaración del
tipo:
char name [ size ] = { valores del arreglo separados con comas };
La declaración es guardada en el stream de salida output y el
nombre del arreglo es name.
@param[in] name nombre de la variable arreglo.
@param[in] output stream de salida donde se desea guardar la
declaración.
*/
void save_in_array_of_chars(const string & name, std::ostream & output)
{
const size_t & num_bytes = array_of_bytes.size();
output << "// " << current_size << " bits declaration" << endl
<< "const unsigned char " << name << " [" << num_bytes << "] = {"
<< endl << " ";
for (size_t i = 0; i < num_bytes; i++)
{
if (array_of_bytes.exist(i))
{
int byte = array_of_bytes.access(i).get_int();
output << byte;
}
else
output << "0";
if (i != num_bytes - 1)
output << ", ";
if ((i+1) % 15 == 0)
output << endl << " ";
}
output << endl << "};" << endl << endl;
}
/** Lee un arreglo de bits guardado en un arreglo de caracteres.
load_from_array_of_chars(str, num_bits) toma un arreglo str,
previamente generado con save_in_array_of_chars, de num_bits de
largo, y lo carga.
@param[in] str el arreglo de caracteres donde se encuentra el de
bits.
@param[in] num_bits la cantidad de bits del arreglo. Note que no
es la misma que la cantidad de bytes.
*/
void load_from_array_of_chars(const unsigned char str[],
const size_t num_bits)
{
array_of_bytes.cut();
size_t num_bytes = num_bits/8;
if (num_bits % 8 != 0)
num_bytes++;
for (size_t i = 0; i < num_bytes; i++)
array_of_bytes.touch(i).set_int(str[i]);
current_size = num_bits;
}
/** Desplaza los bits n posiciones a la izquierda.
Los bits son desplazados y se considera al arreglo de tamaño fijo,
es decir, los n primeros bits se pierden.
@param[in] n la cantidad de posiciones que se van a desplazar los bits.
*/
void left_shift(const size_t n = 1) noexcept
{
const size_t real_n = std::min <size_t>(n, current_size);
for (size_t i = 0; i < current_size - real_n; ++i)
write_bit(i, read_bit(i + real_n));
for (size_t i = current_size - real_n; i < current_size; ++i)
write_bit(i, 0);
}
/** Desplaza los bits n posiciones a la derecha.
Los bits son desplazados y se considera al arreglo de tamaño fijo,
es decir, los últimos n bits se pierden.
@param[in] n la cantidad de posiciones que se van a desplazar los bits.
*/
void right_shift(const size_t n = 1) noexcept
{
const size_t real_n = std::min <size_t>(n, current_size);
for (size_t i = current_size; i > real_n; --i)
write_bit(i - 1, read_bit(i - real_n - 1));
for (size_t i = real_n; i > 0; --i)
write_bit(i - 1, 0);
}
/** Desplaza los bits n posiciones a la izquierda dinámicamente.
Los bits son desplazados sin perderse ninguno y el arreglo crece
agregando n ceros a la derecha.
@param[in] n la cantidad de posiciones que se van a desplazar los bits.
*/
void dyn_left_shift(const size_t n = 1)
{
for (size_t i = 0; i < n; ++i)
push(0);
}
/** Desplaza los bits n posiciones a la derecha dinámicamente.
Los bits son desplazados sin perderse ninguno y el arreglo decrece
eliminando los n ceros que quedarían a la izquierda
@param[in] n la cantidad de posiciones que se van a desplazar los bits.
*/
void dyn_right_shift(const size_t n = 1)
{
if (n >= current_size)
{
set_size(1);
array_of_bytes.set_default_initial_value(Byte());
return;
}
BitArray array(current_size - n);
for (size_t i = 0; i < current_size - n; ++i)
array.write_bit(i, read_bit(i));
*this = array;
}
/** Desplaza los bits n posiciones a la izquierda circularmente.
Los bits son desplazados sin perderse ninguno, los n primeros bits
pasan a ocupar las últimas n posiciones del arreglo.
@param[in] n la cantidad de posiciones que se van a desplazar los bits.
*/
void circular_left_shift(const size_t n = 1) noexcept
{
const size_t real_n = n % current_size;
if (real_n == 0)
return;
BitArray array(real_n);
for (size_t i = 0; i < real_n; ++i)
array.write_bit(i, read_bit(i));
for (size_t i = 0; i < current_size - real_n; ++i)
write_bit(i, read_bit(i + real_n));
for (size_t i = 0; i < real_n; ++i)
write_bit(current_size - real_n + i, array.read_bit(i));
}
/** Desplaza los bits n posiciones a la derecha circularmente.
Los bits son desplazados sin perderse ninguno, los últimos n bits
pasan a ocupar las primeras n posiciones del arreglo.
@param[in] n la cantidad de posiciones que se van a desplazar los bits.
*/
void circular_right_shift(const size_t n = 1) noexcept
{
const size_t real_n = n % current_size;
if (real_n == 0)
return;
BitArray array(real_n);
for (size_t i = current_size - real_n; i < current_size; ++i)
array.write_bit(i - (current_size - real_n), read_bit(i));
for (size_t i = current_size - 1; i >= real_n; --i)
write_bit(i, read_bit(i - real_n));
for (size_t i = 0; i < real_n; ++i)
write_bit(i, array.read_bit(i));
}
template <typename T>
void set_num(T n) // Paso copia porque es modificado adentro
{
empty();
const size_t num_bits = sizeof(T) * 8;
reserve(num_bits);
for (size_t i = 0; i < num_bits; ++i)
{
write_bit(current_size - i - 1, n & 1);
n >>= 1;
}
}
void set_num(const char & c)
{
set_num<char>(c);
}
void set_num(const short & c)
{
set_num<short>(c);
}
void set_num(const int & c)
{
set_num<int>(c);
}
void set_num(const long & c)
{
set_num<long>(c);
}
long get_num() noexcept
{
long ret_val = 0;
for (size_t i = 0; i < current_size; ++i)
ret_val += read_bit(current_size - i - 1) << i;
return ret_val;
}
void set_bit_str(const std::string & str)
{
empty();
const size_t & str_size = str.size();
reserve(str_size);
for (size_t i = 0; i < str_size; ++i)
{
char c = str[i];
assert(c == '1' or c == '0');
write_bit(i, (c == '0' ? 0 : 1));
}
}
std::string get_bit_str() const
{
std::string ret_val;
for (size_t i = 0; i < current_size; ++i)
ret_val.append(read_bit(i) == 0 ? "0" : "1");
return ret_val;
}
friend ostream & operator << (ostream & out, const BitArray & array)
{
for (size_t i = 0; i < array.current_size; ++i)
out << array.read_bit(i);
return out;
}
/// Construye un nuevo arreglo de bits a partir de un arreglo de
/// caracteres previamente generado con load_from_array_of_chars
BitArray(const unsigned char str[], const size_t num_bits)
{
load_from_array_of_chars(str, num_bits);
}
BitArray & operator |= (const BitArray & rhs)
{
auto n = std::min(array_of_bytes.size(), rhs.array_of_bytes.size());
for (size_t i = 0; i < n; ++i)
array_of_bytes(i) |= rhs.array_of_bytes(i);
if (size() < n)
{
auto i = array_of_bytes.size();
set_size(rhs.size());
for (; i < rhs.array_of_bytes.size(); ++i)
array_of_bytes(i) = rhs.array_of_bytes(i);
}
return *this;
}
BitArray & operator &= (const BitArray & rhs)
{
set_size(std::min(size(), rhs.size()));
for (size_t i = 0; i < size(); ++i)
array_of_bytes(i) &= rhs.array_of_bytes(i);
return *this;
}
friend BitArray operator | (const BitArray & op1, const BitArray & op2)
{
BitArray ret = op1;
ret |= op2;
return ret;
}
friend BitArray operator & (const BitArray & op1, const BitArray & op2)
{
BitArray ret = op1;
ret &= op2;
return ret;
}
private:
template <class Operation>
bool __traverse(Operation & operation)
{
for (size_t i = 0; i < current_size; ++i)
if (not operation(read_bit(i)))
return false;
return true;
}
public:
class Iterator
{
BitArray * array_ptr = nullptr;
long curr_idx = 0;
public:
Iterator() noexcept { /* empty */ }
Iterator(const BitArray & array) noexcept
: array_ptr(const_cast<BitArray*>(&array)), curr_idx(0)
{
// empty
}
bool has_curr() const noexcept
{
return curr_idx >= 0 and curr_idx < array_ptr->size();
}
unsigned int get_curr_ne() const noexcept
{
return array_ptr->read_bit(curr_idx);
}
unsigned int get_curr() const
{
if (not has_curr())
throw std::overflow_error("Iterator is at the end of the list");
return array_ptr->read_bit(curr_idx);
}
long get_pos() const noexcept { return curr_idx; }
void next_ne() noexcept { ++curr_idx; }
void next()
{
if (curr_idx == array_ptr->size())
throw std::overflow_error("not current item in iterator");
next_ne();
}
void prev_ne() noexcept { --curr_idx; }
void prev()
{
if (curr_idx == -1)
throw std::underflow_error("not current item in iterator");
prev_ne();
}
void reset_last() noexcept { curr_idx = array_ptr->size() - 1; }
void end() noexcept { curr_idx = array_ptr->size(); }
void reset_first() noexcept { curr_idx = 0; }
void reset() noexcept { reset_first(); }
};
template <class Operation>
bool traverse(Operation & operation) const
{
return const_cast<BitArray&>(*this).__traverse(operation);
}
template <class Operation>
bool traverse(Operation & operation)
{
return __traverse(operation);
}
template <class Operation>
bool traverse(Operation && operation = Operation()) const
{
return traverse<Operation>(operation);
}
template <class Operation>
bool traverse(Operation && operation = Operation())
{
return traverse<Operation>(operation);
}
Functional_Methods(unsigned short);
Generic_Items(unsigned char);
STL_ALEPH_ITERATOR(BitArray);
};
} // end namespace Aleph
# endif /* BITARRAY_H */