replace typedef function ptr by std::function
start PIMap refactoring
This commit is contained in:
@@ -1,8 +1,17 @@
|
||||
/*! \file pimap.h
|
||||
* \brief Associative array with custom types of key and value
|
||||
*
|
||||
* This file declares PIMap
|
||||
*/
|
||||
//! \addtogroup Containers
|
||||
//! \{
|
||||
//! \file pideque.h
|
||||
//! \brief
|
||||
//! \~english Declares \a PIMap
|
||||
//! \~russian Объявление \a PIMap
|
||||
//! \~\authors
|
||||
//! \~english
|
||||
//! Ivan Pelipenko peri4ko@yandex.ru;
|
||||
//! Andrey Bychkov work.a.b@yandex.ru;
|
||||
//! \~russian
|
||||
//! Иван Пелипенко peri4ko@yandex.ru;
|
||||
//! Андрей Бычков work.a.b@yandex.ru;
|
||||
//! \~\}
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Associative array with custom types of key and value
|
||||
@@ -30,41 +39,6 @@
|
||||
#include "pipair.h"
|
||||
|
||||
|
||||
template<class T>
|
||||
void piQuickSort(T * a, ssize_t N) {
|
||||
if (N < 1) return;
|
||||
if (N < 46) {
|
||||
T tmp;
|
||||
ssize_t i,j;
|
||||
for(i=1; i<=N; i++) {
|
||||
tmp = a[i];
|
||||
j = i-1;
|
||||
while(tmp<a[j] && j>=0) {
|
||||
a[j+1] = a[j];
|
||||
j = j-1;
|
||||
}
|
||||
a[j+1] = tmp;
|
||||
}
|
||||
} else {
|
||||
ssize_t i = 0, j = N;
|
||||
T & p(a[N >> 1]);
|
||||
do {
|
||||
while (a[i] < p) i++;
|
||||
while (a[j] > p) j--;
|
||||
if (i <= j) {
|
||||
if (i != j) {
|
||||
//piCout << "swap" << i << j << a[i] << a[j];
|
||||
piSwap<T>(a[i], a[j]);
|
||||
}
|
||||
i++; j--;
|
||||
}
|
||||
} while (i <= j);
|
||||
if (j > 0) piQuickSort(a, j);
|
||||
if (N > i) piQuickSort(a + i, N - i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename Key, typename T>
|
||||
class PIMapIterator;
|
||||
|
||||
@@ -81,8 +55,9 @@ public:
|
||||
PIMap(const PIMap<Key, T> & other) {*this = other;}
|
||||
PIMap(PIMap<Key, T> && other) : pim_content(std::move(other.pim_content)), pim_index(std::move(other.pim_index)) {}
|
||||
PIMap(std::initializer_list<std::pair<Key, T>> init_list) {
|
||||
for (auto i: init_list)
|
||||
for (auto i: init_list) {
|
||||
insert(std::get<0>(i), std::get<1>(i));
|
||||
}
|
||||
}
|
||||
virtual ~PIMap() {;}
|
||||
|
||||
@@ -150,7 +125,7 @@ public:
|
||||
public:
|
||||
const_iterator(): parent(0), pos(0) {}
|
||||
const value_type operator *() const {return parent->_pair(pos);}
|
||||
const value_type* operator ->() const {cval = parent->_pair(pos); return &cval;}
|
||||
//const value_type* operator ->() const {cval = parent->_pair(pos); return &cval;}
|
||||
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;}
|
||||
@@ -159,7 +134,7 @@ public:
|
||||
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);}
|
||||
mutable value_type cval;
|
||||
//mutable value_type cval;
|
||||
};
|
||||
|
||||
class const_reverse_iterator {
|
||||
@@ -171,14 +146,14 @@ public:
|
||||
public:
|
||||
const_reverse_iterator(): parent(0), pos(0) {}
|
||||
const value_type operator *() const {return parent->_pair(pos);}
|
||||
const value_type* operator ->() const {cval = parent->_pair(pos); return &cval;}
|
||||
//const value_type* operator ->() const {cval = parent->_pair(pos); return &cval;}
|
||||
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);}
|
||||
mutable value_type cval;
|
||||
//mutable value_type cval;
|
||||
};
|
||||
|
||||
iterator begin() {return iterator(this, 0);}
|
||||
@@ -201,6 +176,7 @@ public:
|
||||
int size_s() const {return pim_content.size_s();}
|
||||
size_t length() const {return pim_content.size();}
|
||||
bool isEmpty() const {return (pim_content.size() == 0);}
|
||||
bool isNotEmpty() const {return (pim_content.size() > 0);}
|
||||
|
||||
T & operator [](const Key & key) {
|
||||
bool f(false);
|
||||
@@ -210,8 +186,7 @@ public:
|
||||
pim_index.insert(i, MapIndex(key, pim_content.size() - 1));
|
||||
return pim_content.back();
|
||||
}
|
||||
const T operator [](const Key & key) const {bool f(false); ssize_t i = _find(key, f); if (f) return pim_content[pim_index[i].index]; return T();}
|
||||
const T at(const Key & key) const {return (*this)[key];}
|
||||
T at(const Key & key) const {return value(key);}
|
||||
|
||||
PIMap<Key, T> & operator <<(const PIMap<Key, T> & other) {
|
||||
#ifndef NDEBUG
|
||||
@@ -268,7 +243,7 @@ public:
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
const 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];}
|
||||
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;}
|
||||
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; return default_;}
|
||||
PIVector<Key> keys() const {
|
||||
@@ -278,16 +253,16 @@ public:
|
||||
return ret;
|
||||
}
|
||||
|
||||
void dump() {
|
||||
piCout << "PIMap" << size() << "entries" << PICoutManipulators::NewLine << "content:";
|
||||
for (size_t i = 0; i < pim_content.size(); ++i)
|
||||
piCout << PICoutManipulators::Tab << i << ":" << pim_content[i];
|
||||
piCout << "index:";
|
||||
for (size_t i = 0; i < pim_index.size(); ++i)
|
||||
piCout << PICoutManipulators::Tab << i << ":" << pim_index[i].key << "->" << pim_index[i].index;
|
||||
}
|
||||
// void dump() {
|
||||
// piCout << "PIMap" << size() << "entries" << PICoutManipulators::NewLine << "content:";
|
||||
// for (size_t i = 0; i < pim_content.size(); ++i)
|
||||
// piCout << PICoutManipulators::Tab << i << ":" << pim_content[i];
|
||||
// piCout << "index:";
|
||||
// for (size_t i = 0; i < pim_index.size(); ++i)
|
||||
// piCout << PICoutManipulators::Tab << i << ":" << pim_index[i].key << "->" << pim_index[i].index;
|
||||
// }
|
||||
|
||||
protected:
|
||||
private:
|
||||
struct MapIndex {
|
||||
MapIndex(Key k = Key(), size_t i = 0): key(k), index(i) {;}
|
||||
Key key;
|
||||
@@ -302,7 +277,7 @@ protected:
|
||||
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 {
|
||||
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;
|
||||
@@ -313,13 +288,12 @@ protected:
|
||||
found = false;
|
||||
return first;
|
||||
}
|
||||
void _sort() {piQuickSort<MapIndex>(pim_index.data(), pim_index.size_s() - 1);}
|
||||
ssize_t _find(const Key & k, bool & found) const {
|
||||
if (pim_index.isEmpty()) {
|
||||
found = false;
|
||||
return 0;
|
||||
}
|
||||
return binarySearch(0, pim_index.size_s() - 1, k, found);
|
||||
return _binarySearch(0, pim_index.size_s() - 1, k, found);
|
||||
}
|
||||
void _remove(ssize_t index) {
|
||||
//if (index >= pim_index.size()) return;
|
||||
@@ -415,7 +389,7 @@ inline PICout operator <<(PICout s, const PIMap<Key, Type> & v) {
|
||||
if (!first)
|
||||
s << ", ";
|
||||
first = false;
|
||||
s << i->first << ": " << i->second;
|
||||
s << i.key() << ": " << i.value();
|
||||
}
|
||||
s << "}";
|
||||
s.restoreControl();
|
||||
|
||||
Reference in New Issue
Block a user