/*! \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 .
*/
#ifndef PIVECTOR_H
#define PIVECTOR_H
#include "piincludes.h"
#if !defined(PIP_CONTAINERS_STL) || defined(DOXYGEN)
template
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 & 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 & operator =(const PIVector & 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;
private:
iterator(PIVector * v, ullong p): parent(v), pos(p) {}
PIVector * 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;
private:
const_iterator(const PIVector * v, ullong p): parent(v), pos(p) {}
const PIVector * 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;
private:
reverse_iterator(PIVector * v, ullong p): parent(v), pos(p) {}
PIVector * 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;
private:
const_reverse_iterator(const PIVector * v, ullong p): parent(v), pos(p) {}
const PIVector * 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) 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) 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 & clear() {resize(0); return *this;}
PIVector & 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 & assign(const T & f = T()) {return fill(f);}
PIVector & 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 & reserve(ullong new_size) {if (new_size <= __piv_rsize__) return *this; ullong os = __piv_size__; alloc(new_size); __piv_size__ = os; return *this;}
PIVector & 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 & 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 & other) {
piSwap(__piv_data__, other.__piv_data__);
piSwap(__piv_size__, other.__piv_size__);
piSwap(__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 & sort(CompareFunc compare = compare_func) {qsort(__piv_data__, __piv_size__, sizeof(T), (int(*)(const void * , const void * ))compare); return *this;}
PIVector & enlarge(llong __piv_size__) {llong ns = size_s() + __piv_size__; if (ns <= 0) clear(); else resize(ullong(ns)); return *this;}
PIVector & removeOne(const T & v) {for (ullong i = 0; i < __piv_size__; ++i) if (__piv_data__[i] == v) {remove(i); return *this;} return *this;}
PIVector & removeAll(const T & v) {for (ullong i = 0; i < __piv_size__; ++i) if (__piv_data__[i] == v) {remove(i); --i;} return *this;}
PIVector & push_back(const T & v) {alloc(__piv_size__ + 1); new(__piv_data__ + __piv_size__ - 1)T(v); return *this;}
PIVector & append(const T & v) {return push_back(v);}
PIVector & operator <<(const T & v) {return push_back(v);}
PIVector & push_front(const T & v) {insert(0, v); return *this;}
PIVector & prepend(const T & v) {return push_front(v);}
PIVector & pop_back() {if (__piv_size__ == 0) return *this; resize(__piv_size__ - 1); return *this;}
PIVector & 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 << " ![("< (new 0x" << (llong)ret << ") ok]!" << endl;
return (T*)ret;
}
void deleteT(T * d, ullong sz) {
//cout << " ~[("< ok]~" << endl;
}
void deleteRaw(T *& d) {
//cout << " ~[("< ok]~" << endl;
}
void zeroRaw(T * d, ullong s) {
//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 << " ![("< (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 >
class PIP_EXPORT PIVector: public vector {
typedef PIVector _CVector;
typedef vector _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(_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 & enlarge(uint size);
void clear();
PIVector & sort(CompareFunc compare = compare_func) {qsort(&at(0), _stlc::size(), sizeof(T), (int(*)(const void * , const void * ))compare); return *this;}
PIVector & fill(const T & t) {_stlc::assign(_stlc::size(), t); return *this;}
T & back();
const T & back() const;
T & front();
const T & front() const;
PIVector & push_back(const T & t);
PIVector & push_front(const T & t) {_stlc::insert(_stlc::begin(), t); return *this;}
PIVector & pop_back();
PIVector & 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 & remove(uint index) {_stlc::erase(_stlc::begin() + index); return *this;}
PIVector & remove(uint index, uint count) {_stlc::erase(_stlc::begin() + index, _stlc::begin() + index + count); return *this;}
PIVector & 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 & 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 & insert(uint pos, const T & t) {_stlc::insert(_stlc::begin() + pos, t); return *this;}
PIVector & insert(uint pos, const PIVector & t) {_stlc::insert(_stlc::begin() + pos, t.begin(), t.end()); return *this;}
T & operator [](uint index);
const T & operator [](uint index) const;
PIVector & operator <<(const T & t) {_stlc::push_back(t); return *this;}
PIVector & operator <<(const PIVector & t) {for (typename _stlc::const_iterator i = t.begin(); i != t.end(); i++) _stlc::push_back(*i); return *this;}
bool operator ==(const PIVector & t) {for (uint i = 0; i < _stlc::size(); ++i) if (t[i] != at(i)) return false; return true;}
bool operator !=(const PIVector & 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
inline std::ostream & operator <<(std::ostream & s, const PIVector & v) {s << "{"; for (uint i = 0; i < v.size(); ++i) {s << v[i]; if (i < v.size() - 1) s << ", ";} s << "}"; return s;}
template
inline PICout operator <<(PICout s, const PIVector & 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