Files
pip/pivector.h

440 lines
19 KiB
C++

/*! \file picontainers.h
* \brief Dynamic array of any type
*
* This file declare PIVector
*/
/*
PIP - Platform Independent Primitives
Dynamic array of any type
Copyright (C) 2013 Ivan Pelipenko peri4ko@gmail.com
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PIVECTOR_H
#define PIVECTOR_H
#include "piincludes.h"
#if !defined(PIP_CONTAINERS_STL) || defined(DOXYGEN)
template <typename T>
class PIVector {
public:
PIVector(): __piv_data__(0), __piv_size__(0), __piv_rsize__(0) {
//printf("new vector 1 %p (%s) ... !{\n", this, typeid(T).name());
//printf("(s=%d, d=%p) }!\n", int(__piv_size__), __piv_data__);
}
PIVector(const PIVector<T> & other): __piv_data__(0), __piv_size__(0), __piv_rsize__(0) {
//printf("new vector 2 %p (%s) ... !{\n", this, typeid(T).name());
alloc(other.__piv_size__);
for (ullong i = 0; i < __piv_size__; ++i)
new(__piv_data__ + i)T(other.__piv_data__[i]);
//printf("(s=%d, d=%p) }!\n", int(__piv_size__), __piv_data__);
}
PIVector(ullong __piv_size__, const T & f = T()): __piv_data__(0), __piv_size__(0), __piv_rsize__(0) {
//printf("new vector 3 %p (%s) ... !{\n", this, typeid(T).name());
resize(__piv_size__, f);
//printf("(s=%d, d=%p) }!\n", int(__piv_size__), __piv_data__);
}
~PIVector() {
//printf("delete vector %p (%s) (s=%d, d=%p) ... ~{\n", this, typeid(T).name(), int(__piv_size__), __piv_data__);
deleteT(__piv_data__, __piv_size__);
dealloc();
_reset();
//printf("}~\n");
}
PIVector<T> & operator =(const PIVector<T> & other) {
if (this == &other) return *this;
bool tj, oj;
tj = (__piv_size__ != 0 && __piv_data__ == 0);// || (__piv_size__ == 0 && __piv_data__ != 0);
oj = (other.__piv_size__ != 0 && other.__piv_data__ == 0);// || (other.__piv_size__ == 0 && other.__piv_data__ != 0);
//printf("operator= (%p = %p) (s=%d, d=%p, o.s=%d, o.d=%p) (%d, %d) ... o[\n", this, &other, int(__piv_size__), __piv_data__, int(other.__piv_size__), other.__piv_data__, int(tj), int(oj));
if (tj) {
printf("JUNK this\n");
_reset();
} else {
clear();
}
/*if (__piv_size__ == other.__piv_size__) {
for (ullong i = 0; i < __piv_size__; ++i)
__piv_data__[i] = other.__piv_data__[i];
return *this;
}*/
if (!oj) {
alloc(other.__piv_size__);
//zeroRaw(__piv_data__, __piv_size__);
for (ullong i = 0; i < __piv_size__; ++i)
new(__piv_data__ + i)T(other.__piv_data__[i]); //__piv_data__[i] = other.__piv_data__[i];
} else {
printf("JUNK other\n");
}
//printf("o]\n");
return *this;
}
typedef T value_type;
class iterator {
friend class PIVector<T>;
private:
iterator(PIVector<T> * v, ullong p): parent(v), pos(p) {}
PIVector<T> * parent;
ullong pos;
public:
iterator(): parent(0) {}
T & operator *() {return (*parent)[pos];}
const T & operator *() const {return (*parent)[pos];}
void operator ++() {++pos;}
void operator ++(int) {++pos;}
void operator --() {--pos;}
void operator --(int) {--pos;}
bool operator ==(const iterator & it) const {return (pos == it.pos);}
bool operator !=(const iterator & it) const {return (pos != it.pos);}
};
class const_iterator {
friend class PIVector<T>;
private:
const_iterator(const PIVector<T> * v, ullong p): parent(v), pos(p) {}
const PIVector<T> * parent;
ullong pos;
public:
const_iterator(): parent(0) {}
//T & operator *() {return (*parent)[pos];}
const T & operator *() const {return (*parent)[pos];}
void operator ++() {++pos;}
void operator ++(int) {++pos;}
void operator --() {--pos;}
void operator --(int) {--pos;}
bool operator ==(const const_iterator & it) const {return (pos == it.pos);}
bool operator !=(const const_iterator & it) const {return (pos != it.pos);}
};
class reverse_iterator {
friend class PIVector<T>;
private:
reverse_iterator(PIVector<T> * v, ullong p): parent(v), pos(p) {}
PIVector<T> * parent;
ullong pos;
public:
reverse_iterator(): parent(0) {}
T & operator *() {return (*parent)[pos];}
const T & operator *() const {return (*parent)[pos];}
void operator ++() {--pos;}
void operator ++(int) {--pos;}
void operator --() {++pos;}
void operator --(int) {++pos;}
bool operator ==(const reverse_iterator & it) const {return (pos == it.pos);}
bool operator !=(const reverse_iterator & it) const {return (pos != it.pos);}
};
class const_reverse_iterator {
friend class PIVector<T>;
private:
const_reverse_iterator(const PIVector<T> * v, ullong p): parent(v), pos(p) {}
const PIVector<T> * parent;
ullong pos;
public:
const_reverse_iterator(): parent(0) {}
//T & operator *() {return (*parent)[pos];}
const T & operator *() const {return (*parent)[pos];}
void operator ++() {--pos;}
void operator ++(int) {--pos;}
void operator --() {++pos;}
void operator --(int) {++pos;}
bool operator ==(const const_reverse_iterator & it) const {return (pos == it.pos);}
bool operator !=(const const_reverse_iterator & it) const {return (pos != it.pos);}
};
iterator begin() {return iterator(this, 0);}
iterator end() {return iterator(this, __piv_size__);}
const_iterator begin() const {return const_iterator(this, 0);}
const_iterator end() const {return const_iterator(this, __piv_size__);}
reverse_iterator rbegin() {return reverse_iterator(this, __piv_size__ - 1);}
reverse_iterator rend() {return reverse_iterator(this, -1);}
const_reverse_iterator rbegin() const {return const_reverse_iterator(this, __piv_size__ - 1);}
const_reverse_iterator rend() const {return const_reverse_iterator(this, -1);}
ullong size() const {return __piv_size__;}
int size_s() const {return __piv_size__;}
ullong length() const {return __piv_size__;}
bool isEmpty() const {return (__piv_size__ == 0);}
T & operator [](ullong index) {return __piv_data__[index];}
T & at(ullong index) {return __piv_data__[index];}
const T & operator [](ullong index) const {return __piv_data__[index];}
const T & at(ullong index) const {return __piv_data__[index];}
T & back() {return __piv_data__[__piv_size__ - 1];}
const T & back() const {return __piv_data__[__piv_size__ - 1];}
T & front() {return __piv_data__[0];}
const T & front() const {return __piv_data__[0];}
bool operator ==(const PIVector<T> & t) const {if (__piv_size__ != t.__piv_size__) return false; for (ullong i = 0; i < __piv_size__; ++i) if (t[i] != __piv_data__[i]) return false; return true;}
bool operator !=(const PIVector<T> & t) const {if (__piv_size__ != t.__piv_size__) return true; for (ullong i = 0; i < __piv_size__; ++i) if (t[i] != __piv_data__[i]) return true; return false;}
bool contains(const T & v) const {for (ullong i = 0; i < __piv_size__; ++i) if (v == __piv_data__[i]) return true; return false;}
int etries(const T & v) const {int ec = 0; for (ullong i = 0; i < __piv_size__; ++i) if (v == __piv_data__[i]) ++ec; return ec;}
T * data(ullong index = 0) {return &(__piv_data__[index]);}
const T * data(ullong index = 0) const {return &(__piv_data__[index]);}
PIVector<T> & clear() {resize(0); return *this;}
PIVector<T> & fill(const T & f = T()) {
//if (sizeof(T) == 1) memset(__piv_data__, f, __piv_size__);
deleteT(__piv_data__, __piv_size__);
//zeroRaw(__piv_data__, __piv_size__);
for (ullong i = 0; i < __piv_size__; ++i)
new(__piv_data__ + i)T(f);
return *this;
}
PIVector<T> & assign(const T & f = T()) {return fill(f);}
PIVector<T> & resize(ullong new_size, const T & f = T()) {
if (new_size < __piv_size__) {
T * de = &(__piv_data__[new_size]);
deleteT(de, __piv_size__ - new_size);
__piv_size__ = new_size;
}
if (new_size > __piv_size__) {
ullong os = __piv_size__;
alloc(new_size);
//if (sizeof(T) == 1) memset(&(__piv_data__[os]), f, ds);
//zeroRaw(&(__piv_data__[os]), new_size - os);
for (ullong i = os; i < new_size; ++i) new(__piv_data__ + i)T(f);
}
return *this;
}
PIVector<T> & reserve(ullong new_size) {if (new_size <= __piv_rsize__) return *this; ullong os = __piv_size__; alloc(new_size); __piv_size__ = os; return *this;}
PIVector<T> & insert(ullong index, const T & v = T()) {
alloc(__piv_size__ + 1);
if (index < __piv_size__ - 1) {
ullong os = __piv_size__ - index - 1;
T * pd = newRaw(os);
memcpy(pd, &(__piv_data__[index]), os * sizeof(T));
memcpy(&(__piv_data__[index + 1]), pd, os * sizeof(T));
deleteRaw(pd);
}
//zeroRaw(&(__piv_data__[index]), 1);
new(__piv_data__ + index)T(v);
return *this;
}
PIVector<T> & remove(ullong index, ullong count = 1) {
if (index + count >= __piv_size__) {
resize(index);
return *this;
}
ullong os = __piv_size__ - index - count;
T * pd = newRaw(os), * de = &(__piv_data__[index]);
deleteT(de, count);
memcpy(pd, &(__piv_data__[index + count]), os * sizeof(T));
memcpy(&(__piv_data__[index]), pd, os * sizeof(T));
deleteRaw(pd);
__piv_size__ -= count;
return *this;
}
void swap(PIVector<T> & other) {
piSwap<T*>(__piv_data__, other.__piv_data__);
piSwap<ullong>(__piv_size__, other.__piv_size__);
piSwap<ullong>(__piv_rsize__, other.__piv_rsize__);
}
typedef int (*CompareFunc)(const T * , const T * );
static int compare_func(const T * t0, const T * t1) {return (*t0) == (*t1) ? 0 : ((*t0) < (*t1) ? -1 : 1);}
PIVector<T> & sort(CompareFunc compare = compare_func) {qsort(__piv_data__, __piv_size__, sizeof(T), (int(*)(const void * , const void * ))compare); return *this;}
PIVector<T> & enlarge(llong __piv_size__) {llong ns = size_s() + __piv_size__; if (ns <= 0) clear(); else resize(ullong(ns)); return *this;}
PIVector<T> & removeOne(const T & v) {for (ullong i = 0; i < __piv_size__; ++i) if (__piv_data__[i] == v) {remove(i); return *this;} return *this;}
PIVector<T> & removeAll(const T & v) {for (ullong i = 0; i < __piv_size__; ++i) if (__piv_data__[i] == v) {remove(i); --i;} return *this;}
PIVector<T> & push_back(const T & v) {alloc(__piv_size__ + 1); new(__piv_data__ + __piv_size__ - 1)T(v); return *this;}
PIVector<T> & append(const T & v) {return push_back(v);}
PIVector<T> & operator <<(const T & v) {return push_back(v);}
PIVector<T> & push_front(const T & v) {insert(0, v); return *this;}
PIVector<T> & prepend(const T & v) {return push_front(v);}
PIVector<T> & pop_back() {if (__piv_size__ == 0) return *this; resize(__piv_size__ - 1); return *this;}
PIVector<T> & pop_front() {if (__piv_size__ == 0) return *this; remove(0); return *this;}
T take_back() {T t(back()); pop_back(); return t;}
T take_front() {T t(front()); pop_front(); return t;}
private:
void _reset() {__piv_size__ = __piv_rsize__ = 0; __piv_data__ = 0;}
ullong asize(ullong s) {
if (s == 0) return 0;
if (__piv_rsize__ + __piv_rsize__ >= s && __piv_rsize__ < s)
return __piv_rsize__ + __piv_rsize__;
ullong t = 0, s_ = s - 1;
while (s_ >> t) ++t;
return (1 << t);
}
T * newRaw(ullong s) {
//cout << std::dec << " ![("<<this<<")newRaw " << s << " elements ... <\n" << endl;
//uchar * ret = new uchar[s * sizeof(T)];
uchar * ret = (uchar*)(malloc(s * sizeof(T)));//new uchar[];
//zeroRaw((T*)ret, s);
//cout << std::hex << " > (new 0x" << (llong)ret << ") ok]!" << endl;
return (T*)ret;
}
void deleteT(T * d, ullong sz) {
//cout << " ~[("<<this<<")deleteT " << std::dec << sz << " elements " << std::hex << "0x" << (llong)d << " ... <\n" << endl;
if ((uchar*)d != 0) {
for (ullong i = 0; i < sz; ++i)
d[i].~T();
//zeroRaw(d, sz);
}
//cout << " > ok]~" << endl;
}
void deleteRaw(T *& d) {
//cout << " ~[("<<this<<")deleteRaw " << std::dec << __piv_rsize__ << " elements " << std::hex << "0x" << (llong)d << " ... <\n" << endl;
if ((uchar*)d != 0) free((uchar*)d);
d = 0;
//cout << " > ok]~" << endl;
}
void zeroRaw(T * d, ullong s) {
//cout << " ~[("<<this<<")zeroRaw " << std::dec << s << " elements " << std::hex << "0x" << (llong)d << " ... <\n" << endl;
if ((uchar*)d != 0) memset(d, 0, s*sizeof(T));
//cout << " > ok]~" << endl;
}
void dealloc() {deleteRaw(__piv_data__);}
void alloc(ullong new_size) {
if (new_size <= __piv_rsize__) {
__piv_size__ = new_size;
return;
}
//int os = __piv_size__;
__piv_size__ = new_size;
ullong as = asize(new_size);
if (as == __piv_rsize__) return;
//cout << std::hex << " ![("<<this<<")realloc " << __piv_data__ << " data ... <\n" << endl;
__piv_data__ = (T*)(realloc(__piv_data__, as*sizeof(T)));
//zeroRaw(&(__piv_data__[os]), as - os);
__piv_rsize__ = as;
//cout << std::hex << " > (new 0x" << (llong)__piv_data__ << ") ok]!" << endl;
/*__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__;
ullong __piv_size__, __piv_rsize__;
};
#else
template<typename T, typename Allocator = std::allocator<T> >
class PIP_EXPORT PIVector: public vector<T, Allocator> {
typedef PIVector<T, Allocator> _CVector;
typedef vector<T, Allocator> _stlc;
public:
PIVector() {piMonitor.containers++;}
PIVector(uint size, const T & value = T()) {piMonitor.containers++; _stlc::resize(size, value);}
~PIVector() {piMonitor.containers--;}
const T & at(uint index) const {return (*this)[index];}
T & at(uint index) {return (*this)[index];}
const T * data(uint index = 0) const {return &(*this)[index];}
T * data(uint index = 0) {return &(*this)[index];}
#ifdef DOXYGEN
uint size() const;
#endif
int size_s() const {return static_cast<int>(_stlc::size());}
bool isEmpty() const {return _stlc::empty();}
bool has(const T & t) const {for (typename _stlc::const_iterator i = _stlc::begin(); i != _stlc::end(); ++i) if (t == *i) return true; return false;}
int etries(const T & t) const {int ec = 0; for (typename _stlc::const_iterator i = _stlc::begin(); i != _stlc::end(); ++i) if (t == *i) ++ec; return ec;}
typedef int (*CompareFunc)(const T * , const T * );
static int compare_func(const T * t0, const T * t1) {return (*t0) == (*t1) ? 0 : ((*t0) < (*t1) ? -1 : 1);}
#ifdef DOXYGEN
void resize(uint size, const T & new_type = T());
PIVector<T, Allocator> & enlarge(uint size);
void clear();
PIVector<T, Allocator> & sort(CompareFunc compare = compare_func) {qsort(&at(0), _stlc::size(), sizeof(T), (int(*)(const void * , const void * ))compare); return *this;}
PIVector<T, Allocator> & fill(const T & t) {_stlc::assign(_stlc::size(), t); return *this;}
T & back();
const T & back() const;
T & front();
const T & front() const;
PIVector<T, Allocator> & push_back(const T & t);
PIVector<T, Allocator> & push_front(const T & t) {_stlc::insert(_stlc::begin(), t); return *this;}
PIVector<T, Allocator> & pop_back();
PIVector<T, Allocator> & pop_front() {_stlc::erase(_stlc::begin()); return *this;}
T take_back() {T t(_stlc::back()); _stlc::pop_back(); return t;}
T take_front() {T t(_stlc::front()); pop_front(); return t;}
PIVector<T, Allocator> & remove(uint index) {_stlc::erase(_stlc::begin() + index); return *this;}
PIVector<T, Allocator> & remove(uint index, uint count) {_stlc::erase(_stlc::begin() + index, _stlc::begin() + index + count); return *this;}
PIVector<T, Allocator> & removeOne(const T & v) {for (typename _stlc::iterator i = _stlc::begin(); i != _stlc::end(); ++i) if (v == *i) {_stlc::erase(i); return *this;} return *this;}
PIVector<T, Allocator> & removeAll(const T & v) {for (typename _stlc::iterator i = _stlc::begin(); i != _stlc::end(); ++i) if (v == *i) {_stlc::erase(i); --i;} return *this;}
PIVector<T, Allocator> & insert(uint pos, const T & t) {_stlc::insert(_stlc::begin() + pos, t); return *this;}
PIVector<T, Allocator> & insert(uint pos, const PIVector<T, Allocator> & t) {_stlc::insert(_stlc::begin() + pos, t.begin(), t.end()); return *this;}
T & operator [](uint index);
const T & operator [](uint index) const;
PIVector<T, Allocator> & operator <<(const T & t) {_stlc::push_back(t); return *this;}
PIVector<T, Allocator> & operator <<(const PIVector<T, Allocator> & t) {for (typename _stlc::const_iterator i = t.begin(); i != t.end(); i++) _stlc::push_back(*i); return *this;}
bool operator ==(const PIVector<T, Allocator> & t) {for (uint i = 0; i < _stlc::size(); ++i) if (t[i] != at(i)) return false; return true;}
bool operator !=(const PIVector<T, Allocator> & t) {for (uint i = 0; i < _stlc::size(); ++i) if (t[i] != at(i)) return true; return false;}
bool contains(const T & v) const {for (uint i = 0; i < _stlc::size(); ++i) if (v == at(i)) return true; return false;}
#else
_CVector & enlarge(int size_) {int ns = size_s() + size_; if (ns <= 0) _stlc::clear(); else _stlc::resize(ns); return *this;}
_CVector & sort(CompareFunc compare = compare_func) {qsort(&at(0), _stlc::size(), sizeof(T), (int(*)(const void * , const void * ))compare); return *this;}
_CVector & fill(const T & t) {_stlc::assign(_stlc::size(), t); return *this;}
_CVector & pop_front() {_stlc::erase(_stlc::begin()); return *this;}
_CVector & push_front(const T & t) {_stlc::insert(_stlc::begin(), t); return *this;}
T take_front() {T t(_stlc::front()); pop_front(); return t;}
T take_back() {T t(_stlc::back()); _stlc::pop_back(); return t;}
_CVector & remove(uint index) {_stlc::erase(_stlc::begin() + index); return *this;}
_CVector & remove(uint index, uint count) {_stlc::erase(_stlc::begin() + index, _stlc::begin() + index + count); return *this;}
_CVector & removeOne(const T & v) {for (typename _stlc::iterator i = _stlc::begin(); i != _stlc::end(); ++i) if (v == *i) {_stlc::erase(i); return *this;} return *this;}
_CVector & removeAll(const T & v) {for (typename _stlc::iterator i = _stlc::begin(); i != _stlc::end(); ++i) if (v == *i) {_stlc::erase(i); --i;} return *this;}
_CVector & insert(uint pos, const T & t) {_stlc::insert(_stlc::begin() + pos, t); return *this;}
_CVector & insert(uint pos, const _CVector & t) {_stlc::insert(_stlc::begin() + pos, t.begin(), t.end()); return *this;}
_CVector & operator <<(const T & t) {_stlc::push_back(t); return *this;}
_CVector & operator <<(const _CVector & t) {for (typename _stlc::const_iterator i = t.begin(); i != t.end(); i++) _stlc::push_back(*i); return *this;}
bool operator ==(const _CVector & t) {for (uint i = 0; i < _stlc::size(); ++i) if (t[i] != at(i)) return false; return true;}
bool operator !=(const _CVector & t) {for (uint i = 0; i < _stlc::size(); ++i) if (t[i] != at(i)) return true; return false;}
bool contains(const T & v) const {for (uint i = 0; i < _stlc::size(); ++i) if (v == at(i)) return true; return false;}
#endif
};
#endif
template<typename T>
inline std::ostream & operator <<(std::ostream & s, const PIVector<T> & v) {s << "{"; for (uint i = 0; i < v.size(); ++i) {s << v[i]; if (i < v.size() - 1) s << ", ";} s << "}"; return s;}
template<typename T>
inline PICout operator <<(PICout s, const PIVector<T> & v) {s.space(); s.setControl(0, true); s << "{"; for (uint i = 0; i < v.size(); ++i) {s << v[i]; if (i < v.size() - 1) s << ", ";} s << "}"; s.restoreControl(); return s;}
#endif // PIVECTOR_H