ICF 3.1.1.10
Technical documentation of ICF Libraries
TPointerVector.h
Go to the documentation of this file.
1/********************************************************************************
2** This file is part of the ICF Framework. Copyright (C) Witold Gantzke & Kirill Lepskiy
3** ICF Framework may be used under the terms of the LGPL License v. 2.1 by the Free Software Foundation.
4********************************************************************************/
5
6#pragma once
7
8
9// STL includes
10#include <vector>
11
12// ICF includes
13#include <istd/istd.h>
14
15
16namespace istd
17{
18
19
23template<typename Pointer>
25{
26public:
27 typedef Pointer* ElementType;
28
29 static Pointer* GetPtr(const ElementType& element)
30 {
31 return element;
32 }
33
34 static Pointer* release(const ElementType& element)
35 {
36 return GetPtr(element);
37 }
38
39 static void Delete(const ElementType& element)
40 {
41 delete element;
42 }
43};
44
45
50template <typename Pointer, class AccessAdapter = TDeleteAdapter<Pointer> >
52{
53public:
54 typedef typename AccessAdapter::ElementType ElementType;
55
56 enum
57 {
61 InvalidIndex = -1
62 };
63
66
68
72 bool IsEmpty() const;
73
77 int GetCount() const;
78
82 void SetCount(int count);
83
87 void Reset();
88
93 int HasElement(const Pointer* elementPtr) const;
94
98 Pointer* GetAt(int index) const;
99
104 const ElementType& GetElementAt(int index) const;
105
109 void SetElementAt(int index, const ElementType& element);
110
116 void RemoveAt(int index);
117
124 bool Remove(Pointer* elementPtr);
125
130 Pointer* PopAt(int index);
131
135 void PushBack(const ElementType& element);
136
140 void InsertElementAt(int index, const ElementType& element);
141
146 void SwapElements(int index1, int index2);
147
148private:
149 typedef std::vector<ElementType> Elements;
150
151 Elements m_elements;
152};
153
154
155// inline methods
156
157template <typename Pointer, class AccessAdapter>
161
162
163template <typename Pointer, class AccessAdapter>
165{
166 Q_ASSERT(otherVector.IsEmpty());
167}
168
169
170template <typename Pointer, class AccessAdapter>
172{
173 return m_elements.empty();
174}
175
176
177template <typename Pointer, class AccessAdapter>
179{
180 return int(m_elements.size());
181}
182
183
184// public methods
185
186template <typename Pointer, class AccessAdapter>
191
192
193template <typename Pointer, class AccessAdapter>
195{
196 while (int(m_elements.size()) > count){
197 AccessAdapter::Delete(m_elements.back());
198
199 m_elements.pop_back();
200 }
201
202 m_elements.resize(count);
203}
204
205
206template <typename Pointer, class AccessAdapter>
208{
209 for ( typename Elements::iterator iter = m_elements.begin();
210 iter != m_elements.end();
211 ++iter){
212 AccessAdapter::Delete(*iter);
213 }
214
215 m_elements.clear();
216}
217
218
219template <typename Pointer, class AccessAdapter>
220int TPointerVector<Pointer, AccessAdapter>::HasElement(const Pointer* elementPtr) const
221{
222 int elementsCount = GetCount();
223
224 for (int elementIndex = 0; elementIndex < elementsCount; elementIndex++){
225 typename Elements::const_iterator delIter = (m_elements.begin() + elementIndex);
226 if (AccessAdapter::GetPtr(*delIter) == elementPtr){
227 return elementIndex;
228 }
229 }
230
231 return InvalidIndex;
232}
233
234
235template <typename Pointer, class AccessAdapter>
237{
238 Q_ASSERT(index >= 0);
239 Q_ASSERT(index < int(m_elements.size()));
240
241 return AccessAdapter::GetPtr(m_elements[index]);
242}
243
244
245template <typename Pointer, class AccessAdapter>
247{
248 Q_ASSERT(index >= 0);
249 Q_ASSERT(index < int(m_elements.size()));
250
251 return m_elements[index];
252}
253
254
255template <typename Pointer, class AccessAdapter>
257{
258 typename Elements::iterator delIter = (m_elements.begin() + index);
259
260 AccessAdapter::Delete(*delIter);
261
262 m_elements[index] = element;
263}
264
265
266template <typename Pointer, class AccessAdapter>
268{
269 Q_ASSERT(index >= 0);
270 Q_ASSERT(index < int(m_elements.size()));
271
272 typename Elements::iterator delIter = (m_elements.begin() + index);
273
274 AccessAdapter::Delete(*delIter);
275
276 m_elements.erase(delIter);
277}
278
279
280template <typename Pointer, class AccessAdapter>
282{
283 int elementsCount = GetCount();
284
285 for (int elementIndex = 0; elementIndex < elementsCount; elementIndex++){
286 typename Elements::iterator delIter = (m_elements.begin() + elementIndex);
287 if (AccessAdapter::GetPtr(*delIter) == elementPtr){
288 RemoveAt(elementIndex);
289
290 return true;
291 }
292 }
293
294 return false;
295}
296
297
298template <typename Pointer, class AccessAdapter>
300{
301 Q_ASSERT(index >= 0);
302 Q_ASSERT(index < int(m_elements.size()));
303
304 Pointer* popPtr = AccessAdapter::release(m_elements[index]);
305
306 m_elements.erase(m_elements.begin() + index);
307
308 return popPtr;
309}
310
311
312template <typename Pointer, class AccessAdapter>
314{
315 m_elements.push_back(element);
316}
317
318
319template <typename Pointer, class AccessAdapter>
321{
322 Q_ASSERT(index >= 0);
323 Q_ASSERT(index <= GetCount());
324 Q_ASSERT(HasElement(AccessAdapter::GetPtr(element)) == InvalidIndex);
325
326 m_elements.insert(m_elements.begin() + index, element);
327}
328
329
330template <typename Pointer, class AccessAdapter>
332{
333 Q_ASSERT(index1 >= 0);
334 Q_ASSERT(index1 <= GetCount());
335 Q_ASSERT(index2 >= 0);
336 Q_ASSERT(index2 <= GetCount());
337
338 ElementType element1 = m_elements[index1];
339 m_elements[index1] = m_elements[index2];
340 m_elements[index2] = element1;
341}
342
343
344} // namespace istd
345
346
Definition of single plane bitmap.
Definition IBitmap.h:24
Default delete adapter.
static Pointer * release(const ElementType &element)
static Pointer * GetPtr(const ElementType &element)
static void Delete(const ElementType &element)
Implementation of a pointer container, which controls the live cycle of the pointer object.
bool Remove(Pointer *elementPtr)
Remove element elementPtr.
Pointer * PopAt(int index)
Pop element at specified index.
void SetCount(int count)
Set number of elements.
int HasElement(const Pointer *elementPtr) const
Check if some element is stored in this vector and return the index of the element,...
TPointerVector(const TPointerVector &)
Pointer * GetAt(int index) const
Get pointer at specified index.
bool IsEmpty() const
Check if there is element stored in this set.
int GetCount() const
Get number of stored elements.
void SwapElements(int index1, int index2)
Switch two elements.
void RemoveAt(int index)
Remove element at specified index.
void SetElementAt(int index, const ElementType &element)
Set element at specified index.
void PushBack(const ElementType &element)
Add new element at the end of collection.
void Reset()
Remove all elements.
void InsertElementAt(int index, const ElementType &element)
Insert element at specified position.
@ InvalidIndex
Invalid index in the vector.
const ElementType & GetElementAt(int index) const
Get element at specified index.
AccessAdapter::ElementType ElementType
Standard library.
Definition IComponent.h:17

© Witold Gantzke and Kirill Lepskiy