Simplify IdList::Add(). NFC.

Offloads most of the work onto standard algorithms to make it
more self-evidently correct.
pull/459/head
Ryan Pavlik 2019-05-22 16:09:43 -05:00 committed by whitequark
parent 3340392bf0
commit 60fdac141d
1 changed files with 8 additions and 10 deletions

View File

@ -399,16 +399,14 @@ public:
if(n >= elemsAllocated) { if(n >= elemsAllocated) {
ReserveMore((elemsAllocated + 32)*2 - n); ReserveMore((elemsAllocated + 32)*2 - n);
} }
auto newIndex = LowerBoundIndex(*t); // Look to see if we already have something with the same handle value.
if (newIndex < n) { ssassert(FindByIdNoOops(t->h) == nullptr, "Handle isn't unique");
H hm = elem[newIndex].h;
ssassert(hm.v != t->h.v, "Handle isn't unique"); // Copy-construct at the end of the list.
} new(&elem[n]) T(*t);
int i = static_cast<int>(newIndex); ++n;
new(&elem[n]) T(); // The item we just added is trivially sorted, so "merge"
std::move_backward(elem + i, elem + n, elem + n + 1); std::inplace_merge(begin(), end() - 1, end(), Compare());
elem[i] = *t;
n++;
} }
T *FindById(H h) { T *FindById(H h) {