DataContainer.cpp 12.9 KB
Newer Older
Tobias Lasser's avatar
Tobias Lasser committed
1
2
#include "DataContainer.h"
#include "DataHandlerCPU.h"
3
4
#include "DataHandlerMapCPU.h"
#include "BlockDescriptor.h"
Tobias Lasser's avatar
Tobias Lasser committed
5
6
7
8

#include <stdexcept>
#include <utility>

Jens Petit's avatar
Jens Petit committed
9
10
namespace elsa
{
Tobias Lasser's avatar
Tobias Lasser committed
11
12

    template <typename data_t>
Jens Petit's avatar
Jens Petit committed
13
14
15
16
17
18
19
20
21
22
23
24
25
    DataContainer<data_t>::DataContainer(const DataDescriptor& dataDescriptor,
                                         DataHandlerType handlerType)
        : _dataDescriptor{dataDescriptor.clone()},
          _dataHandler{createDataHandler(handlerType, _dataDescriptor->getNumberOfCoefficients())}
    {
    }

    template <typename data_t>
    DataContainer<data_t>::DataContainer(const DataDescriptor& dataDescriptor,
                                         const Eigen::Matrix<data_t, Eigen::Dynamic, 1>& data,
                                         DataHandlerType handlerType)
        : _dataDescriptor{dataDescriptor.clone()},
          _dataHandler{createDataHandler(handlerType, _dataDescriptor->getNumberOfCoefficients())}
Tobias Lasser's avatar
Tobias Lasser committed
26
27
28
    {
        if (_dataHandler->getSize() != data.size())
            throw std::invalid_argument("DataContainer: initialization vector has invalid size");
Jens Petit's avatar
Jens Petit committed
29

Tobias Lasser's avatar
Tobias Lasser committed
30
31
32
33
34
        for (index_t i = 0; i < _dataHandler->getSize(); ++i)
            (*_dataHandler)[i] = data[i];
    }

    template <typename data_t>
Jens Petit's avatar
Jens Petit committed
35
    DataContainer<data_t>::DataContainer(const DataContainer<data_t>& other)
36
        : _dataDescriptor{other._dataDescriptor->clone()}, _dataHandler{other._dataHandler->clone()}
Jens Petit's avatar
Jens Petit committed
37
38
    {
    }
Tobias Lasser's avatar
Tobias Lasser committed
39
40
41
42
43
44

    template <typename data_t>
    DataContainer<data_t>& DataContainer<data_t>::operator=(const DataContainer<data_t>& other)
    {
        if (this != &other) {
            _dataDescriptor = other._dataDescriptor->clone();
45
46
47
48
49
50

            if (_dataHandler) {
                *_dataHandler = *other._dataHandler;
            } else {
                _dataHandler = other._dataHandler->clone();
            }
Tobias Lasser's avatar
Tobias Lasser committed
51
52
53
54
55
56
        }

        return *this;
    }

    template <typename data_t>
Jens Petit's avatar
Jens Petit committed
57
    DataContainer<data_t>::DataContainer(DataContainer<data_t>&& other) noexcept
Tobias Lasser's avatar
Tobias Lasser committed
58
59
60
        : _dataDescriptor{std::move(other._dataDescriptor)},
          _dataHandler{std::move(other._dataHandler)}
    {
61
62
63
        // leave other in a valid state
        other._dataDescriptor = nullptr;
        other._dataHandler = nullptr;
Tobias Lasser's avatar
Tobias Lasser committed
64
65
66
    }

