[Glacier] Add a vector iterator.

This commit is contained in:
Drew Galbraith 2024-01-11 16:59:10 -08:00
parent 36a09f98c9
commit c06d1741f3
2 changed files with 63 additions and 0 deletions

View file

@ -47,6 +47,44 @@ class Vector {
T&& PopBack();
// Forward Iter
class Iterator {
public:
Iterator(T* item, uint64_t size) : item_(item), size_(size) {}
Iterator next() {
if (size_ <= 1) {
return {nullptr, 0};
}
return {item_ + 1, size_ - 1};
}
Iterator& operator++() {
if (size_ <= 1) {
item_ = nullptr;
size_ = 0;
} else {
item_++;
size_--;
}
return *this;
}
T& operator*() { return *item_; }
T* operator->() { return item_; }
bool operator==(const Iterator& other) { return item_ == other.item_; }
bool operator!=(const Iterator& other) { return item_ != other.item_; }
private:
T* item_;
uint64_t size_;
};
Iterator begin() { return {data_, size_}; }
const Iterator begin() const { return {data_, size_}; }
Iterator end() { return {nullptr, 0}; }
const Iterator end() const { return {nullptr, 0}; }
private:
T* data_;
uint64_t size_;