diff --git a/Map.hpp b/Map.hpp index ba366fb..4c8a65c 100644 --- a/Map.hpp +++ b/Map.hpp @@ -20,7 +20,10 @@ private: public: // TODO: Iterator functionality class Iterator { + public: using underlying = typename std::vector>::iterator; + + private: underlying store_iter; public: @@ -28,8 +31,11 @@ public: Iterator(underlying iter) : store_iter{iter} {} }; class ConstIterator { + public: using underlying = typename std::vector>::const_iterator; + + private: underlying store_iter; public: @@ -37,7 +43,10 @@ public: ConstIterator(underlying iter) : store_iter{iter} {} }; class ReverseIterator { + public: using underlying = typename std::vector>::iterator; + + private: underlying store_iter; public: @@ -57,28 +66,57 @@ public: bool empty() const { return this->store.empty(); } // TODO: iterator creation Iterator begin() { - Key_T const &start = - std::min_element(store.begin(), store.end(), - [](std::optional const &lhs, - std::optional const &rhs) { - if (lhs.has_value() != rhs.has_value()) { - return lhs.has_value(); - } - - return lhs.value().first < rhs.value().first; - }) - ->first; - return Iterator(*this, start); + std::size_t start = 0; + while (2 * (start - store.begin()) + 1 < store.size()) { + store = 2 * store + 2; + } + return Iterator(store.cbegin() + start); + } + Iterator end() { return Iterator(store.end()); } + ConstIterator begin() const { + std::size_t start = 0; + while (2 * (start - store.begin()) + 1 < store.size()) { + store = 2 * store + 2; + } + return ConstIterator(store.cbegin() + start); + } + ConstIterator end() const { return ConstIterator(store.cend()); } + ConstIterator cbegin() const { return this->begin(); } + ConstIterator cend() const { return this->end(); } + ReverseIterator rbegin() { + std::size_t start = 0; + while (2 * (start - store.begin()) + 1 < store.size()) { + store = 2 * store + 2; + } + return ReverseIterator(store.begin() + start); + } + ReverseIterator rend() { return ReverseIterator(store.end()); } + Iterator find(const Key_T &key) { + std::size_t idx = 0; + while (store[idx].first != key) { + if (idx >= store.size()) { + return this->end(); + } + if (store[idx].first < key) { + idx = idx * 2 + 1; + } else { + idx = idx * 2 + 2; + } + } + } + ConstIterator find(const Key_T &key) const { + std::size_t idx = 0; + while (store[idx].first != key) { + if (idx >= store.size()) { + return this->end(); + } + if (store[idx].first < key) { + idx = idx * 2 + 1; + } else { + idx = idx * 2 + 2; + } + } } - Iterator end() { std::optional return Iterator(*this, ); } - ConstIterator begin() const { return ConstIterator(*this, ); } - ConstIterator end() const { return this->cbegin(); } - ConstIterator cbegin() const { return this->cend(); } - ConstIterator cend() const { return ConstIterator(*this, ); } - ReverseIterator rbegin() { return Iterator(*this, ); } - ReverseIterator rend() { return Iterator(*this, ); } - Iterator find(const Key_T &) {} - ConstIterator find(const Key_T &) const; Mapped_T &at(const Key_T &key) {} const Mapped_T &at(const Key_T &key) const {} Mapped_T &operator[](const Key_T &) {} @@ -111,6 +149,6 @@ public: return !(lhs == rhs); } // TODO < operator - friend bool operator<(const Map &lhs, const Map &rhs) {} + friend bool operator<(const Map &lhs, const Map &rhs) { return false; } }; } // namespace cs440