From 60fdac141d9667edf5cf0d08a2e1720748bac605 Mon Sep 17 00:00:00 2001 From: Ryan Pavlik Date: Wed, 22 May 2019 16:09:43 -0500 Subject: [PATCH] Simplify IdList::Add(). NFC. Offloads most of the work onto standard algorithms to make it more self-evidently correct. --- src/dsc.h | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/dsc.h b/src/dsc.h index a244be0b..042d1d02 100644 --- a/src/dsc.h +++ b/src/dsc.h @@ -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(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) {