/usr/include/mia-2.2/mia/core/vector.hh is in libmia-2.2-dev 2.2.2-1+b1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | /* -*- mia-c++ -*-
*
* This file is part of MIA - a toolbox for medical image analysis
* Copyright (c) Leipzig, Madrid 1999-2014 Gert Wollny
*
* MIA is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MIA; if not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef mia_core_vector_hh
#define mia_core_vector_hh
#include <mia/core/defines.hh>
#include <mia/core/errormacro.hh>
#include <memory>
#include <cstring>
#include <cassert>
#include <ostream>
NS_MIA_BEGIN
/**
\cond INTERNAL
\ingroup misc
Helper structure used to initialize std::shared_ptr with the proper
delete operator.
*/
template <typename T>
struct array_destructor {
/// run delete on an array
virtual void operator () (T *p) {
delete[] p;
}
};
/**
\ingroup misc
Helper structure used to initialize std::shared_ptr with an empty
delete operator, i.e. to fake a shared pointer
*/
template <typename T>
struct array_void_destructor {
/// skip deleting the pointer
virtual void operator () (T *) {
}
};
/// \endcond
/**
\ingroup misc
\brief A wrapper around the c-array to provide an STL like interface for iterators
c-array envelope that supports some facilities of STL like vectors and that
allows holding pre-allocated data.
Handling of the optinal deleting of the array is done by a destructor template
passed to the std::shared_ptr constructor
\tparam the data type of the C-array
*/
template <typename T>
class Vector {
public:
/// \cond STLCOMPAT
typedef T& reference;
typedef const T& const_reference;
typedef T *iterator;
typedef const T *const_iterator;
typedef size_t size_type;
typedef T value_type;
/// \endcond
/**
Create a vector, the data is owned by this vector and will be
deleted if the reference count reaches zero
\param n
\param clean initialize vector to 0
*/
Vector(size_t n, bool clean = true):
m_size(n),
m_data(new T[n], array_destructor<T>()),
m_cdata(m_data.get())
{
if (clean)
memset(m_data.get(), 0, m_size*sizeof(T));
}
/** copy constructor, this is a shallow copy, i.e. the data is shared
between the original and the copied vector
\param other
*/
Vector(const Vector<T>& other):
m_size(other.m_size),
m_data(other.m_data),
m_cdata(other.m_cdata)
{
}
/// assignment operator
Vector<T>& operator = (const Vector<T>& other)
{
m_size = other.m_size;
m_data = other.m_data;
m_cdata = other.m_cdata;
return *this;
}
/**
Constructor that creates the STL-like vector as an envelop around a
C-array. The data will not be freed at destruction time.
\param n size of input array
\param init allocated input data
*/
Vector(size_t n, T *init):
m_size(n),
m_data(init, array_void_destructor<T>()),
m_cdata(init)
{
}
/**
Constructor that creates the STL-like vector as an envelop around a
C-array. The data will not be freed at destruction time.
\param n size of input array
\param init allocated input data
*/
Vector(size_t n, const T *init):
m_size(n),
m_cdata(init)
{
}
/**
Standard array access operator, read-write version
*/
reference operator[] (size_t i) {
assert(i < m_size);
DEBUG_ASSERT_RELEASE_THROW(m_data && m_data.unique(),
"Vector::operator[]: No writeable data availabe or not unique,"
" call Vector::make_unique() first or enforce the use of "
"'Vector::operator[](...) const'");
return m_data.get()[i];
}
/**
Standard array access operator, read-only version
*/
const_reference operator[] (size_t i) const {
assert(i < m_size);
return m_cdata[i];
}
/**
STL compatible iterator, begin of range
*/
iterator begin() {
DEBUG_ASSERT_RELEASE_THROW(m_data && m_data.unique(),
"Vector::begin(): No writeable data availabe or not unique, "
"call Vector::make_unique() first or enforce the use of "
"'Vector::begin() const'");
return m_data.get();
}
/**
STL compatible iterator, end of range
*/
iterator end() {
DEBUG_ASSERT_RELEASE_THROW(m_data && m_data.unique(),
"Vector::begin(): No writeable data availabe or not unique, "
"call Vector::make_unique() first or enforce the use of "
"'Vector::end() const'");
return m_data.get() + m_size;
}
/**
STL compatible const_iterator, begin of range
*/
const_iterator begin() const{
return m_cdata;
}
/**
STL compatible const_iterator, end of range
*/
const_iterator end() const{
return m_cdata + m_size;
}
/**
\returns number of elements in the array
*/
size_type size() const
{
return m_size;
}
/**
If the wrapped data is referenced more than once or is
read-only, make a copy inside this object that is refereneces
only once and writable.
*/
void make_unique()
{
// if we have writable dataand is it already unique
// then do nothing
if (m_data && m_data.unique())
return;
// create the new array and copy from the constant origial
// in case we didn't have writable data
m_data.reset(new T[m_size], array_destructor<T>());
std::copy(m_cdata, m_cdata + m_size, m_data.get());
m_cdata = m_data.get();
}
private:
size_t m_size;
std::shared_ptr<T> m_data;
const T *m_cdata;
};
template <typename T>
std::ostream& operator << (std::ostream& os, const Vector<T>& v)
{
os << "[";
for(auto i: v)
os << i << ", ";
os << "]";
return os;
}
/**
\ingroup misc
STL vector like c-array wrapper for double floating point arrays
*/
typedef Vector<double> CDoubleVector;
NS_MIA_END
#endif
|