diff --git a/libs/main/containers/pivector.h b/libs/main/containers/pivector.h index dd8c213d..97f99453 100644 --- a/libs/main/containers/pivector.h +++ b/libs/main/containers/pivector.h @@ -39,10 +39,10 @@ public: alloc(size); newT(piv_data, data, piv_size); } - inline PIVector(const PIVector & other): piv_data(0), piv_size(0), piv_rsize(0) { + inline PIVector(const PIVector & v): piv_data(0), piv_size(0), piv_rsize(0) { PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T)) - alloc(other.piv_size); - newT(piv_data, other.piv_data, piv_size); + alloc(v.piv_size); + newT(piv_data, v.piv_data, piv_size); } inline PIVector(std::initializer_list init_list): piv_data(0), piv_size(0), piv_rsize(0) { PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T)) @@ -53,13 +53,13 @@ public: PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T)) resize(piv_size, f); } - inline PIVector(size_t piv_size, std::function f): piv_data(0), piv_size(0), piv_rsize(0) { + inline PIVector(size_t piv_size, std::function f): piv_data(0), piv_size(0), piv_rsize(0) { PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T)) resize(piv_size, f); } - inline PIVector(PIVector && other): piv_data(other.piv_data), piv_size(other.piv_size), piv_rsize(other.piv_rsize) { + inline PIVector(PIVector && v): piv_data(v.piv_data), piv_size(v.piv_size), piv_rsize(v.piv_rsize) { PIINTROSPECTION_CONTAINER_NEW(T, sizeof(T)) - other._reset(); + v._reset(); } inline virtual ~PIVector() { PIINTROSPECTION_CONTAINER_DELETE(T) @@ -69,17 +69,17 @@ public: _reset(); } - inline PIVector & operator =(const PIVector & other) { - if (this == &other) return *this; + inline PIVector & operator =(const PIVector & v) { + if (this == &v) return *this; clear(); deleteT(piv_data, piv_size); - alloc(other.piv_size); - newT(piv_data, other.piv_data, piv_size); + alloc(v.piv_size); + newT(piv_data, v.piv_data, piv_size); return *this; } - inline PIVector & operator =(PIVector && other) { - swap(other); + inline PIVector & operator =(PIVector && v) { + swap(v); return *this; } @@ -221,37 +221,37 @@ public: } 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) { - if (v == piv_data[i]) { + if (e == piv_data[i]) { return true; } } 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; for (size_t i = start; i < piv_size; ++i) { - if (v == piv_data[i]) ++ec; + if (e == piv_data[i]) ++ec; } return ec; } - inline int etries(std::function test, size_t start = 0) const { + inline int etries(std::function test, size_t start = 0) const { int ec = 0; for (size_t i = start; i < piv_size; ++i) { if (test(piv_data[i])) ++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) { - if (v == piv_data[i]) { + if (e == piv_data[i]) { return i; } } return -1; } - inline ssize_t indexWhere(std::function test, size_t start = 0) const { + inline ssize_t indexWhere(std::function test, size_t start = 0) const { for (size_t i = start; i < piv_size; ++i) { if (test(piv_data[i])) { return i; @@ -259,17 +259,17 @@ public: } 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; else start = piMin(piv_size - 1, start); for (ssize_t i = start; i >= 0; --i) { - if (v == piv_data[i]) { + if (e == piv_data[i]) { return i; } } return -1; } - inline ssize_t lastIndexWhere(std::function test, ssize_t start = -1) const { + inline ssize_t lastIndexWhere(std::function test, ssize_t start = -1) const { if (start < 0) start = piv_size - 1; else start = piMin(piv_size - 1, start); for (ssize_t i = start; i >= 0; --i) { @@ -307,7 +307,7 @@ public: } return *this; } - inline PIVector & fill(std::function f) { + inline PIVector & fill(std::function f) { deleteT(piv_data, piv_size); PIINTROSPECTION_CONTAINER_USED(T, piv_size) for (size_t i = 0; i < piv_size; ++i) { @@ -347,7 +347,7 @@ public: } return *this; } - inline PIVector & resize(size_t new_size, std::function f) { + inline PIVector & resize(size_t new_size, std::function f) { if (new_size < piv_size) { T * de = &(piv_data[new_size]); deleteT(de, piv_size - new_size); @@ -388,35 +388,35 @@ public: return *this; } - inline PIVector & insert(size_t index, const T & v = T()) { + inline PIVector & insert(size_t index, const T & e = T()) { alloc(piv_size + 1); if (index < piv_size - 1) { size_t os = piv_size - index - 1; memmove((void*)(&(piv_data[index + 1])), (const void*)(&(piv_data[index])), os * sizeof(T)); } PIINTROSPECTION_CONTAINER_USED(T, 1) - elementNew(piv_data + index, v); + elementNew(piv_data + index, e); return *this; } - inline PIVector & insert(size_t index, T && v) { + inline PIVector & insert(size_t index, T && e) { alloc(piv_size + 1); if (index < piv_size - 1) { size_t os = piv_size - index - 1; memmove((void*)(&(piv_data[index + 1])), (const void*)(&(piv_data[index])), os * sizeof(T)); } PIINTROSPECTION_CONTAINER_USED(T, 1) - elementNew(piv_data + index, std::move(v)); + elementNew(piv_data + index, std::move(e)); return *this; } - inline PIVector & insert(size_t index, const PIVector & other) { - if (other.isEmpty()) return *this; - assert(&other != this); + inline PIVector & insert(size_t index, const PIVector & v) { + if (v.isEmpty()) return *this; + assert(&v != this); ssize_t os = piv_size - index; - alloc(piv_size + other.piv_size); + alloc(piv_size + v.piv_size); 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; } @@ -433,10 +433,10 @@ public: return *this; } - inline void swap(PIVector & other) { - piSwap(piv_data, other.piv_data); - piSwap(piv_size, other.piv_size); - piSwap(piv_rsize, other.piv_rsize); + inline void swap(PIVector & v) { + piSwap(piv_data, v.piv_data); + piSwap(piv_size, v.piv_size); + piSwap(piv_rsize, v.piv_rsize); } typedef int (*CompareFunc)(const T * , const T * ); @@ -453,9 +453,9 @@ public: return *this; } - inline PIVector & removeOne(const T & v) { + inline PIVector & removeOne(const T & e) { for (size_t i = 0; i < piv_size; ++i) { - if (piv_data[i] == v) { + if (piv_data[i] == e) { remove(i); return *this; } @@ -463,9 +463,9 @@ public: return *this; } - inline PIVector & removeAll(const T & v) { + inline PIVector & removeAll(const T & e) { for (ssize_t i = 0; i < ssize_t(piv_size); ++i) { - if (piv_data[i] == v) { + if (piv_data[i] == e) { remove(i); --i; } @@ -473,37 +473,37 @@ public: return *this; } - inline PIVector & push_back(const T & v) { + inline PIVector & push_back(const T & e) { alloc(piv_size + 1); PIINTROSPECTION_CONTAINER_USED(T, 1); - elementNew(piv_data + piv_size - 1, v); + elementNew(piv_data + piv_size - 1, e); return *this; } - inline PIVector & push_back(T && v) { + inline PIVector & push_back(T && e) { alloc(piv_size + 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; } - inline PIVector & append(const T & v) {return push_back(v);} - inline PIVector & append(T && v) {return push_back(std::move(v));} - inline PIVector & append(const PIVector & other) { - assert(&other != this); + inline PIVector & append(const T & e) {return push_back(e);} + inline PIVector & append(T && e) {return push_back(std::move(e));} + inline PIVector & append(const PIVector & v) { + assert(&v != this); size_t ps = piv_size; - alloc(piv_size + other.piv_size); - newT(piv_data + ps, other.piv_data, other.piv_size); + alloc(piv_size + v.piv_size); + newT(piv_data + ps, v.piv_data, v.piv_size); return *this; } - inline PIVector & operator <<(const T & v) {return push_back(v);} - inline PIVector & operator <<(T && v) {return push_back(std::move(v));} - inline PIVector & operator <<(const PIVector & other) {return append(other);} + inline PIVector & operator <<(const T & e) {return push_back(e);} + inline PIVector & operator <<(T && e) {return push_back(std::move(e));} + inline PIVector & operator <<(const PIVector & v) {return append(v);} - 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 & push_front(const T & e) {insert(0, e); return *this;} + inline PIVector & push_front(T && e) {insert(0, std::move(e)); return *this;} + inline PIVector & prepend(const T & e) {return push_front(e);} + inline PIVector & prepend(T && e) {return push_front(std::move(e));} inline PIVector & pop_back() { if (piv_size == 0) return *this; @@ -528,33 +528,45 @@ public: return ret; } - const PIVector & forEach(std::function f) const { + const PIVector & forEach(std::function f) const { for (uint i = 0; i < piv_size; ++i) { f(piv_data[i]); } return *this; } - PIVector copyForEach(std::function f) const { + PIVector copyForEach(std::function f) const { PIVector ret; ret.reserve(piv_size); for (uint i = 0; i < piv_size; ++i) { ret << f(piv_data[i]); } return ret; } - PIVector & forEachInplace(std::function f) { + PIVector & forEachInplace(std::function f) { for (uint i = 0; i < piv_size; ++i) { piv_data[i] = f(piv_data[i]); } return *this; } + template - PIVector toType(std::function f) const { + PIVector map(std::function f) const { PIVector ret; ret.reserve(piv_size); for (uint i = 0; i < piv_size; ++i) { ret << f(piv_data[i]); } return ret; } + template + PIVector toType(std::function f) const {return map(f);} + + template + ST reduce(std::function 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> reshape(size_t rows, size_t cols, int order = byRow) const { PIVector> ret; diff --git a/main.cpp b/main.cpp index d470a6b0..8cf6599e 100644 --- a/main.cpp +++ b/main.cpp @@ -2,16 +2,22 @@ int main(int argc, char * argv[]) { - PIVector x(20, [](int i) {return i/2+1;}); + + PIVector x(20, [](int i) {return i;}); 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.etries([](int v) {return v % 5 == 0;}); piCout << x.indexWhere([](int v) {return v % 8 == 0;}); piCout << x.indexOf(4, -1); piCout << x.lastIndexOf(1, 0); piCout << x.lastIndexWhere([](int v) {return v % 8 == 0;}); - return 0; + PIVector x2 = x.map([](int v) {return v / 10;}); + piCout << x2; + piCout << x.reduce([](int v, PIString s){return s + PIString::fromNumber(v);}); + + return 0; // TODO: + PIByteArray rnd; rnd.resize(1024*1024, 'x'); PICLI cli(argc, argv);