105# include <type_traits>
122 return (
value >> i) & 0x1;
125 void write_bit(
const unsigned int i,
const unsigned int val)
noexcept
129 const std::uint8_t mask =
static_cast<std::uint8_t
>(1) << i;
130 const auto bit =
static_cast<std::uint8_t
>(val & 0x1u);
131 this->value = (this->value &
~mask) | (
bit << i);
138 return static_cast<int>(
value);
143 value =
static_cast<std::uint8_t
>(i);
160 return std::popcount(
value);
314 for (
size_t i = 0; i <
dim; ++i)
362 const unsigned int bit_index =
static_cast<unsigned int>(i % 8);
365 const Byte &
byte = *ptr;
392 void write_bit(
const size_t i,
const unsigned int value)
415 const int bit_index = i % 8;
426 void write(
const size_t i,
const unsigned int value)
450 void push(
const unsigned int value)
557 for (
size_t i = 0; i < num_bytes; ++i)
564 if (num_bytes != 0 && i + 1 == num_bytes)
567 const int mask = (1u <<
used) - 1u;
573 <<
"BitArray::save: write payload failed (byte " << i <<
")";
593 size_t num_bytes = 0;
598 const size_t expected = (num_bits + 7) / 8;
600 <<
"BitArray::load: inconsistent header (num_bytes vs num_bits)";
605 tmp.array_of_bytes.cut();
606 tmp.ensure_num_bytes(num_bytes);
608 for (
size_t i = 0; i < num_bytes; ++i)
614 tmp.array_of_bytes.touch(i).set_int(
static_cast<int>(v));
617 tmp.clear_unused_bits_in_last_byte();
648 <<
"const unsigned char " << name <<
" [" << num_bytes <<
"] = {"
651 for (
size_t i = 0; i < num_bytes; i++)
657 if (num_bytes != 0 && i + 1 == num_bytes)
660 const int mask = (1u <<
used) - 1u;
666 if (i != num_bytes - 1)
669 if ((i + 1) % 15 == 0)
673 output <<
'\n' <<
"};" <<
'\n' <<
'\n';
688 const size_t num_bits)
692 size_t num_bytes = num_bits / 8;
694 if (num_bits % 8 != 0)
697 for (
size_t i = 0; i < num_bytes; i++)
736 for (
size_t i =
real_n; i > 0; --i)
749 for (
size_t i = 0; i < n; ++i)
800 for (
size_t i = 0; i <
real_n; ++i)
806 for (
size_t i = 0; i <
real_n; ++i)
835 for (
size_t i = 0; i <
real_n; ++i)
839 template <
typename T>
842 using U = std::make_unsigned_t<T>;
843 U u =
static_cast<U>(n);
845 const size_t num_bits =
sizeof(
T) * 8;
847 for (
size_t i = 0; i < num_bits; ++i)
881 for (
size_t i = 0; i < n; ++i)
888 return static_cast<long>(
get_unum());
894 const size_t &
str_size = str.size();
898 for (
size_t i = 0; i <
str_size; ++i)
929 BitArray(
const unsigned char str[],
const size_t num_bits)
957 if (
masked.get_int() == 0)
976 for (
size_t i = 0; i < num_bytes; ++i)
1011 for (
size_t i = 0; i <
size(); ++i)
1019 template <
class Operation>
1057 <<
"Iterator is at the end of the list";
1069 <<
"not current item in iterator";
1107 template <
class Operation>
1113 template <
class Operation>
1119 template <
class Operation>
1125 template <
class Operation>
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.
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
DRY (Don't Repeat Yourself) utilities and macros.
#define Generic_Items(Type)
Generates an items() method returning all container elements.
#define STL_ALEPH_ITERATOR(Set_Name)
Core header for the Aleph-w library.
BitProxy(BitArray &a, const size_t i) noexcept
BitProxy & operator=(const BitProxy &proxy)
BitProxy & operator=(const size_t value)
void reset_first() noexcept
unsigned int get_curr_ne() const noexcept
Iterator(const BitArray &array) noexcept
unsigned int get_curr() const
bool has_curr() const noexcept
long get_pos() const noexcept
void reset_last() noexcept
Contiguous array of bits.
bool traverse(Operation &operation) const
void fast_write(const size_t i, const unsigned int value)
int read_bit(const size_t i) const
Read bit i.
bool traverse(Operation &&operation=Operation()) const
void set_num(const short &c)
std::string get_bit_str() const
BitArray & operator=(BitArray &&array) noexcept
BitArray & operator&=(const BitArray &rhs)
void circular_left_shift(const size_t n=1)
Shifts the bits n positions to the left circularly.
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_...
BitProxy operator[](const size_t i) noexcept
void set_num(const char &c)
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.
bool __traverse(Operation &operation)
bool operator==(const BitArray &rhs) const
long get_num() const noexcept
void set_bit_str(const std::string &str)
int fast_read(const size_t i) const noexcept
void write(const size_t i, const unsigned int value)
Writes bit i with value without memory check.
auto get_it() const noexcept
void left_shift(const size_t n=1)
Shifts the bits n positions to the left.
BitArray(const BitArray &array)
Copy constructor.
void circular_right_shift(const size_t n=1)
Shifts the bits n positions to the right circularly.
void pop()
Removes the last bit of the array.
void set_num(const long &c)
void write_bit(const size_t i, const unsigned int value)
Write bit i with the value.
BitArray(const size_t dim, const unsigned int value)
Build a BitArray of size dim with all bits set to value.
void reserve(const size_t dim)
Reserve memory in advance for the bit array dim dimension.
void dyn_left_shift(const size_t n=1)
Shifts bits n positions to the left dynamically.
BitArray & operator|=(const BitArray &rhs)
BitArray(const size_t dim=0)
Bit array constructor.
void right_shift(const size_t n=1)
Shifts the bits n positions to the right.
void load(std::istream &input)
Loads an array of bits from a file.
DynList< char > bits_list() const
Converts it to a list.
void empty() noexcept
Delete all inserted bits.
BitArray(BitArray &&array) noexcept
int count_ones() const noexcept
size_t get_num_bytes() const noexcept
void save_in_array_of_chars(const std::string &name, std::ostream &output) const
Saves a static string declaration to a text file.
int read(const size_t i) const
Quick read of bit i.
friend BitArray operator&(const BitArray &op1, const BitArray &op2)
friend std::ostream & operator<<(std::ostream &out, const BitArray &array)
void ensure_num_bytes(const size_t num_bytes)
BitArray(std::ifstream &input)
Build a new array of bits from a file constructed using the save() method.
void push(const unsigned int value)
Inserts the value at the end of the array.
constexpr size_t size() const noexcept
Returns the dimension of the bit array.
void swap(BitArray &array) noexcept
BitArray & operator=(const BitArray &array)
Bit array allocation.
void dyn_right_shift(const size_t n=1)
Shifts bits n positions to the right dynamically.
void set_num(const int &c)
friend BitArray operator|(const BitArray &op1, const BitArray &op2)
int operator()(const size_t i) const
bool traverse(Operation &operation)
bool traverse(Operation &&operation=Operation())
void save(std::ostream &output) const
Saves the bit sequence in a text file.
int count_zeros() const noexcept
int read_bit_ne(const size_t i) const noexcept
void clear_unused_bits_in_last_byte() noexcept
unsigned int Item_Type
Type returned by Iterator::get_curr() - individual bits as unsigned int.
unsigned long get_unum() const noexcept
std::string to_string() const
DynArray< Byte > array_of_bytes
int operator[](const size_t i) const
void set_size(const size_t sz)
Resets the dimension of the array.
void write_bit(const unsigned int i, const unsigned int val) noexcept
int get_int() const noexcept
Byte & operator|=(const Byte &rhs) noexcept
int count_ones() const noexcept
void set_int(int i) noexcept
unsigned int read_bit(const unsigned int i) const noexcept
Byte & operator&=(const Byte &rhs) noexcept
int count_zeros() const noexcept
Dynamic singly linked list with functional programming support.
T & append(const T &item)
Append a new item by copy.
CRTP Mixin providing functional programming operations.
Container< __Type > maps(Operation &operation) const
Transform elements using a mapping function.
__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)
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
DynList< T > maps(const C &c, Op op)
Classic map operation.
Lazy and scalable dynamic array implementation.