change PIMap::at and add const to PIMap and PIBinaryLog

This commit is contained in:
2023-07-13 12:32:45 +03:00
parent 299a009d61
commit 66fb93ba88
3 changed files with 58 additions and 41 deletions

View File

@@ -319,16 +319,24 @@ public:
inline T & operator[](const Key & key) {
bool f(false);
const ssize_t i = _find(key, f);
if (f) return pim_content[pim_index[i].index];
if (f) return _value(i);
pim_content.push_back(T());
pim_index.insert(i, MapIndex(key, pim_content.size() - 1));
return pim_content.back();
}
//! \~english Same as \a value().
//! \~russian Синоним \a value().
//! \~english Read only access to element by `key`.
//! \~russian Доступ исключительно на чтение к элементу по ключу `key`.
//! \~\note
//! \~english Element with key `key` must exists, otherwise will be undefined behavior.
//! \~russian Элемент по ключу `key` должен существовать,
//! иначе это приведёт к неопределённому поведению программы и ошибкам памяти.
//! \~\sa \a operator[](), \a value(), \a key()
inline T at(const Key & key) const { return value(key); }
inline const T & at(const Key & key) const {
bool f(false);
const ssize_t i = _find(key, f);
return _value(i);
}
//! \~english Remove element with key `key` from the array and return it.
//! \~russian Удаляет элемент с ключом `key` из массива и возвращает его.
@@ -336,7 +344,7 @@ public:
bool f(false);
const ssize_t i = _find(key, f);
if (!f) return default_;
T ret(pim_content[pim_index[i].index]);
const T ret = _value(i);
_remove(i);
return ret;
}
@@ -410,7 +418,8 @@ public:
//! заданному в передаваемой функции `test`.
inline PIMap<Key, T> & removeWhere(std::function<bool(const Key & key, const T & value)> test) {
for (int i = 0; i < pim_index.size_s(); ++i) {
if (test(pim_index[i].key, pim_content[pim_index[i].index])) {
const auto & mi(pim_index[i]);
if (test(mi.key, pim_content[mi.index])) {
_remove(i);
--i;
}
@@ -454,7 +463,7 @@ public:
bool f(false);
const ssize_t i = _find(key, f);
if (f) {
pim_content[pim_index[i].index] = value;
_value(i) = value;
} else {
pim_content.push_back(value);
pim_index.insert(i, MapIndex(key, pim_content.size() - 1));
@@ -466,7 +475,7 @@ public:
bool f(false);
const ssize_t i = _find(key, f);
if (f) {
pim_content[pim_index[i].index] = std::move(value);
_value(i) = std::move(value);
} else {
pim_content.push_back(std::move(value));
pim_index.insert(i, MapIndex(key, pim_content.size() - 1));
@@ -483,7 +492,7 @@ public:
bool f(false);
const ssize_t i = _find(pair.first, f);
if (f) {
pim_content[pim_index[i].index] = pair.second;
_value(i) = pair.second;
} else {
pim_content.push_back(pair.second);
pim_index.insert(i, MapIndex(pair.first, pim_content.size() - 1));
@@ -496,7 +505,7 @@ public:
Key k(std::move(pair.first));
const ssize_t i = _find(k, f);
if (f) {
pim_content[pim_index[i].index] = std::move(pair.second);
_value(i) = std::move(pair.second);
} else {
pim_content.push_back(std::move(pair.second));
pim_index.insert(i, MapIndex(k, pim_content.size() - 1));
@@ -512,7 +521,7 @@ public:
bool f(false);
const ssize_t i = _find(key, f);
if (!f) return default_;
return pim_content[pim_index[i].index];
return _value(i);
}
//! \~english Returns an array of values of all elements
@@ -525,8 +534,9 @@ public:
//! совпадает с `value` или `default_` если такого элемента нет.
inline Key key(const T & value, const Key & default_ = Key()) const {
for (int i = 0; i < pim_index.size_s(); ++i) {
if (pim_content[pim_index[i].index] == value) {
return pim_index[i].key;
const auto & mi(pim_index[i]);
if (pim_content[mi.index] == value) {
return mi.key;
}
}
return default_;
@@ -547,7 +557,8 @@ public:
//! \~russian Выполняет функцию `void f(const Key & key, const T & value)` для каждого элемента массива.
inline void forEach(std::function<void(const Key & key, const T & value)> f) const {
for (int i = 0; i < pim_index.size_s(); ++i) {
f(pim_index[i].key, pim_content[pim_index[i].index]);
const auto & mi(pim_index[i]);
f(mi.key, pim_content[mi.index]);
}
}
@@ -560,7 +571,8 @@ public:
PIMap<Key2, T2> ret;
ret.reserve(size());
for (int i = 0; i < pim_index.size_s(); ++i) {
ret.insert(f(pim_index[i].key, pim_content[pim_index[i].index]));
const auto & mi(pim_index[i]);
ret.insert(f(mi.key, pim_content[mi.index]));
}
return ret;
}
@@ -574,7 +586,8 @@ public:
PIVector<ST> ret;
ret.reserve(size());
for (int i = 0; i < pim_index.size_s(); ++i) {
ret << f(pim_index[i].key, pim_content[pim_index[i].index]);
const auto & mi(pim_index[i]);
ret << f(mi.key, pim_content[mi.index]);
}
return ret;
}
@@ -586,8 +599,10 @@ public:
inline PIMap<Key, T> filter(std::function<bool(const Key & key, const T & value)> test) const {
PIMap<Key, T> ret;
for (int i = 0; i < pim_index.size_s(); ++i) {
if (test(pim_index[i].key, pim_content[pim_index[i].index])) {
ret.insert(pim_index[i].key, pim_content[pim_index[i].index]);
const auto & mi(pim_index[i]);
const auto & v(pim_content[mi.index]);
if (test(mi.key, v)) {
ret.insert(mi.key, v);
}
}
return ret;
@@ -650,7 +665,8 @@ private:
inline const value_type _pair(ssize_t index) const {
if (index < 0 || index >= pim_index.size_s()) return value_type();
return value_type(pim_index[index].key, pim_content[pim_index[index].index]);
const auto & mi(pim_index[index]);
return value_type(mi.key, pim_content[mi.index]);
}
inline Key & _key(ssize_t index) { return pim_index[index].key; }