rvalue functions for containers

This commit is contained in:
2020-07-30 22:26:05 +03:00
parent a12e63e569
commit 79e17b48b8
10 changed files with 61 additions and 41 deletions

View File

@@ -74,7 +74,7 @@ class PIMap {
public:
PIMap() {;}
PIMap(const PIMap<Key, T> & other) {*this = other;}
PIMap(PIMap<Key, T> && other) {swap(other);}
PIMap(PIMap<Key, T> && other) : pim_content(std::move(other.pim_content)), pim_index(std::move(other.pim_index)) {}
virtual ~PIMap() {;}
PIMap<Key, T> & operator =(const PIMap<Key, T> & other) {
@@ -86,8 +86,8 @@ public:
}
PIMap<Key, T> & operator =(PIMap<Key, T> && other) {
if (this == &other) return *this;
swap(other);
PIMap<Key, T> moved(std::move(other));
swap(moved);
return *this;
}
@@ -226,10 +226,6 @@ public:
pim_content.swap(other.pim_content);
pim_index.swap(other.pim_index);
}
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) {
bool f(false);
@@ -243,6 +239,18 @@ public:
}
return *this;
}
PIMap<Key, T> & insert(const Key & key, T && value) {
bool f(false);
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);
} else {
pim_content.push_back(std::move(value));
pim_index.insert(i, MapIndex(key, pim_content.size() - 1));
}
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<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_;}