Developer documentation
Version 3.0.3-105-gd3941f44
min_mem_array.h
Go to the documentation of this file.
1/* Copyright (c) 2008-2022 the MRtrix3 contributors.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 *
7 * Covered Software is provided under this License on an "as is"
8 * basis, without warranty of any kind, either expressed, implied, or
9 * statutory, including, without limitation, warranties that the
10 * Covered Software is free of defects, merchantable, fit for a
11 * particular purpose or non-infringing.
12 * See the Mozilla Public License v. 2.0 for more details.
13 *
14 * For more details, see http://www.mrtrix.org/.
15 */
16
17#ifndef __min_mem_array_h__
18#define __min_mem_array_h__
19
20#include <algorithm>
21#include <cassert>
22#include <cstddef>
23#include <cstdint>
24
25#include "types.h"
26
27
28namespace MR {
29
30
31
32// This class allows for construction and management of a c-style array, where the
33// memory overhead is minimal (a pointer and a size_t)
34// vector<>'s have some amount of overhead, which can add up if many are being stored
35// Typical usage is to gather the required data using a vector<>, and use that vector
36// to construct a Min_mem_array<>
37
38template <class T>
40
41 public:
42 Min_mem_array () :
43 n (0),
44 d (NULL) { }
45
46 Min_mem_array (const T& i) :
47 n (1),
48 d (new T[1])
49 {
50 d[0] = i;
51 }
52
53 Min_mem_array (const size_t size, const T& i) :
54 n (size),
55 d (new T[n])
56 {
57 for (size_t a = 0; a != n; ++a)
58 d[n] = i;
59 }
60
61 template <class C>
62 Min_mem_array (const C& data) :
63 n (data.size()),
64 d (new T[data.size()])
65 {
66 size_t index = 0;
67 for (typename C::const_iterator i = data.begin(); i != data.end(); ++i, ++index)
68 d[index] = *i;
69 }
70
71 Min_mem_array (const Min_mem_array& that) :
72 n (that.n),
73 d (new T[n])
74 {
75 memcpy (d, that.d, n * sizeof (T));
76 }
77
78 virtual ~Min_mem_array()
79 {
80 delete[] d;
81 d = NULL;
82 }
83
84 void add (const T& i) {
85 if (d) {
86 T* new_data = new T[n + 1];
87 memcpy (new_data, d, n * sizeof (T));
88 new_data[n] = i;
89 delete[] d;
90 d = new_data;
91 ++n;
92 } else {
93 d = new T[1];
94 d[0] = i;
95 n = 1;
96 }
97 }
98
99 // Second version of add() which invokes copy constructors in the underlying data
100 void add_copyconstruct (const T& i) {
101 if (d) {
102 T* new_data = new T[n + 1];
103 for (size_t a = 0; a != n; ++a)
104 new_data[a] = d[a];
105 new_data[n] = i;
106 delete[] d;
107 d = new_data;
108 ++n;
109 } else {
110 d = new T[1];
111 d[0] = i;
112 n = 1;
113 }
114 }
115
116
117 void erase() {
118 if (d) {
119 delete[] d;
120 d = NULL;
121 n = 0;
122 }
123 }
124
125 // TODO Should be 2 versions of this; 1 retains current size, other takes on size of container
126 // Need to make sure change does not affect any existing code
127 template <class C>
128 void load (C& data) const {
129 for (size_t i = 0; i != n; ++i)
130 data.push_back (d[i]);
131 }
132
133 size_t dim() const { return n; }
134
135 bool operator== (const Min_mem_array<T>& that) const {
136 return ((n == that.n) && !memcmp (d, that.d, n * sizeof (T)));
137 }
138
139 bool operator< (const Min_mem_array<T>& that) const {
140 // If one is empty and the other is not, one is 'less than' the other; but if both are empty, then '<' should return false
141 if (!n)
142 return that.n;
143 if (!that.n)
144 return false;
145 for (size_t i = 0; i != std::min (n, that.n); ++i) {
146 if (d[i] < that.d[i])
147 return true;
148 if (d[i] > that.d[i])
149 return false;
150 // Continue to the next element if they are identical
151 }
152 // If code reaches this point, then all of the elements up to the minimum of the two lengths are identical
153 // Therefore, only return true for '<' if the length of that is longer than this (if they are equivalent in length, '<' does not hold so return false)
154 return (n < that.n);
155 }
156
157 T& operator[] (const size_t i) {
158 assert (i < n);
159 return d[i];
160 }
161
162 const T& operator[] (const size_t i) const {
163 assert (i < n);
164 return d[i];
165 }
166
167 Min_mem_array<T>& operator= (const Min_mem_array<T>& that) {
168 n = that.n;
169 if (d)
170 delete[] d;
171 d = new T[n];
172 memcpy (d, that.d, n * sizeof (T));
173 return (*this);
174 }
175
176
177 private:
178 size_t n;
179 T* d;
180
181};
182
183
184}
185
186#endif
Definition: base.h:24
size_t index