size < and at implemented, iterator still scaring me
This commit is contained in:
parent
c81eaa40cc
commit
a12ee91033
1 changed files with 63 additions and 7 deletions
70
Map.hpp
70
Map.hpp
|
@ -139,9 +139,14 @@ public:
|
|||
// who cares we're using vector
|
||||
~Map() {}
|
||||
|
||||
size_t size() const { return this->store.size(); }
|
||||
size_t size() const {
|
||||
std::size_t count = 0;
|
||||
for (auto &m_pair : this->store) {
|
||||
count += m_pair.has_value() ? 1 : 0;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
bool empty() const { return this->store.empty(); }
|
||||
// TODO: iterator creation
|
||||
Iterator begin() {
|
||||
std::size_t start = 0;
|
||||
while (2 * (start - store.begin()) + 1 < store.size()) {
|
||||
|
@ -194,9 +199,27 @@ public:
|
|||
}
|
||||
}
|
||||
}
|
||||
Mapped_T &at(const Key_T &key) {}
|
||||
const Mapped_T &at(const Key_T &key) const {}
|
||||
Mapped_T &operator[](const Key_T &) {}
|
||||
|
||||
Mapped_T &at(const Key_T &key) {
|
||||
std::size_t i = 0;
|
||||
while (this->store.at(i).has_value()) {
|
||||
switch (true) {
|
||||
case this->store.at(i).first == key:
|
||||
return this->store.at(i).second;
|
||||
case this->store.at(i).first < key:
|
||||
i = 2 * i + 1;
|
||||
break;
|
||||
case this->store.at(i).first > key:
|
||||
i = 2 * i + 2;
|
||||
break;
|
||||
}
|
||||
}
|
||||
throw std::out_of_range{""};
|
||||
}
|
||||
const Mapped_T &at(const Key_T &key) const { return this->at(key); }
|
||||
Mapped_T &operator[](const Key_T &key) { return this->at(key); }
|
||||
|
||||
// TODO: single insert
|
||||
std ::pair<Iterator, bool> insert(const ValueType &) {}
|
||||
template <typename IT_T> void insert(IT_T range_beg, IT_T range_end) {
|
||||
std::for_each(range_beg, range_end,
|
||||
|
@ -225,7 +248,40 @@ public:
|
|||
friend bool operator!=(const Map &lhs, const Map &rhs) {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
// TODO < operator
|
||||
friend bool operator<(const Map &lhs, const Map &rhs) { return false; }
|
||||
friend bool operator<(const Map &lhs, const Map &rhs) {
|
||||
std::size_t lhs_i = 0;
|
||||
std::size_t rhs_i = 0;
|
||||
for (; lhs_i < lhs.store.size() && rhs_i < rhs.store.size();
|
||||
lhs_i++, rhs_i++) {
|
||||
bool lhs_exhaust = false;
|
||||
while (!lhs.store[lhs_i].has_value()) {
|
||||
lhs_i++;
|
||||
if (lhs.store.size() >= lhs_i) {
|
||||
lhs_exhaust = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
bool rhs_exhaust = false;
|
||||
while (!rhs.store[rhs_i].has_value()) {
|
||||
rhs_i++;
|
||||
if (rhs.store.size() >= rhs_i) {
|
||||
rhs_exhaust = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (lhs_exhaust && !rhs_exhaust) {
|
||||
return true;
|
||||
}
|
||||
if (lhs_exhaust || rhs_exhaust) {
|
||||
break;
|
||||
}
|
||||
if (lhs.store[lhs_i] != rhs.store[rhs_i]) {
|
||||
return lhs.store[lhs_i] < rhs.store[rhs_i];
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
} // namespace cs440
|
||||
|
|
Loading…
Reference in a new issue