MeVisLab Toolbox Reference
WEMObjectVector.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 "MLWEMIncludes.h"
16
17#include <mlTemplateHelpers.h>
18
20
22
27template <class T>
29{
30public:
32 WEMObjectVector(unsigned int init = 0, unsigned int bs = 8192);
40 unsigned int num() const { return _length; }
42 T* append(unsigned int *pos);
44 void deleteAt(unsigned int pos);
46 void destroy();
47
48private:
49
51 unsigned int _length;
53 unsigned int _capacity;
55 unsigned int _BLOCKSIZE;
57 std::vector<T*> _block;
59 std::vector<unsigned int> _deleted;
60
61private:
63 void expand();
64};
65
68
69namespace WEMInternal
70{
71 template <typename T>
72 using object_reset_t = decltype(std::declval<T&>().reset());
73
74 template <typename T>
76}
77
78
79template <class T>
80WEMObjectVector<T>::WEMObjectVector(unsigned int init,unsigned int bs)
81 : _length(0)
82 , _capacity(0)
83 , _BLOCKSIZE(bs)
84{
85 if (init > 0)
86 {
87 const unsigned int iBS = init/_BLOCKSIZE+1;
88 _block.resize(iBS);
89 for (unsigned int i = 0; i < iBS; i++)
90 {
91 _block[i] = new T[_BLOCKSIZE];
92 }
93 _capacity = iBS * _BLOCKSIZE;
94 }
95}
96
98
99template <class T>
101{
102 destroy();
103}
104
105template <class T>
107{
108 *this = std::move(other);
109}
110
111template <class T>
113{
114 _length = other._length;
115 other._length = 0;
116 _capacity = other._capacity;
117 other._capacity = 0;
118 _BLOCKSIZE = other._BLOCKSIZE;
119 other._BLOCKSIZE = 0;
120 _block = std::move(other._block);
121 _deleted = std::move(other._deleted);
122 return *this;
123}
124
126
127template <class T>
129{
130 if (_capacity > 0)
131 {
132 for (size_t i=0;i<_block.size();i++)
133 {
134 delete [] _block[i]; _block[i] = nullptr;
135 }
136 _block.clear();
137 }
138 _deleted.clear();
139 _length = _capacity = 0;
140}
141
143
144template <class T>
146{
147 T* newBlock = nullptr;
148 newBlock = new T[_BLOCKSIZE];
149 _block.push_back(newBlock);
150
151 _capacity += _BLOCKSIZE;
152}
153
155
156template <class T>
157T* WEMObjectVector<T>::append(unsigned int *pos)
158{
159 T *elem = nullptr;
160
161 if (!_deleted.empty())
162 {
163 *pos = _deleted.back();
164 _deleted.pop_back();
165 elem = &(_block[*pos/_BLOCKSIZE][*pos%_BLOCKSIZE]);
166
167 // All type T must implement ::reset(). This should not be done via virtual
168 // inheritance because the virtual base function hides the necessity to
169 // implement this function in all derived classes.
170 static_assert(WEMInternal::has_reset_v<T>, "Type T must implement ::reset() to reset the object back to the default constructed state.");
171 elem->reset();
172
173 return elem;
174 }
175
176 if (_length == _capacity)
177 {
178 expand();
179 }
180
181 elem = &(_block[_length/_BLOCKSIZE][_length%_BLOCKSIZE]);
182
183 *pos = _length;
184 _length ++;
185 return elem;
186}
187
189
190template <class T>
191void WEMObjectVector<T>::deleteAt(unsigned int pos)
192{
193 _deleted.push_back(pos);
194}
195
197
@ T
Dynamic template vector.
unsigned int num() const
Returns the number of elements in this vector.
WEMObjectVector(unsigned int init=0, unsigned int bs=8192)
Standard constructor.
WEMObjectVector & operator=(WEMObjectVector &&other) noexcept
Move assignment operator.
void destroy()
Deletes all elements in this vector.
T * append(unsigned int *pos)
Appends an element to this vector and returns a pointer to it.
~WEMObjectVector()
Standard destructor.
void deleteAt(unsigned int pos)
Deletes an element at the given position.
WEMObjectVector(WEMObjectVector &&other) noexcept
Move c'tor.
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
constexpr bool has_reset_v
decltype(std::declval< T & >().reset()) object_reset_t