From fa93c8a486efc57e7521773b345d7b8d04d0b20d Mon Sep 17 00:00:00 2001 From: Andrey Date: Thu, 14 Apr 2022 15:58:40 +0300 Subject: [PATCH] PIVector doc, forEach refactory --- libs/main/containers/pivector.h | 154 ++++++++++++++++++++---------- libs/main/containers/pivector2d.h | 13 +-- libs/main/math/pimathvector.h | 12 +-- main.cpp | 64 +++++++------ utils/deploy_tool/main.cpp | 16 ++-- 5 files changed, 150 insertions(+), 109 deletions(-) diff --git a/libs/main/containers/pivector.h b/libs/main/containers/pivector.h index ab179472..02db962c 100644 --- a/libs/main/containers/pivector.h +++ b/libs/main/containers/pivector.h @@ -515,7 +515,7 @@ public: //! \~\code //! PIVector v{1, 2, 8, 9}; //! piCout << v.any([](int e){return e % 2 == 0;}); // true - //! piCout << v.any([](int e){return e == 3;}); // false + //! piCout << v.any([](int e){return e == 3;}); // false //! \endcode //! \~\sa \a every(), \a contains(), \a etries(), \a forEach() inline bool any(std::function test) const { @@ -541,7 +541,7 @@ public: //! \~\code //! PIVector v{1, 2, 8, 9}; //! piCout << v.every([](int e){return e % 2 == 0;}); // false - //! piCout << v.every([](int e){return e > 0;}); // true + //! piCout << v.every([](int e){return e > 0;}); // true //! \endcode //! \~\sa \a any(), \a contains(), \a entries(), \a forEach() inline bool every(std::function test) const { @@ -565,7 +565,7 @@ public: //! PIVector v{1, 2, 8, 9}; //! piCout << v[2]; // 8 //! v[2] = 5; - //! piCout << v; // 1, 2, 5, 9 + //! piCout << v; // {1, 2, 5, 9} //! \endcode //! \~\sa \a at() inline T & operator [](size_t index) {return piv_data[index];} @@ -650,10 +650,10 @@ public: //! Значение по умолчанию равно 0, что означает, что просматривается весь массив. //! \~\code //! PIVector v{1, 2, 3, 4}; - //! piCout << v.contains(3); // true - //! piCout << v.contains(5); // false - //! piCout << v.contains(3, 3); // false - //! piCout << v.contains(3, -2); // true + //! piCout << v.contains(3); // true + //! piCout << v.contains(5); // false + //! piCout << v.contains(3, 3); // false + //! piCout << v.contains(3, -2); // true //! piCout << v.contains(3, -99); // true //! \endcode //! \~\return @@ -694,8 +694,8 @@ public: //! Значение по умолчанию равно 0, что означает, что просматривается весь массив. //! \~\code //! PIVector v{2, 2, 4, 2, 6}; - //! piCout << v.entries(2); // 3 - //! piCout << v.entries(2, 2); // 1 + //! piCout << v.entries(2); // 3 + //! piCout << v.entries(2, 2); // 1 //! piCout << v.entries(2, -4); // 2 //! \endcode //! \~\sa \a every(), \a any(), \a contains(), \a forEach(), \a indexOf() @@ -854,12 +854,12 @@ public: //! и означает, что просматривается весь массив. //! \~\code //! PIVector v{2, 5, 9, 2}; - //! piCout << v.lastIndexOf(2); // 3 - //! piCout << v.lastIndexOf(7); // -1 - //! piCout << v.lastIndexOf(2, 2); // 0 - //! piCout << v.lastIndexOf(2, -3); // 0 + //! piCout << v.lastIndexOf(2); // 3 + //! piCout << v.lastIndexOf(7); // -1 + //! piCout << v.lastIndexOf(2, 2); // 0 + //! piCout << v.lastIndexOf(2, -3); // 0 //! piCout << v.lastIndexOf(2, -300); // -1 - //! piCout << v.lastIndexOf(2, 300); // 3 + //! piCout << v.lastIndexOf(2, 300); // 3 //! \endcode //! \~\sa \a indexOf(), \a indexWhere(), \a lastIndexWhere(), \a contains() inline ssize_t lastIndexOf(const T & e, ssize_t start = -1) const { @@ -918,7 +918,7 @@ public: //! PIVector v{2, 5, 9, 2}; //! int a[2] = {12, 13}; //! memcpy(vec.data(1), carr, 2 * sizeof(int)); - //! piCout << v; // 2, 12, 13, 2 + //! piCout << v; // {2, 12, 13, 2} //! \endcode inline T * data(size_t index = 0) {return &(piv_data[index]);} @@ -995,7 +995,7 @@ public: //! \code //! PIVector v{1, 3, 5}; //! v.fill(7); - //! piCout << v; // 7, 7, 7 + //! piCout << v; // {7, 7, 7} //! \endcode //! \~\sa \a resize() inline PIVector & fill(const T & f = T()) { @@ -1014,7 +1014,7 @@ public: //! \code //! PIVector v{1, 3, 5}; //! v.fill([](size_t i){return i*2;}); - //! piCout << v; // 0, 2, 4 + //! piCout << v; // {0, 2, 4} //! \endcode //! \~\sa \a resize() inline PIVector & fill(std::function f) { @@ -1160,7 +1160,7 @@ public: //! \code //! PIVector v{1, 3, 5}; //! v.insert(2, 7); - //! piCout << v; // 1, 3, 7, 5 + //! piCout << v; // {1, 3, 7, 5} //! \endcode //! \~\sa \a append(), \a prepend(), \a remove() inline PIVector & insert(size_t index, const T & e = T()) { @@ -1223,7 +1223,7 @@ public: //! \code //! PIVector v{1, 3, 7, 5}; //! v.remove(1, 2); - //! piCout << v; // 1, 5 + //! piCout << v; // {1, 5} //! \endcode //! \~\sa \a resize(), \a insert(), \a removeOne(), \a removeAll(), \a removeWhere() inline PIVector & remove(size_t index, size_t count = 1) { @@ -1266,7 +1266,7 @@ public: //! \~\code //! PIVector v{5, 7, 4, 2, 8, 6, 1, 9, 0, 3}; //! v.sort(); - //! piCout << v; // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 + //! piCout << v; // {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} //! \endcode //! \~\sa \a sort(std::function comp) inline PIVector & sort() { @@ -1301,7 +1301,7 @@ public: //! \~\code //! PIVector v{5, 7, 4, 2, 8, 6, 1, 9, 0, 3}; //! v.sort([](const int & a, const int & b){return a > b;}); - //! piCout << v; // 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 + //! piCout << v; // {9, 8, 7, 6, 5, 4, 3, 2, 1, 0} //! \endcode //! \~\sa \a sort() inline PIVector & sort(std::function comp) { @@ -1324,7 +1324,7 @@ public: //! \~\code //! PIVector v{1, 3, 7, 5}; //! v.reverse(); - //! piCout << v; // 5, 7, 3, 1 + //! piCout << v; // {5, 7, 3, 1} //! \endcode //! \~\sa \a reversed() inline PIVector & reverse() { @@ -1374,7 +1374,7 @@ public: //! \~\code //! PIVector v{3, 2, 5, 2, 7}; //! v.removeOne(2); - //! piCout << v; // 3, 5, 2, 7 + //! piCout << v; // {3, 5, 2, 7} //! \endcode //! \~\sa \a remove(), \a removeAll(), \a removeWhere() inline PIVector & removeOne(const T & e) { @@ -1394,7 +1394,7 @@ public: //! \~\code //! PIVector v{3, 2, 5, 2, 7}; //! v.removeAll(2); - //! piCout << v; // 3, 5, 7 + //! piCout << v; // {3, 5, 7} //! \endcode //! \~\sa \a remove(), \a removeOne(), \a removeWhere() inline PIVector & removeAll(const T & e) { @@ -1416,7 +1416,7 @@ public: //! \~\code //! PIVector v{3, 2, 5, 2, 7}; //! v.removeWhere([](const int & i){return i > 2;}); - //! piCout << v; // 2, 2 + //! piCout << v; // {2, 2} //! \endcode //! \~\sa \a remove(), \a removeOne(), \a removeWhere() inline PIVector & removeWhere(std::function test) { @@ -1449,7 +1449,7 @@ public: //! PIVector v{1, 2, 3}; //! v.push_back(4); //! v.push_back(5); - //! piCout << v; // 1, 2, 3, 4, 5 + //! piCout << v; // {1, 2, 3, 4, 5} //! \endcode //! \~\sa \a push_front(), \a append(), \a prepend(), \a insert() inline PIVector & push_back(const T & e) { @@ -1531,7 +1531,7 @@ public: //! PIVector v{1, 2, 3}; //! v.append(4); //! v.append(5); - //! piCout << v; // 1, 2, 3, 4, 5 + //! piCout << v; // {1, 2, 3, 4, 5} //! \endcode //! \~\sa \a prepend(), \a push_front(), \a push_back(), \a insert() inline PIVector & append(const T & e) {return push_back(e);} @@ -1567,7 +1567,7 @@ public: //! \~\code //! PIVector v{1, 2, 3}; //! v.append(PIVector{4, 5}); - //! piCout << v; // 1, 2, 3, 4, 5 + //! piCout << v; // {1, 2, 3, 4, 5} //! \endcode //! \~\sa \a append() inline PIVector & append(const PIVector & v) {return push_back(v);} @@ -1579,7 +1579,7 @@ public: //! \~\code //! PIVector v{1, 2, 3}; //! v << 4 << 5; - //! piCout << v; // 1, 2, 3, 4, 5 + //! piCout << v; // {1, 2, 3, 4, 5} //! \endcode //! \~\sa \a append() inline PIVector & operator <<(const T & e) {return push_back(e);} @@ -1591,7 +1591,7 @@ public: //! \~\code //! PIVector v{1, 2, 3}; //! v << 4 << 5; - //! piCout << v; // 1, 2, 3, 4, 5 + //! piCout << v; // {1, 2, 3, 4, 5} //! \endcode //! \~\sa \a append() inline PIVector & operator <<(T && e) {return push_back(std::move(e));} @@ -1603,7 +1603,7 @@ public: //! \~\code //! PIVector v{1, 2, 3}; //! v << PIVector{4, 5}; - //! piCout << v; // 1, 2, 3, 4, 5 + //! piCout << v; // {1, 2, 3, 4, 5} //! \endcode //! \~\sa \a append() inline PIVector & operator <<(const PIVector & v) {return push_back(v);} @@ -1622,7 +1622,7 @@ public: //! PIVector v{1, 2, 3}; //! v.push_front(4); //! v.push_front(5); - //! piCout << v; // 5, 4, 1, 2, 3 + //! piCout << v; // {5, 4, 1, 2, 3} //! \endcode //! \~\sa \a push_back(), \a append(), \a prepend(), \a insert() inline PIVector & push_front(const T & e) { @@ -1651,7 +1651,7 @@ public: //! \~\code //! PIVector v{1, 2, 3}; //! v.push_front(PIVector{4, 5}); - //! piCout << v; // 4, 5, 1, 2, 3 + //! piCout << v; // {4, 5, 1, 2, 3} //! \endcode //! \~\sa \a push_front() inline PIVector & push_front(const PIVector & v) { @@ -1673,7 +1673,7 @@ public: //! PIVector v{1, 2, 3}; //! v.prepend(4); //! v.prepend(5); - //! piCout << v; // 5, 4, 1, 2, 3 + //! piCout << v; // {5, 4, 1, 2, 3} //! \endcode //! \~\sa \a append(), \a push_back(), \a push_front(), \a insert() inline PIVector & prepend(const T & e) {return push_front(e);} @@ -1696,38 +1696,100 @@ public: //! \~\code //! PIVector v{1, 2, 3}; //! v.prepend(PIVector{4, 5}); - //! piCout << v; // 4, 5, 1, 2, 3 + //! piCout << v; // {4, 5, 1, 2, 3} //! \endcode //! \~\sa \a prepend() inline PIVector & prepend(const PIVector & v) {return push_front(v);} + //! \brief + //! \~english Remove one element from the end of the array. + //! \~russian Удаляет один элемент с конца массива. + //! \~\details + //! \~english Deleting an element from the end is very fast + //! and does not depend on the size of the array. + //! \~russian Удаление элемента с конца выполняется очень быстро + //! и не зависит от размера массива. + //! \~\code + //! PIVector v{1, 2, 3}; + //! v.pop_back(); + //! piCout << v; // {1, 2} + //! \endcode + //! \~\sa \a pop_front(), \a take_back(), \a take_front() inline PIVector & pop_back() { if (piv_size == 0) return *this; resize(piv_size - 1); return *this; } + + //! \brief + //! \~english Remove one element from the begining of the array. + //! \~russian Удаляет один элемент с начала массива. + //! \~\details + //! \~english Removing an element from the beginning takes longer than from the end. + //! This time is directly proportional to the size of the array. + //! All iterators and references are invalidated. + //! \~russian Удаление элемента с начала выполняется дольше чем с конца. + //! Это время прямопропорционально размеру массива. + //! При удалении элемента все итераторы и указатели становятся нерабочими. + //! \~\code + //! PIVector v{1, 2, 3}; + //! v.pop_front(); + //! piCout << v; // {2, 3} + //! \endcode + //! \~\sa \a pop_back(), \a take_back(), \a take_front() inline PIVector & pop_front() { if (piv_size == 0) return *this; remove(0); return *this; } + //! \brief + //! \~english Remove one element from the end of the array and return it. + //! \~russian Удаляет один элемент с начала массива и возвращает его. + //! \~\details + //! \~\code + //! PIVector v{1, 2, 3}; + //! piCout << v.take_back(); // 3 + //! piCout << v; // {1, 2} + //! \endcode + //! \~\sa \a take_front(), \a pop_back(), \a pop_front() inline T take_back() { T e(back()); pop_back(); return e; } + + //! \brief + //! \~english Remove one element from the begining of the array and return it. + //! \~russian Удаляет один элемент с конца массива и возвращает его. + //! \~\details + //! \~\code + //! PIVector v{1, 2, 3}; + //! piCout << v.take_front(); // 1 + //! piCout << v; // {2, 3} + //! \endcode + //! \~\sa \a take_front(), \a pop_back(), \a pop_front() inline T take_front() { T e(front()); pop_front(); return e; } + //! \brief + //! \~english Returns an array converted to another type. + //! \~russian Возвращает конвертированный в другой тип массив. + //! \~\details + //! \~\code + //! PIVector v{1.1, 2.5, 3.8}; + //! PIVector v2 = v.toType(); + //! piCout << v2; // {1, 2, 3} + //! \endcode + //! \~\sa \a take_front(), \a pop_back(), \a pop_front() template inline PIVector toType() const { - PIVector ret(piv_size); + PIVector ret; ret.reserve(piv_size); for (size_t i = 0; i < piv_size; ++i) { - ret[i] = ST(piv_data[i]); + ret << ST(piv_data[i]); } return ret; } @@ -1740,24 +1802,15 @@ public: return ret; } - inline const PIVector & forEach(std::function f) const { + inline void forEach(std::function f) const { for (size_t i = 0; i < piv_size; ++i) { f(piv_data[i]); } - return *this; } - inline PIVector copyForEach(std::function f) const { - PIVector ret; ret.reserve(piv_size); + inline PIVector & forEach(std::function f) { for (size_t i = 0; i < piv_size; ++i) { - ret << f(piv_data[i]); - } - return ret; - } - - inline PIVector & forEachInplace(std::function f) { - for (size_t i = 0; i < piv_size; ++i) { - piv_data[i] = f(piv_data[i]); + f(piv_data[i]); } return *this; } @@ -1771,9 +1824,6 @@ public: return ret; } - template - inline PIVector toType(std::function f) const {return map(f);} - template inline ST reduce(std::function f, const ST & initial = ST()) const { ST ret(initial); diff --git a/libs/main/containers/pivector2d.h b/libs/main/containers/pivector2d.h index 9f8a7957..6a170e0a 100644 --- a/libs/main/containers/pivector2d.h +++ b/libs/main/containers/pivector2d.h @@ -275,17 +275,12 @@ public: mat.clear(); } - const PIVector2D & forEach(std::function f) const { + void forEach(std::function f) const { mat.forEach(f); - return *this; } - PIVector2D copyForEach(std::function f) const { - PIVector2D ret(*this); - ret.mat = mat.copyForEach(f); - return ret; - } - PIVector2D & forEachInplace(std::function f) { - mat.forEachInplace(f); + + PIVector2D & forEach(std::function f) { + mat.forEach(f); return *this; } diff --git a/libs/main/math/pimathvector.h b/libs/main/math/pimathvector.h index bfdf5a1d..5ec4c13d 100644 --- a/libs/main/math/pimathvector.h +++ b/libs/main/math/pimathvector.h @@ -458,17 +458,11 @@ public: PIVector toVector() const {return c;} - const _CVector & forEach(std::function f) const { + void forEach(std::function f) const { c.forEach(f); - return *this; } - _CVector copyForEach(std::function f) const { - _CVector ret; - ret.c = c.copyForEach(f); - return ret; - } - _CVector & forEachInplace(std::function f) { - c.forEachInplace(f); + _CVector & forEach(std::function f) { + c.forEach(f); return *this; } diff --git a/main.cpp b/main.cpp index d6f0ed6a..4b9ef294 100644 --- a/main.cpp +++ b/main.cpp @@ -1,14 +1,13 @@ #include "pip.h" - -class ROString { +class ConstChars { public: - inline ROString() : len(0), str(nullptr) {} - inline ROString(const char * string) { + inline ConstChars() : len(0), str(nullptr) {} + inline ConstChars(const char * string) { str = string; len = strlen(string); } - inline ROString(const char * data, size_t size) { + inline ConstChars(const char * data, size_t size) { str = data; len = size; } @@ -29,7 +28,7 @@ public: //! \~\brief //! \~english Assign operator. //! \~russian Оператор присваивания. - inline ROString & operator =(const ROString & s) { + inline ConstChars & operator =(const ConstChars & s) { if (this == &s) return *this; len = s.len; str = s.str; @@ -39,12 +38,12 @@ public: //! \~\brief //! \~english Assign move operator. //! \~russian Оператор перемещающего присваивания. - inline ROString & operator =(ROString && s) { + inline ConstChars & operator =(ConstChars && s) { swap(s); return *this; } - inline void swap(ROString& v) { + inline void swap(ConstChars& v) { piSwap(str, v.str); piSwap(len, v.len); } @@ -54,7 +53,7 @@ private: const char * str; }; -PICout operator <<(PICout s, const ROString & v) { +PICout operator <<(PICout s, const ConstChars & v) { s.space(); s.quote(); s.write(v.data(), v.size()); @@ -63,30 +62,33 @@ PICout operator <<(PICout s, const ROString & v) { } int main(int argc, char * argv[]) { - const char * dd = "12345"; - char text[]{ "hello" }; - ROString s("test"); - piCout << s; - ROString s2; - piCout << s2; - s2 = s; - piCout << s2; - s2 = text; - s = s2; - piCout << s << s2; +// const char * dd = "12345"; +// char text[]{ "hello" }; +// ConstChars s("test"); +// piCout << s; +// ConstChars s2; +// piCout << s2; // s2 = s; - text[1] = '0'; - piCout << text; - piCout << s << s2 << ROString(text, 3); +// piCout << s2; +// s2 = text; +// s = s2; // piCout << s << s2; -// PIString ss(s.data(), s.length()); -// piCout << ss; - PICout(PICoutManipulators::DefaultControls | PICoutManipulators::AddQuotes) << PIString(dd); +//// s2 = s; +// text[1] = '0'; +// piCout << text; +// piCout << s << s2 << ConstChars(text, 3); +//// piCout << s << s2; +//// PIString ss(s.data(), s.length()); +//// piCout << ss; +// PICout(PICoutManipulators::DefaultControls | PICoutManipulators::AddQuotes) << PIString(dd); - piCout << PIString::fromUTF8("test"); - piCout << PIString::fromUTF8("бюд\n"); - piCout.writePIString(PIString::fromUTF8("test\n")); - piCout.writePIString(PIString::fromUTF8("бюд\n")); - piCout << "бюд\n"; +// piCout << PIString::fromUTF8("test"); +// piCout << PIString::fromUTF8("бюд\n"); +// piCout.writePIString(PIString::fromUTF8("test\n")); +// piCout.writePIString(PIString::fromUTF8("бюд\n")); +// piCout << "бюд\n"; + PIVector v{1.1, 2.5, 3.8}; + PIVector v2 = v.toType(); + piCout << v2; return 0; } diff --git a/utils/deploy_tool/main.cpp b/utils/deploy_tool/main.cpp index 4ef02a34..28112044 100644 --- a/utils/deploy_tool/main.cpp +++ b/utils/deploy_tool/main.cpp @@ -622,7 +622,7 @@ int main(int argc, char * argv[]) { for (int i = 0; ; ++i) { if (qt_deps[i].lib.isEmpty()) break; - qt_deps[i].plugins.forEach([&](const PIString & p){qt_filters[p] = PIStringList("*");}); + qt_deps[i].plugins.forEach([](const PIString & p){qt_filters[p] = PIStringList("*");}); } if (!cli.argumentValue("Platforms").isEmpty()) qplatforms = cli.argumentValue("Platforms"); @@ -635,15 +635,15 @@ int main(int argc, char * argv[]) { int _i = qp.indexOf("="); if (_i < 0) continue; PIString pname = qp.left(_i).trim(); - PIString pfilt = qp.mid(_i + 1).trim(); + PIStringList pfilt = qp.mid(_i + 1).trim().split(","); if (pname == "*") { for (int i = 0; ; ++i) { if (qt_deps[i].lib.isEmpty()) break; - qt_deps[i].plugins.forEach([&](const PIString & p){qt_filters[p] = pfilt.split(",");}); + qt_deps[i].plugins.forEach([&pfilt](const PIString & p){qt_filters[p] = pfilt;}); } } else { qt_plugins << pname; - qt_filters[pname] = pfilt.split(","); + qt_filters[pname] = pfilt; } } qt_filters["platforms"] = platforms; @@ -674,11 +674,11 @@ int main(int argc, char * argv[]) { if (!cli.argumentValue("depth").isEmpty()) depth = cli.argumentValue("depth").toInt(); - cli.optionalArguments().forEach([&](const PIString & a){ + cli.optionalArguments().forEach([](const PIString & a){ if (PIDir::isExists(a)) { if (target_dir.isEmpty()) target_dir = a; - PIDir(a).allEntries().forEach([&](const PIFile::FileInfo & fi){ + PIDir(a).allEntries().forEach([](const PIFile::FileInfo & fi){ if (fi.isFile()) input_files << fi.path; }); @@ -692,8 +692,8 @@ int main(int argc, char * argv[]) { //piCout << files; if (depth > 0) { - input_files.forEach([&](const PIString & f){procLdd(f);}); - qt_add_libs.forEach([&](const PIString & f){ + input_files.forEach([](const PIString & f){procLdd(f);}); + qt_add_libs.forEach([](const PIString & f){ PIString l = findLib(qt_pref + f + qt_suff); if (l.isEmpty()) return; procLdd(l, true);