DOLFIN
DOLFIN C++ interface
ArrayView.h
1// Copyright (C) 2015 Garth N. Wells
2//
3// This file is part of DOLFIN.
4//
5// DOLFIN is free software: you can redistribute it and/or modify
6// it under the terms of the GNU Lesser General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// DOLFIN is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU Lesser General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public License
16// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17
18#ifndef __DOLFIN_ARRAYVIEW_H
19#define __DOLFIN_ARRAYVIEW_H
20
21#include <cstddef>
22#include <dolfin/log/log.h>
23
24namespace dolfin
25{
26
30
31 template <typename T> class ArrayView
32 {
33
34 public:
35
37 ArrayView() : _size(0), _x(NULL) {}
38
40 ArrayView(std::size_t N, T* x) : _size(N), _x(x) {}
41
44 template<typename V>
45 explicit ArrayView(V& v) : _size(v.size()), _x(v.data()) {}
46
48 ArrayView(const ArrayView& x) : _size(x._size), _x(x._x) {}
49
52
54 void set(std::size_t N, T* x)
55 { _size = N; _x = x; }
56
58 template<typename V>
59 void set(V& v)
60 { _size = v.size(); _x = v.data(); }
61
63 std::size_t size() const
64 { return _size; }
65
67 bool empty() const
68 { return (_size == 0) ? true : false; }
69
71 const T& operator[] (std::size_t i) const
72 { dolfin_assert(i < _size); return _x[i]; }
73
75 T& operator[] (std::size_t i)
76 { dolfin_assert(i < _size); return _x[i]; }
77
79 T* begin()
80 { return &_x[0]; }
81
83 const T* begin() const
84 { return &_x[0]; }
85
87 T* end()
88 { return &_x[_size]; }
89
91 const T* end() const
92 { return &_x[_size]; }
93
95 const T* data() const
96 { return _x; }
97
99 T* data()
100 { return _x; }
101
102 private:
103
104 // Length of array
105 std::size_t _size;
106
107 // Array data
108 T* _x;
109
110 };
111
112}
113
114#endif
Definition: ArrayView.h:32
T * data()
Return pointer to data (non-const version)
Definition: ArrayView.h:99
void set(std::size_t N, T *x)
Update object to point to new data.
Definition: ArrayView.h:54
const T * end() const
Pointer to beyond end of array (const)
Definition: ArrayView.h:91
ArrayView(const ArrayView &x)
Copy constructor.
Definition: ArrayView.h:48
std::size_t size() const
Return size of array.
Definition: ArrayView.h:63
ArrayView()
Constructor.
Definition: ArrayView.h:37
ArrayView(V &v)
Definition: ArrayView.h:45
T * begin()
Pointer to start of array.
Definition: ArrayView.h:79
const T * begin() const
Pointer to start of array (const)
Definition: ArrayView.h:83
~ArrayView()
Destructor.
Definition: ArrayView.h:51
T * end()
Pointer to beyond end of array.
Definition: ArrayView.h:87
const T & operator[](std::size_t i) const
Access value of given entry (const version)
Definition: ArrayView.h:71
void set(V &v)
Update object to point to new container.
Definition: ArrayView.h:59
const T * data() const
Return pointer to data (const version)
Definition: ArrayView.h:95
ArrayView(std::size_t N, T *x)
Construct array from a pointer. Array does not take ownership.
Definition: ArrayView.h:40
bool empty() const
Test if array view is empty.
Definition: ArrayView.h:67
Definition: adapt.h:30