Note: this is documentation for an old release. View the latest documentation at docs.fenicsproject.org/v0.3.0/v0.9.0/cpp
DOLFINx  0.3.0
DOLFINx C++ interface
array2d.h
1 // Copyright (C) 2021 Igor Baratta
2 //
3 // This file is part of DOLFINx (https://www.fenicsproject.org)
4 //
5 // SPDX-License-Identifier: LGPL-3.0-or-later
6 
7 #pragma once
8 
9 #include <array>
10 #include <cassert>
11 #include <vector>
12 #include <xtl/xspan.hpp>
13 
14 namespace dolfinx
15 {
16 
19 template <typename T, class Allocator = std::allocator<T>>
20 class array2d
21 {
22 public:
24  using value_type = T;
25  using allocator_type = Allocator;
26  using size_type = typename std::vector<T, Allocator>::size_type;
27  using reference = typename std::vector<T, Allocator>::reference;
28  using const_reference = typename std::vector<T, Allocator>::const_reference;
29  using pointer = typename std::vector<T, Allocator>::pointer;
30  using iterator = typename std::vector<T, Allocator>::iterator;
31  using const_iterator = typename std::vector<T, Allocator>::const_iterator;
33 
38  array2d(std::array<size_type, 2> shape, value_type value = T(),
39  const Allocator& alloc = Allocator())
40  : shape(shape), _storage(shape[0] * shape[1], value, alloc)
41  {
42  }
43 
49  array2d(size_type rows, size_type cols, value_type value = T(),
50  const Allocator& alloc = Allocator())
51  : shape({rows, cols}), _storage(shape[0] * shape[1], value, alloc)
52  {
53  }
54 
57  template <typename Vector>
58  array2d(std::array<size_type, 2> shape, Vector&& x)
59  : shape(shape), _storage(std::forward<Vector>(x))
60  {
61  // Do nothing
62  }
63 
66  constexpr array2d(std::initializer_list<std::initializer_list<T>> list)
67  : shape({list.size(), (*list.begin()).size()})
68  {
69  _storage.reserve(shape[0] * shape[1]);
70  for (std::initializer_list<T> l : list)
71  for (const T val : l)
72  _storage.push_back(val);
73  }
74 
76  array2d(const array2d& x) = default;
77 
79  array2d(array2d&& x) = default;
80 
82  ~array2d() = default;
83 
85  array2d& operator=(const array2d& x) = default;
86 
88  array2d& operator=(array2d&& x) = default;
89 
95  constexpr reference operator()(size_type i, size_type j)
96  {
97  return _storage[i * shape[1] + j];
98  }
99 
106  constexpr const_reference operator()(size_type i, size_type j) const
107  {
108  return _storage[i * shape[1] + j];
109  }
110 
114  constexpr xtl::span<value_type> row(size_type i)
115  {
116  return xtl::span<value_type>(std::next(_storage.data(), i * shape[1]),
117  shape[1]);
118  }
119 
123  constexpr xtl::span<const value_type> row(size_type i) const
124  {
125  return xtl::span<const value_type>(std::next(_storage.data(), i * shape[1]),
126  shape[1]);
127  }
128 
131  constexpr value_type* data() noexcept { return _storage.data(); }
132 
136  constexpr const value_type* data() const noexcept { return _storage.data(); };
137 
142  constexpr size_type size() const noexcept { return _storage.size(); }
143 
145  constexpr std::array<size_type, 2> strides() const noexcept
146  {
147  return {shape[1] * sizeof(T), sizeof(T)};
148  }
149 
152  constexpr bool empty() const noexcept { return _storage.empty(); }
153 
155  std::array<size_type, 2> shape;
156 
157 private:
158  std::vector<T, Allocator> _storage;
159 };
160 } // namespace dolfinx
This class provides a dynamic 2-dimensional row-wise array data structure.
Definition: array2d.h:21
constexpr xtl::span< value_type > row(size_type i)
Access a row in the array.
Definition: array2d.h:114
constexpr reference operator()(size_type i, size_type j)
Return a reference to the element at specified location (i, j)
Definition: array2d.h:95
constexpr xtl::span< const value_type > row(size_type i) const
Access a row in the array (const version)
Definition: array2d.h:123
constexpr const_reference operator()(size_type i, size_type j) const
Return a reference to the element at specified location (i, j) (const version)
Definition: array2d.h:106
array2d(std::array< size_type, 2 > shape, value_type value=T(), const Allocator &alloc=Allocator())
Construct a two dimensional array.
Definition: array2d.h:38
std::array< size_type, 2 > shape
The shape of the array.
Definition: array2d.h:155
array2d(array2d &&x)=default
Move constructor.
array2d & operator=(array2d &&x)=default
Move assignment.
~array2d()=default
Destructor.
constexpr bool empty() const noexcept
Checks whether the container is empty.
Definition: array2d.h:152
constexpr array2d(std::initializer_list< std::initializer_list< T >> list)
Construct a two dimensional array using nested initializer lists.
Definition: array2d.h:66
array2d(size_type rows, size_type cols, value_type value=T(), const Allocator &alloc=Allocator())
Construct a two dimensional array.
Definition: array2d.h:49
constexpr value_type * data() noexcept
Get pointer to the first element of the underlying storage.
Definition: array2d.h:131
constexpr size_type size() const noexcept
Returns the number of elements in the array.
Definition: array2d.h:142
array2d & operator=(const array2d &x)=default
Copy assignment.
constexpr std::array< size_type, 2 > strides() const noexcept
Returns the strides of the array.
Definition: array2d.h:145
array2d(std::array< size_type, 2 > shape, Vector &&x)
Definition: array2d.h:58
constexpr const value_type * data() const noexcept
Get pointer to the first element of the underlying storage (const version)
Definition: array2d.h:136
array2d(const array2d &x)=default
Copy constructor.