core/vector.hh
Go to the documentation of this file.
/* -*- mia-c++ -*-
*
* This file is part of MIA - a toolbox for medical image analysis
* Copyright (c) Leipzig, Madrid 1999-2017 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
template <typename T>
struct array_destructor {
virtual void operator () (T *p)
{
delete[] p;
}
};
template <typename T>
struct array_void_destructor {
virtual void operator () (T *)
{
}
};
template <typename T>
class TCArrayWrapper
{
public:
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;
TCArrayWrapper(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));
}
TCArrayWrapper(const TCArrayWrapper<T>& other):
m_size(other.m_size),
m_data(other.m_data),
m_cdata(other.m_cdata)
{
}
TCArrayWrapper<T>& operator = (const TCArrayWrapper<T>& other)
{
m_size = other.m_size;
m_data = other.m_data;
m_cdata = other.m_cdata;
return *this;
}
TCArrayWrapper(size_t n, T *init):
m_size(n),
m_data(init, array_void_destructor<T>()),
m_cdata(init)
{
}
TCArrayWrapper(size_t n, const T *init):
m_size(n),
m_cdata(init)
{
}
reference operator[] (size_t i)
{
assert(i < m_size);
DEBUG_ASSERT_RELEASE_THROW(m_data && m_data.unique(),
"TCArrayWrapper::operator[]: No writeable data available or not unique,"
" call TCArrayWrapper::make_unique() first or enforce the use of "
"'TCArrayWrapper::operator[](...) const'");
return m_data.get()[i];
}
const_reference operator[] (size_t i) const
{
assert(i < m_size);
return m_cdata[i];
}
iterator begin()
{
DEBUG_ASSERT_RELEASE_THROW(m_data && m_data.unique(),
"TCArrayWrapper::begin(): No writeable data available or not unique, "
"call TCArrayWrapper::make_unique() first or enforce the use of "
"'TCArrayWrapper::begin() const'");
return m_data.get();
}
iterator end()
{
DEBUG_ASSERT_RELEASE_THROW(m_data && m_data.unique(),
"TCArrayWrapper::begin(): No writeable data available or not unique, "
"call TCArrayWrapper::make_unique() first or enforce the use of "
"'TCArrayWrapper::end() const'");
return m_data.get() + m_size;
}
const_iterator begin() const
{
return m_cdata;
}
const_iterator end() const
{
return m_cdata + m_size;
}
size_type size() const
{
return m_size;
}
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 TCArrayWrapper<T>& v)
{
os << "[";
for (auto i : v)
os << i << ", ";
os << "]";
return os;
}
typedef TCArrayWrapper<double> CDoubleVector;
NS_MIA_END
#endif
TCArrayWrapper
A wrapper around the c-array to provide an STL like interface for iterators.
Definition core/vector.hh:78
TCArrayWrapper::operator[]
reference operator[](size_t i)
Definition core/vector.hh:154
TCArrayWrapper::size
size_type size() const
Definition core/vector.hh:217
TCArrayWrapper::TCArrayWrapper
TCArrayWrapper(const TCArrayWrapper< T > &other)
Definition core/vector.hh:109
TCArrayWrapper::make_unique
void make_unique()
Definition core/vector.hh:227
TCArrayWrapper::TCArrayWrapper
TCArrayWrapper(size_t n, const T *init)
Definition core/vector.hh:144
TCArrayWrapper::end
iterator end()
Definition core/vector.hh:188
TCArrayWrapper::end
const_iterator end() const
Definition core/vector.hh:209
TCArrayWrapper::TCArrayWrapper
TCArrayWrapper(size_t n, bool clean=true)
*‍/
Definition core/vector.hh:96
TCArrayWrapper::operator=
TCArrayWrapper< T > & operator=(const TCArrayWrapper< T > &other)
assignment operator
Definition core/vector.hh:117
TCArrayWrapper::begin
iterator begin()
Definition core/vector.hh:176
TCArrayWrapper::begin
const_iterator begin() const
Definition core/vector.hh:201
TCArrayWrapper::TCArrayWrapper
TCArrayWrapper(size_t n, T *init)
Definition core/vector.hh:131
const_iterator
operator<<
std::ostream & operator<<(std::ostream &os, const TCArrayWrapper< T > &v)
Definition core/vector.hh:249
defines.hh
NS_MIA_BEGIN
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition defines.hh:33
NS_MIA_END
#define NS_MIA_END
conveniance define to end the mia namespace
Definition defines.hh:36
errormacro.hh
DEBUG_ASSERT_RELEASE_THROW
#define DEBUG_ASSERT_RELEASE_THROW(cond, msg...)
Definition errormacro.hh:99
CDoubleVector
TCArrayWrapper< double > CDoubleVector
Definition core/vector.hh:266