1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
/****************************************************************************
**
** Copyright (C) 2006-2009 fullmetalcoder <fullmetalcoder@hotmail.fr>
**
** This file is part of the Edyuk project <http://edyuk.org>
**
** This file may be used under the terms of the GNU General Public License
** version 3 as published by the Free Software Foundation and appearing in the
** file GPL.txt included in the packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
#ifndef _LIGHT_VECTOR_H_
#define _LIGHT_VECTOR_H_
#include <qglobal.h>
/*!
\file light_vector.h
\brief Definition of the light_vector class
*/
template <typename T>
class light_vector
{
public:
light_vector() : m_data(0), size(0) {}
~light_vector() { free(m_data); }
light_vector& operator = (const light_vector& o)
{
size = o.size;
m_data = o.m_data;
return *this;
}
light_vector& operator << (const T& v)
{
append(v);
return *this;
}
inline quint16 length() const
{ return size; }
inline quint16 count() const
{ return size; }
inline T* data()
{ return m_data; }
void alloc(int pos, size_t n)
{
size += n;
m_data = !m_data ? (T*)malloc(size * sizeof(T)) : (T*)realloc(m_data, size * sizeof(T));
for ( int i = size - 1; (i > pos) && (i >= (int)n); --i )
m_data[i] = m_data[i - n];
//for ( int i = pos; (i < (pos + n)) && ((i + n) < size); ++i )
// m_data[i + n] = m_data[i];
}
inline void prepend(const T& v)
{
insert(0, v);
}
void insert(int i, const T& v)
{
i = qBound(0, i, (int)size);
alloc(i, 1);
m_data[i] = v;
}
void append(const T& v)
{
++size;
m_data = !m_data ? (T*)malloc(size * sizeof(T)) : (T*)realloc(m_data, size * sizeof(T));
m_data[size - 1] = v;
}
inline const T& at(quint16 i) const
{
return *(m_data + i);
}
inline T& operator [] (quint16 i)
{
return *(m_data + i);
}
bool contains(const T& v) const
{
for ( int i = 0; i < size; i++ )
if ( m_data[i] == v )
return true;
return false;
}
private:
T* m_data;
quint16 size;
};
#endif // _LIGHT_VECTOR_H_
|