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 
15 #include "WEMBase/WEMPrimitive.h"
16 #include "WEMVector.h"
17 
18 
19 ML_START_NAMESPACE
20 
22 
25 template <class T>
26 class WEMContainer : public WEMVector<WEMPrimitive>
27 {
28 public:
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 
56 template <class T>
57 WEMContainer<T>::WEMContainer(unsigned int init) :
59 {
60 }
61 
63 
64 template <class T>
66 {
67 }
68 
70 
71 template <class T>
72 T* 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 
87 template <class T>
88 const T* WEMContainer<T>::at(unsigned int pos) const
89 {
90  return at(pos);
91 }
92 
94 
95 template <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 
120 template <class T>
121 void WEMContainer<T>::swap(unsigned int i, unsigned int j)
122 {
124  at(i)->setHeapPosition(i);
125  at(j)->setHeapPosition(j);
126 }
127 
129 
130 template <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 
143 template <class T>
144 void WEMContainer<T>::replace(WEMPrimitive *wp, unsigned int pos)
145 {
147  if (wp)
148  {
149  wp->setHeapPosition(pos);
150  }
151 }
152 
154 
155 template <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 
176 ML_END_NAMESPACE
@ T
Definition: SoKeyGrabber.h:71
Container with the ability to detect double inserts.
Definition: WEMContainer.h:27
WEMContainer(WEMContainer &&) noexcept=default
This is the base class for the WEM elements nodes, edges, and faces.
Definition: WEMPrimitive.h:28
int getHeapPosition() const
Returns the heap position.
Definition: WEMPrimitive.h:58
void setHeapPosition(int heapPosition)
Sets the heap position.
Definition: WEMPrimitive.h:62
Dynamic templated vector.
Definition: WEMVector.h:28
IteratorTraits::value_type & at(const ml_iterator_map< IteratorTraits, IDMap > &i, typename property_traits< IDMap >::key_type key)
void init()
Initializes the ML, the runtime type system, the memory manager, fields, static buffers,...
void destroy()
Delete dynamic data structures allocated by init().