aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/containers/stack_vector
diff options
context:
space:
mode:
authorkartynnik <kartynnik@yandex-team.ru>2022-02-10 16:48:07 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:48:07 +0300
commitca2a705e6e39e85df30054d7e806e572de9cfe23 (patch)
treefb7cc52bc2579366b2796a24f91f6df7a223f9ab /library/cpp/containers/stack_vector
parente20e2b362f0232ed5a389db887e6e27e7763af18 (diff)
downloadydb-ca2a705e6e39e85df30054d7e806e572de9cfe23.tar.gz
Restoring authorship annotation for <kartynnik@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/containers/stack_vector')
-rw-r--r--library/cpp/containers/stack_vector/stack_vec.cpp2
-rw-r--r--library/cpp/containers/stack_vector/stack_vec.h136
-rw-r--r--library/cpp/containers/stack_vector/stack_vec_ut.cpp96
-rw-r--r--library/cpp/containers/stack_vector/ut/ya.make18
-rw-r--r--library/cpp/containers/stack_vector/ya.make16
5 files changed, 134 insertions, 134 deletions
diff --git a/library/cpp/containers/stack_vector/stack_vec.cpp b/library/cpp/containers/stack_vector/stack_vec.cpp
index 21c0ab3f11..aa84285b9a 100644
--- a/library/cpp/containers/stack_vector/stack_vec.cpp
+++ b/library/cpp/containers/stack_vector/stack_vec.cpp
@@ -1 +1 @@
-#include "stack_vec.h"
+#include "stack_vec.h"
diff --git a/library/cpp/containers/stack_vector/stack_vec.h b/library/cpp/containers/stack_vector/stack_vec.h
index fcc5d9a2a5..1cefef2c73 100644
--- a/library/cpp/containers/stack_vector/stack_vec.h
+++ b/library/cpp/containers/stack_vector/stack_vec.h
@@ -1,22 +1,22 @@
-#pragma once
-
-#include <util/generic/vector.h>
+#pragma once
+
+#include <util/generic/vector.h>
#include <util/ysaveload.h>
-
+
#include <type_traits>
-// A vector preallocated on the stack.
-// After exceeding the preconfigured stack space falls back to the heap.
+// A vector preallocated on the stack.
+// After exceeding the preconfigured stack space falls back to the heap.
// Publicly inherits TVector, but disallows swap (and hence shrink_to_fit, also operator= is reimplemented via copying).
-//
-// Inspired by: http://qt-project.org/doc/qt-4.8/qvarlengtharray.html#details
-
+//
+// Inspired by: http://qt-project.org/doc/qt-4.8/qvarlengtharray.html#details
+
template <typename T, size_t CountOnStack = 256, bool UseFallbackAlloc = true, class Alloc = std::allocator<T>>
-class TStackVec;
-
+class TStackVec;
+
template <typename T, class Alloc = std::allocator<T>>
using TSmallVec = TStackVec<T, 16, true, Alloc>;
-
+
template <typename T, size_t CountOnStack = 256>
using TStackOnlyVec = TStackVec<T, CountOnStack, false>;
@@ -24,27 +24,27 @@ namespace NPrivate {
template <class Alloc, class StackAlloc, typename T, typename U>
struct TRebind {
typedef TReboundAllocator<Alloc, U> other;
- };
-
+ };
+
template <class Alloc, class StackAlloc, typename T>
struct TRebind<Alloc, StackAlloc, T, T> {
- typedef StackAlloc other;
- };
-
+ typedef StackAlloc other;
+ };
+
template <typename T, size_t CountOnStack, bool UseFallbackAlloc, class Alloc = std::allocator<T>>
class TStackBasedAllocator: public Alloc {
- public:
+ public:
typedef TStackBasedAllocator<T, CountOnStack, UseFallbackAlloc, Alloc> TSelf;
-
+
using typename Alloc::difference_type;
- using typename Alloc::size_type;
+ using typename Alloc::size_type;
using typename Alloc::value_type;
-
+
template <class U>
struct rebind: public ::NPrivate::TRebind<Alloc, TSelf, T, U> {
- };
-
- public:
+ };
+
+ public:
TStackBasedAllocator() = default;
template <
@@ -61,26 +61,26 @@ namespace NPrivate {
if (!IsStorageUsed && CountOnStack >= n) {
IsStorageUsed = true;
return reinterpret_cast<T*>(&StackBasedStorage[0]);
- } else {
+ } else {
if constexpr (!UseFallbackAlloc) {
Y_FAIL(
"Stack storage overflow. Capacity: %d, requested: %d", (int)CountOnStack, int(n));
}
return FallbackAllocator().allocate(n);
- }
- }
-
+ }
+ }
+
void deallocate(T* p, size_type n) {
if (p >= reinterpret_cast<T*>(&StackBasedStorage[0]) &&
p < reinterpret_cast<T*>(&StackBasedStorage[CountOnStack])) {
Y_VERIFY(IsStorageUsed);
IsStorageUsed = false;
- } else {
+ } else {
FallbackAllocator().deallocate(p, n);
- }
- }
-
- private:
+ }
+ }
+
+ private:
std::aligned_storage_t<sizeof(T), alignof(T)> StackBasedStorage[CountOnStack];
bool IsStorageUsed = false;
@@ -88,53 +88,53 @@ namespace NPrivate {
Alloc& FallbackAllocator() noexcept {
return static_cast<Alloc&>(*this);
}
- };
+ };
}
-
+
template <typename T, size_t CountOnStack, bool UseFallbackAlloc, class Alloc>
class TStackVec: public TVector<T, ::NPrivate::TStackBasedAllocator<T, CountOnStack, UseFallbackAlloc, TReboundAllocator<Alloc, T>>> {
private:
using TBase = TVector<T, ::NPrivate::TStackBasedAllocator<T, CountOnStack, UseFallbackAlloc, TReboundAllocator<Alloc, T>>>;
using TAllocator = typename TBase::allocator_type;
-public:
+public:
using typename TBase::const_iterator;
using typename TBase::const_reverse_iterator;
- using typename TBase::iterator;
- using typename TBase::reverse_iterator;
+ using typename TBase::iterator;
+ using typename TBase::reverse_iterator;
using typename TBase::size_type;
- using typename TBase::value_type;
-
-public:
+ using typename TBase::value_type;
+
+public:
TStackVec(const TAllocator& alloc = TAllocator())
: TBase(alloc)
- {
- TBase::reserve(CountOnStack);
- }
-
+ {
+ TBase::reserve(CountOnStack);
+ }
+
explicit TStackVec(size_type count, const TAllocator& alloc = TAllocator())
: TBase(alloc)
- {
- if (count <= CountOnStack) {
- TBase::reserve(CountOnStack);
- }
- TBase::resize(count);
- }
-
+ {
+ if (count <= CountOnStack) {
+ TBase::reserve(CountOnStack);
+ }
+ TBase::resize(count);
+ }
+
TStackVec(size_type count, const T& val, const TAllocator& alloc = TAllocator())
: TBase(alloc)
- {
- if (count <= CountOnStack) {
- TBase::reserve(CountOnStack);
- }
- TBase::assign(count, val);
- }
-
+ {
+ if (count <= CountOnStack) {
+ TBase::reserve(CountOnStack);
+ }
+ TBase::assign(count, val);
+ }
+
TStackVec(const TStackVec& src)
: TStackVec(src.begin(), src.end())
- {
- }
-
+ {
+ }
+
template <class A>
TStackVec(const TVector<T, A>& src)
: TStackVec(src.begin(), src.end())
@@ -166,14 +166,14 @@ public:
}
}
-public:
+public:
void swap(TStackVec&) = delete;
- void shrink_to_fit() = delete;
-
+ void shrink_to_fit() = delete;
+
TStackVec& operator=(const TStackVec& src) {
TBase::assign(src.begin(), src.end());
- return *this;
- }
+ return *this;
+ }
template <class A>
TStackVec& operator=(const TVector<T, A>& src) {
@@ -185,7 +185,7 @@ public:
TBase::assign(il.begin(), il.end());
return *this;
}
-};
+};
template <typename T, size_t CountOnStack, class Alloc>
class TSerializer<TStackVec<T, CountOnStack, true, Alloc>>: public TVectorSerializer<TStackVec<T, CountOnStack, true, Alloc>> {
diff --git a/library/cpp/containers/stack_vector/stack_vec_ut.cpp b/library/cpp/containers/stack_vector/stack_vec_ut.cpp
index 19f9677781..0325b614c0 100644
--- a/library/cpp/containers/stack_vector/stack_vec_ut.cpp
+++ b/library/cpp/containers/stack_vector/stack_vec_ut.cpp
@@ -1,7 +1,7 @@
-#include "stack_vec.h"
-
+#include "stack_vec.h"
+
#include <library/cpp/testing/unittest/registar.h>
-
+
namespace {
struct TNotCopyAssignable {
const int Value;
@@ -45,19 +45,19 @@ namespace {
Y_UNIT_TEST_SUITE(TStackBasedVectorTest) {
Y_UNIT_TEST(TestCreateEmpty) {
- TStackVec<int> ints;
- UNIT_ASSERT_EQUAL(ints.size(), 0);
- }
-
+ TStackVec<int> ints;
+ UNIT_ASSERT_EQUAL(ints.size(), 0);
+ }
+
Y_UNIT_TEST(TestCreateNonEmpty) {
- TStackVec<int> ints(5);
- UNIT_ASSERT_EQUAL(ints.size(), 5);
-
- for (size_t i = 0; i < ints.size(); ++i) {
- UNIT_ASSERT_EQUAL(ints[i], 0);
- }
- }
-
+ TStackVec<int> ints(5);
+ UNIT_ASSERT_EQUAL(ints.size(), 5);
+
+ for (size_t i = 0; i < ints.size(); ++i) {
+ UNIT_ASSERT_EQUAL(ints[i], 0);
+ }
+ }
+
Y_UNIT_TEST(TestReallyOnStack) {
const TStackVec<int> vec(5);
@@ -65,39 +65,39 @@ Y_UNIT_TEST_SUITE(TStackBasedVectorTest) {
(const char*)&vec <= (const char*)&vec[0] &&
(const char*)&vec[0] <= (const char*)&vec + sizeof(vec)
);
- }
-
+ }
+
Y_UNIT_TEST(TestFallback) {
- TSmallVec<int> ints;
- for (int i = 0; i < 14; ++i) {
- ints.push_back(i);
- }
-
- for (size_t i = 0; i < ints.size(); ++i) {
- UNIT_ASSERT_EQUAL(ints[i], (int)i);
- }
-
- for (int i = 14; i < 20; ++i) {
- ints.push_back(i);
- }
-
- for (size_t i = 0; i < ints.size(); ++i) {
- UNIT_ASSERT_EQUAL(ints[i], (int)i);
- }
-
- TSmallVec<int> ints2 = ints;
-
- for (size_t i = 0; i < ints2.size(); ++i) {
- UNIT_ASSERT_EQUAL(ints2[i], (int)i);
- }
-
- TSmallVec<int> ints3;
- ints3 = ints2;
-
- for (size_t i = 0; i < ints3.size(); ++i) {
- UNIT_ASSERT_EQUAL(ints3[i], (int)i);
- }
- }
+ TSmallVec<int> ints;
+ for (int i = 0; i < 14; ++i) {
+ ints.push_back(i);
+ }
+
+ for (size_t i = 0; i < ints.size(); ++i) {
+ UNIT_ASSERT_EQUAL(ints[i], (int)i);
+ }
+
+ for (int i = 14; i < 20; ++i) {
+ ints.push_back(i);
+ }
+
+ for (size_t i = 0; i < ints.size(); ++i) {
+ UNIT_ASSERT_EQUAL(ints[i], (int)i);
+ }
+
+ TSmallVec<int> ints2 = ints;
+
+ for (size_t i = 0; i < ints2.size(); ++i) {
+ UNIT_ASSERT_EQUAL(ints2[i], (int)i);
+ }
+
+ TSmallVec<int> ints3;
+ ints3 = ints2;
+
+ for (size_t i = 0; i < ints3.size(); ++i) {
+ UNIT_ASSERT_EQUAL(ints3[i], (int)i);
+ }
+ }
Y_UNIT_TEST(TestCappedSize) {
TStackVec<int, 8, false> ints;
@@ -141,4 +141,4 @@ Y_UNIT_TEST_SUITE(TStackBasedVectorTest) {
}
UNIT_ASSERT_VALUES_EQUAL(count, 3);
}
-}
+}
diff --git a/library/cpp/containers/stack_vector/ut/ya.make b/library/cpp/containers/stack_vector/ut/ya.make
index 1d70496954..da5a9d0b11 100644
--- a/library/cpp/containers/stack_vector/ut/ya.make
+++ b/library/cpp/containers/stack_vector/ut/ya.make
@@ -1,11 +1,11 @@
-UNITTEST()
-
+UNITTEST()
+
OWNER(ilnurkh)
-
+
SRCDIR(library/cpp/containers/stack_vector)
-
-SRCS(
- stack_vec_ut.cpp
-)
-
-END()
+
+SRCS(
+ stack_vec_ut.cpp
+)
+
+END()
diff --git a/library/cpp/containers/stack_vector/ya.make b/library/cpp/containers/stack_vector/ya.make
index cfb63295ec..3c31a00b70 100644
--- a/library/cpp/containers/stack_vector/ya.make
+++ b/library/cpp/containers/stack_vector/ya.make
@@ -1,11 +1,11 @@
-LIBRARY()
-
+LIBRARY()
+
OWNER(ilnurkh)
-
-SRCS(
- stack_vec.cpp
-)
-
-END()
+
+SRCS(
+ stack_vec.cpp
+)
+
+END()
RECURSE_FOR_TESTS(ut)