32 explicit AdjacencyList(
const std::int32_t n) : _array(n), _offsets(n + 1)
34 std::iota(_array.begin(), _array.end(), 0);
35 std::iota(_offsets.begin(), _offsets.end(), 0);
42 template <
typename U,
typename V>
43 requires std::is_convertible_v<std::remove_cvref_t<U>, std::vector<T>>
44 and std::is_convertible_v<std::remove_cvref_t<V>,
45 std::vector<std::int32_t>>
47 : _array(std::forward<U>(data)), _offsets(std::forward<V>(
offsets))
49 _array.reserve(_offsets.back());
50 assert(_offsets.back() == (std::int32_t)_array.size());
62 _offsets.reserve(data.size() + 1);
63 _offsets.push_back(0);
64 for (
auto& row : data)
65 _offsets.push_back(_offsets.back() + row.size());
67 _array.reserve(_offsets.back());
69 _array.insert(_array.end(), e.begin(), e.end());
91 return this->_array == list._array and this->_offsets == list._offsets;
96 std::int32_t
num_nodes()
const {
return _offsets.size() - 1; }
103 assert((node + 1) < _offsets.size());
104 return _offsets[node + 1] - _offsets[node];
111 std::span<T>
links(std::size_t node)
113 return std::span<T>(_array.data() + _offsets[node],
114 _offsets[node + 1] - _offsets[node]);
121 std::span<const T>
links(std::size_t node)
const
123 return std::span<const T>(_array.data() + _offsets[node],
124 _offsets[node + 1] - _offsets[node]);
128 const std::vector<T>&
array()
const {
return _array; }
131 std::vector<T>&
array() {
return _array; }
134 const std::vector<std::int32_t>&
offsets()
const {
return _offsets; }
137 std::vector<std::int32_t>&
offsets() {
return _offsets; }
144 s <<
"<AdjacencyList> with " + std::to_string(this->
num_nodes()) +
" nodes"
146 for (std::size_t e = 0; e < _offsets.size() - 1; ++e)
148 s <<
" " << e <<
": [";
149 for (
auto link : this->
links(e))
151 s <<
"]" << std::endl;
158 std::vector<T> _array;
161 std::vector<std::int32_t> _offsets;
165template <
typename T,
typename U>
166AdjacencyList(T, U) -> AdjacencyList<typename T::value_type>;
177 typename std::decay_t<U>::value_type;
178 requires std::convertible_to<
179 U, std::vector<typename std::decay_t<U>::value_type>>;
181AdjacencyList<typename std::decay_t<U>::value_type>
184 if (degree == 0 and !data.empty())
186 throw std::runtime_error(
"Degree is zero but data is not empty for "
187 "constant degree AdjacencyList");
190 if (degree > 0 and data.size() % degree != 0)
192 throw std::runtime_error(
193 "Incompatible data size and degree for constant degree AdjacencyList");
196 std::int32_t num_nodes = degree == 0 ? data.size() : data.size() / degree;
197 std::vector<std::int32_t> offsets(num_nodes + 1, 0);
198 for (std::size_t i = 1; i < offsets.size(); ++i)
199 offsets[i] = offsets[i - 1] + degree;
201 std::forward<U>(data), std::move(offsets));
Definition topologycomputation.h:24
int num_links(std::size_t node) const
Definition AdjacencyList.h:101
std::span< T > links(std::size_t node)
Definition AdjacencyList.h:111
bool operator==(const AdjacencyList &list) const
Definition AdjacencyList.h:89
std::vector< std::int32_t > & offsets()
Offset for each node in array()
Definition AdjacencyList.h:137
const std::vector< T > & array() const
Return contiguous array of links for all nodes (const version)
Definition AdjacencyList.h:128
AdjacencyList & operator=(AdjacencyList &&list)=default
Move assignment operator.
std::span< const T > links(std::size_t node) const
Definition AdjacencyList.h:121
AdjacencyList(const std::int32_t n)
Definition AdjacencyList.h:32
AdjacencyList(AdjacencyList &&list)=default
Move constructor.
AdjacencyList & operator=(const AdjacencyList &list)=default
Assignment operator.
AdjacencyList(const std::vector< X > &data)
Definition AdjacencyList.h:59
std::int32_t num_nodes() const
Definition AdjacencyList.h:96
AdjacencyList(const AdjacencyList &list)=default
Copy constructor.
std::vector< T > & array()
Return contiguous array of links for all nodes.
Definition AdjacencyList.h:131
AdjacencyList(U &&data, V &&offsets)
Definition AdjacencyList.h:46
const std::vector< std::int32_t > & offsets() const
Offset for each node in array() (const version)
Definition AdjacencyList.h:134
std::string str() const
Definition AdjacencyList.h:141
~AdjacencyList()=default
Destructor.
Graph data structures and algorithms.
Definition dofmapbuilder.h:26
AdjacencyList< typename std::decay_t< U >::value_type > regular_adjacency_list(U &&data, int degree)
Construct a constant degree (valency) adjacency list.
Definition AdjacencyList.h:182