MeVisLab Toolbox Reference
WEMContainer.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
16#include "WEMVector.h"
17
18
19ML_START_NAMESPACE
20
22
25template <class T>
26class WEMContainer : public WEMVector<WEMPrimitive>
27{
28public:
29
31 WEMContainer(unsigned int init = 0);
32 WEMContainer(WEMContainer &&) noexcept = default;
33 WEMContainer& operator=(WEMContainer &&) noexcept = default;
35 ~WEMContainer() override;
37 T* at(unsigned int pos);
39 const T* at(unsigned int pos) const;
41 void swap(unsigned int i,unsigned int j) override;
44 unsigned int append(WEMPrimitive *wp) override;
47 int remove(WEMPrimitive *wp) override;
49 void replace(WEMPrimitive *wp,unsigned int pos) override;
51 void destroy() override;
52};
53
55
56template <class T>
57WEMContainer<T>::WEMContainer(unsigned int init) :
59{
60}
61
63
64template <class T>
68
70
71template <class T>
72T* WEMContainer<T>::at(unsigned int pos)
73{
75 if (wp)
76 {
77 return reinterpret_cast<T*>(wp);
78 }
79 else
80 {
81 return nullptr;
82 }
83}
84
86
87template <class T>
88const T* WEMContainer<T>::at(unsigned int pos) const
89{
90 return at(pos);
91}
92
94
95template <class T>
97{
98 // This is the only safe way to delete entries in this vector!!
99 //
100 // Everything else (no overloading, parent call, direct block access)
101 // goes to WEMVector and causes 'double free or corruption'
102
103 const unsigned int n = num();
104
105 for (unsigned int i=0;i<n;i++)
106 {
107 if (at(i))
108 {
109 T* toDel = at(i);
110 delete toDel;
111 toDel = nullptr;
112 }
114 }
115 clear();
116}
117
119
120template <class T>
121void WEMContainer<T>::swap(unsigned int i, unsigned int j)
122{
124 at(i)->setHeapPosition(i);
125 at(j)->setHeapPosition(j);
126}
127
129
130template <class T>
132{
133 if (wp == nullptr) { return (num()-1); }
134 if (wp->getHeapPosition() != -1) { return (num()-1); }
135
136 const int vecLength = WEMVector<WEMPrimitive>::appendUnsafe(wp);
137 last()->setHeapPosition(num()-1);
138 return vecLength;
139}
140
142
143template <class T>
144void WEMContainer<T>::replace(WEMPrimitive *wp, unsigned int pos)
145{
147 if (wp)
148 {
149 wp->setHeapPosition(pos);
150 }
151}
152
154
155template <class T>
157{
158 if (wp == nullptr) { return -1; }
159 const int i = wp->getHeapPosition();
160 const int n = num();
161 if (i >= n) { return -1; }
162 if (i <= -1) { return -1; }
163
164 if (i != n - 1)
165 {
166 swap(i,n - 1);
167 }
168 at(n - 1)->setHeapPosition(-1);
169 deleteLast();
170
171 return i;
172}
173
175
176ML_END_NAMESPACE
@ T
Container with the ability to detect double inserts.
WEMContainer(WEMContainer &&) noexcept=default
This is the base class for the WEM elements nodes, edges, and faces.
int getHeapPosition() const
Returns the heap position.
void setHeapPosition(int heapPosition)
Sets the heap position.
Dynamic templated vector.
Definition WEMVector.h:28