diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/generic/iterator_range.h | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/generic/iterator_range.h')
-rw-r--r-- | util/generic/iterator_range.h | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/util/generic/iterator_range.h b/util/generic/iterator_range.h new file mode 100644 index 0000000000..9f4d02da29 --- /dev/null +++ b/util/generic/iterator_range.h @@ -0,0 +1,104 @@ +#pragma once + +#include <util/system/yassert.h> + +#include <iterator> +#include <utility> + +template <typename TBegin, typename TEnd = TBegin> +struct TIteratorRange { + using TElement = std::remove_reference_t<decltype(*std::declval<TBegin>())>; + + TIteratorRange(TBegin begin, TEnd end) + : Begin_(begin) + , End_(end) + { + } + + TIteratorRange() + : TIteratorRange(TBegin{}, TEnd{}) + { + } + + TBegin begin() const { + return Begin_; + } + + TEnd end() const { + return End_; + } + + bool empty() const { + // because range based for requires exactly '!=' + return !(Begin_ != End_); + } + +private: + TBegin Begin_; + TEnd End_; +}; + +template <class TIterator> +class TIteratorRange<TIterator, TIterator> { +public: + using iterator = TIterator; + using const_iterator = TIterator; + using value_type = typename std::iterator_traits<iterator>::value_type; + using reference = typename std::iterator_traits<iterator>::reference; + using const_reference = typename std::iterator_traits<const_iterator>::reference; + using difference_type = typename std::iterator_traits<iterator>::difference_type; + using size_type = std::size_t; + + TIteratorRange() + : Begin_() + , End_() + { + } + + TIteratorRange(TIterator begin, TIterator end) + : Begin_(begin) + , End_(end) + { + } + + TIterator begin() const { + return Begin_; + } + + TIterator end() const { + return End_; + } + + Y_PURE_FUNCTION bool empty() const { + return Begin_ == End_; + } + + size_type size() const { + return End_ - Begin_; + } + + reference operator[](size_t at) const { + Y_ASSERT(at < size()); + + return *(Begin_ + at); + } + +private: + TIterator Begin_; + TIterator End_; +}; + +template <class TIterator> +TIteratorRange<TIterator> MakeIteratorRange(TIterator begin, TIterator end) { + return TIteratorRange<TIterator>(begin, end); +} + +template <class TIterator> +TIteratorRange<TIterator> MakeIteratorRange(const std::pair<TIterator, TIterator>& range) { + return TIteratorRange<TIterator>(range.first, range.second); +} + +template <class TBegin, class TEnd> +TIteratorRange<TBegin, TEnd> MakeIteratorRange(TBegin begin, TEnd end) { + return {begin, end}; +} |