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