git-svn-id: svn://db.shs.com.ru/pip@400 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
175
src_main/containers/picontainers.cpp
Executable file
175
src_main/containers/picontainers.cpp
Executable file
@@ -0,0 +1,175 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Generic containers
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
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/>.
|
||||
*/
|
||||
|
||||
// * This class based on std::vector, expanding his functionality
|
||||
|
||||
/** \class PIVector
|
||||
* \brief Dynamic array of 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 this is stream operator <<.
|
||||
|
||||
* \fn PIVector::PIVector();
|
||||
* Contructs an empty vector
|
||||
|
||||
* \fn PIVector::PIVector(ullong size, const Type & value = Type());
|
||||
* \brief Contructs vector with size "size" filled elements "value"
|
||||
* \details Example: \snippet picontainers.cpp PIVector::PIVector
|
||||
|
||||
* \fn const Type & PIVector::at(ullong index) const;
|
||||
* \brief Read-only access to element by index "index"
|
||||
* \details Example: \snippet picontainers.cpp PIVector::at_c
|
||||
* \sa \a operator[]
|
||||
|
||||
* \fn Type & PIVector::at(ullong index);
|
||||
* \brief Full access to element by index "index"
|
||||
* \details Example: \snippet picontainers.cpp PIVector::at
|
||||
* \sa \a operator[]
|
||||
|
||||
* \fn const Type * PIVector::data(ullong index = 0) const;
|
||||
* \brief Read-only pointer to element by index "index"
|
||||
* \details Example: \snippet picontainers.cpp PIVector::data_c
|
||||
|
||||
* \fn Type * PIVector::data(ullong index = 0);
|
||||
* \brief Pointer to element by index "index"
|
||||
* \details Example: \snippet picontainers.cpp PIVector::data
|
||||
|
||||
* \fn ullong PIVector::size() const;
|
||||
* \brief Elements count
|
||||
|
||||
* \fn int PIVector::size_s() const;
|
||||
* \brief Elements count
|
||||
|
||||
* \fn bool PIVector::isEmpty() const;
|
||||
* \brief Return \c "true" if vector is empty, i.e. size = 0
|
||||
|
||||
* \fn bool PIVector::has(const Type & t) const;
|
||||
|
||||
* \fn bool PIVector::contains(const Type & v) const;
|
||||
* \brief Return \c "true" if vector has at least one element equal "t"
|
||||
|
||||
* \fn int PIVector::etries(const Type & t) const;
|
||||
* \brief Return how many times element "t" appears in vector
|
||||
|
||||
* \fn static int PIVector::compare_func(const Type * t0, const Type * t1);
|
||||
* \brief Standard compare function for type "Type". Return 0 if t0 = t1, -1 if t0 < t1 and 1 if t0 > t1.
|
||||
|
||||
* \fn void PIVector::resize(ullong size, const Type & new_type = Type());
|
||||
* \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()
|
||||
|
||||
* \fn PIVector<T> & PIVector::enlarge(ullong size);
|
||||
* \brief Increase vector size with "size" elements
|
||||
|
||||
* \fn void PIVector::clear();
|
||||
* \brief Clear vector. Equivalent to call <tt>"resize(0)"</tt>
|
||||
|
||||
* \fn PIVector<T> & PIVector::sort(CompareFunc compare = compare_func);
|
||||
* \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
|
||||
|
||||
* \fn PIVector<T> & PIVector::fill(const Type & t);
|
||||
* \brief Fill vector with elements "t" leave size is unchanged and return reference to vector
|
||||
* \details Example: \snippet picontainers.cpp PIVector::fill
|
||||
|
||||
* \fn Type & PIVector::back();
|
||||
* \brief Last element of the vector
|
||||
|
||||
* \fn const Type & PIVector::back() const;
|
||||
* \brief Last element of the vector
|
||||
|
||||
* \fn Type & PIVector::front();
|
||||
* \brief First element of the vector
|
||||
|
||||
* \fn const Type & PIVector::front() const;
|
||||
* \brief First element of the vector
|
||||
|
||||
* \fn PIVector<T> & PIVector::push_back(const Type & t);
|
||||
* \brief Add new element "t" at the end of vector and return reference to vector
|
||||
|
||||
* \fn PIVector<T> & PIVector::push_front(const Type & t);
|
||||
* \brief Add new element "t" at the beginning of vector and return reference to vector
|
||||
|
||||
* \fn PIVector<T> & PIVector::pop_back();
|
||||
* \brief Remove one element from the end of vector and return reference to vector
|
||||
|
||||
* \fn PIVector<T> & PIVector::pop_front();
|
||||
* \brief Remove one element from the beginning of vector and return reference to vector
|
||||
|
||||
* \fn Type PIVector::take_back();
|
||||
* \brief Remove one element from the end of vector and return it
|
||||
|
||||
* \fn Type PIVector::take_front();
|
||||
* \brief Remove one element from the beginning of vector and return it
|
||||
|
||||
* \fn PIVector<T> & PIVector::remove(uint index);
|
||||
* \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()
|
||||
|
||||
* \fn PIVector<T> & PIVector::remove(uint index, uint count);
|
||||
* \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()
|
||||
|
||||
* \fn PIVector<T> & PIVector::removeOne(const Type & v);
|
||||
* \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()
|
||||
|
||||
* \fn PIVector<T> & PIVector::removeAll(const Type & v);
|
||||
* \brief Remove all elements equal "v" and return reference to vector
|
||||
* \details Example: \snippet picontainers.cpp PIVector::removeAll
|
||||
* \sa \a remove(), \a removeOne()
|
||||
|
||||
* \fn PIVector<T> & PIVector::insert(uint pos, const Type & t);
|
||||
* \brief Insert element "t" after index "pos" and return reference to vector
|
||||
* \details Example: \snippet picontainers.cpp PIVector::insert_0
|
||||
|
||||
* \fn PIVector<T> & PIVector::insert(uint pos, const PIVector<T> & t);
|
||||
* \brief Insert other vector "t" after index "pos" and return reference to vector
|
||||
* \details Example: \snippet picontainers.cpp PIVector::insert_1
|
||||
|
||||
* \fn Type & PIVector::operator [](uint index);
|
||||
* \brief Full access to element by index "index"
|
||||
* \details Example: \snippet picontainers.cpp PIVector::()
|
||||
* \sa \a at()
|
||||
|
||||
* \fn const Type & PIVector::operator [](uint index) const;
|
||||
* \brief Read-only access to element by index "index"
|
||||
* \details Example: \snippet picontainers.cpp PIVector::()_c
|
||||
* \sa \a at()
|
||||
|
||||
* \fn PIVector<T> & PIVector::operator <<(const Type & t);
|
||||
* \brief Add new element "t" at the end of vector and return reference to vector
|
||||
|
||||
* \fn PIVector<T> & PIVector::operator <<(const PIVector<T> & t);
|
||||
* \brief Add vector "t" at the end of vector and return reference to vector
|
||||
|
||||
* \fn bool PIVector::operator ==(const PIVector<T> & t);
|
||||
* \brief Compare with vector "t"
|
||||
|
||||
* \fn bool PIVector::operator !=(const PIVector<T> & t);
|
||||
* \brief Compare with vector "t"
|
||||
|
||||
* */
|
||||
235
src_main/containers/picontainers.h
Executable file
235
src_main/containers/picontainers.h
Executable file
@@ -0,0 +1,235 @@
|
||||
/*! \file picontainers.h
|
||||
* \brief Generic containers
|
||||
*
|
||||
* This file declare all containers and useful macros
|
||||
* to use them
|
||||
*/
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Generic containers
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
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 PICONTAINERS_H
|
||||
#define PICONTAINERS_H
|
||||
|
||||
#include "picout.h"
|
||||
#ifdef PIP_DEBUG
|
||||
# include <cassert>
|
||||
#endif
|
||||
#ifndef assert
|
||||
# define assert(x)
|
||||
#endif
|
||||
#ifdef MAC_OS
|
||||
# include <stdlib.h>
|
||||
#else
|
||||
# include <malloc.h>
|
||||
#endif
|
||||
#include <string.h>
|
||||
#include <new>
|
||||
#ifndef PIP_MEMALIGN_BYTES
|
||||
# define PIP_MEMALIGN_BYTES (sizeof(void*)*4)
|
||||
#endif
|
||||
#ifdef WINDOWS
|
||||
# ifdef CC_GCC
|
||||
# define amalloc(s) __mingw_aligned_malloc(s, PIP_MEMALIGN_BYTES)
|
||||
# define afree(p) __mingw_aligned_free(p)
|
||||
# else
|
||||
# ifdef CC_VC
|
||||
# define amalloc(s) _aligned_malloc(s, PIP_MEMALIGN_BYTES)
|
||||
# define afree(p) _aligned_free(p)
|
||||
# endif
|
||||
# endif
|
||||
#else
|
||||
# define amalloc(s) aligned_alloc(PIP_MEMALIGN_BYTES, s)
|
||||
# define afree(p) free(p)
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef DOXYGEN
|
||||
|
||||
/*!\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
|
||||
*/
|
||||
# define piForeach(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
|
||||
*/
|
||||
# define piForeachC(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
|
||||
*/
|
||||
# define piForeachR(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
|
||||
*/
|
||||
# define piForeachCR(i,c)
|
||||
|
||||
/*!\brief Macro for break from any piForeach* loop
|
||||
* \details \warning C++ ordinary "break" doesn`t work inside piForeach*
|
||||
* loops! Always use "piBreak" instead!
|
||||
*/
|
||||
# define piBreak
|
||||
|
||||
#else
|
||||
|
||||
# define piBreak {_for._end = true; break;}
|
||||
|
||||
# define piForTimes(c) for(int _i##c = 0; _i##c < c; ++_i##c)
|
||||
|
||||
#ifdef CC_GCC
|
||||
|
||||
template<typename Type>
|
||||
class _PIForeach {
|
||||
public:
|
||||
_PIForeach(Type & t): _t(t), _break(false), _end(false) {_it = _t.begin();}
|
||||
typename Type::value_type _var;
|
||||
typename Type::iterator _it;
|
||||
Type & _t;
|
||||
bool _break, _end;
|
||||
inline bool isEnd() {return _it == _t.end();}
|
||||
inline void operator ++() {if (_end) _it = _t.end(); else _it++; _break = false;}
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
class _PIForeachR {
|
||||
public:
|
||||
_PIForeachR(Type & t): _t(t), _break(false), _end(false) {_rit = _t.rbegin();}
|
||||
typename Type::value_type _var;
|
||||
typename Type::reverse_iterator _rit;
|
||||
Type & _t;
|
||||
bool _break, _end;
|
||||
inline bool isEnd() {return _rit == _t.rend();}
|
||||
inline void operator ++() {if (_end) _rit = _t.rend(); else _rit++; _break = false;}
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
class _PIForeachC {
|
||||
public:
|
||||
_PIForeachC(const Type & t): _t(t), _break(false), _end(false) {_it = _t.begin();}
|
||||
typename Type::value_type _var;
|
||||
typename Type::const_iterator _it;
|
||||
const Type & _t;
|
||||
bool _break, _end;
|
||||
inline bool isEnd() {return _it == _t.end();}
|
||||
inline void operator ++() {if (_end) _it = _t.end(); else _it++; _break = false;}
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
class _PIForeachCR {
|
||||
public:
|
||||
_PIForeachCR(const Type & t): _t(t), _break(false), _end(false) {_rit = _t.rbegin();}
|
||||
typename Type::value_type _var;
|
||||
typename Type::const_reverse_iterator _rit;
|
||||
const Type & _t;
|
||||
bool _break, _end;
|
||||
inline bool isEnd() {return _rit == _t.rend();}
|
||||
inline void operator ++() {if (_end) _rit = _t.rend(); else _rit++; _break = false;}
|
||||
};
|
||||
|
||||
#define piForeach(i,c) for(_PIForeach<typeof(c)> _for(c); !_for.isEnd(); ++_for) \
|
||||
for(i(*_for._it); !_for._break; _for._break = true)
|
||||
#define piForeachR(i,c) for(_PIForeachR<typeof(c)> _for(c); !_for.isEnd(); ++_for) \
|
||||
for(i(*_for._rit); !_for._break; _for._break = true)
|
||||
#define piForeachA(i,c) for(_PIForeach<typeof(c)> _for(c); !_for.isEnd(); ++_for) \
|
||||
for(typeof(_for._var) & i(*_for._it); !_for._break; _for._break = true)
|
||||
#define piForeachAR(i,c) for(_PIForeachR<typeof(c)> _for(c); !_for.isEnd(); ++_for) \
|
||||
for(typeof(_for._var) & i(*_for._rit); !_for._break; _for._break = true)
|
||||
#define piForeachC(i,c) for(_PIForeachC<typeof(c)> _for(c); !_for.isEnd(); ++_for) \
|
||||
for(const i(*_for._it); !_for._break; _for._break = true)
|
||||
#define piForeachCR(i,c) for(_PIForeachCR<typeof(c)> _for(c); !_for.isEnd(); ++_for) \
|
||||
for(const i(*_for._rit); !_for._break; _for._break = true)
|
||||
#define piForeachCA(i,c) for(_PIForeachC<typeof(c)> _for(c); !_for.isEnd(); ++_for) \
|
||||
for(const typeof(_for._var) & i(*_for._it); !_for._break; _for._break = true)
|
||||
#define piForeachCAR(i,c) for(_PIForeachCR<typeof(c)> _for(c); !_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, _end;};
|
||||
|
||||
template<typename Type>
|
||||
class _PIForeach: public _PIForeachBase {
|
||||
public:
|
||||
_PIForeach(Type & t, bool i = false): _break(false), _end(false), _t(t), _inv(i) {if (_inv) _rit = _t.rbegin(); else _it = _t.begin();}
|
||||
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) {if (_end) _rit = _t.rend(); else _rit++;} else {if (_end) _it = _t.end(); else _it++;} _break = false;}
|
||||
};
|
||||
|
||||
template<typename Type>
|
||||
class _PIForeachC: public _PIForeachBase {
|
||||
public:
|
||||
_PIForeachC(const Type & t, bool i = false): _break(false), _end(false), _t(t), _inv(i) {if (_inv) _rit = _t.rbegin(); else _it = _t.begin();}
|
||||
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) {if (_end) _rit = _t.rend(); else _rit++;} else {if (_end) _it = _t.end(); else _it++;} _break = false;}
|
||||
};
|
||||
|
||||
template <typename T> inline _PIForeach<T> _PIForeachNew(T & t, bool i = false) {return _PIForeach<T>(t, i);}
|
||||
template <typename T> inline _PIForeach<T> * _PIForeachCast(_PIForeachBase & c, T & ) {return static_cast<_PIForeach<T> * >(&c);}
|
||||
|
||||
template <typename T> inline _PIForeachC<T> _PIForeachNewC(const T & t, bool i = false) {return _PIForeachC<T>(t, i);}
|
||||
template <typename T> inline _PIForeachC<T> * _PIForeachCastC(_PIForeachBase & c, const T & ) {return static_cast<_PIForeachC<T> * >(&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
|
||||
|
||||
#endif // DOXYGEN
|
||||
|
||||
|
||||
#endif // PICONTAINERS_H
|
||||
31
src_main/containers/picontainersmodule.h
Normal file
31
src_main/containers/picontainersmodule.h
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Module includes
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
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 PICONTAINERSMODULE_H
|
||||
#define PICONTAINERSMODULE_H
|
||||
|
||||
#include "pivector.h"
|
||||
#include "pideque.h"
|
||||
#include "pimap.h"
|
||||
#include "piqueue.h"
|
||||
#include "piset.h"
|
||||
#include "pilist.h"
|
||||
#include "pistack.h"
|
||||
|
||||
#endif // PICONTAINERSMODULE_H
|
||||
539
src_main/containers/pideque.h
Executable file
539
src_main/containers/pideque.h
Executable file
@@ -0,0 +1,539 @@
|
||||
/*! \file pideque.h
|
||||
* \brief Dynamic array of any type
|
||||
*
|
||||
* This file declares PIDeque
|
||||
*/
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Dynamic array of any type
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
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 PIDEQUE_H
|
||||
#define PIDEQUE_H
|
||||
|
||||
#include "picontainers.h"
|
||||
#include "piintrospection_proxy.h"
|
||||
|
||||
|
||||
#if !defined(PIP_CONTAINERS_STL) || defined(DOXYGEN)
|
||||
|
||||
|
||||
template <typename T>
|
||||
class PIDeque {
|
||||
public:
|
||||
PIDeque(): pid_data(0), pid_size(0), pid_rsize(0), pid_start(0) {
|
||||
PIINTROSPECTION_CONTAINER_NEW()
|
||||
//printf("new vector 1 %p (%s) ... !{\n", this, typeid(T).name());
|
||||
//printf("(s=%d, d=%p) }!\n", int(pid_size), pid_data);
|
||||
}
|
||||
PIDeque(const PIDeque<T> & other): pid_data(0), pid_size(0), pid_rsize(0), pid_start(0) {
|
||||
PIINTROSPECTION_CONTAINER_NEW()
|
||||
//printf("new vector 2 %p (%s) ... !{\n", this, typeid(T).name());
|
||||
alloc(other.pid_size, true);
|
||||
newT(pid_data + pid_start, other.pid_data + other.pid_start, pid_size);
|
||||
//printf("(s=%d, d=%p) }!\n", int(pid_size), pid_data);
|
||||
}
|
||||
PIDeque(const T * data, size_t size): pid_data(0), pid_size(0), pid_rsize(0), pid_start(0) {
|
||||
PIINTROSPECTION_CONTAINER_NEW()
|
||||
//printf("new vector 2 %p (%s) ... !{\n", this, typeid(T).name());
|
||||
alloc(size, true);
|
||||
newT(pid_data + pid_start, data, pid_size);
|
||||
//printf("(s=%d, d=%p) }!\n", int(pid_size), pid_data);
|
||||
}
|
||||
PIDeque(size_t pid_size, const T & f = T()): pid_data(0), pid_size(0), pid_rsize(0), pid_start(0) {
|
||||
PIINTROSPECTION_CONTAINER_NEW()
|
||||
//printf("new vector 3 %p (%s) ... !{\n", this, typeid(T).name());
|
||||
resize(pid_size, f);
|
||||
//printf("(s=%d, d=%p) }!\n", int(pid_size), pid_data);
|
||||
}
|
||||
~PIDeque() {
|
||||
PIINTROSPECTION_CONTAINER_DELETE()
|
||||
PIINTROSPECTION_CONTAINER_FREE((pid_rsize)*sizeof(T))
|
||||
//printf("delete deque %p (%s) (s=%d, rs=%d, st=%d, d=%p) ... ~{\n", this, typeid(T).name(), int(pid_size), int(pid_rsize), int(pid_start), pid_data);
|
||||
deleteT(pid_data + pid_start, pid_size);
|
||||
dealloc();
|
||||
//deleteRaw(pid_tdata);
|
||||
_reset();
|
||||
//printf("}~\n");
|
||||
}
|
||||
|
||||
PIDeque<T> & operator =(const PIDeque<T> & other) {
|
||||
if (this == &other) return *this;
|
||||
deleteT(pid_data + pid_start, pid_size);
|
||||
alloc(other.pid_size, true);
|
||||
newT(pid_data + pid_start, other.pid_data + other.pid_start, pid_size);
|
||||
return *this;
|
||||
}
|
||||
|
||||
typedef T value_type;
|
||||
|
||||
class iterator {
|
||||
friend class PIDeque<T>;
|
||||
private:
|
||||
iterator(PIDeque<T> * v, size_t p): parent(v), pos(p) {}
|
||||
PIDeque<T> * parent;
|
||||
size_t pos;
|
||||
public:
|
||||
iterator(): parent(0), pos(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 PIDeque<T>;
|
||||
private:
|
||||
const_iterator(const PIDeque<T> * v, size_t p): parent(v), pos(p) {}
|
||||
const PIDeque<T> * parent;
|
||||
size_t pos;
|
||||
public:
|
||||
const_iterator(): parent(0), pos(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 PIDeque<T>;
|
||||
private:
|
||||
reverse_iterator(PIDeque<T> * v, size_t p): parent(v), pos(p) {}
|
||||
PIDeque<T> * parent;
|
||||
size_t pos;
|
||||
public:
|
||||
reverse_iterator(): parent(0), pos(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 PIDeque<T>;
|
||||
private:
|
||||
const_reverse_iterator(const PIDeque<T> * v, size_t p): parent(v), pos(p) {}
|
||||
const PIDeque<T> * parent;
|
||||
size_t pos;
|
||||
public:
|
||||
const_reverse_iterator(): parent(0), pos(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, pid_size);}
|
||||
const_iterator begin() const {return const_iterator(this, 0);}
|
||||
const_iterator end() const {return const_iterator(this, pid_size);}
|
||||
reverse_iterator rbegin() {return reverse_iterator(this, pid_size - 1);}
|
||||
reverse_iterator rend() {return reverse_iterator(this, -1);}
|
||||
const_reverse_iterator rbegin() const {return const_reverse_iterator(this, pid_size - 1);}
|
||||
const_reverse_iterator rend() const {return const_reverse_iterator(this, -1);}
|
||||
|
||||
size_t size() const {return pid_size;}
|
||||
ssize_t size_s() const {return pid_size;}
|
||||
size_t length() const {return pid_size;}
|
||||
size_t capacity() const {return pid_rsize;}
|
||||
size_t _start() const {return pid_start;}
|
||||
bool isEmpty() const {return (pid_size == 0);}
|
||||
|
||||
T & operator [](size_t index) {return pid_data[pid_start + index];}
|
||||
T & at(size_t index) {return pid_data[pid_start + index];}
|
||||
const T & operator [](size_t index) const {return pid_data[pid_start + index];}
|
||||
const T & at(size_t index) const {return pid_data[pid_start + index];}
|
||||
T & back() {return pid_data[pid_start + pid_size - 1];}
|
||||
const T & back() const {return pid_data[pid_start + pid_size - 1];}
|
||||
T & front() {return pid_data[pid_start];}
|
||||
const T & front() const {return pid_data[pid_start];}
|
||||
bool operator ==(const PIDeque<T> & t) const {if (pid_size != t.pid_size) return false; for (size_t i = 0; i < pid_size; ++i) if (t[i] != (*this)[i]) return false; return true;}
|
||||
bool operator !=(const PIDeque<T> & t) const {if (pid_size != t.pid_size) return true; for (size_t i = 0; i < pid_size; ++i) if (t[i] != (*this)[i]) return true; return false;}
|
||||
bool contains(const T & v) const {for (size_t i = pid_start; i < pid_start + pid_size; ++i) if (v == pid_data[i]) return true; return false;}
|
||||
int etries(const T & v) const {int ec = 0; for (size_t i = pid_start; i < pid_start + pid_size; ++i) if (v == pid_data[i]) ++ec; return ec;}
|
||||
|
||||
T * data(size_t index = 0) {return &(pid_data[pid_start + index]);}
|
||||
const T * data(size_t index = 0) const {return &(pid_data[pid_start + index]);}
|
||||
PIDeque<T> & clear() {resize(0); return *this;}
|
||||
PIDeque<T> & fill(const T & f = T()) {
|
||||
//if (sizeof(T) == 1) memset(pid_data, f, pid_size);
|
||||
deleteT(pid_data + pid_start, pid_size);
|
||||
//zeroRaw(pid_data, pid_size);
|
||||
for (size_t i = pid_start; i < pid_start + pid_size; ++i)
|
||||
elementNew(pid_data + i, f);
|
||||
return *this;
|
||||
}
|
||||
PIDeque<T> & assign(const T & f = T()) {return fill(f);}
|
||||
PIDeque<T> & assign(size_t new_size, const T & f) {resize(new_size); return fill(f);}
|
||||
PIDeque<T> & resize(size_t new_size, const T & f = T()) {
|
||||
if (new_size < pid_size) {
|
||||
deleteT(&(pid_data[new_size + pid_start]), pid_size - new_size);
|
||||
pid_size = new_size;
|
||||
}
|
||||
if (new_size > pid_size) {
|
||||
size_t os = pid_size;
|
||||
alloc(new_size, true);
|
||||
//if (sizeof(T) == 1) memset(&(pid_data[os]), f, ds);
|
||||
//zeroRaw(&(pid_data[os]), new_size - os);
|
||||
PIINTROSPECTION_CONTAINER_USED((new_size-os)*sizeof(T))
|
||||
for (size_t i = os + pid_start; i < new_size + pid_start; ++i) elementNew(pid_data + i, f);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
PIDeque<T> & reserve(size_t new_size) {
|
||||
if (new_size <= pid_rsize) return *this;
|
||||
size_t os = pid_size;
|
||||
alloc(new_size, true);
|
||||
pid_size = os;
|
||||
return *this;
|
||||
}
|
||||
|
||||
PIDeque<T> & insert(size_t index, const T & v = T()) {
|
||||
bool dir = pid_rsize <= 2 ? true : (index >= pid_rsize / 2 ? true : false);
|
||||
//piCout << "insert" << dir << index << pid_size << pid_rsize << pid_start << "!<";
|
||||
if (dir) {
|
||||
alloc(pid_size + 1, true);
|
||||
if (index < pid_size - 1) {
|
||||
size_t os = pid_size - index - 1;
|
||||
memmove(&(pid_data[index + pid_start + 1]), &(pid_data[index + pid_start]), os * sizeof(T));
|
||||
}
|
||||
} else {
|
||||
alloc(pid_size + 1, false, -1);
|
||||
//piCout << "insert front" << pid_size << pid_rsize << pid_start << "!<";
|
||||
if (index > 0)
|
||||
memmove(&(pid_data[pid_start]), &(pid_data[pid_start + 1]), index * sizeof(T));
|
||||
}
|
||||
//piCout << "insert" << pid_start << index << (pid_start + ssize_t(index)) << pid_size << ">!";
|
||||
PIINTROSPECTION_CONTAINER_USED(sizeof(T))
|
||||
elementNew(pid_data + pid_start + index, v);
|
||||
return *this;
|
||||
}
|
||||
PIDeque<T> & insert(size_t index, const PIDeque<T> & other) {
|
||||
if (other.isEmpty()) return *this;
|
||||
bool dir = pid_rsize <= 2 ? true : (index >= pid_rsize / 2 ? true : false);
|
||||
//piCout << this << "insert" << dir << index << pid_size << pid_rsize << pid_start << " <- " << other.size() << "!<";
|
||||
if (dir) {
|
||||
ssize_t os = pid_size - index;
|
||||
alloc(pid_size + other.pid_size, true);
|
||||
if (os > 0)
|
||||
memmove(&(pid_data[index + pid_start + other.pid_size]), &(pid_data[index + pid_start]), os * sizeof(T));
|
||||
} else {
|
||||
//if (pid_start < other.pid_size) pid_start = 0;
|
||||
//piCout << this << " insert offseted start ba" << pid_start << pid_size << pid_rsize;
|
||||
alloc(pid_size + other.pid_size, false, -other.pid_size);
|
||||
//piCout << this << " insert offseted start aa" << pid_start << pid_size << pid_rsize;
|
||||
//piCout << this << " insert front" << pid_size << pid_rsize << pid_start << "!<";
|
||||
if (index > 0)
|
||||
memmove(&(pid_data[pid_start]), &(pid_data[pid_start + other.pid_size]), index * sizeof(T));
|
||||
}
|
||||
//piCout << this << "insert" << pid_start << index << (pid_start + ssize_t(index)) << pid_size << ">!";
|
||||
newT(pid_data + pid_start + index, other.pid_data + other.pid_start, other.pid_size);
|
||||
return *this;
|
||||
}
|
||||
|
||||
PIDeque<T> & remove(size_t index, size_t count = 1) {
|
||||
if (count == 0) return *this;
|
||||
if (index + count >= pid_size) {
|
||||
resize(index);
|
||||
return *this;
|
||||
}
|
||||
size_t os = pid_size - index - count;
|
||||
deleteT(&(pid_data[index + pid_start]), count);
|
||||
if (os <= index) {
|
||||
//if (true) {
|
||||
if (os > 0) memmove(&(pid_data[index + pid_start]), &(pid_data[index + pid_start + count]), os * sizeof(T));
|
||||
} else {
|
||||
if (index > 0) memmove(&(pid_data[pid_start + count]), &(pid_data[pid_start]), index * sizeof(T));
|
||||
pid_start += count;
|
||||
}
|
||||
pid_size -= count;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void swap(PIDeque<T> & other) {
|
||||
piSwap<T*>(pid_data, other.pid_data);
|
||||
piSwap<size_t>(pid_size, other.pid_size);
|
||||
piSwap<size_t>(pid_rsize, other.pid_rsize);
|
||||
piSwap<size_t>(pid_start, other.pid_start);
|
||||
}
|
||||
|
||||
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);}
|
||||
PIDeque<T> & sort(CompareFunc compare = compare_func) {piqsort(pid_data + pid_start, pid_size, sizeof(T), (int(*)(const void * , const void * ))compare); return *this;}
|
||||
|
||||
PIDeque<T> & enlarge(llong pid_size) {llong ns = size_s() + pid_size; if (ns <= 0) clear(); else resize(size_t(ns)); return *this;}
|
||||
|
||||
PIDeque<T> & removeOne(const T & v) {for (size_t i = 0; i < pid_size; ++i) if (pid_data[i + pid_start] == v) {remove(i); return *this;} return *this;}
|
||||
PIDeque<T> & removeAll(const T & v) {for (ssize_t i = 0; i < ssize_t(pid_size); ++i) if (pid_data[i + pid_start] == v) {remove(i); --i;} return *this;}
|
||||
|
||||
PIDeque<T> & push_back(const T & v) {alloc(pid_size + 1, true); PIINTROSPECTION_CONTAINER_USED(sizeof(T)); elementNew(pid_data + pid_start + pid_size - 1, v); return *this;}
|
||||
PIDeque<T> & append(const T & v) {return push_back(v);}
|
||||
PIDeque<T> & operator <<(const T & v) {return push_back(v);}
|
||||
PIDeque<T> & operator <<(const PIDeque<T> & t) {
|
||||
size_t ps = pid_size;
|
||||
alloc(pid_size + t.pid_size, true);
|
||||
newT(pid_data + ps + pid_start, t.pid_data + t.pid_start, t.pid_size);
|
||||
return *this;
|
||||
}
|
||||
|
||||
PIDeque<T> & push_front(const T & v) {insert(0, v); return *this;}
|
||||
PIDeque<T> & prepend(const T & v) {return push_front(v);}
|
||||
|
||||
PIDeque<T> & pop_back() {if (pid_size == 0) return *this; resize(pid_size - 1); return *this;}
|
||||
PIDeque<T> & pop_front() {if (pid_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;}
|
||||
|
||||
template <typename ST>
|
||||
PIDeque<ST> toType() const {PIDeque<ST> ret(pid_size); for (uint i = 0; i < pid_size; ++i) ret[i] = ST(pid_data[i + pid_start]); return ret;}
|
||||
|
||||
private:
|
||||
void _reset() {pid_size = pid_rsize = pid_start = 0; pid_data = 0;}
|
||||
/*void * qmemmove(void * dst, void * src, size_t size) {
|
||||
if (piAbs<ssize_t>(ssize_t(dst) - ssize_t(src)) >= size)
|
||||
memcpy(dst, src, size);
|
||||
else {
|
||||
char * tb = new char[size];
|
||||
memcpy(tb, src, size);
|
||||
memcpy(dst, tb, size);
|
||||
delete tb;
|
||||
}
|
||||
return dst;
|
||||
}*/
|
||||
inline size_t asize(ssize_t s) {
|
||||
if (s <= 0) return 0;
|
||||
if (pid_rsize + pid_rsize >= size_t(s) && pid_rsize < size_t(s))
|
||||
return pid_rsize + pid_rsize;
|
||||
ssize_t t = 0, s_ = s - 1;
|
||||
//printf("asize .. %p rs=%d ns=%d s_=%d t=%d \n", this, pid_rsize, s, s_, t);
|
||||
while (s_ >> t) {
|
||||
++t;
|
||||
//printf("asize ++ %p rs=%d ns=%d s_=%d t=%d \n", this, pid_rsize, s, s_, t);
|
||||
}
|
||||
//printf("asize ok %p rs=%d ns=%d s_=%d t=%d \n", this, pid_rsize, s, s_, t);
|
||||
return (1 << t);
|
||||
}
|
||||
inline void newT(T * dst, const T * src, size_t s) {
|
||||
PIINTROSPECTION_CONTAINER_USED(s*sizeof(T))
|
||||
for (size_t i = 0; i < s; ++i)
|
||||
elementNew(dst + i, src[i]);
|
||||
}
|
||||
static T * newRaw(size_t s) {
|
||||
//std::cout << std::dec << " ![("<<this<<")newRaw " << s << " elements ... <" << std::endl;
|
||||
//uchar * ret = new uchar[s * sizeof(T)];
|
||||
uchar * ret = (uchar*)(malloc(s * sizeof(T)));//new uchar[];
|
||||
//zeroRaw((T*)ret, s);
|
||||
//std::cout << std::hex << " > (new 0x" << (llong)ret << ") ok]!" << std::endl;
|
||||
return (T*)ret;
|
||||
}
|
||||
/*void reallocRawTemp(size_t s) {
|
||||
if (pid_tdata == 0) pid_tdata = (T*)(malloc(s * sizeof(T)));
|
||||
else pid_tdata = (T*)(realloc(pid_tdata, s * sizeof(T)));
|
||||
}*/
|
||||
inline void deleteT(T * d, size_t sz) {
|
||||
PIINTROSPECTION_CONTAINER_UNUSED(sz*sizeof(T))
|
||||
//std::cout << " ~[("<<this<<")deleteT " << std::dec << sz << " elements " << " start " << pid_start << std::hex << " 0x" << (llong)d << " ... <" << std::endl;
|
||||
if ((uchar*)d != 0) {
|
||||
for (size_t i = 0; i < sz; ++i)
|
||||
elementDelete(d[i]);
|
||||
//zeroRaw(d, sz);
|
||||
}
|
||||
//cout << " > ok]~" << endl;
|
||||
}
|
||||
static void deleteRaw(T *& d) {
|
||||
//cout << " ~[("<<this<<")deleteRaw " << std::dec << pid_rsize << " elements " << std::hex << "0x" << (llong)d << " ... <\n" << endl;
|
||||
if ((uchar*)d != 0) free((uchar*)d);
|
||||
d = 0;
|
||||
//cout << " > ok]~" << endl;
|
||||
}
|
||||
static void zeroRaw(T * d, size_t 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;
|
||||
}
|
||||
inline void elementNew(T * to, const T & from) {new(to)T(from);}
|
||||
inline void elementDelete(T & from) {from.~T();}
|
||||
void dealloc() {deleteRaw(pid_data);}
|
||||
inline void checkMove(bool direction) {
|
||||
if (pid_size >= 4) {
|
||||
if (pid_size < pid_rsize / 6) {
|
||||
/*if (direction) {
|
||||
if (pid_start >= 4 && pid_start > pid_size + pid_size && pid_start > pid_rsize / 2) {
|
||||
piCout << (int)this << "checkMove" << direction << pid_start << (int)pid_data << pid_rsize << pid_size;
|
||||
piCout << (int)this << "move from" << pid_start << "to" << pid_size << "," << (int)pid_data << pid_rsize << pid_size;
|
||||
memmove(pid_data + pid_size, pid_data + pid_start, pid_size * sizeof(T));
|
||||
pid_start = pid_size;
|
||||
}
|
||||
} else {
|
||||
if (ssize_t(pid_start) < ssize_t(pid_rsize) - pid_size - pid_size && ssize_t(pid_start) < ssize_t(pid_rsize / 2) - pid_size) {
|
||||
piCout << (int)this << "checkMove" << direction << pid_start << (int)pid_data << pid_rsize << pid_size;
|
||||
piCout << (int)this << "move from" << pid_start << "to" << (ssize_t(pid_rsize) - pid_size) << "," << (int)pid_data << pid_rsize << pid_size;
|
||||
memmove(pid_data + ssize_t(pid_rsize) - pid_size - pid_size, pid_data + pid_start, pid_size * sizeof(T));
|
||||
pid_start = ssize_t(pid_rsize) - pid_size - pid_size;
|
||||
}
|
||||
}*/
|
||||
//printf("(%p) check move st=%d sz=%d rs=%d\n", this, pid_start, pid_size, pid_rsize);
|
||||
if (pid_start < ssize_t(pid_size + pid_size) || pid_start > (ssize_t(pid_rsize) - ssize_t(pid_size) - ssize_t(pid_size))) {
|
||||
ssize_t ns = (pid_rsize - pid_size) / 2;
|
||||
if (pid_start != ns) {
|
||||
//printf("(%p) move %d -> %d\n", this, pid_start, ns);
|
||||
memmove(pid_data + ns, pid_data + pid_start, pid_size * sizeof(T));
|
||||
pid_start = ns;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
ssize_t ns = (pid_rsize - pid_size) / 2;
|
||||
if (pid_start != ns) {
|
||||
//printf("(%p) move %d -> %d\n", this, pid_start, ns);
|
||||
memmove(pid_data + ns, pid_data + pid_start, pid_size * sizeof(T));
|
||||
pid_start = ns;
|
||||
}
|
||||
}
|
||||
}
|
||||
inline void alloc(size_t new_size, bool direction, ssize_t start_offset = 0) { // direction == true -> alloc forward
|
||||
if (direction) {
|
||||
if (pid_start + new_size <= pid_rsize) {
|
||||
pid_size = new_size;
|
||||
checkMove(direction);
|
||||
return;
|
||||
}
|
||||
pid_size = new_size;
|
||||
size_t as = asize(pid_start + new_size);
|
||||
if (as != pid_rsize) {
|
||||
//printf("(%p) realloc %d -> %d (%p)\n", this, pid_rsize, as, pid_data);
|
||||
PIINTROSPECTION_CONTAINER_ALLOC((as-pid_rsize)*sizeof(T))
|
||||
T * p_d = (T*)(realloc(pid_data, as*sizeof(T)));
|
||||
assert(p_d);
|
||||
pid_data = p_d;
|
||||
pid_rsize = as;
|
||||
//printf("(%p) realloc done (%p)\n", this, pid_data);
|
||||
}
|
||||
} else {
|
||||
size_t as;
|
||||
//piCout << "INS ba" << *this;
|
||||
if (pid_start + start_offset < 0)
|
||||
as = asize(pid_rsize - start_offset);
|
||||
else as = pid_rsize;
|
||||
//printf("%X alloc %d %d\n", this, pid_rsize, start_offset);
|
||||
//printf("%X alloc %d %d %d %d %d %d\n", this, new_size, pid_size, pid_rsize, as, pid_start, start_offset);
|
||||
if (as > pid_rsize) {
|
||||
//printf("%X alloc new size %d\n", this, as);
|
||||
//cout << std::hex << " ![("<<this<<")realloc " << pid_data << " data ... <\n" << endl;
|
||||
T * td = newRaw(as);
|
||||
ssize_t ns = pid_start + as - pid_rsize;
|
||||
//printf("%X pid_start ost=%d ors=%d nst=%d nrs=%d\n", this, pid_start, pid_rsize, ns, as);
|
||||
PIINTROSPECTION_CONTAINER_ALLOC((as-pid_rsize)*sizeof(T))
|
||||
if (pid_rsize > 0 && pid_data != 0) {
|
||||
//printf("%X copy from %p + %d to %p + %d %d el\n", this, pid_data, pid_start, td, ns, pid_size);
|
||||
memcpy(td + ns, pid_data + pid_start, pid_size * sizeof(T));
|
||||
deleteRaw(pid_data);
|
||||
}
|
||||
pid_data = td;
|
||||
pid_rsize = as;
|
||||
pid_start = ns;
|
||||
//piCout << "INS aa" << *this;
|
||||
}
|
||||
pid_start += start_offset;
|
||||
pid_size = new_size;
|
||||
checkMove(direction);
|
||||
}
|
||||
//checkMove(direction);
|
||||
//printf("%X alloc new start %d\n", this, pid_start);
|
||||
}
|
||||
|
||||
T * pid_data;
|
||||
volatile size_t pid_size, pid_rsize;
|
||||
volatile ssize_t pid_start;
|
||||
};
|
||||
|
||||
#define __PIDEQUE_SIMPLE_TYPE__(T) \
|
||||
template<> inline void PIDeque<T>::newT(T * dst, const T * src, size_t s) {PIINTROSPECTION_CONTAINER_USED(s*sizeof(T)); memcpy(dst, src, s * sizeof(T));} \
|
||||
template<> inline void PIDeque<T>::deleteT(T * d, size_t sz) {PIINTROSPECTION_CONTAINER_UNUSED(sz*sizeof(T));} \
|
||||
template<> inline void PIDeque<T>::elementNew(T * to, const T & from) {(*to) = from;} \
|
||||
template<> inline void PIDeque<T>::elementDelete(T & from) {;}
|
||||
|
||||
#else
|
||||
|
||||
|
||||
template<typename Type, typename Allocator = std::allocator<Type> >
|
||||
class PIP_EXPORT PIDeque: public deque<Type, Allocator> {
|
||||
typedef PIDeque<Type, Allocator> _CDeque;
|
||||
typedef deque<Type, Allocator> _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<int>(_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;}
|
||||
PIDeque<Type> toVector() {PIDeque<Type> v; for (typename _stlc::const_iterator i = _stlc::begin(); i != _stlc::end(); ++i) v << *i; return v;}
|
||||
};
|
||||
|
||||
|
||||
#define __PIDEQUE_SIMPLE_FUNCTIONS__(T)
|
||||
|
||||
|
||||
#endif
|
||||
__PIDEQUE_SIMPLE_TYPE__(bool)
|
||||
__PIDEQUE_SIMPLE_TYPE__(char)
|
||||
__PIDEQUE_SIMPLE_TYPE__(uchar)
|
||||
__PIDEQUE_SIMPLE_TYPE__(short)
|
||||
__PIDEQUE_SIMPLE_TYPE__(ushort)
|
||||
__PIDEQUE_SIMPLE_TYPE__(int)
|
||||
__PIDEQUE_SIMPLE_TYPE__(uint)
|
||||
__PIDEQUE_SIMPLE_TYPE__(long)
|
||||
__PIDEQUE_SIMPLE_TYPE__(ulong)
|
||||
__PIDEQUE_SIMPLE_TYPE__(llong)
|
||||
__PIDEQUE_SIMPLE_TYPE__(ullong)
|
||||
__PIDEQUE_SIMPLE_TYPE__(float)
|
||||
__PIDEQUE_SIMPLE_TYPE__(double)
|
||||
__PIDEQUE_SIMPLE_TYPE__(ldouble)
|
||||
|
||||
|
||||
#ifdef PIP_STD_IOSTREAM
|
||||
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;}
|
||||
#endif
|
||||
|
||||
template<typename T>
|
||||
inline PICout operator <<(PICout s, const PIDeque<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 // PIDEQUE_H
|
||||
45
src_main/containers/pilist.h
Normal file
45
src_main/containers/pilist.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#ifndef PILIST_H
|
||||
#define PILIST_H
|
||||
#include "pibase.h"
|
||||
#include <list>
|
||||
|
||||
template<typename Type, typename Allocator = std::allocator<Type> >
|
||||
class PIP_EXPORT PIList: public std::list<Type, Allocator> {
|
||||
typedef PIList<Type, Allocator> _CList;
|
||||
typedef std::list<Type, Allocator> _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<int>(_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<Type> toVector() const {PIVector<Type> v; for (typename _stlc::const_iterator i = _stlc::begin(); i != _stlc::end(); ++i) v << *i; return v;}
|
||||
};
|
||||
|
||||
//! \relatesalso PIByteArray \brief Store operator
|
||||
template<typename T> inline PIByteArray & operator <<(PIByteArray & s, const PIList<T> & v);
|
||||
//! \relatesalso PIByteArray \brief Restore operator
|
||||
template<typename T> inline PIByteArray & operator >>(PIByteArray & s, PIList<T> & v);
|
||||
|
||||
template<typename T>
|
||||
inline PIByteArray & operator <<(PIByteArray & s, const PIList<T> & v) {s << int(v.size_s()); for (uint i = 0; i < v.size(); ++i) s << v[i]; return s;}
|
||||
template<typename T>
|
||||
inline PIByteArray & operator >>(PIByteArray & s, PIList<T> & v) {assert(s.size_s() >= 4); int sz; s >> sz; v.resize(sz); for (int i = 0; i < sz; ++i) s >> v[i]; return s;}
|
||||
|
||||
|
||||
#endif // PILIST_H
|
||||
465
src_main/containers/pimap.h
Normal file
465
src_main/containers/pimap.h
Normal file
@@ -0,0 +1,465 @@
|
||||
/*! \file pimap.h
|
||||
* \brief Associative array with custom types of key and value
|
||||
*
|
||||
* This file declares PIMap
|
||||
*/
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Dynamic array of any type
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
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 PIMAP_H
|
||||
#define PIMAP_H
|
||||
|
||||
#include "pivector.h"
|
||||
#include "pideque.h"
|
||||
#include "pipair.h"
|
||||
# define __PICONTAINERS_SIMPLE_TYPE__(T) \
|
||||
__PIDEQUE_SIMPLE_TYPE__(T)\
|
||||
__PIVECTOR_SIMPLE_TYPE__(T)
|
||||
|
||||
|
||||
#if !defined(PIP_CONTAINERS_STL) || defined(DOXYGEN)
|
||||
|
||||
template<class T>
|
||||
void piQuickSort(T * a, ssize_t N) {
|
||||
if (N < 1) return;
|
||||
ssize_t i = 0, j = N;
|
||||
T & p(a[N >> 1]);
|
||||
do {
|
||||
while (a[i] < p) i++;
|
||||
while (a[j] > p) j--;
|
||||
if (i <= j) {
|
||||
if (i != j) {
|
||||
//piCout << "swap" << i << j << a[i] << a[j];
|
||||
piSwapBinary<T>(a[i], a[j]);
|
||||
}
|
||||
i++; j--;
|
||||
}
|
||||
} while (i <= j);
|
||||
if (j > 0) piQuickSort(a, j);
|
||||
if (N > i) piQuickSort(a + i, N - i);
|
||||
}
|
||||
|
||||
template <typename Key, typename T>
|
||||
class PIMap {
|
||||
template <typename Key1, typename T1> friend PIByteArray & operator >>(PIByteArray & s, PIMap<Key1, T1> & v);
|
||||
template <typename Key1, typename T1> friend PIByteArray & operator <<(PIByteArray & s, const PIMap<Key1, T1> & v);
|
||||
public:
|
||||
PIMap() {;}
|
||||
PIMap(const PIMap<Key, T> & other) {*this = other;}
|
||||
~PIMap() {;}
|
||||
|
||||
PIMap<Key, T> & operator =(const PIMap<Key, T> & other) {
|
||||
if (this == &other) return *this;
|
||||
clear();
|
||||
pim_content = other.pim_content;
|
||||
pim_index = other.pim_index;
|
||||
return *this;
|
||||
}
|
||||
|
||||
typedef T mapped_type;
|
||||
typedef Key key_type;
|
||||
typedef PIPair<Key, T> value_type;
|
||||
|
||||
class iterator {
|
||||
friend class PIMap<Key, T>;
|
||||
private:
|
||||
iterator(const PIMap<Key, T> * v, ssize_t p): parent(v), pos(p) {}
|
||||
const PIMap<Key, T> * parent;
|
||||
ssize_t pos;
|
||||
public:
|
||||
iterator(): parent(0), pos(0) {}
|
||||
const Key & key() const {return const_cast<PIMap<Key, T> * >(parent)->_key(pos);}
|
||||
T & value() {return const_cast<PIMap<Key, T> * >(parent)->_value(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 reverse_iterator {
|
||||
friend class PIMap<Key, T>;
|
||||
private:
|
||||
reverse_iterator(const PIMap<Key, T> * v, ssize_t p): parent(v), pos(p) {}
|
||||
const PIMap<Key, T> * parent;
|
||||
ssize_t pos;
|
||||
public:
|
||||
reverse_iterator(): parent(0), pos(0) {}
|
||||
const Key & key() const {return const_cast<PIMap<Key, T> * >(parent)->_key(pos);}
|
||||
T & value() const {return const_cast<PIMap<Key, T> * >(parent)->_value(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_iterator {
|
||||
friend class PIMap<Key, T>;
|
||||
private:
|
||||
const_iterator(const PIMap<Key, T> * v, ssize_t p): parent(v), pos(p) {}
|
||||
const PIMap<Key, T> * parent;
|
||||
ssize_t pos;
|
||||
public:
|
||||
const_iterator(): parent(0), pos(0) {}
|
||||
const value_type operator *() const {return parent->_pair(pos);}
|
||||
const value_type* operator ->() const {cval = parent->_pair(pos); return &cval;}
|
||||
const Key & key() const {return const_cast<PIMap<Key, T> * >(parent)->_key(pos);}
|
||||
const T & value() const {return const_cast<PIMap<Key, T> * >(parent)->_value(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);}
|
||||
mutable value_type cval;
|
||||
};
|
||||
|
||||
class const_reverse_iterator {
|
||||
friend class PIMap<Key, T>;
|
||||
private:
|
||||
const_reverse_iterator(const PIMap<Key, T> * v, ssize_t p): parent(v), pos(p) {}
|
||||
const PIMap<Key, T> * parent;
|
||||
ssize_t pos;
|
||||
public:
|
||||
const_reverse_iterator(): parent(0), pos(0) {}
|
||||
const value_type operator *() const {return parent->_pair(pos);}
|
||||
const value_type* operator ->() const {cval = parent->_pair(pos); return &cval;}
|
||||
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);}
|
||||
mutable value_type cval;
|
||||
};
|
||||
|
||||
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);}
|
||||
|
||||
size_t size() const {return pim_content.size();}
|
||||
int size_s() const {return pim_content.size_s();}
|
||||
size_t length() const {return pim_content.size();}
|
||||
bool isEmpty() const {return (pim_content.size() == 0);}
|
||||
|
||||
T & operator [](const Key & key) {
|
||||
bool f(false);
|
||||
ssize_t i = _find(key, f);
|
||||
if (f) return pim_content[pim_index[i].index];
|
||||
pim_content.push_back(T());
|
||||
pim_index.insert(i, MapIndex(key, pim_content.size() - 1));
|
||||
return pim_content.back();
|
||||
}
|
||||
const T operator [](const Key & key) const {bool f(false); ssize_t i = _find(key, f); if (f) return pim_content[pim_index[i].index]; return T();}
|
||||
T & at(const Key & key) {return (*this)[key];}
|
||||
const T at(const Key & key) const {return (*this)[key];}
|
||||
|
||||
PIMap<Key, T> & operator <<(const PIMap<Key, T> & other) {
|
||||
if (other.isEmpty()) return *this;
|
||||
if (other.size() == 1) {insert(other.pim_index[0].key, other.pim_content[0]); return *this;}
|
||||
if (other.size() == 2) {insert(other.pim_index[0].key, other.pim_content[0]); insert(other.pim_index[1].key, other.pim_content[1]); return *this;}
|
||||
pim_content << other.pim_content;
|
||||
size_t si = pim_index.size();
|
||||
for (int i = 0; i < other.pim_index.size_s(); ++i)
|
||||
pim_index << MapIndex(other.pim_index[i].key, other.pim_index[i].index + si);
|
||||
_sort();
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool operator ==(const PIMap<Key, T> & t) const {return (pim_content == t.pim_content && pim_index == t.pim_index);}
|
||||
bool operator !=(const PIMap<Key, T> & t) const {return (pim_content != t.pim_content || pim_index != t.pim_index);}
|
||||
bool contains(const Key & key) const {bool f(false); _find(key, f); return f;}
|
||||
//int etries(const T & v) const {int ec = 0; for (size_t i = 0; i < pim_size; ++i) if (v == pim_data[i]) ++ec; return ec;}
|
||||
|
||||
PIMap<Key, T> & reserve(size_t new_size) {pim_content.reserve(new_size); pim_index.reserve(new_size); return *this;}
|
||||
|
||||
//PIMap<Key, T> & removeAll(const T & v) {for (llong i = 0; i < pim_size; ++i) if (pim_data[i] == v) {remove(i); --i;} return *this;}
|
||||
PIMap<Key, T> & removeOne(const Key & key) {bool f(false); ssize_t i = _find(key, f); if (f) _remove(i); return *this;}
|
||||
PIMap<Key, T> & remove(const Key & key) {return removeOne(key);}
|
||||
PIMap<Key, T> & erase(const Key & key) {return removeOne(key);}
|
||||
PIMap<Key, T> & clear() {pim_content.clear(); pim_index.clear(); return *this;}
|
||||
|
||||
void swap(PIMap<Key, T> & other) {
|
||||
piSwapBinary<PIVector<T> >(pim_content, other.pim_content);
|
||||
piSwapBinary<PIVector<MapIndex> >(pim_index, other.pim_index);
|
||||
}
|
||||
|
||||
PIMap<Key, T> & insert(const Key & key, const T & value) {
|
||||
//MapIndex * i = _find(key);
|
||||
bool f(false);
|
||||
ssize_t i = _find(key, f);
|
||||
//piCout << "insert key=" << key << "found=" << f << "index=" << i << "value=" << value;
|
||||
if (f) {
|
||||
pim_content[pim_index[i].index] = value;
|
||||
} else {
|
||||
pim_content.push_back(value);
|
||||
pim_index.insert(i, MapIndex(key, pim_content.size() - 1));
|
||||
//_sort();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
//const T value(const Key & key, const T & default_ = T()) const {MapIndex * i = _find(key); if (i == 0) return default_; return pim_content[i->index];}
|
||||
const T value(const Key & key, const T & default_ = T()) const {bool f(false); ssize_t i = _find(key, f); if (!f) return default_; return pim_content[pim_index[i].index];}
|
||||
PIVector<T> values() const {return pim_content;}
|
||||
Key key(const T & value_, const Key & default_ = Key()) const {for (int i = 0; i < pim_index.size_s(); ++i) if (pim_content[pim_index[i].index] == value_) return pim_index[i].key; return default_;}
|
||||
PIVector<Key> keys() const {
|
||||
PIVector<Key> ret;
|
||||
for (int i = 0; i < pim_index.size_s(); ++i)
|
||||
ret << pim_index[i].key;
|
||||
return ret;
|
||||
}
|
||||
|
||||
void dump() {
|
||||
piCout << "PIMap" << size() << "entries" << PICoutManipulators::NewLine << "content:";
|
||||
for (size_t i = 0; i < pim_content.size(); ++i)
|
||||
piCout << PICoutManipulators::Tab << i << ":" << pim_content[i];
|
||||
piCout << "index:";
|
||||
for (size_t i = 0; i < pim_index.size(); ++i)
|
||||
piCout << PICoutManipulators::Tab << i << ":" << pim_index[i].key << "->" << pim_index[i].index;
|
||||
}
|
||||
|
||||
protected:
|
||||
struct MapIndex {
|
||||
MapIndex(Key k = Key(), size_t i = 0): key(k), index(i) {;}
|
||||
Key key;
|
||||
size_t index;
|
||||
bool operator ==(const MapIndex & s) const {return key == s.key;}
|
||||
bool operator !=(const MapIndex & s) const {return key != s.key;}
|
||||
bool operator <(const MapIndex & s) const {return key < s.key;}
|
||||
bool operator >(const MapIndex & s) const {return key > s.key;}
|
||||
};
|
||||
template <typename Key1, typename T1> friend PIByteArray & operator >>(PIByteArray & s, PIDeque<typename PIMap<Key1, T1>::MapIndex> & v);
|
||||
template <typename Key1, typename T1> friend PIByteArray & operator <<(PIByteArray & s, const PIDeque<typename PIMap<Key1, T1>::MapIndex> & v);
|
||||
|
||||
ssize_t binarySearch(ssize_t first, ssize_t last, const Key & key, bool & found) const {
|
||||
ssize_t mid;
|
||||
while (first <= last) {
|
||||
mid = (first + last) / 2;
|
||||
if (key > pim_index[mid].key) first = mid + 1;
|
||||
else if (key < pim_index[mid].key) last = mid - 1;
|
||||
else {found = true; return mid;}
|
||||
}
|
||||
found = false;
|
||||
return first;
|
||||
}
|
||||
void _sort() {piQuickSort<MapIndex>(pim_index.data(), pim_index.size_s() - 1);}
|
||||
ssize_t _find(const Key & k, bool & found) const {
|
||||
/*for (size_t i = 0; i < pim_index.size(); ++i)
|
||||
if (pim_index[i].key == k) {
|
||||
return (MapIndex * )&(pim_index[i]);
|
||||
}
|
||||
return 0;*/
|
||||
//piCout << "find for" << k << pim_index.size_s();
|
||||
if (pim_index.isEmpty()) {
|
||||
found = false;
|
||||
return 0;
|
||||
}
|
||||
//piCout << k << ret << found;
|
||||
return binarySearch(0, pim_index.size_s() - 1, k, found);
|
||||
}
|
||||
void _remove(ssize_t index) {
|
||||
//if (index >= pim_index.size()) return;
|
||||
size_t ci = pim_index[index].index, bi = pim_index.size() - 1;
|
||||
pim_index.remove(index);
|
||||
for (size_t i = 0; i < pim_index.size(); ++i)
|
||||
if (pim_index[i].index == bi) {
|
||||
pim_index[i].index = ci;
|
||||
break;
|
||||
}
|
||||
piSwapBinary<T>(pim_content[ci], pim_content.back());
|
||||
pim_content.resize(pim_index.size());
|
||||
}
|
||||
const value_type _pair(ssize_t index) const {
|
||||
if (index < 0 || index >= pim_index.size_s())
|
||||
return value_type();
|
||||
//piCout << "_pair" << index << pim_index[index].index;
|
||||
return value_type(pim_index[index].key, pim_content[pim_index[index].index]);
|
||||
}
|
||||
Key & _key(ssize_t index) {return pim_index[index].key;}
|
||||
T & _value(ssize_t index) {return pim_content[pim_index[index].index];}
|
||||
|
||||
PIVector<T> pim_content;
|
||||
PIDeque<MapIndex> pim_index;
|
||||
};
|
||||
//template <typename Key, typename T> bool operator <(const typename PIMap<Key, T>::MapIndex & f, const typename PIMap<Key, T>::MapIndex & s) {return f.key < s.key;}
|
||||
//template <typename Key, typename T> bool operator >(const typename PIMap<Key, T>::MapIndex & f, const typename PIMap<Key, T>::MapIndex & s) {return f.key > s.key;}
|
||||
|
||||
|
||||
/*#define __PIMAP_SIMPLE_FUNCTIONS__(T)
|
||||
template<> inline PIMap<Key, T>::~PIMap() {dealloc(); _reset();} \
|
||||
template<> inline PIMap<Key, T> & PIMap<Key, T>::push_back(const T & v) {alloc(pim_size + 1); pim_data[pim_size - 1] = v; return *this;} \
|
||||
template<> inline PIMap<Key, T> & PIMap<Key, T>::fill(const T & f) { \
|
||||
for (size_t i = 0; i < pim_size; ++i) \
|
||||
pim_data[i] = f; \
|
||||
return *this; \
|
||||
} \
|
||||
template<> inline PIMap<Key, T> & PIMap<Key, T>::resize(size_t new_size, const T & f) { \
|
||||
if (new_size < pim_size) \
|
||||
pim_size = new_size; \
|
||||
if (new_size > pim_size) { \
|
||||
size_t os = pim_size; \
|
||||
alloc(new_size); \
|
||||
for (size_t i = os; i < new_size; ++i) pim_data[i] = f; \
|
||||
} \
|
||||
return *this; \
|
||||
} \
|
||||
template<> inline PIMap<Key, T> & PIMap<Key, T>::insert(size_t index, const T & v) { \
|
||||
alloc(pim_size + 1); \
|
||||
if (index < pim_size - 1) { \
|
||||
size_t os = pim_size - index - 1; \
|
||||
memmove(&(pim_data[index + 1]), &(pim_data[index]), os * sizeof(T)); \
|
||||
} \
|
||||
pim_data[index] = v; \
|
||||
return *this; \
|
||||
} \
|
||||
template<> inline PIMap<Key, T> & PIMap<Key, T>::remove(size_t index, size_t count) { \
|
||||
if (index + count >= pim_size) { \
|
||||
resize(index); \
|
||||
return *this; \
|
||||
} \
|
||||
size_t os = pim_size - index - count; \
|
||||
memmove(&(pim_data[index]), &(pim_data[index + count]), os * sizeof(T)); \
|
||||
pim_size -= count; \
|
||||
return *this; \
|
||||
}
|
||||
|
||||
__PIMAP_SIMPLE_FUNCTIONS__(char)
|
||||
__PIMAP_SIMPLE_FUNCTIONS__(uchar)
|
||||
__PIMAP_SIMPLE_FUNCTIONS__(short)
|
||||
__PIMAP_SIMPLE_FUNCTIONS__(ushort)
|
||||
__PIMAP_SIMPLE_FUNCTIONS__(int)
|
||||
__PIMAP_SIMPLE_FUNCTIONS__(uint)
|
||||
__PIMAP_SIMPLE_FUNCTIONS__(long)
|
||||
__PIMAP_SIMPLE_FUNCTIONS__(ulong)
|
||||
__PIMAP_SIMPLE_FUNCTIONS__(llong)
|
||||
__PIMAP_SIMPLE_FUNCTIONS__(ullong)
|
||||
__PIMAP_SIMPLE_FUNCTIONS__(float)
|
||||
__PIMAP_SIMPLE_FUNCTIONS__(double)
|
||||
__PIMAP_SIMPLE_FUNCTIONS__(ldouble)*/
|
||||
|
||||
#else
|
||||
|
||||
|
||||
template<typename Key, typename Type>
|
||||
class PIP_EXPORT PIMap: public map<Key, Type> {
|
||||
typedef PIMap<Key, Type> _CMap;
|
||||
typedef map<Key, Type> _stlc;
|
||||
typedef std::pair<Key, Type> _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;}
|
||||
int size_s() const {return static_cast<int>(_stlc::size());}
|
||||
_CMap & insert(const Key & key_, const Type & value_) {_stlc::insert(_stlpair(key_, value_)); return *this;}
|
||||
_CMap & insert(PIPair<Key, Type> entry_) {_stlc::insert(_stlpair(entry_.first, entry_.second)); return *this;}
|
||||
Key key(Type value_, const Key & default_ = Key()) const {for (typename _stlc::const_iterator i = _stlc::begin(); i != _stlc::end(); i++) if (i->second == value_) return i->first; return default_;}
|
||||
PIVector<Key> keys() const {
|
||||
PIVector<Key> ret;
|
||||
for (typename _stlc::const_iterator i = _stlc::begin(); i != _stlc::end(); i++)
|
||||
ret << i->first;
|
||||
return ret;
|
||||
}
|
||||
Type & at(const Key & key_) {return _stlc::find(key_)->second;}
|
||||
Type value(const Key & key_) const {typename _stlc::const_iterator it = _stlc::find(key_); if (it != _stlc::end()) return it->second; return Type();}
|
||||
};
|
||||
|
||||
|
||||
template<typename Key, typename Type>
|
||||
class PIP_EXPORT PIMultiMap: public multimap<Key, Type> {
|
||||
typedef PIMultiMap<Key, Type> _CMultiMap;
|
||||
typedef multimap<Key, Type> _stlc;
|
||||
typedef std::pair<Key, Type> _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<Key, Type> 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 Key & default_ = Key()) const {for (typename _stlc::const_iterator i = _stlc::begin(); i != _stlc::end(); i++) if (i->second == value_) return i->first; return default_;}
|
||||
PIVector<Key> keys(Type value_) const {
|
||||
PIVector<Key> 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_) {typename _stlc::iterator i = _stlc::find(key_); if (i == _stlc::end()) return Type(); return i->second;}
|
||||
Type value(const Key & key_) const {typename _stlc::const_iterator i = _stlc::find(key_); if (i == _stlc::end()) return Type(); return i->second;}
|
||||
PIVector<Type> values(const Key & key_) const {
|
||||
std::pair<typename _stlc::const_iterator, typename _stlc::const_iterator> range = _stlc::equal_range(key_);
|
||||
PIVector<Type> 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;}
|
||||
};
|
||||
|
||||
#define __PIMAP_SIMPLE_FUNCTIONS__(T)
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef PIP_STD_IOSTREAM
|
||||
template<typename Key, typename Type>
|
||||
inline std::ostream & operator <<(std::ostream & s, const PIMap<Key, Type> & v) {
|
||||
s << "{";
|
||||
bool first = true;
|
||||
for (typename PIMap<Key, Type>::const_iterator i = v.begin(); i != v.end(); ++i) {
|
||||
if (!first)
|
||||
s << ", ";
|
||||
first = false;
|
||||
s << i->first << ": " << i->second;
|
||||
}
|
||||
s << "}";
|
||||
return s;
|
||||
}
|
||||
#endif
|
||||
|
||||
template<typename Key, typename Type>
|
||||
inline PICout operator <<(PICout s, const PIMap<Key, Type> & v) {
|
||||
s.space();
|
||||
s.setControl(0, true);
|
||||
s << "{";
|
||||
bool first = true;
|
||||
for (typename PIMap<Key, Type>::const_iterator i = v.begin(); i != v.end(); ++i) {
|
||||
if (!first)
|
||||
s << ", ";
|
||||
first = false;
|
||||
s << i->first << ": " << i->second;
|
||||
}
|
||||
s << "}";
|
||||
s.restoreControl();
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
#endif // PIMAP_H
|
||||
30
src_main/containers/pipair.h
Normal file
30
src_main/containers/pipair.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef PIPAIR_H
|
||||
#define PIPAIR_H
|
||||
|
||||
#include "pibase.h"
|
||||
class PICout;
|
||||
|
||||
template<typename Type0, typename Type1>
|
||||
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<typename Type0, typename Type1>
|
||||
inline bool operator <(const PIPair<Type0, Type1> & value0, const PIPair<Type0, Type1> & value1) {return value0.first < value1.first;}
|
||||
template<typename Type0, typename Type1>
|
||||
inline bool operator ==(const PIPair<Type0, Type1> & value0, const PIPair<Type0, Type1> & value1) {return (value0.first == value1.first) && (value0.second == value1.second);}
|
||||
template<typename Type0, typename Type1>
|
||||
inline bool operator !=(const PIPair<Type0, Type1> & value0, const PIPair<Type0, Type1> & value1) {return (value0.first != value1.first) || (value0.second != value1.second);}
|
||||
|
||||
#ifdef PIP_STD_IOSTREAM
|
||||
template<typename Type0, typename Type1>
|
||||
inline std::ostream & operator <<(std::ostream & s, const PIPair<Type0, Type1> & v) {s << "(" << v.first << ", " << v.second << ")"; return s;}
|
||||
#endif
|
||||
|
||||
template<typename Type0, typename Type1>
|
||||
inline PICout operator <<(PICout s, const PIPair<Type0, Type1> & v) {s.space(); s.setControl(0, true); s << "(" << v.first << ", " << v.second << ")"; s.restoreControl(); return s;}
|
||||
|
||||
#endif // PIPAIR_H
|
||||
41
src_main/containers/piqueue.h
Executable file
41
src_main/containers/piqueue.h
Executable file
@@ -0,0 +1,41 @@
|
||||
/*! \file picontainers.h
|
||||
* \brief Queue container
|
||||
*
|
||||
* This file declare PIQueue
|
||||
*/
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Queue container
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
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 PIQUEUE_H
|
||||
#define PIQUEUE_H
|
||||
|
||||
#include "pideque.h"
|
||||
|
||||
template<typename T>
|
||||
class PIP_EXPORT PIQueue: public PIDeque<T> {
|
||||
public:
|
||||
PIQueue() {;}
|
||||
PIDeque<T> & enqueue(const T & v) {PIDeque<T>::push_front(v); return *this;}
|
||||
T dequeue() {return PIDeque<T>::take_back();}
|
||||
T & head() {return PIDeque<T>::back();}
|
||||
const T & head() const {return PIDeque<T>::back();}
|
||||
PIVector<T> toVector() {PIVector<T> v(PIDeque<T>::size()); for (uint i = 0; i < PIDeque<T>::size(); ++i) v[i] = PIDeque<T>::at(i); return v;}
|
||||
};
|
||||
|
||||
#endif // PIQUEUE_H
|
||||
117
src_main/containers/piset.h
Normal file
117
src_main/containers/piset.h
Normal file
@@ -0,0 +1,117 @@
|
||||
/*! \file piset.h
|
||||
* \brief Set container
|
||||
*
|
||||
* This file declare PISet
|
||||
*/
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Set container
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
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 PISET_H
|
||||
#define PISET_H
|
||||
|
||||
#include "pimap.h"
|
||||
|
||||
/*! \brief Set of any type
|
||||
* \details This class used to store collection of unique elements
|
||||
* of any type. You can only add values to set with \a operator<< or
|
||||
* with function \a insert(). You can discover if value already in
|
||||
* set with \a operator[] or with function \a find(). These function
|
||||
* has logarithmic complexity.
|
||||
*/
|
||||
template <typename T>
|
||||
class PIP_EXPORT PISet: public PIMap<T, uchar> {
|
||||
typedef PIMap<T, uchar> _CSet;
|
||||
public:
|
||||
|
||||
//! Contructs an empty set
|
||||
PISet() {}
|
||||
|
||||
//! Contructs set with one element "value"
|
||||
PISet(const T & value) {_CSet::insert(value, 0);}
|
||||
|
||||
//! Contructs set with elements "v0" and "v1"
|
||||
PISet(const T & v0, const T & v1) {_CSet::insert(v0, 0); _CSet::insert(v1, 0);}
|
||||
|
||||
//! Contructs set with elements "v0", "v1" and "v2"
|
||||
PISet(const T & v0, const T & v1, const T & v2) {_CSet::insert(v0, 0); _CSet::insert(v1, 0); _CSet::insert(v2, 0);}
|
||||
|
||||
//! Contructs set with elements "v0", "v1", "v2" and "v3"
|
||||
PISet(const T & v0, const T & v1, const T & v2, const T & v3) {_CSet::insert(v0, 0); _CSet::insert(v1, 0); _CSet::insert(v2, 0); _CSet::insert(v3, 0);}
|
||||
|
||||
//! Contructs set from vector of elements
|
||||
PISet(const PIVector<T> & values) {
|
||||
if (values.isEmpty()) return;
|
||||
//_CSet::pim_content.resize(values.size_s());
|
||||
//_CSet::pim_index.resize(values.size_s());
|
||||
for (int i = 0; i < values.size_s(); ++i) {
|
||||
//_CSet::pim_index[i].index = i;
|
||||
//_CSet::pim_index[i].key = values[i];
|
||||
_CSet::insert(values[i], 0);
|
||||
}
|
||||
//_CSet::_sort();
|
||||
}
|
||||
|
||||
typedef T key_type;
|
||||
|
||||
PISet<T> & operator <<(const T & t) {_CSet::insert(t, 0); return *this;}
|
||||
PISet<T> & operator <<(const PISet<T> & other) {(*(_CSet*)this) << *((_CSet*)&other); return *this;}
|
||||
|
||||
//! Returns if element "t" exists in this set
|
||||
bool operator [](const T & t) const {return _CSet::contains(t);}
|
||||
|
||||
//! Returns if element "t" exists in this set
|
||||
PISet<T> & remove(const T & t) {_CSet::remove(t); return *this;}
|
||||
|
||||
//! Unite set with "v"
|
||||
PISet<T> & unite(const PISet<T> & v) {
|
||||
for (typename PIMap<T, uchar>::const_iterator i = v.begin(); i != v.end(); ++i)
|
||||
_CSet::insert(i->first, 0);
|
||||
return *this;
|
||||
}
|
||||
|
||||
//! Subtract set with "v"
|
||||
PISet<T> & subtract(const PISet<T> & v) {
|
||||
for (typename PIMap<T, uchar>::const_iterator i = v.begin(); i != v.end(); ++i)
|
||||
_CSet::remove(i->first);
|
||||
return *this;
|
||||
}
|
||||
|
||||
//! Returns content of set as PIVector
|
||||
PIVector<T> toVector() const {PIVector<T> ret; for (typename _CSet::const_iterator i = _CSet::begin(); i != _CSet::end(); ++i) ret << (*i).first; return ret;}
|
||||
};
|
||||
|
||||
|
||||
template<typename Type>
|
||||
inline PICout operator <<(PICout s, const PISet<Type> & v) {
|
||||
s.space();
|
||||
s.setControl(0, true);
|
||||
s << "{";
|
||||
bool first = true;
|
||||
for (typename PIMap<Type, uchar>::const_iterator i = v.begin(); i != v.end(); ++i) {
|
||||
if (!first)
|
||||
s << ", ";
|
||||
first = false;
|
||||
s << i->first;
|
||||
}
|
||||
s << "}";
|
||||
s.restoreControl();
|
||||
return s;
|
||||
}
|
||||
|
||||
#endif // PISET_H
|
||||
41
src_main/containers/pistack.h
Executable file
41
src_main/containers/pistack.h
Executable file
@@ -0,0 +1,41 @@
|
||||
/*! \file pistack.h
|
||||
* \brief Stack container
|
||||
*
|
||||
* This file declare PIStack
|
||||
*/
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Stack container
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
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 PISTACK_H
|
||||
#define PISTACK_H
|
||||
|
||||
#include "pivector.h"
|
||||
|
||||
template<typename T>
|
||||
class PIP_EXPORT PIStack: public PIVector<T> {
|
||||
public:
|
||||
PIStack() {;}
|
||||
PIVector<T> & push(const T & v) {PIVector<T>::push_back(v); return *this;}
|
||||
T pop() {return PIVector<T>::take_back();}
|
||||
T & top() {return PIVector<T>::back();}
|
||||
const T & top() const {return PIVector<T>::back();}
|
||||
PIVector<T> toVector() {PIVector<T> v(PIVector<T>::size()); for (uint i = 0; i < PIVector<T>::size(); ++i) v[i] = PIVector<T>::at(i); return v;}
|
||||
};
|
||||
|
||||
#endif // PISTACK_H
|
||||
549
src_main/containers/pivector.h
Executable file
549
src_main/containers/pivector.h
Executable file
@@ -0,0 +1,549 @@
|
||||
/*! \file pivector.h
|
||||
* \brief Dynamic array of any type
|
||||
*
|
||||
* This file declares PIVector
|
||||
*/
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Dynamic array of any type
|
||||
Copyright (C) 2016 Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
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 "picontainers.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 T * data, size_t size): piv_data(0), piv_size(0), piv_rsize(0) {
|
||||
//printf("new vector 2 %p (%s) ... !{\n", this, typeid(T).name());
|
||||
alloc(size);
|
||||
newT(piv_data, data, piv_size);
|
||||
//printf("(s=%d, d=%p) }!\n", int(pid_size), pid_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);
|
||||
newT(piv_data, other.piv_data, piv_size);
|
||||
//printf("(s=%d, d=%p) }!\n", int(piv_size), piv_data);
|
||||
}
|
||||
PIVector(size_t 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();
|
||||
//deleteRaw(piv_tdata);
|
||||
_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 (size_t i = 0; i < piv_size; ++i)
|
||||
piv_data[i] = other.piv_data[i];
|
||||
return *this;
|
||||
}*/
|
||||
if (!oj) {
|
||||
deleteT(piv_data, piv_size);
|
||||
alloc(other.piv_size);
|
||||
//zeroRaw(piv_data, piv_size);
|
||||
for (size_t i = 0; i < piv_size; ++i)
|
||||
elementNew(piv_data + i, 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, size_t p): parent(v), pos(p) {}
|
||||
PIVector<T> * parent;
|
||||
size_t pos;
|
||||
public:
|
||||
iterator(): parent(0), pos(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, size_t p): parent(v), pos(p) {}
|
||||
const PIVector<T> * parent;
|
||||
size_t pos;
|
||||
public:
|
||||
const_iterator(): parent(0), pos(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, size_t p): parent(v), pos(p) {}
|
||||
PIVector<T> * parent;
|
||||
size_t pos;
|
||||
public:
|
||||
reverse_iterator(): parent(0), pos(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, size_t p): parent(v), pos(p) {}
|
||||
const PIVector<T> * parent;
|
||||
size_t pos;
|
||||
public:
|
||||
const_reverse_iterator(): parent(0), pos(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);}
|
||||
|
||||
size_t size() const {return piv_size;}
|
||||
ssize_t size_s() const {return piv_size;}
|
||||
size_t length() const {return piv_size;}
|
||||
size_t capacity() const {return piv_rsize;}
|
||||
bool isEmpty() const {return (piv_size == 0);}
|
||||
|
||||
T & operator [](size_t index) {return piv_data[index];}
|
||||
T & at(size_t index) {return piv_data[index];}
|
||||
const T & operator [](size_t index) const {return piv_data[index];}
|
||||
const T & at(size_t 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 (size_t 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 (size_t i = 0; i < piv_size; ++i) if (t[i] != piv_data[i]) return true; return false;}
|
||||
bool contains(const T & v) const {for (size_t 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 (size_t i = 0; i < piv_size; ++i) if (v == piv_data[i]) ++ec; return ec;}
|
||||
ssize_t indexOf(const T & v) const {for (ssize_t i = 0; i < piv_size; ++i) if (v == piv_data[i]) return i; return -1;}
|
||||
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;}
|
||||
|
||||
T * data(size_t index = 0) {return &(piv_data[index]);}
|
||||
const T * data(size_t 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 (size_t i = 0; i < piv_size; ++i)
|
||||
elementNew(piv_data + i, f);
|
||||
return *this;
|
||||
}
|
||||
PIVector<T> & assign(const T & f = T()) {return fill(f);}
|
||||
PIVector<T> & assign(size_t new_size, const T & f) {resize(new_size); return fill(f);}
|
||||
PIVector<T> & resize(size_t 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) {
|
||||
size_t os = piv_size;
|
||||
alloc(new_size);
|
||||
//if (sizeof(T) == 1) memset(&(piv_data[os]), f, ds);
|
||||
//zeroRaw(&(piv_data[os]), new_size - os);
|
||||
for (size_t i = os; i < new_size; ++i) elementNew(piv_data + i, f);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
PIVector<T> & reserve(size_t new_size) {if (new_size <= piv_rsize) return *this; size_t os = piv_size; alloc(new_size); piv_size = os; return *this;}
|
||||
|
||||
PIVector<T> & insert(size_t index, const T & v = T()) {
|
||||
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));
|
||||
}
|
||||
//zeroRaw(&(piv_data[index]), 1);
|
||||
elementNew(piv_data + index, v);
|
||||
return *this;
|
||||
}
|
||||
PIVector<T> & insert(size_t index, const PIVector<T> & other) {
|
||||
if (other.isEmpty()) return *this;
|
||||
ssize_t os = piv_size - index;
|
||||
alloc(piv_size + other.piv_size);
|
||||
if (os > 0)
|
||||
memmove(&(piv_data[index + other.piv_size]), &(piv_data[index]), os * sizeof(T));
|
||||
newT(piv_data + index, other.piv_data, other.piv_size);
|
||||
return *this;
|
||||
}
|
||||
|
||||
PIVector<T> & remove(size_t index, size_t count = 1) {
|
||||
if (count == 0) return *this;
|
||||
if (index + count >= piv_size) {
|
||||
resize(index);
|
||||
return *this;
|
||||
}
|
||||
size_t os = piv_size - index - count;
|
||||
deleteT(&(piv_data[index]), count);
|
||||
memmove(&(piv_data[index]), &(piv_data[index + count]), os * sizeof(T));
|
||||
piv_size -= count;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void swap(PIVector<T> & other) {
|
||||
piSwap<T*>(piv_data, other.piv_data);
|
||||
piSwap<size_t>(piv_size, other.piv_size);
|
||||
piSwap<size_t>(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) ? -1 : ((*t0) == (*t1) ? 0 : 1);}
|
||||
PIVector<T> & sort(CompareFunc compare = compare_func) {piqsort(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(size_t(ns)); return *this;}
|
||||
|
||||
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;}
|
||||
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;}
|
||||
|
||||
PIVector<T> & push_back(const T & v) {alloc(piv_size + 1); elementNew(piv_data + piv_size - 1, 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> & operator <<(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;
|
||||
}
|
||||
|
||||
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;}
|
||||
|
||||
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;}
|
||||
|
||||
private:
|
||||
void _reset() {piv_size = piv_rsize = 0; piv_data = 0;}
|
||||
size_t asize(size_t s) {
|
||||
if (s == 0) return 0;
|
||||
if (piv_rsize + piv_rsize >= s && piv_rsize < s)
|
||||
return piv_rsize + piv_rsize;
|
||||
ssize_t t = 0, s_ = s - 1;
|
||||
while (s_ >> t) ++t;
|
||||
return (1 << t);
|
||||
}
|
||||
inline void newT(T * dst, const T * src, size_t s) {
|
||||
for (size_t i = 0; i < s; ++i)
|
||||
elementNew(dst + i, src[i]);
|
||||
}
|
||||
T * newRaw(size_t 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 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) {
|
||||
//cout << " ~[("<<this<<")deleteT " << std::dec << sz << " elements " << std::hex << "0x" << (llong)d << " ... <\n" << endl;
|
||||
if ((uchar*)d != 0) {
|
||||
for (size_t i = 0; i < sz; ++i)
|
||||
elementDelete(d[i]);
|
||||
//zeroRaw(d, sz);
|
||||
}
|
||||
//cout << " > ok]~" << endl;
|
||||
}
|
||||
inline 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;
|
||||
}
|
||||
inline void zeroRaw(T * d, size_t 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;
|
||||
}
|
||||
inline void elementNew(T * to, const T & from) {new(to)T(from);}
|
||||
inline void elementDelete(T & from) {from.~T();}
|
||||
void dealloc() {deleteRaw(piv_data);}
|
||||
inline void alloc(size_t new_size) {
|
||||
if (new_size <= piv_rsize) {
|
||||
piv_size = new_size;
|
||||
return;
|
||||
}
|
||||
//int os = piv_size;
|
||||
piv_size = new_size;
|
||||
size_t as = asize(new_size);
|
||||
if (as == piv_rsize) return;
|
||||
|
||||
//cout << std::hex << " ![("<<this<<")realloc " << piv_data << " data ... <\n" << endl;
|
||||
T * p_d = (T*)(realloc(piv_data, as*sizeof(T)));
|
||||
assert(p_d);
|
||||
piv_data = p_d;
|
||||
//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;
|
||||
volatile 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) {memcpy(dst, src, s * sizeof(T));} \
|
||||
template<> inline void PIVector<T>::deleteT(T * d, size_t sz) {;} \
|
||||
template<> inline void PIVector<T>::elementNew(T * to, const T & from) {(*to) = from;} \
|
||||
template<> inline void PIVector<T>::elementDelete(T & from) {;}
|
||||
|
||||
#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) {piqsort(&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) {piqsort(&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
|
||||
};
|
||||
|
||||
#define __PIVECTOR_SIMPLE_TYPE__(T)
|
||||
|
||||
|
||||
#endif
|
||||
__PIVECTOR_SIMPLE_TYPE__(bool)
|
||||
__PIVECTOR_SIMPLE_TYPE__(char)
|
||||
__PIVECTOR_SIMPLE_TYPE__(uchar)
|
||||
__PIVECTOR_SIMPLE_TYPE__(short)
|
||||
__PIVECTOR_SIMPLE_TYPE__(ushort)
|
||||
__PIVECTOR_SIMPLE_TYPE__(int)
|
||||
__PIVECTOR_SIMPLE_TYPE__(uint)
|
||||
__PIVECTOR_SIMPLE_TYPE__(long)
|
||||
__PIVECTOR_SIMPLE_TYPE__(ulong)
|
||||
__PIVECTOR_SIMPLE_TYPE__(llong)
|
||||
__PIVECTOR_SIMPLE_TYPE__(ullong)
|
||||
__PIVECTOR_SIMPLE_TYPE__(float)
|
||||
__PIVECTOR_SIMPLE_TYPE__(double)
|
||||
__PIVECTOR_SIMPLE_TYPE__(ldouble)
|
||||
|
||||
|
||||
#ifdef PIP_STD_IOSTREAM
|
||||
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;}
|
||||
#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;}
|
||||
|
||||
|
||||
#endif // PIVECTOR_H
|
||||
Reference in New Issue
Block a user