NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
bitmap.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_CONTAINER_BITMAP_HPP__
2#define NEFORCE_CORE_CONTAINER_BITMAP_HPP__
3
12
15NEFORCE_BEGIN_NAMESPACE__
16
22
24NEFORCE_INLINE17 constexpr uint32_t BITMAP_WORD_SIZE = 8 * sizeof(uint32_t);
25
33struct bit_reference : icommon<bit_reference>, istringify<bit_reference> {
34private:
35 uint32_t* ptr_ = nullptr;
36 uint32_t mask_ = 0;
37
38public:
39 NEFORCE_CONSTEXPR20 bit_reference() = default;
40 NEFORCE_CONSTEXPR20 ~bit_reference() = default;
41
47 NEFORCE_CONSTEXPR20 bit_reference(uint32_t* ptr, const uint32_t mask) noexcept :
48 ptr_(ptr),
49 mask_(mask) {}
50
55 NEFORCE_CONSTEXPR20 bit_reference(const bit_reference& other) noexcept :
56 ptr_(other.ptr_),
57 mask_(other.mask_) {}
58
64 NEFORCE_CONSTEXPR20 bit_reference& operator=(const bit_reference& other) noexcept {
65 if (addressof(other) == this) {
66 return *this;
67 }
68 return *this = static_cast<bool>(other);
69 }
70
75 NEFORCE_CONSTEXPR20 bit_reference(bit_reference&& other) noexcept :
76 ptr_(other.ptr_),
77 mask_(other.mask_) {
78 other.ptr_ = nullptr;
79 other.mask_ = 0;
80 }
81
87 NEFORCE_CONSTEXPR20 bit_reference& operator=(bit_reference&& other) noexcept {
88 if (addressof(other) == this) {
89 return *this;
90 }
91 *this = static_cast<bool>(other);
92 other.ptr_ = nullptr;
93 other.mask_ = 0;
94 return *this;
95 }
96
102 NEFORCE_CONSTEXPR20 bit_reference& operator=(const bool value) noexcept {
103 if (ptr_ == nullptr) {
104 return *this;
105 }
106 *ptr_ = (*ptr_ & ~mask_) | (value ? mask_ : 0);
107 return *this;
108 }
109
114 NEFORCE_CONSTEXPR20 explicit operator bool() const noexcept {
115 if (ptr_ == nullptr) {
116 return false;
117 }
118 return (*ptr_ & mask_) != 0U;
119 }
120
124 NEFORCE_CONSTEXPR20 void flip() const noexcept {
125 if (ptr_ == nullptr) {
126 return;
127 }
128 *ptr_ ^= mask_;
129 }
130
135 NEFORCE_CONSTEXPR20 void swap(bit_reference& other) noexcept {
136 if (_NEFORCE addressof(other) == this) {
137 return;
138 }
139 const bool tmp = static_cast<bool>(other);
140 other = *this;
141 *this = tmp;
142 }
143
149 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 bool equal_to(const bit_reference& rhs) const noexcept {
150 return static_cast<bool>(*this) == static_cast<bool>(rhs);
151 }
152
158 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 bool less_than(const bit_reference& rhs) const noexcept {
159 return !static_cast<bool>(*this) && static_cast<bool>(rhs);
160 }
161
166 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 size_t to_hash() const noexcept {
167 return hash<bool>()(static_cast<bool>(*this));
168 }
169
174 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 string to_string() const { return static_cast<bool>(*this) ? "1"_s : "0"_s; }
175};
176
177
186template <bool IsConst, typename BitMap>
187struct bitmap_iterator : iiterator<bitmap_iterator<IsConst, BitMap>> {
188public:
189 using container_type = BitMap;
190 using value_type = typename container_type::value_type;
191 using size_type = typename container_type::size_type;
192 using difference_type = typename container_type::difference_type;
194 using reference = conditional_t<IsConst, typename container_type::const_reference,
195 typename container_type::reference>;
196 using pointer = conditional_t<IsConst, typename container_type::const_pointer,
197 typename container_type::pointer>;
198
199private:
200 uint32_t* ptr_ = nullptr;
201 uint32_t off_ = 0;
202 const container_type* container_ = nullptr;
203
204 friend class bitmap;
205 template <bool, typename>
206 friend struct bitmap_iterator;
207
208private:
209 template <typename Ref>
210 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 enable_if_t<is_boolean_v<Ref>, Ref> reference_dispatch() const noexcept {
211 return (*ptr_ & (1ULL << off_)) != 0;
212 }
213
214 template <typename Ref>
215 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 enable_if_t<!is_boolean_v<Ref>, Ref> reference_dispatch() const noexcept {
216 return Ref(ptr_, 1ULL << off_);
217 }
218
219public:
220 NEFORCE_CONSTEXPR20 bitmap_iterator() noexcept = default;
221 NEFORCE_CONSTEXPR20 ~bitmap_iterator() = default;
222
223 NEFORCE_CONSTEXPR20 bitmap_iterator(const bitmap_iterator&) noexcept = default;
224 NEFORCE_CONSTEXPR20 bitmap_iterator& operator=(const bitmap_iterator&) noexcept = default;
225 NEFORCE_CONSTEXPR20 bitmap_iterator(bitmap_iterator&&) noexcept = default;
226 NEFORCE_CONSTEXPR20 bitmap_iterator& operator=(bitmap_iterator&&) noexcept = default;
227
232 explicit NEFORCE_CONSTEXPR20 bitmap_iterator(const container_type* bm) noexcept :
233 container_(bm) {}
234
241 NEFORCE_CONSTEXPR20 bitmap_iterator(uint32_t* ptr, const uint32_t offset, const container_type* bm) noexcept :
242 ptr_(ptr),
243 off_(offset),
244 container_(bm) {}
245
251 template <bool IsConst2>
252 NEFORCE_CONSTEXPR20 explicit bitmap_iterator(const bitmap_iterator<IsConst2, BitMap>& other) noexcept :
253 ptr_(other.ptr_),
254 off_(other.off_),
255 container_(other.container_) {}
256
261 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 reference dereference() const noexcept {
262 NEFORCE_DEBUG_VERIFY(ptr_ && container_, "Attempting to dereference on a null pointer");
263 return reference_dispatch<reference>();
264 }
265
269 NEFORCE_CONSTEXPR20 void increment() noexcept {
270 NEFORCE_DEBUG_VERIFY(ptr_ && container_, "Attempting to increment a null pointer");
271 if (off_++ == BITMAP_WORD_SIZE - 1) {
272 off_ = 0;
273 ++ptr_;
274 }
275 }
276
280 NEFORCE_CONSTEXPR20 void decrement() noexcept {
281 NEFORCE_DEBUG_VERIFY(ptr_ && container_, "Attempting to increment a null pointer");
282 if (off_-- == 0) {
283 off_ = BITMAP_WORD_SIZE - 1;
284 --ptr_;
285 }
286 }
287
292 NEFORCE_CONSTEXPR20 void advance(difference_type off) noexcept {
293 NEFORCE_DEBUG_VERIFY((off == 0 || (ptr_ != nullptr && container_ != nullptr)),
294 "Attempting to advance a null pointer");
295 difference_type n = off + static_cast<difference_type>(off_);
296 ptr_ += n / static_cast<difference_type>(BITMAP_WORD_SIZE);
297 n = n % static_cast<difference_type>(BITMAP_WORD_SIZE);
298 if (n < 0) {
299 off_ = static_cast<uint32_t>(n + BITMAP_WORD_SIZE);
300 --ptr_;
301 } else {
302 off_ = static_cast<uint32_t>(n);
303 }
304 }
305
311 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 difference_type distance_to(const bitmap_iterator& other) const noexcept {
312 NEFORCE_DEBUG_VERIFY(container_ == other.container_, "Attempting to distance to a different container");
313 return BITMAP_WORD_SIZE * (ptr_ - other.ptr_) + static_cast<difference_type>(off_) -
314 static_cast<difference_type>(other.off_);
315 }
316
322 NEFORCE_CONSTEXPR20 reference operator[](const difference_type n) const noexcept { return *(*this + n); }
323
329 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 bool equal_to(const bitmap_iterator& rhs) const noexcept {
330 NEFORCE_DEBUG_VERIFY(container_ == rhs.container_, "Attempting to equal to a different container");
331 return ptr_ == rhs.ptr_ && off_ == rhs.off_;
332 }
333
339 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 bool less_than(const bitmap_iterator& rhs) const noexcept {
340 NEFORCE_DEBUG_VERIFY(container_ == rhs.container_, "Attempting to equal to a different container");
341 return ptr_ < rhs.ptr_ || (ptr_ == rhs.ptr_ && off_ < rhs.off_);
342 }
343
348 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 decltype(auto) base() const noexcept { return ptr_; }
349
354 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 const container_type* container() const noexcept { return container_; }
355};
356
357
365class bitmap : public icomparable<bitmap> {
366public:
367 using value_type = bool;
370 using const_pointer = const bool*;
371 using const_reference = const bool;
379
380private:
387 struct bit_storage {
388 uint32_t* ptr = nullptr;
390
394 NEFORCE_CONSTEXPR20 bit_storage() noexcept = default;
395
400 explicit NEFORCE_CONSTEXPR20 bit_storage(const size_t word) {
401 if (word == 0) {
402 return;
403 }
404 ptr = cpair.get_base().allocate(word);
405 cpair.value = word;
406 memory_zero(ptr, word * sizeof(uint32_t));
407 }
408
412 NEFORCE_CONSTEXPR20 ~bit_storage() {
413 if (ptr != nullptr) {
414 cpair.get_base().deallocate(ptr, cpair.value);
415 }
416 }
417
418 bit_storage(const bit_storage&) = delete;
419 bit_storage& operator=(const bit_storage&) = delete;
420
425 NEFORCE_CONSTEXPR20 bit_storage(bit_storage&& other) noexcept :
426 ptr(other.ptr),
427 cpair(_NEFORCE move(other.cpair)) {
428 other.ptr = nullptr;
429 other.cpair.value = 0;
430 }
431
437 NEFORCE_CONSTEXPR20 bit_storage& operator=(bit_storage&& other) noexcept {
438 if (addressof(other) == this) {
439 return *this;
440 }
441
442 reset();
443 ptr = other.ptr;
444 cpair = _NEFORCE move(other.cpair);
445 other.ptr = nullptr;
446 other.cpair.value = 0;
447
448 return *this;
449 }
450
456 NEFORCE_CONSTEXPR20 void reset(uint32_t* new_ptr = nullptr, const size_t cap = 0) {
457 if (ptr != nullptr) {
458 cpair.get_base().deallocate(ptr, cpair.value);
459 }
460 ptr = new_ptr;
461 cpair.value = cap;
462 }
463
468 NEFORCE_CONSTEXPR20 void allocate(const size_t word) { reset(cpair.get_base().allocate(word), word); }
469
474 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 uint32_t* get() const noexcept { return ptr; }
475
480 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 size_t capacity() const noexcept { return cpair.value; }
481 };
482
483 iterator start_{this};
484 iterator finish_{this};
485 bit_storage storage_;
486
487private:
493 static constexpr size_t word_count(const size_type word) noexcept {
494 return (word + BITMAP_WORD_SIZE - 1) / BITMAP_WORD_SIZE;
495 }
496
501 NEFORCE_CONSTEXPR20 void allocate_storage(const size_type word) {
502 if (word == 0) {
503 return;
504 }
505 storage_.allocate(word_count(word));
506 }
507
512 NEFORCE_CONSTEXPR20 void set_iterators(const size_type word) noexcept {
513 start_ = iterator(storage_.get(), 0, this);
514 finish_ = start_ + static_cast<difference_type>(word);
515 }
516
526 template <typename Iterator1, typename Iterator2>
527 NEFORCE_CONSTEXPR20 Iterator2 bit_copy(Iterator1 first, Iterator1 last, Iterator2 result) {
528 iter_difference_t<Iterator1> n = _NEFORCE distance(first, last);
529 for (; n > 0; --n, ++first, ++result) {
530 *result = *first;
531 }
532 return result;
533 }
534
544 template <typename Iterator1, typename Iterator2>
545 NEFORCE_CONSTEXPR20 Iterator2 bit_copy_backward(Iterator1 first, Iterator1 last, Iterator2 result) {
546 iter_difference_t<Iterator1> n = _NEFORCE distance(first, last);
547 for (; n > 0; --n) {
548 *--result = *--last;
549 }
550 return result;
551 }
552
559 void reallocate_insert(const iterator& position, const size_type extra_len, const bool value) {
560 const size_type old_size = size();
561 const size_type new_size = old_size + extra_len;
562 const size_type new_words = word_count(new_size);
563 bit_storage new_storage(new_words);
564
565 const iterator new_start(new_storage.get(), 0, this);
566 auto new_finish = bitmap::bit_copy(begin(), position, new_start);
567 _NEFORCE fill_n(new_finish, static_cast<difference_type>(extra_len), value);
568 new_finish += static_cast<difference_type>(extra_len);
569 new_finish = bitmap::bit_copy(position, end(), new_finish);
570
571 storage_ = _NEFORCE move(new_storage);
572 start_ = new_start;
573 finish_ = new_finish;
574 }
575
584 template <typename Iterator>
585 void reallocate_insert_range(const iterator position, Iterator first, Iterator last, const size_type extra_len) {
586 const size_type old_size = size();
587 const size_type new_size = old_size + extra_len;
588 const size_type new_words = word_count(new_size);
589 bit_storage new_storage(new_words);
590
591 const iterator new_start(new_storage.get(), 0, this);
592 auto new_finish = bitmap::bit_copy(begin(), position, new_start);
593 new_finish = bitmap::bit_copy(first, last, new_finish);
594 new_finish = bitmap::bit_copy(position, end(), new_finish);
595
596 storage_ = _NEFORCE move(new_storage);
597 start_ = new_start;
598 finish_ = new_finish;
599 }
600
606 void insert_aux(const iterator& position, const bool value) {
607 if (finish_.ptr_ != storage_.get() + storage_.capacity()) {
608 bit_copy_backward(position, finish_, finish_ + 1);
609 *position = value;
610 ++finish_;
611 } else {
612 reallocate_insert(position, 1, value);
613 }
614 }
615
622 template <typename Iterator>
623 enable_if_t<!is_ranges_fwd_iter_v<Iterator>> range_init(Iterator first, Iterator last) {
624 if (first == last) {
625 return;
626 }
627
628 bitmap tmp{};
629 while (first != last) {
630 tmp.push_back(*first);
631 ++first;
632 }
633
634 bitmap::swap(tmp);
635 return;
636 }
637
644 template <typename Iterator>
645 enable_if_t<is_ranges_fwd_iter_v<Iterator>> range_init(Iterator first, Iterator last) {
646 if (first == last) {
647 return;
648 }
649
650 const size_type n = _NEFORCE distance(first, last);
651 bit_storage tmp_storage(word_count(n));
652 iterator tmp_start(tmp_storage.get(), 0, this);
653 const iterator tmp_finish = bitmap::bit_copy(first, last, tmp_start);
654
655 storage_ = _NEFORCE move(tmp_storage);
656 start_ = tmp_start;
657 finish_ = tmp_finish;
658 return;
659 }
660
668 template <typename Iterator>
669 enable_if_t<!is_ranges_fwd_iter_v<Iterator>> insert_range(iterator position, Iterator first, Iterator last) {
670 if (first == last) {
671 return;
672 }
673 bitmap tmp(first, last);
674 insert_range(position, tmp.begin(), tmp.end());
675 return;
676 }
677
685 template <typename Iterator>
686 enable_if_t<is_ranges_fwd_iter_v<Iterator>> insert_range(iterator position, Iterator first, Iterator last) {
687 if (first == last) {
688 return;
689 }
690 const size_type n = _NEFORCE distance(first, last);
691 if (capacity() - size() >= n) {
692 bitmap::bit_copy_backward(position, end(), finish_ + static_cast<difference_type>(n));
693 bitmap::bit_copy(first, last, position);
694 finish_ += static_cast<difference_type>(n);
695 } else {
696 bitmap::reallocate_insert_range(position, first, last, n);
697 }
698 return;
699 }
700
701public:
707 NEFORCE_CONSTEXPR20 bitmap() noexcept = default;
708
715 NEFORCE_CONSTEXPR20 explicit bitmap(const size_type word) {
716 if (word == 0) {
717 return;
718 }
719 allocate_storage(word);
720 set_iterators(word);
721 _NEFORCE fill_n(storage_.get(), static_cast<ptrdiff_t>(storage_.capacity()), 0);
722 }
723
729 NEFORCE_CONSTEXPR20 explicit bitmap(const size_type word, const bool value) {
730 if (word == 0) {
731 return;
732 }
733 allocate_storage(word);
734 set_iterators(word);
735 _NEFORCE fill(storage_.get(), storage_.get() + storage_.capacity(), value ? ~0U : 0U);
736 }
737
743 NEFORCE_CONSTEXPR20 explicit bitmap(const int32_t word, const bool value) :
744 bitmap(static_cast<size_type>(word), value) {}
745
751 NEFORCE_CONSTEXPR20 explicit bitmap(const int64_t word, const bool value) :
752 bitmap(static_cast<size_type>(word), value) {}
753
758 NEFORCE_CONSTEXPR20 bitmap(const bitmap& other) {
759 if (other.empty()) {
760 return;
761 }
762
763 const size_type n = other.size();
764 bit_storage tmp(word_count(n));
765 const iterator tmp_start(tmp.get(), 0, this);
766 const auto tmp_finish = bit_copy(other.cbegin(), other.cend(), tmp_start);
767
768 storage_ = _NEFORCE move(tmp);
769 start_ = tmp_start;
770 finish_ = tmp_finish;
771 }
772
778 NEFORCE_CONSTEXPR20 bitmap& operator=(const bitmap& other) {
779 if (_NEFORCE addressof(other) == this) {
780 return *this;
781 }
782 bitmap tmp(other);
783 *this = _NEFORCE move(tmp);
784 // NOLINTNEXTLINE(clang-analyzer-core.StackAddressEscape)
785 return *this;
786 }
787
792 NEFORCE_CONSTEXPR20 bitmap(bitmap&& other) noexcept :
793 start_(other.start_.ptr_, other.start_.off_, this),
794 finish_(other.finish_.ptr_, other.finish_.off_, this),
795 storage_(_NEFORCE move(other.storage_)) {
796 other.start_ = iterator();
797 other.finish_ = iterator();
798 }
799
805 NEFORCE_CONSTEXPR20 bitmap& operator=(bitmap&& other) noexcept {
806 if (_NEFORCE addressof(other) == this) {
807 return *this;
808 }
809 bitmap::swap(other);
810 return *this;
811 }
812
819 template <typename Iterator, enable_if_t<is_iter_v<Iterator>, int> = 0>
820 NEFORCE_CONSTEXPR20 bitmap(Iterator first, Iterator last) {
821 bitmap::range_init(first, last);
822 }
823
827 NEFORCE_CONSTEXPR20 ~bitmap() = default;
828
833 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 iterator begin() noexcept { return start_; }
834
839 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 iterator end() noexcept { return finish_; }
840
845 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 const_iterator begin() const noexcept { return cbegin(); }
846
851 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 const_iterator end() const noexcept { return cend(); }
852
857 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 const_iterator cbegin() const noexcept { return const_iterator{start_}; }
858
863 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 const_iterator cend() const noexcept { return const_iterator{finish_}; }
864
869 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 reverse_iterator rbegin() noexcept { return reverse_iterator(end()); }
870
875 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 reverse_iterator rend() noexcept { return reverse_iterator(begin()); }
876
881 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 const_reverse_iterator rbegin() const noexcept { return crbegin(); }
882
887 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 const_reverse_iterator rend() const noexcept { return crend(); }
888
893 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 const_reverse_iterator crbegin() const noexcept {
895 }
896
901 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 const_reverse_iterator crend() const noexcept {
903 }
904
909 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 size_type size() const noexcept { return cend() - cbegin(); }
910
915 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 size_type max_size() const noexcept { return static_cast<size_type>(-1); }
916
921 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 bool empty() const noexcept { return start_ == finish_; }
922
927 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 size_type capacity() const noexcept {
928 return storage_.capacity() * BITMAP_WORD_SIZE;
929 }
930
936 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 reference operator[](const size_type n) {
937 return *(begin() + static_cast<difference_type>(n));
938 }
939
945 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 const_reference operator[](const size_type n) const {
946 return *(cbegin() + static_cast<difference_type>(n));
947 }
948
953 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 reference front() { return *begin(); }
954
959 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 const_reference front() const { return *cbegin(); }
960
965 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 reference back() { return *(end() - 1); }
966
971 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 const_reference back() const { return *(cend() - 1); }
972
979 NEFORCE_CONSTEXPR20 void reserve(const size_type n) {
980 if (capacity() < n) {
981 const size_type new_words = word_count(n);
982 bit_storage new_storage(new_words);
983 const iterator new_start(new_storage.get(), 0, this);
984 const auto new_finish = bit_copy(begin(), end(), new_start);
985
986 storage_ = _NEFORCE move(new_storage);
987 start_ = new_start;
988 finish_ = new_finish;
989 }
990 }
991
996 NEFORCE_CONSTEXPR20 void push_back(const bool value) {
997 if (finish_.ptr_ != storage_.get() + storage_.capacity()) {
998 *finish_++ = value;
999 } else {
1000 insert_aux(end(), value);
1001 }
1002 }
1003
1010 NEFORCE_CONSTEXPR20 iterator insert(const iterator& position, const bool value) {
1011 const difference_type n = position - begin();
1012 if (finish_.ptr_ != storage_.get() + storage_.capacity() && position == end()) {
1013 *finish_++ = value;
1014 } else {
1015 insert_aux(position, value);
1016 }
1017 return begin() + n;
1018 }
1019
1027 template <typename Iterator>
1028 NEFORCE_CONSTEXPR20 void insert(iterator position, Iterator first, Iterator last) {
1029 bitmap::insert_range(position, first, last);
1030 }
1031
1038 NEFORCE_CONSTEXPR20 void insert(const iterator& position, const bool* first, const bool* last) {
1039 if (first == last) {
1040 return;
1041 }
1042 const size_type n = _NEFORCE distance(first, last);
1043 if (capacity() - size() >= n) {
1044 bitmap::bit_copy_backward(position, end(), finish_ + static_cast<difference_type>(n));
1045 bitmap::bit_copy(first, last, position);
1046 finish_ += static_cast<difference_type>(n);
1047 } else {
1048 bitmap::reallocate_insert_range(position, first, last, n);
1049 }
1050 }
1051
1058 NEFORCE_CONSTEXPR20 void insert(const iterator& position, const size_type n, const bool value) {
1059 if (n == 0) {
1060 return;
1061 }
1062 if (capacity() - size() >= n) {
1063 bitmap::bit_copy_backward(position, end(), finish_ + static_cast<difference_type>(n));
1064 _NEFORCE fill(position, position + static_cast<difference_type>(n), value);
1065 finish_ += static_cast<difference_type>(n);
1066 } else {
1067 bitmap::reallocate_insert(position, n, value);
1068 }
1069 }
1070
1077 NEFORCE_CONSTEXPR20 void insert(const iterator& pos, const int32_t n, const bool value) {
1078 bitmap::insert(pos, static_cast<size_type>(n), value);
1079 }
1080
1087 NEFORCE_CONSTEXPR20 void insert(const iterator& pos, const int64_t n, const bool value) {
1088 bitmap::insert(pos, static_cast<size_type>(n), value);
1089 }
1090
1094 NEFORCE_CONSTEXPR20 void pop_back() { --finish_; }
1095
1101 NEFORCE_CONSTEXPR20 iterator erase(const iterator& position) {
1102 if (position + 1 != end()) {
1103 bitmap::bit_copy(position + 1, end(), position);
1104 }
1105 --finish_;
1106 return position;
1107 }
1108
1115 NEFORCE_CONSTEXPR20 iterator erase(const iterator& first, const iterator& last) {
1116 finish_ = bitmap::bit_copy(last, end(), first);
1117 return first;
1118 }
1119
1125 NEFORCE_CONSTEXPR20 void resize(const size_type n, const bool value = bool()) {
1126 if (n < size()) {
1127 bitmap::erase(begin() + static_cast<difference_type>(n), end());
1128 } else {
1129 bitmap::insert(end(), n - size(), value);
1130 }
1131 }
1132
1136 NEFORCE_CONSTEXPR20 void clear() { bitmap::erase(begin(), end()); }
1137
1142 NEFORCE_CONSTEXPR20 void swap(bitmap& other) noexcept {
1143 if (_NEFORCE addressof(other) == this) {
1144 return;
1145 }
1146 _NEFORCE swap(start_, other.start_);
1147 _NEFORCE swap(finish_, other.finish_);
1148 _NEFORCE swap(storage_, other.storage_);
1149 start_.container_ = this;
1150 finish_.container_ = this;
1151 other.start_.container_ = &other;
1152 other.finish_.container_ = &other;
1153 }
1154
1160 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 bool equal_to(const bitmap& rhs) const
1161 noexcept(noexcept(_NEFORCE equal(this->cbegin(), this->cend(), rhs.cbegin()))) {
1162 return this->size() == rhs.size() && _NEFORCE equal(this->cbegin(), this->cend(), rhs.cbegin());
1163 }
1164
1170 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20 bool less_than(const bitmap& rhs) const
1171 noexcept(noexcept(_NEFORCE lexicographical_compare(this->cbegin(), this->cend(), rhs.cbegin(),
1172 rhs.cend()))) {
1173 return _NEFORCE lexicographical_compare(this->cbegin(), this->cend(), rhs.cbegin(), rhs.cend());
1174 }
1175};
1176 // BitManipulation
1178
1179NEFORCE_END_NAMESPACE__
1180#endif // NEFORCE_CORE_CONTAINER_BITMAP_HPP__
_NEFORCE reverse_iterator< const_iterator > const_reverse_iterator
常量反向迭代器类型
bit_reference reference
引用类型
constexpr void insert(const iterator &position, const size_type n, const bool value)
插入n个相同的位
constexpr reference operator[](const size_type n)
下标访问操作符
constexpr const_iterator cbegin() const noexcept
获取常量起始迭代器
bitmap_iterator< false, bitmap > iterator
迭代器类型
constexpr void insert(const iterator &pos, const int64_t n, const bool value)
插入n个相同的位
constexpr bool less_than(const bitmap &rhs) const noexcept(noexcept(_NEFORCE lexicographical_compare(this->cbegin(), this->cend(), rhs.cbegin(), rhs.cend())))
小于比较操作符
constexpr void insert(iterator position, Iterator first, Iterator last)
范围插入
allocator< uint32_t > allocator_type
分配器类型
constexpr void push_back(const bool value)
在末尾插入位
constexpr const_reverse_iterator crend() const noexcept
获取常量反向结束迭代器
ptrdiff_t difference_type
差值类型
constexpr const_iterator begin() const noexcept
获取常量起始迭代器
constexpr reverse_iterator rend() noexcept
获取反向结束迭代器
constexpr const_reverse_iterator rend() const noexcept
获取常量反向结束迭代器
constexpr iterator erase(const iterator &position)
删除指定位置的位
bool value_type
值类型
constexpr size_type capacity() const noexcept
获取容量(位数)
bitmap_iterator< true, bitmap > const_iterator
常量迭代器类型
_NEFORCE reverse_iterator< iterator > reverse_iterator
反向迭代器类型
constexpr bitmap(const bitmap &other)
拷贝构造函数
bit_reference * pointer
指针类型
constexpr bitmap() noexcept=default
默认构造函数
constexpr reference back()
访问最后一个位
const bool const_reference
常量引用类型
constexpr bitmap(bitmap &&other) noexcept
移动构造函数
constexpr iterator erase(const iterator &first, const iterator &last)
删除指定范围内的位
constexpr const_reference operator[](const size_type n) const
常量下标访问操作符
const bool * const_pointer
常量指针类型
constexpr iterator end() noexcept
获取结束迭代器
constexpr reverse_iterator rbegin() noexcept
获取反向起始迭代器
constexpr bool empty() const noexcept
检查是否为空
constexpr const_iterator end() const noexcept
获取常量结束迭代器
constexpr void pop_back()
删除末尾位
size_t size_type
大小类型
constexpr size_type size() const noexcept
获取位数
constexpr ~bitmap()=default
析构函数
constexpr void swap(bitmap &other) noexcept
交换两个位图的内容
constexpr bitmap & operator=(bitmap &&other) noexcept
移动赋值运算符
constexpr void clear()
清空位图
constexpr bitmap(Iterator first, Iterator last)
范围构造函数
constexpr const_reference front() const
常量版本,访问第一个位
constexpr bitmap(const int64_t word, const bool value)
64位整数构造函数
constexpr bitmap(const size_type word, const bool value)
构造函数,指定大小和初始值
constexpr void resize(const size_type n, const bool value=bool())
调整大小
constexpr size_type max_size() const noexcept
获取最大可能位数
constexpr const_iterator cend() const noexcept
获取常量结束迭代器
constexpr iterator insert(const iterator &position, const bool value)
在指定位置插入位
constexpr const_reference back() const
常量版本,访问最后一个位
constexpr iterator begin() noexcept
获取起始迭代器
constexpr reference front()
访问第一个位
constexpr void reserve(const size_type n)
预留容量
constexpr void insert(const iterator &position, const bool *first, const bool *last)
插入布尔数组范围
constexpr const_reverse_iterator rbegin() const noexcept
获取常量反向起始迭代器
constexpr const_reverse_iterator crbegin() const noexcept
获取常量反向起始迭代器
constexpr bitmap(const int32_t word, const bool value)
32位整数构造函数
constexpr bitmap & operator=(const bitmap &other)
拷贝赋值运算符
constexpr void insert(const iterator &pos, const int32_t n, const bool value)
插入n个相同的位
constexpr bool equal_to(const bitmap &rhs) const noexcept(noexcept(_NEFORCE equal(this->cbegin(), this->cend(), rhs.cbegin())))
相等比较操作符
constexpr T * addressof(T &x) noexcept
获取对象的地址
enable_if_t< is_void_v< T >, future_result_t< T > > get(future< T > &f)
通用future结果获取函数
constexpr uint32_t BITMAP_WORD_SIZE
每个字的位数
constexpr bool lexicographical_compare(Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2, Compare comp) noexcept(noexcept(++first1) &&noexcept(++first2) &&noexcept(comp(*first1, *first2)) &&noexcept(first1==last1 &&first2 !=last2))
字典序比较两个范围
constexpr bool equal(Iterator1 first1, Iterator1 last1, Iterator2 first2, BinaryPredicate binary_pred) noexcept(noexcept(++first1) &&noexcept(++first2) &&noexcept(binary_pred(*first1, *first2)))
比较两个范围是否相等
unsigned int uint32_t
32位无符号整数类型
long long int64_t
64位有符号整数类型
int int32_t
32位有符号整数类型
constexpr iter_difference_t< Iterator > distance(Iterator first, Iterator last)
计算两个迭代器之间的距离
typename iterator_traits< Iterator >::difference_type iter_difference_t
获取迭代器的差值类型
standard_allocator< T > allocator
标准分配器别名
constexpr void * allocate(const inner::alloc_size_t bytes)
内存分配函数
constexpr void memory_zero(void *dest, const size_t count) noexcept
将内存区域清零
uint64_t size_t
无符号大小类型
int64_t ptrdiff_t
指针差类型
constexpr void fill(Iterator first, Iterator last, const T &value) noexcept(is_nothrow_assignable_v< iter_value_t< Iterator >, T >)
填充范围元素
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
constexpr Iterator fill_n(Iterator first, iter_difference_t< Iterator > n, const T &value) noexcept(is_nothrow_assignable_v< iter_value_t< Iterator >, T >)
填充指定数量的元素
void swap()=delete
删除无参数的swap重载
typename conditional< Test, T1, T2 >::type conditional_t
conditional的便捷别名
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
迭代器接口
可字符串化接口
位引用类
constexpr bit_reference & operator=(bit_reference &&other) noexcept
移动赋值运算符
constexpr bool equal_to(const bit_reference &rhs) const noexcept
相等比较操作符
constexpr bit_reference & operator=(const bit_reference &other) noexcept
拷贝赋值运算符
constexpr bit_reference & operator=(const bool value) noexcept
赋值布尔值
constexpr void flip() const noexcept
翻转位值
constexpr bit_reference(const bit_reference &other) noexcept
拷贝构造函数
constexpr bit_reference(uint32_t *ptr, const uint32_t mask) noexcept
构造函数
constexpr void swap(bit_reference &other) noexcept
交换两个位引用
constexpr bit_reference(bit_reference &&other) noexcept
移动构造函数
constexpr bool less_than(const bit_reference &rhs) const noexcept
小于比较操作符
constexpr string to_string() const
转换为字符串
constexpr size_t to_hash() const noexcept
计算哈希值
位图迭代器
typename container_type::value_type value_type
值类型
BitMap container_type
容器类型
constexpr reference operator[](const difference_type n) const noexcept
下标访问操作符
constexpr bool equal_to(const bitmap_iterator &rhs) const noexcept
相等比较
constexpr decltype(auto) base() const noexcept
获取底层指针
random_access_iterator_tag iterator_category
迭代器类别
constexpr difference_type distance_to(const bitmap_iterator &other) const noexcept
计算距离操作
conditional_t< IsConst, typename container_type::const_reference, typename container_type::reference > reference
引用类型
constexpr void advance(difference_type off) noexcept
前进操作
conditional_t< IsConst, typename container_type::const_pointer, typename container_type::pointer > pointer
指针类型
constexpr reference dereference() const noexcept
解引用操作
constexpr bitmap_iterator(const bitmap_iterator< IsConst2, BitMap > &other) noexcept
从另一个迭代器转换构造
constexpr bool less_than(const bitmap_iterator &rhs) const noexcept
小于比较
typename container_type::size_type size_type
大小类型
constexpr bitmap_iterator(uint32_t *ptr, const uint32_t offset, const container_type *bm) noexcept
构造函数
constexpr void increment() noexcept
递增操作
constexpr void decrement() noexcept
递减操作
constexpr const container_type * container() const noexcept
获取关联容器
typename container_type::difference_type difference_type
差值类型
压缩对主模板,使用EBCO优化
constexpr compressed_pair & get_base() &noexcept
获取基类引用
默认构造标签
哈希函数的主模板
通用接口,同时具备可比较和可哈希功能
可比较对象接口模板
迭代器接口模板
可字符串化接口
随机访问迭代器标签