39template <
typename LinkData,
typename NodeData = std::
nullptr_t>
51 explicit AdjacencyList(
const std::int32_t n) : _array(n), _offsets(n + 1)
53 std::iota(_array.begin(), _array.end(), 0);
54 std::iota(_offsets.begin(), _offsets.end(), 0);
64 template <
typename U,
typename V>
65 requires std::is_convertible_v<std::remove_cvref_t<U>,
66 std::vector<LinkData>>
67 and std::is_convertible_v<std::remove_cvref_t<V>,
68 std::vector<std::int32_t>>
70 : _array(std::forward<U>(data)), _offsets(std::forward<V>(
offsets))
72 if (_offsets.back() != (std::int32_t)_array.size())
74 throw std::runtime_error(
"Last offset must equal the size of the data "
87 template <
typename U,
typename V,
typename W>
88 requires std::is_convertible_v<std::remove_cvref_t<U>,
89 std::vector<LinkData>>
90 and std::is_convertible_v<std::remove_cvref_t<V>,
91 std::vector<std::int32_t>>
92 and std::is_convertible_v<std::remove_cvref_t<W>,
93 std::vector<NodeData>>
95 : _array(std::forward<U>(data)), _offsets(std::forward<V>(
offsets)),
98 if (!_node_data.has_value() or _node_data->size() != _offsets.size() - 1)
100 throw std::runtime_error(
"Node data size must equal the number of "
103 _array.reserve(_offsets.back());
104 if (_offsets.back() != (std::int32_t)_array.size())
106 throw std::runtime_error(
"Last offset must equal the size of the data "
116 template <
typename X>
120 _offsets.reserve(data.size() + 1);
121 _offsets.push_back(0);
122 for (
auto& row : data)
123 _offsets.push_back(_offsets.back() + row.size());
125 _array.reserve(_offsets.back());
127 _array.insert(_array.end(), e.begin(), e.end());
149 return this->_array == list._array and this->_offsets == list._offsets;
154 std::int32_t
num_nodes()
const {
return _offsets.size() - 1; }
161 assert((node + 1) < _offsets.size());
162 return _offsets[node + 1] - _offsets[node];
169 std::span<LinkData>
links(std::size_t node)
171 auto it = std::next(_offsets.begin(), node);
172 return std::span<LinkData>(std::next(_array.begin(), *it),
173 std::next(_array.begin(), *(it + 1)));
180 std::span<const LinkData>
links(std::size_t node)
const
182 auto it = std::next(_offsets.begin(), node);
183 return std::span<const LinkData>(std::next(_array.begin(), *it),
184 std::next(_array.begin(), *(it + 1)));
188 const std::vector<LinkData>&
array()
const {
return _array; }
191 std::vector<LinkData>&
array() {
return _array; }
194 const std::vector<std::int32_t>&
offsets()
const {
return _offsets; }
197 std::vector<std::int32_t>&
offsets() {
return _offsets; }
201 const std::optional<std::vector<NodeData>>&
node_data()
const
208 std::optional<std::vector<NodeData>>&
node_data() {
return _node_data; }
215 = std::format(
"<AdjacencyList> with {} nodes\n", this->
num_nodes());
216 for (std::size_t e = 0; e < _offsets.size() - 1; ++e)
218 std::format_to(std::back_inserter(s),
" {}: [", e);
219 for (
auto link : this->
links(e))
220 std::format_to(std::back_inserter(s),
"{} ", link);
229 std::vector<LinkData> _array;
232 std::vector<std::int32_t> _offsets;
235 std::optional<std::vector<NodeData>> _node_data = std::nullopt;
239template <
typename T,
typename U>
240AdjacencyList(T, U) -> AdjacencyList<typename T::value_type, std::nullptr_t>;
243template <
typename T,
typename U,
typename W>
244AdjacencyList(T, U, W)
245 -> AdjacencyList<typename T::value_type, typename W::value_type>;
255template <
typename V = std::
nullptr_t,
typename U>
257 typename std::decay_t<U>::value_type;
258 requires std::convertible_to<
259 U, std::vector<typename std::decay_t<U>::value_type>>;
261AdjacencyList<typename std::decay_t<U>::value_type, V>
264 if (degree == 0 and !data.empty())
266 throw std::runtime_error(
"Degree is zero but data is not empty for "
267 "constant degree AdjacencyList");
270 if (degree > 0 and data.size() % degree != 0)
272 throw std::runtime_error(
273 "Incompatible data size and degree for constant degree AdjacencyList");
276 std::int32_t num_nodes = degree == 0 ? data.size() : data.size() / degree;
277 std::vector<std::int32_t> offsets(num_nodes + 1, 0);
278 for (std::size_t i = 1; i < offsets.size(); ++i)
279 offsets[i] = offsets[i - 1] + degree;
281 std::forward<U>(data), std::move(offsets));
This class provides a static adjacency list data structure.
Definition AdjacencyList.h:41
int num_links(std::size_t node) const
Number of connections for given node.
Definition AdjacencyList.h:159
bool operator==(const AdjacencyList &list) const
Definition AdjacencyList.h:147
std::vector< std::int32_t > & offsets()
Offset for each node in array().
Definition AdjacencyList.h:197
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:94
const std::vector< LinkData > & array() const
Return contiguous array of links for all nodes (const version).
Definition AdjacencyList.h:188
AdjacencyList(const std::int32_t n)
Construct trivial adjacency list where each of the n nodes is connected to itself.
Definition AdjacencyList.h:51
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:169
AdjacencyList(U &&data, V &&offsets)
Construct adjacency list from arrays of link (edge) data and offsets.
Definition AdjacencyList.h:69
AdjacencyList(const std::vector< X > &data)
Definition AdjacencyList.h:117
NodeData node_data_type
Adjacency list node data type.
Definition AdjacencyList.h:46
std::int32_t num_nodes() const
Get the number of nodes.
Definition AdjacencyList.h:154
AdjacencyList(const AdjacencyList &list)=default
Copy constructor.
LinkData link_type
Adjacency list link (edge) type.
Definition AdjacencyList.h:44
const std::optional< std::vector< NodeData > > & node_data() const
Definition AdjacencyList.h:201
const std::vector< std::int32_t > & offsets() const
Offset for each node in array() (const version).
Definition AdjacencyList.h:194
std::span< const LinkData > links(std::size_t node) const
Get the links (edges) for given node (const version).
Definition AdjacencyList.h:180
std::string str() const
Informal string representation (pretty-print).
Definition AdjacencyList.h:212
std::optional< std::vector< NodeData > > & node_data()
Definition AdjacencyList.h:208
~AdjacencyList()=default
Destructor.
std::vector< LinkData > & array()
Return contiguous array of links for all nodes.
Definition AdjacencyList.h:191
Graph data structures and algorithms.
Definition AdjacencyList.h:23
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:262