diff options
author | anelyubin <anelyubin@yandex-team.ru> | 2022-02-10 16:49:39 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:39 +0300 |
commit | 8d333d5e773f6b0ef31e7b3c92339af7e71413f6 (patch) | |
tree | be79c5bb4a39ab3410c17c33f1aff87810558f54 /util/generic | |
parent | ad8c15be8c1a4f48a52390f23cb735060461684e (diff) | |
download | ydb-8d333d5e773f6b0ef31e7b3c92339af7e71413f6.tar.gz |
Restoring authorship annotation for <anelyubin@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'util/generic')
-rw-r--r-- | util/generic/bitops.h | 8 | ||||
-rw-r--r-- | util/generic/fastqueue.h | 92 | ||||
-rw-r--r-- | util/generic/strbuf.h | 8 | ||||
-rw-r--r-- | util/generic/utility.cpp | 2 | ||||
-rw-r--r-- | util/generic/xrange.cpp | 2 | ||||
-rw-r--r-- | util/generic/xrange.h | 110 | ||||
-rw-r--r-- | util/generic/xrange_ut.cpp | 52 | ||||
-rw-r--r-- | util/generic/yexception.h | 2 |
8 files changed, 138 insertions, 138 deletions
diff --git a/util/generic/bitops.h b/util/generic/bitops.h index 2db15fc59b..7dedd06efe 100644 --- a/util/generic/bitops.h +++ b/util/generic/bitops.h @@ -4,7 +4,7 @@ #include "typelist.h" #include <util/system/compiler.h> -#include <util/system/yassert.h> +#include <util/system/yassert.h> #ifdef _MSC_VER #include <intrin.h> @@ -98,7 +98,7 @@ namespace NBitOps { Y_ASSERT(value); // because __builtin_ctz* have undefined result for zero. return __builtin_ctzl(value); } - + inline unsigned CountTrailingZeroBitsImpl(unsigned long long value) noexcept { Y_ASSERT(value); // because __builtin_ctz* have undefined result for zero. return __builtin_ctzll(value); @@ -113,7 +113,7 @@ namespace NBitOps { value >>= 1; ++result; } - + return result; } #endif @@ -210,7 +210,7 @@ static inline T FastClp2(T t) noexcept { Y_ASSERT(t > 0); using TCvt = typename ::TUnsignedInts::template TSelectBy<TSizeOfPredicate<sizeof(T)>::template TResult>::type; return 1 + ::NBitOps::NPrivate::TClp2Helper<sizeof(TCvt) * 4, T>::Calc(static_cast<TCvt>(t)); -} +} /** * Check if integer is a power of 2. diff --git a/util/generic/fastqueue.h b/util/generic/fastqueue.h index 1fee5b86f6..c67947b102 100644 --- a/util/generic/fastqueue.h +++ b/util/generic/fastqueue.h @@ -1,54 +1,54 @@ -#pragma once - -#include <util/memory/smallobj.h> +#pragma once + +#include <util/memory/smallobj.h> #include "ptr.h" - -template <class T> -class TFastQueue { - struct THelper: public TObjectFromPool<THelper>, public TIntrusiveListItem<THelper> { - inline THelper(const T& t) - : Obj(t) - { - } - - T Obj; - }; - -public: - inline TFastQueue() - : Pool_(TDefaultAllocator::Instance()) - , Size_(0) - { - } - - inline void Push(const T& t) { - Queue_.PushFront(new (&Pool_) THelper(t)); - ++Size_; - } - - inline T Pop() { + +template <class T> +class TFastQueue { + struct THelper: public TObjectFromPool<THelper>, public TIntrusiveListItem<THelper> { + inline THelper(const T& t) + : Obj(t) + { + } + + T Obj; + }; + +public: + inline TFastQueue() + : Pool_(TDefaultAllocator::Instance()) + , Size_(0) + { + } + + inline void Push(const T& t) { + Queue_.PushFront(new (&Pool_) THelper(t)); + ++Size_; + } + + inline T Pop() { Y_ASSERT(!this->Empty()); - - THolder<THelper> tmp(Queue_.PopBack()); - --Size_; - - return tmp->Obj; - } - + + THolder<THelper> tmp(Queue_.PopBack()); + --Size_; + + return tmp->Obj; + } + inline size_t Size() const noexcept { - return Size_; - } - + return Size_; + } + Y_PURE_FUNCTION inline bool Empty() const noexcept { - return !this->Size(); - } - + return !this->Size(); + } + inline explicit operator bool() const noexcept { return !this->Empty(); } -private: - typename THelper::TPool Pool_; - TIntrusiveListWithAutoDelete<THelper, TDelete> Queue_; - size_t Size_; -}; +private: + typename THelper::TPool Pool_; + TIntrusiveListWithAutoDelete<THelper, TDelete> Queue_; + size_t Size_; +}; diff --git a/util/generic/strbuf.h b/util/generic/strbuf.h index 70b9360d58..b37f7c9406 100644 --- a/util/generic/strbuf.h +++ b/util/generic/strbuf.h @@ -393,7 +393,7 @@ public: return NextTokTemplate(delim, tok); } - bool NextTok(TdSelf delim, TdSelf& tok) { + bool NextTok(TdSelf delim, TdSelf& tok) { return NextTokTemplate(delim, tok); } @@ -401,7 +401,7 @@ public: return RNextTokTemplate(delim, tok); } - bool RNextTok(TdSelf delim, TdSelf& tok) { + bool RNextTok(TdSelf delim, TdSelf& tok) { return RNextTokTemplate(delim, tok); } @@ -425,11 +425,11 @@ public: return RNextTokTemplate(delim); } - TdSelf NextTok(TdSelf delim) { + TdSelf NextTok(TdSelf delim) { return NextTokTemplate(delim); } - TdSelf RNextTok(TdSelf delim) { + TdSelf RNextTok(TdSelf delim) { return RNextTokTemplate(delim); } diff --git a/util/generic/utility.cpp b/util/generic/utility.cpp index e1ed101c76..0c40777882 100644 --- a/util/generic/utility.cpp +++ b/util/generic/utility.cpp @@ -1,4 +1,4 @@ -#include "utility.h" +#include "utility.h" #ifdef _MSC_VER #include <Windows.h> diff --git a/util/generic/xrange.cpp b/util/generic/xrange.cpp index 74ee8a40fe..0182779829 100644 --- a/util/generic/xrange.cpp +++ b/util/generic/xrange.cpp @@ -1 +1 @@ -#include "xrange.h" +#include "xrange.h" diff --git a/util/generic/xrange.h b/util/generic/xrange.h index 5fc8c82912..0544bea31a 100644 --- a/util/generic/xrange.h +++ b/util/generic/xrange.h @@ -1,37 +1,37 @@ -#pragma once - +#pragma once + #include "typetraits.h" #include "utility.h" -#include <util/system/yassert.h> +#include <util/system/yassert.h> #include <iterator> - -/** @file - * Some similar for python xrange(): https://docs.python.org/2/library/functions.html#xrange - * Discussion: https://clubs.at.yandex-team.ru/arcadia/6124 - * - * Example usage: - * for (auto i: xrange(MyVector.size())) { // instead for (size_t i = 0; i < MyVector.size(); ++i) - * DoSomething(i, MyVector[i]); - * } + +/** @file + * Some similar for python xrange(): https://docs.python.org/2/library/functions.html#xrange + * Discussion: https://clubs.at.yandex-team.ru/arcadia/6124 + * + * Example usage: + * for (auto i: xrange(MyVector.size())) { // instead for (size_t i = 0; i < MyVector.size(); ++i) + * DoSomething(i, MyVector[i]); + * } * * TVector<int> arithmeticSeq = xrange(10); // instead: TVector<int> arithmeticSeq; for(size_t i = 0; i < 10; ++i) { arithmeticSeq.push_back(i); } * - */ - -namespace NPrivate { - template <typename T> + */ + +namespace NPrivate { + template <typename T> class TSimpleXRange { using TDiff = decltype(T() - T()); - public: + public: constexpr TSimpleXRange(T start, T finish) noexcept : Start(start) , Finish(Max(start, finish)) { - } - + } + class TIterator { - public: + public: using value_type = T; using difference_type = TDiff; using pointer = const T*; @@ -39,26 +39,26 @@ namespace NPrivate { using iterator_category = std::random_access_iterator_tag; constexpr TIterator(T value) noexcept - : Value(value) - { - } - + : Value(value) + { + } + constexpr T operator*() const noexcept { - return Value; - } - + return Value; + } + constexpr bool operator!=(const TIterator& other) const noexcept { - return Value != other.Value; - } - + return Value != other.Value; + } + constexpr bool operator==(const TIterator& other) const noexcept { return Value == other.Value; } TIterator& operator++() noexcept { - ++Value; + ++Value; return *this; - } + } TIterator& operator--() noexcept { --Value; @@ -95,21 +95,21 @@ namespace NPrivate { return Value < b.Value; } - private: - T Value; - }; - + private: + T Value; + }; + using value_type = T; using iterator = TIterator; using const_iterator = TIterator; constexpr TIterator begin() const noexcept { - return TIterator(Start); - } - + return TIterator(Start); + } + constexpr TIterator end() const noexcept { - return TIterator(Finish); - } + return TIterator(Finish); + } constexpr T size() const noexcept { return Finish - Start; @@ -120,11 +120,11 @@ namespace NPrivate { return Container(begin(), end()); } - private: - T Start; - T Finish; - }; - + private: + T Start; + T Finish; + }; + template <typename T> class TSteppedXRange { using TDiff = decltype(T() - T()); @@ -244,7 +244,7 @@ namespace NPrivate { }; } - + /** * generate arithmetic progression that starts at start with certain step and stop at finish (not including) * @@ -255,14 +255,14 @@ constexpr ::NPrivate::TSteppedXRange<T> xrange(T start, T finish, decltype(T() - return {start, finish, step}; } -/// generate sequence [start; finish) -template <typename T> +/// generate sequence [start; finish) +template <typename T> constexpr ::NPrivate::TSimpleXRange<T> xrange(T start, T finish) noexcept { return {start, finish}; -} - -/// generate sequence [0; finish) -template <typename T> +} + +/// generate sequence [0; finish) +template <typename T> constexpr auto xrange(T finish) noexcept -> decltype(xrange(T(), finish)) { - return xrange(T(), finish); -} + return xrange(T(), finish); +} diff --git a/util/generic/xrange_ut.cpp b/util/generic/xrange_ut.cpp index 8106da03e7..41ba8812a9 100644 --- a/util/generic/xrange_ut.cpp +++ b/util/generic/xrange_ut.cpp @@ -5,31 +5,31 @@ #include "vector.h" #include <library/cpp/testing/unittest/registar.h> #include <util/string/builder.h> - + Y_UNIT_TEST_SUITE(XRange) { - void TestXRangeImpl(size_t begin, size_t end) { - size_t count = 0; - size_t sum = 0; - size_t first = 42; - bool firstInited = false; - size_t last = 0; - + void TestXRangeImpl(size_t begin, size_t end) { + size_t count = 0; + size_t sum = 0; + size_t first = 42; + bool firstInited = false; + size_t last = 0; + for (auto i : xrange(begin, end)) { ++count; - sum += i; - last = i; - if (!firstInited) { - first = i; - firstInited = true; - } - } - - UNIT_ASSERT_VALUES_EQUAL(count, end - begin); - UNIT_ASSERT_VALUES_EQUAL(first, begin); - UNIT_ASSERT_VALUES_EQUAL(last, end - 1); - UNIT_ASSERT_VALUES_EQUAL(sum, count * (first + last) / 2); - } - + sum += i; + last = i; + if (!firstInited) { + first = i; + firstInited = true; + } + } + + UNIT_ASSERT_VALUES_EQUAL(count, end - begin); + UNIT_ASSERT_VALUES_EQUAL(first, begin); + UNIT_ASSERT_VALUES_EQUAL(last, end - 1); + UNIT_ASSERT_VALUES_EQUAL(sum, count * (first + last) / 2); + } + void TestSteppedXRangeImpl(int begin, int end, int step, const TVector<int>& expected) { size_t expInd = 0; for (auto i : xrange(begin, end, step)) { @@ -41,9 +41,9 @@ Y_UNIT_TEST_SUITE(XRange) { } Y_UNIT_TEST(IncrementWorks) { - TestXRangeImpl(0, 10); - TestXRangeImpl(10, 20); - } + TestXRangeImpl(0, 10); + TestXRangeImpl(10, 20); + } Y_UNIT_TEST(DecrementWorks) { TestSteppedXRangeImpl(10, 0, -1, {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}); @@ -214,4 +214,4 @@ Y_UNIT_TEST_SUITE(XRange) { UNIT_ASSERT_VALUES_EQUAL(5, *it); } } -} +} diff --git a/util/generic/yexception.h b/util/generic/yexception.h index b0c604e8c4..f094c1c0ee 100644 --- a/util/generic/yexception.h +++ b/util/generic/yexception.h @@ -148,7 +148,7 @@ namespace NPrivate { TStringBuf Message; }; - [[noreturn]] void ThrowYException(const TSimpleExceptionMessage& sm); + [[noreturn]] void ThrowYException(const TSimpleExceptionMessage& sm); [[noreturn]] void ThrowYExceptionWithBacktrace(const TSimpleExceptionMessage& sm); } |