DOLFINx 0.11.0.0
DOLFINx C++
Loading...
Searching...
No Matches
AdjacencyList.h
1// Copyright (C) 2019-2025 Garth N. Wells
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 <cassert>
10#include <concepts>
11#include <cstdint>
12#include <numeric>
13#include <optional>
14#include <span>
15#include <sstream>
16#include <utility>
17#include <vector>
18
20{
36template <typename LinkData, typename NodeData = std::nullptr_t>
38{
39public:
41 using link_type = LinkData;
43 using node_data_type = NodeData;
44
48 explicit AdjacencyList(const std::int32_t n) : _array(n), _offsets(n + 1)
49 {
50 std::iota(_array.begin(), _array.end(), 0);
51 std::iota(_offsets.begin(), _offsets.end(), 0);
52 }
53
61 template <typename U, typename V>
62 requires std::is_convertible_v<std::remove_cvref_t<U>,
63 std::vector<LinkData>>
64 and std::is_convertible_v<std::remove_cvref_t<V>,
65 std::vector<std::int32_t>>
66 AdjacencyList(U&& data, V&& offsets)
67 : _array(std::forward<U>(data)), _offsets(std::forward<V>(offsets))
68 {
69 assert(_offsets.back() == (std::int32_t)_array.size());
70 }
71
80 template <typename U, typename V, typename W>
81 requires std::is_convertible_v<std::remove_cvref_t<U>,
82 std::vector<LinkData>>
83 and std::is_convertible_v<std::remove_cvref_t<V>,
84 std::vector<std::int32_t>>
85 and std::is_convertible_v<std::remove_cvref_t<W>,
86 std::vector<NodeData>>
87 AdjacencyList(U&& data, V&& offsets, W&& node_data)
88 : _array(std::forward<U>(data)), _offsets(std::forward<V>(offsets)),
89 _node_data(std::forward<W>(node_data))
90 {
91 assert(_node_data.has_value()
92 and _node_data->size() == _offsets.size() - 1);
93 _array.reserve(_offsets.back());
94 assert(_offsets.back() == (std::int32_t)_array.size());
95 }
96
102 template <typename X>
103 explicit AdjacencyList(const std::vector<X>& data)
104 {
105 // Initialize offsets and compute total size
106 _offsets.reserve(data.size() + 1);
107 _offsets.push_back(0);
108 for (auto& row : data)
109 _offsets.push_back(_offsets.back() + row.size());
110
111 _array.reserve(_offsets.back());
112 for (auto& e : data)
113 _array.insert(_array.end(), e.begin(), e.end());
114 }
115
117 AdjacencyList(const AdjacencyList& list) = default;
118
120 AdjacencyList(AdjacencyList&& list) = default;
121
123 ~AdjacencyList() = default;
124
126 AdjacencyList& operator=(const AdjacencyList& list) = default;
127
130
133 bool operator==(const AdjacencyList& list) const
134 {
135 return this->_array == list._array and this->_offsets == list._offsets;
136 }
137
140 std::int32_t num_nodes() const { return _offsets.size() - 1; }
141
145 int num_links(std::size_t node) const
146 {
147 assert((node + 1) < _offsets.size());
148 return _offsets[node + 1] - _offsets[node];
149 }
150
155 std::span<LinkData> links(std::size_t node)
156 {
157 auto it = std::next(_offsets.begin(), node);
158 return std::span<LinkData>(std::next(_array.begin(), *it),
159 std::next(_array.begin(), *(it + 1)));
160 }
161
166 std::span<const LinkData> links(std::size_t node) const
167 {
168 auto it = std::next(_offsets.begin(), node);
169 return std::span<const LinkData>(std::next(_array.begin(), *it),
170 std::next(_array.begin(), *(it + 1)));
171 }
172
174 const std::vector<LinkData>& array() const { return _array; }
175
177 std::vector<LinkData>& array() { return _array; }
178
180 const std::vector<std::int32_t>& offsets() const { return _offsets; }
181
183 std::vector<std::int32_t>& offsets() { return _offsets; }
184
187 const std::optional<std::vector<NodeData>>& node_data() const
188 {
189 return _node_data;
190 }
191
194 std::optional<std::vector<NodeData>>& node_data() { return _node_data; }
195
198 std::string str() const
199 {
200 std::stringstream s;
201 s << "<AdjacencyList> with " + std::to_string(this->num_nodes()) + " nodes"
202 << std::endl;
203 for (std::size_t e = 0; e < _offsets.size() - 1; ++e)
204 {
205 s << " " << e << ": [";
206 for (auto link : this->links(e))
207 s << link << " ";
208 s << "]" << '\n';
209 }
210 return s.str();
211 }
212
213private:
214 // Connections (links/edges) for all entities stored as a contiguous
215 // array
216 std::vector<LinkData> _array;
217
218 // Position of first connection for each entity (using local index)
219 std::vector<std::int32_t> _offsets;
220
221 // Node data, where _node_data[i] is the data associated with node `i`
222 std::optional<std::vector<NodeData>> _node_data = std::nullopt;
223};
224
226template <typename T, typename U>
227AdjacencyList(T, U) -> AdjacencyList<typename T::value_type, std::nullptr_t>;
228
230template <typename T, typename U, typename W>
231AdjacencyList(T, U, W)
232 -> AdjacencyList<typename T::value_type, typename W::value_type>;
233
242template <typename V = std::nullptr_t, typename U>
243 requires requires {
244 typename std::decay_t<U>::value_type;
245 requires std::convertible_to<
246 U, std::vector<typename std::decay_t<U>::value_type>>;
247 }
248AdjacencyList<typename std::decay_t<U>::value_type, V>
249regular_adjacency_list(U&& data, int degree)
250{
251 if (degree == 0 and !data.empty())
252 {
253 throw std::runtime_error("Degree is zero but data is not empty for "
254 "constant degree AdjacencyList");
255 }
256
257 if (degree > 0 and data.size() % degree != 0)
258 {
259 throw std::runtime_error(
260 "Incompatible data size and degree for constant degree AdjacencyList");
261 }
262
263 std::int32_t num_nodes = degree == 0 ? data.size() : data.size() / degree;
264 std::vector<std::int32_t> offsets(num_nodes + 1, 0);
265 for (std::size_t i = 1; i < offsets.size(); ++i)
266 offsets[i] = offsets[i - 1] + degree;
268 std::forward<U>(data), std::move(offsets));
269}
270
271} // namespace dolfinx::graph
This class provides a static adjacency list data structure.
Definition AdjacencyList.h:38
int num_links(std::size_t node) const
Number of connections for given node.
Definition AdjacencyList.h:145
bool operator==(const AdjacencyList &list) const
Definition AdjacencyList.h:133
std::vector< std::int32_t > & offsets()
Offset for each node in array().
Definition AdjacencyList.h:183
AdjacencyList & operator=(AdjacencyList &&list)=default
Move assignment operator.
AdjacencyList(U &&data, V &&offsets, W &&node_data)
Construct adjacency list from arrays of link (edge) data, offsets, and node data.
Definition AdjacencyList.h:87
const std::vector< LinkData > & array() const
Return contiguous array of links for all nodes (const version).
Definition AdjacencyList.h:174
AdjacencyList(const std::int32_t n)
Construct trivial adjacency list where each of the n nodes is connected to itself.
Definition AdjacencyList.h:48
AdjacencyList(AdjacencyList &&list)=default
Move constructor.
AdjacencyList & operator=(const AdjacencyList &list)=default
Assignment operator.
std::span< LinkData > links(std::size_t node)
Get the links (edges) for given node.
Definition AdjacencyList.h:155
AdjacencyList(U &&data, V &&offsets)
Construct adjacency list from arrays of link (edge) data and offsets.
Definition AdjacencyList.h:66
AdjacencyList(const std::vector< X > &data)
Definition AdjacencyList.h:103
NodeData node_data_type
Adjacency list node data type.
Definition AdjacencyList.h:43
std::int32_t num_nodes() const
Get the number of nodes.
Definition AdjacencyList.h:140
AdjacencyList(const AdjacencyList &list)=default
Copy constructor.
LinkData link_type
Adjacency list link (edge) type.
Definition AdjacencyList.h:41
const std::optional< std::vector< NodeData > > & node_data() const
Definition AdjacencyList.h:187
const std::vector< std::int32_t > & offsets() const
Offset for each node in array() (const version).
Definition AdjacencyList.h:180
std::span< const LinkData > links(std::size_t node) const
Get the links (edges) for given node (const version).
Definition AdjacencyList.h:166
std::string str() const
Informal string representation (pretty-print).
Definition AdjacencyList.h:198
std::optional< std::vector< NodeData > > & node_data()
Definition AdjacencyList.h:194
~AdjacencyList()=default
Destructor.
std::vector< LinkData > & array()
Return contiguous array of links for all nodes.
Definition AdjacencyList.h:177
Graph data structures and algorithms.
Definition AdjacencyList.h:20
AdjacencyList< typename std::decay_t< U >::value_type, V > regular_adjacency_list(U &&data, int degree)
Construct a constant degree (valency) adjacency list.
Definition AdjacencyList.h:249