diff --git a/lib/main/containers/pideque.h b/lib/main/containers/pideque.h index 770c123e..259b997e 100644 --- a/lib/main/containers/pideque.h +++ b/lib/main/containers/pideque.h @@ -49,8 +49,8 @@ public: PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T)) resize(pid_size, f); } - inline PIDeque(PIDeque && other): pid_data(0), pid_size(0), pid_rsize(0), pid_start(0) { - swap(other); + inline PIDeque(PIDeque && other): pid_data(other.pid_data), pid_size(other.pid_size), pid_rsize(other.pid_size), pid_start(other.pid_start) { + other._reset(); } inline virtual ~PIDeque() { //piCout << "~PIDeque"; @@ -70,7 +70,8 @@ public: } inline PIDeque & operator =(PIDeque && other) { - swap(other); + PIDeque moved(std::move(other)); + swap(moved); return *this; } @@ -323,12 +324,6 @@ public: piSwap(pid_rsize, other.pid_rsize); piSwap(pid_start, other.pid_start); } - inline void swap(PIDeque && other) { - piSwap(pid_data, other.pid_data); - piSwap(pid_size, other.pid_size); - piSwap(pid_rsize, other.pid_rsize); - piSwap(pid_start, other.pid_start); - } typedef int (*CompareFunc)(const T * , const T * ); static int compare_func(const T * t0, const T * t1) {return (*t0) < (*t1) ? -1 : ((*t0) == (*t1) ? 0 : 1);} @@ -387,7 +382,9 @@ public: inline PIDeque & operator <<(const PIDeque & t) {return append(t);} inline PIDeque & push_front(const T & v) {insert(0, v); return *this;} + inline PIDeque & push_front(T && v) {insert(0, std::move(v)); return *this;} inline PIDeque & prepend(const T & v) {return push_front(v);} + inline PIDeque & prepend(T && v) {return push_front(std::move(v));} inline PIDeque & pop_back() {if (pid_size == 0) return *this; resize(pid_size - 1); return *this;} inline PIDeque & pop_front() {if (pid_size == 0) return *this; remove(0); return *this;} @@ -525,7 +522,7 @@ private: template<> inline void PIDeque::newT(T * dst, const T * src, size_t s) {PIINTROSPECTION_CONTAINER_USED(T, s); memcpy((void*)(dst), (const void*)(src), s * sizeof(T));} \ template<> inline void PIDeque::deleteT(T *, size_t sz) {PIINTROSPECTION_CONTAINER_UNUSED(T, sz);} \ template<> inline void PIDeque::elementNew(T * to, const T & from) {(*to) = from;} \ - template<> inline void PIDeque::elementNew(T * to, T && from) {(*to) = from;} \ + template<> inline void PIDeque::elementNew(T * to, T && from) {(*to) = std::move(from);} \ template<> inline void PIDeque::elementDelete(T &) {;} \ template<> inline PIDeque & PIDeque::_resizeRaw(size_t new_size) { \ if (new_size > pid_size) { \ diff --git a/lib/main/containers/pimap.h b/lib/main/containers/pimap.h index da18e4a9..6cf4b5c4 100644 --- a/lib/main/containers/pimap.h +++ b/lib/main/containers/pimap.h @@ -74,7 +74,7 @@ class PIMap { public: PIMap() {;} PIMap(const PIMap & other) {*this = other;} - PIMap(PIMap && other) {swap(other);} + PIMap(PIMap && other) : pim_content(std::move(other.pim_content)), pim_index(std::move(other.pim_index)) {} virtual ~PIMap() {;} PIMap & operator =(const PIMap & other) { @@ -86,8 +86,8 @@ public: } PIMap & operator =(PIMap && other) { - if (this == &other) return *this; - swap(other); + PIMap 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 && other) { - pim_content.swap(other.pim_content); - pim_index.swap(other.pim_index); - } PIMap & insert(const Key & key, const T & value) { bool f(false); @@ -243,6 +239,18 @@ public: } return *this; } + PIMap & 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 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_;} diff --git a/lib/main/containers/piqueue.h b/lib/main/containers/piqueue.h index 60612797..cebd1d76 100644 --- a/lib/main/containers/piqueue.h +++ b/lib/main/containers/piqueue.h @@ -35,13 +35,15 @@ public: PIQueue() {} virtual ~PIQueue() {} PIDeque & enqueue(const T & v) {PIDeque::push_front(v); return *this;} + PIDeque & enqueue(T && v) {PIDeque::push_front(std::move(v)); return *this;} T dequeue() {return PIDeque::take_back();} T & head() {return PIDeque::back();} const T & head() const {return PIDeque::back();} PIVector toVector() { - PIVector v(PIDeque::size()); + PIVector v; + v.reserve(PIDeque::size()); for (uint i = 0; i < PIDeque::size(); ++i) - v[i] = PIDeque::at(i); + v.push_back(PIDeque::at(i)); return v; } }; diff --git a/lib/main/containers/piset.h b/lib/main/containers/piset.h index 795124ac..5a819f78 100644 --- a/lib/main/containers/piset.h +++ b/lib/main/containers/piset.h @@ -75,6 +75,7 @@ public: typedef T key_type; PISet & operator <<(const T & t) {_CSet::insert(t, 0); return *this;} + PISet & operator <<(T && t) {_CSet::insert(std::move(t), 0); return *this;} PISet & operator <<(const PISet & other) {(*(_CSet*)this) << *((_CSet*)&other); return *this;} //! Returns if element "t" exists in this set diff --git a/lib/main/containers/pistack.h b/lib/main/containers/pistack.h index 9cbd072b..02e62318 100644 --- a/lib/main/containers/pistack.h +++ b/lib/main/containers/pistack.h @@ -33,10 +33,17 @@ public: PIStack() {;} virtual ~PIStack() {;} PIVector & push(const T & v) {PIVector::push_back(v); return *this;} + PIVector & push(T && v) {PIVector::push_back(std::move(v)); return *this;} T pop() {return PIVector::take_back();} T & top() {return PIVector::back();} const T & top() const {return PIVector::back();} - PIVector toVector() {PIVector v(PIVector::size()); for (uint i = 0; i < PIVector::size(); ++i) v[i] = PIVector::at(i); return v;} + PIVector toVector() { + PIVector v; + v.reserve(PIVector::size()); + for (uint i = 0; i < PIVector::size(); ++i) + v.push_back(PIVector::at(i)); + return v; + } }; #endif // PISTACK_H diff --git a/lib/main/containers/pivector.h b/lib/main/containers/pivector.h index 7b3e3982..017e0f73 100644 --- a/lib/main/containers/pivector.h +++ b/lib/main/containers/pivector.h @@ -48,8 +48,8 @@ public: PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T)) resize(piv_size, f); } - inline PIVector(PIVector && other): piv_data(0), piv_size(0), piv_rsize(0) { - swap(other); + inline PIVector(PIVector && other): piv_data(other.piv_data), piv_size(other.piv_size), piv_rsize(other.piv_size) { + other._reset(); } inline virtual ~PIVector() { PIINTROSPECTION_CONTAINER_DELETE(T) @@ -69,7 +69,8 @@ public: } inline PIVector & operator =(PIVector && other) { - swap(other); + PIVector moved(std::move(other)); + swap(moved); return *this; } @@ -300,11 +301,6 @@ public: piSwap(piv_size, other.piv_size); piSwap(piv_rsize, other.piv_rsize); } - inline void swap(PIVector && other) { - piSwap(piv_data, other.piv_data); - piSwap(piv_size, other.piv_size); - piSwap(piv_rsize, other.piv_rsize); - } typedef int (*CompareFunc)(const T * , const T * ); static int compare_func(const T * t0, const T * t1) {return (*t0) < (*t1) ? -1 : ((*t0) == (*t1) ? 0 : 1);} @@ -363,7 +359,9 @@ public: inline PIVector & operator <<(const PIVector & other) {return append(other);} inline PIVector & push_front(const T & v) {insert(0, v); return *this;} + inline PIVector & push_front(T && v) {insert(0, std::move(v)); return *this;} inline PIVector & prepend(const T & v) {return push_front(v);} + inline PIVector & prepend(T && v) {return push_front(std::move(v));} inline PIVector & pop_back() { if (piv_size == 0) @@ -466,7 +464,7 @@ private: template<> inline void PIVector::newT(T * dst, const T * src, size_t s) {PIINTROSPECTION_CONTAINER_USED(T, s); memcpy((void*)(dst), (const void*)(src), s * sizeof(T));} \ template<> inline void PIVector::deleteT(T *, size_t sz) {PIINTROSPECTION_CONTAINER_UNUSED(T, sz);} \ template<> inline void PIVector::elementNew(T * to, const T & from) {(*to) = from;} \ - template<> inline void PIVector::elementNew(T * to, T && from) {(*to) = from;} \ + template<> inline void PIVector::elementNew(T * to, T && from) {(*to) = std::move(from);} \ template<> inline void PIVector::elementDelete(T &) {;} \ template<> inline PIVector & PIVector::_resizeRaw(size_t new_size) { \ if (new_size > piv_size) { \ diff --git a/lib/main/containers/pivector2d.h b/lib/main/containers/pivector2d.h index 744316af..f573439f 100644 --- a/lib/main/containers/pivector2d.h +++ b/lib/main/containers/pivector2d.h @@ -44,10 +44,10 @@ public: cols_ = cols; mat.resize(rows*cols, f); } - inline PIVector2D(size_t rows, size_t cols, const PIVector & v) { - mat = v; - rows_ = rows; - cols_ = cols; + inline PIVector2D(size_t rows, size_t cols, const PIVector & v) : rows_(rows), cols_(cols), mat(v) { + mat.resize(rows*cols); + } + inline PIVector2D(size_t rows, size_t cols, PIVector && v) : rows_(rows), cols_(cols), mat(std::move(v)) { mat.resize(rows*cols); } inline PIVector2D(const PIVector > & v) { @@ -236,6 +236,7 @@ public: PIVector > toVectors() const { PIVector > ret; + ret.reserve(rows_); for(size_t i = 0; i < rows_; ++i) ret << PIVector(mat.data(i*cols_), cols_); return ret; diff --git a/lib/main/core/pistring.h b/lib/main/core/pistring.h index 00c87197..bbc8543a 100644 --- a/lib/main/core/pistring.h +++ b/lib/main/core/pistring.h @@ -49,9 +49,9 @@ public: PIString & operator +=(const PIByteArray & ba) {appendFromChars((const char * )ba.data(), ba.size_s(), __utf8name__); return *this;} PIString & operator +=(const PIString & str); - PIString(const PIString & o): PIDeque() {*this += o;} + PIString(const PIString & o): PIDeque(o) {} - PIString(PIString && o): PIDeque() {swap(o);} + PIString(PIString && o): PIDeque(std::move(o)) {} //! Contructs string with single symbol "c" @@ -91,7 +91,7 @@ public: PIString & operator =(const PIString & o) {if (this == &o) return *this; clear(); *this += o; return *this;} - PIString & operator =(PIString && o) {if (this == &o) return *this; swap(o); return *this;} + PIString & operator =(PIString && o) {PIString moved(std::move(o)); swap(moved); return *this;} /*! \brief Return c-string representation of string * \details Converts content of string to c-string and return diff --git a/lib/main/core/pistringlist.h b/lib/main/core/pistringlist.h index 141b1478..e5a9210e 100644 --- a/lib/main/core/pistringlist.h +++ b/lib/main/core/pistringlist.h @@ -37,17 +37,22 @@ public: //! Contructs strings list with one string "str" PIStringList(const PIString & str) {push_back(str);} + PIStringList(PIString && str) {push_back(std::move(str));} //! Contructs empty strings list with strings "s0" and "s1" PIStringList(const PIString & s0, const PIString & s1) {push_back(s0); push_back(s1);} + PIStringList(PIString && s0, PIString && s1) {push_back(std::move(s0)); push_back(std::move(s1));} //! Contructs empty strings list with strings "s0", "s1" and "s2" PIStringList(const PIString & s0, const PIString & s1, const PIString & s2) {push_back(s0); push_back(s1); push_back(s2);} + PIStringList(PIString && s0, PIString && s1, PIString && s2) {push_back(std::move(s0)); push_back(std::move(s1)); push_back(std::move(s2));} //! Contructs empty strings list with strings "s0", "s1", "s2" and "s3" PIStringList(const PIString & s0, const PIString & s1, const PIString & s2, const PIString & s3) {push_back(s0); push_back(s1); push_back(s2); push_back(s3);} + PIStringList(PIString && s0, PIString && s1, PIString && s2, PIString && s3) {push_back(std::move(s0)); push_back(std::move(s1)); push_back(std::move(s2)); push_back(std::move(s3));} - PIStringList(const PIStringList & o): PIDeque() {resize(o.size()); for (uint i = 0; i < size(); ++i) (*this)[i] = o[i];} + PIStringList(const PIStringList & o): PIDeque(o) {} + PIStringList(PIStringList && o): PIDeque(std::move(o)) {} PIStringList(const PIVector & o): PIDeque() {resize(o.size()); for (uint i = 0; i < size(); ++i) (*this)[i] = o[i];} PIStringList(const PIDeque & o): PIDeque() {resize(o.size()); for (uint i = 0; i < size(); ++i) (*this)[i] = o[i];} diff --git a/main.cpp b/main.cpp index 8e7a86b4..dbabd1f9 100644 --- a/main.cpp +++ b/main.cpp @@ -35,6 +35,7 @@ int main() { #include "picodeparser.h" int main() { piDebug = false; + PIString t("1234567890-="); double min = -1, max = -1, mean = 0; for (int i = 0; i < 50; ++i) { PITimeMeasurer tm; @@ -43,10 +44,10 @@ int main() { PIStringList sl; sl.reserve(500000); for (int i = 0; i < 500000; ++i) { - //PIString s("1234567890-="); - //sl << s; - //sl.push_back(PIString("1234567890-=")); - sl << PIString("1234567890-="); +// PIString s(t); +// sl << s; + sl.push_back(PIString(t)); + //sl << PIString("1234567890-="); } double ms = tm.elapsed_m(); if (min < 0) min = ms;