MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
WEMIndexVector.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2007, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#pragma once
14
15#include "WEMObjectVector.h"
16
17#include <mlContainerHelpers.h>
18
20
22
24template <class T> class WEMIndexVector
25{
26public:
28 WEMIndexVector(unsigned int init = 0, unsigned int bs = 8192);
29 WEMIndexVector(WEMIndexVector&& other) noexcept = default;
34 unsigned int num() const
35 {
36 return static_cast<unsigned int>(_pointers.size());
37 }
39 T* at(unsigned int pos) const
40 {
41 return (pos < _pointers.size()) ? _pointers[pos] : nullptr;
42 }
45 {
46 return at(0);
47 }
49 const T* first() const
50 {
51 return first();
52 }
57 int remove(T* t);
59 void destroy();
62 void reorder(const std::vector<unsigned int>& indexOrder);
64 void reserve(unsigned int numObjects);
66 void createNumObjects(unsigned int numObjects);
67
68private:
70 void swap(unsigned int i, unsigned int j);
72 std::vector<T*> _pointers;
74 std::vector<int> _positions;
76 WEMObjectVector<T> _objects;
77};
78
80
82template <typename T> using WEMIndexVectorProxy =
84
86
87template <class T> WEMIndexVector<T>::WEMIndexVector(unsigned int init, unsigned int bs)
88 : _objects(init, bs)
89{
90 _pointers.reserve(init);
91 _positions.reserve(init);
92}
93
95
97{
98 destroy();
99}
100
102
103template <class T> void WEMIndexVector<T>::destroy()
104{
105 _pointers.clear();
106 _positions.clear();
107 _objects.destroy();
108}
109
111
112template <class T> void WEMIndexVector<T>::swap(unsigned int i, unsigned int j)
113{
114 T* tTemp = nullptr;
115 int pTemp;
116 if (i != j) {
117 T*& pi = _pointers[i];
118 T*& pj = _pointers[j];
119 tTemp = pi;
120 pi = pj;
121 pj = tTemp;
122
123 pTemp = _positions[i];
124 _positions[i] = _positions[j];
125 _positions[j] = pTemp;
126
127 _pointers[i]->setEntryNumber(static_cast<int>(i));
128 _pointers[j]->setEntryNumber(static_cast<int>(j));
129 }
130}
131
133
134template <class T> T* WEMIndexVector<T>::append()
135{
136 unsigned int pos;
137 T* t = _objects.append(&pos);
138 if (t) {
139 int entryNumber = static_cast<int>(_pointers.size());
140 _pointers.push_back(t);
141 _positions.push_back(static_cast<int>(pos));
142 t->setEntryNumber(entryNumber);
143 }
144 return t;
145}
146
148
149template <class T> int WEMIndexVector<T>::remove(T* t)
150{
151 if (t == nullptr) {
152 return -1;
153 }
154 const int i = t->getEntryNumber();
155 const int n = static_cast<int>(num());
156 if (i >= n) {
157 return -1;
158 }
159 if (i <= -1) {
160 return -1;
161 }
162
163 _objects.deleteAt(static_cast<unsigned int>(_positions[static_cast<unsigned int>(i)]));
164
165 if (i != n - 1) {
166 swap(static_cast<unsigned int>(i), static_cast<unsigned int>(n - 1));
167 }
168 at(static_cast<unsigned int>(n - 1))->setEntryNumber(-1);
169
170 _pointers.pop_back();
171 _positions.pop_back();
172
173 return i;
174}
175
177
178template <class T> void WEMIndexVector<T>::reorder(const std::vector<unsigned int>& indexOrder)
179{
180 if (indexOrder.size() == _pointers.size()) {
181 std::vector<T*> tempPointers;
182 std::vector<int> tempPositions;
183 tempPointers.resize(indexOrder.size());
184 tempPositions.resize(indexOrder.size());
185
186 for (size_t i = 0; i < indexOrder.size(); ++i) {
187 const unsigned int index = indexOrder.at(i);
188 tempPointers[i] = _pointers.at(index);
189 tempPositions[i] = _positions.at(index);
190 }
191 for (size_t i = 0; i < indexOrder.size(); ++i) {
192 _pointers[i] = tempPointers.at(i);
193 _positions[i] = tempPositions.at(i);
194 at(static_cast<unsigned int>(i))->setEntryNumber(static_cast<int>(i));
195 }
196 }
197 else {
198 WEM_ERROR("Vector of reordered indices must have the same size as the internal vectors!");
199 }
200}
201
203
204template <class T> void WEMIndexVector<T>::reserve(unsigned int numObjects)
205{
206 _pointers.assign(numObjects, nullptr);
207 _positions.assign(numObjects, -1);
208 _objects.reserve(numObjects);
209}
210
212
213template <class T> void WEMIndexVector<T>::createNumObjects(unsigned int numObjects)
214{
215 _pointers.assign(numObjects, nullptr);
216 _positions.assign(numObjects, -1);
217 _objects.createNumObjects(numObjects);
218
219 for (unsigned int i = 0; i < numObjects; ++i) {
220 T* object = _objects.at(i);
221 object->setEntryNumber(static_cast<int>(i));
222 _pointers[i] = object;
223 _positions[i] = static_cast<int>(i);
224 }
225}
226
#define WEM_ERROR(msg)
Defines an output routine for error messages.
@ T
This template implements a proxy for containers that contain a collection of items normally accessed ...
Dynamic vector, based on a memory pool. Keeps track on entry numbers of stored primitives.
void createNumObjects(unsigned int numObjects)
Creates the given number of new objects.
void reserve(unsigned int numObjects)
Reserves memory for the given number of objects.
WEMIndexVector(unsigned int init=0, unsigned int bs=8192)
Standard constructor.
WEMIndexVector(WEMIndexVector &&other) noexcept=default
WEMIndexVector & operator=(WEMIndexVector &&other) noexcept=default
T * first()
Returns the first element of this vector.
const T * first() const
Returns the first element of this vector.
void destroy()
Destroys all elements in this vector.
~WEMIndexVector()
Standard destructor.
unsigned int num() const
Returns the number of elements in this vector.
T * at(unsigned int pos) const
Returns the element at the given position, typecast from WEMPrimitive to T.
int remove(T *t)
Removes the given element from vector.
void reorder(const std::vector< unsigned int > &indexOrder)
Sets the order by the given vector of indices.
T * append()
Appends an element to this vector, returns it and updates the index.
Dynamic template vector.
Target mlrange_cast(Source arg)
Generic version of checked ML casts.