PIVector: map, reduce
rename arguments in uniform style
This commit is contained in:
@@ -39,10 +39,10 @@ public:
|
||||
alloc(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))
|
||||
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<T> 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<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))
|
||||
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))
|
||||
other._reset();
|
||||
v._reset();
|
||||
}
|
||||
inline virtual ~PIVector() {
|
||||
PIINTROSPECTION_CONTAINER_DELETE(T)
|
||||
@@ -69,17 +69,17 @@ public:
|
||||
_reset();
|
||||
}
|
||||
|
||||
inline PIVector<T> & operator =(const PIVector<T> & other) {
|
||||
if (this == &other) return *this;
|
||||
inline PIVector<T> & operator =(const PIVector<T> & 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<T> & operator =(PIVector<T> && other) {
|
||||
swap(other);
|
||||
inline PIVector<T> & operator =(PIVector<T> && 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<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;
|
||||
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<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) {
|
||||
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<ssize_t>(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<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;
|
||||
else start = piMin<ssize_t>(piv_size - 1, start);
|
||||
for (ssize_t i = start; i >= 0; --i) {
|
||||
@@ -307,7 +307,7 @@ public:
|
||||
}
|
||||
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);
|
||||
PIINTROSPECTION_CONTAINER_USED(T, piv_size)
|
||||
for (size_t i = 0; i < piv_size; ++i) {
|
||||
@@ -347,7 +347,7 @@ public:
|
||||
}
|
||||
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) {
|
||||
T * de = &(piv_data[new_size]);
|
||||
deleteT(de, piv_size - new_size);
|
||||
@@ -388,35 +388,35 @@ public:
|
||||
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);
|
||||
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<T> & insert(size_t index, T && v) {
|
||||
inline PIVector<T> & 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<T> & insert(size_t index, const PIVector<T> & other) {
|
||||
if (other.isEmpty()) return *this;
|
||||
assert(&other != this);
|
||||
inline PIVector<T> & insert(size_t index, const PIVector<T> & 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<T> & other) {
|
||||
piSwap<T*>(piv_data, other.piv_data);
|
||||
piSwap<size_t>(piv_size, other.piv_size);
|
||||
piSwap<size_t>(piv_rsize, other.piv_rsize);
|
||||
inline void swap(PIVector<T> & v) {
|
||||
piSwap<T*>(piv_data, v.piv_data);
|
||||
piSwap<size_t>(piv_size, v.piv_size);
|
||||
piSwap<size_t>(piv_rsize, v.piv_rsize);
|
||||
}
|
||||
|
||||
typedef int (*CompareFunc)(const T * , const T * );
|
||||
@@ -453,9 +453,9 @@ public:
|
||||
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) {
|
||||
if (piv_data[i] == v) {
|
||||
if (piv_data[i] == e) {
|
||||
remove(i);
|
||||
return *this;
|
||||
}
|
||||
@@ -463,9 +463,9 @@ public:
|
||||
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) {
|
||||
if (piv_data[i] == v) {
|
||||
if (piv_data[i] == e) {
|
||||
remove(i);
|
||||
--i;
|
||||
}
|
||||
@@ -473,37 +473,37 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline PIVector<T> & push_back(const T & v) {
|
||||
inline PIVector<T> & 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<T> & push_back(T && v) {
|
||||
inline PIVector<T> & 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<T> & append(const T & v) {return push_back(v);}
|
||||
inline PIVector<T> & append(T && v) {return push_back(std::move(v));}
|
||||
inline PIVector<T> & append(const PIVector<T> & other) {
|
||||
assert(&other != this);
|
||||
inline PIVector<T> & append(const T & e) {return push_back(e);}
|
||||
inline PIVector<T> & append(T && e) {return push_back(std::move(e));}
|
||||
inline PIVector<T> & append(const PIVector<T> & 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<T> & operator <<(const T & v) {return push_back(v);}
|
||||
inline PIVector<T> & operator <<(T && v) {return push_back(std::move(v));}
|
||||
inline PIVector<T> & operator <<(const PIVector<T> & other) {return append(other);}
|
||||
inline PIVector<T> & operator <<(const T & e) {return push_back(e);}
|
||||
inline PIVector<T> & operator <<(T && e) {return push_back(std::move(e));}
|
||||
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(T && v) {insert(0, std::move(v)); return *this;}
|
||||
inline PIVector<T> & prepend(const T & v) {return push_front(v);}
|
||||
inline PIVector<T> & prepend(T && v) {return push_front(std::move(v));}
|
||||
inline PIVector<T> & push_front(const T & e) {insert(0, e); return *this;}
|
||||
inline PIVector<T> & push_front(T && e) {insert(0, std::move(e)); return *this;}
|
||||
inline PIVector<T> & prepend(const T & e) {return push_front(e);}
|
||||
inline PIVector<T> & prepend(T && e) {return push_front(std::move(e));}
|
||||
|
||||
inline PIVector<T> & pop_back() {
|
||||
if (piv_size == 0) return *this;
|
||||
@@ -528,33 +528,45 @@ public:
|
||||
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) {
|
||||
f(piv_data[i]);
|
||||
}
|
||||
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);
|
||||
for (uint i = 0; i < piv_size; ++i) {
|
||||
ret << f(piv_data[i]);
|
||||
}
|
||||
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) {
|
||||
piv_data[i] = f(piv_data[i]);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
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);
|
||||
for (uint i = 0; i < piv_size; ++i) {
|
||||
ret << f(piv_data[i]);
|
||||
}
|
||||
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 {
|
||||
PIVector<PIVector<T>> ret;
|
||||
|
||||
Reference in New Issue
Block a user