Plasma Engine  2.0
Loading...
Searching...
No Matches
IdTable.h
1#pragma once
2
3#include <Foundation/Memory/AllocatorWrapper.h>
4#include <Foundation/Types/Id.h>
5
16template <typename IdType, typename ValueType>
18{
19public:
20 using IndexType = typename IdType::StorageType;
21 using TypeOfId = IdType;
22
25 {
26 public:
28 bool IsValid() const; // [tested]
29
31 bool operator==(const typename plIdTableBase<IdType, ValueType>::ConstIterator& it2) const;
32
34 bool operator!=(const typename plIdTableBase<IdType, ValueType>::ConstIterator& it2) const;
35
37 IdType Id() const; // [tested]
38
40 const ValueType& Value() const; // [tested]
41
43 void Next(); // [tested]
44
46 void operator++(); // [tested]
47
48 protected:
49 friend class plIdTableBase<IdType, ValueType>;
50
51 explicit ConstIterator(const plIdTableBase<IdType, ValueType>& idTable);
52
53 const plIdTableBase<IdType, ValueType>& m_IdTable;
54 IndexType m_CurrentIndex; // current element index that this iterator points to.
55 IndexType m_CurrentCount; // current number of valid elements that this iterator has found so far.
56 };
57
59 struct Iterator : public ConstIterator
60 {
61 public:
62 // this is required to pull in the const version of this function
64
66 ValueType& Value(); // [tested]
67
68 private:
69 friend class plIdTableBase<IdType, ValueType>;
70
71 explicit Iterator(const plIdTableBase<IdType, ValueType>& idTable);
72 };
73
74protected:
76 explicit plIdTableBase(plAllocator* pAllocator); // [tested]
77
79 plIdTableBase(const plIdTableBase<IdType, ValueType>& rhs, plAllocator* pAllocator); // [tested]
80
82 ~plIdTableBase(); // [tested]
83
85 void operator=(const plIdTableBase<IdType, ValueType>& rhs); // [tested]
86
87public:
89 void Reserve(IndexType capacity); // [tested]
90
92 IndexType GetCount() const; // [tested]
93
95 bool IsEmpty() const; // [tested]
96
98 void Clear(); // [tested]
99
101 IdType Insert(const ValueType& value); // [tested]
102
104 IdType Insert(ValueType&& value);
105
107 bool Remove(const IdType id, ValueType* out_pOldValue = nullptr); // [tested]
108
110 bool TryGetValue(const IdType id, ValueType& out_value) const; // [tested]
111
113 bool TryGetValue(const IdType id, ValueType*& out_pValue) const; // [tested]
114
116 const ValueType& operator[](const IdType id) const; // [tested]
117
119 ValueType& operator[](const IdType id); // [tested]
120
122 const ValueType& GetValueUnchecked(const IndexType index) const;
123
125 ValueType& GetValueUnchecked(const IndexType index);
126
128 bool Contains(const IdType id) const; // [tested]
129
131 Iterator GetIterator(); // [tested]
132
134 ConstIterator GetIterator() const; // [tested]
135
137 plAllocator* GetAllocator() const;
138
140 bool IsFreelistValid() const;
141
142private:
143 enum
144 {
145 CAPACITY_ALIGNMENT = 16
146 };
147
148 struct Entry
149 {
150 IdType id;
151 ValueType value;
152 };
153
154 Entry* m_pEntries;
155
156 IndexType m_Count;
157 IndexType m_Capacity;
158
159 IndexType m_FreelistEnqueue;
160 IndexType m_FreelistDequeue;
161
162 plAllocator* m_pAllocator;
163
164 void SetCapacity(IndexType uiCapacity);
165 void InitializeFreelist(IndexType uiStart, IndexType uiEnd);
166};
167
169template <typename IdType, typename ValueType, typename AllocatorWrapper = plDefaultAllocatorWrapper>
170class plIdTable : public plIdTableBase<IdType, ValueType>
171{
172public:
173 plIdTable();
174 explicit plIdTable(plAllocator* pAllocator);
175
178
179 void operator=(const plIdTable<IdType, ValueType, AllocatorWrapper>& rhs);
180 void operator=(const plIdTableBase<IdType, ValueType>& rhs);
181};
182
183#include <Foundation/Containers/Implementation/IdTable_inl.h>
Base class for all memory allocators.
Definition Allocator.h:23
Const iterator.
Definition IdTable.h:25
bool IsValid() const
Checks whether this iterator points to a valid element.
Definition IdTable_inl.h:20
const ValueType & Value() const
Returns the 'value' of the element that this iterator points to.
Definition IdTable_inl.h:46
bool operator==(const typename plIdTableBase< IdType, ValueType >::ConstIterator &it2) const
Checks whether the two iterators point to the same element.
Definition IdTable_inl.h:26
void Next()
Advances the iterator to the next element in the map. The iterator will not be valid anymore,...
Definition IdTable_inl.h:52
void operator++()
Shorthand for 'Next'.
Definition IdTable_inl.h:65
IdType Id() const
Returns the 'id' of the element that this iterator points to.
Definition IdTable_inl.h:40
bool operator!=(const typename plIdTableBase< IdType, ValueType >::ConstIterator &it2) const
Checks whether the two iterators point to the same element.
Definition IdTable_inl.h:33
Implementation of an id mapping table which stores id/value pairs.
Definition IdTable.h:18
~plIdTableBase()
Destructor.
Definition IdTable_inl.h:113
IndexType GetCount() const
Returns the number of active entries in the table.
Definition IdTable_inl.h:168
const ValueType & operator[](const IdType id) const
Returns the value to the given id. Does bounds checks in debug builds.
Definition IdTable_inl.h:291
bool TryGetValue(const IdType id, ValueType &out_value) const
Returns if an entry with the given id was found and if found writes out the corresponding value to ou...
Definition IdTable_inl.h:267
void Clear()
Clears the table.
Definition IdTable_inl.h:180
void Reserve(IndexType capacity)
Expands the table so it can at least store the given capacity.
Definition IdTable_inl.h:149
Iterator GetIterator()
Returns an Iterator to the very first element.
Definition IdTable_inl.h:335
const ValueType & GetValueUnchecked(const IndexType index) const
Returns the value at the given index. Does bounds checks in debug builds but does not check for stale...
Definition IdTable_inl.h:314
bool Contains(const IdType id) const
Returns if the table contains an entry corresponding to the given id.
Definition IdTable_inl.h:328
void operator=(const plIdTableBase< IdType, ValueType > &rhs)
Copies the data from another table into this one.
Definition IdTable_inl.h:128
plIdTableBase(plAllocator *pAllocator)
Creates an empty id-table. Does not allocate any data yet.
Definition IdTable_inl.h:89
bool IsFreelistValid() const
Returns whether the internal free-list is valid. For testing purpose only.
Definition IdTable_inl.h:353
bool Remove(const IdType id, ValueType *out_pOldValue=nullptr)
Removes the entry with the given id. Returns if an entry was removed and optionally writes out the ol...
Definition IdTable_inl.h:237
IdType Insert(const ValueType &value)
Inserts the value into the table and returns the corresponding id.
Definition IdTable_inl.h:201
bool IsEmpty() const
Returns true, if the table does not contain any elements.
Definition IdTable_inl.h:174
plAllocator * GetAllocator() const
Returns the allocator that is used by this instance.
Definition IdTable_inl.h:347
Definition IdTable.h:171
Iterator with write access.
Definition IdTable.h:60
ValueType & Value()
Returns the 'value' of the element that this iterator points to.
Definition IdTable_inl.h:80