From 66e7e95855e37b186d2ca6641a98b47ad317b92d Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Thu, 11 Jan 2024 17:32:06 -0800 Subject: [PATCH] [Glacier] Factor Vector iterator logic into standalone class. --- lib/glacier/container/array_iter.h | 40 ++++++++++++++++++++++++++++++ lib/glacier/container/vector.h | 34 ++----------------------- 2 files changed, 42 insertions(+), 32 deletions(-) create mode 100644 lib/glacier/container/array_iter.h diff --git a/lib/glacier/container/array_iter.h b/lib/glacier/container/array_iter.h new file mode 100644 index 0000000..195e9df --- /dev/null +++ b/lib/glacier/container/array_iter.h @@ -0,0 +1,40 @@ +#pragma once + +#include + +namespace glcr { + +template +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 diff --git a/lib/glacier/container/vector.h b/lib/glacier/container/vector.h index eafeb78..5544436 100644 --- a/lib/glacier/container/vector.h +++ b/lib/glacier/container/vector.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -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 Iterator; Iterator begin() { return {data_, size_}; } const Iterator begin() const { return {data_, size_}; }