Loading...
Searching...
No Matches
ArrayView.h
// Copyright (C) 2015 Garth N. Wells
//
// This file is part of DOLFIN.
//
// DOLFIN is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// DOLFIN 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
#ifndef __DOLFIN_ARRAYVIEW_H
#define __DOLFIN_ARRAYVIEW_H
#include <cstddef>
#include <dolfin/log/log.h>
namespace dolfin
{
template <typename T> class ArrayView
{
public:
ArrayView() : _size(0), _x(NULL) {}
ArrayView(std::size_t N, T* x) : _size(N), _x(x) {}
template<typename V>
explicit ArrayView(V& v) : _size(v.size()), _x(v.data()) {}
ArrayView(const ArrayView& x) : _size(x._size), _x(x._x) {}
~ArrayView() {}
void set(std::size_t N, T* x)
{ _size = N; _x = x; }
template<typename V>
void set(V& v)
{ _size = v.size(); _x = v.data(); }
std::size_t size() const
{ return _size; }
bool empty() const
{ return (_size == 0) ? true : false; }
const T& operator[] (std::size_t i) const
{ dolfin_assert(i < _size); return _x[i]; }
T& operator[] (std::size_t i)
{ dolfin_assert(i < _size); return _x[i]; }
T* begin()
{ return &_x[0]; }
const T* begin() const
{ return &_x[0]; }
T* end()
{ return &_x[_size]; }
const T* end() const
{ return &_x[_size]; }
const T* data() const
{ return _x; }
T* data()
{ return _x; }
private:
// Length of array
std::size_t _size;
// Array data
T* _x;
};
}
#endif
dolfin::ArrayView::data
T * data()
Return pointer to data (non-const version).
Definition ArrayView.h:99
dolfin::ArrayView::set
void set(std::size_t N, T *x)
Update object to point to new data.
Definition ArrayView.h:54
dolfin::ArrayView::end
const T * end() const
Pointer to beyond end of array (const).
Definition ArrayView.h:91
dolfin::ArrayView::ArrayView
ArrayView(const ArrayView &x)
Copy constructor.
Definition ArrayView.h:48
dolfin::ArrayView::size
std::size_t size() const
Return size of array.
Definition ArrayView.h:63
dolfin::ArrayView::ArrayView
ArrayView()
Constructor.
Definition ArrayView.h:37
dolfin::ArrayView::ArrayView
ArrayView(V &v)
Definition ArrayView.h:45
dolfin::ArrayView::begin
T * begin()
Pointer to start of array.
Definition ArrayView.h:79
dolfin::ArrayView::begin
const T * begin() const
Pointer to start of array (const).
Definition ArrayView.h:83
dolfin::ArrayView::~ArrayView
~ArrayView()
Destructor.
Definition ArrayView.h:51
dolfin::ArrayView::end
T * end()
Pointer to beyond end of array.
Definition ArrayView.h:87
dolfin::ArrayView::operator[]
const T & operator[](std::size_t i) const
Access value of given entry (const version).
Definition ArrayView.h:71
dolfin::ArrayView::set
void set(V &v)
Update object to point to new container.
Definition ArrayView.h:59
dolfin::ArrayView::data
const T * data() const
Return pointer to data (const version).
Definition ArrayView.h:95
dolfin::ArrayView::ArrayView
ArrayView(std::size_t N, T *x)
Construct array from a pointer. Array does not take ownership.
Definition ArrayView.h:40
dolfin::ArrayView::empty
bool empty() const
Test if array view is empty.
Definition ArrayView.h:67
dolfin
Definition adapt.h:30