Note: this is documentation for an old release. View the latest documentation at docs.fenicsproject.org/v0.1.0/v0.9.0/cpp
DOLFINx  0.1.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)
41  {
42  _storage = std::vector<T, Allocator>(shape[0] * shape[1], value, alloc);
43  }
44 
50  array2d(size_type rows, size_type cols, value_type value = T(),
51  const Allocator& alloc = Allocator())
52  : shape({rows, cols})
53  {
54  _storage = std::vector<T, Allocator>(shape[0] * shape[1], value, alloc);
55  }
56 
59  template <typename Vector>
60  array2d(std::array<size_type, 2> shape, Vector&& x)
61  : shape(shape), _storage(std::forward<Vector>(x))
62  {
63  // Do nothing
64  }
65 
68  constexpr array2d(std::initializer_list<std::initializer_list<T>> list)
69  : shape({list.size(), (*list.begin()).size()})
70  {
71  _storage.reserve(shape[0] * shape[1]);
72  for (std::initializer_list<T> l : list)
73  for (const T val : l)
74  _storage.push_back(val);
75  }
76 
78  array2d(const array2d& x) = default;
79 
81  array2d(array2d&& x) = default;
82 
84  ~array2d() = default;
85 
87  array2d& operator=(const array2d& x) = default;
88 
90  array2d& operator=(array2d&& x) = default;
91 
97  constexpr reference operator()(size_type i, size_type j)
98  {
99  return _storage[i * shape[1] + j];
100  }
101 
108  constexpr const_reference operator()(size_type i, size_type j) const
109  {
110  return _storage[i * shape[1] + j];
111  }
112 
116  constexpr xtl::span<value_type> row(size_type i)
117  {
118  return xtl::span<value_type>(std::next(_storage.data(), i * shape[1]),
119  shape[1]);
120  }
121 
125  constexpr xtl::span<const value_type> row(size_type i) const
126  {
127  return xtl::span<const value_type>(std::next(_storage.data(), i * shape[1]),
128  shape[1]);
129  }
130 
133  constexpr value_type* data() noexcept { return _storage.data(); }
134 
138  constexpr const value_type* data() const noexcept { return _storage.data(); };
139 
144  constexpr size_type size() const noexcept { return _storage.size(); }
145 
147  constexpr std::array<size_type, 2> strides() const noexcept
148  {
149  return {shape[1] * sizeof(T), sizeof(T)};
150  }
151 
154  constexpr bool empty() const noexcept { return _storage.empty(); }
155 
157  std::array<size_type, 2> shape;
158 
159 private:
160  std::vector<T, Allocator> _storage;
161 };
162 } // namespace dolfinx
dolfinx::array2d::empty
constexpr bool empty() const noexcept
Checks whether the container is empty.
Definition: array2d.h:154
dolfinx::array2d::row
constexpr xtl::span< const value_type > row(size_type i) const
Access a row in the array (const version)
Definition: array2d.h:125
dolfinx::array2d::array2d
array2d(size_type rows, size_type cols, value_type value=T(), const Allocator &alloc=Allocator())
Construct a two dimensional array.
Definition: array2d.h:50
dolfinx::array2d::~array2d
~array2d()=default
Destructor.
dolfinx::array2d::size
constexpr size_type size() const noexcept
Returns the number of elements in the array.
Definition: array2d.h:144
dolfinx::array2d::data
constexpr const value_type * data() const noexcept
Get pointer to the first element of the underlying storage (const version)
Definition: array2d.h:138
dolfinx::array2d::array2d
array2d(std::array< size_type, 2 > shape, Vector &&x)
Definition: array2d.h:60
dolfinx::array2d::operator=
array2d & operator=(const array2d &x)=default
Copy assignment.
dolfinx::array2d::array2d
constexpr array2d(std::initializer_list< std::initializer_list< T >> list)
Construct a two dimensional array using nested initializer lists.
Definition: array2d.h:68
dolfinx::array2d::operator()
constexpr reference operator()(size_type i, size_type j)
Return a reference to the element at specified location (i, j)
Definition: array2d.h:97
dolfinx::array2d::array2d
array2d(std::array< size_type, 2 > shape, value_type value=T(), const Allocator &alloc=Allocator())
Construct a two dimensional array.
Definition: array2d.h:38
dolfinx::array2d
This class provides a dynamic 2-dimensional row-wise array data structure.
Definition: array2d.h:20
dolfinx::array2d::operator()
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:108
dolfinx::array2d::shape
std::array< size_type, 2 > shape
The shape of the array.
Definition: array2d.h:157
dolfinx::array2d::row
constexpr xtl::span< value_type > row(size_type i)
Access a row in the array.
Definition: array2d.h:116
dolfinx::array2d::strides
constexpr std::array< size_type, 2 > strides() const noexcept
Returns the strides of the array.
Definition: array2d.h:147
dolfinx::array2d::data
constexpr value_type * data() noexcept
Get pointer to the first element of the underlying storage.
Definition: array2d.h:133