diff --git a/libs/main/containers/pimap.h b/libs/main/containers/pimap.h index 14640065..2f3ad565 100644 --- a/libs/main/containers/pimap.h +++ b/libs/main/containers/pimap.h @@ -30,41 +30,6 @@ #include "pipair.h" -template -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=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(a[i], a[j]); - } - i++; j--; - } - } while (i <= j); - if (j > 0) piQuickSort(a, j); - if (N > i) piQuickSort(a + i, N - i); - } -} - - template class PIMapIterator; @@ -75,9 +40,9 @@ class PIMap { template friend PIByteArray & operator <<(PIByteArray & s, const PIMap & v); template friend class PIMapIterator; public: - PIMap() {;} + PIMap() {} PIMap(const PIMap & other) {*this = other;} - PIMap(PIMap && other) : pim_content(std::move(other.pim_content)), pim_index(std::move(other.pim_index)) {} + PIMap(PIMap && other) : pim_content(std::move(other.pim_content)) {} PIMap(std::initializer_list> init_list) { for (auto i: init_list) insert(std::get<0>(i), std::get<1>(i)); @@ -88,7 +53,6 @@ public: if (this == &other) return *this; clear(); pim_content = other.pim_content; - pim_index = other.pim_index; return *this; } @@ -203,12 +167,15 @@ public: T & operator [](const Key & key) { bool f(false); ssize_t i = _find(key, f); - if (f) return pim_content[pim_index[i].index]; - pim_content.push_back(T()); - pim_index.insert(i, MapIndex(key, pim_content.size() - 1)); - return pim_content.back(); + if (!f) pim_content.insert(i, PIPair(key, T())); + return pim_content[i].second; + } + const T operator [](const Key & key) const { + bool f(false); + ssize_t i = _find(key, f); + if (f) return pim_content[i].second; + return T(); } - 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];} PIMap & operator <<(const PIMap & other) { @@ -219,27 +186,26 @@ public: #endif assert(&other != this); if (other.isEmpty()) return *this; - if (other.size() == 1) {insert(other.pim_index[0].key, other.pim_content[0]); return *this;} - if (other.size() == 2) {insert(other.pim_index[0].key, other.pim_content[0]); insert(other.pim_index[1].key, other.pim_content[1]); return *this;} - for (int i = 0; i < other.pim_index.size_s(); ++i) - insert(other.pim_index[i].key, other.pim_content[other.pim_index[i].index]); +// if (other.size() == 1) {insert(other.pim_index[0].key, other.pim_content[0]); return *this;} +// if (other.size() == 2) {insert(other.pim_index[0].key, other.pim_content[0]); insert(other.pim_index[1].key, other.pim_content[1]); return *this;} + for (int i = 0; i < other.pim_content.size_s(); ++i) + insert(other.pim_content[i].first, other.pim_content[i].second); return *this; } - bool operator ==(const PIMap & t) const {return (pim_content == t.pim_content && pim_index == t.pim_index);} - bool operator !=(const PIMap & t) const {return (pim_content != t.pim_content || pim_index != t.pim_index);} + bool operator ==(const PIMap & t) const {return (pim_content == t.pim_content);} + bool operator !=(const PIMap & t) const {return (pim_content != t.pim_content);} bool contains(const Key & key) const {bool f(false); _find(key, f); return f;} - PIMap & reserve(size_t new_size) {pim_content.reserve(new_size); pim_index.reserve(new_size); return *this;} + PIMap & reserve(size_t new_size) {pim_content.reserve(new_size);return *this;} PIMap & removeOne(const Key & key) {bool f(false); ssize_t i = _find(key, f); if (f) _remove(i); return *this;} PIMap & remove(const Key & key) {return removeOne(key);} PIMap & erase(const Key & key) {return removeOne(key);} - PIMap & clear() {pim_content.clear(); pim_index.clear(); return *this;} + PIMap & clear() {pim_content.clear(); return *this;} void swap(PIMap & other) { pim_content.swap(other.pim_content); - pim_index.swap(other.pim_index); } PIMap & insert(const Key & key, const T & value) { @@ -247,10 +213,9 @@ public: ssize_t i = _find(key, f); //piCout << "insert key=" << key << "found=" << f << "index=" << i << "value=" << value; if (f) { - pim_content[pim_index[i].index] = value; + pim_content[i].second = value; } else { - pim_content.push_back(value); - pim_index.insert(i, MapIndex(key, pim_content.size() - 1)); + pim_content.insert(i, PIPair(key, value)); } return *this; } @@ -259,20 +224,34 @@ public: ssize_t i = _find(key, f); //piCout << "insert key=" << key << "found=" << f << "index=" << i << "value=" << value; if (f) { - pim_content[pim_index[i].index] = std::move(value); + pim_content[i].second = std::move(value); } else { - pim_content.push_back(std::move(value)); - pim_index.insert(i, MapIndex(key, pim_content.size() - 1)); +// pim_content.push_back(std::move(value)); +// pim_index.insert(i, MapIndex(key, pim_content.size() - 1)); + pim_content.insert(i, PIPair(key, std::move(value))); } 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];} - PIVector 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_;} + 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[i].second; + } + PIVector values() const { + PIVector ret; + for (size_t i = 0; i < pim_content.size(); ++i) ret << pim_content[i].second; + return ret; + } + Key key(const T & value_, const Key & default_ = Key()) const { + for (int i = 0; i < pim_content.size_s(); ++i) + if (pim_content[i].second == value_) + return pim_content[i].first; + return default_; + } PIVector keys() const { PIVector ret; - for (int i = 0; i < pim_index.size_s(); ++i) - ret << pim_index[i].key; + for (size_t i = 0; i < pim_content.size(); ++i) ret << pim_content[i].first; return ret; } @@ -280,66 +259,53 @@ public: 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; +// 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: - struct MapIndex { - MapIndex(Key k = Key(), size_t i = 0): key(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;} - }; - template friend PIByteArray & operator >>(PIByteArray & s, PIDeque::MapIndex> & v); - template friend PIByteArray & operator <<(PIByteArray & s, const PIDeque::MapIndex> & v); +// struct MapIndex { +// MapIndex(Key k = Key(), size_t i = 0): key(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;} +// }; +// template friend PIByteArray & operator >>(PIByteArray & s, PIDeque::MapIndex> & v); +// template friend PIByteArray & operator <<(PIByteArray & s, const PIDeque::MapIndex> & v); 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; + if (key > pim_content[mid].first) first = mid + 1; + else if (key < pim_content[mid].first) last = mid - 1; else {found = true; return mid;} } found = false; return first; } - void _sort() {piQuickSort(pim_index.data(), pim_index.size_s() - 1);} ssize_t _find(const Key & k, bool & found) const { - if (pim_index.isEmpty()) { + if (pim_content.isEmpty()) { found = false; return 0; } - return binarySearch(0, pim_index.size_s() - 1, k, found); + return binarySearch(0, pim_content.size_s() - 1, k, found); } void _remove(ssize_t index) { - //if (index >= pim_index.size()) return; - 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) - if (pim_index[i].index == bi) { - pim_index[i].index = ci; - break; - } - piSwap(pim_content[ci], pim_content.back()); - pim_content.resize(pim_index.size()); + pim_content.remove(index); } const value_type _pair(ssize_t index) const { - if (index < 0 || index >= pim_index.size_s()) - return value_type(); - //piCout << "_pair" << index << pim_index[index].index; - return value_type(pim_index[index].key, pim_content[pim_index[index].index]); + if (index < 0 || index >= pim_content.size_s()) return value_type(); + return pim_content[index]; } - Key & _key(ssize_t index) {return pim_index[index].key;} - T & _value(ssize_t index) {return pim_content[pim_index[index].index];} + Key & _key(ssize_t index) {return pim_content[index].first;} + T & _value(ssize_t index) {return pim_content[index].second;} - PIVector pim_content; - PIDeque pim_index; + PIDeque> pim_content; }; diff --git a/libs/main/core/pibytearray.h b/libs/main/core/pibytearray.h index da738b30..0c1a6fac 100644 --- a/libs/main/core/pibytearray.h +++ b/libs/main/core/pibytearray.h @@ -630,9 +630,9 @@ inline PIByteArray & operator >>(PIByteArray & s, PIVector2D & v) { //! \~russian Оператор сохранения template inline PIByteArray & operator <<(PIByteArray & s, const PIMap & v) { - s << int(v.pim_index.size_s()); - for (uint i = 0; i < v.size(); ++i) - s << int(v.pim_index[i].index) << v.pim_index[i].key; +// s << int(v.pim_index.size_s()); +// for (uint i = 0; i < v.size(); ++i) +// s << int(v.pim_index[i].index) << v.pim_index[i].key; s << v.pim_content; return s; } @@ -647,17 +647,17 @@ inline PIByteArray & operator >>(PIByteArray & s, PIMap & v) { printf("error with PIMap<%s, %s>\n", __PIP_TYPENAME__(Key), __PIP_TYPENAME__(T)); assert(s.size_s() >= 4); } - int sz; s >> sz; v.pim_index.resize(sz); - int ind = 0; - for (int i = 0; i < sz; ++i) { - s >> ind >> v.pim_index[i].key; - v.pim_index[i].index = ind; - } +// int sz; s >> sz; v.pim_index.resize(sz); +// int ind = 0; +// for (int i = 0; i < sz; ++i) { +// s >> ind >> v.pim_index[i].key; +// v.pim_index[i].index = ind; +// } s >> v.pim_content; - if (v.pim_content.size_s() != v.pim_index.size_s()) { - piCout << "Warning: loaded invalid PIMap, clear"; - v.clear(); - } +// if (v.pim_content.size_s() != v.pim_index.size_s()) { +// piCout << "Warning: loaded invalid PIMap, clear"; +// v.clear(); +// } return s; } diff --git a/main.cpp b/main.cpp index 5843de39..33a23c95 100644 --- a/main.cpp +++ b/main.cpp @@ -9,10 +9,33 @@ int main(int argc, char * argv[]) { PITimeMeasurer tm; + PIStringList small_sl; + PIStringList big_sl; +// { +// for (int i=0; i> small_sl; + slba >> big_sl; + f.close(); + } + piCout << "map insert..."; tm.reset(); for (int c=0; c m1; + PIMap m1; for (int i=0; i insert []..."; tm.reset(); for (int c=0; c m1; + PIMap m1; for (int i=0; i insert rnd..."; tm.reset(); for (int c=0; c m1; + PIMap m1; for (int i=0; i insert rnd []..."; tm.reset(); for (int c=0; c m1; + PIMap m1; for (int i=0; i insert..."; tm.reset(); for (int c=0; c m1; + PIMap m1; for (int i=0; i insert []..."; tm.reset(); for (int c=0; c m1; + PIMap m1; for (int i=0; i insert rnd..."; tm.reset(); for (int c=0; c m1; + PIMap m1; for (int i=0; i insert rnd []..."; tm.reset(); for (int c=0; c m1; + PIMap m1; for (int i=0; i insert rnd []" << tm.elapsed_m(); - PIStringList small_sl; - for (int i=0; i insert..."; tm.reset(); for (int c=0; c insert..."; - tm.reset(); PIMap m3; + m3.reserve(big_cnt); + tm.reset(); for (int i=0; i insert" << tm.elapsed_m(); piCout << "map interate..."; @@ -171,8 +191,9 @@ int main(int argc, char * argv[]) { piCout << "map interate" << tm.elapsed_m(); piCout << "map insert..."; - tm.reset(); PIMap m4; + m4.reserve(big_cnt); + tm.reset(); for (int i=0; i