Aleph-w 3.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
147
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
171 {
172 return std::popcount(value);
173 }
174
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;
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
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;
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
340 void reserve(const size_t dim)
341 {
342 set_size(dim);
343 }
344
346 [[nodiscard]] constexpr size_t size() const noexcept { return current_size; }
347
349 void set_size(const size_t sz)
350 {
351 const size_t old_size = current_size;
352 const size_t array_size = (sz + 7) / 8;
353
354 array_of_bytes.adjust(array_size); // allocate for fast read()/write()
355 current_size = sz;
356
357 if (sz > old_size && old_size != 0)
358 if (const size_t rem = old_size % 8; rem != 0)
359 {
360 const size_t clear_end = std::min(sz, old_size + (8 - rem));
361 for (size_t i = old_size; i < clear_end; ++i)
362 write_bit(i, 0);
363 }
364
366 }
367
368 int operator[](const size_t i) const { return read_bit(i); }
369
370 BitProxy operator[](const size_t i) noexcept { return BitProxy(*this, i); }
371
372 int read_bit_ne(const size_t i) const noexcept
373 {
374 const unsigned int bit_index = static_cast<unsigned int>(i % 8);
375 if (const auto ptr = array_of_bytes.test(i / 8))
376 {
377 const Byte & byte = *ptr;
378 return byte.read_bit(bit_index);
379 }
380 return 0;
381 }
382
389 int read_bit(const size_t i) const
390 {
391 ah_out_of_range_error_if(i >= current_size) << "index out of range";
392 return read_bit_ne(i);
393 }
394
395 int operator()(const size_t i) const { return read_bit(i); }
396
404 void write_bit(const size_t i, const unsigned int value)
405 {
406 ah_out_of_range_error_if(value > 1) << "BitArray::write_bit: value must be 0 or 1";
407 array_of_bytes.touch(i / 8).write_bit(i % 8, value);
408 if (i >= current_size)
409 {
410 current_size = i + 1;
412 }
413 }
414
423 int read(const size_t i) const
424 {
425 ah_out_of_range_error_if(i >= current_size) << "index out of range";
426
427 const int bit_index = i % 8;
428 return array_of_bytes.access(i / 8).read_bit(bit_index);
429 }
430
438 void write(const size_t i, const unsigned int value)
439 {
440 ah_out_of_range_error_if(value > 1) << "BitArray::write: value must be 0 or 1";
441
442 array_of_bytes.access(i / 8).write_bit(i % 8, value);
443 if (i >= current_size)
444 {
445 current_size = i + 1;
447 }
448 }
449
450 int fast_read(const size_t i) const noexcept
451 {
452 return array_of_bytes.access(i / 8).read_bit(i % 8);
453 }
454
455 void fast_write(const size_t i, const unsigned int value)
456 {
457 ah_out_of_range_error_if(value > 1) << "BitArray::fast_write: value must be 0 or 1";
458 array_of_bytes.access(i / 8).write_bit(i % 8, value);
459 }
460
462 void push(const unsigned int value)
463 {
465 }
466
468 void pop()
469 {
470 ah_underflow_error_if(current_size == 0) << "BitArray::pop on empty array";
471 current_size--;
474 }
475
478 {
479 current_size = 0;
480 array_of_bytes.cut();
481 }
482
491 BitArray(const BitArray & array)
493 {
494 // empty
495 }
496
497 void swap(BitArray & array) noexcept
498 {
499 std::swap(current_size, array.current_size);
500 array_of_bytes.swap(array.array_of_bytes);
501 }
502
504 : BitArray()
505 {
506 swap(array);
507 }
508
509 BitArray &operator=(BitArray && array) noexcept
510 {
511 current_size = 0;
512 array_of_bytes.cut();
513 swap(array);
514 return *this;
515 }
516
519 {
521 for (size_t i = 0; i < current_size; ++i)
522 ret_val.append(static_cast<char>(read_bit(i)));
523 return ret_val;
524 }
525
538 {
539 if (this == &array)
540 return *this;
541
544
545 return *this;
546 }
547
560 void save(std::ostream & output) const
561 {
562 const size_t num_bytes = get_num_bytes();
563
564 // Header
565 output << num_bytes << " " << current_size << '\n';
566 ah_runtime_error_if(not output) << "BitArray::save: write failed (header)";
567
568 // Payload
569 for (size_t i = 0; i < num_bytes; ++i)
570 {
571 int byte = 0;
572 if (const Byte *p = array_of_bytes.test(i); p != nullptr)
573 byte = p->get_int();
574
575 // defend export invariant last masked byte
576 if (num_bytes != 0 && i + 1 == num_bytes)
577 if (const size_t used = current_size % 8; used != 0)
578 {
579 const int mask = (1u << used) - 1u;
580 byte &= mask;
581 }
582
583 output << byte << " ";
585 << "BitArray::save: write payload failed (byte " << i << ")";
586 }
587
588 output << '\n';
589 ah_runtime_error_if(not output) << "BitArray::save: write failed (newline)";
590 }
591
602 void load(std::istream & input)
603 {
604 // Read header first (without touching the current state until validated)
605 size_t num_bytes = 0;
606 size_t num_bits = 0;
607
608 ah_runtime_error_if(not (input >> num_bytes >> num_bits)) << "BitArray::load: read failed (header)";
609
610 const size_t expected = (num_bits + 7) / 8;
611 ah_runtime_error_if(num_bytes != expected)
612 << "BitArray::load: inconsistent header (num_bytes vs num_bits)";
613
614 // Heavy Load: build temporary and then swap
616 tmp.current_size = num_bits;
617 tmp.array_of_bytes.cut();
618 tmp.ensure_num_bytes(num_bytes);
619
620 for (size_t i = 0; i < num_bytes; ++i)
621 {
622 long v = 0;
623 ah_runtime_error_if(not (input >> v)) << "BitArray::load: read failed (payload)";
624 ah_runtime_error_if(v < 0 or v > 255) << "BitArray::load: byte value out of range";
625
626 tmp.array_of_bytes.touch(i).set_int(static_cast<int>(v));
627 }
628
629 tmp.clear_unused_bits_in_last_byte();
630 swap(tmp);
631 }
632
635 BitArray(std::ifstream & input)
636 {
637 load(input);
638 }
639
655 void save_in_array_of_chars(const std::string & name, std::ostream & output) const
656 {
657 const size_t num_bytes = get_num_bytes();
658
659 output << "// " << current_size << " bits declaration" << '\n'
660 << "const unsigned char " << name << " [" << num_bytes << "] = {"
661 << '\n' << " ";
662
663 for (size_t i = 0; i < num_bytes; i++)
664 {
665 int byte = 0;
666 if (const Byte *p = array_of_bytes.test(i); p != nullptr)
667 byte = p->get_int();
668
669 if (num_bytes != 0 && i + 1 == num_bytes)
670 if (const size_t used = current_size % 8; used != 0)
671 {
672 const int mask = (1u << used) - 1u;
673 byte &= mask;
674 }
675
676 output << byte;
677
678 if (i != num_bytes - 1)
679 output << ", ";
680
681 if ((i + 1) % 15 == 0)
682 output << '\n' << " ";
683 }
684
685 output << '\n' << "};" << '\n' << '\n';
686 }
687
699 void load_from_array_of_chars(const unsigned char str[],
700 const size_t num_bits)
701 {
702 array_of_bytes.cut();
703
704 size_t num_bytes = num_bits / 8;
705
706 if (num_bits % 8 != 0)
707 num_bytes++;
708
709 for (size_t i = 0; i < num_bytes; i++)
710 array_of_bytes.touch(i).set_int(str[i]);
711
712 current_size = num_bits;
714 }
715
723 void left_shift(const size_t n = 1)
724 {
725 const size_t real_n = std::min<size_t>(n, current_size);
726
727 for (size_t i = 0; i < current_size - real_n; ++i)
728 write_bit(i, read_bit(i + real_n));
729
730 for (size_t i = current_size - real_n; i < current_size; ++i)
731 write_bit(i, 0);
732 }
733
741 void right_shift(const size_t n = 1)
742 {
743 const size_t real_n = std::min<size_t>(n, current_size);
744
745 for (size_t i = current_size; i > real_n; --i)
746 write_bit(i - 1, read_bit(i - real_n - 1));
747
748 for (size_t i = real_n; i > 0; --i)
749 write_bit(i - 1, 0);
750 }
751
759 void dyn_left_shift(const size_t n = 1)
760 {
761 for (size_t i = 0; i < n; ++i)
762 push(0);
763 }
764
772 void dyn_right_shift(const size_t n = 1)
773 {
774 if (current_size == 0)
775 return;
776
777 if (n >= current_size)
778 {
779 set_size(1);
780 write_bit(0, 0);
782 return;
783 }
784
785 BitArray array(current_size - n);
786
787 for (size_t i = 0; i < current_size - n; ++i)
788 array.write_bit(i, read_bit(i));
789
790 *this = array;
791 }
792
800 void circular_left_shift(const size_t n = 1)
801 {
802 if (current_size < 2)
803 return;
804
805 const size_t real_n = n % current_size;
806
807 if (real_n == 0)
808 return;
809
810 BitArray array(real_n);
811
812 for (size_t i = 0; i < real_n; ++i)
813 array.write_bit(i, read_bit(i));
814
815 for (size_t i = 0; i < current_size - real_n; ++i)
816 write_bit(i, read_bit(i + real_n));
817
818 for (size_t i = 0; i < real_n; ++i)
819 write_bit(current_size - real_n + i, array.read_bit(i));
820 }
821
829 void circular_right_shift(const size_t n = 1)
830 {
831 if (current_size < 2)
832 return;
833
834 const size_t real_n = n % current_size;
835
836 if (real_n == 0)
837 return;
838
839 BitArray array(real_n);
840
841 for (size_t i = current_size - real_n; i < current_size; ++i)
842 array.write_bit(i - (current_size - real_n), read_bit(i));
843
844 for (size_t i = current_size; i-- > real_n;)
845 write_bit(i, read_bit(i - real_n));
846
847 for (size_t i = 0; i < real_n; ++i)
848 write_bit(i, array.read_bit(i));
849 }
850
851 template <typename T>
852 void set_num(T n) // Copy step because it is modified inside
853 {
854 using U = std::make_unsigned_t<T>;
855 U u = static_cast<U>(n);
856 empty();
857 const size_t num_bits = sizeof(T) * 8;
858 reserve(num_bits);
859 for (size_t i = 0; i < num_bits; ++i)
860 {
861 write_bit(current_size - i - 1, u & 1u);
862 u >>= 1;
863 }
864 }
865
866 void set_num(const char & c)
867 {
868 set_num<char>(c);
869 }
870
871 void set_num(const short & c)
872 {
874 }
875
876 void set_num(const int & c)
877 {
878 set_num<int>(c);
879 }
880
881 void set_num(const long & c)
882 {
883 set_num<long>(c);
884 }
885
886 unsigned long get_unum() const noexcept
887 {
888 using UL = unsigned long;
889 constexpr size_t max_bits = sizeof(UL) * 8;
890 const size_t n = std::min(current_size, max_bits);
891
892 UL ret_val = 0;
893 for (size_t i = 0; i < n; ++i)
894 ret_val |= static_cast<UL>(read_bit_ne(current_size - i - 1)) << i;
895 return ret_val;
896 }
897
899 {
900 return static_cast<long>(get_unum());
901 }
902
903 void set_bit_str(const std::string & str)
904 {
905 empty();
906 const size_t & str_size = str.size();
907
909
910 for (size_t i = 0; i < str_size; ++i)
911 {
912 char c = str[i];
913 assert(c == '1' or c == '0');
914 write_bit(i, (c == '0' ? 0 : 1));
915 }
916 }
917
918 std::string get_bit_str() const
919 {
920 std::string ret_val;
921 for (size_t i = 0; i < current_size; ++i)
922 ret_val.append(read_bit(i) == 0 ? "0" : "1");
923
924 return ret_val;
925 }
926
927 std::string to_string() const
928 {
929 return get_bit_str();
930 }
931
932 friend std::ostream &operator<<(std::ostream & out, const BitArray & array)
933 {
934 for (size_t i = 0; i < array.current_size; ++i)
935 out << array.read_bit(i);
936 return out;
937 }
938
941 BitArray(const unsigned char str[], const size_t num_bits)
942 {
943 load_from_array_of_chars(str, num_bits);
944 }
945
947 {
948 if (rhs.size() > current_size)
949 {
950 current_size = rhs.size();
952 }
953
954 const size_t rhs_num_bytes = (rhs.size() + 7) / 8;
955 const size_t rhs_used_bits_in_last_byte = rhs.size() % 8;
956 for (size_t i = 0; i < rhs_num_bytes; ++i)
957 {
958 const Byte *rhs_byte = rhs.array_of_bytes.test(i);
959 if (rhs_byte == nullptr)
960 continue;
961
963 if (rhs_used_bits_in_last_byte != 0 && i + 1 == rhs_num_bytes)
964 {
965 const int mask = (1u << rhs_used_bits_in_last_byte) - 1u;
966 masked.set_int(masked.get_int() & mask);
967 }
968
969 if (masked.get_int() == 0)
970 continue;
971
972 array_of_bytes.touch(i) |= masked;
973 }
974
976 return *this;
977 }
978
980 {
981 if (const size_t new_size = std::min(size(), rhs.size()); new_size != current_size)
982 {
985 }
986
987 const size_t num_bytes = get_num_bytes();
988 for (size_t i = 0; i < num_bytes; ++i)
989 {
990 Byte *lhs_byte = array_of_bytes.test(i);
991 if (lhs_byte == nullptr)
992 continue;
993
994 if (const Byte *rhs_byte = rhs.array_of_bytes.test(i); rhs_byte == nullptr)
995 lhs_byte->set_int(0);
996 else
997 *lhs_byte &= *rhs_byte;
998 }
999
1001 return *this;
1002 }
1003
1004 friend BitArray operator|(const BitArray & op1, const BitArray & op2)
1005 {
1006 BitArray ret = op1;
1007 ret |= op2;
1008 return ret;
1009 }
1010
1011 friend BitArray operator&(const BitArray & op1, const BitArray & op2)
1012 {
1013 BitArray ret = op1;
1014 ret &= op2;
1015 return ret;
1016 }
1017
1018 bool operator==(const BitArray & rhs) const
1019 {
1020 if (size() != rhs.size())
1021 return false;
1022
1023 for (size_t i = 0; i < size(); ++i)
1024 if (read_bit(i) != rhs.read_bit(i))
1025 return false;
1026
1027 return true;
1028 }
1029
1030 private:
1031 template <class Operation>
1033 {
1034 for (size_t i = 0; i < current_size; ++i)
1035 if (not operation(read_bit(i)))
1036 return false;
1037 return true;
1038 }
1039
1040 public:
1042 {
1044 long curr_idx = 0;
1045
1046 public:
1047 Iterator() = default;
1048
1050 : array_ptr(const_cast<BitArray *>(&array)), curr_idx(0)
1051 {
1052 // empty
1053 }
1054
1056 {
1057 return array_ptr != nullptr and curr_idx >= 0 and
1058 static_cast<size_t>(curr_idx) < array_ptr->size();
1059 }
1060
1062 {
1063 return has_curr() ? array_ptr->read_bit_ne(static_cast<size_t>(curr_idx)) : 0;
1064 }
1065
1066 [[nodiscard]] unsigned int get_curr() const
1067 {
1069 << "Iterator is at the end of the list";
1070 return array_ptr->read_bit(static_cast<size_t>(curr_idx));
1071 }
1072
1073 [[nodiscard]] long get_pos() const noexcept { return curr_idx; }
1074
1076
1077 void next()
1078 {
1079 ah_overflow_error_if(array_ptr == nullptr) << "Iterator is not bound to any BitArray";
1080 ah_overflow_error_if(curr_idx == static_cast<long>(array_ptr->size()))
1081 << "not current item in iterator";
1082 next_ne();
1083 }
1084
1086
1087 void prev()
1088 {
1089 ah_underflow_error_if(array_ptr == nullptr) << "Iterator is not bound to any BitArray";
1090 ah_underflow_error_if(curr_idx == -1) << "not current item in iterator";
1091 prev_ne();
1092 }
1093
1095 {
1096 if (array_ptr == nullptr or array_ptr->size() == 0)
1097 curr_idx = -1;
1098 else
1099 curr_idx = static_cast<long>(array_ptr->size()) - 1;
1100 }
1101
1103 {
1104 if (array_ptr == nullptr)
1105 {
1106 curr_idx = 0;
1107 return;
1108 }
1109 curr_idx = static_cast<long>(array_ptr->size());
1110 }
1111
1113
1115 };
1116
1117 auto get_it() const noexcept { return Iterator(*this); }
1118
1119 template <class Operation>
1121 {
1122 return const_cast<BitArray &>(*this).__traverse(operation);
1123 }
1124
1125 template <class Operation>
1127 {
1128 return __traverse(operation);
1129 }
1130
1131 template <class Operation>
1133 {
1135 }
1136
1137 template <class Operation>
1139 {
1141 }
1142
1143 // Functional methods provided by FunctionalMixin<BitArray, unsigned int>
1144
1145 Generic_Items(unsigned int);
1146
1148
1150 {
1151 return foldl<int>(0, [](int acc, int x) { return acc + x; });
1152 }
1153
1155 {
1156 return foldl<int>(0, [](int acc, int x) { return acc + (x == 0); });
1157 }
1158 };
1159} // end namespace Aleph
1160# 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:584
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
Definition ah-errors.H:373
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:468
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
Definition ah-errors.H:271
DRY (Don't Repeat Yourself) utilities and macros.
#define Generic_Items(Type)
Generates an items() method returning all container elements.
Definition ahDry.H:139
#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:1112
unsigned int get_curr_ne() const noexcept
Definition bitArray.H:1061
void prev_ne() noexcept
Definition bitArray.H:1085
Iterator(const BitArray &array) noexcept
Definition bitArray.H:1049
unsigned int get_curr() const
Definition bitArray.H:1066
void reset() noexcept
Definition bitArray.H:1114
bool has_curr() const noexcept
Definition bitArray.H:1055
long get_pos() const noexcept
Definition bitArray.H:1073
void end() noexcept
Definition bitArray.H:1102
void next_ne() noexcept
Definition bitArray.H:1075
void reset_last() noexcept
Definition bitArray.H:1094
Contiguous array of bits.
Definition bitArray.H:201
bool traverse(Operation &operation) const
Definition bitArray.H:1120
void fast_write(const size_t i, const unsigned int value)
Definition bitArray.H:455
int read_bit(const size_t i) const
Read bit i.
Definition bitArray.H:389
bool traverse(Operation &&operation=Operation()) const
Definition bitArray.H:1132
void set_num(const short &c)
Definition bitArray.H:871
std::string get_bit_str() const
Definition bitArray.H:918
BitArray & operator=(BitArray &&array) noexcept
Definition bitArray.H:509
BitArray & operator&=(const BitArray &rhs)
Definition bitArray.H:979
void circular_left_shift(const size_t n=1)
Shifts the bits n positions to the left circularly.
Definition bitArray.H:800
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:941
BitProxy operator[](const size_t i) noexcept
Definition bitArray.H:370
void set_num(const char &c)
Definition bitArray.H:866
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:699
bool __traverse(Operation &operation)
Definition bitArray.H:1032
bool operator==(const BitArray &rhs) const
Definition bitArray.H:1018
long get_num() const noexcept
Definition bitArray.H:898
void set_bit_str(const std::string &str)
Definition bitArray.H:903
int fast_read(const size_t i) const noexcept
Definition bitArray.H:450
void write(const size_t i, const unsigned int value)
Writes bit i with value without memory check.
Definition bitArray.H:438
auto get_it() const noexcept
Definition bitArray.H:1117
void left_shift(const size_t n=1)
Shifts the bits n positions to the left.
Definition bitArray.H:723
BitArray(const BitArray &array)
Copy constructor.
Definition bitArray.H:491
void circular_right_shift(const size_t n=1)
Shifts the bits n positions to the right circularly.
Definition bitArray.H:829
void pop()
Removes the last bit of the array.
Definition bitArray.H:468
size_t current_size
Definition bitArray.H:202
void set_num(const long &c)
Definition bitArray.H:881
void write_bit(const size_t i, const unsigned int value)
Write bit i with the value.
Definition bitArray.H:404
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:340
void dyn_left_shift(const size_t n=1)
Shifts bits n positions to the left dynamically.
Definition bitArray.H:759
BitArray & operator|=(const BitArray &rhs)
Definition bitArray.H:946
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:741
void load(std::istream &input)
Loads an array of bits from a file.
Definition bitArray.H:602
DynList< char > bits_list() const
Converts it to a list.
Definition bitArray.H:518
void empty() noexcept
Delete all inserted bits.
Definition bitArray.H:477
BitArray(BitArray &&array) noexcept
Definition bitArray.H:503
int count_ones() const noexcept
Definition bitArray.H:1149
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:655
int read(const size_t i) const
Quick read of bit i.
Definition bitArray.H:423
friend BitArray operator&(const BitArray &op1, const BitArray &op2)
Definition bitArray.H:1011
friend std::ostream & operator<<(std::ostream &out, const BitArray &array)
Definition bitArray.H:932
void set_num(T n)
Definition bitArray.H:852
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:635
void push(const unsigned int value)
Inserts the value at the end of the array.
Definition bitArray.H:462
constexpr size_t size() const noexcept
Returns the dimension of the bit array.
Definition bitArray.H:346
void swap(BitArray &array) noexcept
Definition bitArray.H:497
BitArray & operator=(const BitArray &array)
Bit array allocation.
Definition bitArray.H:537
void dyn_right_shift(const size_t n=1)
Shifts bits n positions to the right dynamically.
Definition bitArray.H:772
void set_num(const int &c)
Definition bitArray.H:876
friend BitArray operator|(const BitArray &op1, const BitArray &op2)
Definition bitArray.H:1004
int operator()(const size_t i) const
Definition bitArray.H:395
bool traverse(Operation &operation)
Definition bitArray.H:1126
bool traverse(Operation &&operation=Operation())
Definition bitArray.H:1138
void save(std::ostream &output) const
Saves the bit sequence in a text file.
Definition bitArray.H:560
int count_zeros() const noexcept
Definition bitArray.H:1154
int read_bit_ne(const size_t i) const noexcept
Definition bitArray.H:372
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:886
std::string to_string() const
Definition bitArray.H:927
DynArray< Byte > array_of_bytes
Definition bitArray.H:203
int operator[](const size_t i) const
Definition bitArray.H:368
void set_size(const size_t sz)
Resets the dimension of the array.
Definition bitArray.H:349
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:1155
T & append(const T &item)
Definition htlist.H:1271
CRTP Mixin providing functional programming operations.
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:4063
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
and
Check uniqueness with explicit hash + equality functors.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:105
Lazy and scalable dynamic array implementation.
ofstream output
Definition writeHeap.C:215