find and iterator stuff done kinda

This commit is contained in:
Pagwin 2024-11-18 17:24:59 -05:00
parent e3ba3ec09f
commit ee7aad38f0
No known key found for this signature in database
GPG key ID: 81137023740CA260

82
Map.hpp
View file

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