/*! \file picontainers.h * \brief Generic containers based on STL * * This file declare all containers and useful macroses * to use them */ /* PIP - Platform Independent Primitives Generic containers based on STL 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 PICONTAINERS_H #define PICONTAINERS_H #include "piincludes.h" #ifdef DOXYGEN /*! \def piForeach(i,c) * \brief Macro for iterate any container * \details Use this macros instead of standard "for" * to get read/write access to each element of container. * Pass direction is direct \n * Example: \snippet picontainers.cpp foreach */ /*! \def piForeachC(i,c) * \brief Macro for iterate any container only for read * \details Use this macros instead of standard "for" * to get read access to each element of container. * Pass direction is direct \n * Example: \snippet picontainers.cpp foreachC */ /*! \def piForeachR(i,c) * \brief Macro for iterate any container with reverse direction * \details Use this macros instead of standard "for" * to get read/write access to each element of container. * Pass direction is reverse \n * Example: \snippet picontainers.cpp foreachR */ /*! \def piForeachCR(i,c) * \brief Macro for iterate any container only for read with reverse direction * \details Use this macros instead of standard "for" * to get read access to each element of container. * Pass direction is reverse \n * Example: \snippet picontainers.cpp foreachCR */ #endif #ifdef CC_GCC template class _PIForeachC { public: _PIForeachC(const Type & t, bool i = false): _t(t), _inv(i) {if (_inv) _rit = _t.rbegin(); else _it = _t.begin(); _break = false;} typename Type::value_type _var; typename Type::const_iterator _it; typename Type::const_reverse_iterator _rit; const Type & _t; bool _break, _inv; inline bool isEnd() {if (_inv) return _rit == _t.rend(); else return _it == _t.end();} inline void operator ++() {if (_inv) _rit++; else _it++; _break = false;} }; template class _PIForeach { public: _PIForeach(Type & t, bool i = false): _t(t), _inv(i) {if (_inv) _rit = _t.rbegin(); else _it = _t.begin(); _break = false;} typename Type::value_type _var; typename Type::iterator _it; typename Type::reverse_iterator _rit; Type & _t; bool _break, _inv; inline bool isEnd() {if (_inv) return _rit == _t.rend(); else return _it == _t.end();} inline void operator ++() {if (_inv) _rit++; else _it++; _break = false;} }; #define piForeach(i,c) for(_PIForeach _for(c); !_for.isEnd(); ++_for) \ for(i = *_for._it; !_for._break; _for._break = true) #define piForeachR(i,c) for(_PIForeach _for(c, true); !_for.isEnd(); ++_for) \ for(i = *_for._rit; !_for._break; _for._break = true) #define piForeachA(i,c) for(_PIForeach _for(c); !_for.isEnd(); ++_for) \ for(typeof(_for._var) & i(*_for._it); !_for._break; _for._break = true) #define piForeachAR(i,c) for(_PIForeach _for(c, true); !_for.isEnd(); ++_for) \ for(typeof(_for._var) & i(*_for._rit); !_for._break; _for._break = true) #define piForeachC(i,c) for(_PIForeachC _for(c); !_for.isEnd(); ++_for) \ for(const i = *_for._it; !_for._break; _for._break = true) #define piForeachCR(i,c) for(_PIForeachC _for(c, true); !_for.isEnd(); ++_for) \ for(const i = *_for._rit; !_for._break; _for._break = true) #define piForeachCA(i,c) for(_PIForeachC _for(c); !_for.isEnd(); ++_for) \ for(const typeof(_for._var) & i(*_for._it); !_for._break; _for._break = true) #define piForeachCAR(i,c) for(_PIForeachC _for(c, true); !_for.isEnd(); ++_for) \ for(const typeof(_for._var) & i(*_for._rit); !_for._break; _for._break = true) #define piForeachRA piForeachAR #define piForeachAC piForeachCA #define piForeachCRA piForeachCAR #define piForeachARC piForeachCAR #define piForeachACR piForeachCAR #define piForeachRCA piForeachCAR #define piForeachRAC piForeachCAR #else struct _PIForeachBase {mutable bool _break;}; template class _PIForeach: public _PIForeachBase { public: _PIForeach(Type & t, bool i = false): _t(t), _inv(i) {if (_inv) _rit = _t.rbegin(); else _it = _t.begin(); _break = false;} mutable typename Type::value_type _var; mutable typename Type::iterator _it; mutable typename Type::reverse_iterator _rit; Type & _t; bool _inv; bool isEnd() {if (_inv) return _rit == _t.rend(); else return _it == _t.end();} void operator ++() {if (_inv) _rit++; else _it++; _break = false;} }; template class _PIForeachC: public _PIForeachBase { public: _PIForeachC(const Type & t, bool i = false): _t(t), _inv(i) {if (_inv) _rit = _t.rbegin(); else _it = _t.begin(); _break = false;} mutable typename Type::value_type _var; mutable typename Type::const_iterator _it; mutable typename Type::const_reverse_iterator _rit; const Type & _t; bool _inv; bool isEnd() {if (_inv) return _rit == _t.rend(); else return _it == _t.end();} void operator ++() {if (_inv) _rit++; else _it++; _break = false;} }; template inline _PIForeach _PIForeachNew(T & t, bool i = false) {return _PIForeach(t, i);} template inline _PIForeach * _PIForeachCast(_PIForeachBase & c, T & ) {return static_cast<_PIForeach * >(&c);} template inline _PIForeachC _PIForeachNewC(const T & t, bool i = false) {return _PIForeachC(t, i);} template inline _PIForeachC * _PIForeachCastC(_PIForeachBase & c, const T & ) {return static_cast<_PIForeachC * >(&c);} #define piForeach(i,c) for(_PIForeachBase & _for = _PIForeachNew(c); !_PIForeachCast(_for, c)->isEnd(); ++(*_PIForeachCast(_for, c))) \ for(i = *(_PIForeachCast(_for, c)->_it); !_for._break; _for._break = true) #define piForeachR(i,c) for(_PIForeachBase & _for = _PIForeachNew(c, true); !_PIForeachCast(_for, c)->isEnd(); ++(*_PIForeachCast(_for, c))) \ for(i = *(_PIForeachCast(_for, c)->_rit); !_for._break; _for._break = true) #define piForeachC(i,c) for(_PIForeachBase & _for = _PIForeachNewC(c); !_PIForeachCastC(_for, c)->isEnd(); ++(*_PIForeachCastC(_for, c))) \ for(const i = *(_PIForeachCastC(_for, c)->_it); !_for._break; _for._break = true) #define piForeachCR(i,c) for(_PIForeachBase & _for = _PIForeachNewC(c, false); !_PIForeachCastC(_for, c)->isEnd(); ++(*_PIForeachCastC(_for, c))) \ for(const i = *(_PIForeachCastC(_for, c)->_rit); !_for._break; _for._break = true) #endif #define piForeachRC piForeachCR #define piForTimes(c) for(int _i##c = 0; _i##c < c; ++_i##c) /* template class PIVector { public: PIVector(uint size = 0, const T & f = T()): data_(0), size_(0), rsize_(0) {resize(size, f);} PIVector(T * d, uint size): data_(0), size_(0), rsize_(0) {alloc(size); for (uint i = 0; i < size; ++i) data_[i] = d[i];} ~PIVector() {dealloc();} typedef T value_type; class iterator { friend class PIVector; private: iterator(PIVector * v, int p): parent(v), pos(p) {} PIVector * parent; int 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, int p): parent(v), pos(p) {} const PIVector * parent; int 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, int p): parent(v), pos(p) {} PIVector * parent; int 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, int p): parent(v), pos(p) {} const PIVector * parent; int 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, size_);} const_iterator begin() const {return const_iterator(this, 0);} const_iterator end() const {return const_iterator(this, size_);} reverse_iterator rbegin() {return reverse_iterator(this, size_ - 1);} reverse_iterator rend() {return reverse_iterator(this, -1);} const_reverse_iterator rbegin() const {return const_reverse_iterator(this, size_ - 1);} const_reverse_iterator rend() const {return const_reverse_iterator(this, -1);} uint size() const {return size_;} int size_s() const {return size_;} int length() const {return size_;} bool isEmpty() const {return (size_ == 0);} T & operator [](int index) {return data_[index];} T & at(uint index) {return data_[index];} const T & operator [](int index) const {return data_[index];} const T & at(uint index) const {return data_[index];} T & back() {return data_[size_ - 1];} const T & back() const {return data_[size_ - 1];} T & front() {return data_[0];} const T & front() const {return data_[0];} bool operator ==(const PIVector & t) const {if (size_ != t.size_) return false; for (uint i = 0; i < size_; ++i) if (t[i] != data_[i]) return false; return true;} bool operator !=(const PIVector & t) const {if (size_ != t.size_) return true; for (uint i = 0; i < size_; ++i) if (t[i] != data_[i]) return true; return false;} bool contains(const T & v) const {for (uint i = 0; i < size_; ++i) if (v == data_[i]) return true; return false;} T * data(int index = 0) {return &(data_[index]);} const T * data(int index = 0) const {return &(data_[index]);} PIVector & clear() {resize(0); return *this;} PIVector & fill(const T & f = T()) { if (sizeof(T) == 1) memset(data_, f, size_); else for (uint i = 0; i < size_; ++i) memcpy(&(data_[i]), &f, sizeof(T)); return *this; } PIVector & assign(const T & f = T()) {return fill(f);} PIVector & resize(uint new_size, const T & f = T()) { if (new_size < size_) { size_ = new_size; return *this; } if (new_size > size_) { os = size_; alloc(new_size); uint ds = size_ - os; //if (sizeof(T) == 1) memset(&(data_[os]), f, ds); for (uint i = 0; i < ds; ++i) data_[os + i] = f; return *this; } return *this; } PIVector & insert(int index, const T & v = T()) { push_back(v); if (index >= int(size_ - 1)) return *this; os = size_ - index; T * pd = new T[os * sizeof(T)]; memcpy(pd, &(data_[index]), os * sizeof(T)); memcpy(&(data_[index + 1]), pd, os * sizeof(T)); delete[] pd; data_[index] = v; return *this; } PIVector & remove(int index, int count = 1) { if (index + count >= int(size_)) { resize(index); return *this; } os = size_ - index - count; T * pd = new T[os * sizeof(T)]; memcpy(pd, &(data_[index + count]), os * sizeof(T)); memcpy(&(data_[index]), pd, os * sizeof(T)); delete[] pd; resize(size_ - count); return *this; } 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(data_, size_, sizeof(T), (int(*)(const void * , const void * ))compare); return *this;} PIVector & removeOne(const T & v) {for (uint i = 0; i < size_; ++i) if (data_[i] == v) {remove(i); return *this;} return *this;} PIVector & removeAll(const T & v) {for (uint i = 0; i < size_; ++i) if (data_[i] == v) {remove(i); --i;} return *this;} PIVector & push_back(const T & v) {alloc(size_ + 1); data_[size_ - 1] = 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 & operator <<(const T & v) {return push_back(v);} PIVector & pop_back() {if (size_ == 0) return *this; resize(size_ - 1); return *this;} PIVector & pop_front() {if (size_ == 0) return *this; remove(0); return *this;} private: uint asize(uint s) {if (s == 0) return 0; if (rsize_ + rsize_ >= s && rsize_ < s) return rsize_ + rsize_; uint t = 0, s_ = s - 1; while (s_ >> t) ++t; return (1 << t);} void dealloc() {if (data_ != 0) delete[] data_; data_ = 0;} void alloc(uint new_size) { if (new_size <= rsize_) { size_ = new_size; return; } os = size_; size_ = new_size; uint as = asize(new_size); if (as == rsize_) return; T * pd = 0; if (os > 0) { pd = new T[os * sizeof(T)]; memcpy(pd, data_, os * sizeof(T)); } rsize_ = as; dealloc(); data_ = new T[rsize_ * sizeof(T)]; if (os > 0) { memcpy(data_, pd, size_ * sizeof(T)); delete[] pd; } } T * data_; uint size_, rsize_, os; }; */ /*! \brief Dynamic array for any type * \details This class used to store dynamic array of any * type of data. In memory data stored linear. You can insert * item in any place of remove some items from any place. * For quick add elements there is stream operator <<. * This class based on std::vector, expanding his * functionality. */ template > class PIP_EXPORT PIVector: public vector { typedef PIVector _CVector; typedef vector _stlc; public: //! Contructs an empty vector PIVector() {piMonitor.containers++;} //! Contructs vector with single element "value" PIVector(const Type & value) {piMonitor.containers++; _stlc::push_back(value);} //! Contructs vector with two elements "v0" and "v1" PIVector(const Type & v0, const Type & v1) {piMonitor.containers++; _stlc::push_back(v0); _stlc::push_back(v1);} //! Contructs vector with three elements "v0", "v1" and "v2" PIVector(const Type & v0, const Type & v1, const Type & v2) {piMonitor.containers++; _stlc::push_back(v0); _stlc::push_back(v1); _stlc::push_back(v2);} //! Contructs vector with four elements "v0", "v1", "v2" and "v3" PIVector(const Type & v0, const Type & v1, const Type & v2, const Type & v3) {piMonitor.containers++; _stlc::push_back(v0); _stlc::push_back(v1); _stlc::push_back(v2); _stlc::push_back(v3);} /*! \brief Contructs vector with size "size" filled elements "value" * \details Example: \snippet picontainers.cpp PIVector::PIVector */ PIVector(uint size, const Type & value = Type()) {piMonitor.containers++; _stlc::resize(size, value);} ~PIVector() {piMonitor.containers--;} /*! \brief Read-only access to element by index "index" * \details Example: \snippet picontainers.cpp PIVector::at_c * \sa \a operator[] */ const Type & at(uint index) const {return (*this)[index];} /*! \brief Full access to element by index "index" * \details Example: \snippet picontainers.cpp PIVector::at * \sa \a operator[] */ Type & at(uint index) {return (*this)[index];} /*! \brief Read-only pointer to element by index "index" * \details Example: \snippet picontainers.cpp PIVector::data_c */ const Type * data(uint index = 0) const {return &(*this)[index];} /*! \brief Pointer to element by index "index" * \details Example: \snippet picontainers.cpp PIVector::data */ Type * data(uint index = 0) {return &(*this)[index];} #ifdef DOXYGEN //! Elements count uint size() const; #endif //! Elements count int size_s() const {return static_cast(_stlc::size());} //! Return \c "true" if vector is empty, i.e. size = 0 bool isEmpty() const {return _stlc::empty();} //! Return \c "true" if vector has at least one element equal "t" bool has(const Type & t) const {for (typename _stlc::const_iterator i = _stlc::begin(); i != _stlc::end(); ++i) if (t == *i) return true; return false;} //! Return how many times element "t" appears in vector int etries(const Type & 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 Type * , const Type * ); //! Standard compare function for type "Type". Return 0 if t0 = t1, -1 if t0 < t1 and 1 if t0 > t1. static int compare_func(const Type * t0, const Type * t1) {return (*t0) == (*t1) ? 0 : ((*t0) < (*t1) ? -1 : 1);} #ifdef DOXYGEN /*! \brief Resize vector to size "size" * \details Elements removed from end of vector if new size < old size, or added new elements = "new_type" if new size > old size.\n * Example: \snippet picontainers.cpp PIVector::resize * \sa \a size(), \a clear() */ void resize(uint size, const Type & new_type = Type()); //! Increase vector size with "size" elements PIVector & enlarge(uint size); //! Clear vector. Equivalent to call "resize(0)" void clear(); /*! \brief Sort vector using quick sort algorithm and standard compare function * \details Example: \snippet picontainers.cpp PIVector::sort_0 * With custom compare function: \snippet picontainers.cpp PIVector::sort_1 */ PIVector & sort(CompareFunc compare = compare_func) {qsort(&at(0), _stlc::size(), sizeof(Type), (int(*)(const void * , const void * ))compare); return *this;} /*! \brief Fill vector with elements "t" leave size is unchanged and return reference to vector * \details Example: \snippet picontainers.cpp PIVector::fill */ PIVector & fill(const Type & t) {_stlc::assign(_stlc::size(), t); return *this;} //! Last element of vector Type & back(); //! Last element of vector const Type & back() const; //! First element of vector Type & front(); //! First element of vector const Type & front() const; //! Add new element "t" at the end of vector and return reference to vector PIVector & push_back(const Type & t); //! Add new element "t" at the beginning of vector and return reference to vector PIVector & push_front(const Type & t) {_stlc::insert(_stlc::begin(), t); return *this;} //! Remove one element from the end of vector and return reference to vector PIVector & pop_back(); //! Remove one element from the beginning of vector and return reference to vector PIVector & pop_front() {_stlc::erase(_stlc::begin()); return *this;} //! Remove one element from the end of vector and return it Type take_back() {Type t(_stlc::back()); _stlc::pop_back(); return t;} //! Remove one element from the beginning of vector and return it Type take_front() {Type t(_stlc::front()); pop_front(); return t;} /*! \brief Remove one element by index "index" and return reference to vector * \details Example: \snippet picontainers.cpp PIVector::remove_0 * \sa \a removeOne(), \a removeAll() */ PIVector & remove(uint index) {_stlc::erase(_stlc::begin() + index); return *this;} /*! \brief Remove "count" elements by first index "index" and return reference to vector * \details Example: \snippet picontainers.cpp PIVector::remove_1 * \sa \a removeOne(), \a removeAll() */ PIVector & remove(uint index, uint count) {_stlc::erase(_stlc::begin() + index, _stlc::begin() + index + count); return *this;} /*! \brief Remove no more than one element equal "v" and return reference to vector * \details Example: \snippet picontainers.cpp PIVector::removeOne * \sa \a remove(), \a removeAll() */ PIVector & removeOne(const Type & v) {for (typename _stlc::iterator i = _stlc::begin(); i != _stlc::end(); ++i) if (v == *i) {_stlc::erase(i); return *this;} return *this;} /*! \brief Remove all elements equal "v" and return reference to vector * \details Example: \snippet picontainers.cpp PIVector::removeAll * \sa \a remove(), \a removeOne() */ */ PIVector & removeAll(const Type & v) {for (typename _stlc::iterator i = _stlc::begin(); i != _stlc::end(); ++i) if (v == *i) {_stlc::erase(i); --i;} return *this;} /*! \brief Insert element "t" after index "pos" and return reference to vector * \details Example: \snippet picontainers.cpp PIVector::insert_0 */ PIVector & insert(uint pos, const Type & t) {_stlc::insert(_stlc::begin() + pos, t); return *this;} /*! \brief Insert other vector "t" after index "pos" and return reference to vector * \details Example: \snippet picontainers.cpp PIVector::insert_1 */ PIVector & insert(uint pos, const PIVector & t) {_stlc::insert(_stlc::begin() + pos, t.begin(), t.end()); return *this;} /*! \brief Full access to element by index "index" * \details Example: \snippet picontainers.cpp PIVector::() * \sa \a at() */ Type & operator [](uint index); /*! \brief Read-only access to element by index "index" * \details Example: \snippet picontainers.cpp PIVector::()_c * \sa \a at() */ const Type & operator [](uint index) const; //! Add new element "t" at the end of vector and return reference to vector PIVector & operator <<(const Type & t) {_stlc::push_back(t); return *this;} //! Add vector "t" at the end of vector and return reference to vector PIVector & operator <<(const PIVector & t) {for (typename _stlc::const_iterator i = t.begin(); i != t.end(); i++) _stlc::push_back(*i); return *this;} //! Compare with vector "t" bool operator ==(const PIVector & t) {for (uint i = 0; i < _stlc::size(); ++i) if (t[i] != at(i)) return false; return true;} //! Compare with vector "t" 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 Type & 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(Type), (int(*)(const void * , const void * ))compare); return *this;} _CVector & fill(const Type & t) {_stlc::assign(_stlc::size(), t); return *this;} _CVector & pop_front() {_stlc::erase(_stlc::begin()); return *this;} _CVector & push_front(const Type & t) {_stlc::insert(_stlc::begin(), t); return *this;} Type take_front() {Type t(_stlc::front()); pop_front(); return t;} Type take_back() {Type 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 Type & 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 Type & 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 Type & 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 Type & 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 Type & v) const {for (uint i = 0; i < _stlc::size(); ++i) if (v == at(i)) return true; return false;} #endif }; /*! \brief Output operator for std::ostream * \relates PIVector * \details Example: \snippet picontainers.cpp PIVector::ostream<< */ 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;} /*! \brief Output operator for PICout * \relates PIVector * \details Example: \snippet picontainers.cpp PIVector::PICout<< */ 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;} template > class PIP_EXPORT PIList: public list { typedef PIList _CList; typedef list _stlc; public: PIList() {piMonitor.containers++;} PIList(const Type & value) {piMonitor.containers++; _stlc::resize(1, value);} PIList(const Type & v0, const Type & v1) {piMonitor.containers++; _stlc::push_back(v0); _stlc::push_back(v1);} PIList(const Type & v0, const Type & v1, const Type & v2) {piMonitor.containers++; _stlc::push_back(v0); _stlc::push_back(v1); _stlc::push_back(v2);} PIList(const Type & v0, const Type & v1, const Type & v2, const Type & v3) {piMonitor.containers++; _stlc::push_back(v0); _stlc::push_back(v1); _stlc::push_back(v2); _stlc::push_back(v3);} PIList(uint size, const Type & value = Type()) {piMonitor.containers++; _stlc::resize(size, value);} ~PIList() {piMonitor.containers--;} Type & operator [](uint index) {return (*this)[index];} Type & operator [](uint index) const {return (*this)[index];} const Type * data(uint index = 0) const {return &(*this)[index];} Type * data(uint index = 0) {return &(*this)[index];} int size_s() const {return static_cast(_stlc::size());} bool isEmpty() const {return _stlc::empty();} bool has(const Type & t) const {for (typename _stlc::const_iterator i = _stlc::begin(); i != _stlc::end(); ++i) if (t == *i) return true; return false;} int etries(const Type & t) const {int ec = 0; for (typename _stlc::const_iterator i = _stlc::begin(); i != _stlc::end(); ++i) if (t == *i) ++ec; return ec;} _CList & fill(const Type & t) {_stlc::assign(_stlc::size(), t); return *this;} _CList & remove(uint index) {_stlc::erase(_stlc::begin() + index); return *this;} _CList & remove(uint index, uint count) {_stlc::erase(_stlc::begin() + index, _stlc::begin() + index + count); return *this;} _CList & insert(uint pos, const Type & t) {_stlc::insert(_stlc::begin() + pos, t); return *this;} _CList & operator <<(const Type & t) {_stlc::push_back(t); return *this;} PIVector toVector() {PIVector v; for (typename _stlc::const_iterator i = _stlc::begin(); i != _stlc::end(); ++i) v << *i; return v;} }; template, typename Allocator = std::allocator > class PIP_EXPORT PISet: public set { typedef PISet _CSet; typedef set _stlc; public: PISet() {piMonitor.containers++;} PISet(const Type & value) {piMonitor.containers++; _stlc::resize(1, value);} PISet(const Type & v0, const Type & v1) {piMonitor.containers++; _stlc::insert(v0); _stlc::insert(v1);} PISet(const Type & v0, const Type & v1, const Type & v2) {piMonitor.containers++; _stlc::insert(v0); _stlc::insert(v1); _stlc::insert(v2);} PISet(const Type & v0, const Type & v1, const Type & v2, const Type & v3) {piMonitor.containers++; _stlc::insert(v0); _stlc::insert(v1); _stlc::insert(v2); _stlc::insert(v3);} ~PISet() {piMonitor.containers--;} int size_s() const {return static_cast(_stlc::size());} bool isEmpty() const {return _stlc::empty();} _CSet & remove(uint index) {_stlc::erase(_stlc::begin() + index); return *this;} _CSet & remove(uint index, uint count) {_stlc::erase(_stlc::begin() + index, _stlc::begin() + index + count); return *this;} _CSet & operator <<(const Type & t) {_stlc::insert(t); return *this;} bool operator [](const Type & t) {return _stlc::find(t);} PIVector toVector() {PIVector v; for (typename _stlc::const_iterator i = _stlc::begin(); i != _stlc::end(); ++i) v << *i; return v;} }; template class PIP_EXPORT PIStack: public PIVector { typedef PIStack _CStack; public: PIStack() {;} PIStack(const Type & value) {_CStack::resize(1, value);} PIStack(const Type & v0, const Type & v1) {_CStack::push_back(v0); _CStack::push_back(v1);} PIStack(const Type & v0, const Type & v1, const Type & v2) {_CStack::push_back(v0); _CStack::push_back(v1); _CStack::push_back(v2);} PIStack(const Type & v0, const Type & v1, const Type & v2, const Type & v3) {_CStack::push_back(v0); _CStack::push_back(v1); _CStack::push_back(v2); _CStack::push_back(v3);} _CStack & push(const Type & v) {_CStack::push_back(v); return *this;} Type pop() {Type t = Type(); if (_CStack::size() == 0) return t; t = _CStack::back(); _CStack::pop_back(); return t;} Type & top() {return _CStack::back();} const Type & top() const {return _CStack::back();} PIVector toVector() {PIVector v; for (typename _CStack::const_iterator i = _CStack::begin(); i != _CStack::end(); ++i) v << *i; return v;} }; template > class PIP_EXPORT PIDeque: public deque { typedef PIDeque _CDeque; typedef deque _stlc; public: PIDeque() {piMonitor.containers++;} PIDeque(const Type & value) {piMonitor.containers++; _stlc::resize(1, value);} PIDeque(const Type & v0, const Type & v1) {piMonitor.containers++; _stlc::push_back(v0); _stlc::push_back(v1);} PIDeque(const Type & v0, const Type & v1, const Type & v2) {piMonitor.containers++; _stlc::push_back(v0); _stlc::push_back(v1); _stlc::push_back(v2);} PIDeque(const Type & v0, const Type & v1, const Type & v2, const Type & v3) {piMonitor.containers++; _stlc::push_back(v0); _stlc::push_back(v1); _stlc::push_back(v2); _stlc::push_back(v3);} ~PIDeque() {piMonitor.containers--;} int size_s() const {return static_cast(_stlc::size());} bool isEmpty() const {return _stlc::empty();} bool has(const Type & t) const {for (typename _stlc::const_iterator i = _stlc::begin(); i != _stlc::end(); ++i) if (t == *i) return true; return false;} int etries(const Type & t) const {int ec = 0; for (typename _stlc::const_iterator i = _stlc::begin(); i != _stlc::end(); ++i) if (t == *i) ++ec; return ec;} _CDeque & operator <<(const Type & t) {_CDeque::push_back(t); return *this;} PIVector toVector() {PIVector v; for (typename _stlc::const_iterator i = _stlc::begin(); i != _stlc::end(); ++i) v << *i; return v;} }; template class PIP_EXPORT PIQueue: public PIDeque { typedef PIQueue _CQueue; public: PIQueue() {;} PIQueue(const Type & value) {_CQueue::resize(1, value);} PIQueue(const Type & v0, const Type & v1) {_CQueue::push_front(v0); _CQueue::push_front(v1);} PIQueue(const Type & v0, const Type & v1, const Type & v2) {_CQueue::push_front(v0); _CQueue::push_front(v1); _CQueue::push_front(v2);} PIQueue(const Type & v0, const Type & v1, const Type & v2, const Type & v3) {_CQueue::push_front(v0); _CQueue::push_front(v1); _CQueue::push_front(v2); _CQueue::push_front(v3);} _CQueue & enqueue(const Type & v) {_CQueue::push_front(v); return *this;} Type dequeue() {Type t = Type(); if (_CQueue::size() == 0) return t; t = _CQueue::back(); _CQueue::pop_back(); return t;} Type & head() {return _CQueue::back();} const Type & head() const {return _CQueue::back();} PIVector toVector() {PIVector v; for (typename _CQueue::const_iterator i = _CQueue::begin(); i != _CQueue::end(); ++i) v << *i; return v;} }; template class PIP_EXPORT PIPair { public: PIPair() {first = Type0(); second = Type1();} PIPair(const Type0 & value0, const Type1 & value1) {first = value0; second = value1;} Type0 first; Type1 second; }; template inline bool operator <(const PIPair & value0, const PIPair & value1) {return value0.first < value1.first;} template inline std::ostream & operator <<(std::ostream & s, const PIPair & v) {s << "(" << v.first << ", " << v.second << ")"; return s;} template inline PICout operator <<(PICout s, const PIPair & v) {s.space(); s.setControl(0, true); s << "(" << v.first << ", " << v.second << ")"; s.restoreControl(); return s;} template class PIP_EXPORT PIMap: public map { typedef PIMap _CMap; typedef map _stlc; typedef std::pair _stlpair; public: PIMap() {;} PIMap(const Key & key_, const Type & value_) {insert(key_, value_);} bool isEmpty() const {return _stlc::empty();} bool contains(const Key & key_) const {return _stlc::count(key_) > 0;} _CMap & insert(const Key & key_, const Type & value_) {_stlc::insert(_stlpair(key_, value_)); return *this;} _CMap & insert(PIPair entry_) {_stlc::insert(_stlpair(entry_.first, entry_.second)); return *this;} Key key(Type value_) const {for (typename _stlc::const_iterator i = _stlc::begin(); i != _stlc::end(); i++) if (i->second == value_) return i->first; return Key();} Type & value(const Key & key_) {typename _stlc::iterator it = _stlc::find(key_); if (it == _stlc::end()) it->second = Type(); return it->second;} Type value(const Key & key_) const {return _stlc::find(key_)->second;} }; template class PIP_EXPORT PIMultiMap: public multimap { typedef PIMultiMap _CMultiMap; typedef multimap _stlc; typedef std::pair _stlpair; public: PIMultiMap() {;} PIMultiMap(const Key & key_, const Type & value_) {insert(key_, value_);} _CMultiMap & insert(const Key & key_, const Type & value_) {_stlc::insert(_stlpair(key_, value_)); return *this;} _CMultiMap & insert(PIPair entry_) {_stlc::insert(_stlpair(entry_.first, entry_.second)); return *this;} bool isEmpty() const {return _stlc::empty();} bool contains(const Key & key_) const {return _stlc::count(key_) > 0;} Key key(Type value_) const {for (typename _stlc::const_iterator i = _stlc::begin(); i != _stlc::end(); i++) if (i->second == value_) return i->first; return Key();} PIVector keys(Type value_) const { PIVector ret; for (typename _stlc::const_iterator i = _stlc::begin(); i != _stlc::end(); i++) if (i->second == value_) ret << i->first; return ret; } Type & value(const Key & key_) {return _stlc::find(key_)->second;} Type value(const Key & key_) const {return _stlc::find(key_)->second;} PIVector values(const Key & key_) const { std::pair range = _stlc::equal_range(key_); PIVector ret; for (typename _stlc::const_iterator i = range.first; i != range.second; ++i) ret << i->second; return ret; } Type & operator [](const Key & key_) {if (!contains(key_)) return _stlc::insert(_stlpair(key_, Type()))->second; return _stlc::find(key_)->second;} Type operator [](const Key & key_) const {return _stlc::find(key_)->second;} }; #endif // PICONTAINERS_H