Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
bitArray.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31
97# ifndef BITARRAY_H
98# define BITARRAY_H
99
100# include <atomic>
101# include <bit>
102# include <cstdint>
103# include <iostream>
104# include <fstream>
105# include <algorithm>
106# include <type_traits>
107# include <aleph.H>
108# include <tpl_dynArray.H>
109# include <ah-errors.H>
110# include <ahDry.H>
111# include <ah-dry-mixin.H>
112
113namespace Aleph
114{
115 class Byte
116 {
117 std::uint8_t value = 0;
118
119 public:
120 [[nodiscard]] unsigned int read_bit(const unsigned int i) const noexcept
121 {
122 assert(i < 8);
123 return (value >> i) & 0x1;
124 }
125
126 void write_bit(const unsigned int i, const unsigned int val) noexcept
127 {
128 assert(i < 8);
129 assert(val <= 1);
130 const std::uint8_t mask = static_cast<std::uint8_t>(1) << i;
131#ifdef __cpp_lib_atomic_ref
132 std::atomic_ref<std::uint8_t> atm_val(this->value);
133 if (val)
134 atm_val.fetch_or(mask, std::memory_order_relaxed);
135 else
136 atm_val.fetch_and(static_cast<std::uint8_t>(~mask), std::memory_order_relaxed);
137#else
138 // Fallback for compilers without std::atomic_ref (e.g., Ubuntu 22.04)
139 if (val)
140 __atomic_fetch_or(&this->value, mask, __ATOMIC_RELAXED);
141 else
142 __atomic_fetch_and(&this->value, static_cast<std::uint8_t>(~mask), __ATOMIC_RELAXED);
143#endif
144 }
145
146 Byte() noexcept = default;
147
148 [[nodiscard]] int get_int() const noexcept
149 {
150 return static_cast<int>(value);
151 }
152
153 void set_int(int i) noexcept
154 {
155 value = static_cast<std::uint8_t>(i);
156 }
157
158 Byte &operator|=(const Byte & rhs) noexcept
159 {
160 value |= rhs.value;
161 return *this;
162 }
163
164 Byte &operator&=(const Byte & rhs) noexcept
165 {
166 value &= rhs.value;
167 return *this;
168 }
169
170 [[nodiscard]] int count_ones() const noexcept
171 {
172 return std::popcount(value);
173 }
174
175 [[nodiscard]] int count_zeros() const noexcept
176 {
177 return 8 - count_ones();
178 }
179 };
180
200 class BitArray : public FunctionalMixin<BitArray, unsigned int>
201 {
204
206 {
207 if (current_size == 0)
208 return;
209
210 const size_t used_bits_in_last_byte = current_size % 8;
211 if (used_bits_in_last_byte == 0)
212 return;
213
214 const size_t last_byte_index = current_size / 8;
215 Byte *last_byte = array_of_bytes.test(last_byte_index);
216 if (last_byte == nullptr)
217 return;
218
219 const int mask = (1u << used_bits_in_last_byte) - 1u;
220 last_byte->set_int(last_byte->get_int() & mask);
221 }
222
223 void ensure_num_bytes(const size_t num_bytes)
224 {
225 if (num_bytes == 0)
226 {
227 array_of_bytes.cut();
228 return;
229 }
230
231 if (array_of_bytes.size() < num_bytes)
232 array_of_bytes.touch(num_bytes - 1);
233 }
234
235 size_t get_num_bytes() const noexcept
236 {
237 const size_t div = current_size / 8;
238
239 return (current_size % 8) != 0 ? div + 1 : div;
240 }
241
243 {
244 const size_t index;
245 const size_t bit_index;
246 const size_t byte_index;
249
250 public:
251 BitProxy(BitArray & a, const size_t i) noexcept
252 : index(i), bit_index(i % 8), byte_index(i / 8), array(&a)
253 {
254 if (array->array_of_bytes.exist(byte_index))
256 else
257 byte_ptr = nullptr;
258 }
259
260 operator int() const
261 {
262 ah_out_of_range_error_if(index >= array->current_size) << "Index out of range";
263
264 return byte_ptr != nullptr ? byte_ptr->read_bit(bit_index) : 0;
265 }
266
267 BitProxy &operator=(const size_t value)
268 {
269 assert(value <= 1);
270
271 if (byte_ptr == nullptr)
273
274 const bool grew = index >= array->current_size;
275 if (grew)
276 array->current_size = index + 1;
277
279
280 if (grew)
282
283 return *this;
284 }
285
287 {
288 if (byte_ptr == nullptr)
290
291 const bool grew = index >= array->current_size;
292 if (grew)
293 array->current_size = index + 1;
294
295 const int rhs_value = proxy;
296 byte_ptr->write_bit(bit_index, rhs_value);
297
298 if (grew)
300
301 return *this;
302 }
303 };
304
305 public:
307 using Item_Type = unsigned int;
308
315 BitArray(const size_t dim = 0)
317 {
318 array_of_bytes.set_default_initial_value(Byte());
319 }
320
322 BitArray(const size_t dim, const unsigned int value)
323 : BitArray(dim)
324 {
325 assert(value <= 1);
326 for (size_t i = 0; i < dim; ++i)
327 write_bit(i, value);
328 }
329
344 void reserve(const size_t dim)
345 {
346 set_size(dim);
347 }
348
350 [[nodiscard]] constexpr size_t size() const noexcept { return current_size; }
351
362 void set_size(const size_t sz)
363 {
364 const size_t old_size = current_size;
365 // Without the sum sz + 7, which wraps for the last seven sizes of size_t
366 const size_t array_size = sz / 8 + (sz % 8 != 0 ? 1 : 0);
367
368 // Allocate every byte, for the accesses without checks. adjust() does
369 // not do it: it allocates only when the dimension grows, and the bytes
370 // below the current one may not exist. reserve() goes first: it is
371 // the step that may fail, and then it leaves the bytes as they were
372 if (array_size > 0)
373 array_of_bytes.reserve(array_size);
374 array_of_bytes.cut(array_size);
375 current_size = sz;
376
377 if (sz > old_size && old_size != 0)
378 if (const size_t rem = old_size % 8; rem != 0)
379 {
380 const size_t clear_end = std::min(sz, old_size + (8 - rem));
381 for (size_t i = old_size; i < clear_end; ++i)
382 write_bit(i, 0);
383 }
384
386 }
387
388 int operator[](const size_t i) const { return read_bit(i); }
389
390 BitProxy operator[](const size_t i) noexcept { return BitProxy(*this, i); }
391
392 int read_bit_ne(const size_t i) const noexcept
393 {
394 const unsigned int bit_index = static_cast<unsigned int>(i % 8);
395 if (const auto ptr = array_of_bytes.test(i / 8))
396 {
397 const Byte & byte = *ptr;
398 return byte.read_bit(bit_index);
399 }
400 return 0;
401 }
402
409 int read_bit(const size_t i) const
410 {
411 ah_out_of_range_error_if(i >= current_size) << "index out of range";
412 return read_bit_ne(i);
413 }
414
415 int operator()(const size_t i) const { return read_bit(i); }
416
424 void write_bit(const size_t i, const unsigned int value)
425 {
426 ah_out_of_range_error_if(value > 1) << "BitArray::write_bit: value must be 0 or 1";
427 array_of_bytes.touch(i / 8).write_bit(i % 8, value);
428 if (i >= current_size)
429 {
430 current_size = i + 1;
432 }
433 }
434
445 int read(const size_t i) const
446 {
447 ah_out_of_range_error_if(i >= current_size) << "index out of range";
448
449 const int bit_index = i % 8;
450 return array_of_bytes.access(i / 8).read_bit(bit_index);
451 }
452
462 void write(const size_t i, const unsigned int value)
463 {
464 ah_out_of_range_error_if(value > 1) << "BitArray::write: value must be 0 or 1";
465
466 array_of_bytes.access(i / 8).write_bit(i % 8, value);
467 if (i >= current_size)
468 {
469 current_size = i + 1;
471 }
472 }
473
482 int fast_read(const size_t i) const noexcept
483 {
484 assert(i < current_size);
485 return array_of_bytes.access(i / 8).read_bit(i % 8);
486 }
487
498 void fast_write(const size_t i, const unsigned int value)
499 {
500 ah_out_of_range_error_if(value > 1) << "BitArray::fast_write: value must be 0 or 1";
501 assert(i < current_size);
502 array_of_bytes.access(i / 8).write_bit(i % 8, value);
503 }
504
506 void push(const unsigned int value)
507 {
509 }
510
512 void pop()
513 {
514 ah_underflow_error_if(current_size == 0) << "BitArray::pop on empty array";
515 current_size--;
518 }
519
521 void empty() noexcept
522 {
523 current_size = 0;
524 array_of_bytes.cut();
525 }
526
535 BitArray(const BitArray & array)
537 {
538 // empty
539 }
540
541 void swap(BitArray & array) noexcept
542 {
543 std::swap(current_size, array.current_size);
544 array_of_bytes.swap(array.array_of_bytes);
545 }
546
555 BitArray(BitArray && array) noexcept
556 : current_size(array.current_size), array_of_bytes(std::move(array.array_of_bytes))
557 {
558 array.current_size = 0;
559 }
560
561 BitArray &operator=(BitArray && array) noexcept
562 {
563 current_size = 0;
564 array_of_bytes.cut();
565 swap(array);
566 return *this;
567 }
568
571 {
572 DynList<char> ret_val;
573 for (size_t i = 0; i < current_size; ++i)
574 ret_val.append(static_cast<char>(read_bit(i)));
575 return ret_val;
576 }
577
590 {
591 if (this == &array)
592 return *this;
593
596
597 return *this;
598 }
599
612 void save(std::ostream & output) const
613 {
614 const size_t num_bytes = get_num_bytes();
615
616 // Header
617 output << num_bytes << " " << current_size << '\n';
618 ah_runtime_error_if(not output) << "BitArray::save: write failed (header)";
619
620 // Payload
621 for (size_t i = 0; i < num_bytes; ++i)
622 {
623 int byte = 0;
624 if (const Byte *p = array_of_bytes.test(i); p != nullptr)
625 byte = p->get_int();
626
627 // defend export invariant last masked byte
628 if (num_bytes != 0 && i + 1 == num_bytes)
629 if (const size_t used = current_size % 8; used != 0)
630 {
631 const int mask = (1u << used) - 1u;
632 byte &= mask;
633 }
634
635 output << byte << " ";
637 << "BitArray::save: write payload failed (byte " << i << ")";
638 }
639
640 output << '\n';
641 ah_runtime_error_if(not output) << "BitArray::save: write failed (newline)";
642 }
643
661 void load(std::istream & input)
662 {
663 // Read header first (without touching the current state until validated)
664 size_t num_bytes = 0;
665 size_t num_bits = 0;
666
667 ah_runtime_error_if(not (input >> num_bytes >> num_bits)) << "BitArray::load: read failed (header)";
669 << "BitArray::load: header claims too many bytes (" << num_bytes << ")";
670
671 // Without the sum num_bits + 7, which wraps near the maximum of size_t
672 // and let a header of 0 bytes and 2^64 - 1 bits pass
673 const size_t expected = num_bits / 8 + (num_bits % 8 != 0 ? 1 : 0);
674 ah_runtime_error_if(num_bytes != expected)
675 << "BitArray::load: inconsistent header (num_bytes vs num_bits)";
676
677 // Heavy Load: build temporary and then swap. A byte is reserved when
678 // it is read, not on the word of the header
679 BitArray tmp;
680 tmp.current_size = num_bits;
681 tmp.array_of_bytes.cut();
682
683 for (size_t i = 0; i < num_bytes; ++i)
684 {
685 long v = 0;
686 ah_runtime_error_if(not (input >> v)) << "BitArray::load: read failed (payload)";
687 ah_runtime_error_if(v < 0 or v > 255) << "BitArray::load: byte value out of range";
688
689 tmp.array_of_bytes.touch(i).set_int(static_cast<int>(v));
690 }
691
693 swap(tmp);
694 }
695
698 BitArray(std::ifstream & input)
699 {
700 load(input);
701 }
702
718 void save_in_array_of_chars(const std::string & name, std::ostream & output) const
719 {
720 const size_t num_bytes = get_num_bytes();
721
722 output << "// " << current_size << " bits declaration" << '\n'
723 << "const unsigned char " << name << " [" << num_bytes << "] = {"
724 << '\n' << " ";
725
726 for (size_t i = 0; i < num_bytes; i++)
727 {
728 int byte = 0;
729 if (const Byte *p = array_of_bytes.test(i); p != nullptr)
730 byte = p->get_int();
731
732 if (num_bytes != 0 && i + 1 == num_bytes)
733 if (const size_t used = current_size % 8; used != 0)
734 {
735 const int mask = (1u << used) - 1u;
736 byte &= mask;
737 }
738
739 output << byte;
740
741 if (i != num_bytes - 1)
742 output << ", ";
743
744 if ((i + 1) % 15 == 0)
745 output << '\n' << " ";
746 }
747
748 output << '\n' << "};" << '\n' << '\n';
749 }
750
762 void load_from_array_of_chars(const unsigned char str[],
763 const size_t num_bits)
764 {
765 array_of_bytes.cut();
766
767 size_t num_bytes = num_bits / 8;
768
769 if (num_bits % 8 != 0)
770 num_bytes++;
771
772 for (size_t i = 0; i < num_bytes; i++)
773 array_of_bytes.touch(i).set_int(str[i]);
774
775 current_size = num_bits;
777 }
778
786 void left_shift(const size_t n = 1)
787 {
788 const size_t real_n = std::min<size_t>(n, current_size);
789
790 for (size_t i = 0; i < current_size - real_n; ++i)
791 write_bit(i, read_bit(i + real_n));
792
793 for (size_t i = current_size - real_n; i < current_size; ++i)
794 write_bit(i, 0);
795 }
796
804 void right_shift(const size_t n = 1)
805 {
806 const size_t real_n = std::min<size_t>(n, current_size);
807
808 for (size_t i = current_size; i > real_n; --i)
809 write_bit(i - 1, read_bit(i - real_n - 1));
810
811 for (size_t i = real_n; i > 0; --i)
812 write_bit(i - 1, 0);
813 }
814
822 void dyn_left_shift(const size_t n = 1)
823 {
824 for (size_t i = 0; i < n; ++i)
825 push(0);
826 }
827
835 void dyn_right_shift(const size_t n = 1)
836 {
837 if (current_size == 0)
838 return;
839
840 if (n >= current_size)
841 {
842 set_size(1);
843 write_bit(0, 0);
845 return;
846 }
847
848 BitArray array(current_size - n);
849
850 for (size_t i = 0; i < current_size - n; ++i)
851 array.write_bit(i, read_bit(i));
852
853 *this = array;
854 }
855
863 void circular_left_shift(const size_t n = 1)
864 {
865 if (current_size < 2)
866 return;
867
868 const size_t real_n = n % current_size;
869
870 if (real_n == 0)
871 return;
872
873 BitArray array(real_n);
874
875 for (size_t i = 0; i < real_n; ++i)
876 array.write_bit(i, read_bit(i));
877
878 for (size_t i = 0; i < current_size - real_n; ++i)
879 write_bit(i, read_bit(i + real_n));
880
881 for (size_t i = 0; i < real_n; ++i)
882 write_bit(current_size - real_n + i, array.read_bit(i));
883 }
884
892 void circular_right_shift(const size_t n = 1)
893 {
894 if (current_size < 2)
895 return;
896
897 const size_t real_n = n % current_size;
898
899 if (real_n == 0)
900 return;
901
902 BitArray array(real_n);
903
904 for (size_t i = current_size - real_n; i < current_size; ++i)
905 array.write_bit(i - (current_size - real_n), read_bit(i));
906
907 for (size_t i = current_size; i-- > real_n;)
908 write_bit(i, read_bit(i - real_n));
909
910 for (size_t i = 0; i < real_n; ++i)
911 write_bit(i, array.read_bit(i));
912 }
913
914 template <typename T>
915 void set_num(T n) // Copy step because it is modified inside
916 {
917 using U = std::make_unsigned_t<T>;
918 U u = static_cast<U>(n);
919 empty();
920 const size_t num_bits = sizeof(T) * 8;
921 reserve(num_bits);
922 for (size_t i = 0; i < num_bits; ++i)
923 {
924 write_bit(current_size - i - 1, u & 1u);
925 u >>= 1;
926 }
927 }
928
929 void set_num(const char & c)
930 {
931 set_num<char>(c);
932 }
933
934 void set_num(const short & c)
935 {
936 set_num<short>(c);
937 }
938
939 void set_num(const int & c)
940 {
941 set_num<int>(c);
942 }
943
944 void set_num(const long & c)
945 {
946 set_num<long>(c);
947 }
948
949 unsigned long get_unum() const noexcept
950 {
951 using UL = unsigned long;
952 constexpr size_t max_bits = sizeof(UL) * 8;
953 const size_t n = std::min(current_size, max_bits);
954
955 UL ret_val = 0;
956 for (size_t i = 0; i < n; ++i)
957 ret_val |= static_cast<UL>(read_bit_ne(current_size - i - 1)) << i;
958 return ret_val;
959 }
960
961 long get_num() const noexcept
962 {
963 return static_cast<long>(get_unum());
964 }
965
966 void set_bit_str(const std::string & str)
967 {
968 empty();
969 const size_t & str_size = str.size();
970
971 reserve(str_size);
972
973 for (size_t i = 0; i < str_size; ++i)
974 {
975 char c = str[i];
976 assert(c == '1' or c == '0');
977 write_bit(i, (c == '0' ? 0 : 1));
978 }
979 }
980
981 std::string get_bit_str() const
982 {
983 std::string ret_val;
984 for (size_t i = 0; i < current_size; ++i)
985 ret_val.append(read_bit(i) == 0 ? "0" : "1");
986
987 return ret_val;
988 }
989
990 std::string to_string() const
991 {
992 return get_bit_str();
993 }
994
995 friend std::ostream &operator<<(std::ostream & out, const BitArray & array)
996 {
997 for (size_t i = 0; i < array.current_size; ++i)
998 out << array.read_bit(i);
999 return out;
1000 }
1001
1004 BitArray(const unsigned char str[], const size_t num_bits)
1005 {
1006 load_from_array_of_chars(str, num_bits);
1007 }
1008
1010 {
1011 if (rhs.size() > current_size)
1012 {
1013 current_size = rhs.size();
1015 }
1016
1017 const size_t rhs_num_bytes = rhs.get_num_bytes();
1018 const size_t rhs_used_bits_in_last_byte = rhs.size() % 8;
1019 for (size_t i = 0; i < rhs_num_bytes; ++i)
1020 {
1021 const Byte *rhs_byte = rhs.array_of_bytes.test(i);
1022 if (rhs_byte == nullptr)
1023 continue;
1024
1025 Byte masked = *rhs_byte;
1026 if (rhs_used_bits_in_last_byte != 0 && i + 1 == rhs_num_bytes)
1027 {
1028 const int mask = (1u << rhs_used_bits_in_last_byte) - 1u;
1029 masked.set_int(masked.get_int() & mask);
1030 }
1031
1032 if (masked.get_int() == 0)
1033 continue;
1034
1035 array_of_bytes.touch(i) |= masked;
1036 }
1037
1039 return *this;
1040 }
1041
1043 {
1044 if (const size_t new_size = std::min(size(), rhs.size()); new_size != current_size)
1045 {
1046 current_size = new_size;
1048 }
1049
1050 const size_t num_bytes = get_num_bytes();
1051 for (size_t i = 0; i < num_bytes; ++i)
1052 {
1053 Byte *lhs_byte = array_of_bytes.test(i);
1054 if (lhs_byte == nullptr)
1055 continue;
1056
1057 if (const Byte *rhs_byte = rhs.array_of_bytes.test(i); rhs_byte == nullptr)
1058 lhs_byte->set_int(0);
1059 else
1060 *lhs_byte &= *rhs_byte;
1061 }
1062
1064 return *this;
1065 }
1066
1067 friend BitArray operator|(const BitArray & op1, const BitArray & op2)
1068 {
1069 BitArray ret = op1;
1070 ret |= op2;
1071 return ret;
1072 }
1073
1074 friend BitArray operator&(const BitArray & op1, const BitArray & op2)
1075 {
1076 BitArray ret = op1;
1077 ret &= op2;
1078 return ret;
1079 }
1080
1081 bool operator==(const BitArray & rhs) const
1082 {
1083 if (size() != rhs.size())
1084 return false;
1085
1086 for (size_t i = 0; i < size(); ++i)
1087 if (read_bit(i) != rhs.read_bit(i))
1088 return false;
1089
1090 return true;
1091 }
1092
1093 private:
1094 template <class Operation>
1095 bool __traverse(Operation & operation)
1096 {
1097 for (size_t i = 0; i < current_size; ++i)
1098 if (not operation(read_bit(i)))
1099 return false;
1100 return true;
1101 }
1102
1103 public:
1105 {
1107 long curr_idx = 0;
1108
1109 public:
1110 Iterator() = default;
1111
1112 Iterator(const BitArray & array) noexcept
1113 : array_ptr(const_cast<BitArray *>(&array)), curr_idx(0)
1114 {
1115 // empty
1116 }
1117
1118 [[nodiscard]] bool has_curr() const noexcept
1119 {
1120 return array_ptr != nullptr and curr_idx >= 0 and
1121 static_cast<size_t>(curr_idx) < array_ptr->size();
1122 }
1123
1124 [[nodiscard]] unsigned int get_curr_ne() const noexcept
1125 {
1126 return has_curr() ? array_ptr->read_bit_ne(static_cast<size_t>(curr_idx)) : 0;
1127 }
1128
1129 [[nodiscard]] unsigned int get_curr() const
1130 {
1132 << "Iterator is at the end of the list";
1133 return array_ptr->read_bit(static_cast<size_t>(curr_idx));
1134 }
1135
1136 [[nodiscard]] long get_pos() const noexcept { return curr_idx; }
1137
1138 void next_ne() noexcept { ++curr_idx; }
1139
1140 void next()
1141 {
1142 ah_overflow_error_if(array_ptr == nullptr) << "Iterator is not bound to any BitArray";
1143 ah_overflow_error_if(curr_idx == static_cast<long>(array_ptr->size()))
1144 << "not current item in iterator";
1145 next_ne();
1146 }
1147
1148 void prev_ne() noexcept { --curr_idx; }
1149
1150 void prev()
1151 {
1152 ah_underflow_error_if(array_ptr == nullptr) << "Iterator is not bound to any BitArray";
1153 ah_underflow_error_if(curr_idx == -1) << "not current item in iterator";
1154 prev_ne();
1155 }
1156
1157 void reset_last() noexcept
1158 {
1159 if (array_ptr == nullptr or array_ptr->size() == 0)
1160 curr_idx = -1;
1161 else
1162 curr_idx = static_cast<long>(array_ptr->size()) - 1;
1163 }
1164
1165 void end() noexcept
1166 {
1167 if (array_ptr == nullptr)
1168 {
1169 curr_idx = 0;
1170 return;
1171 }
1172 curr_idx = static_cast<long>(array_ptr->size());
1173 }
1174
1175 void reset_first() noexcept { curr_idx = 0; }
1176
1177 void reset() noexcept { reset_first(); }
1178 };
1179
1180 auto get_it() const noexcept { return Iterator(*this); }
1181
1182 template <class Operation>
1183 bool traverse(Operation & operation) const
1184 {
1185 return const_cast<BitArray &>(*this).__traverse(operation);
1186 }
1187
1188 template <class Operation>
1189 bool traverse(Operation & operation)
1190 {
1191 return __traverse(operation);
1192 }
1193
1194 template <class Operation>
1195 bool traverse(Operation && operation = Operation()) const
1196 {
1197 return traverse<Operation>(operation);
1198 }
1199
1200 template <class Operation>
1201 bool traverse(Operation && operation = Operation())
1202 {
1203 return traverse<Operation>(operation);
1204 }
1205
1206 // Functional methods provided by FunctionalMixin<BitArray, unsigned int>
1207
1208 Generic_Items(unsigned int);
1209
1211
1212 int count_ones() const noexcept
1213 {
1214 return foldl<int>(0, [](int acc, int x) { return acc + x; });
1215 }
1216
1217 int count_zeros() const noexcept
1218 {
1219 return foldl<int>(0, [](int acc, int x) { return acc + (x == 0); });
1220 }
1221 };
1222} // end namespace Aleph
1223# endif /* BITARRAY_H */
CRTP Mixins for container functionality (DRY principle).
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Definition ah-errors.H:727
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
Definition ah-errors.H:517
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:611
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
Definition ah-errors.H:415
DRY (Don't Repeat Yourself) utilities and macros.
#define Generic_Items(Type)
Generates an items() method returning all container elements.
Definition ahDry.H:151
#define STL_ALEPH_ITERATOR(Set_Name)
Definition ahIterator.H:208
Core header for the Aleph-w library.
size_t size_t int32_t value
Definition ca-c-api.h:116
size_t size_t int32_t * out
Definition ca-c-api.h:120
const size_t byte_index
Definition bitArray.H:246
BitProxy(BitArray &a, const size_t i) noexcept
Definition bitArray.H:251
BitProxy & operator=(const BitProxy &proxy)
Definition bitArray.H:286
BitProxy & operator=(const size_t value)
Definition bitArray.H:267
void reset_first() noexcept
Definition bitArray.H:1175
unsigned int get_curr_ne() const noexcept
Definition bitArray.H:1124
void prev_ne() noexcept
Definition bitArray.H:1148
Iterator(const BitArray &array) noexcept
Definition bitArray.H:1112
unsigned int get_curr() const
Definition bitArray.H:1129
void reset() noexcept
Definition bitArray.H:1177
bool has_curr() const noexcept
Definition bitArray.H:1118
long get_pos() const noexcept
Definition bitArray.H:1136
void end() noexcept
Definition bitArray.H:1165
void next_ne() noexcept
Definition bitArray.H:1138
void reset_last() noexcept
Definition bitArray.H:1157
Contiguous array of bits.
Definition bitArray.H:201
bool traverse(Operation &operation) const
Definition bitArray.H:1183
void fast_write(const size_t i, const unsigned int value)
Write bit i without checking i.
Definition bitArray.H:498
int read_bit(const size_t i) const
Read bit i.
Definition bitArray.H:409
bool traverse(Operation &&operation=Operation()) const
Definition bitArray.H:1195
void set_num(const short &c)
Definition bitArray.H:934
std::string get_bit_str() const
Definition bitArray.H:981
BitArray & operator=(BitArray &&array) noexcept
Definition bitArray.H:561
BitArray & operator&=(const BitArray &rhs)
Definition bitArray.H:1042
void circular_left_shift(const size_t n=1)
Shifts the bits n positions to the left circularly.
Definition bitArray.H:863
BitArray(const unsigned char str[], const size_t num_bits)
Constructs a new array of bits from an array of characters previously generated with load_from_array_...
Definition bitArray.H:1004
BitProxy operator[](const size_t i) noexcept
Definition bitArray.H:390
void set_num(const char &c)
Definition bitArray.H:929
void load_from_array_of_chars(const unsigned char str[], const size_t num_bits)
Reads an array of bits saved in a character array.
Definition bitArray.H:762
bool __traverse(Operation &operation)
Definition bitArray.H:1095
bool operator==(const BitArray &rhs) const
Definition bitArray.H:1081
long get_num() const noexcept
Definition bitArray.H:961
void set_bit_str(const std::string &str)
Definition bitArray.H:966
int fast_read(const size_t i) const noexcept
Read bit i without any check.
Definition bitArray.H:482
void write(const size_t i, const unsigned int value)
Writes bit i with value without memory check.
Definition bitArray.H:462
auto get_it() const noexcept
Definition bitArray.H:1180
void left_shift(const size_t n=1)
Shifts the bits n positions to the left.
Definition bitArray.H:786
BitArray(const BitArray &array)
Copy constructor.
Definition bitArray.H:535
void circular_right_shift(const size_t n=1)
Shifts the bits n positions to the right circularly.
Definition bitArray.H:892
void pop()
Removes the last bit of the array.
Definition bitArray.H:512
size_t current_size
Definition bitArray.H:202
void set_num(const long &c)
Definition bitArray.H:944
void write_bit(const size_t i, const unsigned int value)
Write bit i with the value.
Definition bitArray.H:424
BitArray(const size_t dim, const unsigned int value)
Build a BitArray of size dim with all bits set to value.
Definition bitArray.H:322
void reserve(const size_t dim)
Reserve memory in advance for the bit array dim dimension.
Definition bitArray.H:344
void dyn_left_shift(const size_t n=1)
Shifts bits n positions to the left dynamically.
Definition bitArray.H:822
BitArray & operator|=(const BitArray &rhs)
Definition bitArray.H:1009
BitArray(const size_t dim=0)
Bit array constructor.
Definition bitArray.H:315
void right_shift(const size_t n=1)
Shifts the bits n positions to the right.
Definition bitArray.H:804
void load(std::istream &input)
Loads an array of bits saved with save().
Definition bitArray.H:661
DynList< char > bits_list() const
Converts it to a list.
Definition bitArray.H:570
void empty() noexcept
Delete all inserted bits.
Definition bitArray.H:521
BitArray(BitArray &&array) noexcept
Move constructor; it allocates nothing.
Definition bitArray.H:555
int count_ones() const noexcept
Definition bitArray.H:1212
size_t get_num_bytes() const noexcept
Definition bitArray.H:235
void save_in_array_of_chars(const std::string &name, std::ostream &output) const
Saves a static string declaration to a text file.
Definition bitArray.H:718
int read(const size_t i) const
Quick read of bit i.
Definition bitArray.H:445
friend BitArray operator&(const BitArray &op1, const BitArray &op2)
Definition bitArray.H:1074
friend std::ostream & operator<<(std::ostream &out, const BitArray &array)
Definition bitArray.H:995
void set_num(T n)
Definition bitArray.H:915
void ensure_num_bytes(const size_t num_bytes)
Definition bitArray.H:223
BitArray(std::ifstream &input)
Build a new array of bits from a file constructed using the save() method.
Definition bitArray.H:698
void push(const unsigned int value)
Inserts the value at the end of the array.
Definition bitArray.H:506
constexpr size_t size() const noexcept
Returns the dimension of the bit array.
Definition bitArray.H:350
void swap(BitArray &array) noexcept
Definition bitArray.H:541
BitArray & operator=(const BitArray &array)
Bit array allocation.
Definition bitArray.H:589
void dyn_right_shift(const size_t n=1)
Shifts bits n positions to the right dynamically.
Definition bitArray.H:835
void set_num(const int &c)
Definition bitArray.H:939
friend BitArray operator|(const BitArray &op1, const BitArray &op2)
Definition bitArray.H:1067
int operator()(const size_t i) const
Definition bitArray.H:415
bool traverse(Operation &operation)
Definition bitArray.H:1189
bool traverse(Operation &&operation=Operation())
Definition bitArray.H:1201
void save(std::ostream &output) const
Saves the bit sequence in a text file.
Definition bitArray.H:612
int count_zeros() const noexcept
Definition bitArray.H:1217
int read_bit_ne(const size_t i) const noexcept
Definition bitArray.H:392
void clear_unused_bits_in_last_byte() noexcept
Definition bitArray.H:205
unsigned int Item_Type
Type returned by Iterator::get_curr() - individual bits as unsigned int.
Definition bitArray.H:307
unsigned long get_unum() const noexcept
Definition bitArray.H:949
std::string to_string() const
Definition bitArray.H:990
DynArray< Byte > array_of_bytes
Definition bitArray.H:203
int operator[](const size_t i) const
Definition bitArray.H:388
void set_size(const size_t sz)
Resets the dimension of the array.
Definition bitArray.H:362
void write_bit(const unsigned int i, const unsigned int val) noexcept
Definition bitArray.H:126
int get_int() const noexcept
Definition bitArray.H:148
std::uint8_t value
Definition bitArray.H:117
Byte & operator|=(const Byte &rhs) noexcept
Definition bitArray.H:158
int count_ones() const noexcept
Definition bitArray.H:170
void set_int(int i) noexcept
Definition bitArray.H:153
Byte() noexcept=default
unsigned int read_bit(const unsigned int i) const noexcept
Definition bitArray.H:120
Byte & operator&=(const Byte &rhs) noexcept
Definition bitArray.H:164
int count_zeros() const noexcept
Definition bitArray.H:175
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
CRTP Mixin providing functional programming operations.
and
Transform elements using a mapping function.
Minimal std::expected-style result type for C++20.
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_dim_function > > dim(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4130
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
Lazy and scalable dynamic array implementation.
ofstream output
Definition writeHeap.C:215