[Glacier] Factor Vector iterator logic into standalone class.
This commit is contained in:
parent
b2354ae341
commit
66e7e95855
|
@ -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
|
|
@ -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_}; }
|
||||
|
|
Loading…
Reference in New Issue