    template <typename data_t>
67
    DataContainer<data_t>& DataContainer<data_t>::operator=(DataContainer<data_t>&& other)
Tobias Lasser's avatar
Tobias Lasser committed
68
69
    {
        _dataDescriptor = std::move(other._dataDescriptor);
70
71
72
73
74
        if (_dataHandler) {
            *_dataHandler = std::move(*other._dataHandler);
        } else {
            _dataHandler = std::move(other._dataHandler);
        }
Tobias Lasser's avatar
Tobias Lasser committed
75

76
77
78
        // leave other in a valid state
        other._dataDescriptor = nullptr;
        other._dataHandler = nullptr;
Tobias Lasser's avatar
Tobias Lasser committed
79
80
81
82

        return *this;
    }

Jens Petit's avatar
Jens Petit committed
83
84
    template <typename data_t>
    const DataDescriptor& DataContainer<data_t>::getDataDescriptor() const
Tobias Lasser's avatar
Tobias Lasser committed
85
86
87
88
    {
        return *_dataDescriptor;
    }

Jens Petit's avatar
Jens Petit committed
89
    template <typename data_t>
Tobias Lasser's avatar
Tobias Lasser committed
90
91
92
93
94
    index_t DataContainer<data_t>::getSize() const
    {
        return _dataHandler->getSize();
    }

Jens Petit's avatar
Jens Petit committed
95
96
    template <typename data_t>
    data_t& DataContainer<data_t>::operator[](index_t index)
Tobias Lasser's avatar
Tobias Lasser committed
97
98
99
100
    {
        return (*_dataHandler)[index];
    }

Jens Petit's avatar
Jens Petit committed
101
102
    template <typename data_t>
    const data_t& DataContainer<data_t>::operator[](index_t index) const
Tobias Lasser's avatar
Tobias Lasser committed
103
    {
104
        return static_cast<const DataHandler<data_t>&>(*_dataHandler)[index];
Tobias Lasser's avatar
Tobias Lasser committed
105
106
    }

Jens Petit's avatar
Jens Petit committed
107
108
    template <typename data_t>
    data_t& DataContainer<data_t>::operator()(IndexVector_t coordinate)
Tobias Lasser's avatar
Tobias Lasser committed
109
    {
110
        return (*_dataHandler)[_dataDescriptor->getIndexFromCoordinate(std::move(coordinate))];
Tobias Lasser's avatar
Tobias Lasser committed
111
112
    }

Jens Petit's avatar
Jens Petit committed
113
114
    template <typename data_t>
    const data_t& DataContainer<data_t>::operator()(IndexVector_t coordinate) const
Tobias Lasser's avatar
Tobias Lasser committed
115
    {
116
        return static_cast<const DataHandler<data_t>&>(
117
            *_dataHandler)[_dataDescriptor->getIndexFromCoordinate(std::move(coordinate))];
Tobias Lasser's avatar
Tobias Lasser committed
118
119
    }

Jens Petit's avatar
Jens Petit committed
120
121
    template <typename data_t>
    data_t DataContainer<data_t>::dot(const DataContainer<data_t>& other) const
Tobias Lasser's avatar
Tobias Lasser committed
122
123
124
125
    {
        return _dataHandler->dot(*other._dataHandler);
    }

Jens Petit's avatar
Jens Petit committed
126
    template <typename data_t>
Tobias Lasser's avatar
Tobias Lasser committed
127
    data_t DataContainer<data_t>::squaredL2Norm() const
Tobias Lasser's avatar
Tobias Lasser committed
128
    {
Tobias Lasser's avatar
Tobias Lasser committed
129
130
131
132
133
134
135
136
137
138
139
140
141
        return _dataHandler->squaredL2Norm();
    }

    template <typename data_t>
    data_t DataContainer<data_t>::l1Norm() const
    {
        return _dataHandler->l1Norm();
    }

    template <typename data_t>
    data_t DataContainer<data_t>::lInfNorm() const
    {
        return _dataHandler->lInfNorm();
Tobias Lasser's avatar
Tobias Lasser committed
142
143
    }

Jens Petit's avatar
Jens Petit committed
144
    template <typename data_t>
Tobias Lasser's avatar
Tobias Lasser committed
145
146
147
148
149
    data_t DataContainer<data_t>::sum() const
    {
        return _dataHandler->sum();
    }

Jens Petit's avatar
Jens Petit committed
150
    template <typename data_t>
Tobias Lasser's avatar
Tobias Lasser committed
151
152
153
154
155
    DataContainer<data_t> DataContainer<data_t>::square() const
    {
        return DataContainer<data_t>(*_dataDescriptor, _dataHandler->square());
    }

Jens Petit's avatar
Jens Petit committed
156
    template <typename data_t>
Tobias Lasser's avatar
Tobias Lasser committed
157
158
159
160
161
    DataContainer<data_t> DataContainer<data_t>::sqrt() const
    {
        return DataContainer<data_t>(*_dataDescriptor, _dataHandler->sqrt());
    }

Jens Petit's avatar
Jens Petit committed
162
    template <typename data_t>
Tobias Lasser's avatar
Tobias Lasser committed
163
164
165
166
167
    DataContainer<data_t> DataContainer<data_t>::exp() const
    {
        return DataContainer<data_t>(*_dataDescriptor, _dataHandler->exp());
    }

Jens Petit's avatar
Jens Petit committed
168
    template <typename data_t>
Tobias Lasser's avatar
Tobias Lasser committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
    DataContainer<data_t> DataContainer<data_t>::log() const
    {
        return DataContainer<data_t>(*_dataDescriptor, _dataHandler->log());
    }

