My Project
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  {
46  delete[] p;
47  }
48 };
49 
56 template <typename T>
57 struct array_void_destructor {
59  virtual void operator () (T *)
60  {
61  }
62 };
64 
76 template <typename T>
78 {
79 public:
80 
82  typedef T& reference;
83  typedef const T& const_reference;
84  typedef T *iterator;
85  typedef const T *const_iterator;
86  typedef size_t size_type;
87  typedef T value_type;
89 
96  TCArrayWrapper(size_t n, bool clean = true):
97  m_size(n),
98  m_data(new T[n], array_destructor<T>()),
99  m_cdata(m_data.get())
100  {
101  if (clean)
102  memset(m_data.get(), 0, m_size * sizeof(T));
103  }
104 
110  m_size(other.m_size),
111  m_data(other.m_data),
112  m_cdata(other.m_cdata)
113  {
114  }
115 
118  {
119  m_size = other.m_size;
120  m_data = other.m_data;
121  m_cdata = other.m_cdata;
122  return *this;
123  }
124 
131  TCArrayWrapper(size_t n, T *init):
132  m_size(n),
133  m_data(init, array_void_destructor<T>()),
134  m_cdata(init)
135  {
136  }
137 
144  TCArrayWrapper(size_t n, const T *init):
145  m_size(n),
146  m_cdata(init)
147  {
148  }
149 
150 
154  reference operator[] (size_t i)
155  {
156  assert(i < m_size);
157  DEBUG_ASSERT_RELEASE_THROW(m_data && m_data.unique(),
158  "TCArrayWrapper::operator[]: No writeable data available or not unique,"
159  " call TCArrayWrapper::make_unique() first or enforce the use of "
160  "'TCArrayWrapper::operator[](...) const'");
161  return m_data.get()[i];
162  }
163 
167  const_reference operator[] (size_t i) const
168  {
169  assert(i < m_size);
170  return m_cdata[i];
171  }
172 
176  iterator begin()
177  {
178  DEBUG_ASSERT_RELEASE_THROW(m_data && m_data.unique(),
179  "TCArrayWrapper::begin(): No writeable data available or not unique, "
180  "call TCArrayWrapper::make_unique() first or enforce the use of "
181  "'TCArrayWrapper::begin() const'");
182  return m_data.get();
183  }
184 
188  iterator end()
189  {
190  DEBUG_ASSERT_RELEASE_THROW(m_data && m_data.unique(),
191  "TCArrayWrapper::begin(): No writeable data available or not unique, "
192  "call TCArrayWrapper::make_unique() first or enforce the use of "
193  "'TCArrayWrapper::end() const'");
194  return m_data.get() + m_size;
195  }
196 
197 
201  const_iterator begin() const
202  {
203  return m_cdata;
204  }
205 
209  const_iterator end() const
210  {
211  return m_cdata + m_size;
212  }
213 
217  size_type size() const
218  {
219  return m_size;
220  }
221 
227  void make_unique()
228  {
229  // if we have writable dataand is it already unique
230  // then do nothing
231  if (m_data && m_data.unique())
232  return;
233 
234  // create the new array and copy from the constant origial
235  // in case we didn't have writable data
236  m_data.reset(new T[m_size], array_destructor<T>());
237  std::copy(m_cdata, m_cdata + m_size, m_data.get());
238  m_cdata = m_data.get();
239  }
240 
241 private:
242  size_t m_size;
243  std::shared_ptr<T> m_data;
244  const T *m_cdata;
245 };
246 
247 
248 template <typename T>
249 std::ostream& operator << (std::ostream& os, const TCArrayWrapper<T>& v)
250 {
251  os << "[";
252 
253  for (auto i : v)
254  os << i << ", ";
255 
256  os << "]";
257  return os;
258 }
259 
260 
267 
269 
270 
271 #endif
TCArrayWrapper::size
size_type size() const
Definition: core/vector.hh:217
operator<<
std::ostream & operator<<(std::ostream &os, const TCArrayWrapper< T > &v)
Definition: core/vector.hh:249
NS_MIA_BEGIN
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:33
TCArrayWrapper::operator[]
reference operator[](size_t i)
Definition: core/vector.hh:154
TCArrayWrapper::operator=
TCArrayWrapper< T > & operator=(const TCArrayWrapper< T > &other)
assignment operator
Definition: core/vector.hh:117
TCArrayWrapper::end
const_iterator end() const
Definition: core/vector.hh:209
TCArrayWrapper::begin
iterator begin()
Definition: core/vector.hh:176
NS_MIA_END
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:36
TCArrayWrapper::TCArrayWrapper
TCArrayWrapper(size_t n, bool clean=true)
*‍/
Definition: core/vector.hh:96
DEBUG_ASSERT_RELEASE_THROW
#define DEBUG_ASSERT_RELEASE_THROW(cond, msg...)
Definition: errormacro.hh:99
errormacro.hh
TCArrayWrapper::TCArrayWrapper
TCArrayWrapper(size_t n, const T *init)
Definition: core/vector.hh:144
TCArrayWrapper::TCArrayWrapper
TCArrayWrapper(const TCArrayWrapper< T > &other)
Definition: core/vector.hh:109
TCArrayWrapper::end
iterator end()
Definition: core/vector.hh:188
TCArrayWrapper
A wrapper around the c-array to provide an STL like interface for iterators.
Definition: core/vector.hh:77
TCArrayWrapper::begin
const_iterator begin() const
Definition: core/vector.hh:201
defines.hh
TCArrayWrapper::make_unique
void make_unique()
Definition: core/vector.hh:227
CDoubleVector
TCArrayWrapper< double > CDoubleVector
Definition: core/vector.hh:266
TCArrayWrapper::TCArrayWrapper
TCArrayWrapper(size_t n, T *init)
Definition: core/vector.hh:131