FreeNOS
Vector.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Niek Linnenbank
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef __LIBSTD_VECTOR_H
19 #define __LIBSTD_VECTOR_H
20 
21 #include "Assert.h"
22 #include "Sequence.h"
23 #include "Types.h"
24 #include "Macros.h"
25 #include "MemoryBlock.h"
26 
36 #define VECTOR_DEFAULT_SIZE 64
37 
41 template <class T> class Vector : public Sequence<T>
42 {
43  public:
44 
53  {
54  assert(size > 0);
55 
56  m_size = size;
57  m_count = 0;
58  m_array = new T[m_size];
59  }
60 
66  Vector(const Vector<T> & a)
67  {
68  assert(a.m_size > 0);
69 
70  m_size = a.m_size;
71  m_count = a.m_count;
72  m_array = new T[m_size];
73 
74  for (Size i = 0; i < m_size; i++)
75  m_array[i] = a.m_array[i];
76  }
77 
81  virtual ~Vector()
82  {
83  delete[] m_array;
84  }
85 
93  virtual int insert(const T & item)
94  {
95  if (m_count == m_size)
96  if (!resize(m_size*2))
97  return -1;
98 
99  m_array[m_count++] = item;
100  return m_count-1;
101  }
102 
113  virtual bool insert(Size position, const T & item)
114  {
115  // Resize the vector if needed
116  if (position >= m_size)
117  {
118  Size increase = position > (m_size * 2) ? position : m_size * 2;
119 
120  if (!resize(m_size+increase))
121  return false;
122  }
123  // Update the item count if needed
124  if (position >= m_count)
125  m_count += (position+1) - m_count;
126 
127  // Assign the item
128  m_array[position] = item;
129  return true;
130  }
131 
139  virtual const T * get(Size position) const
140  {
141  if (position >= m_count)
142  {
143  return ZERO;
144  }
145  return &m_array[position];
146  }
147 
153  virtual const T & at(Size position) const
154  {
155  return m_array[position];
156  }
157 
161  virtual void clear()
162  {
163  m_count = 0;
164  }
165 
173  virtual bool removeAt(Size position)
174  {
175  if (position >= m_count)
176  {
177  return false;
178  }
179 
180  // Move all consequetive items
181  for (Size i = position; i < m_count-1; i++)
182  {
183  m_array[i] = m_array[i+1];
184  }
185  m_count--;
186  return true;
187  }
188 
194  virtual Size size() const
195  {
196  return m_size;
197  }
198 
204  virtual Size count() const
205  {
206  return m_count;
207  }
208 
214  virtual const T * vector() const
215  {
216  return m_array;
217  }
218 
224  virtual bool resize(Size size)
225  {
226  assert(size > 0);
227 
228  T *arr = new T[size];
229  if (!arr)
230  return false;
231 
232  // Copy the old array in the new one
233  for (Size i = 0; i < m_size; i++)
234  {
235  arr[i] = m_array[i];
236  }
237  // Clean up the old array and set the new one
238  delete[] m_array;
239  m_array = arr;
240  m_size = size;
241  return true;
242  }
243 
244  private:
245 
248 
251 
254 };
255 
261 #endif /* __LIBSTD_VECTOR_H */
Vector::get
virtual const T * get(Size position) const
Returns the item at the given position.
Definition: Vector.h:139
Vector::Vector
Vector(Size size=VECTOR_DEFAULT_SIZE)
Constructor.
Definition: Vector.h:52
Macros.h
Types.h
Vector::at
virtual const T & at(Size position) const
Return item at the given position as a reference.
Definition: Vector.h:153
Vector::Vector
Vector(const Vector< T > &a)
Copy constructor.
Definition: Vector.h:66
Sequence.h
Assert.h
MemoryBlock.h
Vector::m_size
Size m_size
The maximum size of the array.
Definition: Vector.h:250
Vector::~Vector
virtual ~Vector()
Destructor.
Definition: Vector.h:81
Vector::vector
virtual const T * vector() const
Get Vector data pointer.
Definition: Vector.h:214
Sequence
Sequences are containers that provide indexed based storage of items.
Definition: Sequence.h:37
Vector::count
virtual Size count() const
Returns the number of items inside the Vector.
Definition: Vector.h:204
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
Vector::insert
virtual int insert(const T &item)
Adds the given item to the Vector, if possible.
Definition: Vector.h:93
Vector::size
virtual Size size() const
Returns the maximum size of this Vector.
Definition: Vector.h:194
Vector::insert
virtual bool insert(Size position, const T &item)
Inserts the given item at the given position.
Definition: Vector.h:113
Vector::resize
virtual bool resize(Size size)
Resize the Vector.
Definition: Vector.h:224
assert
#define assert(exp)
Insert program diagnostics.
Definition: assert.h:60
Vector::m_array
T * m_array
The actual array where the data is stored.
Definition: Vector.h:247
Vector::clear
virtual void clear()
Remove all items from the vector.
Definition: Vector.h:161
Vector::removeAt
virtual bool removeAt(Size position)
Removes the item at the given position.
Definition: Vector.h:173
Vector
Vectors are dynamically resizeable Arrays.
Definition: Vector.h:41
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
Vector::m_count
Size m_count
Number of used items in the array.
Definition: Vector.h:253
VECTOR_DEFAULT_SIZE
#define VECTOR_DEFAULT_SIZE
Default size of an Vector.
Definition: Vector.h:36