PIVector: map, reduce

rename arguments in uniform style
This commit is contained in:
2021-09-07 18:29:09 +03:00
parent 127935086c
commit 61d42e0ac5
2 changed files with 85 additions and 67 deletions

View File

@@ -39,10 +39,10 @@ public:
alloc(size); alloc(size);
newT(piv_data, data, piv_size); newT(piv_data, data, piv_size);
} }
inline PIVector(const PIVector<T> & other): piv_data(0), piv_size(0), piv_rsize(0) { inline PIVector(const PIVector<T> & v): piv_data(0), piv_size(0), piv_rsize(0) {
PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T)) PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T))
alloc(other.piv_size); alloc(v.piv_size);
newT(piv_data, other.piv_data, piv_size); newT(piv_data, v.piv_data, piv_size);
} }
inline PIVector(std::initializer_list<T> init_list): piv_data(0), piv_size(0), piv_rsize(0) { inline PIVector(std::initializer_list<T> init_list): piv_data(0), piv_size(0), piv_rsize(0) {
PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T)) PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T))
@@ -53,13 +53,13 @@ public:
PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T)) PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T))
resize(piv_size, f); resize(piv_size, f);
} }
inline PIVector(size_t piv_size, std::function<T(size_t)> f): piv_data(0), piv_size(0), piv_rsize(0) { inline PIVector(size_t piv_size, std::function<T(size_t i)> f): piv_data(0), piv_size(0), piv_rsize(0) {
PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T)) PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T))
resize(piv_size, f); resize(piv_size, f);
} }
inline PIVector(PIVector<T> && other): piv_data(other.piv_data), piv_size(other.piv_size), piv_rsize(other.piv_rsize) { inline PIVector(PIVector<T> && v): piv_data(v.piv_data), piv_size(v.piv_size), piv_rsize(v.piv_rsize) {
PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T)) PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T))
other._reset(); v._reset();
} }
inline virtual ~PIVector() { inline virtual ~PIVector() {
PIINTROSPECTION_CONTAINER_DELETE(T) PIINTROSPECTION_CONTAINER_DELETE(T)
@@ -69,17 +69,17 @@ public:
_reset(); _reset();
} }
inline PIVector<T> & operator =(const PIVector<T> & other) { inline PIVector<T> & operator =(const PIVector<T> & v) {
if (this == &other) return *this; if (this == &v) return *this;
clear(); clear();
deleteT(piv_data, piv_size); deleteT(piv_data, piv_size);
alloc(other.piv_size); alloc(v.piv_size);
newT(piv_data, other.piv_data, piv_size); newT(piv_data, v.piv_data, piv_size);
return *this; return *this;
} }
inline PIVector<T> & operator =(PIVector<T> && other) { inline PIVector<T> & operator =(PIVector<T> && v) {
swap(other); swap(v);
return *this; return *this;
} }
@@ -221,37 +221,37 @@ public:
} }
return false; return false;
} }
inline bool contains(const T & v) const { inline bool contains(const T & e) const {
for (size_t i = 0; i < piv_size; ++i) { for (size_t i = 0; i < piv_size; ++i) {
if (v == piv_data[i]) { if (e == piv_data[i]) {
return true; return true;
} }
} }
return false; return false;
} }
inline int etries(const T & v, size_t start = 0) const { inline int etries(const T & e, size_t start = 0) const {
int ec = 0; int ec = 0;
for (size_t i = start; i < piv_size; ++i) { for (size_t i = start; i < piv_size; ++i) {
if (v == piv_data[i]) ++ec; if (e == piv_data[i]) ++ec;
} }
return ec; return ec;
} }
inline int etries(std::function<bool(const T &)> test, size_t start = 0) const { inline int etries(std::function<bool(const T & e)> test, size_t start = 0) const {
int ec = 0; int ec = 0;
for (size_t i = start; i < piv_size; ++i) { for (size_t i = start; i < piv_size; ++i) {
if (test(piv_data[i])) ++ec; if (test(piv_data[i])) ++ec;
} }
return ec; return ec;
} }
inline ssize_t indexOf(const T & v, size_t start = 0) const { inline ssize_t indexOf(const T & e, size_t start = 0) const {
for (size_t i = start; i < piv_size; ++i) { for (size_t i = start; i < piv_size; ++i) {
if (v == piv_data[i]) { if (e == piv_data[i]) {
return i; return i;
} }
} }
return -1; return -1;
} }
inline ssize_t indexWhere(std::function<bool(const T &)> test, size_t start = 0) const { inline ssize_t indexWhere(std::function<bool(const T & e)> test, size_t start = 0) const {
for (size_t i = start; i < piv_size; ++i) { for (size_t i = start; i < piv_size; ++i) {
if (test(piv_data[i])) { if (test(piv_data[i])) {
return i; return i;
@@ -259,17 +259,17 @@ public:
} }
return -1; return -1;
} }
inline ssize_t lastIndexOf(const T & v, ssize_t start = -1) const { inline ssize_t lastIndexOf(const T & e, ssize_t start = -1) const {
if (start < 0) start = piv_size - 1; if (start < 0) start = piv_size - 1;
else start = piMin<ssize_t>(piv_size - 1, start); else start = piMin<ssize_t>(piv_size - 1, start);
for (ssize_t i = start; i >= 0; --i) { for (ssize_t i = start; i >= 0; --i) {
if (v == piv_data[i]) { if (e == piv_data[i]) {
return i; return i;
} }
} }
return -1; return -1;
} }
inline ssize_t lastIndexWhere(std::function<bool(const T &)> test, ssize_t start = -1) const { inline ssize_t lastIndexWhere(std::function<bool(const T & e)> test, ssize_t start = -1) const {
if (start < 0) start = piv_size - 1; if (start < 0) start = piv_size - 1;
else start = piMin<ssize_t>(piv_size - 1, start); else start = piMin<ssize_t>(piv_size - 1, start);
for (ssize_t i = start; i >= 0; --i) { for (ssize_t i = start; i >= 0; --i) {
@@ -307,7 +307,7 @@ public:
} }
return *this; return *this;
} }
inline PIVector<T> & fill(std::function<T(size_t)> f) { inline PIVector<T> & fill(std::function<T(size_t i)> f) {
deleteT(piv_data, piv_size); deleteT(piv_data, piv_size);
PIINTROSPECTION_CONTAINER_USED(T, piv_size) PIINTROSPECTION_CONTAINER_USED(T, piv_size)
for (size_t i = 0; i < piv_size; ++i) { for (size_t i = 0; i < piv_size; ++i) {
@@ -347,7 +347,7 @@ public:
} }
return *this; return *this;
} }
inline PIVector<T> & resize(size_t new_size, std::function<T(size_t)> f) { inline PIVector<T> & resize(size_t new_size, std::function<T(size_t i)> f) {
if (new_size < piv_size) { if (new_size < piv_size) {
T * de = &(piv_data[new_size]); T * de = &(piv_data[new_size]);
deleteT(de, piv_size - new_size); deleteT(de, piv_size - new_size);
@@ -388,35 +388,35 @@ public:
return *this; return *this;
} }
inline PIVector<T> & insert(size_t index, const T & v = T()) { inline PIVector<T> & insert(size_t index, const T & e = T()) {
alloc(piv_size + 1); alloc(piv_size + 1);
if (index < piv_size - 1) { if (index < piv_size - 1) {
size_t os = piv_size - index - 1; size_t os = piv_size - index - 1;
memmove((void*)(&(piv_data[index + 1])), (const void*)(&(piv_data[index])), os * sizeof(T)); memmove((void*)(&(piv_data[index + 1])), (const void*)(&(piv_data[index])), os * sizeof(T));
} }
PIINTROSPECTION_CONTAINER_USED(T, 1) PIINTROSPECTION_CONTAINER_USED(T, 1)
elementNew(piv_data + index, v); elementNew(piv_data + index, e);
return *this; return *this;
} }
inline PIVector<T> & insert(size_t index, T && v) { inline PIVector<T> & insert(size_t index, T && e) {
alloc(piv_size + 1); alloc(piv_size + 1);
if (index < piv_size - 1) { if (index < piv_size - 1) {
size_t os = piv_size - index - 1; size_t os = piv_size - index - 1;
memmove((void*)(&(piv_data[index + 1])), (const void*)(&(piv_data[index])), os * sizeof(T)); memmove((void*)(&(piv_data[index + 1])), (const void*)(&(piv_data[index])), os * sizeof(T));
} }
PIINTROSPECTION_CONTAINER_USED(T, 1) PIINTROSPECTION_CONTAINER_USED(T, 1)
elementNew(piv_data + index, std::move(v)); elementNew(piv_data + index, std::move(e));
return *this; return *this;
} }
inline PIVector<T> & insert(size_t index, const PIVector<T> & other) { inline PIVector<T> & insert(size_t index, const PIVector<T> & v) {
if (other.isEmpty()) return *this; if (v.isEmpty()) return *this;
assert(&other != this); assert(&v != this);
ssize_t os = piv_size - index; ssize_t os = piv_size - index;
alloc(piv_size + other.piv_size); alloc(piv_size + v.piv_size);
if (os > 0) { if (os > 0) {
memmove((void*)(&(piv_data[index + other.piv_size])), (const void*)(&(piv_data[index])), os * sizeof(T)); memmove((void*)(&(piv_data[index + v.piv_size])), (const void*)(&(piv_data[index])), os * sizeof(T));
} }
newT(piv_data + index, other.piv_data, other.piv_size); newT(piv_data + index, v.piv_data, v.piv_size);
return *this; return *this;
} }
@@ -433,10 +433,10 @@ public:
return *this; return *this;
} }
inline void swap(PIVector<T> & other) { inline void swap(PIVector<T> & v) {
piSwap<T*>(piv_data, other.piv_data); piSwap<T*>(piv_data, v.piv_data);
piSwap<size_t>(piv_size, other.piv_size); piSwap<size_t>(piv_size, v.piv_size);
piSwap<size_t>(piv_rsize, other.piv_rsize); piSwap<size_t>(piv_rsize, v.piv_rsize);
} }
typedef int (*CompareFunc)(const T * , const T * ); typedef int (*CompareFunc)(const T * , const T * );
@@ -453,9 +453,9 @@ public:
return *this; return *this;
} }
inline PIVector<T> & removeOne(const T & v) { inline PIVector<T> & removeOne(const T & e) {
for (size_t i = 0; i < piv_size; ++i) { for (size_t i = 0; i < piv_size; ++i) {
if (piv_data[i] == v) { if (piv_data[i] == e) {
remove(i); remove(i);
return *this; return *this;
} }
@@ -463,9 +463,9 @@ public:
return *this; return *this;
} }
inline PIVector<T> & removeAll(const T & v) { inline PIVector<T> & removeAll(const T & e) {
for (ssize_t i = 0; i < ssize_t(piv_size); ++i) { for (ssize_t i = 0; i < ssize_t(piv_size); ++i) {
if (piv_data[i] == v) { if (piv_data[i] == e) {
remove(i); remove(i);
--i; --i;
} }
@@ -473,37 +473,37 @@ public:
return *this; return *this;
} }
inline PIVector<T> & push_back(const T & v) { inline PIVector<T> & push_back(const T & e) {
alloc(piv_size + 1); alloc(piv_size + 1);
PIINTROSPECTION_CONTAINER_USED(T, 1); PIINTROSPECTION_CONTAINER_USED(T, 1);
elementNew(piv_data + piv_size - 1, v); elementNew(piv_data + piv_size - 1, e);
return *this; return *this;
} }
inline PIVector<T> & push_back(T && v) { inline PIVector<T> & push_back(T && e) {
alloc(piv_size + 1); alloc(piv_size + 1);
PIINTROSPECTION_CONTAINER_USED(T, 1); PIINTROSPECTION_CONTAINER_USED(T, 1);
elementNew(piv_data + piv_size - 1, std::move(v)); elementNew(piv_data + piv_size - 1, std::move(e));
return *this; return *this;
} }
inline PIVector<T> & append(const T & v) {return push_back(v);} inline PIVector<T> & append(const T & e) {return push_back(e);}
inline PIVector<T> & append(T && v) {return push_back(std::move(v));} inline PIVector<T> & append(T && e) {return push_back(std::move(e));}
inline PIVector<T> & append(const PIVector<T> & other) { inline PIVector<T> & append(const PIVector<T> & v) {
assert(&other != this); assert(&v != this);
size_t ps = piv_size; size_t ps = piv_size;
alloc(piv_size + other.piv_size); alloc(piv_size + v.piv_size);
newT(piv_data + ps, other.piv_data, other.piv_size); newT(piv_data + ps, v.piv_data, v.piv_size);
return *this; return *this;
} }
inline PIVector<T> & operator <<(const T & v) {return push_back(v);} inline PIVector<T> & operator <<(const T & e) {return push_back(e);}
inline PIVector<T> & operator <<(T && v) {return push_back(std::move(v));} inline PIVector<T> & operator <<(T && e) {return push_back(std::move(e));}
inline PIVector<T> & operator <<(const PIVector<T> & other) {return append(other);} inline PIVector<T> & operator <<(const PIVector<T> & v) {return append(v);}
inline PIVector<T> & push_front(const T & v) {insert(0, v); return *this;} inline PIVector<T> & push_front(const T & e) {insert(0, e); return *this;}
inline PIVector<T> & push_front(T && v) {insert(0, std::move(v)); return *this;} inline PIVector<T> & push_front(T && e) {insert(0, std::move(e)); return *this;}
inline PIVector<T> & prepend(const T & v) {return push_front(v);} inline PIVector<T> & prepend(const T & e) {return push_front(e);}
inline PIVector<T> & prepend(T && v) {return push_front(std::move(v));} inline PIVector<T> & prepend(T && e) {return push_front(std::move(e));}
inline PIVector<T> & pop_back() { inline PIVector<T> & pop_back() {
if (piv_size == 0) return *this; if (piv_size == 0) return *this;
@@ -528,33 +528,45 @@ public:
return ret; return ret;
} }
const PIVector<T> & forEach(std::function<void(const T &)> f) const { const PIVector<T> & forEach(std::function<void(const T & e)> f) const {
for (uint i = 0; i < piv_size; ++i) { for (uint i = 0; i < piv_size; ++i) {
f(piv_data[i]); f(piv_data[i]);
} }
return *this; return *this;
} }
PIVector<T> copyForEach(std::function<T(const T &)> f) const { PIVector<T> copyForEach(std::function<T(const T & e)> f) const {
PIVector<T> ret; ret.reserve(piv_size); PIVector<T> ret; ret.reserve(piv_size);
for (uint i = 0; i < piv_size; ++i) { for (uint i = 0; i < piv_size; ++i) {
ret << f(piv_data[i]); ret << f(piv_data[i]);
} }
return ret; return ret;
} }
PIVector<T> & forEachInplace(std::function<T(const T &)> f) { PIVector<T> & forEachInplace(std::function<T(const T & e)> f) {
for (uint i = 0; i < piv_size; ++i) { for (uint i = 0; i < piv_size; ++i) {
piv_data[i] = f(piv_data[i]); piv_data[i] = f(piv_data[i]);
} }
return *this; return *this;
} }
template <typename ST> template <typename ST>
PIVector<ST> toType(std::function<ST(const T &)> f) const { PIVector<ST> map(std::function<ST(const T & e)> f) const {
PIVector<ST> ret; ret.reserve(piv_size); PIVector<ST> ret; ret.reserve(piv_size);
for (uint i = 0; i < piv_size; ++i) { for (uint i = 0; i < piv_size; ++i) {
ret << f(piv_data[i]); ret << f(piv_data[i]);
} }
return ret; return ret;
} }
template <typename ST>
PIVector<ST> toType(std::function<ST(const T & e)> f) const {return map(f);}
template <typename ST>
ST reduce(std::function<ST(const T & e, const ST & acc)> f, const ST & initial = ST()) const {
ST ret(initial);
for (uint i = 0; i < piv_size; ++i) {
ret = f(piv_data[i], ret);
}
return ret;
}
inline PIVector<PIVector<T>> reshape(size_t rows, size_t cols, int order = byRow) const { inline PIVector<PIVector<T>> reshape(size_t rows, size_t cols, int order = byRow) const {
PIVector<PIVector<T>> ret; PIVector<PIVector<T>> ret;

View File

@@ -2,16 +2,22 @@
int main(int argc, char * argv[]) { int main(int argc, char * argv[]) {
PIVector<int> x(20, [](int i) {return i/2+1;});
PIVector<int> x(20, [](int i) {return i;});
piCout << x; piCout << x;
piCout << x.any([](int v) {return v == 20;}); piCout << x.any([](int v) {return v == 10;});
piCout << x.every([](int v) {return v > 0;}); piCout << x.every([](int v) {return v > 0;});
piCout << x.etries([](int v) {return v % 5 == 0;}); piCout << x.etries([](int v) {return v % 5 == 0;});
piCout << x.indexWhere([](int v) {return v % 8 == 0;}); piCout << x.indexWhere([](int v) {return v % 8 == 0;});
piCout << x.indexOf(4, -1); piCout << x.indexOf(4, -1);
piCout << x.lastIndexOf(1, 0); piCout << x.lastIndexOf(1, 0);
piCout << x.lastIndexWhere([](int v) {return v % 8 == 0;}); piCout << x.lastIndexWhere([](int v) {return v % 8 == 0;});
return 0; PIVector<double> x2 = x.map<double>([](int v) {return v / 10;});
piCout << x2;
piCout << x.reduce<PIString>([](int v, PIString s){return s + PIString::fromNumber(v);});
return 0; // TODO:
PIByteArray rnd; PIByteArray rnd;
rnd.resize(1024*1024, 'x'); rnd.resize(1024*1024, 'x');
PICLI cli(argc, argv); PICLI cli(argc, argv);