MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
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> class WEMObjectVector
28{
29public:
31 WEMObjectVector(unsigned int init = 0, unsigned int bs = 8192);
39 unsigned int num() const
40 {
41 return _length;
42 }
44 T* append(unsigned int* pos);
46 void deleteAt(unsigned int pos);
48 void destroy();
50 void createNumObjects(unsigned int numObjects);
52 T* at(unsigned int pos) const;
53
54private:
56 unsigned int _length;
58 unsigned int _capacity;
60 unsigned int _BLOCKSIZE;
62 std::vector<T*> _block;
64 std::vector<unsigned int> _deleted;
65
66private:
68 void expand();
69};
70
73
74namespace WEMInternal
75{
76 template <typename T> using object_reset_t = decltype(std::declval<T&>().reset());
77
78 template <typename T> constexpr bool has_reset_v = is_detected_v<object_reset_t, T>;
79} // namespace WEMInternal
80
81
82template <class T> WEMObjectVector<T>::WEMObjectVector(unsigned int init, unsigned int bs)
83 : _length(0)
84 , _capacity(0)
85 , _BLOCKSIZE(bs)
86{
87 if (init > 0) {
88 const unsigned int iBS = init / _BLOCKSIZE + 1;
89 _block.resize(iBS);
90 for (unsigned int i = 0; i < iBS; i++) {
91 _block[i] = new T[_BLOCKSIZE];
92 }
93 _capacity = iBS * _BLOCKSIZE;
94 }
95}
96
98
100{
101 destroy();
102}
103
105{
106 *this = std::move(other);
107}
108
110{
111 _length = other._length;
112 other._length = 0;
113 _capacity = other._capacity;
114 other._capacity = 0;
115 _BLOCKSIZE = other._BLOCKSIZE;
116 other._BLOCKSIZE = 0;
117 _block = std::move(other._block);
118 _deleted = std::move(other._deleted);
119 return *this;
120}
121
123
124template <class T> void WEMObjectVector<T>::destroy()
125{
126 if (_capacity > 0) {
127 for (size_t i = 0; i < _block.size(); i++) {
128 delete[] _block[i];
129 _block[i] = nullptr;
130 }
131 _block.clear();
132 }
133 _deleted.clear();
134 _length = _capacity = 0;
135}
136
138
139template <class T> void WEMObjectVector<T>::expand()
140{
141 T* newBlock = nullptr;
142 newBlock = new T[_BLOCKSIZE];
143 _block.push_back(newBlock);
144
145 _capacity += _BLOCKSIZE;
146}
147
149
150template <class T> T* WEMObjectVector<T>::append(unsigned int* pos)
151{
152 T* elem = nullptr;
153
154 if (!_deleted.empty()) {
155 *pos = _deleted.back();
156 _deleted.pop_back();
157 elem = &(_block[*pos / _BLOCKSIZE][*pos % _BLOCKSIZE]);
158
159 // All type T must implement ::reset(). This should not be done via virtual
160 // inheritance because the virtual base function hides the necessity to
161 // implement this function in all derived classes.
162 static_assert(WEMInternal::has_reset_v<T>,
163 "Type T must implement ::reset() to reset the object back to the default constructed state.");
164 elem->reset();
165
166 return elem;
167 }
168
169 if (_length == _capacity) {
170 expand();
171 }
172
173 elem = &(_block[_length / _BLOCKSIZE][_length % _BLOCKSIZE]);
174
175 *pos = _length;
176 _length++;
177 return elem;
178}
179
181
182template <class T> void WEMObjectVector<T>::deleteAt(unsigned int pos)
183{
184 _deleted.push_back(pos);
185}
186
188
189template <class T> void WEMObjectVector<T>::createNumObjects(unsigned int numObjects)
190{
191 const auto numBlocks = (numObjects + _BLOCKSIZE - 1) / _BLOCKSIZE;
192 for (unsigned int i = 0; i < numBlocks; ++i) {
193 expand();
194 }
195 _length = numObjects;
196}
197
199
200template <class T> T* WEMObjectVector<T>::at(unsigned int pos) const
201{
202 if (pos < _length) {
203 return &(_block[pos / _BLOCKSIZE][pos % _BLOCKSIZE]);
204 }
205 return nullptr;
206}
207
209
@ 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.
void createNumObjects(unsigned int numObjects)
Creates the given number of objects.
WEMObjectVector & operator=(WEMObjectVector &&other) noexcept
Move assignment operator.
T * at(unsigned int pos) const
Returns the element at the given position.
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