From 894021ab61f78ff42ecc7004574403212c49b891 Mon Sep 17 00:00:00 2001 From: Pagwin Date: Tue, 19 Nov 2024 22:36:09 -0500 Subject: [PATCH] Redid iterator bookkeeping Everything works via a bookkeeping type which tracks a bunch of stuff like the color and neighbors. This is needed to make the iterator go brrrr instead of needing to relocate the relevant tree node every time we want to increment the iterator. --- Map.hpp | 74 +++++++++++++++++++++------------------------------------ 1 file changed, 27 insertions(+), 47 deletions(-) diff --git a/Map.hpp b/Map.hpp index f04a4d8..a23cd52 100644 --- a/Map.hpp +++ b/Map.hpp @@ -11,8 +11,20 @@ #include namespace cs440 { -enum class Color { Red, Black }; +template class Map; +namespace { +enum class Color { Red, Black }; +template struct BookKeeping { + using ValueType = std::pair; + Map &parent; + ValueType value; + std::size_t self; + std::size_t next; + std::size_t prev; + Color color; +}; +} // namespace // https://en.wikipedia.org/wiki/Red%E2%80%93black_tree template class Map { private: @@ -20,74 +32,42 @@ private: // idx 0 = root // left = parent * 2 + 1 // right = parent * 2 + 2 - std::vector> store; + std::vector>> store; std::vector coloration; public: class Iterator; class ConstIterator; class ReverseIterator; + + friend class Iterator; + friend class ConstIterator; + friend class ReverseIterator; // TODO: Iterator functionality class Iterator { public: private: - // in order traversal, deque used as the stack we traverse up after each - // right hand side traversal completes - // - // if a right child exists traverse down to the left most child of that - // child pushing to stack elements that are passed if it doesn't pop from - // the stack - // - std::vector &parent; - std::deque forward_traversal; - std::deque backward_traversal; - std::size_t current; - + BookKeeping &parent; + // TODO: replace queues with functions that just go through the entire in + // order traversal and give the next/previous element public: Iterator() = delete; - Iterator(std::vector &parent, std::size_t current) - : parent{parent}, forward_traversal{}, backward_traversal{}, - current{current} {} - ConstIterator to_const() const { return ConstIterator(this); } - Iterator &operator++() { - backward_traversal.push_back(current); - std::size_t next = current * 2 + 2; - while (next < parent.size() && parent.at(next).has_value()) { - forward_traversal.push_back(next); - next = next * 2 + 1; - } - current = forward_traversal.back(); - forward_traversal.pop_back(); - if (this->forward_traversal.empty()) { - // deref can just check for nullopt to see if it's end - current = next; - } else { - // pop from stack - current = forward_traversal.back(); - forward_traversal.pop_back(); - } - return *this; - } + Iterator(std::optional> &parent) + : parent{parent} {} + ConstIterator to_const() const { return ConstIterator(*this); } + Iterator &operator++() { return *this; } Iterator operator++(int) { Iterator tmp = *this; ++(*this); return tmp; } - Iterator &operator--() { - std::size_t tmp = backward_traversal.back(); - backward_traversal.pop_back(); - if (tmp > current) { - forward_traversal.push_back(current); - } - current = tmp; - return *this; - } + Iterator &operator--() { return *this; } Iterator operator--(int) { Iterator tmp = *this; --(*this); return tmp; } - ValueType &operator*() const { return parent.at(current); } + ValueType &operator*() const { return parent.parent.at(parent.self); } ValueType *operator->() const { return &**this; } friend bool operator==(Iterator const &lhs, Iterator const &rhs) { return lhs.store_iter == rhs.store_iter;