diff options
author | pechatnov <pechatnov@yandex-team.ru> | 2022-02-10 16:48:57 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:48:57 +0300 |
commit | 132a3640fac343164b858d0a914e020a848a2848 (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /library/cpp/iterator/mapped.h | |
parent | 8e9b2f8bbf4a2320f539eef5b85555f42c065425 (diff) | |
download | ydb-132a3640fac343164b858d0a914e020a848a2848.tar.gz |
Restoring authorship annotation for <pechatnov@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/iterator/mapped.h')
-rw-r--r-- | library/cpp/iterator/mapped.h | 128 |
1 files changed, 64 insertions, 64 deletions
diff --git a/library/cpp/iterator/mapped.h b/library/cpp/iterator/mapped.h index 486efd0899..6c5e763184 100644 --- a/library/cpp/iterator/mapped.h +++ b/library/cpp/iterator/mapped.h @@ -1,33 +1,33 @@ #pragma once #include <util/generic/iterator_range.h> -#include <util/generic/store_policy.h> +#include <util/generic/store_policy.h> + +#include <iterator> + -#include <iterator> - - namespace NIteratorPrivate { - template <class TIterator> - constexpr bool HasRandomAccess() { - return std::is_same_v<typename std::iterator_traits<TIterator>::iterator_category, - std::random_access_iterator_tag>; - } -}; - - + template <class TIterator> + constexpr bool HasRandomAccess() { + return std::is_same_v<typename std::iterator_traits<TIterator>::iterator_category, + std::random_access_iterator_tag>; + } +}; + + template <class TIterator, class TMapper> class TMappedIterator { protected: using TSelf = TMappedIterator<TIterator, TMapper>; using TSrcPointerType = typename std::iterator_traits<TIterator>::reference; using TValue = typename std::invoke_result_t<TMapper, TSrcPointerType>; -public: +public: 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>; + std::random_access_iterator_tag, std::input_iterator_tag>; TMappedIterator(TIterator it, TMapper mapper) : Iter(it) @@ -43,9 +43,9 @@ public: --Iter; return *this; } - TValue operator*() { - return Mapper((*Iter)); - } + TValue operator*() { + return Mapper((*Iter)); + } TValue operator*() const { return Mapper((*Iter)); } @@ -92,13 +92,13 @@ private: TMapper Mapper; }; - + template <class TContainer, class TMapper> -class TInputMappedRange { -protected: - using TContainerStorage = TAutoEmbedOrPtrPolicy<TContainer>; - using TMapperStorage = TAutoEmbedOrPtrPolicy<TMapper>; - using TMapperWrapper = std::reference_wrapper<std::remove_reference_t<TMapper>>; +class TInputMappedRange { +protected: + using TContainerStorage = TAutoEmbedOrPtrPolicy<TContainer>; + using TMapperStorage = TAutoEmbedOrPtrPolicy<TMapper>; + using TMapperWrapper = std::reference_wrapper<std::remove_reference_t<TMapper>>; using TInternalIterator = decltype(std::begin(std::declval<TContainer&>())); using TIterator = TMappedIterator<TInternalIterator, TMapperWrapper>; public: @@ -108,68 +108,68 @@ public: using reference = typename TIterator::reference; using const_reference = typename TIterator::reference; - TInputMappedRange(TContainer&& container, TMapper&& mapper) - : Container(std::forward<TContainer>(container)) - , Mapper(std::forward<TMapper>(mapper)) + TInputMappedRange(TContainer&& container, TMapper&& mapper) + : Container(std::forward<TContainer>(container)) + , Mapper(std::forward<TMapper>(mapper)) { } TIterator begin() const { - return {std::begin(*Container.Ptr()), {*Mapper.Ptr()}}; + return {std::begin(*Container.Ptr()), {*Mapper.Ptr()}}; } TIterator end() const { - return {std::end(*Container.Ptr()), {*Mapper.Ptr()}}; + return {std::end(*Container.Ptr()), {*Mapper.Ptr()}}; } bool empty() const { return std::begin(*Container.Ptr()) == std::end(*Container.Ptr()); } -protected: - mutable TContainerStorage Container; - mutable TMapperStorage Mapper; -}; +protected: + mutable TContainerStorage Container; + mutable TMapperStorage Mapper; +}; + - -template <class TContainer, class TMapper> -class TRandomAccessMappedRange : public TInputMappedRange<TContainer, TMapper> { - using TBase = TInputMappedRange<TContainer, TMapper>; +template <class TContainer, class TMapper> +class TRandomAccessMappedRange : public TInputMappedRange<TContainer, TMapper> { + using TBase = TInputMappedRange<TContainer, TMapper>; using TInternalIterator = typename TBase::TInternalIterator; using TIterator = typename TBase::TIterator; -public: - using iterator = typename TBase::iterator; - using const_iterator = typename TBase::const_iterator; - using value_type = typename TBase::value_type; - using reference = typename TBase::reference; - using const_reference = typename TBase::const_reference; - - using difference_type = typename std::iterator_traits<iterator>::difference_type; - using size_type = std::size_t; - - TRandomAccessMappedRange(TContainer&& container, TMapper&& mapper) - : TBase(std::forward<TContainer>(container), std::forward<TMapper>(mapper)) - { - } - - using TBase::begin; - using TBase::end; +public: + using iterator = typename TBase::iterator; + using const_iterator = typename TBase::const_iterator; + using value_type = typename TBase::value_type; + using reference = typename TBase::reference; + using const_reference = typename TBase::const_reference; + + using difference_type = typename std::iterator_traits<iterator>::difference_type; + using size_type = std::size_t; + + TRandomAccessMappedRange(TContainer&& container, TMapper&& mapper) + : TBase(std::forward<TContainer>(container), std::forward<TMapper>(mapper)) + { + } + + using TBase::begin; + using TBase::end; using TBase::empty; - + size_type size() const { - return std::end(*this->Container.Ptr()) - std::begin(*this->Container.Ptr()); + return std::end(*this->Container.Ptr()) - std::begin(*this->Container.Ptr()); } const_reference operator[](size_t at) const { - Y_ASSERT(at < this->size()); + Y_ASSERT(at < this->size()); - return *(this->begin() + at); + return *(this->begin() + at); } reference operator[](size_t at) { - Y_ASSERT(at < this->size()); + Y_ASSERT(at < this->size()); - return *(this->begin() + at); + return *(this->begin() + at); } }; @@ -184,10 +184,10 @@ auto MakeMappedRange(TIterator begin, TIterator end, TMapper mapper) { } template <class TContainer, class TMapper> -auto MakeMappedRange(TContainer&& container, TMapper&& mapper) { +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)); - } + 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)); + } } |