core/vector.hh
Go to the documentation of this file.
1 /* -*- mia-c++ -*-
2  *
3  * This file is part of MIA - a toolbox for medical image analysis
4  * Copyright (c) Leipzig, Madrid 1999-2017 Gert Wollny
5  *
6  * MIA is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with MIA; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef mia_core_vector_hh
22 #define mia_core_vector_hh
23 
24 #include <mia/core/defines.hh>
25 #include <mia/core/errormacro.hh>
26 #include <memory>
27 #include <cstring>
28 #include <cassert>
29 #include <ostream>
30 
32 
33 
41 template <typename T>
42 struct array_destructor {
44  virtual void operator () (T *p) {
45  delete[] p;
46  }
47 };
48 
55 template <typename T>
56 struct array_void_destructor {
58  virtual void operator () (T *) {
59  }
60 };
62 
74 template <typename T>
76 public:
77 
79  typedef T& reference;
80  typedef const T& const_reference;
81  typedef T *iterator;
82  typedef const T *const_iterator;
83  typedef size_t size_type;
84  typedef T value_type;
86 
93  TCArrayWrapper(size_t n, bool clean = true):
94  m_size(n),
95  m_data(new T[n], array_destructor<T>()),
96  m_cdata(m_data.get())
97  {
98  if (clean)
99  memset(m_data.get(), 0, m_size*sizeof(T));
100  }
101 
107  m_size(other.m_size),
108  m_data(other.m_data),
109  m_cdata(other.m_cdata)
110  {
111  }
112 
114  TCArrayWrapper<T>& operator = (const TCArrayWrapper<T>& other)
115  {
116  m_size = other.m_size;
117  m_data = other.m_data;
118  m_cdata = other.m_cdata;
119  return *this;
120  }
121 
128  TCArrayWrapper(size_t n, T *init):
129  m_size(n),
130  m_data(init, array_void_destructor<T>()),
131  m_cdata(init)
132  {
133  }
134 
141  TCArrayWrapper(size_t n, const T *init):
142  m_size(n),
143  m_cdata(init)
144  {
145  }
146 
147 
151  reference operator[] (size_t i) {
152  assert(i < m_size);
153  DEBUG_ASSERT_RELEASE_THROW(m_data && m_data.unique(),
154  "TCArrayWrapper::operator[]: No writeable data available or not unique,"
155  " call TCArrayWrapper::make_unique() first or enforce the use of "
156  "'TCArrayWrapper::operator[](...) const'");
157  return m_data.get()[i];
158  }
159 
163  const_reference operator[] (size_t i) const {
164  assert(i < m_size);
165  return m_cdata[i];
166  }
167 
171  iterator begin() {
172  DEBUG_ASSERT_RELEASE_THROW(m_data && m_data.unique(),
173  "TCArrayWrapper::begin(): No writeable data available or not unique, "
174  "call TCArrayWrapper::make_unique() first or enforce the use of "
175  "'TCArrayWrapper::begin() const'");
176  return m_data.get();
177  }
178 
182  iterator end() {
183  DEBUG_ASSERT_RELEASE_THROW(m_data && m_data.unique(),
184  "TCArrayWrapper::begin(): No writeable data available or not unique, "
185  "call TCArrayWrapper::make_unique() first or enforce the use of "
186  "'TCArrayWrapper::end() const'");
187  return m_data.get() + m_size;
188  }
189 
190 
194  const_iterator begin() const{
195  return m_cdata;
196  }
197 
201  const_iterator end() const{
202  return m_cdata + m_size;
203  }
204 
208  size_type size() const
209  {
210  return m_size;
211  }
212 
218  void make_unique()
219  {
220  // if we have writable dataand is it already unique
221  // then do nothing
222  if (m_data && m_data.unique())
223  return;
224 
225  // create the new array and copy from the constant origial
226  // in case we didn't have writable data
227  m_data.reset(new T[m_size], array_destructor<T>());
228  std::copy(m_cdata, m_cdata + m_size, m_data.get());
229  m_cdata = m_data.get();
230  }
231 
232 private:
233  size_t m_size;
234  std::shared_ptr<T> m_data;
235  const T *m_cdata;
236 };
237 
238 
239 template <typename T>
240 std::ostream& operator << (std::ostream& os, const TCArrayWrapper<T>& v)
241 {
242  os << "[";
243  for(auto i: v)
244  os << i << ", ";
245  os << "]";
246  return os;
247 }
248 
249 
256 
258 
259 
260 #endif
iterator begin()
Definition: core/vector.hh:171
void make_unique()
Definition: core/vector.hh:218
TCArrayWrapper(size_t n, bool clean=true)
*/
Definition: core/vector.hh:93
A wrapper around the c-array to provide an STL like interface for iterators.
Definition: core/vector.hh:75
const_iterator end() const
Definition: core/vector.hh:201
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:33
#define DEBUG_ASSERT_RELEASE_THROW(cond, msg...)
Definition: errormacro.hh:99
TCArrayWrapper(size_t n, const T *init)
Definition: core/vector.hh:141
TCArrayWrapper< double > CDoubleVector
Definition: core/vector.hh:255
TCArrayWrapper(const TCArrayWrapper< T > &other)
Definition: core/vector.hh:106
const_iterator begin() const
Definition: core/vector.hh:194
iterator end()
Definition: core/vector.hh:182
TCArrayWrapper(size_t n, T *init)
Definition: core/vector.hh:128
size_type size() const
Definition: core/vector.hh:208
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:36