3 #include <torch/csrc/utils/variadic.h> 4 #include <torch/types.h> 6 #include <c10/util/Exception.h> 11 #include <type_traits> 25 template <
typename Batch>
28 template <
typename Batch>
32 template <
typename Batch>
35 virtual void next() = 0;
36 virtual Batch&
get() = 0;
37 virtual bool operator==(
const IteratorImpl& other)
const = 0;
42 template <
typename Batch>
44 using BatchProducer = std::function<optional<Batch>()>;
47 : next_batch_(std::move(next_batch)) {}
54 batch_.has_value(),
"Attempted to increment iterator past the end");
56 batch_ = next_batch_();
62 Batch&
get()
override {
67 "Attempted to dereference iterator that was past the end");
68 return batch_.value();
73 return other == *
this;
85 return &other ==
this;
91 batch_ = next_batch_();
96 BatchProducer next_batch_;
98 mutable bool initialized_ =
false;
101 template <
typename Batch>
103 void next()
override {
105 "Incrementing the DataLoader's past-the-end iterator is not allowed");
108 Batch&
get()
override {
110 "Dereferencing the DataLoader's past-the-end iterator is not allowed");
115 return other == *
this;
121 return other == *
this;
131 template <
typename Batch>
135 using difference_type = std::ptrdiff_t;
136 using value_type = Batch;
137 using pointer = Batch*;
138 using reference = Batch&;
139 using iterator_category = std::input_iterator_tag;
142 : impl_(std::move(impl)) {}
160 return &impl_->get();
165 return *impl_ == *other.impl_;
170 return !(*
this == other);
175 std::shared_ptr<detail::IteratorImpl<Batch>> impl_;
bool operator==(const IteratorImpl< Batch > &other) const override
Does double dispatch.
bool operator==(const ValidIterator< Batch > &other) const override
Returns true if the memory address of other equals that of this.
Base class for the ValidIterator and SentinelIterator
Batch * operator->()
Returns a pointer to the current batch.
bool operator==(const Iterator &other) const
Compares two iterators for equality.
bool operator==(const SentinelIterator< Batch > &) const override
A ValidIterator is equal to the SentinelIterator iff.
bool operator==(const ValidIterator< Batch > &other) const override
Calls the comparison operator between ValidIterator and SentinelIterator.
Iterator & operator++()
Increments the iterator.
bool operator==(const SentinelIterator< Batch > &other) const override
Sentinel iterators always compare equal.
Batch & operator*()
Returns the current batch.
bool operator==(const IteratorImpl< Batch > &other) const override
Does double dispatch.
bool operator!=(const Iterator &other) const
Compares two iterators for inequality.
void next() override
Fetches the next batch.
void lazy_initialize() const
Gets the very first batch if it has not yet been fetched.