[Glacier] Factor Vector iterator logic into standalone class.

This commit is contained in:
Drew Galbraith 2024-01-11 17:32:06 -08:00
parent b2354ae341
commit 66e7e95855
2 changed files with 42 additions and 32 deletions

View File

@ -0,0 +1,40 @@
#pragma once
#include <stdint.h>
namespace glcr {
template <typename T>
class ArrayIterator {
public:
ArrayIterator(T* item, uint64_t size) : item_(item), size_(size) {}
ArrayIterator next() {
if (size_ <= 1) {
return {nullptr, 0};
}
return {item_ + 1, size_ - 1};
}
ArrayIterator& operator++() {
if (size_ <= 1) {
item_ = nullptr;
size_ = 0;
} else {
item_++;
size_--;
}
return *this;
}
T& operator*() { return *item_; }
T* operator->() { return item_; }
bool operator==(const ArrayIterator& other) { return item_ == other.item_; }
bool operator!=(const ArrayIterator& other) { return item_ != other.item_; }
private:
T* item_;
uint64_t size_;
};
} // namespace glcr

View File

@ -1,5 +1,6 @@
#pragma once
#include <glacier/container/array_iter.h>
#include <glacier/memory/move.h>
#include <stdint.h>
@ -48,38 +49,7 @@ 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_;
};
typedef ArrayIterator<T> Iterator;
Iterator begin() { return {data_, size_}; }
const Iterator begin() const { return {data_, size_}; }