MeVisLab Toolbox Reference
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>
26{
27public:
28
30 WEMIndexVector(unsigned int init = 0,unsigned int bs = 8192);
31 WEMIndexVector(WEMIndexVector &&other) noexcept = default;
36 unsigned int num() const {return static_cast<unsigned int>(_pointers.size());}
38 T* at(unsigned int pos) const { return (pos<_pointers.size()) ? _pointers[pos] : nullptr; }
40 T* first() { return at(0); }
42 const T* first() const { return first(); }
47 int remove(T *t);
49 void destroy();
52 void reorder(const std::vector<unsigned int>& indexOrder);
53
54private:
55
57 void swap(unsigned int i, unsigned int j);
59 std::vector<T*> _pointers;
61 std::vector<int> _positions;
63 WEMObjectVector<T> _objects;
64};
65
67
69template <typename T>
71
73
74template <class T>
75WEMIndexVector<T>::WEMIndexVector(unsigned int init,unsigned int bs):_objects(init,bs)
76{
77 _pointers.reserve(init);
78 _positions.reserve(init);
79}
80
82
83template <class T>
85{
86 destroy();
87}
88
90
91template <class T>
93{
94 _pointers.clear();
95 _positions.clear();
96 _objects.destroy();
97}
98
100
101template <class T>
102void WEMIndexVector<T>::swap(unsigned int i,unsigned int j)
103{
104 T* tTemp = nullptr;
105 int pTemp;
106 if (i != j)
107 {
108 T*& pi = _pointers[i];
109 T*& pj = _pointers[j];
110 tTemp = pi;
111 pi = pj;
112 pj = tTemp;
113
114 pTemp = _positions[i];
115 _positions[i] = _positions[j];
116 _positions[j] = pTemp;
117
118 _pointers[i]->setEntryNumber(i);
119 _pointers[j]->setEntryNumber(j);
120 }
121}
122
124
125template <class T>
127{
128 unsigned int pos;
129 T *t = _objects.append(&pos);
130 if (t)
131 {
132 unsigned int entryNumber = static_cast<unsigned int>(_pointers.size());
133 _pointers.push_back(t);
134 _positions.push_back(pos);
135 t->setEntryNumber(entryNumber);
136 }
137 return t;
138}
139
141
142template <class T>
144{
145 if (t == nullptr) { return -1; }
146 const int i = t->getEntryNumber();
147 const int n = num();
148 if (i >= n) { return -1; }
149 if (i <= -1) { return -1; }
150
151 _objects.deleteAt(_positions[i]);
152
153 if (i != n - 1)
154 {
155 swap(i,n - 1);
156 }
157 at(n - 1)->setEntryNumber(-1);
158
159 _pointers.pop_back();
160 _positions.pop_back();
161
162 return i;
163}
164
166
167template <class T>
168void WEMIndexVector<T>::reorder(const std::vector<unsigned int>& indexOrder)
169{
170 if (indexOrder.size() == _pointers.size())
171 {
172 std::vector<T*> tempPointers;
173 std::vector<int> tempPositions;
174 tempPointers.resize(indexOrder.size());
175 tempPositions.resize(indexOrder.size());
176
177 for (size_t i = 0; i < indexOrder.size(); ++i)
178 {
179 const unsigned int index = indexOrder.at(i);
180 tempPointers[i] = _pointers.at(index);
181 tempPositions[i] = _positions.at(index);
182 }
183 for (size_t i = 0; i < indexOrder.size(); ++i)
184 {
185 _pointers[i] = tempPointers.at(i);
186 _positions[i] = tempPositions.at(i);
187 at(static_cast<unsigned int>(i))->setEntryNumber(static_cast<int>(i));
188 }
189 }
190 else
191 {
192 WEM_ERROR("Vector of re-ordered indices must have the same size as the internal vectors!");
193 }
194}
195
197
#define WEM_ERROR(msg)
Defines an output routine for error messages.
@ T
This template implements a proxy for container that contain a collection of items that are normally a...
Dynamic vector, based on a memory pool. Keeps track on entry numbers of stored primitives.
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.