FreeNOS
Index.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 __LIB_LIBSTD_INDEX_H
19 #define __LIB_LIBSTD_INDEX_H
20 
21 #include "Assert.h"
22 #include "Types.h"
23 #include "Macros.h"
24 
36 template <class T, const Size N> class Index
37 {
38  public:
39 
44  : m_count(0)
45  {
46  for (Size i = 0; i < N; i++)
47  {
48  m_array[i] = ZERO;
49  }
50  }
51 
60  virtual bool insert(Size & position, T *item)
61  {
62  // Check if we are full
63  if (m_count == N)
64  {
65  return false;
66  }
67  // The item must point to an object
68  else if (item == ZERO)
69  {
70  return false;
71  }
72 
73  // There is space, add the item.
74  for (Size i = 0; i < N; i++)
75  {
76  if (m_array[i] == ZERO)
77  {
78  m_array[i] = item;
79  m_count++;
80  position = i;
81  return true;
82  }
83  }
84 
85  // Should not be reached
86  assert(false);
87  return false;
88  }
89 
97  virtual bool insert(T *item)
98  {
99  Size ignored = 0;
100  return insert(ignored, item);
101  }
102 
113  virtual bool insertAt(const Size position, T *item)
114  {
115  // Position must be in range of the array
116  if (position >= N)
117  {
118  return false;
119  }
120  // The item must point to an object
121  else if (item == ZERO)
122  {
123  return false;
124  }
125 
126  // Increment counter only when needed
127  if (m_array[position] == ZERO)
128  {
129  m_count++;
130  }
131 
132  m_array[position] = item;
133  return true;
134  }
135 
143  virtual bool remove(const Size position)
144  {
145  // Position must be in range of the array
146  if (position >= N)
147  {
148  return false;
149  }
150 
151  // See if the item exists
152  if (!m_array[position])
153  {
154  return false;
155  }
156 
157  m_array[position] = ZERO;
158  assert(m_count >= 1);
159  m_count--;
160  return true;
161  }
162 
166  void deleteAll()
167  {
168  for (Size i = 0; i < N; i++)
169  {
170  if (m_array[i] != ZERO)
171  {
172  delete m_array[i];
173  m_array[i] = ZERO;
174  }
175  }
176 
177  m_count = 0;
178  }
179 
187  virtual T * get(const Size position) const
188  {
189  // Position must be in range of the array
190  if (position >= N)
191  {
192  return ZERO;
193  }
194 
195  return m_array[position];
196  }
197 
201  virtual bool contains(const T *item) const
202  {
203  for (Size i = 0; i < N; i++)
204  {
205  if (m_array[i] == item)
206  {
207  return true;
208  }
209  }
210 
211  return false;
212  }
213 
217  virtual Size size() const
218  {
219  return N;
220  }
221 
225  virtual Size count() const
226  {
227  return m_count;
228  }
229 
237  T * operator [] (const Size i)
238  {
239  return get(i);
240  }
241 
242  private:
243 
245  T* m_array[N];
246 
249 };
250 
256 #endif /* __LIB_LIBSTD_INDEX_H */
Index::count
virtual Size count() const
Item count in the Index.
Definition: Index.h:225
Index::insertAt
virtual bool insertAt(const Size position, T *item)
Inserts the given item at the given position.
Definition: Index.h:113
Macros.h
Types.h
Index
Index is a N-sized array of pointers to items of type T.
Definition: Index.h:36
Assert.h
Index::m_array
T * m_array[N]
Array of pointers to items.
Definition: Index.h:245
Index::deleteAll
void deleteAll()
Removes and delete()'s all items.
Definition: Index.h:166
Index::Index
Index()
Constructor.
Definition: Index.h:43
Index::size
virtual Size size() const
Size of the Index.
Definition: Index.h:217
Size
unsigned int Size
Any sane size indicator cannot go negative.
Definition: Types.h:128
Index::get
virtual T * get(const Size position) const
Returns the item at the given position.
Definition: Index.h:187
Index::operator[]
T * operator[](const Size i)
Returns the item at the given position in the Index.
Definition: Index.h:237
Index::insert
virtual bool insert(T *item)
Adds the given item, if possible.
Definition: Index.h:97
assert
#define assert(exp)
Insert program diagnostics.
Definition: assert.h:60
Index::remove
virtual bool remove(const Size position)
Removes the item at the given position.
Definition: Index.h:143
Index::contains
virtual bool contains(const T *item) const
Check if the given item is stored in this Sequence.
Definition: Index.h:201
Index::insert
virtual bool insert(Size &position, T *item)
Adds the given item, if possible.
Definition: Index.h:60
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
Index::m_count
Size m_count
Amount of valid pointers in the array.
Definition: Index.h:248