diff --git a/Map.hpp b/Map.hpp index cc2d08e..f04a4d8 100644 --- a/Map.hpp +++ b/Map.hpp @@ -11,6 +11,9 @@ #include namespace cs440 { +enum class Color { Red, Black }; + +// https://en.wikipedia.org/wiki/Red%E2%80%93black_tree template class Map { private: using ValueType = std::pair; @@ -18,6 +21,7 @@ private: // left = parent * 2 + 1 // right = parent * 2 + 2 std::vector> store; + std::vector coloration; public: class Iterator; @@ -46,21 +50,45 @@ public: current{current} {} ConstIterator to_const() const { return ConstIterator(this); } Iterator &operator++() { - // TODO: implement this + 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 operator++(int) { Iterator tmp = *this; ++(*this); return tmp; } - Iterator &operator--() {} + 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--(int) { Iterator tmp = *this; --(*this); return tmp; } - ValueType &operator*() const { return this->store_iter->value(); } - ValueType *operator->() const { return &this->store_iter->value(); } + ValueType &operator*() const { return parent.at(current); } + ValueType *operator->() const { return &**this; } friend bool operator==(Iterator const &lhs, Iterator const &rhs) { return lhs.store_iter == rhs.store_iter; } @@ -234,13 +262,16 @@ public: Mapped_T &operator[](const Key_T &key) { return this->at(key); } // TODO: single insert + // OH NO IT SHOULD BE RED-BLACK std ::pair insert(const ValueType &) {} template void insert(IT_T range_beg, IT_T range_end) { std::for_each(range_beg, range_end, [&](ValueType &val) { this->insert(val); }); } // TODO: erase via iterator - void erase(Iterator pos) {} + void erase(Iterator pos) { + // RED BLACK TREE oh no + } void erase(const Key_T &key) { this->erase(this->find(key)); } void clear() { this->store = {}; } friend bool operator==(const Map &lhs, const Map &rhs) {