4#ifndef GREM_CORE_DATA_BIT_BUFFER_HPP
5#define GREM_CORE_DATA_BIT_BUFFER_HPP
15#include <initializer_list>
19#include <memory_resource>
27template <
typename Allocator = std::allocator<
bool>>
30 using Integer = std::size_t;
31 using IntegerAllocator =
typename std::allocator_traits<Allocator>::template rebind_alloc<Integer>;
32 using IntegerPointer =
typename std::allocator_traits<IntegerAllocator>::pointer;
37 using size_type =
typename std::allocator_traits<IntegerAllocator>::size_type;
38 using difference_type =
typename std::allocator_traits<IntegerAllocator>::difference_type;
41 static constexpr size_type INTEGER_BITS = std::numeric_limits<Integer>::digits;
42 static constexpr size_type INTEGER_BIT_SHIFT = std::countr_zero(INTEGER_BITS);
49 , bitIndex(bitIndex) {}
51 constexpr operator bool() const noexcept {
52 return ((*bits >> bitIndex) & 1) != 0;
56 *bits = (*bits & ~(Integer{1} << bitIndex)) | (
static_cast<Integer
>(newValue) << bitIndex);
61 *bits = (*bits & ~(Integer{1} << bitIndex)) | (
static_cast<Integer
>(newValue) << bitIndex);
65 constexpr void flip() noexcept {
66 *bits ^= (Integer{1} << bitIndex);
96 static constexpr size_type INTEGER_BITS = BitBufferBase::INTEGER_BITS;
97 static constexpr size_type INTEGER_BIT_SHIFT = BitBufferBase::INTEGER_BIT_SHIFT;
99 using BitsPointer = std::conditional_t<Const, const Integer*, Integer*>;
104 using reference = std::conditional_t<Const, typename BitBufferBase::const_reference, typename BitBufferBase::reference>;
105 using iterator_category = std::random_access_iterator_tag;
106 using pointer = std::conditional_t<Const, typename BitBufferBase::const_pointer, typename BitBufferBase::pointer>;
108 Iterator() noexcept = default;
110 constexpr Iterator(BitsPointer bits,
size_type bitIndex) noexcept
112 , bitIndex(bitIndex) {}
114 constexpr operator Iterator<true>() const noexcept requires(!Const) {
115 return Iterator<true>{bits, bitIndex};
118 [[nodiscard]]
constexpr reference operator*()
const {
119 if constexpr (Const) {
120 return ((*bits >> bitIndex) & 1) != 0;
122 return reference{bits, bitIndex};
126 [[nodiscard]]
constexpr pointer operator->()
const {
127 return pointer{**
this};
130 [[nodiscard]]
constexpr reference operator[](difference_type n)
const {
134 constexpr Iterator& operator++() {
135 if (++bitIndex == INTEGER_BITS) {
142 constexpr Iterator& operator--() {
143 if (bitIndex-- == 0) {
144 bitIndex = INTEGER_BITS - 1;
150 constexpr Iterator operator++(
int) {
151 Iterator old = *
this;
156 constexpr Iterator operator--(
int) {
157 Iterator old = *
this;
162 constexpr Iterator& operator+=(difference_type n) {
163 bitIndex +=
static_cast<size_type>(
static_cast<std::make_signed_t<size_type>
>(n));
164 bits +=
static_cast<std::make_signed_t<size_type>
>(bitIndex) >> INTEGER_BIT_SHIFT;
165 bitIndex &= (INTEGER_BITS - 1);
169 constexpr Iterator& operator-=(difference_type n) {
173 [[nodiscard]]
friend constexpr Iterator operator+(Iterator a, difference_type b) {
177 [[nodiscard]]
friend constexpr Iterator operator+(difference_type a, Iterator b) {
181 [[nodiscard]]
friend constexpr Iterator operator-(Iterator a, difference_type b) {
185 [[nodiscard]]
friend constexpr difference_type operator-(Iterator a, Iterator b) {
186 return static_cast<difference_type
>(a.bits - b.bits) * difference_type{INTEGER_BITS} +
187 static_cast<difference_type
>(
static_cast<std::make_signed_t<size_type>
>(a.bitIndex - b.bitIndex));
190 [[nodiscard]]
friend constexpr bool operator==(Iterator a, Iterator b) {
191 return a.bits == b.bits && a.bitIndex == b.bitIndex;
194 [[nodiscard]]
friend constexpr auto operator<=>(Iterator a, Iterator b) {
195 return (a.bits == b.bits) ? a.bitIndex <=> b.bitIndex : a.bits <=> b.bits;
212 : allocator(allocator) {}
224 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
225 constexpr BitBufferBase(InputIterator first, Sentinel last,
const Allocator& allocator = Allocator())
230 constexpr BitBufferBase(std::initializer_list<bool> ilist,
const Allocator& allocator = Allocator())
247 : bits(std::exchange(other.bits,
nullptr))
248 , bitCount(std::exchange(other.bitCount,
size_type{0}))
249 , bitCapacity(std::exchange(other.bitCapacity,
size_type{0}))
250 , allocator(allocator) {}
253 if (
this == &other) {
256 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_copy_assignment::value) {
257 if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
269 std::allocator_traits<
270 Allocator>::propagate_on_container_move_assignment::value ||
271 std::allocator_traits<Allocator>::is_always_equal::value) {
272 if (
this == &other) {
275 if constexpr (!std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value && !std::allocator_traits<Allocator>::is_always_equal::value) {
276 if (allocator != other.allocator) {
279 copyAppend(other.begin(), other.end());
285 bits = std::exchange(other.bits,
nullptr);
286 bitCount = std::exchange(other.bitCount,
size_type{0});
287 bitCapacity = std::exchange(other.bitCapacity,
size_type{0});
288 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_move_assignment::value) {
289 allocator = other.allocator;
310 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
311 constexpr void assign(InputIterator first, Sentinel last) {
312 if constexpr (std::is_convertible_v<typename std::iterator_traits<InputIterator>::iterator_category, std::random_access_iterator_tag>) {
319 begin()[index] = *first++;
321 copyAppend(first, last);
324 copyAppend(first, last);
328 constexpr void assign(std::initializer_list<bool> ilist) {
329 assign(ilist.begin(), ilist.end());
332 template <
typename R>
334 assign(std::begin(r), std::end(r));
343 throw std::out_of_range{
"pos >= size()"};
350 throw std::out_of_range{
"pos >= size()"};
365 [[nodiscard]]
constexpr reference
front() {
375 [[nodiscard]]
constexpr reference
back() {
399 return iterator{bits + n / INTEGER_BITS, n % INTEGER_BITS};
435 [[nodiscard]]
constexpr bool empty() const noexcept {
436 return bitCount == 0;
444 const size_type maxIntegerCount = std::min(
static_cast<size_type>(std::numeric_limits<size_type>::max() /
sizeof(Integer)),
445 static_cast<size_type>(std::allocator_traits<IntegerAllocator>::max_size(allocator)));
447 (maxIntegerCount >= std::numeric_limits<size_type>::max() / INTEGER_BITS) ? std::numeric_limits<size_type>::max() : maxIntegerCount * INTEGER_BITS;
448 return std::min(maxBitCount,
size_type{std::numeric_limits<difference_type>::max()});
454 throw std::length_error{
"newCapacity > max_size()"};
481 throw std::length_error{
"count > max_size() - size()"};
492 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
495 copyAppend(first, last);
503 return insert(pos, ilist.begin(), ilist.end());
506 template <
typename R>
508 return insert(pos, std::begin(r), std::end(r));
511 template <
typename R>
516 template <
typename... Args>
521 std::rotate(it,
end() - 1,
end());
529 std::shift_left(it,
end(), 1);
547 template <
typename... Args>
549 reserveAtLeast(
size() + 1);
550 reference result =
begin()[bitCount];
551 result = bool(std::forward<Args>(args)...);
565 reserveAtLeast(
count);
574 reserveAtLeast(
count);
575 while (bitCount <
count) {
583 std::allocator_traits<Allocator>::propagate_on_container_swap::value ||
584 std::allocator_traits<Allocator>::is_always_equal::value) {
585 if (
this == &other) {
588 if constexpr (!std::allocator_traits<Allocator>::propagate_on_container_swap::value && !std::allocator_traits<Allocator>::is_always_equal::value) {
589 if (allocator != other.allocator) {
590 throw std::logic_error{
"get_allocator() != other.get_allocator()"};
594 swap(bits, other.bits);
595 swap(bitCount, other.bitCount);
596 swap(bitCapacity, other.bitCapacity);
597 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_swap::value) {
598 swap(allocator, other.allocator);
607 return std::lexicographical_compare(
begin(),
end(), other.
begin(), other.
end());
611 return !(other < *
this);
615 return other < *
this;
619 return !(*
this < other);
633 template <
typename Predicate>
642 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
646 std::allocator_traits<IntegerAllocator>::allocate(this->allocator, (
capacity + INTEGER_BITS - 1) / INTEGER_BITS);
648 copyAppend(first, last);
651 void reset() noexcept {
653 std::allocator_traits<IntegerAllocator>::deallocate(allocator, bits, (bitCapacity + INTEGER_BITS - 1) / INTEGER_BITS);
660 void reserveAtLeast(
size_type newCapacity) {
664 if (newCapacity > maxSize || halfCap > maxSize - cap) {
665 throw std::length_error{
"newCapacity > max_size()"};
667 newCapacity = std::max(newCapacity, cap + halfCap);
672 template <
typename InputIterator,
typename Sentinel>
673 void copyAppend(InputIterator first, Sentinel last) {
674 if constexpr (std::is_convertible_v<typename std::iterator_traits<InputIterator>::iterator_category, std::random_access_iterator_tag>) {
681 throw std::length_error{
"count > max_size() - size()"};
684 reserveAtLeast(newSize);
688 while (first != last) {
694 IntegerPointer bits =
nullptr;
697 [[no_unique_address]] IntegerAllocator allocator;
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Definition BitBuffer.hpp:45
constexpr const reference & operator=(bool newValue) const noexcept
Definition BitBuffer.hpp:60
constexpr void flip() noexcept
Definition BitBuffer.hpp:65
constexpr reference & operator=(bool newValue) noexcept
Definition BitBuffer.hpp:55
constexpr reference(Integer *bits, size_type bitIndex) noexcept
Definition BitBuffer.hpp:47
Definition BitBuffer.hpp:28
constexpr void assign(std::initializer_list< bool > ilist)
Definition BitBuffer.hpp:328
constexpr size_type capacity() const noexcept
Definition BitBuffer.hpp:460
constexpr BitBufferBase(InputIterator first, Sentinel last, const Allocator &allocator=Allocator())
Definition BitBuffer.hpp:225
constexpr iterator emplace(const_iterator pos, Args &&... args)
Definition BitBuffer.hpp:517
constexpr const_reverse_iterator crbegin() const noexcept
Definition BitBuffer.hpp:419
constexpr BitBufferBase(size_type count, bool value, const Allocator &allocator=Allocator())
Definition BitBuffer.hpp:214
constexpr void assign(InputIterator first, Sentinel last)
Definition BitBuffer.hpp:311
constexpr BitBufferBase & operator=(const BitBufferBase &other)
Definition BitBuffer.hpp:252
constexpr const_reference back() const
Definition BitBuffer.hpp:380
constexpr reference operator[](size_type pos)
Definition BitBuffer.hpp:355
constexpr BitBufferBase(std::initializer_list< bool > ilist, const Allocator &allocator=Allocator())
Definition BitBuffer.hpp:230
constexpr void resize(size_type count)
Definition BitBuffer.hpp:561
constexpr BitBufferBase & operator=(std::initializer_list< bool > ilist) noexcept
Definition BitBuffer.hpp:294
constexpr reference emplace_back(Args &&... args)
Definition BitBuffer.hpp:548
constexpr iterator erase(const_iterator first, const_iterator last)
Definition BitBuffer.hpp:534
Allocator allocator_type
Definition BitBuffer.hpp:36
constexpr void append_range(R &&r)
Definition BitBuffer.hpp:512
constexpr iterator insert(const_iterator pos, size_type count, bool value)
Definition BitBuffer.hpp:478
bool operator<=(const BitBufferBase &other) const
Definition BitBuffer.hpp:610
constexpr BitBufferBase() noexcept=default
constexpr BitBufferBase(BitBufferBase &&other) noexcept
Definition BitBuffer.hpp:243
constexpr BitBufferBase & operator=(BitBufferBase &&other) noexcept(std::allocator_traits< Allocator >::propagate_on_container_move_assignment::value||//NOLINT(cppcoreguidelines-noexcept-move-operations, performance-noexcept-move-constructor) std::allocator_traits< Allocator >::is_always_equal::value)
Definition BitBuffer.hpp:268
constexpr reverse_iterator rend() noexcept
Definition BitBuffer.hpp:423
constexpr iterator erase(const_iterator pos)
Definition BitBuffer.hpp:525
bool operator<(const BitBufferBase &other) const
Definition BitBuffer.hpp:606
friend size_type erase(BitBufferBase &c, bool value)
Definition BitBuffer.hpp:626
constexpr const_reference at(size_type pos) const
Definition BitBuffer.hpp:348
constexpr const_reference operator[](size_type pos) const
Definition BitBuffer.hpp:360
constexpr const_iterator end() const noexcept
Definition BitBuffer.hpp:402
constexpr iterator end() noexcept
Definition BitBuffer.hpp:397
constexpr BitBufferBase(const BitBufferBase &other, const Allocator &allocator)
Definition BitBuffer.hpp:240
constexpr const_reverse_iterator rend() const noexcept
Definition BitBuffer.hpp:427
constexpr void shrink_to_fit()
Definition BitBuffer.hpp:464
constexpr const_reverse_iterator crend() const noexcept
Definition BitBuffer.hpp:431
constexpr reference front()
Definition BitBuffer.hpp:365
constexpr iterator insert(const_iterator pos, bool value)
Definition BitBuffer.hpp:474
std::reverse_iterator< iterator > reverse_iterator
Definition BitBuffer.hpp:206
bool operator==(const BitBufferBase &other) const
Definition BitBuffer.hpp:602
constexpr void swap(BitBufferBase &other) noexcept(std::allocator_traits< Allocator >::propagate_on_container_swap::value||//NOLINT(cppcoreguidelines-noexcept-swap, performance-noexcept-swap) std::allocator_traits< Allocator >::is_always_equal::value)
Definition BitBuffer.hpp:582
constexpr void assign_range(R &&r)
Definition BitBuffer.hpp:333
constexpr void clear() noexcept
Definition BitBuffer.hpp:470
constexpr const_reference front() const
Definition BitBuffer.hpp:370
constexpr iterator insert_range(const_iterator pos, R &&r)
Definition BitBuffer.hpp:507
constexpr const_iterator begin() const noexcept
Definition BitBuffer.hpp:389
bool const_reference
Definition BitBuffer.hpp:74
constexpr reverse_iterator rbegin() noexcept
Definition BitBuffer.hpp:411
constexpr bool empty() const noexcept
Definition BitBuffer.hpp:435
friend size_type erase_if(BitBufferBase &c, Predicate predicate)
Definition BitBuffer.hpp:634
constexpr const_reverse_iterator rbegin() const noexcept
Definition BitBuffer.hpp:415
constexpr const_iterator cbegin() const noexcept
Definition BitBuffer.hpp:393
constexpr size_type size() const noexcept
Definition BitBuffer.hpp:439
constexpr iterator begin() noexcept
Definition BitBuffer.hpp:385
friend constexpr void swap(BitBufferBase &a, BitBufferBase &b) noexcept
Definition BitBuffer.hpp:622
constexpr void push_back(bool value)
Definition BitBuffer.hpp:543
typename std::allocator_traits< IntegerAllocator >::size_type size_type
Definition BitBuffer.hpp:37
constexpr const_iterator cend() const noexcept
Definition BitBuffer.hpp:407
constexpr reference back()
Definition BitBuffer.hpp:375
constexpr iterator insert(const_iterator pos, InputIterator first, Sentinel last)
Definition BitBuffer.hpp:493
constexpr iterator insert(const_iterator pos, std::initializer_list< bool > ilist)
Definition BitBuffer.hpp:502
constexpr size_type max_size() const noexcept
Definition BitBuffer.hpp:443
allocator_type get_allocator() const noexcept
Definition BitBuffer.hpp:337
constexpr BitBufferBase(BitBufferBase &&other, const Allocator &allocator) noexcept
Definition BitBuffer.hpp:246
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition BitBuffer.hpp:207
bool operator>(const BitBufferBase &other) const
Definition BitBuffer.hpp:614
Iterator< false > iterator
Definition BitBuffer.hpp:204
Iterator< true > const_iterator
Definition BitBuffer.hpp:205
bool operator>=(const BitBufferBase &other) const
Definition BitBuffer.hpp:618
constexpr ~BitBufferBase()
Definition BitBuffer.hpp:233
constexpr void resize(size_type count, bool value)
Definition BitBuffer.hpp:570
constexpr BitBufferBase(const BitBufferBase &other)
Definition BitBuffer.hpp:237
constexpr void assign(size_type count, bool value)
Definition BitBuffer.hpp:299
constexpr reference at(size_type pos)
Definition BitBuffer.hpp:341
constexpr void pop_back()
Definition BitBuffer.hpp:556
bool value_type
Definition BitBuffer.hpp:35
constexpr BitBufferBase(size_type count, const Allocator &allocator=Allocator())
Definition BitBuffer.hpp:219
typename std::allocator_traits< IntegerAllocator >::difference_type difference_type
Definition BitBuffer.hpp:38
constexpr void reserve(size_type newCapacity)
Definition BitBuffer.hpp:451
Definition concepts.hpp:106
Definition Allocation.hpp:554
grem::BitBufferBase< std::pmr::polymorphic_allocator< bool > > BitBuffer
Definition BitBuffer.hpp:706
constexpr auto count(input_range auto &&range, const auto &value)
Definition algorithms.hpp:234
BitBufferBase<> BitBuffer
Definition BitBuffer.hpp:700
Definition BitBuffer.hpp:84
const_reference ref
Definition BitBuffer.hpp:85
constexpr const_reference * operator->() noexcept
Definition BitBuffer.hpp:87
reference ref
Definition BitBuffer.hpp:77
constexpr reference * operator->() noexcept
Definition BitBuffer.hpp:79