diff options
author | mstebelev <mstebelev@yandex-team.ru> | 2022-02-10 16:47:17 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:47:17 +0300 |
commit | 5a176099114d03d5669e95a55a3a514bd24dd8b1 (patch) | |
tree | 13d2cd27a59ccff4e44d7c7f530f923483f82267 /library/cpp/iterator/mapped.h | |
parent | 3c1fe03778c21d7aa4a5c4a4433ca611dbf2f785 (diff) | |
download | ydb-5a176099114d03d5669e95a55a3a514bd24dd8b1.tar.gz |
Restoring authorship annotation for <mstebelev@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/iterator/mapped.h')
-rw-r--r-- | library/cpp/iterator/mapped.h | 152 |
1 files changed, 76 insertions, 76 deletions
diff --git a/library/cpp/iterator/mapped.h b/library/cpp/iterator/mapped.h index 6c5e763184..255ebb1803 100644 --- a/library/cpp/iterator/mapped.h +++ b/library/cpp/iterator/mapped.h @@ -1,8 +1,8 @@ -#pragma once - -#include <util/generic/iterator_range.h> +#pragma once + +#include <util/generic/iterator_range.h> #include <util/generic/store_policy.h> - + #include <iterator> @@ -15,83 +15,83 @@ namespace NIteratorPrivate { }; -template <class TIterator, class TMapper> -class TMappedIterator { +template <class TIterator, class TMapper> +class TMappedIterator { protected: - using TSelf = TMappedIterator<TIterator, TMapper>; - using TSrcPointerType = typename std::iterator_traits<TIterator>::reference; + using TSelf = TMappedIterator<TIterator, TMapper>; + using TSrcPointerType = typename std::iterator_traits<TIterator>::reference; using TValue = typename std::invoke_result_t<TMapper, TSrcPointerType>; public: - using difference_type = std::ptrdiff_t; - using value_type = TValue; - using reference = TValue&; - using pointer = std::remove_reference_t<TValue>*; + using difference_type = std::ptrdiff_t; + using value_type = TValue; + using reference = TValue&; + using pointer = std::remove_reference_t<TValue>*; using iterator_category = std::conditional_t<NIteratorPrivate::HasRandomAccess<TIterator>(), std::random_access_iterator_tag, std::input_iterator_tag>; - + TMappedIterator(TIterator it, TMapper mapper) - : Iter(it) + : Iter(it) , Mapper(std::move(mapper)) - { - } - - TSelf& operator++() { - ++Iter; - return *this; - } - TSelf& operator--() { - --Iter; - return *this; - } + { + } + + TSelf& operator++() { + ++Iter; + return *this; + } + TSelf& operator--() { + --Iter; + return *this; + } TValue operator*() { return Mapper((*Iter)); } - TValue operator*() const { - return Mapper((*Iter)); - } - - pointer operator->() const { - return &(Mapper((*Iter))); - } - - TValue operator[](difference_type n) const { - return Mapper(*(Iter + n)); - } - TSelf& operator+=(difference_type n) { - Iter += n; - return *this; - } - TSelf& operator-=(difference_type n) { - Iter -= n; - return *this; - } - TSelf operator+(difference_type n) const { - return TSelf(Iter + n, Mapper); - } + TValue operator*() const { + return Mapper((*Iter)); + } + + pointer operator->() const { + return &(Mapper((*Iter))); + } + + TValue operator[](difference_type n) const { + return Mapper(*(Iter + n)); + } + TSelf& operator+=(difference_type n) { + Iter += n; + return *this; + } + TSelf& operator-=(difference_type n) { + Iter -= n; + return *this; + } + TSelf operator+(difference_type n) const { + return TSelf(Iter + n, Mapper); + } TSelf operator-(difference_type n) const { return TSelf(Iter - n, Mapper); } - difference_type operator-(const TSelf& other) const { - return Iter - other.Iter; - } - bool operator==(const TSelf& other) const { - return Iter == other.Iter; - } - bool operator!=(const TSelf& other) const { - return Iter != other.Iter; - } - bool operator>(const TSelf& other) const { - return Iter > other.Iter; - } - bool operator<(const TSelf& other) const { - return Iter < other.Iter; - } - -private: - TIterator Iter; + difference_type operator-(const TSelf& other) const { + return Iter - other.Iter; + } + bool operator==(const TSelf& other) const { + return Iter == other.Iter; + } + bool operator!=(const TSelf& other) const { + return Iter != other.Iter; + } + bool operator>(const TSelf& other) const { + return Iter > other.Iter; + } + bool operator<(const TSelf& other) const { + return Iter < other.Iter; + } + +private: + TIterator Iter; TMapper Mapper; -}; - +}; + template <class TContainer, class TMapper> class TInputMappedRange { @@ -173,21 +173,21 @@ public: } }; -template <class TIterator, class TMapper> +template <class TIterator, class TMapper> TMappedIterator<TIterator, TMapper> MakeMappedIterator(TIterator iter, TMapper mapper) { return {iter, mapper}; -} - -template <class TIterator, class TMapper> +} + +template <class TIterator, class TMapper> auto MakeMappedRange(TIterator begin, TIterator end, TMapper mapper) { - return MakeIteratorRange(MakeMappedIterator(begin, mapper), MakeMappedIterator(end, mapper)); -} - -template <class TContainer, class TMapper> + return MakeIteratorRange(MakeMappedIterator(begin, mapper), MakeMappedIterator(end, mapper)); +} + +template <class TContainer, class TMapper> auto MakeMappedRange(TContainer&& container, TMapper&& mapper) { if constexpr (NIteratorPrivate::HasRandomAccess<decltype(std::begin(container))>()) { return TRandomAccessMappedRange<TContainer, TMapper>(std::forward<TContainer>(container), std::forward<TMapper>(mapper)); } else { return TInputMappedRange<TContainer, TMapper>(std::forward<TContainer>(container), std::forward<TMapper>(mapper)); } -} +} |