    template <typename data_t>
    DataContainer<data_t>& DataContainer<data_t>::operator+=(const DataContainer<data_t>& dc)
    {
        *_dataHandler += *dc._dataHandler;
        return *this;
    }

    template <typename data_t>
    DataContainer<data_t>& DataContainer<data_t>::operator-=(const DataContainer<data_t>& dc)
    {
        *_dataHandler -= *dc._dataHandler;
        return *this;
    }

    template <typename data_t>
    DataContainer<data_t>& DataContainer<data_t>::operator*=(const DataContainer<data_t>& dc)
    {
        *_dataHandler *= *dc._dataHandler;
        return *this;
    }

    template <typename data_t>
    DataContainer<data_t>& DataContainer<data_t>::operator/=(const DataContainer<data_t>& dc)
    {
        *_dataHandler /= *dc._dataHandler;
        return *this;
    }

    template <typename data_t>
    DataContainer<data_t>& DataContainer<data_t>::operator+=(data_t scalar)
    {
        *_dataHandler += scalar;
        return *this;
    }

    template <typename data_t>
    DataContainer<data_t>& DataContainer<data_t>::operator-=(data_t scalar)
    {
        *_dataHandler -= scalar;
        return *this;
    }

    template <typename data_t>
    DataContainer<data_t>& DataContainer<data_t>::operator*=(data_t scalar)
    {
        *_dataHandler *= scalar;
        return *this;
    }

    template <typename data_t>
    DataContainer<data_t>& DataContainer<data_t>::operator/=(data_t scalar)
    {
        *_dataHandler /= scalar;
        return *this;
    }

    template <typename data_t>
    DataContainer<data_t>& DataContainer<data_t>::operator=(data_t scalar)
    {
        *_dataHandler = scalar;
        return *this;
    }

Jens Petit's avatar
Jens Petit committed
237
238
    template <typename data_t>
    template <typename... Args>
Tobias Lasser's avatar
Tobias Lasser committed
239
    std::unique_ptr<DataHandler<data_t>>
Jens Petit's avatar
Jens Petit committed
240
241
        DataContainer<data_t>::createDataHandler(DataHandlerType handlerType, Args&&... args)
    {
Tobias Lasser's avatar
Tobias Lasser committed
242
243
244
245
246
247
248
249
        switch (handlerType) {
            case DataHandlerType::CPU:
                return std::make_unique<DataHandlerCPU<data_t>>(std::forward<Args>(args)...);
            default:
                throw std::invalid_argument("DataContainer: unknown handler type");
        }
    }

Jens Petit's avatar
Jens Petit committed
250
251
    template <typename data_t>
    DataContainer<data_t>::DataContainer(const DataDescriptor& dataDescriptor,
Tobias Lasser's avatar
Tobias Lasser committed
252
                                         std::unique_ptr<DataHandler<data_t>> dataHandler)
Jens Petit's avatar
Jens Petit committed
253
        : _dataDescriptor{dataDescriptor.clone()}, _dataHandler{std::move(dataHandler)}
Tobias Lasser's avatar
Tobias Lasser committed
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
    {
    }

    template <typename data_t>
    bool DataContainer<data_t>::operator==(const DataContainer<data_t>& other) const
    {
        if (*_dataDescriptor != *other._dataDescriptor)
            return false;

        if (*_dataHandler != *other._dataHandler)
            return false;

        return true;
    }

    template <typename data_t>
    bool DataContainer<data_t>::operator!=(const DataContainer<data_t>& other) const
    {
        return !(*this == other);
    }

Jens Petit's avatar
Jens Petit committed
275
    template <typename data_t>
276
    DataContainer<data_t> DataContainer<data_t>::getBlock(index_t i)
Jens Petit's avatar
Jens Petit committed
277
    {
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
        const auto blockDesc = dynamic_cast<const BlockDescriptor*>(_dataDescriptor.get());
        if (!blockDesc)
            throw std::logic_error("DataContainer: cannot get block from not-blocked container");

        if (i >= blockDesc->getNumberOfBlocks() || i < 0)
            throw std::invalid_argument("DataContainer: block index out of bounds");

        index_t startIndex = blockDesc->getOffsetOfBlock(i);
        const auto& ithDesc = blockDesc->getDescriptorOfBlock(i);
        index_t blockSize = ithDesc.getNumberOfCoefficients();

        return DataContainer<data_t>{ithDesc, _dataHandler->getBlock(startIndex, blockSize)};
    }

