15 #ifndef SERIALBOX_CORE_STORAGEVIEWITERATOR_H 16 #define SERIALBOX_CORE_STORAGEVIEWITERATOR_H 22 #include <type_traits> 33 template <
class ValueType>
39 using value_type = ValueType;
40 using diffrence_type = std::ptrdiff_t;
41 using pointer = value_type*;
42 using refrence = value_type&;
44 using iterator_category = std::forward_iterator_tag;
65 const std::vector<int>& strides,
const Slice&
slice,
bool beginning)
66 : dims_(dims), strides_(strides), bytesPerElement_(bytesPerElement), slice_(slice) {
68 if(!(end_ = !beginning)) {
71 index_.resize(dims_.size(), 0);
74 index_.push_back(triple.start);
76 originPtr_ = originPtr;
93 return (curPtr_ == right.curPtr_ || (end_ ==
true && end_ == right.end_));
109 int size = index_.size();
110 for(
int i = 0; i < size; ++i)
126 int size = index_.size();
129 for(
int i = 0; i < size; ++i)
130 if((index_[i] += triples[i].step) < triples[i].stop)
133 index_[i] = triples[i].start;
151 std::swap(curPtr_, other.curPtr_);
152 index_.swap(other.index_);
154 std::swap(originPtr_, other.originPtr_);
155 dims_.swap(other.dims_);
156 strides_.swap(other.strides_);
157 slice_.
swap(other.slice_);
158 std::swap(bytesPerElement_, other.bytesPerElement_);
163 stream <<
"StorageViewIterator = {\n";
164 stream <<
" curPtr: " <<
static_cast<void*
>(it.curPtr_) <<
"\n";
165 stream <<
" index: [";
166 for(
auto i : it.index_)
168 stream <<
" ]\n end: " << std::boolalpha << it.end_ <<
"\n";
169 stream <<
" originPtr: " <<
static_cast<void*
>(it.originPtr_) <<
"\n";
170 stream <<
" dims: [";
171 for(
auto i : it.dims_)
173 stream <<
" ]\n strides: [";
174 for(
auto i : it.strides_)
176 stream <<
" ]\n bytesPerElement: " << it.bytesPerElement_ <<
"\n";
187 value_type*
ptr() noexcept {
return curPtr_; }
191 typename match_cv_qualifier<value_type, T>::type&
as() noexcept {
192 return *((T*)(curPtr_));
195 typename match_cv_qualifier<value_type, T>::type&
as()
const noexcept {
196 return *((T*)(curPtr_));
200 const std::vector<int>&
index() const noexcept {
return index_; }
206 const int size = index_.size();
207 for(
int i = 0; i < size; ++i)
208 pos += bytesPerElement_ * (strides_[i] * index_[i]);
215 std::vector<int> index_;
219 value_type* originPtr_;
220 std::vector<int> dims_;
221 std::vector<int> strides_;
222 int bytesPerElement_;
248 const std::vector<int>& dims,
const std::vector<int>& strides,
250 :
Base(originPtr, bytesPerElement, dims, strides, slice, beginning) {}
275 const std::vector<int>& dims,
const std::vector<int>& strides,
277 :
Base(originPtr, bytesPerElement, dims, strides, slice, beginning) {}
StorageViewIteratorBase(value_type *originPtr, int bytesPerElement, const std::vector< int > &dims, const std::vector< int > &strides, const Slice &slice, bool beginning)
Construct iterator at specific location in the data.
#define SERIALBOX_BUILTIN_LIKELY(x)
Mark this expression as being likely evaluated to "true".
refrence operator*() noexcept
Derefrence.
#define SERIALBOX_BUILTIN_UNLIKELY(x)
Mark this expression as being likely evaluated to "false".
friend std::ostream & operator<<(std::ostream &stream, const iterator &it)
Convert to stream.
value_type * ptr() noexcept
Get current data pointer.
Namespace of the serialbox library.
void swap(Slice &other) noexcept
Swap with other
StorageViewIterator(Base::value_type *originPtr, int bytesPerElement, const std::vector< int > &dims, const std::vector< int > &strides, const Slice &slice, bool beginning)
Construct iterator at specific location in the data.
const std::vector< int > & index() const noexcept
Get current index position in the data.
ConstStorageViewIterator(Base::value_type *originPtr, int bytesPerElement, const std::vector< int > &dims, const std::vector< int > &strides, const Slice &slice, bool beginning)
Construct iterator at specific location in the data.
bool operator!=(const iterator &right) const noexcept
Test for inequality.
void swap(iterator &other) noexcept
Swap with other.
iterator & operator=(const iterator &)=default
Copy assignment.
bool operator==(const iterator &right) const noexcept
Test for equality.
Non-mutable forward iterator to access the data of a StorageView.
int computeCurrentIndex() const noexcept
Compute the current linear index in the data according to the index vector.
std::vector< SliceTriple > & sliceTriples() noexcept
Get slice triples.
match_cv_qualifier< value_type, T >::type & as() noexcept
Interpret current data pointer as type ´T´
StorageViewIteratorBase(const iterator &)=default
Copy constructor.
bool empty() const noexcept
Check if slice is empty.
iterator & operator++() noexcept
Pre-increment.
Forward iterator to access the data of a StorageView.
int bytesPerElement() noexcept
Get bytes per element.
Specification of the slice indices which is used for partial loading of serialized data...
Mutable forward iterator to access the data of a StorageView.