MCPcopy Create free account
hub / github.com/FlaxEngine/FlaxEngine / SetCapacity

Method SetCapacity

Source/Engine/Core/Collections/HashSetBase.h:148–190  ·  view source on GitHub ↗

Changes the capacity of the collection. The new capacity. True if preserve collection data when changing its size, otherwise collection after resize will be empty.

Source from the content-addressed store, hash-verified

146 /// <param name="capacity">The new capacity.</param>
147 /// <param name="preserveContents">True if preserve collection data when changing its size, otherwise collection after resize will be empty.</param>
148 void SetCapacity(int32 capacity, const bool preserveContents = true)
149 {
150 if (capacity == _size)
151 return;
152 ASSERT(capacity >= 0);
153 AllocationData oldAllocation;
154 AllocationUtils::MoveToEmpty<BucketType, AllocationType>(oldAllocation, _allocation, _size, _size);
155 const int32 oldSize = _size;
156 const int32 oldElementsCount = _elementsCount;
157 _deletedCount = _elementsCount = 0;
158 if (capacity != 0 && (capacity & (capacity - 1)) != 0)
159 capacity = AllocationUtils::AlignToPowerOf2(capacity);
160 if (capacity)
161 {
162 _allocation.Allocate(capacity);
163 BucketType* data = _allocation.Get();
164 for (int32 i = 0; i < capacity; i++)
165 data[i]._state = HashSetBucketState::Empty;
166 }
167 _size = capacity;
168 BucketType* oldData = oldAllocation.Get();
169 if (oldElementsCount != 0 && capacity != 0 && preserveContents)
170 {
171 FindPositionResult pos;
172 for (int32 i = 0; i < oldSize; i++)
173 {
174 BucketType& oldBucket = oldData[i];
175 if (oldBucket.IsOccupied())
176 {
177 FindPosition(oldBucket.GetKey(), pos);
178 ASSERT(pos.FreeSlotIndex != -1);
179 BucketType& bucket = _allocation.Get()[pos.FreeSlotIndex];
180 bucket = MoveTemp(oldBucket);
181 _elementsCount++;
182 }
183 }
184 }
185 if (oldElementsCount != 0)
186 {
187 for (int32 i = 0; i < oldSize; i++)
188 oldData[i].Free();
189 }
190 }
191
192 /// <summary>
193 /// Ensures that collection has given capacity.

Callers

nothing calls this directly

Calls 6

AlignToPowerOf2Function · 0.85
AllocateMethod · 0.45
GetMethod · 0.45
IsOccupiedMethod · 0.45
GetKeyMethod · 0.45
FreeMethod · 0.45

Tested by

no test coverage detected