aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/containers/compact_vector
diff options
context:
space:
mode:
authornga <nga@yandex-team.ru>2022-02-10 16:48:09 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:48:09 +0300
commit1f553f46fb4f3c5eec631352cdd900a0709016af (patch)
treea231fba2c03b440becaea6c86a2702d0bfb0336e /library/cpp/containers/compact_vector
parentc4de7efdedc25b49cbea74bd589eecb61b55b60a (diff)
downloadydb-1f553f46fb4f3c5eec631352cdd900a0709016af.tar.gz
Restoring authorship annotation for <nga@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/containers/compact_vector')
-rw-r--r--library/cpp/containers/compact_vector/compact_vector.h366
-rw-r--r--library/cpp/containers/compact_vector/compact_vector_ut.cpp62
2 files changed, 214 insertions, 214 deletions
diff --git a/library/cpp/containers/compact_vector/compact_vector.h b/library/cpp/containers/compact_vector/compact_vector.h
index dbe7473f0c..0e9667c906 100644
--- a/library/cpp/containers/compact_vector/compact_vector.h
+++ b/library/cpp/containers/compact_vector/compact_vector.h
@@ -1,209 +1,209 @@
#pragma once
-
-#include <util/generic/yexception.h>
-#include <util/generic/utility.h>
+
+#include <util/generic/yexception.h>
+#include <util/generic/utility.h>
#include <util/memory/alloc.h>
#include <util/stream/output.h>
-#include <util/system/yassert.h>
-
+#include <util/system/yassert.h>
+
#include <cstdlib>
// vector that is 8 bytes when empty (TVector is 24 bytes)
-
-template <typename T>
-class TCompactVector {
-private:
- typedef TCompactVector<T> TThis;
-
- // XXX: make header independent on T and introduce nullptr
- struct THeader {
- size_t Size;
- size_t Capacity;
- };
-
- T* Ptr;
-
- THeader* Header() {
+
+template <typename T>
+class TCompactVector {
+private:
+ typedef TCompactVector<T> TThis;
+
+ // XXX: make header independent on T and introduce nullptr
+ struct THeader {
+ size_t Size;
+ size_t Capacity;
+ };
+
+ T* Ptr;
+
+ THeader* Header() {
return ((THeader*)Ptr) - 1;
- }
-
- const THeader* Header() const {
+ }
+
+ const THeader* Header() const {
return ((THeader*)Ptr) - 1;
- }
-
-public:
- typedef T* TIterator;
- typedef const T* TConstIterator;
-
- typedef TIterator iterator;
- typedef TConstIterator const_iterator;
-
- TCompactVector()
+ }
+
+public:
+ typedef T* TIterator;
+ typedef const T* TConstIterator;
+
+ typedef TIterator iterator;
+ typedef TConstIterator const_iterator;
+
+ TCompactVector()
: Ptr(nullptr)
{
}
-
- TCompactVector(const TThis& that)
+
+ TCompactVector(const TThis& that)
: Ptr(nullptr)
- {
- Reserve(that.Size());
- for (TConstIterator i = that.Begin(); i != that.End(); ++i) {
- PushBack(*i);
- }
- }
-
- ~TCompactVector() {
- for (size_t i = 0; i < Size(); ++i) {
- try {
- (*this)[i].~T();
- } catch (...) {
- }
- }
+ {
+ Reserve(that.Size());
+ for (TConstIterator i = that.Begin(); i != that.End(); ++i) {
+ PushBack(*i);
+ }
+ }
+
+ ~TCompactVector() {
+ for (size_t i = 0; i < Size(); ++i) {
+ try {
+ (*this)[i].~T();
+ } catch (...) {
+ }
+ }
if (Ptr)
- free(Header());
- }
-
- TIterator Begin() {
- return Ptr;
- }
-
- TIterator End() {
- return Ptr + Size();
- }
-
- TConstIterator Begin() const {
- return Ptr;
- }
-
- TConstIterator End() const {
- return Ptr + Size();
- }
-
- iterator begin() {
- return Begin();
- }
-
- const_iterator begin() const {
- return Begin();
- }
-
- iterator end() {
- return End();
- }
-
- const_iterator end() const {
- return End();
- }
-
- void Swap(TThis& that) {
- DoSwap(Ptr, that.Ptr);
- }
-
- void Reserve(size_t newCapacity) {
- if (newCapacity <= Capacity()) {
+ free(Header());
+ }
+
+ TIterator Begin() {
+ return Ptr;
+ }
+
+ TIterator End() {
+ return Ptr + Size();
+ }
+
+ TConstIterator Begin() const {
+ return Ptr;
+ }
+
+ TConstIterator End() const {
+ return Ptr + Size();
+ }
+
+ iterator begin() {
+ return Begin();
+ }
+
+ const_iterator begin() const {
+ return Begin();
+ }
+
+ iterator end() {
+ return End();
+ }
+
+ const_iterator end() const {
+ return End();
+ }
+
+ void Swap(TThis& that) {
+ DoSwap(Ptr, that.Ptr);
+ }
+
+ void Reserve(size_t newCapacity) {
+ if (newCapacity <= Capacity()) {
} else if (Ptr == nullptr) {
void* mem = ::malloc(sizeof(THeader) + newCapacity * sizeof(T));
if (mem == nullptr)
- ythrow yexception() << "out of memory";
+ ythrow yexception() << "out of memory";
Ptr = (T*)(((THeader*)mem) + 1);
- Header()->Size = 0;
- Header()->Capacity = newCapacity;
- } else {
- TThis copy;
- size_t realNewCapacity = Max(Capacity() * 2, newCapacity);
- copy.Reserve(realNewCapacity);
- for (TConstIterator it = Begin(); it != End(); ++it) {
- copy.PushBack(*it);
- }
- Swap(copy);
- }
- }
-
- size_t Size() const {
+ Header()->Size = 0;
+ Header()->Capacity = newCapacity;
+ } else {
+ TThis copy;
+ size_t realNewCapacity = Max(Capacity() * 2, newCapacity);
+ copy.Reserve(realNewCapacity);
+ for (TConstIterator it = Begin(); it != End(); ++it) {
+ copy.PushBack(*it);
+ }
+ Swap(copy);
+ }
+ }
+
+ size_t Size() const {
return Ptr ? Header()->Size : 0;
- }
-
- size_t size() const {
- return Size();
- }
-
- bool Empty() const {
- return Size() == 0;
- }
-
- bool empty() const {
- return Empty();
- }
-
- size_t Capacity() const {
+ }
+
+ size_t size() const {
+ return Size();
+ }
+
+ bool Empty() const {
+ return Size() == 0;
+ }
+
+ bool empty() const {
+ return Empty();
+ }
+
+ size_t Capacity() const {
return Ptr ? Header()->Capacity : 0;
- }
-
- void PushBack(const T& elem) {
- Reserve(Size() + 1);
- new (Ptr + Size()) T(elem);
- ++(Header()->Size);
- }
-
- T& Back() {
- return *(End() - 1);
- }
-
- const T& Back() const {
- return *(End() - 1);
- }
-
- T& back() {
- return Back();
- }
-
- const T& back() const {
- return Back();
- }
-
- TIterator Insert(TIterator pos, const T& elem) {
+ }
+
+ void PushBack(const T& elem) {
+ Reserve(Size() + 1);
+ new (Ptr + Size()) T(elem);
+ ++(Header()->Size);
+ }
+
+ T& Back() {
+ return *(End() - 1);
+ }
+
+ const T& Back() const {
+ return *(End() - 1);
+ }
+
+ T& back() {
+ return Back();
+ }
+
+ const T& back() const {
+ return Back();
+ }
+
+ TIterator Insert(TIterator pos, const T& elem) {
Y_ASSERT(pos >= Begin());
Y_ASSERT(pos <= End());
-
- size_t posn = pos - Begin();
- if (pos == End()) {
- PushBack(elem);
- } else {
+
+ size_t posn = pos - Begin();
+ if (pos == End()) {
+ PushBack(elem);
+ } else {
Y_ASSERT(Size() > 0);
-
- Reserve(Size() + 1);
-
- PushBack(*(End() - 1));
-
- for (size_t i = Size() - 2; i + 1 > posn; --i) {
- (*this)[i + 1] = (*this)[i];
- }
-
- (*this)[posn] = elem;
- }
- return Begin() + posn;
- }
-
- iterator insert(iterator pos, const T& elem) {
- return Insert(pos, elem);
- }
-
- void Clear() {
- TThis clean;
- Swap(clean);
- }
-
- void clear() {
- Clear();
- }
-
+
+ Reserve(Size() + 1);
+
+ PushBack(*(End() - 1));
+
+ for (size_t i = Size() - 2; i + 1 > posn; --i) {
+ (*this)[i + 1] = (*this)[i];
+ }
+
+ (*this)[posn] = elem;
+ }
+ return Begin() + posn;
+ }
+
+ iterator insert(iterator pos, const T& elem) {
+ return Insert(pos, elem);
+ }
+
+ void Clear() {
+ TThis clean;
+ Swap(clean);
+ }
+
+ void clear() {
+ Clear();
+ }
+
T& operator[](size_t index) {
Y_ASSERT(index < Size());
- return Ptr[index];
- }
-
+ return Ptr[index];
+ }
+
const T& operator[](size_t index) const {
Y_ASSERT(index < Size());
- return Ptr[index];
- }
-};
+ return Ptr[index];
+ }
+};
diff --git a/library/cpp/containers/compact_vector/compact_vector_ut.cpp b/library/cpp/containers/compact_vector/compact_vector_ut.cpp
index 7d413d6575..cb5e603fd9 100644
--- a/library/cpp/containers/compact_vector/compact_vector_ut.cpp
+++ b/library/cpp/containers/compact_vector/compact_vector_ut.cpp
@@ -1,39 +1,39 @@
#include <library/cpp/testing/unittest/registar.h>
-
-#include "compact_vector.h"
-
+
+#include "compact_vector.h"
+
Y_UNIT_TEST_SUITE(TCompactVectorTest) {
Y_UNIT_TEST(TestSimple1) {
- }
-
+ }
+
Y_UNIT_TEST(TestSimple) {
- TCompactVector<ui32> vector;
- for (ui32 i = 0; i < 10000; ++i) {
- vector.PushBack(i + 20);
- UNIT_ASSERT_VALUES_EQUAL(i + 1, vector.Size());
- }
- for (ui32 i = 0; i < 10000; ++i) {
- UNIT_ASSERT_VALUES_EQUAL(i + 20, vector[i]);
- }
- }
-
+ TCompactVector<ui32> vector;
+ for (ui32 i = 0; i < 10000; ++i) {
+ vector.PushBack(i + 20);
+ UNIT_ASSERT_VALUES_EQUAL(i + 1, vector.Size());
+ }
+ for (ui32 i = 0; i < 10000; ++i) {
+ UNIT_ASSERT_VALUES_EQUAL(i + 20, vector[i]);
+ }
+ }
+
Y_UNIT_TEST(TestInsert) {
- TCompactVector<ui32> vector;
-
- for (ui32 i = 0; i < 10; ++i) {
- vector.PushBack(i + 2);
- }
-
- vector.Insert(vector.Begin(), 99);
-
+ TCompactVector<ui32> vector;
+
+ for (ui32 i = 0; i < 10; ++i) {
+ vector.PushBack(i + 2);
+ }
+
+ vector.Insert(vector.Begin(), 99);
+
UNIT_ASSERT_VALUES_EQUAL(11u, vector.Size());
UNIT_ASSERT_VALUES_EQUAL(99u, vector[0]);
- for (ui32 i = 0; i < 10; ++i) {
- UNIT_ASSERT_VALUES_EQUAL(i + 2, vector[i + 1]);
- }
-
- vector.Insert(vector.Begin() + 3, 77);
-
+ for (ui32 i = 0; i < 10; ++i) {
+ UNIT_ASSERT_VALUES_EQUAL(i + 2, vector[i + 1]);
+ }
+
+ vector.Insert(vector.Begin() + 3, 77);
+
UNIT_ASSERT_VALUES_EQUAL(12u, vector.Size());
UNIT_ASSERT_VALUES_EQUAL(99u, vector[0]);
UNIT_ASSERT_VALUES_EQUAL(2u, vector[1]);
@@ -42,5 +42,5 @@ Y_UNIT_TEST_SUITE(TCompactVectorTest) {
UNIT_ASSERT_VALUES_EQUAL(4u, vector[4]);
UNIT_ASSERT_VALUES_EQUAL(5u, vector[5]);
UNIT_ASSERT_VALUES_EQUAL(11u, vector[11]);
- }
-}
+ }
+}