4#ifndef GREM_CORE_DATA_REGISTRY_HPP
5#define GREM_CORE_DATA_REGISTRY_HPP
24template <
typename T,
typename ElementID,
typename Container>
32template <
typename Self>
35 using GREM_private_DerivedFromRegistryElementIDBaseTag = void;
62 : value((uint32_t{flags} << 30) | ((uint32_t{generation} & 0x3FFF) << 16) | uint32_t{index}) {}
70 constexpr explicit operator bool() const noexcept {
108 return static_cast<Flags>(value >> 30);
137 [[nodiscard]]
friend constexpr bool operator==(
const Self& a,
const Self& b)
noexcept {
149 [[nodiscard]]
friend constexpr std::strong_ordering
operator<=>(
const Self& a,
const Self& b)
noexcept {
174template <
typename T,
typename ElementID = RegistryElementID,
typename Container = ArrayList<T>>
184 template <
bool Const>
187 using UnderlyingIterator = std::conditional_t<Const, typename Container::const_iterator, typename Container::iterator>;
193 using iterator_category = std::random_access_iterator_tag;
203 Iterator() noexcept = default;
205 constexpr Iterator(const ElementID* elementID, UnderlyingIterator it) noexcept
206 : elementID(elementID)
207 , it(std::
move(it)) {}
209 constexpr operator Iterator<true>() const noexcept requires(!Const) {
210 return Iterator<true>{elementID, it};
213 [[nodiscard]]
constexpr reference operator*()
const {
214 return reference{*elementID, *it};
217 [[nodiscard]]
constexpr pointer operator->()
const {
221 [[nodiscard]]
constexpr reference operator[](difference_type n)
const {
225 constexpr Iterator& operator++() {
231 constexpr Iterator& operator--() {
237 constexpr Iterator operator++(
int) {
238 Iterator old = *
this;
243 constexpr Iterator operator--(
int) {
244 Iterator old = *
this;
249 constexpr Iterator& operator+=(difference_type n) {
255 constexpr Iterator& operator-=(difference_type n) {
261 [[nodiscard]]
friend constexpr Iterator operator+(Iterator a, difference_type b) {
265 [[nodiscard]]
friend constexpr Iterator operator+(difference_type a, Iterator b) {
269 [[nodiscard]]
friend constexpr Iterator operator-(Iterator a, difference_type b) {
273 [[nodiscard]]
friend constexpr difference_type operator-(Iterator a, Iterator b) {
274 GREM_ASSERT(a.elementID - b.elementID == a.it - b.it);
275 return static_cast<difference_type
>(a.it - b.it);
278 [[nodiscard]]
friend constexpr bool operator==(Iterator a, Iterator b) {
279 GREM_ASSERT(a.elementID - b.elementID == a.it - b.it);
283 [[nodiscard]]
friend constexpr auto operator<=>(Iterator a, Iterator b) {
284 GREM_ASSERT(a.elementID - b.elementID == a.it - b.it);
285 return a.it <=> b.it;
289 const ElementID* elementID;
290 UnderlyingIterator it;
312 , elementIDs(allocator)
313 , elements(allocator) {}
321 :
Registry(std::move(other), other.get_allocator()) {}
324 : nextAvailableSlotIndex(other.nextAvailableSlotIndex)
325 , slots(other.slots, allocator)
326 , elementIDs(other.elementIDs, allocator)
327 , elements(other.elements, allocator) {}
330 : nextAvailableSlotIndex(std::exchange(other.nextAvailableSlotIndex, Index{0}))
331 , slots(std::move(other.slots), allocator)
332 , elementIDs(std::move(other.elementIDs), allocator)
333 , elements(std::move(other.elements), allocator) {}
337 std::allocator_traits<
339 std::allocator_traits<
allocator_type>::is_always_equal::value) = default;
341 constexpr ElementID
setFlags(ElementID key, ElementID::Flags newFlags) {
342 const Index slotIndex = key.getIndex();
343 if (slotIndex < slots.size() && (key.getGeneration() & 1) != 0) {
344 ElementID& slot = slots[slotIndex];
345 if (slot.getGeneration() == key.getGeneration()) {
346 const Index idIndex = slot.getIndex();
347 slot = ElementID{idIndex, key.getGeneration(), newFlags};
348 const ElementID newElementID{slotIndex, key.getGeneration(), newFlags};
349 GREM_ASSERT(elementIDs[idIndex].getGeneration() == key.getGeneration());
350 elementIDs[idIndex] = newElementID;
357 [[nodiscard]]
constexpr const Container&
getElements() const noexcept {
362 return elements.get_allocator();
365 [[nodiscard]]
constexpr T*
data() noexcept requires(requires(Container c) { c.data(); }) {
366 return elements.data();
369 [[nodiscard]]
constexpr const T*
data() const noexcept requires(requires(const Container c) { c.data(); }) {
370 return elements.data();
374 return iterator{elementIDs.data(), elements.begin()};
386 return iterator{elementIDs.data() +
size(), elements.end()};
421 [[nodiscard]]
constexpr bool empty() const noexcept {
422 return elements.empty();
426 return elements.size();
430 return min(
min(slots.max_size(), elements.max_size()),
static_cast<size_type>(
static_cast<size_type>(MAX_INDEX) + 1));
433 [[nodiscard]]
constexpr bool contains(ElementID key)
const noexcept {
434 const Index slotIndex = key.getIndex();
435 return slotIndex < slots.size() && (key.getGeneration() & 1) != 0 && slots[slotIndex].getGeneration() == key.getGeneration();
454 return elementIDs[pos];
458 const Index idIndex =
static_cast<Index
>(pos -
cbegin());
460 return elementIDs[idIndex];
463 [[nodiscard]]
constexpr T&
at(ElementID key) {
464 if (
const auto it =
find(key); it !=
end()) {
467 throw std::out_of_range{
"!contains(key)"};
470 [[nodiscard]]
constexpr const T&
at(ElementID key)
const {
471 if (
const auto it =
find(key); it !=
end()) {
474 throw std::out_of_range{
"!contains(key)"};
478 const Index slotIndex = key.getIndex();
480 GREM_ASSERT(slots[slotIndex].getGeneration() == key.getGeneration());
481 return elements[slots[slotIndex].getIndex()];
484 [[nodiscard]]
constexpr const T&
operator[](ElementID key)
const {
485 const Index slotIndex = key.getIndex();
487 GREM_ASSERT(slots[slotIndex].getGeneration() == key.getGeneration());
488 return elements[slots[slotIndex].getIndex()];
493 throw std::out_of_range{
"pos >= size()"};
500 throw std::out_of_range{
"pos >= size()"};
513 template <
typename ColumnType>
518 template <
typename ColumnType>
523 template <
size_t ColumnIndex>
528 template <
size_t ColumnIndex>
535 throw std::length_error{
"newCap > max_size()"};
537 slots.reserve(newCapacity);
538 elementIDs.reserve(newCapacity);
539 elements.reserve(newCapacity);
543 return slots.capacity();
547 slots.shrink_to_fit();
548 elementIDs.shrink_to_fit();
549 elements.shrink_to_fit();
557 for (Index slotIndex = 0; slotIndex < slots.size(); ++slotIndex) {
558 const Generation generation = slots[slotIndex].getGeneration();
559 const Generation destroyedGeneration = generation + (generation & 1);
560 slots[slotIndex] = ElementID{
static_cast<Index
>(slotIndex + 1), destroyedGeneration, Flags{}};
562 nextAvailableSlotIndex = 0;
570 return emplace(std::move(value));
573 template <
typename... Args>
578 template <
typename... Args>
583 throw std::length_error{
"size() >= max_size()"};
586 const Index idIndex =
static_cast<Index
>(
size());
587 ElementID& elementID = elementIDs.push_back_unspecified_value();
589 Index slotIndex =
static_cast<Index
>(slots.size());
590 Generation generation = 1;
591 if (nextAvailableSlotIndex >= slotIndex) {
595 slots.push_back(ElementID{idIndex, generation, flags});
597 elements.emplace_back(std::forward<Args>(args)...);
602 ++nextAvailableSlotIndex;
604 elementIDs.pop_back();
609 elements.emplace_back(std::forward<Args>(args)...);
611 elementIDs.pop_back();
614 slotIndex = nextAvailableSlotIndex;
615 nextAvailableSlotIndex = slots[slotIndex].getIndex();
620 generation =
static_cast<Generation
>(slots[slotIndex].getGeneration() + 1);
621 slots[slotIndex] = ElementID{idIndex, generation, flags};
624 elementID = ElementID{slotIndex, generation, flags};
632 eraseImplementation(key.getIndex());
637 return eraseImplementation(
id(pos).getIndex());
641 noexcept(elements.swap(other.elements)) &&
noexcept(elementIDs.swap(other.elementIDs))) {
643 swap(nextAvailableSlotIndex, other.nextAvailableSlotIndex);
644 swap(slots, other.slots);
645 swap(elementIDs, other.elementIDs);
646 swap(elements, other.elements);
649 template <
typename Predicate>
653 while (it != c.
end()) {
654 if (predicate(*it)) {
660 return oldSize - c.
size();
664 using ElementIDAllocator =
typename std::allocator_traits<allocator_type>::template rebind_alloc<ElementID>;
666 iterator eraseImplementation(Index slotIndex) {
671 const Index idIndex = slots[slotIndex].getIndex();
672 const Generation generation = slots[slotIndex].getGeneration();
673 const Index lastIDIndex =
static_cast<Index
>(size() - 1);
674 const ElementID lastElementID = elementIDs[lastIDIndex];
675 const Index lastSlotIndex = lastElementID.getIndex();
676 if (lastSlotIndex != slotIndex) {
677 elements[idIndex] = std::move(elements[lastIDIndex]);
678 elementIDs[idIndex] = lastElementID;
679 slots[lastSlotIndex] = ElementID{idIndex, lastElementID.getGeneration(), lastElementID.getFlags()};
682 elementIDs.pop_back();
685 slots[slotIndex] = ElementID{nextAvailableSlotIndex,
static_cast<Generation
>(generation + 1), Flags{}};
686 nextAvailableSlotIndex = slotIndex;
688 return begin() +
static_cast<difference_type
>(idIndex);
691 Index nextAvailableSlotIndex = 0;
697template <
typename ColumnType,
typename T,
typename ElementID,
typename Container>
699 return r.template column<ColumnType>();
702template <
typename ColumnType,
typename T,
typename ElementID,
typename Container>
704 return r.template column<ColumnType>();
707template <
size_t ColumnIndex,
typename T,
typename ElementID,
typename Container>
709 return r.template column<ColumnIndex>();
712template <
size_t ColumnIndex,
typename T,
typename ElementID,
typename Container>
714 return r.template column<ColumnIndex>();
721template <
typename T,
typename ElementID>
726template <
typename ElementID>
727requires(
requires {
typename ElementID::GREM_private_DerivedFromRegistryElementIDBaseTag; })
struct std::hash<ElementID> {
728 [[nodiscard]] std::size_t
operator()(
const ElementID& elementID)
const {
729 return hasher(elementID.getIdentifier());
733 [[no_unique_address]] std::hash<typename grem::RegistryElementID::Identifier> hasher;
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
constexpr Flags getFlags() const noexcept
Get the user-defined flags of the element.
Definition Registry.hpp:107
friend constexpr std::strong_ordering operator<=>(const Self &a, const Self &b) noexcept
Compare two handles.
Definition Registry.hpp:149
friend constexpr bool operator==(const Self &a, const Self &b) noexcept
Compare two handles for equality.
Definition Registry.hpp:137
constexpr Index getIndex() const noexcept
Get the index of the element's generation slot.
Definition Registry.hpp:89
static constexpr Index INVALID_INDEX
Reserved index value for an invalid element handle.
Definition Registry.hpp:42
uint8_t Flags
User-defined flags index type.
Definition Registry.hpp:40
constexpr bool operator==(const RegistryElementIDBase &other) const noexcept=default
Compare this handle to another for equality.
uint16_t Generation
Generation counter type.
Definition Registry.hpp:39
static constexpr Index MAX_INDEX
Maximum valid index value.
Definition Registry.hpp:43
constexpr std::strong_ordering operator<=>(const RegistryElementIDBase &other) const noexcept=default
Compare this handle to another.
static constexpr Generation MAX_GENERATION
Maximum valid generation counter value.
Definition Registry.hpp:44
uint32_t Identifier
Underlying value type.
Definition Registry.hpp:37
uint16_t Index
Index type.
Definition Registry.hpp:38
constexpr Identifier getIdentifier() const noexcept
Get a unique identifier for the underlying representation of the element handle.
Definition Registry.hpp:80
constexpr RegistryElementIDBase() noexcept=default
Construct an invalid element handle.
static constexpr Flags ALL_FLAGS
Maximum valid set of user-defined flags.
Definition Registry.hpp:45
constexpr Generation getGeneration() const noexcept
Get the generation counter value of the element.
Definition Registry.hpp:98
Tightly packed set of elements with stable IDs, supporting fast iteration as well as amortized O(1) i...
Definition Registry.hpp:175
constexpr const T & operator[](ElementID key) const
Definition Registry.hpp:484
constexpr ElementID id(size_type pos) const
Definition Registry.hpp:452
friend constexpr size_type erase_if(Registry &c, Predicate predicate)
Definition Registry.hpp:650
constexpr allocator_type get_allocator() const noexcept
Definition Registry.hpp:361
constexpr ElementID id(const_iterator pos) const
Definition Registry.hpp:457
constexpr Registry & operator=(const Registry &other)=default
constexpr iterator find(ElementID key) noexcept
Definition Registry.hpp:438
constexpr Registry(Registry &&other, const allocator_type &allocator) noexcept
Definition Registry.hpp:329
constexpr const Container & getElements() const noexcept
Definition Registry.hpp:357
constexpr size_type max_size() const noexcept
Definition Registry.hpp:429
constexpr Registry(Registry &&other) noexcept
Definition Registry.hpp:320
constexpr const_iterator begin() const noexcept
Definition Registry.hpp:377
constexpr decltype(auto) column()
Definition Registry.hpp:514
constexpr const_reference operator[](size_type pos) const
Definition Registry.hpp:509
constexpr size_type size() const noexcept
Definition Registry.hpp:425
typename iterator::difference_type difference_type
Definition Registry.hpp:299
constexpr iterator emplaceWithFlags(Flags flags, Args &&... args)
Definition Registry.hpp:579
constexpr bool contains(ElementID key) const noexcept
Definition Registry.hpp:433
constexpr const_iterator cbegin() const noexcept
Definition Registry.hpp:381
typename Container::allocator_type allocator_type
Definition Registry.hpp:297
constexpr ~Registry()=default
constexpr const_reverse_iterator rbegin() const noexcept
Definition Registry.hpp:401
constexpr void swap(Registry &other) noexcept(noexcept(elements.swap(other.elements)) &&noexcept(elementIDs.swap(other.elementIDs)))
Definition Registry.hpp:640
typename iterator::reference reference
Definition Registry.hpp:300
constexpr reverse_iterator rbegin() noexcept
Definition Registry.hpp:397
constexpr Registry(const Registry &other)
Definition Registry.hpp:317
typename const_iterator::reference const_reference
Definition Registry.hpp:301
constexpr Registry(const Registry &other, const allocator_type &allocator)
Definition Registry.hpp:323
constexpr const_reverse_iterator crend() const noexcept
Definition Registry.hpp:417
typename const_iterator::pointer const_pointer
Definition Registry.hpp:303
constexpr void reserve(size_type newCapacity)
Definition Registry.hpp:533
constexpr iterator begin() noexcept
Definition Registry.hpp:373
constexpr void shrink_to_fit()
Definition Registry.hpp:546
constexpr Registry() noexcept(noexcept(allocator_type()))
Definition Registry.hpp:307
constexpr bool empty() const noexcept
Definition Registry.hpp:421
constexpr iterator erase(const_iterator pos)
Definition Registry.hpp:636
constexpr reverse_iterator rend() noexcept
Definition Registry.hpp:409
constexpr const_iterator cend() const noexcept
Definition Registry.hpp:393
constexpr T * data() noexcept
Definition Registry.hpp:365
constexpr iterator insert(const T &value)
Definition Registry.hpp:565
constexpr void clear() noexcept
Definition Registry.hpp:552
constexpr decltype(auto) column() const
Definition Registry.hpp:519
constexpr const_reverse_iterator rend() const noexcept
Definition Registry.hpp:413
constexpr size_type capacity() const noexcept
Definition Registry.hpp:542
constexpr const T & at(ElementID key) const
Definition Registry.hpp:470
constexpr const_iterator end() const noexcept
Definition Registry.hpp:389
std::reverse_iterator< iterator > reverse_iterator
Definition Registry.hpp:304
constexpr const_reference at(size_type pos) const
Definition Registry.hpp:498
typename Container::size_type size_type
Definition Registry.hpp:298
constexpr decltype(auto) column()
Definition Registry.hpp:524
constexpr size_type erase(ElementID key)
Definition Registry.hpp:628
Iterator< false > iterator
Definition Registry.hpp:294
constexpr decltype(auto) column() const
Definition Registry.hpp:529
constexpr reference at(size_type pos)
Definition Registry.hpp:491
constexpr const_reverse_iterator crbegin() const noexcept
Definition Registry.hpp:405
constexpr Registry & operator=(Registry &&) noexcept(std::allocator_traits< allocator_type >::propagate_on_container_move_assignment::value||//NOLINT(cppcoreguidelines-noexcept-move-operations, performance-noexcept-move-constructor) std::allocator_traits< allocator_type >::is_always_equal::value)=default
typename iterator::value_type value_type
Definition Registry.hpp:296
Iterator< true > const_iterator
Definition Registry.hpp:295
constexpr reference operator[](size_type pos)
Definition Registry.hpp:505
constexpr const T * data() const noexcept
Definition Registry.hpp:369
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition Registry.hpp:305
constexpr iterator emplace(Args &&... args)
Definition Registry.hpp:574
constexpr iterator end() noexcept
Definition Registry.hpp:385
constexpr T & at(ElementID key)
Definition Registry.hpp:463
constexpr Registry(const allocator_type &allocator) noexcept
Definition Registry.hpp:310
constexpr const_iterator find(ElementID key) const noexcept
Definition Registry.hpp:445
constexpr ElementID setFlags(ElementID key, ElementID::Flags newFlags)
Definition Registry.hpp:341
constexpr iterator insert(T &&value)
Definition Registry.hpp:569
constexpr T & operator[](ElementID key)
Definition Registry.hpp:477
Definition concepts.hpp:106
Definition Allocation.hpp:554
grem::Registry< T, ElementID, pmr::ArrayList< T > > Registry
Definition Registry.hpp:722
constexpr auto find(input_range auto &&range, const auto &value)
Definition algorithms.hpp:242
constexpr auto move(input_range auto &&range, auto outputIterator)
Definition algorithms.hpp:669
constexpr decltype(auto) column(Registry< T, ElementID, Container > &r)
Definition Registry.hpp:698
constexpr bool contains(input_range auto &&range, const auto &value)
Definition algorithms.hpp:299
GREM_ALWAYS_INLINE constexpr decltype(auto) min(A &&a, B &&b)
Get the smaller of two values, or the first value if they are equivalent.
Definition fundamentals.hpp:585
static constexpr T MAX
Definition fundamentals.hpp:29
Opaque handle to a specific element in a Registry.
Definition Registry.hpp:160
constexpr RegistryElementIDBase() noexcept=default
Construct an invalid element handle.
Definition Registry.hpp:195
constexpr reference * operator->() noexcept
Definition Registry.hpp:198
reference ref
Definition Registry.hpp:196
std::size_t operator()(const ElementID &elementID) const
Definition Registry.hpp:728