aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/xrange.h
diff options
context:
space:
mode:
authoranelyubin <anelyubin@yandex-team.ru>2022-02-10 16:49:40 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:49:40 +0300
commit9dac44a55ede6f682527e82880847f99bd7b2d93 (patch)
tree5d5cb817648f650d76cf1076100726fd9b8448e8 /util/generic/xrange.h
parent8d333d5e773f6b0ef31e7b3c92339af7e71413f6 (diff)
downloadydb-9dac44a55ede6f682527e82880847f99bd7b2d93.tar.gz
Restoring authorship annotation for <anelyubin@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'util/generic/xrange.h')
-rw-r--r--util/generic/xrange.h110
1 files changed, 55 insertions, 55 deletions
diff --git a/util/generic/xrange.h b/util/generic/xrange.h
index 0544bea31a7..5fc8c829121 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);
+}