aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/containers
diff options
context:
space:
mode:
authorleo <leo@yandex-team.ru>2022-02-10 16:46:40 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:46:40 +0300
commit980edcd3304699edf9d4e4d6a656e585028e2a72 (patch)
tree139f47f3911484ae9af0eb347b1a88bd6c4bb35f /library/cpp/containers
parentb036a557f285146e5e35d4213e29a094ab907bcf (diff)
downloadydb-980edcd3304699edf9d4e4d6a656e585028e2a72.tar.gz
Restoring authorship annotation for <leo@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/containers')
-rw-r--r--library/cpp/containers/2d_array/2d_array.h150
-rw-r--r--library/cpp/containers/atomizer/atomizer.h154
-rw-r--r--library/cpp/containers/comptrie/comptrie_builder.inl2
-rw-r--r--library/cpp/containers/str_map/str_map.h202
4 files changed, 254 insertions, 254 deletions
diff --git a/library/cpp/containers/2d_array/2d_array.h b/library/cpp/containers/2d_array/2d_array.h
index 9e24650637..090a8aff57 100644
--- a/library/cpp/containers/2d_array/2d_array.h
+++ b/library/cpp/containers/2d_array/2d_array.h
@@ -1,110 +1,110 @@
#pragma once
#include <util/system/yassert.h>
-#include <util/generic/algorithm.h>
+#include <util/generic/algorithm.h>
-#ifdef _DEBUG
-template <class T>
-struct TBoundCheck {
- T* Data;
+#ifdef _DEBUG
+template <class T>
+struct TBoundCheck {
+ T* Data;
size_t Size;
TBoundCheck(T* d, size_t s) {
- Data = d;
- Size = s;
- }
+ Data = d;
+ Size = s;
+ }
T& operator[](size_t i) const {
Y_ASSERT(i >= 0 && i < Size);
- return Data[i];
- }
+ return Data[i];
+ }
};
-#endif
+#endif
template <class T>
-class TArray2D {
-private:
- typedef T* PT;
- T* Data;
- T** PData;
+class TArray2D {
+private:
+ typedef T* PT;
+ T* Data;
+ T** PData;
size_t XSize;
size_t YSize;
-private:
+private:
void Copy(const TArray2D& a) {
- XSize = a.XSize;
- YSize = a.YSize;
- Create();
+ XSize = a.XSize;
+ YSize = a.YSize;
+ Create();
for (size_t i = 0; i < XSize * YSize; i++)
- Data[i] = a.Data[i];
- }
- void Destroy() {
- delete[] Data;
- delete[] PData;
- }
- void Create() {
- Data = new T[XSize * YSize];
- PData = new PT[YSize];
+ Data[i] = a.Data[i];
+ }
+ void Destroy() {
+ delete[] Data;
+ delete[] PData;
+ }
+ void Create() {
+ Data = new T[XSize * YSize];
+ PData = new PT[YSize];
for (size_t i = 0; i < YSize; i++)
- PData[i] = Data + i * XSize;
- }
+ PData[i] = Data + i * XSize;
+ }
public:
TArray2D(size_t xsize = 1, size_t ysize = 1) {
- XSize = xsize;
- YSize = ysize;
- Create();
- }
+ XSize = xsize;
+ YSize = ysize;
+ Create();
+ }
TArray2D(const TArray2D& a) {
- Copy(a);
- }
+ Copy(a);
+ }
TArray2D& operator=(const TArray2D& a) {
- Destroy();
- Copy(a);
- return *this;
- }
- ~TArray2D() {
- Destroy();
- }
+ Destroy();
+ Copy(a);
+ return *this;
+ }
+ ~TArray2D() {
+ Destroy();
+ }
void SetSizes(size_t xsize, size_t ysize) {
- if (XSize == xsize && YSize == ysize)
- return;
- Destroy();
- XSize = xsize;
- YSize = ysize;
- Create();
- }
- void Clear() {
+ if (XSize == xsize && YSize == ysize)
+ return;
+ Destroy();
+ XSize = xsize;
+ YSize = ysize;
+ Create();
+ }
+ void Clear() {
SetSizes(1, 1);
- }
+ }
#ifdef _DEBUG
TBoundCheck<T> operator[](size_t i) const {
Y_ASSERT(i < YSize);
- return TBoundCheck<T>(PData[i], XSize);
- }
+ return TBoundCheck<T>(PData[i], XSize);
+ }
#else
T* operator[](size_t i) const {
Y_ASSERT(i < YSize);
- return PData[i];
- }
+ return PData[i];
+ }
#endif
size_t GetXSize() const {
- return XSize;
- }
+ return XSize;
+ }
size_t GetYSize() const {
- return YSize;
- }
- void FillZero() {
- memset(Data, 0, sizeof(T) * XSize * YSize);
- }
- void FillEvery(const T& a) {
+ return YSize;
+ }
+ void FillZero() {
+ memset(Data, 0, sizeof(T) * XSize * YSize);
+ }
+ void FillEvery(const T& a) {
for (size_t i = 0; i < XSize * YSize; i++)
- Data[i] = a;
- }
- void Swap(TArray2D& a) {
- std::swap(Data, a.Data);
- std::swap(PData, a.PData);
- std::swap(XSize, a.XSize);
- std::swap(YSize, a.YSize);
- }
+ Data[i] = a;
+ }
+ void Swap(TArray2D& a) {
+ std::swap(Data, a.Data);
+ std::swap(PData, a.PData);
+ std::swap(XSize, a.XSize);
+ std::swap(YSize, a.YSize);
+ }
};
template <class T>
@@ -121,5 +121,5 @@ inline bool operator==(const TArray2D<T>& a, const TArray2D<T>& b) {
template <class T>
inline bool operator!=(const TArray2D<T>& a, const TArray2D<T>& b) {
- return !(a == b);
-}
+ return !(a == b);
+}
diff --git a/library/cpp/containers/atomizer/atomizer.h b/library/cpp/containers/atomizer/atomizer.h
index 5e40f47ab9..f18397c24e 100644
--- a/library/cpp/containers/atomizer/atomizer.h
+++ b/library/cpp/containers/atomizer/atomizer.h
@@ -16,7 +16,7 @@ class super_atomizer;
template <class HashFcn, class EqualTo>
class atomizer: public string_hash<ui32, HashFcn, EqualTo> {
-private:
+private:
TVector<const char*> order;
public:
@@ -36,7 +36,7 @@ public:
atomizer() {
order.reserve(HASH_SIZE_DEFAULT);
}
- atomizer(size_type hash_size, pool_size_type pool_size)
+ atomizer(size_type hash_size, pool_size_type pool_size)
: string_hash<ui32, HashFcn, EqualTo>(hash_size, pool_size)
{
order.reserve(hash_size);
@@ -80,63 +80,63 @@ public:
order.clear();
}
void SaveC2N(FILE* f) const { // we write sorted file
- for (ui32 i = 0; i < order.size(); i++)
- if (order[i])
+ for (ui32 i = 0; i < order.size(); i++)
+ if (order[i])
fprintf(f, "%d\t%s\n", i + 1, order[i]);
- }
+ }
void LoadC2N(FILE* f) { // but can read unsorted one
- long k, km = 0;
- char buf[1000];
+ long k, km = 0;
+ char buf[1000];
char* s;
- while (fgets(buf, 1000, f)) {
- k = strtol(buf, &s, 10);
+ while (fgets(buf, 1000, f)) {
+ k = strtol(buf, &s, 10);
char* endl = strchr(s, '\n');
- if (endl)
- *endl = 0;
- if (k > 0 && k != LONG_MAX) {
+ if (endl)
+ *endl = 0;
+ if (k > 0 && k != LONG_MAX) {
km = Max(km, k);
- insert_copy(++s, ui32(k));
- }
- }
- order.resize(km);
+ insert_copy(++s, ui32(k));
+ }
+ }
+ order.resize(km);
memset(&order[0], 0, order.size()); // if some atoms are absent
for (const_iterator I = this->begin(); I != end(); ++I)
- order[(*I).second - 1] = (*I).first;
- }
-};
+ order[(*I).second - 1] = (*I).first;
+ }
+};
-template <class T, class HashFcn, class EqualTo>
+template <class T, class HashFcn, class EqualTo>
class super_atomizer: public string_hash<ui32, HashFcn, EqualTo> {
-private:
+private:
using TOrder = TVector<std::pair<const char*, T>>;
- TOrder order;
+ TOrder order;
-public:
+public:
using iterator = typename string_hash<ui32, HashFcn, EqualTo>::iterator;
using const_iterator = typename string_hash<ui32, HashFcn, EqualTo>::const_iterator;
using value_type = typename string_hash<ui32, HashFcn, EqualTo>::value_type;
using size_type = typename string_hash<ui32, HashFcn, EqualTo>::size_type;
using pool_size_type = typename string_hash<ui32, HashFcn, EqualTo>::pool_size_type;
-
+
using o_iterator = typename TOrder::iterator;
using o_const_iterator = typename TOrder::const_iterator;
using o_value_type = typename TOrder::value_type;
-
- using string_hash<ui32, HashFcn, EqualTo>::pool;
- using string_hash<ui32, HashFcn, EqualTo>::size;
- using string_hash<ui32, HashFcn, EqualTo>::find;
- using string_hash<ui32, HashFcn, EqualTo>::end;
- using string_hash<ui32, HashFcn, EqualTo>::insert_copy;
- using string_hash<ui32, HashFcn, EqualTo>::clear_hash;
-
+
+ using string_hash<ui32, HashFcn, EqualTo>::pool;
+ using string_hash<ui32, HashFcn, EqualTo>::size;
+ using string_hash<ui32, HashFcn, EqualTo>::find;
+ using string_hash<ui32, HashFcn, EqualTo>::end;
+ using string_hash<ui32, HashFcn, EqualTo>::insert_copy;
+ using string_hash<ui32, HashFcn, EqualTo>::clear_hash;
+
super_atomizer() {
- order.reserve(HASH_SIZE_DEFAULT);
- }
- super_atomizer(size_type hash_size, pool_size_type pool_size)
+ order.reserve(HASH_SIZE_DEFAULT);
+ }
+ super_atomizer(size_type hash_size, pool_size_type pool_size)
: string_hash<ui32, HashFcn, EqualTo>(hash_size, pool_size)
- {
- order.reserve(hash_size);
- }
+ {
+ order.reserve(hash_size);
+ }
~super_atomizer() = default;
ui32 string_to_atom(const char* key, const T* atom_data = NULL) {
const char* old_begin = pool.Begin();
@@ -145,56 +145,56 @@ public:
if (ins.second) { // new?
if (pool.Begin() != old_begin) // repoint?
for (typename TOrder::iterator ptr = order.begin(); ptr != order.end(); ++ptr)
- if (old_begin <= (*ptr).first && (*ptr).first < old_end) // from old pool?
+ if (old_begin <= (*ptr).first && (*ptr).first < old_end) // from old pool?
(*ptr).first += pool.Begin() - old_begin;
order.push_back(std::pair<const char*, T>((*ins.first).first, atom_data ? *atom_data : T()));
- }
- return (*ins.first).second;
- }
-
+ }
+ return (*ins.first).second;
+ }
+
ui32 perm_string_to_atom(const char* key, const T* atom_data = NULL) {
value_type val(key, ui32(size() + 1));
std::pair<iterator, bool> ins = this->insert(val);
- if (ins.second)
+ if (ins.second)
order.push_back(std::pair<const char*, T>((*ins.first).first, atom_data ? *atom_data : T()));
- return (*ins.first).second; // == size()+1
- }
+ return (*ins.first).second; // == size()+1
+ }
ui32 find_atom(const char* key) const {
- const_iterator it = find(key);
- if (it == end())
- return 0; // INVALID_ATOM
- else
- return (*it).second;
- }
+ const_iterator it = find(key);
+ if (it == end())
+ return 0; // INVALID_ATOM
+ else
+ return (*it).second;
+ }
const char* get_atom_name(ui32 atom) const {
- if (atom && atom <= size())
+ if (atom && atom <= size())
return order[atom - 1].first;
return nullptr;
- }
- const T* get_atom_data(ui32 atom) const {
- if (atom && atom <= size())
+ }
+ const T* get_atom_data(ui32 atom) const {
+ if (atom && atom <= size())
return &order[atom - 1].second;
- return NULL;
- }
- T* get_atom_data(ui32 atom) {
- if (atom && atom <= size())
+ return NULL;
+ }
+ T* get_atom_data(ui32 atom) {
+ if (atom && atom <= size())
return &order[atom - 1].second;
- return NULL;
- }
- o_iterator o_begin() {
- return order.begin();
- }
- o_iterator o_end() {
- return order.end();
- }
- o_const_iterator o_begin() const {
- return order.begin();
- }
- o_const_iterator o_end() const {
- return order.end();
- }
+ return NULL;
+ }
+ o_iterator o_begin() {
+ return order.begin();
+ }
+ o_iterator o_end() {
+ return order.end();
+ }
+ o_const_iterator o_begin() const {
+ return order.begin();
+ }
+ o_const_iterator o_end() const {
+ return order.end();
+ }
void clear_atomizer() {
- clear_hash();
- order.clear();
- }
+ clear_hash();
+ order.clear();
+ }
};
diff --git a/library/cpp/containers/comptrie/comptrie_builder.inl b/library/cpp/containers/comptrie/comptrie_builder.inl
index f273fa6571..a7da790348 100644
--- a/library/cpp/containers/comptrie/comptrie_builder.inl
+++ b/library/cpp/containers/comptrie/comptrie_builder.inl
@@ -508,7 +508,7 @@ void TCompactTrieBuilder<T, D, S>::TCompactTrieBuilderImpl::ConvertSymbolArrayTo
for (size_t i = 0; i < keylen; ++i) {
TSymbol label = key[i];
- for (int j = (int)NCompactTrie::ExtraBits<TSymbol>(); j >= 0; j -= 8) {
+ for (int j = (int)NCompactTrie::ExtraBits<TSymbol>(); j >= 0; j -= 8) {
Y_ASSERT(ckeyptr < buf.Data() + buflen);
*(ckeyptr++) = (char)(label >> j);
}
diff --git a/library/cpp/containers/str_map/str_map.h b/library/cpp/containers/str_map/str_map.h
index 31b00d1b99..997da8d4a0 100644
--- a/library/cpp/containers/str_map/str_map.h
+++ b/library/cpp/containers/str_map/str_map.h
@@ -31,9 +31,9 @@ pool_insert(Map* m, const char* key, const typename Map::mapped_type& data, TBuf
return ins;
}
-#define HASH_SIZE_DEFAULT 100
+#define HASH_SIZE_DEFAULT 100
#define AVERAGEWORD_BUF 10
-
+
template <class T, class HashFcn, class EqualTo, class Alloc>
class string_hash: public THashMap<const char*, T, HashFcn, EqualTo, Alloc> {
protected:
@@ -49,7 +49,7 @@ public:
string_hash() {
pool.Reserve(HASH_SIZE_DEFAULT * AVERAGEWORD_BUF); // reserve here
}
- string_hash(size_type hash_size, pool_size_type pool_size)
+ string_hash(size_type hash_size, pool_size_type pool_size)
: THashMap<const char*, T, HashFcn, EqualTo, Alloc>(hash_size)
{
pool.Reserve(pool_size); // reserve here
@@ -66,46 +66,46 @@ public:
pool_size_type pool_size() const {
return pool.Size();
}
-
- string_hash(const string_hash& sh)
+
+ string_hash(const string_hash& sh)
: THashMap<const char*, T, HashFcn, EqualTo, Alloc>()
- {
- for (const_iterator i = sh.begin(); i != sh.end(); ++i)
- insert_copy((*i).first, (*i).second);
- }
- /* May be faster?
- string_hash(const string_hash& sh)
+ {
+ for (const_iterator i = sh.begin(); i != sh.end(); ++i)
+ insert_copy((*i).first, (*i).second);
+ }
+ /* May be faster?
+ string_hash(const string_hash& sh)
: THashMap<const char *, T, HashFcn, EqualTo>(sh)
- {
- pool = sh.pool;
- size_t delta = pool.begin() - sh.pool.begin();
- for (iterator i = begin(); i != end(); ++i)
- (const char*&)(*i).first += delta;
- }
- */
+ {
+ pool = sh.pool;
+ size_t delta = pool.begin() - sh.pool.begin();
+ for (iterator i = begin(); i != end(); ++i)
+ (const char*&)(*i).first += delta;
+ }
+ */
string_hash& operator=(const string_hash& sh) {
- if (&sh != this) {
- clear_hash();
- for (const_iterator i = sh.begin(); i != sh.end(); ++i)
- insert_copy((*i).first, (*i).second);
- }
- return *this;
- }
-
+ if (&sh != this) {
+ clear_hash();
+ for (const_iterator i = sh.begin(); i != sh.end(); ++i)
+ insert_copy((*i).first, (*i).second);
+ }
+ return *this;
+ }
+
mapped_type& operator[](const char* key) {
iterator I = yh::find(key);
if (I == yh::end())
- I = insert_copy(key, mapped_type()).first;
- return (*I).second;
- }
+ I = insert_copy(key, mapped_type()).first;
+ return (*I).second;
+ }
};
-template <class C, class T, class HashFcn, class EqualTo>
+template <class C, class T, class HashFcn, class EqualTo>
class THashWithSegmentedPoolForKeys: protected THashMap<const C*, T, HashFcn, EqualTo>, TNonCopyable {
-protected:
- segmented_pool<C> pool;
+protected:
+ segmented_pool<C> pool;
-public:
+public:
using yh = THashMap<const C*, T, HashFcn, EqualTo>;
using iterator = typename yh::iterator;
using const_iterator = typename yh::const_iterator;
@@ -114,68 +114,68 @@ public:
using key_type = typename yh::key_type;
using value_type = typename yh::value_type;
- THashWithSegmentedPoolForKeys(size_type hash_size = HASH_SIZE_DEFAULT, size_t segsize = HASH_SIZE_DEFAULT * AVERAGEWORD_BUF, bool afs = false)
- : yh(hash_size)
- , pool(segsize)
- {
- if (afs)
- pool.alloc_first_seg();
- }
-
+ THashWithSegmentedPoolForKeys(size_type hash_size = HASH_SIZE_DEFAULT, size_t segsize = HASH_SIZE_DEFAULT * AVERAGEWORD_BUF, bool afs = false)
+ : yh(hash_size)
+ , pool(segsize)
+ {
+ if (afs)
+ pool.alloc_first_seg();
+ }
+
std::pair<iterator, bool> insert_copy(const C* key, size_t keylen, const mapped_type& data) {
std::pair<iterator, bool> ins = this->insert(value_type(key, data));
- if (ins.second) // new?
- (const C*&)(*ins.first).first = pool.append(key, keylen);
- return ins;
- }
-
- void clear_hash() {
- yh::clear();
- pool.restart();
- }
-
- size_t pool_size() const {
- return pool.size();
- }
-
- size_t size() const {
- return yh::size();
- }
-
- bool empty() const {
- return yh::empty();
- }
-
- iterator begin() {
- return yh::begin();
- }
-
- iterator end() {
- return yh::end();
- }
-
- const_iterator begin() const {
- return yh::begin();
- }
-
- const_iterator end() const {
- return yh::end();
- }
-
- iterator find(const key_type& key) {
- return yh::find(key);
- }
-
- const_iterator find(const key_type& key) const {
- return yh::find(key);
- }
-
+ if (ins.second) // new?
+ (const C*&)(*ins.first).first = pool.append(key, keylen);
+ return ins;
+ }
+
+ void clear_hash() {
+ yh::clear();
+ pool.restart();
+ }
+
+ size_t pool_size() const {
+ return pool.size();
+ }
+
+ size_t size() const {
+ return yh::size();
+ }
+
+ bool empty() const {
+ return yh::empty();
+ }
+
+ iterator begin() {
+ return yh::begin();
+ }
+
+ iterator end() {
+ return yh::end();
+ }
+
+ const_iterator begin() const {
+ return yh::begin();
+ }
+
+ const_iterator end() const {
+ return yh::end();
+ }
+
+ iterator find(const key_type& key) {
+ return yh::find(key);
+ }
+
+ const_iterator find(const key_type& key) const {
+ return yh::find(key);
+ }
+
const yh& get_THashMap() const {
return static_cast<const yh&>(*this);
}
-};
-
-template <class T, class HashFcn, class EqualTo>
+};
+
+template <class T, class HashFcn, class EqualTo>
class segmented_string_hash: public THashWithSegmentedPoolForKeys<char, T, HashFcn, EqualTo> {
public:
using Base = THashWithSegmentedPoolForKeys<char, T, HashFcn, EqualTo>;
@@ -186,20 +186,20 @@ public:
using key_type = typename Base::key_type;
using value_type = typename Base::value_type;
-public:
- segmented_string_hash(size_type hash_size = HASH_SIZE_DEFAULT, size_t segsize = HASH_SIZE_DEFAULT * AVERAGEWORD_BUF, bool afs = false)
+public:
+ segmented_string_hash(size_type hash_size = HASH_SIZE_DEFAULT, size_t segsize = HASH_SIZE_DEFAULT * AVERAGEWORD_BUF, bool afs = false)
: Base(hash_size, segsize, afs)
{
}
-
+
std::pair<iterator, bool> insert_copy(const char* key, const mapped_type& data) {
- return Base::insert_copy(key, strlen(key) + 1, data);
- }
-
- mapped_type& operator[](const char* key) {
- iterator I = Base::find(key);
- if (I == Base::end())
- I = insert_copy(key, mapped_type()).first;
- return (*I).second;
+ return Base::insert_copy(key, strlen(key) + 1, data);
+ }
+
+ mapped_type& operator[](const char* key) {
+ iterator I = Base::find(key);
+ if (I == Base::end())
+ I = insert_copy(key, mapped_type()).first;
+ return (*I).second;
}
};