PIVector doc, forEach refactory

This commit is contained in:
Andrey
2022-04-14 15:58:40 +03:00
parent 77e0423375
commit fa93c8a486
5 changed files with 150 additions and 109 deletions

View File

@@ -515,7 +515,7 @@ public:
//! \~\code
//! PIVector<int> 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<bool(const T & e)> test) const {
@@ -541,7 +541,7 @@ public:
//! \~\code
//! PIVector<int> 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<bool(const T & e)> test) const {
@@ -565,7 +565,7 @@ public:
//! PIVector<int> 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<int> 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<int> 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<int> 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<int> 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<int> v{1, 3, 5};
//! v.fill(7);
//! piCout << v; // 7, 7, 7
//! piCout << v; // {7, 7, 7}
//! \endcode
//! \~\sa \a resize()
inline PIVector<T> & fill(const T & f = T()) {
@@ -1014,7 +1014,7 @@ public:
//! \code
//! PIVector<int> 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<T> & fill(std::function<T(size_t i)> f) {
@@ -1160,7 +1160,7 @@ public:
//! \code
//! PIVector<int> 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<T> & insert(size_t index, const T & e = T()) {
@@ -1223,7 +1223,7 @@ public:
//! \code
//! PIVector<int> 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<T> & remove(size_t index, size_t count = 1) {
@@ -1266,7 +1266,7 @@ public:
//! \~\code
//! PIVector<int> 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<bool(const T &a, const T &b)> comp)
inline PIVector<T> & sort() {
@@ -1301,7 +1301,7 @@ public:
//! \~\code
//! PIVector<int> 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<T> & sort(std::function<bool(const T &a, const T &b)> comp) {
@@ -1324,7 +1324,7 @@ public:
//! \~\code
//! PIVector<int> 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<T> & reverse() {
@@ -1374,7 +1374,7 @@ public:
//! \~\code
//! PIVector<int> 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<T> & removeOne(const T & e) {
@@ -1394,7 +1394,7 @@ public:
//! \~\code
//! PIVector<int> 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<T> & removeAll(const T & e) {
@@ -1416,7 +1416,7 @@ public:
//! \~\code
//! PIVector<int> 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<T> & removeWhere(std::function<bool(const T & e)> test) {
@@ -1449,7 +1449,7 @@ public:
//! PIVector<int> 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<T> & push_back(const T & e) {
@@ -1531,7 +1531,7 @@ public:
//! PIVector<int> 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<T> & append(const T & e) {return push_back(e);}
@@ -1567,7 +1567,7 @@ public:
//! \~\code
//! PIVector<int> v{1, 2, 3};
//! v.append(PIVector<int>{4, 5});
//! piCout << v; // 1, 2, 3, 4, 5
//! piCout << v; // {1, 2, 3, 4, 5}
//! \endcode
//! \~\sa \a append()
inline PIVector<T> & append(const PIVector<T> & v) {return push_back(v);}
@@ -1579,7 +1579,7 @@ public:
//! \~\code
//! PIVector<int> 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<T> & operator <<(const T & e) {return push_back(e);}
@@ -1591,7 +1591,7 @@ public:
//! \~\code
//! PIVector<int> 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<T> & operator <<(T && e) {return push_back(std::move(e));}
@@ -1603,7 +1603,7 @@ public:
//! \~\code
//! PIVector<int> v{1, 2, 3};
//! v << PIVector<int>{4, 5};
//! piCout << v; // 1, 2, 3, 4, 5
//! piCout << v; // {1, 2, 3, 4, 5}
//! \endcode
//! \~\sa \a append()
inline PIVector<T> & operator <<(const PIVector<T> & v) {return push_back(v);}
@@ -1622,7 +1622,7 @@ public:
//! PIVector<int> 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<T> & push_front(const T & e) {
@@ -1651,7 +1651,7 @@ public:
//! \~\code
//! PIVector<int> v{1, 2, 3};
//! v.push_front(PIVector<int>{4, 5});
//! piCout << v; // 4, 5, 1, 2, 3
//! piCout << v; // {4, 5, 1, 2, 3}
//! \endcode
//! \~\sa \a push_front()
inline PIVector<T> & push_front(const PIVector<T> & v) {
@@ -1673,7 +1673,7 @@ public:
//! PIVector<int> 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<T> & prepend(const T & e) {return push_front(e);}
@@ -1696,38 +1696,100 @@ public:
//! \~\code
//! PIVector<int> v{1, 2, 3};
//! v.prepend(PIVector<int>{4, 5});
//! piCout << v; // 4, 5, 1, 2, 3
//! piCout << v; // {4, 5, 1, 2, 3}
//! \endcode
//! \~\sa \a prepend()
inline PIVector<T> & prepend(const PIVector<T> & 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<int> v{1, 2, 3};
//! v.pop_back();
//! piCout << v; // {1, 2}
//! \endcode
//! \~\sa \a pop_front(), \a take_back(), \a take_front()
inline PIVector<T> & 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<int> v{1, 2, 3};
//! v.pop_front();
//! piCout << v; // {2, 3}
//! \endcode
//! \~\sa \a pop_back(), \a take_back(), \a take_front()
inline PIVector<T> & 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<int> 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<int> 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<double> v{1.1, 2.5, 3.8};
//! PIVector<int> v2 = v.toType<int>();
//! piCout << v2; // {1, 2, 3}
//! \endcode
//! \~\sa \a take_front(), \a pop_back(), \a pop_front()
template <typename ST>
inline PIVector<ST> toType() const {
PIVector<ST> ret(piv_size);
PIVector<ST> 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<T> & forEach(std::function<void(const T & e)> f) const {
inline void forEach(std::function<void(const T & e)> f) const {
for (size_t i = 0; i < piv_size; ++i) {
f(piv_data[i]);
}
return *this;
}
inline PIVector<T> copyForEach(std::function<T(const T & e)> f) const {
PIVector<T> ret; ret.reserve(piv_size);
inline PIVector<T> & forEach(std::function<void(T & e)> f) {
for (size_t i = 0; i < piv_size; ++i) {
ret << f(piv_data[i]);
}
return ret;
}
inline PIVector<T> & forEachInplace(std::function<T(const T & e)> 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 <typename ST>
inline PIVector<ST> toType(std::function<ST(const T & e)> f) const {return map(f);}
template <typename ST>
inline ST reduce(std::function<ST(const T & e, const ST & acc)> f, const ST & initial = ST()) const {
ST ret(initial);

View File

@@ -275,17 +275,12 @@ public:
mat.clear();
}
const PIVector2D<T> & forEach(std::function<void(const T &)> f) const {
void forEach(std::function<void(const T &)> f) const {
mat.forEach(f);
return *this;
}
PIVector2D<T> copyForEach(std::function<T(const T &)> f) const {
PIVector2D<T> ret(*this);
ret.mat = mat.copyForEach(f);
return ret;
}
PIVector2D<T> & forEachInplace(std::function<T(const T &)> f) {
mat.forEachInplace(f);
PIVector2D<T> & forEach(std::function<void(T &)> f) {
mat.forEach(f);
return *this;
}

View File

@@ -458,17 +458,11 @@ public:
PIVector<Type> toVector() const {return c;}
const _CVector & forEach(std::function<void(const Type &)> f) const {
void forEach(std::function<void(const Type &)> f) const {
c.forEach(f);
return *this;
}
_CVector copyForEach(std::function<Type(const Type &)> f) const {
_CVector ret;
ret.c = c.copyForEach(f);
return ret;
}
_CVector & forEachInplace(std::function<Type(const Type &)> f) {
c.forEachInplace(f);
_CVector & forEach(std::function<void(Type &)> f) {
c.forEach(f);
return *this;
}

View File

@@ -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<const char *>(str, v.str);
piSwap<size_t>(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<double> v{1.1, 2.5, 3.8};
PIVector<int> v2 = v.toType<int>();
piCout << v2;
return 0;
}

View File

@@ -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);