From 61a710f938ba0ac6c06b5017d186fb5415d035e4 Mon Sep 17 00:00:00 2001 From: Pagwin Date: Thu, 21 Nov 2024 19:04:54 -0500 Subject: [PATCH] have self iterator for easier erasure and revalidate them all after erasure but realizing that that isn't good enough for performance so might need to shift to using unique_ptr to do proper pointers --- Map.hpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Map.hpp b/Map.hpp index 476a917..b82f0e2 100644 --- a/Map.hpp +++ b/Map.hpp @@ -36,6 +36,7 @@ template struct BookKeeping { friend class Map; Map &container; ValueType value; + typename std::vector::iterator self; Color color; // nullptr indicates empty Self *parent; @@ -559,7 +560,10 @@ private: public: // TODO: check that the way of reconnecting next and prev works + // TODO: need to revalidate void erase(Iterator pos) { + auto revalidate_begin = pos.ref->container.begin() - pos.ref->self; + auto &container = pos.ref->container; // simple cases Node *ref = pos.ref; // 2 children @@ -604,6 +608,11 @@ public: else { this->complex_erase(pos); } + // need to revalidate iterators now so future erasures aren't fucked + for (auto iter = container.begin() + revalidate_begin; + iter != container.end(); ++iter) { + iter->self = iter; + } } void erase(const Key_T &key) { this->erase(this->find(key)); } void clear() {