MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
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
20
22
25template <class T> class WEMContainer : public WEMVector<WEMPrimitive>
26{
27public:
29 WEMContainer(unsigned int init = 0);
35 T* at(unsigned int pos);
37 const T* at(unsigned int pos) const;
39 void swap(unsigned int i, unsigned int j) override;
42 unsigned int append(WEMPrimitive* wp) override;
45 int remove(WEMPrimitive* wp) override;
47 void replace(WEMPrimitive* wp, unsigned int pos) override;
49 void destroy() override;
50};
51
53
54template <class T> WEMContainer<T>::WEMContainer(unsigned int init)
55 : WEMVector<WEMPrimitive>(init)
56{}
57
59
61{}
62
64
65template <class T> T* WEMContainer<T>::at(unsigned int pos)
66{
68 if (wp) {
69 return reinterpret_cast<T*>(wp);
70 }
71 else {
72 return nullptr;
73 }
74}
75
77
78template <class T> const T* WEMContainer<T>::at(unsigned int pos) const
79{
80 return at(pos);
81}
82
84
85template <class T> void WEMContainer<T>::destroy()
86{
87 // This is the only safe way to delete entries in this vector!!
88 //
89 // Everything else (no overloading, parent call, direct block access)
90 // goes to WEMVector and causes 'double free or corruption'
91
92 const unsigned int n = num();
93
94 for (unsigned int i = 0; i < n; i++) {
95 if (at(i)) {
96 T* toDel = at(i);
97 delete toDel;
98 toDel = nullptr;
99 }
101 }
102 clear();
103}
104
106
107template <class T> void WEMContainer<T>::swap(unsigned int i, unsigned int j)
108{
110 at(i)->setHeapPosition(i);
111 at(j)->setHeapPosition(j);
112}
113
115
116template <class T> unsigned int WEMContainer<T>::append(WEMPrimitive* wp)
117{
118 if (wp == nullptr) {
119 return (num() - 1);
120 }
121 if (wp->getHeapPosition() != -1) {
122 return (num() - 1);
123 }
124
126 last()->setHeapPosition(num() - 1);
127 return vecLength;
128}
129
131
132template <class T> void WEMContainer<T>::replace(WEMPrimitive* wp, unsigned int pos)
133{
135 if (wp) {
136 wp->setHeapPosition(pos);
137 }
138}
139
141
143{
144 if (wp == nullptr) {
145 return -1;
146 }
147 const int i = wp->getHeapPosition();
148 const int n = num();
149 if (i >= n) {
150 return -1;
151 }
152 if (i <= -1) {
153 return -1;
154 }
155
156 if (i != n - 1) {
157 swap(i, n - 1);
158 }
159 at(n - 1)->setHeapPosition(-1);
160 deleteLast();
161
162 return i;
163}
164
166
@ 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.
Dynamic templated vector.
Definition WEMVector.h:27
Target mlrange_cast(Source arg)
Generic version of checked ML casts.