code brush

This commit is contained in:
2021-09-07 15:39:44 +03:00
parent 3b0a1c70fe
commit 76ed60edf3
2 changed files with 138 additions and 64 deletions

View File

@@ -174,6 +174,7 @@ public:
inline size_t capacity() const {return pid_rsize;} inline size_t capacity() const {return pid_rsize;}
inline size_t _start() const {return pid_start;} inline size_t _start() const {return pid_start;}
inline bool isEmpty() const {return (pid_size == 0);} inline bool isEmpty() const {return (pid_size == 0);}
inline bool isNotEmpty() const {return (pid_size > 0);}
inline T & operator [](size_t index) {return pid_data[pid_start + index];} inline T & operator [](size_t index) {return pid_data[pid_start + index];}
inline const T & operator [](size_t index) const {return pid_data[pid_start + index];} inline const T & operator [](size_t index) const {return pid_data[pid_start + index];}
@@ -184,34 +185,41 @@ public:
inline const T & front() const {return pid_data[pid_start];} inline const T & front() const {return pid_data[pid_start];}
inline bool operator ==(const PIDeque<T> & t) const { inline bool operator ==(const PIDeque<T> & t) const {
if (pid_size != t.pid_size) return false; if (pid_size != t.pid_size) return false;
for (size_t i = 0; i < pid_size; ++i) for (size_t i = 0; i < pid_size; ++i) {
if (t[i] != (*this)[i]) if (t[i] != (*this)[i]) {
return false; return false;
}
}
return true; return true;
} }
inline bool operator !=(const PIDeque<T> & t) const {return !(*this == t);} inline bool operator !=(const PIDeque<T> & t) const {return !(*this == t);}
inline bool operator <(const PIDeque<T> & t) const { inline bool operator <(const PIDeque<T> & t) const {
if (pid_size != t.pid_size) return pid_size < t.pid_size; if (pid_size != t.pid_size) return pid_size < t.pid_size;
for (size_t i = 0; i < pid_size; ++i) for (size_t i = 0; i < pid_size; ++i) {
if ((*this)[i] != t[i]) return (*this)[i] < t[i]; if ((*this)[i] != t[i]) return (*this)[i] < t[i];
}
return false; return false;
} }
inline bool operator >(const PIDeque<T> & t) const { inline bool operator >(const PIDeque<T> & t) const {
if (pid_size != t.pid_size) return pid_size > t.pid_size; if (pid_size != t.pid_size) return pid_size > t.pid_size;
for (size_t i = 0; i < pid_size; ++i) for (size_t i = 0; i < pid_size; ++i) {
if ((*this)[i] != t[i]) return (*this)[i] > t[i]; if ((*this)[i] != t[i]) return (*this)[i] > t[i];
}
return false; return false;
} }
inline bool contains(const T & v) const { inline bool contains(const T & v) const {
for (size_t i = pid_start; i < pid_start + pid_size; ++i) for (size_t i = pid_start; i < pid_start + pid_size; ++i) {
if (v == pid_data[i]) if (v == pid_data[i]) {
return true; return true;
}
}
return false; return false;
} }
inline int etries(const T & v) const { inline int etries(const T & v) const {
int ec = 0; int ec = 0;
for (size_t i = pid_start; i < pid_start + pid_size; ++i) for (size_t i = pid_start; i < pid_start + pid_size; ++i) {
if (v == pid_data[i]) ++ec; if (v == pid_data[i]) ++ec;
}
return ec; return ec;
} }
inline ssize_t indexOf(const T & v) const { inline ssize_t indexOf(const T & v) const {
@@ -221,9 +229,11 @@ public:
return -1; return -1;
} }
inline ssize_t lastIndexOf(const T & v) const { inline ssize_t lastIndexOf(const T & v) const {
for (ssize_t i = pid_start + (ssize_t)pid_size - 1; i >= pid_start; --i) for (ssize_t i = pid_start + (ssize_t)pid_size - 1; i >= pid_start; --i) {
if (v == pid_data[i]) if (v == pid_data[i]) {
return i - pid_start; return i - pid_start;
}
}
return -1; return -1;
} }
@@ -250,15 +260,17 @@ public:
inline PIDeque<T> & fill(const T & f = T()) { inline PIDeque<T> & fill(const T & f = T()) {
deleteT(pid_data + pid_start, pid_size); deleteT(pid_data + pid_start, pid_size);
PIINTROSPECTION_CONTAINER_USED(T, pid_size) PIINTROSPECTION_CONTAINER_USED(T, pid_size)
for (size_t i = pid_start; i < pid_start + pid_size; ++i) for (size_t i = pid_start; i < pid_start + pid_size; ++i) {
elementNew(pid_data + i, f); elementNew(pid_data + i, f);
}
return *this; return *this;
} }
inline PIDeque<T> & fill(std::function<T(size_t)> f) { inline PIDeque<T> & fill(std::function<T(size_t)> f) {
deleteT(pid_data + pid_start, pid_size); deleteT(pid_data + pid_start, pid_size);
PIINTROSPECTION_CONTAINER_USED(T, pid_size) PIINTROSPECTION_CONTAINER_USED(T, pid_size)
for (size_t i = pid_start; i < pid_start + pid_size; ++i) for (size_t i = pid_start; i < pid_start + pid_size; ++i) {
elementNew(pid_data + i, f(i)); elementNew(pid_data + i, f(i));
}
return *this; return *this;
} }
inline PIDeque<T> & assign(const T & f = T()) {return fill(f);} inline PIDeque<T> & assign(const T & f = T()) {return fill(f);}
@@ -281,32 +293,36 @@ public:
if (new_size < pid_size) { if (new_size < pid_size) {
deleteT(&(pid_data[new_size + pid_start]), pid_size - new_size); deleteT(&(pid_data[new_size + pid_start]), pid_size - new_size);
pid_size = new_size; pid_size = new_size;
if (new_size == 0) if (new_size == 0) {
pid_start = (pid_rsize - pid_size) / 2; pid_start = (pid_rsize - pid_size) / 2;
} }
}
if (new_size > pid_size) { if (new_size > pid_size) {
size_t os = pid_size; size_t os = pid_size;
alloc(new_size, true); alloc(new_size, true);
PIINTROSPECTION_CONTAINER_USED(T, (new_size-os)) PIINTROSPECTION_CONTAINER_USED(T, (new_size-os))
for (size_t i = os + pid_start; i < new_size + pid_start; ++i) for (size_t i = os + pid_start; i < new_size + pid_start; ++i) {
elementNew(pid_data + i, f); elementNew(pid_data + i, f);
} }
}
return *this; return *this;
} }
inline PIDeque<T> & resize(size_t new_size, std::function<T(size_t)> f) { inline PIDeque<T> & resize(size_t new_size, std::function<T(size_t)> f) {
if (new_size < pid_size) { if (new_size < pid_size) {
deleteT(&(pid_data[new_size + pid_start]), pid_size - new_size); deleteT(&(pid_data[new_size + pid_start]), pid_size - new_size);
pid_size = new_size; pid_size = new_size;
if (new_size == 0) if (new_size == 0) {
pid_start = (pid_rsize - pid_size) / 2; pid_start = (pid_rsize - pid_size) / 2;
} }
}
if (new_size > pid_size) { if (new_size > pid_size) {
size_t os = pid_size; size_t os = pid_size;
alloc(new_size, true); alloc(new_size, true);
PIINTROSPECTION_CONTAINER_USED(T, (new_size-os)) PIINTROSPECTION_CONTAINER_USED(T, (new_size-os))
for (size_t i = os + pid_start; i < new_size + pid_start; ++i) for (size_t i = os + pid_start; i < new_size + pid_start; ++i) {
elementNew(pid_data + i, f(i)); elementNew(pid_data + i, f(i));
} }
}
return *this; return *this;
} }
@@ -344,9 +360,10 @@ public:
} }
} else { } else {
alloc(pid_size + 1, false, -1); alloc(pid_size + 1, false, -1);
if (index > 0) if (index > 0) {
memmove((void*)(&(pid_data[pid_start])), (const void*)(&(pid_data[pid_start + 1])), index * sizeof(T)); memmove((void*)(&(pid_data[pid_start])), (const void*)(&(pid_data[pid_start + 1])), index * sizeof(T));
} }
}
elementNew(pid_data + pid_start + index, v); elementNew(pid_data + pid_start + index, v);
return *this; return *this;
} }
@@ -362,9 +379,10 @@ public:
} }
} else { } else {
alloc(pid_size + 1, false, -1); alloc(pid_size + 1, false, -1);
if (index > 0) if (index > 0) {
memmove((void*)(&(pid_data[pid_start])), (const void*)(&(pid_data[pid_start + 1])), index * sizeof(T)); memmove((void*)(&(pid_data[pid_start])), (const void*)(&(pid_data[pid_start + 1])), index * sizeof(T));
} }
}
elementNew(pid_data + pid_start + index, std::move(v)); elementNew(pid_data + pid_start + index, std::move(v));
return *this; return *this;
} }
@@ -375,13 +393,15 @@ public:
if (dir) { if (dir) {
ssize_t os = pid_size - index; ssize_t os = pid_size - index;
alloc(pid_size + other.pid_size, true); alloc(pid_size + other.pid_size, true);
if (os > 0) if (os > 0) {
memmove((void*)(&(pid_data[index + pid_start + other.pid_size])), (const void*)(&(pid_data[index + pid_start])), os * sizeof(T)); memmove((void*)(&(pid_data[index + pid_start + other.pid_size])), (const void*)(&(pid_data[index + pid_start])), os * sizeof(T));
}
} else { } else {
alloc(pid_size + other.pid_size, false, -other.pid_size); alloc(pid_size + other.pid_size, false, -other.pid_size);
if (index > 0) if (index > 0) {
memmove((void*)(&(pid_data[pid_start])), (const void*)(&(pid_data[pid_start + other.pid_size])), index * sizeof(T)); memmove((void*)(&(pid_data[pid_start])), (const void*)(&(pid_data[pid_start + other.pid_size])), index * sizeof(T));
} }
}
newT(pid_data + pid_start + index, other.pid_data + other.pid_start, other.pid_size); newT(pid_data + pid_start + index, other.pid_data + other.pid_start, other.pid_size);
return *this; return *this;
} }
@@ -395,9 +415,13 @@ public:
size_t os = pid_size - index - count; size_t os = pid_size - index - count;
deleteT(&(pid_data[index + pid_start]), count); deleteT(&(pid_data[index + pid_start]), count);
if (os <= index) { if (os <= index) {
if (os > 0) memmove((void*)(&(pid_data[index + pid_start])), (const void*)(&(pid_data[index + pid_start + count])), os * sizeof(T)); if (os > 0) {
memmove((void*)(&(pid_data[index + pid_start])), (const void*)(&(pid_data[index + pid_start + count])), os * sizeof(T));
}
} else { } else {
if (index > 0) memmove((void*)(&(pid_data[pid_start + count])), (const void*)(&(pid_data[pid_start])), index * sizeof(T)); if (index > 0) {
memmove((void*)(&(pid_data[pid_start + count])), (const void*)(&(pid_data[pid_start])), index * sizeof(T));
}
pid_start += count; pid_start += count;
} }
pid_size -= count; pid_size -= count;
@@ -426,19 +450,21 @@ public:
} }
inline PIDeque<T> & removeOne(const T & v) { inline PIDeque<T> & removeOne(const T & v) {
for (size_t i = 0; i < pid_size; ++i) for (size_t i = 0; i < pid_size; ++i) {
if (pid_data[i + pid_start] == v) { if (pid_data[i + pid_start] == v) {
remove(i); remove(i);
return *this; return *this;
} }
}
return *this; return *this;
} }
inline PIDeque<T> & removeAll(const T & v) { inline PIDeque<T> & removeAll(const T & v) {
for (ssize_t i = 0; i < ssize_t(pid_size); ++i) for (ssize_t i = 0; i < ssize_t(pid_size); ++i) {
if (pid_data[i + pid_start] == v) { if (pid_data[i + pid_start] == v) {
remove(i); remove(i);
--i; --i;
} }
}
return *this; return *this;
} }
@@ -481,20 +507,23 @@ public:
template <typename ST> template <typename ST>
PIDeque<ST> toType() const { PIDeque<ST> toType() const {
PIDeque<ST> ret(pid_size); PIDeque<ST> ret(pid_size);
for (uint i = 0; i < pid_size; ++i) for (uint i = 0; i < pid_size; ++i) {
ret[i] = ST(pid_data[i + pid_start]); ret[i] = ST(pid_data[i + pid_start]);
}
return ret; return ret;
} }
const PIDeque<T> & forEach(std::function<void(const T &)> f) const { const PIDeque<T> & forEach(std::function<void(const T &)> f) const {
for (uint i = 0; i < pid_size; ++i) for (uint i = 0; i < pid_size; ++i) {
f(pid_data[i + pid_start]); f(pid_data[i + pid_start]);
}
return *this; return *this;
} }
PIDeque<T> copyForEach(std::function<T(const T &)> f) const { PIDeque<T> copyForEach(std::function<T(const T &)> f) const {
PIDeque<T> ret; ret.reserve(pid_size); PIDeque<T> ret; ret.reserve(pid_size);
for (uint i = 0; i < pid_size; ++i) for (uint i = 0; i < pid_size; ++i) {
ret << f(pid_data[i + pid_start]); ret << f(pid_data[i + pid_start]);
}
return ret; return ret;
} }
PIDeque<T> & forEachInplace(std::function<T(const T &)> f) { PIDeque<T> & forEachInplace(std::function<T(const T &)> f) {
@@ -505,8 +534,9 @@ public:
template <typename ST> template <typename ST>
PIDeque<ST> toType(std::function<ST(const T &)> f) const { PIDeque<ST> toType(std::function<ST(const T &)> f) const {
PIDeque<ST> ret; ret.reserve(pid_size); PIDeque<ST> ret; ret.reserve(pid_size);
for (uint i = 0; i < pid_size; ++i) for (uint i = 0; i < pid_size; ++i) {
ret << f(pid_data[i + pid_start]); ret << f(pid_data[i + pid_start]);
}
return ret; return ret;
} }
@@ -516,16 +546,18 @@ public:
assert(rows*cols == pid_size); assert(rows*cols == pid_size);
ret.resize(rows); ret.resize(rows);
if (order == byRow) { if (order == byRow) {
for (size_t r = 0; r < rows; r++) for (size_t r = 0; r < rows; r++) {
ret[r] = PIDeque<T>(&(pid_data[r*cols]), cols); ret[r] = PIDeque<T>(&(pid_data[r*cols]), cols);
} }
}
if (order == byColumn) { if (order == byColumn) {
for (size_t r = 0; r < rows; r++) { for (size_t r = 0; r < rows; r++) {
ret[r].resize(cols); ret[r].resize(cols);
for (size_t c = 0; c < cols; c++) for (size_t c = 0; c < cols; c++) {
ret[r][c] = pid_data[c*rows + r]; ret[r][c] = pid_data[c*rows + r];
} }
} }
}
return ret; return ret;
} }
@@ -539,14 +571,17 @@ public:
size_t cols = at(0).size(); size_t cols = at(0).size();
ret.reserve(rows * cols); ret.reserve(rows * cols);
if (order == byRow) { if (order == byRow) {
for (size_t r = 0; r < rows; r++) for (size_t r = 0; r < rows; r++) {
ret.append(at(r)); ret.append(at(r));
} }
}
if (order == byColumn) { if (order == byColumn) {
for (size_t c = 0; c < cols; c++) for (size_t c = 0; c < cols; c++) {
for (size_t r = 0; r < rows; r++) for (size_t r = 0; r < rows; r++) {
ret << at(r)[c]; ret << at(r)[c];
} }
}
}
ret.resize(rows * cols); ret.resize(rows * cols);
return ret; return ret;
} }
@@ -555,11 +590,13 @@ private:
inline void _reset() {pid_size = pid_rsize = pid_start = 0; pid_data = 0;} inline void _reset() {pid_size = pid_rsize = pid_start = 0; pid_data = 0;}
inline size_t asize(ssize_t s) { inline size_t asize(ssize_t s) {
if (s <= 0) return 0; if (s <= 0) return 0;
if (pid_rsize + pid_rsize >= size_t(s) && pid_rsize < size_t(s)) if (pid_rsize + pid_rsize >= size_t(s) && pid_rsize < size_t(s)) {
return pid_rsize + pid_rsize; return pid_rsize + pid_rsize;
}
ssize_t t = 0, s_ = s - 1; ssize_t t = 0, s_ = s - 1;
while (s_ >> t) while (s_ >> t) {
++t; ++t;
}
return (1 << t); return (1 << t);
} }
template<typename T1 = T, typename std::enable_if< template<typename T1 = T, typename std::enable_if<
@@ -583,10 +620,11 @@ private:
inline void deleteT(T * d, size_t sz) { inline void deleteT(T * d, size_t sz) {
PIINTROSPECTION_CONTAINER_UNUSED(T, sz) PIINTROSPECTION_CONTAINER_UNUSED(T, sz)
if ((uchar*)d != 0) { if ((uchar*)d != 0) {
for (size_t i = 0; i < sz; ++i) for (size_t i = 0; i < sz; ++i) {
elementDelete(d[i]); elementDelete(d[i]);
} }
} }
}
template<typename T1 = T, typename std::enable_if< template<typename T1 = T, typename std::enable_if<
std::is_trivially_copyable<T1>::value std::is_trivially_copyable<T1>::value
, int>::type = 0> , int>::type = 0>
@@ -658,10 +696,11 @@ private:
} }
} else { } else {
size_t as; size_t as;
if (pid_start + start_offset < 0) if (pid_start + start_offset < 0) {
as = asize(pid_rsize - start_offset); as = asize(pid_rsize - start_offset);
else as = pid_rsize; } else {
as = pid_rsize;
}
if (as > pid_rsize) { if (as > pid_rsize) {
T * td = (T*)(malloc(as * sizeof(T))); T * td = (T*)(malloc(as * sizeof(T)));
ssize_t ns = pid_start + as - pid_rsize; ssize_t ns = pid_start + as - pid_rsize;
@@ -688,7 +727,15 @@ private:
#ifdef PIP_STD_IOSTREAM #ifdef PIP_STD_IOSTREAM
template<typename T> template<typename T>
inline std::ostream & operator <<(std::ostream & s, const PIDeque<T> & v) {s << "{"; for (size_t i = 0; i < v.size(); ++i) {s << v[i]; if (i < v.size() - 1) s << ", ";} s << "}"; return s;} inline std::ostream & operator <<(std::ostream & s, const PIDeque<T> & v) {
s << "{";
for (size_t i = 0; i < v.size(); ++i) {
s << v[i];
if (i < v.size() - 1) s << ", ";
}
s << "}";
return s;
}
#endif #endif
template<typename T> template<typename T>
@@ -698,8 +745,7 @@ inline PICout operator <<(PICout s, const PIDeque<T> & v) {
s << "{"; s << "{";
for (size_t i = 0; i < v.size(); ++i) { for (size_t i = 0; i < v.size(); ++i) {
s << v[i]; s << v[i];
if (i < v.size() - 1) if (i < v.size() - 1) s << ", ";
s << ", ";
} }
s << "}"; s << "}";
s.restoreControl(); s.restoreControl();

View File

@@ -174,6 +174,7 @@ public:
inline size_t length() const {return piv_size;} inline size_t length() const {return piv_size;}
inline size_t capacity() const {return piv_rsize;} inline size_t capacity() const {return piv_rsize;}
inline bool isEmpty() const {return (piv_size == 0);} inline bool isEmpty() const {return (piv_size == 0);}
inline bool isNotEmpty() const {return (piv_size > 0);}
inline T & operator [](size_t index) {return piv_data[index];} inline T & operator [](size_t index) {return piv_data[index];}
inline const T & operator [](size_t index) const {return piv_data[index];} inline const T & operator [](size_t index) const {return piv_data[index];}
@@ -287,9 +288,10 @@ public:
size_t os = piv_size; size_t os = piv_size;
alloc(new_size); alloc(new_size);
PIINTROSPECTION_CONTAINER_USED(T, (new_size-os)) PIINTROSPECTION_CONTAINER_USED(T, (new_size-os))
for (size_t i = os; i < new_size; ++i) for (size_t i = os; i < new_size; ++i) {
elementNew(piv_data + i, f); elementNew(piv_data + i, f);
} }
}
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)> f) {
@@ -302,9 +304,10 @@ public:
size_t os = piv_size; size_t os = piv_size;
alloc(new_size); alloc(new_size);
PIINTROSPECTION_CONTAINER_USED(T, (new_size-os)) PIINTROSPECTION_CONTAINER_USED(T, (new_size-os))
for (size_t i = os; i < new_size; ++i) for (size_t i = os; i < new_size; ++i) {
elementNew(piv_data + i, f(i)); elementNew(piv_data + i, f(i));
} }
}
return *this; return *this;
} }
template<typename T1 = T, typename std::enable_if< template<typename T1 = T, typename std::enable_if<
@@ -357,8 +360,9 @@ public:
assert(&other != this); assert(&other != this);
ssize_t os = piv_size - index; ssize_t os = piv_size - index;
alloc(piv_size + other.piv_size); alloc(piv_size + other.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 + other.piv_size])), (const void*)(&(piv_data[index])), os * sizeof(T));
}
newT(piv_data + index, other.piv_data, other.piv_size); newT(piv_data + index, other.piv_data, other.piv_size);
return *this; return *this;
} }
@@ -397,19 +401,22 @@ public:
} }
inline PIVector<T> & removeOne(const T & v) { inline PIVector<T> & removeOne(const T & v) {
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] == v) {
remove(i); remove(i);
return *this; return *this;
} }
}
return *this; return *this;
} }
inline PIVector<T> & removeAll(const T & v) { inline PIVector<T> & removeAll(const T & v) {
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] == v) {
remove(i); remove(i);
--i; --i;
} }
}
return *this; return *this;
} }
@@ -419,12 +426,14 @@ public:
elementNew(piv_data + piv_size - 1, v); elementNew(piv_data + piv_size - 1, v);
return *this; return *this;
} }
inline PIVector<T> & push_back(T && v) { inline PIVector<T> & push_back(T && v) {
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(v));
return *this; return *this;
} }
inline PIVector<T> & append(const T & v) {return push_back(v);} 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(T && v) {return push_back(std::move(v));}
inline PIVector<T> & append(const PIVector<T> & other) { inline PIVector<T> & append(const PIVector<T> & other) {
@@ -444,14 +453,12 @@ public:
inline PIVector<T> & prepend(T && v) {return push_front(std::move(v));} inline PIVector<T> & prepend(T && v) {return push_front(std::move(v));}
inline PIVector<T> & pop_back() { inline PIVector<T> & pop_back() {
if (piv_size == 0) if (piv_size == 0) return *this;
return *this;
resize(piv_size - 1); resize(piv_size - 1);
return *this; return *this;
} }
inline PIVector<T> & pop_front() { inline PIVector<T> & pop_front() {
if (piv_size == 0) if (piv_size == 0) return *this;
return *this;
remove(0); remove(0);
return *this; return *this;
} }
@@ -462,32 +469,37 @@ public:
template <typename ST> template <typename ST>
PIVector<ST> toType() const { PIVector<ST> toType() const {
PIVector<ST> ret(piv_size); PIVector<ST> ret(piv_size);
for (uint i = 0; i < piv_size; ++i) for (uint i = 0; i < piv_size; ++i) {
ret[i] = ST(piv_data[i]); ret[i] = ST(piv_data[i]);
}
return ret; return ret;
} }
const PIVector<T> & forEach(std::function<void(const T &)> f) const { const PIVector<T> & forEach(std::function<void(const T &)> 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 &)> 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 &)> 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> toType(std::function<ST(const T &)> 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;
} }
@@ -497,16 +509,18 @@ public:
assert(rows*cols == piv_size); assert(rows*cols == piv_size);
ret.resize(rows); ret.resize(rows);
if (order == byRow) { if (order == byRow) {
for (size_t r = 0; r < rows; r++) for (size_t r = 0; r < rows; r++) {
ret[r] = PIVector<T>(&(piv_data[r*cols]), cols); ret[r] = PIVector<T>(&(piv_data[r*cols]), cols);
} }
}
if (order == byColumn) { if (order == byColumn) {
for (size_t r = 0; r < rows; r++) { for (size_t r = 0; r < rows; r++) {
ret[r].resize(cols); ret[r].resize(cols);
for (size_t c = 0; c < cols; c++) for (size_t c = 0; c < cols; c++) {
ret[r][c] = piv_data[c*rows + r]; ret[r][c] = piv_data[c*rows + r];
} }
} }
}
return ret; return ret;
} }
@@ -520,14 +534,17 @@ public:
size_t cols = at(0).size(); size_t cols = at(0).size();
ret.reserve(rows * cols); ret.reserve(rows * cols);
if (order == byRow) { if (order == byRow) {
for (size_t r = 0; r < rows; r++) for (size_t r = 0; r < rows; r++) {
ret.append(at(r)); ret.append(at(r));
} }
}
if (order == byColumn) { if (order == byColumn) {
for (size_t c = 0; c < cols; c++) for (size_t c = 0; c < cols; c++) {
for (size_t r = 0; r < rows; r++) for (size_t r = 0; r < rows; r++) {
ret << at(r)[c]; ret << at(r)[c];
} }
}
}
ret.resize(rows * cols); ret.resize(rows * cols);
return ret; return ret;
} }
@@ -536,8 +553,9 @@ private:
inline void _reset() {piv_size = piv_rsize = 0; piv_data = 0;} inline void _reset() {piv_size = piv_rsize = 0; piv_data = 0;}
inline size_t asize(size_t s) { inline size_t asize(size_t s) {
if (s == 0) return 0; if (s == 0) return 0;
if (piv_rsize + piv_rsize >= s && piv_rsize < s) if (piv_rsize + piv_rsize >= s && piv_rsize < s) {
return piv_rsize + piv_rsize; return piv_rsize + piv_rsize;
}
ssize_t t = 0, s_ = s - 1; ssize_t t = 0, s_ = s - 1;
while (s_ >> t) ++t; while (s_ >> t) ++t;
return (1 << t); return (1 << t);
@@ -563,10 +581,11 @@ private:
inline void deleteT(T * d, size_t sz) { inline void deleteT(T * d, size_t sz) {
PIINTROSPECTION_CONTAINER_UNUSED(T, sz) PIINTROSPECTION_CONTAINER_UNUSED(T, sz)
if ((uchar*)d != 0) { if ((uchar*)d != 0) {
for (size_t i = 0; i < sz; ++i) for (size_t i = 0; i < sz; ++i) {
elementDelete(d[i]); elementDelete(d[i]);
} }
} }
}
template<typename T1 = T, typename std::enable_if< template<typename T1 = T, typename std::enable_if<
std::is_trivially_copyable<T1>::value std::is_trivially_copyable<T1>::value
, int>::type = 0> , int>::type = 0>
@@ -624,7 +643,15 @@ private:
#ifdef PIP_STD_IOSTREAM #ifdef PIP_STD_IOSTREAM
template<typename T> template<typename T>
inline std::ostream & operator <<(std::ostream & s, const PIVector<T> & v) {s << "{"; for (size_t i = 0; i < v.size(); ++i) {s << v[i]; if (i < v.size() - 1) s << ", ";} s << "}"; return s;} inline std::ostream & operator <<(std::ostream & s, const PIVector<T> & v) {
s << "{";
for (size_t i = 0; i < v.size(); ++i) {
s << v[i];
if (i < v.size() - 1) s << ", ";
}
s << "}";
return s;
}
#endif #endif
template<typename T> template<typename T>
@@ -634,9 +661,10 @@ inline PICout operator <<(PICout s, const PIVector<T> & v) {
s << "{"; s << "{";
for (size_t i = 0; i < v.size(); ++i) { for (size_t i = 0; i < v.size(); ++i) {
s << v[i]; s << v[i];
if (i < v.size() - 1) if (i < v.size() - 1) {
s << ", "; s << ", ";
} }
}
s << "}"; s << "}";
s.restoreControl(); s.restoreControl();
return s; return s;