    template <typename data_t>
293
    DataContainer<data_t> DataContainer<data_t>::getBlock(index_t i) const
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
    {
        const auto blockDesc = dynamic_cast<const BlockDescriptor*>(_dataDescriptor.get());
        if (!blockDesc)
            throw std::logic_error("DataContainer: cannot get block from not-blocked container");

        if (i >= blockDesc->getNumberOfBlocks() || i < 0)
            throw std::invalid_argument("DataContainer: block index out of bounds");

        index_t startIndex = blockDesc->getOffsetOfBlock(i);
        const auto& ithDesc = blockDesc->getDescriptorOfBlock(i);
        index_t blockSize = ithDesc.getNumberOfCoefficients();

        // getBlock() returns a pointer to non-const DH, but that's fine as it gets wrapped in a
        // constant container
        return DataContainer<data_t>{ithDesc, _dataHandler->getBlock(startIndex, blockSize)};
    }

    template <typename data_t>
    DataContainer<data_t> DataContainer<data_t>::viewAs(const DataDescriptor& dataDescriptor)
    {
        if (dataDescriptor.getNumberOfCoefficients() != getSize())
            throw std::invalid_argument("DataContainer: view must have same size as container");

        return DataContainer<data_t>{dataDescriptor, _dataHandler->getBlock(0, getSize())};
    }

    template <typename data_t>
321
    DataContainer<data_t> DataContainer<data_t>::viewAs(const DataDescriptor& dataDescriptor) const
322
323
324
325
326
327
328
    {
        if (dataDescriptor.getNumberOfCoefficients() != getSize())
            throw std::invalid_argument("DataContainer: view must have same size as container");

        // getBlock() returns a pointer to non-const DH, but that's fine as it gets wrapped in a
        // constant container
        return DataContainer<data_t>{dataDescriptor, _dataHandler->getBlock(0, getSize())};
Jens Petit's avatar
Jens Petit committed
329
330
    }

331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
    template <typename data_t>
    typename DataContainer<data_t>::iterator DataContainer<data_t>::begin()
    {
        return iterator(&(*this)[0]);
    }

    template <typename data_t>
    typename DataContainer<data_t>::const_iterator DataContainer<data_t>::begin() const
    {
        return cbegin();
    }

    template <typename data_t>
    typename DataContainer<data_t>::const_iterator DataContainer<data_t>::cbegin() const
    {
        return const_iterator(&(*this)[0]);
    }

    template <typename data_t>
    typename DataContainer<data_t>::iterator DataContainer<data_t>::end()
    {
        return iterator(&(*this)[0] + getSize());
    }

    template <typename data_t>
    typename DataContainer<data_t>::const_iterator DataContainer<data_t>::end() const
    {
        return cend();
    }

    template <typename data_t>
    typename DataContainer<data_t>::const_iterator DataContainer<data_t>::cend() const
    {
        return const_iterator(&(*this)[0] + getSize());
    }

    template <typename data_t>
    typename DataContainer<data_t>::reverse_iterator DataContainer<data_t>::rbegin()
    {
        return reverse_iterator(end());
    }

    template <typename data_t>
    typename DataContainer<data_t>::const_reverse_iterator DataContainer<data_t>::rbegin() const
    {
        return crbegin();
    }

    template <typename data_t>
    typename DataContainer<data_t>::const_reverse_iterator DataContainer<data_t>::crbegin() const
    {
        return const_reverse_iterator(cend());
    }

    template <typename data_t>
    typename DataContainer<data_t>::reverse_iterator DataContainer<data_t>::rend()
    {
        return reverse_iterator(begin());
    }

    template <typename data_t>
    typename DataContainer<data_t>::const_reverse_iterator DataContainer<data_t>::rend() const
    {
        return crend();
    }

    template <typename data_t>
    typename DataContainer<data_t>::const_reverse_iterator DataContainer<data_t>::crend() const
    {
        return const_reverse_iterator(cbegin());
    }

Tobias Lasser's avatar
Tobias Lasser committed
403
404
405
406
407
408
409
410
411
    // ------------------------------------------
    // explicit template instantiation
    template class DataContainer<float>;
    template class DataContainer<std::complex<float>>;
    template class DataContainer<double>;
    template class DataContainer<std::complex<double>>;
    template class DataContainer<index_t>;

} // namespace elsa