git-svn-id: svn://db.shs.com.ru/pip@814 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2019-06-25 13:48:18 +00:00
parent df2f01e6aa
commit 1598ac053c
2 changed files with 205 additions and 162 deletions

View File

@@ -96,7 +96,6 @@ public:
size_t pos;
public:
inline const_iterator(): parent(0), pos(0) {}
//T & operator *() {return (*parent)[pos];}
inline const T & operator *() const {return (*parent)[pos];}
inline void operator ++() {++pos;}
inline void operator ++(int) {++pos;}
@@ -132,7 +131,6 @@ public:
size_t pos;
public:
inline const_reverse_iterator(): parent(0), pos(0) {}
//T & operator *() {return (*parent)[pos];}
inline const T & operator *() const {return (*parent)[pos];}
inline void operator ++() {--pos;}
inline void operator ++(int) {--pos;}
@@ -165,12 +163,39 @@ public:
inline const T & back() const {return piv_data[piv_size - 1];}
inline T & front() {return piv_data[0];}
inline const T & front() const {return piv_data[0];}
inline bool operator ==(const PIVector<T> & t) const {if (piv_size != t.piv_size) return false; for (size_t i = 0; i < piv_size; ++i) if (t[i] != piv_data[i]) return false; return true;}
inline bool operator !=(const PIVector<T> & t) const {if (piv_size != t.piv_size) return true; for (size_t i = 0; i < piv_size; ++i) if (t[i] != piv_data[i]) return true; return false;}
inline bool contains(const T & v) const {for (size_t i = 0; i < piv_size; ++i) if (v == piv_data[i]) return true; return false;}
inline int etries(const T & v) const {int ec = 0; for (size_t i = 0; i < piv_size; ++i) if (v == piv_data[i]) ++ec; return ec;}
inline ssize_t indexOf(const T & v) const {for (size_t i = 0; i < piv_size; ++i) if (v == piv_data[i]) return i; return -1;}
inline ssize_t lastIndexOf(const T & v) const {for (ssize_t i = piv_size - 1; i >= 0; --i) if (v == piv_data[i]) return i; return -1;}
inline bool operator ==(const PIVector<T> & t) const {
if (piv_size != t.piv_size)
return false;
for (size_t i = 0; i < piv_size; ++i)
if (t[i] != piv_data[i])
return false;
return true;
}
inline bool operator !=(const PIVector<T> & t) const {return !(*this == t);}
inline bool contains(const T & v) const {
for (size_t i = 0; i < piv_size; ++i)
if (v == piv_data[i])
return true;
return false;
}
inline int etries(const T & v) const {
int ec = 0;
for (size_t i = 0; i < piv_size; ++i)
if (v == piv_data[i]) ++ec;
return ec;
}
inline ssize_t indexOf(const T & v) const {
for (size_t i = 0; i < piv_size; ++i)
if (v == piv_data[i])
return i;
return -1;
}
inline ssize_t lastIndexOf(const T & v) const {
for (ssize_t i = piv_size - 1; i >= 0; --i)
if (v == piv_data[i])
return i;
return -1;
}
inline T * data(size_t index = 0) {return &(piv_data[index]);}
inline const T * data(size_t index = 0) const {return &(piv_data[index]);}
@@ -184,7 +209,10 @@ public:
return *this;
}
inline PIVector<T> & assign(const T & f = T()) {return fill(f);}
inline PIVector<T> & assign(size_t new_size, const T & f) {resize(new_size); return fill(f);}
inline PIVector<T> & assign(size_t new_size, const T & f) {
resize(new_size);
return fill(f);
}
inline PIVector<T> & resize(size_t new_size, const T & f = T()) {
if (new_size < piv_size) {
@@ -230,6 +258,7 @@ public:
}
inline PIVector<T> & insert(size_t index, const PIVector<T> & other) {
if (other.isEmpty()) return *this;
assert(&other != this);
ssize_t os = piv_size - index;
alloc(piv_size + other.piv_size);
if (os > 0)
@@ -259,40 +288,78 @@ public:
typedef int (*CompareFunc)(const T * , const T * );
static int compare_func(const T * t0, const T * t1) {return (*t0) < (*t1) ? -1 : ((*t0) == (*t1) ? 0 : 1);}
inline PIVector<T> & sort(CompareFunc compare = compare_func) {piqsort(piv_data, piv_size, sizeof(T), (int(*)(const void * , const void * ))compare); return *this;}
inline PIVector<T> & sort(CompareFunc compare = compare_func) {
piqsort(piv_data, piv_size, sizeof(T), (int(*)(const void * , const void * ))compare);
return *this;
}
inline PIVector<T> & enlarge(llong piv_size) {llong ns = size_s() + piv_size; if (ns <= 0) clear(); else resize(size_t(ns)); return *this;}
inline PIVector<T> & enlarge(llong piv_size) {
llong ns = size_s() + piv_size;
if (ns <= 0) clear();
else resize(size_t(ns));
return *this;
}
inline PIVector<T> & removeOne(const T & v) {for (size_t i = 0; i < piv_size; ++i) if (piv_data[i] == v) {remove(i); return *this;} return *this;}
inline PIVector<T> & removeAll(const T & v) {for (ssize_t i = 0; i < ssize_t(piv_size); ++i) if (piv_data[i] == v) {remove(i); --i;} return *this;}
inline PIVector<T> & removeOne(const T & v) {
for (size_t i = 0; i < piv_size; ++i)
if (piv_data[i] == v) {
remove(i);
return *this;
}
return *this;
}
inline PIVector<T> & removeAll(const T & v) {
for (ssize_t i = 0; i < ssize_t(piv_size); ++i)
if (piv_data[i] == v) {
remove(i);
--i;
}
return *this;
}
inline PIVector<T> & push_back(const T & v) {alloc(piv_size + 1); PIINTROSPECTION_CONTAINER_USED(T, sizeof(T)); elementNew(piv_data + piv_size - 1, v); return *this;}
inline PIVector<T> & push_back(const T & v) {
alloc(piv_size + 1);
PIINTROSPECTION_CONTAINER_USED(T, sizeof(T));
elementNew(piv_data + piv_size - 1, v);
return *this;
}
inline PIVector<T> & append(const T & v) {return push_back(v);}
inline PIVector<T> & append(const PIVector<T> & other) {
size_t ps = piv_size;
alloc(piv_size + other.piv_size);
newT(piv_data + ps, other.piv_data, other.piv_size);
return *this;
}
inline PIVector<T> & operator <<(const T & v) {return push_back(v);}
inline PIVector<T> & operator <<(const PIVector<T> & other) {
assert(&other != this);
size_t ps = piv_size;
alloc(piv_size + other.piv_size);
newT(piv_data + ps, other.piv_data, other.piv_size);
return *this;
}
inline PIVector<T> & operator <<(const T & v) {return push_back(v);}
inline PIVector<T> & operator <<(const PIVector<T> & other) {return append(other);}
inline PIVector<T> & push_front(const T & v) {insert(0, v); return *this;}
inline PIVector<T> & prepend(const T & v) {return push_front(v);}
inline PIVector<T> & pop_back() {if (piv_size == 0) return *this; resize(piv_size - 1); return *this;}
inline PIVector<T> & pop_front() {if (piv_size == 0) return *this; remove(0); return *this;}
inline PIVector<T> & pop_back() {
if (piv_size == 0)
return *this;
resize(piv_size - 1);
return *this;
}
inline PIVector<T> & pop_front() {
if (piv_size == 0)
return *this;
remove(0);
return *this;
}
inline T take_back() {T t(back()); pop_back(); return t;}
inline T take_front() {T t(front()); pop_front(); return t;}
template <typename ST>
PIVector<ST> toType() const {PIVector<ST> ret(piv_size); for (uint i = 0; i < piv_size; ++i) ret[i] = ST(piv_data[i]); return ret;}
PIVector<ST> toType() const {
PIVector<ST> ret(piv_size);
for (uint i = 0; i < piv_size; ++i)
ret[i] = ST(piv_data[i]);
return ret;
}
private:
inline void _reset() {piv_size = piv_rsize = 0; piv_data = 0;}
@@ -309,14 +376,6 @@ private:
for (size_t i = 0; i < s; ++i)
elementNew(dst + i, src[i]);
}
inline T * newRaw(size_t s) {
uchar * ret = (uchar*)(malloc(s * sizeof(T)));//new uchar[];
return (T*)ret;
}
/*void reallocRawTemp(size_t s) {
if (piv_tdata == 0) piv_tdata = (T*)(malloc(s * sizeof(T)));
else piv_tdata = (T*)(realloc(piv_tdata, s * sizeof(T)));
}*/
inline void deleteT(T * d, size_t sz) {
PIINTROSPECTION_CONTAINER_UNUSED(T, sz*sizeof(T))
if ((uchar*)d != 0) {
@@ -339,81 +398,22 @@ private:
size_t as = asize(new_size);
if (as == piv_rsize) return;
PIINTROSPECTION_CONTAINER_ALLOC(T, (as-piv_rsize)*sizeof(T))
T * p_d = (T*)(realloc((void*)(piv_data), as*sizeof(T)));
assert(p_d);
piv_data = p_d;
piv_rsize = as;
/*piv_rsize = as;
T * pd = newRaw(piv_rsize);
if (os > 0 && piv_data != 0) {
memcpy(pd, piv_data, os * sizeof(T));
deleteRaw(piv_data);
}
piv_data = pd;*/
}
T * piv_data;
size_t piv_size, piv_rsize;
};
/*
#define __PIVECTOR_SIMPLE_FUNCTIONS__(T) \
template<> inline PIVector<T>::~PIVector() {dealloc(); _reset();} \
template<> inline PIVector<T> & PIVector<T>::push_back(const T & v) {alloc(piv_size + 1); piv_data[piv_size - 1] = v; return *this;} \
template<> inline PIVector<T> & PIVector<T>::fill(const T & f) { \
for (size_t i = 0; i < piv_size; ++i) \
piv_data[i] = f; \
return *this; \
} \
template<> inline PIVector<T> & PIVector<T>::resize(size_t new_size, const T & f) { \
if (new_size < piv_size) \
piv_size = new_size; \
if (new_size > piv_size) { \
size_t os = piv_size; \
alloc(new_size); \
for (size_t i = os; i < new_size; ++i) piv_data[i] = f; \
} \
return *this; \
} \
template<> inline PIVector<T> & PIVector<T>::insert(size_t index, const T & v) { \
alloc(piv_size + 1); \
if (index < piv_size - 1) { \
size_t os = piv_size - index - 1; \
memmove(&(piv_data[index + 1]), &(piv_data[index]), os * sizeof(T)); \
} \
piv_data[index] = v; \
return *this; \
} \
template<> inline PIVector<T> & PIVector<T>::remove(size_t index, size_t count) { \
if (count == 0) return *this; \
if (index + count >= piv_size) { \
resize(index); \
return *this; \
} \
size_t os = piv_size - index - count; \
memmove(&(piv_data[index]), &(piv_data[index + count]), os * sizeof(T)); \
piv_size -= count; \
return *this; \
}
__PIVECTOR_SIMPLE_FUNCTIONS__(char)
__PIVECTOR_SIMPLE_FUNCTIONS__(uchar)
__PIVECTOR_SIMPLE_FUNCTIONS__(short)
__PIVECTOR_SIMPLE_FUNCTIONS__(ushort)
__PIVECTOR_SIMPLE_FUNCTIONS__(int)
__PIVECTOR_SIMPLE_FUNCTIONS__(uint)
__PIVECTOR_SIMPLE_FUNCTIONS__(long)
__PIVECTOR_SIMPLE_FUNCTIONS__(ulong)
__PIVECTOR_SIMPLE_FUNCTIONS__(llong)
__PIVECTOR_SIMPLE_FUNCTIONS__(ullong)
__PIVECTOR_SIMPLE_FUNCTIONS__(float)
__PIVECTOR_SIMPLE_FUNCTIONS__(double)
__PIVECTOR_SIMPLE_FUNCTIONS__(ldouble)*/
#define __PIVECTOR_SIMPLE_TYPE__(T) \
template<> inline void PIVector<T>::newT(T * dst, const T * src, size_t s) {PIINTROSPECTION_CONTAINER_USED(T, s*sizeof(T)); memcpy((void*)(dst), (const void*)(src), s * sizeof(T));} \
template<> inline void PIVector<T>::deleteT(T * d, size_t sz) {PIINTROSPECTION_CONTAINER_UNUSED(T, sz*sizeof(T));} \
template<> inline void PIVector<T>::deleteT(T *, size_t sz) {PIINTROSPECTION_CONTAINER_UNUSED(T, sz*sizeof(T));} \
template<> inline void PIVector<T>::elementNew(T * to, const T & from) {(*to) = from;} \
template<> inline void PIVector<T>::elementDelete(T & from) {;} \
template<> inline void PIVector<T>::elementDelete(T &) {;} \
template<> inline PIVector<T> & PIVector<T>::_resizeRaw(size_t new_size) { \
if (new_size > piv_size) { \
PIINTROSPECTION_CONTAINER_USED(T, (new_size-piv_size)*sizeof(T)); \
@@ -537,7 +537,19 @@ inline std::ostream & operator <<(std::ostream & s, const PIVector<T> & v) {s <<
#endif
template<typename T>
inline PICout operator <<(PICout s, const PIVector<T> & v) {s.space(); s.setControl(0, true); s << "{"; for (size_t i = 0; i < v.size(); ++i) {s << v[i]; if (i < v.size() - 1) s << ", ";} s << "}"; s.restoreControl(); return s;}
inline PICout operator <<(PICout s, const PIVector<T> & v) {
s.space();
s.setControl(0, true);
s << "{";
for (size_t i = 0; i < v.size(); ++i) {
s << v[i];
if (i < v.size() - 1)
s << ", ";
}
s << "}";
s.restoreControl();
return s;
}
#endif // PIVECTOR_H