ALBA
albaVector.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: ALBA (Agile Library for Biomedical Applications)
4 Module: albaVector
5 Authors: Marco Petrone
6
7 Copyright (c) BIC
8 All rights reserved. See Copyright.txt or
9
10
11 This software is distributed WITHOUT ANY WARRANTY; without even
12 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 PURPOSE. See the above copyright notice for more information.
14
15=========================================================================*/
16
17#ifndef __albaVector_h
18#define __albaVector_h
19
20#include "albaDefines.h"
21#include "albaBase.h"
22#include "albaUtility.h"
23#include <vector>
24
25template <class T> class mmuVectorItems;
26
31template <class T>
32class albaVector : public albaBase
33{
34public:
36 virtual ~albaVector();
37
40
42 void SetItem(const albaID idx,const T &object);
43
45 albaID AppendItem(const T &object);
46
50 bool ReplaceItem(const albaID idx, const T &newitem);
51
58 bool RemoveItem(const albaID idx);
59
64 bool FindAndRemoveItem(const T &object);
65
69
72 bool IsItemPresent(const T &object);
73
77 bool GetItem(const albaID idx,T &obj);
78
82 T &GetItem(const albaID idx);
83
85 bool FindItem(const T &object, albaID &idx);
86
89 //int GetNumberOfItems();
90
92 albaID Push(const T &obj);
93
95 bool Pop(T &obj);
96
98 T &operator [](const albaID idx);
99
101 const T &operator [](const albaID idx) const;
102
103 static T m_NullItem;
104
105protected:
106
108
109private:
110
111 albaVector(const albaVector&); // Not implemented.
112 void operator=(const albaVector&); // Not implemented.
113
114};
115
117template <class T>
119
120//------------------------------------------------------------------------------
121// PIMPL declarations
122//------------------------------------------------------------------------------
123template <class T>
125{
126 public:
127 std::vector<T> Vector;
128};
129
130//------------------------------------------------------------------------------
131template <class T>
133//------------------------------------------------------------------------------
134{
135 m_Items = new mmuVectorItems<T>;
136}
137
138//------------------------------------------------------------------------------
139template <class T>
141//------------------------------------------------------------------------------
142{
143 RemoveAllItems();
144 delete m_Items;
145}
146
147//------------------------------------------------------------------------------
148template <class T>
149void albaVector<T>::SetItem(albaID idx,const T &object)
150//------------------------------------------------------------------------------
151{
152 if (m_Items->Vector.size()<=idx)
153 {
154 m_Items->Vector.resize(idx+1);
155 }
156
157 m_Items->Vector[idx]=object;
158}
159
160//------------------------------------------------------------------------------
161template <class T>
163//------------------------------------------------------------------------------
164{
165 m_Items->Vector.push_back(object);
166 return m_Items->Vector.size()-1;
167}
168
169//------------------------------------------------------------------------------
170template <class T>
172//------------------------------------------------------------------------------
173{
174 if (idx>=0 && idx<m_Items->Vector.size())
175 {
176 obj=m_Items->Vector[idx];
177 return true;
178 }
179 else
180 {
181 return false;
182 }
183}
184
185//------------------------------------------------------------------------------
186template <class T>
188//------------------------------------------------------------------------------
189{
190 if (idx>=0 && idx<m_Items->Vector.size())
191 {
192 return m_Items->Vector[idx];
193 }
194 else
195 {
196 return m_NullItem;
197 }
198}
199
200//------------------------------------------------------------------------------
201template <class T>
203//------------------------------------------------------------------------------
204{
205 return (idx<m_Items->Vector.size())?m_Items->Vector[idx]:m_NullItem;
206}
207
208//------------------------------------------------------------------------------
209template <class T>
210const T &albaVector<T>::operator [](const albaID idx) const
211//------------------------------------------------------------------------------
212{
213 return (idx<m_Items->Vector.size())?m_Items->Vector[idx]:m_NullItem;
214}
215
216//------------------------------------------------------------------------------
217template <class T>
218bool albaVector<T>::ReplaceItem(albaID idx, const T &newitem)
219//------------------------------------------------------------------------------
220{
221 if (idx<m_Items->Vector.size())
222 {
223 m_Items->Vector[idx]=newitem;
224
225 return true;
226 }
227
228 return false;
229}
230
231//------------------------------------------------------------------------------
232template <class T>
234//------------------------------------------------------------------------------
235{
236 if (idx<m_Items->Vector.size())
237 {
238 m_Items->Vector.erase(m_Items->Vector.begin()+idx);
239
240 return true;
241 }
242
243 return false;
244}
245
246//------------------------------------------------------------------------------
247template <class T>
249//------------------------------------------------------------------------------
250{
251 albaID idx;
252 if (FindItem(object,idx))
253 {
254 return RemoveItem(idx);
255 }
256
257 return false;
258}
259
260//------------------------------------------------------------------------------
261template <class T>
263//------------------------------------------------------------------------------
264{
265 m_Items->Vector.clear();
266}
267
268//------------------------------------------------------------------------------
269template <class T>
270bool albaVector<T>::FindItem(const T &object, albaID &idx)
271//------------------------------------------------------------------------------
272{
273 for (albaID i=0;i<m_Items->Vector.size();i++)
274 if (m_Items->Vector[i]==object)
275 {
276 idx=i;
277 return true;
278 }
279
280 return false;
281}
282
283
284//------------------------------------------------------------------------------
285template <class T>
286bool albaVector<T>::IsItemPresent(const T &object)
287//------------------------------------------------------------------------------
288{
289 albaID idx;
290 return FindItem(object,idx);
291}
292
293//------------------------------------------------------------------------------
294template <class T>
296//------------------------------------------------------------------------------
297{
298 return AppendItem(object);
299}
300
301//------------------------------------------------------------------------------
302template <class T>
304//------------------------------------------------------------------------------
305{
306 if (GetItem(GetNumberOfItems()-1,obj))
307 {
308 RemoveItem(GetNumberOfItems()-1);
309 return true;
310 }
311
312 return false;
313}
314
315//------------------------------------------------------------------------------
316template <class T>
318//------------------------------------------------------------------------------
319{
320 return m_Items->Vector.size();
321}
322
323
324#endif
325
long albaID
type for IDs inside ALBA
Definition: albaDefines.h:58
albaBase - the pourpose of this class is just to groups utilities classes
Definition: albaBase.h:27
albaUtility - the pourpose of this class is just to groups utilities classes
Definition: albaUtility.h:27
albaVector - vector container wrapping STL vectors.
Definition: albaVector.h:33
albaID GetNumberOfItems()
return number of items in the array
Definition: albaVector.h:317
albaID AppendItem(const T &object)
Set an item with the specified Key.
Definition: albaVector.h:162
virtual ~albaVector()
Definition: albaVector.h:140
T & operator[](const albaID idx)
provide access to vector items.
Definition: albaVector.h:202
bool GetItem(const albaID idx, T &obj)
Get the item with given Key in the container.
Definition: albaVector.h:171
bool Pop(T &obj)
Retrieve an item.
Definition: albaVector.h:303
mmuVectorItems< T > * m_Items
Definition: albaVector.h:107
bool ReplaceItem(const albaID idx, const T &newitem)
Replace the item with the given Key in the container with a new one.
Definition: albaVector.h:218
albaID Push(const T &obj)
Return the number of objects in the list.
Definition: albaVector.h:295
T & GetItem(const albaID idx)
Get the item with given Key in the container.
Definition: albaVector.h:187
bool FindItem(const T &object, albaID &idx)
Return the item index.
Definition: albaVector.h:270
void SetItem(const albaID idx, const T &object)
Set an item with the specified Key.
Definition: albaVector.h:149
bool IsItemPresent(const T &object)
Search for an object and return its ID.
Definition: albaVector.h:286
bool RemoveItem(const albaID idx)
Remove the item with given Key in the container.
Definition: albaVector.h:233
void RemoveAllItems()
Remove all objects from the list.
Definition: albaVector.h:262
bool FindAndRemoveItem(const T &object)
Remove an object from the list.
Definition: albaVector.h:248
static T m_NullItem
Definition: albaVector.h:103
std::vector< T > Vector
Definition: albaVector.h:127