code format
This commit is contained in:
@@ -13,36 +13,40 @@
|
||||
//! Андрей Бычков work.a.b@yandex.ru;
|
||||
//! \~\}
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Associative array with custom types of key and value
|
||||
Ivan Pelipenko peri4ko@yandex.ru
|
||||
PIP - Platform Independent Primitives
|
||||
Associative array with custom types of key and value
|
||||
Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef PIMAP_H
|
||||
#define PIMAP_H
|
||||
|
||||
#include "pivector.h"
|
||||
#include "pideque.h"
|
||||
#include "pipair.h"
|
||||
#include "pivector.h"
|
||||
|
||||
|
||||
template <typename Key, typename T> class PIMapIteratorConst;
|
||||
template <typename Key, typename T> class PIMapIteratorConstReverse;
|
||||
template <typename Key, typename T> class PIMapIterator;
|
||||
template <typename Key, typename T> class PIMapIteratorReverse;
|
||||
template<typename Key, typename T>
|
||||
class PIMapIteratorConst;
|
||||
template<typename Key, typename T>
|
||||
class PIMapIteratorConstReverse;
|
||||
template<typename Key, typename T>
|
||||
class PIMapIterator;
|
||||
template<typename Key, typename T>
|
||||
class PIMapIteratorReverse;
|
||||
|
||||
|
||||
//! \addtogroup Containers
|
||||
@@ -74,16 +78,21 @@ template <typename Key, typename T> class PIMapIteratorReverse;
|
||||
//! по которым их можно найти, которыми могут выступать значения любого типа.
|
||||
//! \a operator [] позволяет получить доступ к элементу по ключу,
|
||||
//! и если такого эелемента не было, то он будет создан.
|
||||
template <typename Key, typename T>
|
||||
template<typename Key, typename T>
|
||||
class PIMap {
|
||||
template <typename Key1, typename T1> friend class PIMapIteratorConst;
|
||||
template <typename Key1, typename T1> friend class PIMapIteratorConstReverse;
|
||||
template <typename Key1, typename T1> friend class PIMapIterator;
|
||||
template <typename Key1, typename T1> friend class PIMapIteratorReverse;
|
||||
template <typename P, typename Key1, typename T1>
|
||||
friend PIBinaryStream<P> & operator <<(PIBinaryStream<P> & s, const PIMap<Key1, T1> & v);
|
||||
template <typename P, typename Key1, typename T1>
|
||||
friend PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, PIMap<Key1, T1> & v);
|
||||
template<typename Key1, typename T1>
|
||||
friend class PIMapIteratorConst;
|
||||
template<typename Key1, typename T1>
|
||||
friend class PIMapIteratorConstReverse;
|
||||
template<typename Key1, typename T1>
|
||||
friend class PIMapIterator;
|
||||
template<typename Key1, typename T1>
|
||||
friend class PIMapIteratorReverse;
|
||||
template<typename P, typename Key1, typename T1>
|
||||
friend PIBinaryStream<P> & operator<<(PIBinaryStream<P> & s, const PIMap<Key1, T1> & v);
|
||||
template<typename P, typename Key1, typename T1>
|
||||
friend PIBinaryStream<P> & operator>>(PIBinaryStream<P> & s, PIMap<Key1, T1> & v);
|
||||
|
||||
public:
|
||||
typedef T mapped_type;
|
||||
typedef Key key_type;
|
||||
@@ -95,11 +104,11 @@ public:
|
||||
|
||||
//! \~english Copy constructor.
|
||||
//! \~russian Копирующий конструктор.
|
||||
inline PIMap(const PIMap<Key, T> & other) : pim_content(other.pim_content), pim_index(other.pim_index) {}
|
||||
inline PIMap(const PIMap<Key, T> & other): pim_content(other.pim_content), pim_index(other.pim_index) {}
|
||||
|
||||
//! \~english Move constructor.
|
||||
//! \~russian Перемещающий конструктор.
|
||||
inline 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).
|
||||
@@ -118,150 +127,158 @@ public:
|
||||
|
||||
//! \~english Assign operator.
|
||||
//! \~russian Оператор присваивания.
|
||||
inline 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;
|
||||
pim_index = other.pim_index;
|
||||
pim_index = other.pim_index;
|
||||
return *this;
|
||||
}
|
||||
|
||||
//! \~english Assign move operator.
|
||||
//! \~russian Оператор перемещающего присваивания.
|
||||
inline PIMap<Key, T> & operator =(PIMap<Key, T> && other) {
|
||||
inline PIMap<Key, T> & operator=(PIMap<Key, T> && other) {
|
||||
swap(other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
class iterator {
|
||||
friend class PIMap<Key, T>;
|
||||
|
||||
private:
|
||||
iterator(const PIMap<Key, T> * v, ssize_t p): parent(v), pos(p) {}
|
||||
const PIMap<Key, T> * parent;
|
||||
ssize_t pos;
|
||||
|
||||
public:
|
||||
iterator(): parent(nullptr), pos(0) {}
|
||||
const Key & key() const {return const_cast<PIMap<Key, T> * >(parent)->_key(pos);}
|
||||
T & value() {return const_cast<PIMap<Key, T> * >(parent)->_value(pos);}
|
||||
inline PIPair<Key, T> operator *() const {
|
||||
return PIPair<Key, T>(const_cast<PIMap<Key, T> * >(parent)->_key(pos), const_cast<PIMap<Key, T> * >(parent)->_value(pos));
|
||||
const Key & key() const { return const_cast<PIMap<Key, T> *>(parent)->_key(pos); }
|
||||
T & value() { return const_cast<PIMap<Key, T> *>(parent)->_value(pos); }
|
||||
inline PIPair<Key, T> operator*() const {
|
||||
return PIPair<Key, T>(const_cast<PIMap<Key, T> *>(parent)->_key(pos), const_cast<PIMap<Key, T> *>(parent)->_value(pos));
|
||||
}
|
||||
void operator ++() {++pos;}
|
||||
void operator ++(int) {++pos;}
|
||||
void operator --() {--pos;}
|
||||
void operator --(int) {--pos;}
|
||||
bool operator ==(const iterator & it) const {return (pos == it.pos);}
|
||||
bool operator !=(const iterator & it) const {return (pos != it.pos);}
|
||||
void operator++() { ++pos; }
|
||||
void operator++(int) { ++pos; }
|
||||
void operator--() { --pos; }
|
||||
void operator--(int) { --pos; }
|
||||
bool operator==(const iterator & it) const { return (pos == it.pos); }
|
||||
bool operator!=(const iterator & it) const { return (pos != it.pos); }
|
||||
};
|
||||
|
||||
|
||||
class reverse_iterator {
|
||||
friend class PIMap<Key, T>;
|
||||
|
||||
private:
|
||||
reverse_iterator(const PIMap<Key, T> * v, ssize_t p): parent(v), pos(p) {}
|
||||
const PIMap<Key, T> * parent;
|
||||
ssize_t pos;
|
||||
|
||||
public:
|
||||
reverse_iterator(): parent(nullptr), pos(0) {}
|
||||
const Key & key() const {return const_cast<PIMap<Key, T> * >(parent)->_key(pos);}
|
||||
T & value() const {return const_cast<PIMap<Key, T> * >(parent)->_value(pos);}
|
||||
inline PIPair<Key, T> operator *() const {
|
||||
return PIPair<Key, T>(const_cast<PIMap<Key, T> * >(parent)->_key(pos), const_cast<PIMap<Key, T> * >(parent)->_value(pos));
|
||||
const Key & key() const { return const_cast<PIMap<Key, T> *>(parent)->_key(pos); }
|
||||
T & value() const { return const_cast<PIMap<Key, T> *>(parent)->_value(pos); }
|
||||
inline PIPair<Key, T> operator*() const {
|
||||
return PIPair<Key, T>(const_cast<PIMap<Key, T> *>(parent)->_key(pos), const_cast<PIMap<Key, T> *>(parent)->_value(pos));
|
||||
}
|
||||
void operator ++() {--pos;}
|
||||
void operator ++(int) {--pos;}
|
||||
void operator --() {++pos;}
|
||||
void operator --(int) {++pos;}
|
||||
bool operator ==(const reverse_iterator & it) const {return (pos == it.pos);}
|
||||
bool operator !=(const reverse_iterator & it) const {return (pos != it.pos);}
|
||||
void operator++() { --pos; }
|
||||
void operator++(int) { --pos; }
|
||||
void operator--() { ++pos; }
|
||||
void operator--(int) { ++pos; }
|
||||
bool operator==(const reverse_iterator & it) const { return (pos == it.pos); }
|
||||
bool operator!=(const reverse_iterator & it) const { return (pos != it.pos); }
|
||||
};
|
||||
|
||||
|
||||
class const_iterator {
|
||||
friend class PIMap<Key, T>;
|
||||
|
||||
private:
|
||||
const_iterator(const PIMap<Key, T> * v, ssize_t p): parent(v), pos(p) {}
|
||||
const PIMap<Key, T> * parent;
|
||||
ssize_t pos;
|
||||
|
||||
public:
|
||||
const_iterator(): parent(nullptr), pos(0) {}
|
||||
const value_type operator *() const {return parent->_pair(pos);}
|
||||
const Key & key() const {return const_cast<PIMap<Key, T> * >(parent)->_key(pos);}
|
||||
const T & value() const {return const_cast<PIMap<Key, T> * >(parent)->_value(pos);}
|
||||
void operator ++() {++pos;}
|
||||
void operator ++(int) {++pos;}
|
||||
void operator --() {--pos;}
|
||||
void operator --(int) {--pos;}
|
||||
bool operator ==(const const_iterator & it) const {return (pos == it.pos);}
|
||||
bool operator !=(const const_iterator & it) const {return (pos != it.pos);}
|
||||
const value_type operator*() const { return parent->_pair(pos); }
|
||||
const Key & key() const { return const_cast<PIMap<Key, T> *>(parent)->_key(pos); }
|
||||
const T & value() const { return const_cast<PIMap<Key, T> *>(parent)->_value(pos); }
|
||||
void operator++() { ++pos; }
|
||||
void operator++(int) { ++pos; }
|
||||
void operator--() { --pos; }
|
||||
void operator--(int) { --pos; }
|
||||
bool operator==(const const_iterator & it) const { return (pos == it.pos); }
|
||||
bool operator!=(const const_iterator & it) const { return (pos != it.pos); }
|
||||
};
|
||||
|
||||
|
||||
class const_reverse_iterator {
|
||||
friend class PIMap<Key, T>;
|
||||
|
||||
private:
|
||||
const_reverse_iterator(const PIMap<Key, T> * v, ssize_t p): parent(v), pos(p) {}
|
||||
const PIMap<Key, T> * parent;
|
||||
ssize_t pos;
|
||||
|
||||
public:
|
||||
const_reverse_iterator(): parent(nullptr), pos(0) {}
|
||||
const value_type operator *() const {return parent->_pair(pos);}
|
||||
void operator ++() {--pos;}
|
||||
void operator ++(int) {--pos;}
|
||||
void operator --() {++pos;}
|
||||
void operator --(int) {++pos;}
|
||||
bool operator ==(const const_reverse_iterator & it) const {return (pos == it.pos);}
|
||||
bool operator !=(const const_reverse_iterator & it) const {return (pos != it.pos);}
|
||||
const value_type operator*() const { return parent->_pair(pos); }
|
||||
void operator++() { --pos; }
|
||||
void operator++(int) { --pos; }
|
||||
void operator--() { ++pos; }
|
||||
void operator--(int) { ++pos; }
|
||||
bool operator==(const const_reverse_iterator & it) const { return (pos == it.pos); }
|
||||
bool operator!=(const const_reverse_iterator & it) const { return (pos != it.pos); }
|
||||
};
|
||||
|
||||
|
||||
//! \~english Iterator to the first element.
|
||||
//! \~russian Итератор на первый элемент.
|
||||
inline iterator begin() {return iterator(this, 0);}
|
||||
inline iterator begin() { return iterator(this, 0); }
|
||||
|
||||
//! \~english Iterator to the element following the last element.
|
||||
//! \~russian Итератор на элемент, следующий за последним элементом.
|
||||
inline iterator end() {return iterator(this, size());}
|
||||
inline iterator end() { return iterator(this, size()); }
|
||||
|
||||
inline const_iterator begin() const {return const_iterator(this, 0);}
|
||||
inline const_iterator end() const {return const_iterator(this, size());}
|
||||
inline const_iterator begin() const { return const_iterator(this, 0); }
|
||||
inline const_iterator end() const { return const_iterator(this, size()); }
|
||||
|
||||
//! \~english Returns a reverse iterator to the first element of the reversed array.
|
||||
//! \~russian Обратный итератор на первый элемент.
|
||||
inline reverse_iterator rbegin() {return reverse_iterator(this, size() - 1);}
|
||||
inline reverse_iterator rbegin() { return reverse_iterator(this, size() - 1); }
|
||||
|
||||
//! \~english Returns a reverse iterator to the element.
|
||||
//! following the last element of the reversed array.
|
||||
//! \~russian Обратный итератор на элемент,
|
||||
//! следующий за последним элементом.
|
||||
inline reverse_iterator rend() {return reverse_iterator(this, -1);}
|
||||
inline reverse_iterator rend() { return reverse_iterator(this, -1); }
|
||||
|
||||
inline const_reverse_iterator rbegin() const {return const_reverse_iterator(this, size() - 1);}
|
||||
inline const_reverse_iterator rend() const {return const_reverse_iterator(this, -1);}
|
||||
inline const_reverse_iterator rbegin() const { return const_reverse_iterator(this, size() - 1); }
|
||||
inline const_reverse_iterator rend() const { return const_reverse_iterator(this, -1); }
|
||||
|
||||
//! \relatesalso PIMapIteratorConst
|
||||
inline PIMapIteratorConst<Key, T> makeIterator() const {return PIMapIteratorConst<Key, T>(*this);}
|
||||
inline PIMapIteratorConst<Key, T> makeIterator() const { return PIMapIteratorConst<Key, T>(*this); }
|
||||
|
||||
//! \relatesalso PIMapIterator
|
||||
inline PIMapIterator<Key, T> makeIterator() {return PIMapIterator<Key, T>(*this);}
|
||||
inline PIMapIterator<Key, T> makeIterator() { return PIMapIterator<Key, T>(*this); }
|
||||
|
||||
//! \relatesalso PIMapIteratorConstReverse
|
||||
inline PIMapIteratorConstReverse<Key, T> makeReverseIterator() const {return PIMapIteratorConstReverse<Key, T>(*this);}
|
||||
inline PIMapIteratorConstReverse<Key, T> makeReverseIterator() const { return PIMapIteratorConstReverse<Key, T>(*this); }
|
||||
|
||||
//! \relatesalso PIMapIteratorReverse
|
||||
inline PIMapIteratorReverse<Key, T> makeReverseIterator() {return PIMapIteratorReverse<Key, T>(*this);}
|
||||
inline PIMapIteratorReverse<Key, T> makeReverseIterator() { return PIMapIteratorReverse<Key, T>(*this); }
|
||||
|
||||
//! \~english Number of elements in the container.
|
||||
//! \~russian Количество элементов массива.
|
||||
//! \~\sa \a size_s(), \a capacity(), \a isEmpty(), \a isNotEmpty(), \a resize(), \a reserve()
|
||||
inline size_t size() const {return pim_content.size();}
|
||||
inline size_t size() const { return pim_content.size(); }
|
||||
|
||||
//! \~english Number of elements in the container as signed value.
|
||||
//! \~russian Количество элементов массива в виде знакового числа.
|
||||
//! \~\sa \a size(), \a capacity(), \a isEmpty(), \a isNotEmpty(), \a resize(), \a reserve()
|
||||
inline int size_s() const {return pim_content.size_s();}
|
||||
inline int size_s() const { return pim_content.size_s(); }
|
||||
|
||||
//! \~english Same as \a size().
|
||||
//! \~russian Синоним \a size().
|
||||
//! \~\sa \a size(), \a size_s(), \a capacity(), \a isEmpty(), \a isNotEmpty(), \a resize(), \a reserve()
|
||||
inline size_t length() const {return pim_content.size();}
|
||||
inline size_t length() const { return pim_content.size(); }
|
||||
|
||||
//! \~english Checks if the container has no elements.
|
||||
//! \~russian Проверяет пуст ли массив.
|
||||
@@ -269,7 +286,7 @@ public:
|
||||
//! \~english **true** if the container is empty, **false** otherwise
|
||||
//! \~russian **true** если массив пуст, **false** иначе.
|
||||
//! \~\sa \a size(), \a size_s(), \a isEmpty(), \a isNotEmpty(), \a resize(), \a reserve()
|
||||
inline bool isEmpty() const {return (pim_content.size() == 0);}
|
||||
inline bool isEmpty() const { return (pim_content.size() == 0); }
|
||||
|
||||
//! \~english Checks if the container has elements.
|
||||
//! \~russian Проверяет не пуст ли массив.
|
||||
@@ -277,7 +294,7 @@ public:
|
||||
//! \~english **true** if the container is not empty, **false** otherwise
|
||||
//! \~russian **true** если массив не пуст, **false** иначе.
|
||||
//! \~\sa \a size(), \a size_s(), \a isEmpty(), \a isNotEmpty(), \a resize(), \a reserve()
|
||||
inline bool isNotEmpty() const {return (pim_content.size() > 0);}
|
||||
inline bool isNotEmpty() const { return (pim_content.size() > 0); }
|
||||
|
||||
|
||||
//! \~english Full access to element key `key`.
|
||||
@@ -299,7 +316,7 @@ public:
|
||||
//! piCout << m; // {огурец: 350, лук: 25}
|
||||
//! \endcode
|
||||
//! \~\sa \a insert(), \a value(), \a key()
|
||||
inline 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];
|
||||
@@ -311,7 +328,7 @@ public:
|
||||
//! \~english Same as \a value().
|
||||
//! \~russian Синоним \a value().
|
||||
//! \~\sa \a operator[](), \a value(), \a key()
|
||||
inline T at(const Key & key) const {return value(key);}
|
||||
inline T at(const Key & key) const { return value(key); }
|
||||
|
||||
//! \~english Remove element with key `key` from the array and return it.
|
||||
//! \~russian Удаляет элемент с ключом `key` из массива и возвращает его.
|
||||
@@ -323,10 +340,10 @@ public:
|
||||
_remove(i);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
//! \~english Inserts all elements in array `other` to this array with overwrite.
|
||||
//! \~russian Вставляет все элементы `other` этот массив с перезаписью.
|
||||
inline 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));
|
||||
@@ -351,28 +368,23 @@ public:
|
||||
|
||||
//! \~english Compare operator with array `m`.
|
||||
//! \~russian Оператор сравнения с массивом `m`.
|
||||
inline bool operator ==(const PIMap<Key, T> & m) const {
|
||||
return (pim_content == m.pim_content && pim_index == m.pim_index);
|
||||
}
|
||||
inline bool operator==(const PIMap<Key, T> & m) const { return (pim_content == m.pim_content && pim_index == m.pim_index); }
|
||||
|
||||
//! \~english Compare operator with array `m`.
|
||||
//! \~russian Оператор сравнения с массивом `m`.
|
||||
inline bool operator !=(const PIMap<Key, T> & m) const {
|
||||
return (pim_content != m.pim_content || pim_index != m.pim_index);
|
||||
}
|
||||
inline bool operator!=(const PIMap<Key, T> & m) const { return (pim_content != m.pim_content || pim_index != m.pim_index); }
|
||||
|
||||
//! \~english Tests if element with key `key` exists in the array.
|
||||
//! \~russian Проверяет наличие элемента с ключом `key` в массиве.
|
||||
inline bool contains(const Key & key) const {
|
||||
bool f(false); _find(key, f);
|
||||
bool f(false);
|
||||
_find(key, f);
|
||||
return f;
|
||||
}
|
||||
|
||||
//! \~english Tests if element with value `value` exists in the array.
|
||||
//! \~russian Проверяет наличие элемента со значением `value` в массиве.
|
||||
inline bool containsValue(const T & value) const {
|
||||
return pim_content.contains(value);
|
||||
}
|
||||
inline bool containsValue(const T & value) const { return pim_content.contains(value); }
|
||||
|
||||
//! \~english Attempts to allocate memory for at least `new_size` elements.
|
||||
//! \~russian Резервируется память под как минимум `new_size` элементов.
|
||||
@@ -407,9 +419,7 @@ public:
|
||||
|
||||
//! \~english Same as \a remove().
|
||||
//! \~russian Синоним функции \a remove().
|
||||
inline PIMap<Key, T> & erase(const Key & key) {
|
||||
return remove(key);
|
||||
}
|
||||
inline PIMap<Key, T> & erase(const Key & key) { return remove(key); }
|
||||
|
||||
|
||||
//! \~english Clear array, remove all elements.
|
||||
@@ -424,7 +434,7 @@ public:
|
||||
pim_index.clear();
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
//! \~english Swaps array `v` other with this array.
|
||||
//! \~russian Меняет местами массив `v` с этим массивом.
|
||||
//! \~\details
|
||||
@@ -507,7 +517,7 @@ public:
|
||||
|
||||
//! \~english Returns an array of values of all elements
|
||||
//! \~russian Возвращает массив значений всех эелметнов
|
||||
inline PIVector<T> values() const {return pim_content;}
|
||||
inline PIVector<T> values() const { return pim_content; }
|
||||
|
||||
//! \~english Returns the key of the first element
|
||||
//! whose value matches `value` or `default_` if there is no such element.
|
||||
@@ -545,9 +555,10 @@ public:
|
||||
//! of calling a provided function `PIPair<Key2, T2> f(const Key & key, const T & value)` on every element in the calling array.
|
||||
//! \~russian Создаёт новый словарь PIMap<Key2, T2> с результатом вызова указанной функции
|
||||
//! `PIPair<Key2, T2> f(const Key & key, const T & value)` для каждого элемента массива.
|
||||
template <typename Key2, typename T2>
|
||||
template<typename Key2, typename T2>
|
||||
inline PIMap<Key2, T2> map(std::function<PIPair<Key2, T2>(const Key & key, const T & value)> f) const {
|
||||
PIMap<Key2, T2> ret; ret.reserve(size());
|
||||
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]));
|
||||
}
|
||||
@@ -558,15 +569,16 @@ public:
|
||||
//! of calling a provided function `ST f(const Key & key, const T & value)` on every element in the calling array.
|
||||
//! \~russian Создаёт новый массив PIVector<ST> с результатом вызова указанной функции
|
||||
//! `ST f(const Key & key, const T & value)` для каждого элемента массива.
|
||||
template <typename ST>
|
||||
template<typename ST>
|
||||
inline PIVector<ST> map(std::function<ST(const Key & key, const T & value)> f) const {
|
||||
PIVector<ST> ret; ret.reserve(size());
|
||||
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]);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
//! \~english Returns a new array with all elements
|
||||
//! that pass the test implemented by the provided function `bool test(const Key & key, const T & value)`.
|
||||
//! \~russian Возвращает новый массив со всеми элементами,
|
||||
@@ -587,24 +599,29 @@ private:
|
||||
MapIndex(Key && k, size_t i = 0): key(std::move(k)), index(i) {}
|
||||
Key key;
|
||||
size_t index;
|
||||
bool operator ==(const MapIndex & s) const {return key == s.key;}
|
||||
bool operator !=(const MapIndex & s) const {return key != s.key;}
|
||||
bool operator <(const MapIndex & s) const {return key < s.key;}
|
||||
bool operator >(const MapIndex & s) const {return key > s.key;}
|
||||
bool operator==(const MapIndex & s) const { return key == s.key; }
|
||||
bool operator!=(const MapIndex & s) const { return key != s.key; }
|
||||
bool operator<(const MapIndex & s) const { return key < s.key; }
|
||||
bool operator>(const MapIndex & s) const { return key > s.key; }
|
||||
};
|
||||
|
||||
template <typename P, typename Key1, typename T1>
|
||||
friend PIBinaryStream<P> & operator >>(PIBinaryStream<P> & s, PIDeque<typename PIMap<Key1, T1>::MapIndex> & v);
|
||||
template <typename P, typename Key1, typename T1>
|
||||
friend PIBinaryStream<P> & operator <<(PIBinaryStream<P> & s, const PIDeque<typename PIMap<Key1, T1>::MapIndex> & v);
|
||||
template<typename P, typename Key1, typename T1>
|
||||
friend PIBinaryStream<P> & operator>>(PIBinaryStream<P> & s, PIDeque<typename PIMap<Key1, T1>::MapIndex> & v);
|
||||
template<typename P, typename Key1, typename T1>
|
||||
friend PIBinaryStream<P> & operator<<(PIBinaryStream<P> & s, const PIDeque<typename PIMap<Key1, T1>::MapIndex> & v);
|
||||
|
||||
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;
|
||||
if (key > pim_index[mid].key) first = mid + 1;
|
||||
else if (key < pim_index[mid].key) last = mid - 1;
|
||||
else {found = true; return mid;}
|
||||
if (key > pim_index[mid].key)
|
||||
first = mid + 1;
|
||||
else if (key < pim_index[mid].key)
|
||||
last = mid - 1;
|
||||
else {
|
||||
found = true;
|
||||
return mid;
|
||||
}
|
||||
}
|
||||
found = false;
|
||||
return first;
|
||||
@@ -636,14 +653,14 @@ private:
|
||||
return value_type(pim_index[index].key, pim_content[pim_index[index].index]);
|
||||
}
|
||||
|
||||
inline Key & _key(ssize_t index) {return pim_index[index].key;}
|
||||
inline Key & _key(ssize_t index) { return pim_index[index].key; }
|
||||
|
||||
inline 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; }
|
||||
|
||||
inline 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]; }
|
||||
|
||||
inline 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;
|
||||
PIDeque<MapIndex> pim_index;
|
||||
@@ -678,33 +695,28 @@ private:
|
||||
//! // 2 two
|
||||
//! // 4 four
|
||||
//! \endcode
|
||||
template <typename Key, typename T>
|
||||
template<typename Key, typename T>
|
||||
class PIMapIteratorConst {
|
||||
typedef PIMap<Key, T> MapType;
|
||||
|
||||
public:
|
||||
inline PIMapIteratorConst(const PIMap<Key, T> & map): m(map), pos(-1) {}
|
||||
|
||||
//! \~english Returns current key.
|
||||
//! \~russian Возвращает ключ текущего элемента.
|
||||
//! \~\sa \a value()
|
||||
inline const Key & key() const {
|
||||
return m._key(pos);
|
||||
}
|
||||
inline const Key & key() const { return m._key(pos); }
|
||||
|
||||
//! \~english Returns current value.
|
||||
//! \~russian Возвращает значение текущего элемента.
|
||||
//! \~\sa \a key()
|
||||
inline const T & value() const {
|
||||
return m._value(pos);
|
||||
}
|
||||
inline const T & value() const { return m._value(pos); }
|
||||
|
||||
|
||||
//! \~english Returns true if iterator can jump to next entry
|
||||
//! \~russian Возвращает true если итератор может перейти к следующему элементу.
|
||||
//! \~\sa \a next()
|
||||
inline bool hasNext() const {
|
||||
return pos < (m.size_s() - 1);
|
||||
}
|
||||
inline bool hasNext() const { return pos < (m.size_s() - 1); }
|
||||
|
||||
//! \~english Jump to next entry and return true if new position is valid.
|
||||
//! \~russian Переходит к следующему элементу и возвращает true если он существует.
|
||||
@@ -717,9 +729,8 @@ public:
|
||||
//! \~english Reset iterator to initial position.
|
||||
//! \~russian Переходит на начало.
|
||||
//! \~\sa \a next()
|
||||
inline void reset() {
|
||||
pos = -1;
|
||||
}
|
||||
inline void reset() { pos = -1; }
|
||||
|
||||
private:
|
||||
const MapType & m;
|
||||
ssize_t pos;
|
||||
@@ -754,32 +765,27 @@ private:
|
||||
//! // 2 two
|
||||
//! // 1 one
|
||||
//! \endcode
|
||||
template <typename Key, typename T>
|
||||
template<typename Key, typename T>
|
||||
class PIMapIteratorConstReverse {
|
||||
typedef PIMap<Key, T> MapType;
|
||||
|
||||
public:
|
||||
inline PIMapIteratorConstReverse(const PIMap<Key, T> & map): m(map), pos(m.size_s()) {}
|
||||
|
||||
//! \~english Returns current key.
|
||||
//! \~russian Возвращает ключ текущего элемента.
|
||||
//! \~\sa \a value()
|
||||
inline const Key & key() const {
|
||||
return m._key(pos);
|
||||
}
|
||||
inline const Key & key() const { return m._key(pos); }
|
||||
|
||||
//! \~english Returns current value.
|
||||
//! \~russian Возвращает значение текущего элемента.
|
||||
//! \~\sa \a key()
|
||||
inline const T & value() const {
|
||||
return m._value(pos);
|
||||
}
|
||||
inline const T & value() const { return m._value(pos); }
|
||||
|
||||
//! \~english Returns true if iterator can jump to next entry
|
||||
//! \~russian Возвращает true если итератор может перейти к следующему элементу.
|
||||
//! \~\sa \a next()
|
||||
inline bool hasNext() const {
|
||||
return pos > 0;
|
||||
}
|
||||
inline bool hasNext() const { return pos > 0; }
|
||||
|
||||
//! \~english Jump to next entry and return true if new position is valid.
|
||||
//! \~russian Переходит к следующему элементу и возвращает true если он существует.
|
||||
@@ -792,9 +798,8 @@ public:
|
||||
//! \~english Reset iterator to initial position.
|
||||
//! \~russian Переходит на начало.
|
||||
//! \~\sa \a next()
|
||||
inline void reset() {
|
||||
pos = m.size_s();
|
||||
}
|
||||
inline void reset() { pos = m.size_s(); }
|
||||
|
||||
private:
|
||||
const MapType & m;
|
||||
ssize_t pos;
|
||||
@@ -830,32 +835,27 @@ private:
|
||||
//! // 2 two_!
|
||||
//! // 4 four_!
|
||||
//! \endcode
|
||||
template <typename Key, typename T>
|
||||
template<typename Key, typename T>
|
||||
class PIMapIterator {
|
||||
typedef PIMap<Key, T> MapType;
|
||||
|
||||
public:
|
||||
inline PIMapIterator(PIMap<Key, T> & map): m(map), pos(-1) {}
|
||||
|
||||
//! \~english Returns current key.
|
||||
//! \~russian Возвращает ключ текущего элемента.
|
||||
//! \~\sa \a value()
|
||||
inline const Key & key() const {
|
||||
return m._key(pos);
|
||||
}
|
||||
inline const Key & key() const { return m._key(pos); }
|
||||
|
||||
//! \~english Returns current value.
|
||||
//! \~russian Возвращает значение текущего элемента.
|
||||
//! \~\sa \a key()
|
||||
inline T & value() {
|
||||
return m._value(pos);
|
||||
}
|
||||
inline T & value() { return m._value(pos); }
|
||||
|
||||
//! \~english Returns true if iterator can jump to next entry
|
||||
//! \~russian Возвращает true если итератор может перейти к следующему элементу.
|
||||
//! \~\sa \a next()
|
||||
inline bool hasNext() const {
|
||||
return pos < (m.size_s() - 1);
|
||||
}
|
||||
inline bool hasNext() const { return pos < (m.size_s() - 1); }
|
||||
|
||||
//! \~english Jump to next entry and return true if new position is valid.
|
||||
//! \~russian Переходит к следующему элементу и возвращает true если он существует.
|
||||
@@ -868,9 +868,8 @@ public:
|
||||
//! \~english Reset iterator to initial position.
|
||||
//! \~russian Переходит на начало.
|
||||
//! \~\sa \a next()
|
||||
inline void reset() {
|
||||
pos = -1;
|
||||
}
|
||||
inline void reset() { pos = -1; }
|
||||
|
||||
private:
|
||||
MapType & m;
|
||||
ssize_t pos;
|
||||
@@ -906,32 +905,27 @@ private:
|
||||
//! // 2 two_!
|
||||
//! // 1 one_!
|
||||
//! \endcode
|
||||
template <typename Key, typename T>
|
||||
template<typename Key, typename T>
|
||||
class PIMapIteratorReverse {
|
||||
typedef PIMap<Key, T> MapType;
|
||||
|
||||
public:
|
||||
inline PIMapIteratorReverse(PIMap<Key, T> & map): m(map), pos(m.size_s()) {}
|
||||
|
||||
//! \~english Returns current key.
|
||||
//! \~russian Возвращает ключ текущего элемента.
|
||||
//! \~\sa \a value()
|
||||
inline const Key & key() const {
|
||||
return m._key(pos);
|
||||
}
|
||||
inline const Key & key() const { return m._key(pos); }
|
||||
|
||||
//! \~english Returns current value.
|
||||
//! \~russian Возвращает значение текущего элемента.
|
||||
//! \~\sa \a key()
|
||||
inline T & value() {
|
||||
return m._value(pos);
|
||||
}
|
||||
inline T & value() { return m._value(pos); }
|
||||
|
||||
//! \~english Returns true if iterator can jump to next entry
|
||||
//! \~russian Возвращает true если итератор может перейти к следующему элементу.
|
||||
//! \~\sa \a next()
|
||||
inline bool hasNext() const {
|
||||
return pos > 0;
|
||||
}
|
||||
inline bool hasNext() const { return pos > 0; }
|
||||
|
||||
//! \~english Jump to next entry and return true if new position is valid.
|
||||
//! \~russian Переходит к следующему элементу и возвращает true если он существует.
|
||||
@@ -944,9 +938,8 @@ public:
|
||||
//! \~english Reset iterator to initial position.
|
||||
//! \~russian Переходит на начало.
|
||||
//! \~\sa \a next()
|
||||
inline void reset() {
|
||||
pos = m.size_s();
|
||||
}
|
||||
inline void reset() { pos = m.size_s(); }
|
||||
|
||||
private:
|
||||
MapType & m;
|
||||
ssize_t pos;
|
||||
@@ -957,12 +950,11 @@ private:
|
||||
//! \~english Output operator to [std::ostream](https://en.cppreference.com/w/cpp/io/basic_ostream).
|
||||
//! \~russian Оператор вывода в [std::ostream](https://ru.cppreference.com/w/cpp/io/basic_ostream).
|
||||
template<typename Key, typename Type>
|
||||
inline std::ostream & operator <<(std::ostream & s, const PIMap<Key, Type> & v) {
|
||||
inline std::ostream & operator<<(std::ostream & s, const PIMap<Key, Type> & v) {
|
||||
s << "{";
|
||||
bool first = true;
|
||||
for (typename PIMap<Key, Type>::const_iterator i = v.begin(); i != v.end(); ++i) {
|
||||
if (!first)
|
||||
s << ", ";
|
||||
if (!first) s << ", ";
|
||||
first = false;
|
||||
s << i.key() << ": " << i.value();
|
||||
}
|
||||
@@ -976,14 +968,13 @@ inline std::ostream & operator <<(std::ostream & s, const PIMap<Key, Type> & v)
|
||||
//! \~english Output operator to \a PICout
|
||||
//! \~russian Оператор вывода в \a PICout
|
||||
template<typename Key, typename Type>
|
||||
inline PICout operator <<(PICout s, const PIMap<Key, Type> & v) {
|
||||
inline PICout operator<<(PICout s, const PIMap<Key, Type> & v) {
|
||||
s.space();
|
||||
s.saveAndSetControls(0);
|
||||
s << "{";
|
||||
bool first = true;
|
||||
for (typename PIMap<Key, Type>::const_iterator i = v.begin(); i != v.end(); ++i) {
|
||||
if (!first)
|
||||
s << ", ";
|
||||
if (!first) s << ", ";
|
||||
first = false;
|
||||
s << i.key() << ": " << i.value();
|
||||
}
|
||||
@@ -993,7 +984,9 @@ inline PICout operator <<(PICout s, const PIMap<Key, Type> & v) {
|
||||
}
|
||||
|
||||
template<typename Key, typename Type>
|
||||
inline void piSwap(PIMap<Key, Type> & f, PIMap<Key, Type> & s) {f.swap(s);}
|
||||
inline void piSwap(PIMap<Key, Type> & f, PIMap<Key, Type> & s) {
|
||||
f.swap(s);
|
||||
}
|
||||
|
||||
|
||||
#endif // PIMAP_H
|
||||
|
||||
Reference in New Issue
Block a user