DOLFIN
DOLFIN C++ interface
Array.h
1 // Copyright (C) 2009-2012 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 // Modified by Anders Logg 2010-2011
19 // Modified by Joachim B Haga 2012
20 //
21 // First added: 2009-12-06
22 // Last changed: 2012-03-12
23 
24 #ifndef __DOLFIN_ARRAY_H
25 #define __DOLFIN_ARRAY_H
26 
27 #include <cstddef>
28 #include <sstream>
29 #include <string>
30 
31 #include <dolfin/common/constants.h>
32 #include <dolfin/log/log.h>
33 
34 namespace dolfin
35 {
36 
40 
41  template <typename T> class Array
42  {
43 
44  public:
45 
47  explicit Array(std::size_t N) : _size(N), _x(new T[N]), _owner(true) {}
48 
50  Array(std::size_t N, T* x) : _size(N), _x(x), _owner(false) {}
51 
54  {
55  if (_owner)
56  delete [] _x;
57  }
58 
63  std::string str(bool verbose) const
64  {
65  std::stringstream s;
66 
67  if (verbose)
68  {
69  s << str(false) << std::endl << std::endl;
70 
71  for (std::size_t i = 0; i < size(); i++)
72  s << i << ": " << (*this)[i] << std::endl;
73  }
74  else
75  s << "<Array<T> of size " << size() << ">";
76 
77  return s.str();
78  }
79 
81  std::size_t size() const
82  { return _size; }
83 
85  const T& operator[] (std::size_t i) const
86  { dolfin_assert(i < _size); return _x[i]; }
87 
89  T& operator[] (std::size_t i)
90  { dolfin_assert(i < _size); return _x[i]; }
91 
93  const T* data() const
94  { return _x; }
95 
97  T* data()
98  { return _x; }
99 
100  private:
101 
105  Array(const Array& other) = delete;
106 
107  private:
108 
110  const std::size_t _size;
111 
113  T* _x;
114 
116  bool _owner;
117 
118  };
119 
120 }
121 
122 #endif
std::string str(bool verbose) const
Definition: Array.h:63
Definition: adapt.h:29
Definition: Array.h:41
const T & operator[](std::size_t i) const
Access value of given entry (const version)
Definition: Array.h:85
std::size_t size() const
Return size of array.
Definition: Array.h:81
Array(std::size_t N, T *x)
Construct array from a pointer. Array does not take ownership.
Definition: Array.h:50
const T * data() const
Return pointer to data (const version)
Definition: Array.h:93
Array(std::size_t N)
Create array of size N. Array has ownership.
Definition: Array.h:47
~Array()
Destructor.
Definition: Array.h:53
T * data()
Return pointer to data (non-const version)
Definition: Array.h:97