Add IdList::AllocForOneMore() based on List. NFC.

Also modifies a sizeof call.
pull/459/head
Ryan Pavlik 2019-05-23 12:15:57 -05:00 committed by whitequark
parent 0bfbbe2bf3
commit 9fd09ddd8a
1 changed files with 10 additions and 5 deletions

View File

@ -215,7 +215,7 @@ public:
void ReserveMore(int howMuch) {
if(n + howMuch > elemsAllocated) {
elemsAllocated = n + howMuch;
T *newElem = (T *)MemAlloc((size_t)elemsAllocated*sizeof(elem[0]));
T *newElem = (T *)MemAlloc((size_t)elemsAllocated*sizeof(T));
for(int i = 0; i < n; i++) {
new(&newElem[i]) T(std::move(elem[i]));
elem[i].~T();
@ -348,6 +348,12 @@ public:
return n == 0;
}
void AllocForOneMore() {
if(n >= elemsAllocated) {
ReserveMore((elemsAllocated + 32)*2 - n);
}
}
uint32_t MaximumId() {
if(IsEmpty()) {
return 0;
@ -391,7 +397,7 @@ public:
void ReserveMore(int howMuch) {
if(n + howMuch > elemsAllocated) {
elemsAllocated = n + howMuch;
T *newElem = (T *)MemAlloc((size_t)elemsAllocated*sizeof(elem[0]));
T *newElem = (T *)MemAlloc((size_t)elemsAllocated*sizeof(T));
for(int i = 0; i < n; i++) {
new(&newElem[i]) T(std::move(elem[i]));
elem[i].~T();
@ -402,9 +408,8 @@ public:
}
void Add(T *t) {
if(n >= elemsAllocated) {
ReserveMore((elemsAllocated + 32)*2 - n);
}
AllocForOneMore();
// Look to see if we already have something with the same handle value.
ssassert(FindByIdNoOops(t->h) == nullptr, "Handle isn't unique");