GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
BitBuffer.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_DATA_BIT_BUFFER_HPP
5#define GREM_CORE_DATA_BIT_BUFFER_HPP
6
8
10
11#include <algorithm> // std::min, std::max, std::rotate, std::equal, std::lexicographical_compare, std::remove, std::remove_if, std::copy, std::move, std::fill_n
12#include <bit> // std::countr_zero
13#include <cstddef> // std::size_t, std::max_align_t
14#include <cstring> // std::memcpy
15#include <initializer_list> // std::initializer_list
16#include <iterator> // std::reverse_iterator, std::input_iterator, std::sentinel_for, std::iterator_traits, std::random_access_iterator_tag, std::begin, std::end
17#include <limits> // std::numeric_limits
18#include <memory> // std::allocator, std::allocator_traits, std::to_address, std::uninitialized_...
19#include <memory_resource> // std::pmr::polymorphic_allocator
20#include <stdexcept> // std::out_of_range, std::length_error
21#include <stdlib.h> // malloc, realloc, free // NOLINT(modernize-deprecated-headers)
22#include <type_traits> // std::make_signed_t, std::is_..._v
23#include <utility> // std::move, std::forward, std::exchange, std::swap
24
25namespace grem {
26
27template <typename Allocator = std::allocator<bool>>
29private:
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;
33
34public:
35 using value_type = bool;
36 using allocator_type = Allocator;
37 using size_type = typename std::allocator_traits<IntegerAllocator>::size_type;
38 using difference_type = typename std::allocator_traits<IntegerAllocator>::difference_type;
39
40private:
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);
43
44public:
45 class reference {
46 public:
47 constexpr reference(Integer* bits, size_type bitIndex) noexcept
48 : bits(bits)
49 , bitIndex(bitIndex) {}
50
51 constexpr operator bool() const noexcept {
52 return ((*bits >> bitIndex) & 1) != 0;
53 }
54
55 constexpr reference& operator=(bool newValue) noexcept {
56 *bits = (*bits & ~(Integer{1} << bitIndex)) | (static_cast<Integer>(newValue) << bitIndex);
57 return *this;
58 }
59
60 constexpr const reference& operator=(bool newValue) const noexcept { // NOLINT(misc-unconventional-assign-operator, cppcoreguidelines-c-copy-assignment-signature)
61 *bits = (*bits & ~(Integer{1} << bitIndex)) | (static_cast<Integer>(newValue) << bitIndex);
62 return *this;
63 }
64
65 constexpr void flip() noexcept {
66 *bits ^= (Integer{1} << bitIndex);
67 }
68
69 private:
70 Integer* bits;
71 size_type bitIndex;
72 };
73
74 using const_reference = bool;
75
76 struct pointer {
78
79 [[nodiscard]] constexpr reference* operator->() noexcept {
80 return &ref;
81 }
82 };
83
86
87 [[nodiscard]] constexpr const_reference* operator->() noexcept {
88 return &ref;
89 }
90 };
91
92private:
93 template <bool Const>
94 class Iterator {
95 private:
96 static constexpr size_type INTEGER_BITS = BitBufferBase::INTEGER_BITS;
97 static constexpr size_type INTEGER_BIT_SHIFT = BitBufferBase::INTEGER_BIT_SHIFT;
98
99 using BitsPointer = std::conditional_t<Const, const Integer*, Integer*>;
100
101 public:
102 using difference_type = std::ptrdiff_t;
103 using value_type = bool;
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>;
107
108 Iterator() noexcept = default;
109
110 constexpr Iterator(BitsPointer bits, size_type bitIndex) noexcept
111 : bits(bits)
112 , bitIndex(bitIndex) {}
113
114 constexpr operator Iterator<true>() const noexcept requires(!Const) {
115 return Iterator<true>{bits, bitIndex};
116 }
117
118 [[nodiscard]] constexpr reference operator*() const {
119 if constexpr (Const) {
120 return ((*bits >> bitIndex) & 1) != 0;
121 } else {
122 return reference{bits, bitIndex};
123 }
124 }
125
126 [[nodiscard]] constexpr pointer operator->() const {
127 return pointer{**this};
128 }
129
130 [[nodiscard]] constexpr reference operator[](difference_type n) const {
131 return *(*this + n);
132 }
133
134 constexpr Iterator& operator++() {
135 if (++bitIndex == INTEGER_BITS) {
136 bitIndex = 0;
137 ++bits;
138 }
139 return *this;
140 }
141
142 constexpr Iterator& operator--() {
143 if (bitIndex-- == 0) {
144 bitIndex = INTEGER_BITS - 1;
145 --bits;
146 }
147 return *this;
148 }
149
150 constexpr Iterator operator++(int) {
151 Iterator old = *this;
152 ++*this;
153 return old;
154 }
155
156 constexpr Iterator operator--(int) {
157 Iterator old = *this;
158 --*this;
159 return old;
160 }
161
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);
166 return *this;
167 }
168
169 constexpr Iterator& operator-=(difference_type n) {
170 return *this += -n;
171 }
172
173 [[nodiscard]] friend constexpr Iterator operator+(Iterator a, difference_type b) {
174 return a += b;
175 }
176
177 [[nodiscard]] friend constexpr Iterator operator+(difference_type a, Iterator b) {
178 return b += a;
179 }
180
181 [[nodiscard]] friend constexpr Iterator operator-(Iterator a, difference_type b) {
182 return a -= b;
183 }
184
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));
188 }
189
190 [[nodiscard]] friend constexpr bool operator==(Iterator a, Iterator b) {
191 return a.bits == b.bits && a.bitIndex == b.bitIndex;
192 }
193
194 [[nodiscard]] friend constexpr auto operator<=>(Iterator a, Iterator b) {
195 return (a.bits == b.bits) ? a.bitIndex <=> b.bitIndex : a.bits <=> b.bits;
196 }
197
198 private:
199 BitsPointer bits;
200 size_type bitIndex;
201 };
202
203public:
204 using iterator = Iterator<false>;
205 using const_iterator = Iterator<true>;
206 using reverse_iterator = std::reverse_iterator<iterator>;
207 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
208
209 constexpr BitBufferBase() noexcept = default;
210
211 constexpr explicit BitBufferBase(const Allocator& allocator) noexcept
212 : allocator(allocator) {}
213
214 constexpr BitBufferBase(size_type count, bool value, const Allocator& allocator = Allocator())
215 : BitBufferBase(allocator) {
216 resize(count, value);
217 }
218
219 constexpr explicit BitBufferBase(size_type count, const Allocator& allocator = Allocator())
220 : BitBufferBase(allocator) {
221 resize(count);
222 }
223
224 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
225 constexpr BitBufferBase(InputIterator first, Sentinel last, const Allocator& allocator = Allocator())
226 : BitBufferBase(allocator) {
227 assign(first, last);
228 }
229
230 constexpr BitBufferBase(std::initializer_list<bool> ilist, const Allocator& allocator = Allocator())
231 : BitBufferBase(ilist.begin(), ilist.end(), allocator) {}
232
233 constexpr ~BitBufferBase() {
234 reset();
235 }
236
237 constexpr BitBufferBase(const BitBufferBase& other)
238 : BitBufferBase(other, std::allocator_traits<Allocator>::select_on_container_copy_construction(other.get_allocator())) {}
239
240 constexpr BitBufferBase(const BitBufferBase& other, const Allocator& allocator)
241 : BitBufferBase(other.begin(), other.end(), allocator) {}
242
243 constexpr BitBufferBase(BitBufferBase&& other) noexcept
244 : BitBufferBase(std::move(other), other.get_allocator()) {}
245
246 constexpr BitBufferBase(BitBufferBase&& other, const Allocator& allocator) noexcept // NOLINT(cppcoreguidelines-rvalue-reference-param-not-moved)
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) {}
251
252 constexpr BitBufferBase& operator=(const BitBufferBase& other) {
253 if (this == &other) {
254 return *this;
255 }
256 if constexpr (std::allocator_traits<Allocator>::propagate_on_container_copy_assignment::value) {
257 if constexpr (!std::allocator_traits<Allocator>::is_always_equal::value) {
258 if (allocator != other.get_allocator()) {
259 reset();
260 }
261 }
262 allocator = other.get_allocator();
263 }
264 assign(other.begin(), other.end());
265 return *this;
266 }
267
268 constexpr BitBufferBase& operator=(BitBufferBase&& other) noexcept(
269 std::allocator_traits<
270 Allocator>::propagate_on_container_move_assignment::value || // NOLINT(cppcoreguidelines-noexcept-move-operations, performance-noexcept-move-constructor)
271 std::allocator_traits<Allocator>::is_always_equal::value) {
272 if (this == &other) {
273 return *this;
274 }
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) {
277 clear();
278 reserve(other.size());
279 copyAppend(other.begin(), other.end());
280 other.clear();
281 return *this;
282 }
283 }
284 reset();
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;
290 }
291 return *this;
292 }
293
294 constexpr BitBufferBase& operator=(std::initializer_list<bool> ilist) noexcept {
295 assign(ilist);
296 return *this;
297 }
298
299 constexpr void assign(size_type count, bool value) {
300 while (size() > count) {
301 pop_back();
302 }
303 reserve(count);
304 std::fill_n(begin(), size(), value);
305 for (size_type index = size(); index < count; ++index) {
306 push_back(value);
307 }
308 }
309
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>) {
313 const size_type count = static_cast<size_type>(last - first);
314 while (size() > count) {
315 pop_back();
316 }
317 reserve(count);
318 for (size_type index = 0; index < size(); ++index) {
319 begin()[index] = *first++;
320 }
321 copyAppend(first, last);
322 } else {
323 clear();
324 copyAppend(first, last);
325 }
326 }
327
328 constexpr void assign(std::initializer_list<bool> ilist) {
329 assign(ilist.begin(), ilist.end());
330 }
331
332 template <typename R>
333 constexpr void assign_range(R&& r) { // NOLINT(cppcoreguidelines-missing-std-forward)
334 assign(std::begin(r), std::end(r));
335 }
336
337 [[nodiscard]] allocator_type get_allocator() const noexcept {
338 return allocator;
339 }
340
341 [[nodiscard]] constexpr reference at(size_type pos) {
342 if (pos >= size()) {
343 throw std::out_of_range{"pos >= size()"};
344 }
345 return begin()[pos];
346 }
347
348 [[nodiscard]] constexpr const_reference at(size_type pos) const {
349 if (pos >= size()) {
350 throw std::out_of_range{"pos >= size()"};
351 }
352 return begin()[pos];
353 }
354
355 [[nodiscard]] constexpr reference operator[](size_type pos) {
356 GREM_ASSERT(pos < size());
357 return begin()[static_cast<difference_type>(pos)];
358 }
359
360 [[nodiscard]] constexpr const_reference operator[](size_type pos) const {
361 GREM_ASSERT(pos < size());
362 return begin()[static_cast<difference_type>(pos)];
363 }
364
365 [[nodiscard]] constexpr reference front() {
366 GREM_ASSERT(!empty());
367 return *begin();
368 }
369
370 [[nodiscard]] constexpr const_reference front() const {
371 GREM_ASSERT(!empty());
372 return *begin();
373 }
374
375 [[nodiscard]] constexpr reference back() {
376 GREM_ASSERT(!empty());
377 return begin()[static_cast<difference_type>(size() - 1)];
378 }
379
380 [[nodiscard]] constexpr const_reference back() const {
381 GREM_ASSERT(!empty());
382 return begin()[static_cast<difference_type>(size() - 1)];
383 }
384
385 [[nodiscard]] constexpr iterator begin() noexcept {
386 return iterator{bits, 0};
387 }
388
389 [[nodiscard]] constexpr const_iterator begin() const noexcept {
390 return const_iterator{bits, 0};
391 }
392
393 [[nodiscard]] constexpr const_iterator cbegin() const noexcept {
394 return begin();
395 }
396
397 [[nodiscard]] constexpr iterator end() noexcept {
398 const size_type n = size();
399 return iterator{bits + n / INTEGER_BITS, n % INTEGER_BITS};
400 }
401
402 [[nodiscard]] constexpr const_iterator end() const noexcept {
403 const size_type n = size();
404 return const_iterator{bits + n / INTEGER_BITS, n % INTEGER_BITS};
405 }
406
407 [[nodiscard]] constexpr const_iterator cend() const noexcept {
408 return end();
409 }
410
411 [[nodiscard]] constexpr reverse_iterator rbegin() noexcept {
412 return reverse_iterator{end()};
413 }
414
415 [[nodiscard]] constexpr const_reverse_iterator rbegin() const noexcept {
416 return const_reverse_iterator{end()};
417 }
418
419 [[nodiscard]] constexpr const_reverse_iterator crbegin() const noexcept {
420 return rbegin();
421 }
422
423 [[nodiscard]] constexpr reverse_iterator rend() noexcept {
424 return reverse_iterator{begin()};
425 }
426
427 [[nodiscard]] constexpr const_reverse_iterator rend() const noexcept {
429 }
430
431 [[nodiscard]] constexpr const_reverse_iterator crend() const noexcept {
432 return rend();
433 }
434
435 [[nodiscard]] constexpr bool empty() const noexcept {
436 return bitCount == 0;
437 }
438
439 [[nodiscard]] constexpr size_type size() const noexcept {
440 return bitCount;
441 }
442
443 [[nodiscard]] constexpr size_type max_size() const noexcept {
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)));
446 const size_type maxBitCount =
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()});
449 }
450
451 constexpr void reserve(size_type newCapacity) {
452 if (newCapacity > capacity()) {
453 if (newCapacity > max_size()) {
454 throw std::length_error{"newCapacity > max_size()"};
455 }
456 *this = BitBufferBase(cbegin(), cend(), newCapacity, allocator);
457 }
458 }
459
460 [[nodiscard]] constexpr size_type capacity() const noexcept {
461 return bitCapacity;
462 }
463
464 constexpr void shrink_to_fit() {
465 if (size() < capacity()) {
466 *this = BitBufferBase(cbegin(), cend(), size(), allocator);
467 }
468 }
469
470 constexpr void clear() noexcept {
471 bitCount = 0;
472 }
473
474 constexpr iterator insert(const_iterator pos, bool value) {
475 return emplace(pos, value);
476 }
477
478 constexpr iterator insert(const_iterator pos, size_type count, bool value) {
479 const difference_type offset = pos - cbegin();
480 if (count > max_size() - size()) {
481 throw std::length_error{"count > max_size() - size()"};
482 }
483 reserveAtLeast(size() + count);
484 for (size_type i = 0; i < count; ++i) {
485 push_back(value);
486 }
487 const iterator it = begin() + offset;
488 std::rotate(it, end() - static_cast<difference_type>(count), end());
489 return it;
490 }
491
492 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
493 constexpr iterator insert(const_iterator pos, InputIterator first, Sentinel last) {
494 const difference_type offset = pos - cbegin();
495 copyAppend(first, last);
496 const iterator it = begin() + offset;
497 const size_type count = size() - static_cast<size_type>(offset);
498 std::rotate(it, end() - static_cast<difference_type>(count), end());
499 return it;
500 }
501
502 constexpr iterator insert(const_iterator pos, std::initializer_list<bool> ilist) {
503 return insert(pos, ilist.begin(), ilist.end());
504 }
505
506 template <typename R>
507 constexpr iterator insert_range(const_iterator pos, R&& r) { // NOLINT(cppcoreguidelines-missing-std-forward)
508 return insert(pos, std::begin(r), std::end(r));
509 }
510
511 template <typename R>
512 constexpr void append_range(R&& r) {
513 insert_range(end(), std::forward<R>(r));
514 }
515
516 template <typename... Args>
517 constexpr iterator emplace(const_iterator pos, Args&&... args) {
518 const difference_type offset = pos - cbegin();
519 emplace_back(std::forward<Args>(args)...);
520 const iterator it = begin() + offset;
521 std::rotate(it, end() - 1, end());
522 return it;
523 }
524
526 GREM_ASSERT(!empty());
527 const difference_type offset = pos - cbegin();
528 const iterator it = begin() + offset;
529 std::shift_left(it, end(), 1);
530 pop_back();
531 return it;
532 }
533
535 const size_type count = static_cast<size_type>(last - first);
536 const difference_type offset = first - cbegin();
537 const iterator it = begin() + offset;
538 GREM_ASSERT(count <= size());
539 bitCount -= count;
540 return it;
541 }
542
543 constexpr void push_back(bool value) {
544 emplace_back(value);
545 }
546
547 template <typename... Args>
548 constexpr reference emplace_back(Args&&... args) {
549 reserveAtLeast(size() + 1);
550 reference result = begin()[bitCount];
551 result = bool(std::forward<Args>(args)...);
552 ++bitCount;
553 return result;
554 }
555
556 constexpr void pop_back() {
557 GREM_ASSERT(!empty());
558 --bitCount;
559 }
560
561 constexpr void resize(size_type count) {
562 if (count < size()) {
563 bitCount = count;
564 } else if (count > size()) {
565 reserveAtLeast(count);
566 bitCount = count;
567 }
568 }
569
570 constexpr void resize(size_type count, bool value) {
571 if (count < size()) {
572 bitCount = count;
573 } else if (count > size()) {
574 reserveAtLeast(count);
575 while (bitCount < count) {
576 begin()[static_cast<difference_type>(bitCount)] = value;
577 ++bitCount;
578 }
579 }
580 }
581
582 constexpr void swap(BitBufferBase& other) noexcept(
583 std::allocator_traits<Allocator>::propagate_on_container_swap::value || // NOLINT(cppcoreguidelines-noexcept-swap, performance-noexcept-swap)
584 std::allocator_traits<Allocator>::is_always_equal::value) {
585 if (this == &other) {
586 return;
587 }
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()"};
591 }
592 }
593 using std::swap;
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);
599 }
600 }
601
602 [[nodiscard]] bool operator==(const BitBufferBase& other) const {
603 return std::equal(begin(), end(), other.begin(), other.end());
604 }
605
606 [[nodiscard]] bool operator<(const BitBufferBase& other) const {
607 return std::lexicographical_compare(begin(), end(), other.begin(), other.end());
608 }
609
610 [[nodiscard]] bool operator<=(const BitBufferBase& other) const {
611 return !(other < *this);
612 }
613
614 [[nodiscard]] bool operator>(const BitBufferBase& other) const {
615 return other < *this;
616 }
617
618 [[nodiscard]] bool operator>=(const BitBufferBase& other) const {
619 return !(*this < other);
620 }
621
622 friend constexpr void swap(BitBufferBase& a, BitBufferBase& b) noexcept {
623 a.swap(b);
624 }
625
626 friend size_type erase(BitBufferBase& c, bool value) {
627 const iterator it = std::remove(c.begin(), c.end(), value);
628 const size_type result = static_cast<size_type>(c.end() - it);
629 c.erase(it, c.end());
630 return result;
631 }
632
633 template <typename Predicate>
634 friend size_type erase_if(BitBufferBase& c, Predicate predicate) {
635 const iterator it = std::remove_if(c.begin(), c.end(), predicate);
636 const size_type result = static_cast<size_type>(c.end() - it);
637 c.erase(it, c.end());
638 return result;
639 }
640
641private:
642 template <std::input_iterator InputIterator, std::sentinel_for<InputIterator> Sentinel>
643 BitBufferBase(InputIterator first, Sentinel last, size_type capacity, const Allocator& allocator)
644 : BitBufferBase(allocator) {
645 bits =
646 std::allocator_traits<IntegerAllocator>::allocate(this->allocator, (capacity + INTEGER_BITS - 1) / INTEGER_BITS); // NOLINT(cppcoreguidelines-prefer-member-initializer)
647 bitCapacity = capacity; // NOLINT(cppcoreguidelines-prefer-member-initializer)
648 copyAppend(first, last);
649 }
650
651 void reset() noexcept {
652 if (bits) {
653 std::allocator_traits<IntegerAllocator>::deallocate(allocator, bits, (bitCapacity + INTEGER_BITS - 1) / INTEGER_BITS);
654 bits = nullptr;
655 bitCount = 0;
656 bitCapacity = 0;
657 }
658 }
659
660 void reserveAtLeast(size_type newCapacity) {
661 if (const size_type cap = capacity(); newCapacity > cap) {
662 const size_type maxSize = max_size();
663 const size_type halfCap = cap / 2;
664 if (newCapacity > maxSize || halfCap > maxSize - cap) {
665 throw std::length_error{"newCapacity > max_size()"};
666 }
667 newCapacity = std::max(newCapacity, cap + halfCap);
668 *this = BitBufferBase(cbegin(), cend(), newCapacity, allocator);
669 }
670 }
671
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>) {
675 const size_type count = static_cast<size_type>(last - first);
676 if (count == 0) {
677 return;
678 }
679 const size_type offset = size();
680 if (count > max_size() - offset) {
681 throw std::length_error{"count > max_size() - size()"};
682 }
683 const size_type newSize = offset + count;
684 reserveAtLeast(newSize);
685 std::copy(first, last, begin() + static_cast<difference_type>(offset));
686 bitCount = newSize;
687 } else {
688 while (first != last) {
689 push_back(*first++);
690 }
691 }
692 }
693
694 IntegerPointer bits = nullptr;
695 size_type bitCount = 0;
696 size_type bitCapacity = 0;
697 [[no_unique_address]] IntegerAllocator allocator;
698};
699
701
702} // namespace grem
703
704namespace grem::pmr {
705
707
708} // namespace grem::pmr
709
710#endif
#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
Definition aliases.hpp:9
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