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