picout and clean

This commit is contained in:
Бычков Андрей
2022-08-08 16:44:37 +03:00
parent 8551499a5e
commit 724a2dffcf
16 changed files with 242 additions and 187 deletions

View File

@@ -91,15 +91,15 @@ public:
//! \~english Constructs an empty map.
//! \~russian Создает пустой словарь.
PIMap() {}
inline PIMap() {}
//! \~english Copy constructor.
//! \~russian Копирующий конструктор.
PIMap(const PIMap<Key, T> & other) {*this = other;}
inline PIMap(const PIMap<Key, T> & other) {*this = other;}
//! \~english Move constructor.
//! \~russian Перемещающий конструктор.
PIMap(PIMap<Key, T> && other) : pim_content(std::move(other.pim_content)), pim_index(std::move(other.pim_index)) {}
inline PIMap(PIMap<Key, T> && other) : pim_content(std::move(other.pim_content)), pim_index(std::move(other.pim_index)) {}
//! \~english Contructs map from
//! [C++11 initializer list](https://en.cppreference.com/w/cpp/utility/initializer_list).
@@ -110,7 +110,7 @@ public:
//! PIMap <int, PIString> m{{1, "a"}, {2, "b"}};
//! piCout << m; // {1, 2, 3}
//! \endcode
PIMap(std::initializer_list<std::pair<Key, T>> init_list) {
inline PIMap(std::initializer_list<std::pair<Key, T>> init_list) {
for (auto i: init_list) {
insert(std::get<0>(i), std::get<1>(i));
}
@@ -118,7 +118,7 @@ public:
//! \~english Assign operator.
//! \~russian Оператор присваивания.
PIMap<Key, T> & operator =(const PIMap<Key, T> & other) {
inline PIMap<Key, T> & operator =(const PIMap<Key, T> & other) {
if (this == &other) return *this;
clear();
pim_content = other.pim_content;
@@ -128,7 +128,7 @@ public:
//! \~english Assign move operator.
//! \~russian Оператор перемещающего присваивания.
PIMap<Key, T> & operator =(PIMap<Key, T> && other) {
inline PIMap<Key, T> & operator =(PIMap<Key, T> && other) {
swap(other);
return *this;
}
@@ -212,53 +212,53 @@ public:
};
iterator begin() {return iterator(this, 0);}
inline iterator begin() {return iterator(this, 0);}
iterator end() {return iterator(this, size());}
inline iterator end() {return iterator(this, size());}
const_iterator begin() const {return const_iterator(this, 0);}
inline const_iterator begin() const {return const_iterator(this, 0);}
const_iterator end() const {return const_iterator(this, size());}
inline const_iterator end() const {return const_iterator(this, size());}
const_iterator constBegin() const {return const_iterator(this, 0);}
inline const_iterator constBegin() const {return const_iterator(this, 0);}
const_iterator constEnd() const {return const_iterator(this, size());}
inline const_iterator constEnd() const {return const_iterator(this, size());}
reverse_iterator rbegin() {return reverse_iterator(this, size() - 1);}
inline reverse_iterator rbegin() {return reverse_iterator(this, size() - 1);}
reverse_iterator rend() {return reverse_iterator(this, -1);}
inline reverse_iterator rend() {return reverse_iterator(this, -1);}
const_reverse_iterator rbegin() const {return const_reverse_iterator(this, size() - 1);}
inline const_reverse_iterator rbegin() const {return const_reverse_iterator(this, size() - 1);}
const_reverse_iterator rend() const {return const_reverse_iterator(this, -1);}
inline const_reverse_iterator rend() const {return const_reverse_iterator(this, -1);}
const_reverse_iterator constRbegin() const {return const_reverse_iterator(this, size() - 1);}
inline const_reverse_iterator constRbegin() const {return const_reverse_iterator(this, size() - 1);}
const_reverse_iterator constRend() const {return const_reverse_iterator(this, -1);}
inline const_reverse_iterator constRend() const {return const_reverse_iterator(this, -1);}
//! \relatesalso PIMapIteratorConst
PIMapIteratorConst<Key, T> makeIterator() const {return PIMapIteratorConst<Key, T>(*this);}
inline PIMapIteratorConst<Key, T> makeIterator() const {return PIMapIteratorConst<Key, T>(*this);}
//! \relatesalso PIMapIterator
PIMapIterator<Key, T> makeIterator() {return PIMapIterator<Key, T>(*this);}
inline PIMapIterator<Key, T> makeIterator() {return PIMapIterator<Key, T>(*this);}
//! \relatesalso PIMapIteratorConstReverse
PIMapIteratorConstReverse<Key, T> makeReverseIterator() const {return PIMapIteratorConstReverse<Key, T>(*this);}
inline PIMapIteratorConstReverse<Key, T> makeReverseIterator() const {return PIMapIteratorConstReverse<Key, T>(*this);}
//! \relatesalso PIMapIteratorReverse
PIMapIteratorReverse<Key, T> makeReverseIterator() {return PIMapIteratorReverse<Key, T>(*this);}
inline PIMapIteratorReverse<Key, T> makeReverseIterator() {return PIMapIteratorReverse<Key, T>(*this);}
size_t size() const {return pim_content.size();}
inline size_t size() const {return pim_content.size();}
int size_s() const {return pim_content.size_s();}
inline int size_s() const {return pim_content.size_s();}
size_t length() const {return pim_content.size();}
inline size_t length() const {return pim_content.size();}
bool isEmpty() const {return (pim_content.size() == 0);}
inline bool isEmpty() const {return (pim_content.size() == 0);}
bool isNotEmpty() const {return (pim_content.size() > 0);}
inline bool isNotEmpty() const {return (pim_content.size() > 0);}
T & operator [](const Key & key) {
inline T & operator [](const Key & key) {
bool f(false);
ssize_t i = _find(key, f);
if (f) return pim_content[pim_index[i].index];
@@ -266,9 +266,10 @@ public:
pim_index.insert(i, MapIndex(key, pim_content.size() - 1));
return pim_content.back();
}
T at(const Key & key) const {return value(key);}
T take(const Key & key) const {
inline T at(const Key & key) const {return value(key);}
inline T take(const Key & key) const {
bool f(false);
ssize_t i = _find(key, f);
if (!f) return T();
@@ -277,7 +278,7 @@ public:
return ret;
}
PIMap<Key, T> & operator <<(const PIMap<Key, T> & other) {
inline PIMap<Key, T> & operator <<(const PIMap<Key, T> & other) {
#ifndef NDEBUG
if (&other == this) {
printf("error with PIMap<%s, %s>::<<\n", __PIP_TYPENAME__(Key), __PIP_TYPENAME__(T));
@@ -300,28 +301,28 @@ public:
return *this;
}
bool operator ==(const PIMap<Key, T> & t) const {
inline bool operator ==(const PIMap<Key, T> & t) const {
return (pim_content == t.pim_content && pim_index == t.pim_index);
}
bool operator !=(const PIMap<Key, T> & t) const {
inline bool operator !=(const PIMap<Key, T> & t) const {
return (pim_content != t.pim_content || pim_index != t.pim_index);
}
bool contains(const Key & key) const {
inline bool contains(const Key & key) const {
bool f(false); _find(key, f);
return f;
}
bool containsValue(const T & value) const {
inline bool containsValue(const T & value) const {
return pim_content.contains(value);
}
PIMap<Key, T> & reserve(size_t new_size) {
inline PIMap<Key, T> & reserve(size_t new_size) {
pim_content.reserve(new_size);
pim_index.reserve(new_size);
return *this;
}
PIMap<Key, T> & remove(const Key & key) {
inline PIMap<Key, T> & remove(const Key & key) {
bool f(false);
ssize_t i = _find(key, f);
if (f) _remove(i);
@@ -329,7 +330,7 @@ public:
}
PIMap<Key, T> & removeWhere(std::function<bool(const Key & key, const T & value)> 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 (pim_index[i].key, pim_content[pim_index[i].index]) {
_remove(i);
@@ -338,20 +339,20 @@ public:
}
}
PIMap<Key, T> & erase(const Key & key) {return remove(key);}
inline PIMap<Key, T> & erase(const Key & key) {return remove(key);}
PIMap<Key, T> & clear() {
inline PIMap<Key, T> & clear() {
pim_content.clear();
pim_index.clear();
return *this;
}
void swap(PIMap<Key, T> & other) {
inline void swap(PIMap<Key, T> & other) {
pim_content.swap(other.pim_content);
pim_index.swap(other.pim_index);
}
PIMap<Key, T> & insert(const Key & key, const T & value) {
inline PIMap<Key, T> & insert(const Key & key, const T & value) {
bool f(false);
ssize_t i = _find(key, f);
if (f) {
@@ -362,7 +363,8 @@ public:
}
return *this;
}
PIMap<Key, T> & insert(const Key & key, T && value) {
inline PIMap<Key, T> & insert(const Key & key, T && value) {
bool f(false);
ssize_t i = _find(key, f);
if (f) {
@@ -373,7 +375,8 @@ public:
}
return *this;
}
PIMap<Key, T> & insert(const PIPair<Key, T> & pair) {
inline PIMap<Key, T> & insert(const PIPair<Key, T> & pair) {
bool f(false);
ssize_t i = _find(pair.first, f);
if (f) {
@@ -384,7 +387,8 @@ public:
}
return *this;
}
PIMap<Key, T> & insert(PIPair<Key, T> && pair) {
inline PIMap<Key, T> & insert(PIPair<Key, T> && pair) {
bool f(false);
Key k(std::move(pair.first));
ssize_t i = _find(k, f);
@@ -397,16 +401,16 @@ public:
return *this;
}
T value(const Key & key, const T & default_ = T()) const {
inline T value(const Key & key, const T & default_ = T()) const {
bool f(false);
ssize_t i = _find(key, f);
if (!f) return default_;
return pim_content[pim_index[i].index];
}
PIVector<T> values() const {return pim_content;}
inline PIVector<T> values() const {return pim_content;}
Key key(const T & value_, const Key & default_ = Key()) const {
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;
@@ -415,7 +419,7 @@ public:
return default_;
}
PIVector<Key> keys() const {
inline PIVector<Key> keys() const {
PIVector<Key> ret;
ret.reserve(pim_index.size());
for (int i = 0; i < pim_index.size_s(); ++i) {
@@ -424,7 +428,7 @@ public:
return ret;
}
void forEach(std::function<void(const Key & key, const T & value)> f) const {
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]);
}
@@ -465,7 +469,7 @@ private:
template <typename P, typename Key1, typename T1>
friend PIBinaryStream<P> & operator <<(PIBinaryStream<P> & s, const PIDeque<typename PIMap<Key1, T1>::MapIndex> & v);
ssize_t _binarySearch(ssize_t first, ssize_t last, const Key & key, bool & found) const {
inline ssize_t _binarySearch(ssize_t first, ssize_t last, const Key & key, bool & found) const {
ssize_t mid;
while (first <= last) {
mid = (first + last) / 2;
@@ -477,7 +481,7 @@ private:
return first;
}
ssize_t _find(const Key & k, bool & found) const {
inline ssize_t _find(const Key & k, bool & found) const {
if (pim_index.isEmpty()) {
found = false;
return 0;
@@ -485,7 +489,7 @@ private:
return _binarySearch(0, pim_index.size_s() - 1, k, found);
}
void _remove(ssize_t index) {
inline void _remove(ssize_t index) {
size_t ci = pim_index[index].index, bi = pim_index.size() - 1;
pim_index.remove(index);
for (size_t i = 0; i < pim_index.size(); ++i) {
@@ -498,18 +502,18 @@ private:
pim_content.resize(pim_index.size());
}
const value_type _pair(ssize_t index) const {
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]);
}
Key & _key(ssize_t index) {return pim_index[index].key;}
inline Key & _key(ssize_t index) {return pim_index[index].key;}
const Key & _key(ssize_t index) const {return pim_index[index].key;}
inline const Key & _key(ssize_t index) const {return pim_index[index].key;}
T & _value(ssize_t index) {return pim_content[pim_index[index].index];}
inline T & _value(ssize_t index) {return pim_content[pim_index[index].index];}
const T & _value(ssize_t index) const {return pim_content[pim_index[index].index];}
inline const T & _value(ssize_t index) const {return pim_content[pim_index[index].index];}
PIVector<T> pim_content;
@@ -549,19 +553,19 @@ template <typename Key, typename T>
class PIMapIteratorConst {
typedef PIMap<Key, T> MapType;
public:
PIMapIteratorConst(const PIMap<Key, T> & map): m(map), pos(-1) {}
inline PIMapIteratorConst(const PIMap<Key, T> & map): m(map), pos(-1) {}
//! \~english Returns current key.
//! \~russian Возвращает ключ текущего элемента.
//! \~\sa \a value()
const Key & key() const {
inline const Key & key() const {
return m._key(pos);
}
//! \~english Returns current value.
//! \~russian Возвращает значение текущего элемента.
//! \~\sa \a key()
const T & value() const {
inline const T & value() const {
return m._value(pos);
}
@@ -625,19 +629,19 @@ template <typename Key, typename T>
class PIMapIteratorConstReverse {
typedef PIMap<Key, T> MapType;
public:
PIMapIteratorConstReverse(const PIMap<Key, T> & map): m(map), pos(m.size_s()) {}
inline PIMapIteratorConstReverse(const PIMap<Key, T> & map): m(map), pos(m.size_s()) {}
//! \~english Returns current key.
//! \~russian Возвращает ключ текущего элемента.
//! \~\sa \a value()
const Key & key() const {
inline const Key & key() const {
return m._key(pos);
}
//! \~english Returns current value.
//! \~russian Возвращает значение текущего элемента.
//! \~\sa \a key()
const T & value() const {
inline const T & value() const {
return m._value(pos);
}
@@ -701,19 +705,19 @@ template <typename Key, typename T>
class PIMapIterator {
typedef PIMap<Key, T> MapType;
public:
PIMapIterator(PIMap<Key, T> & map): m(map), pos(-1) {}
inline PIMapIterator(PIMap<Key, T> & map): m(map), pos(-1) {}
//! \~english Returns current key.
//! \~russian Возвращает ключ текущего элемента.
//! \~\sa \a value()
const Key & key() const {
inline const Key & key() const {
return m._key(pos);
}
//! \~english Returns current value.
//! \~russian Возвращает значение текущего элемента.
//! \~\sa \a key()
T & value() {
inline T & value() {
return m._value(pos);
}
@@ -777,19 +781,19 @@ template <typename Key, typename T>
class PIMapIteratorReverse {
typedef PIMap<Key, T> MapType;
public:
PIMapIteratorReverse(PIMap<Key, T> & map): m(map), pos(m.size_s()) {}
inline PIMapIteratorReverse(PIMap<Key, T> & map): m(map), pos(m.size_s()) {}
//! \~english Returns current key.
//! \~russian Возвращает ключ текущего элемента.
//! \~\sa \a value()
const Key & key() const {
inline const Key & key() const {
return m._key(pos);
}
//! \~english Returns current value.
//! \~russian Возвращает значение текущего элемента.
//! \~\sa \a key()
T & value() {
inline T & value() {
return m._value(pos);
}