11 #include <dolfinx/common/span.hpp>
28 std::vector<typename T::Scalar> data(matrix.rows() * matrix.cols());
29 std::vector<std::int32_t> offset(matrix.rows() + 1, 0);
30 for (Eigen::Index i = 0; i < matrix.rows(); ++i)
32 for (Eigen::Index j = 0; j < matrix.cols(); ++j)
33 data[i * matrix.cols() + j] = matrix(i, j);
34 offset[i + 1] = offset[i] + matrix.cols();
36 return std::pair(std::move(data), std::move(offset));
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);
62 typename U,
typename V,
63 typename = std::enable_if_t<
64 std::is_same<std::vector<T>, std::decay_t<U>>::value
65 && std::is_same<std::vector<std::int32_t>, std::decay_t<V>>::value>>
67 : _array(std::forward<U>(data)), _offsets(std::forward<V>(
offsets))
69 assert(_offsets.back() == (std::int32_t)_array.size());
80 _offsets.reserve(data.size() + 1);
81 _offsets.push_back(0);
82 for (
auto row = data.begin(); row != data.end(); ++row)
83 _offsets.push_back(_offsets.back() + row->size());
85 _array.reserve(_offsets.back());
86 for (
auto e = data.begin(); e != data.end(); ++e)
87 _array.insert(_array.end(), e->begin(), e->end());
108 return this->_array == list._array and this->_offsets == list._offsets;
113 std::int32_t
num_nodes()
const {
return _offsets.size() - 1; }
120 assert((node + 1) < (
int)_offsets.size());
121 return _offsets[node + 1] - _offsets[node];
130 return tcb::span(_array.data() + _offsets[node],
131 _offsets[node + 1] - _offsets[node]);
138 tcb::span<const T>
links(
int node)
const
140 return tcb::span(_array.data() + _offsets[node],
141 _offsets[node + 1] - _offsets[node]);
145 const std::vector<T>&
array()
const {
return _array; }
148 std::vector<T>&
array() {
return _array; }
151 const std::vector<std::int32_t>&
offsets()
const {
return _offsets; }
155 template <
typename X>
160 #ifdef __INTEL_COMPILER
161 #pragma warning(disable : 1011)
164 if constexpr (std::is_same<X, T>::value)
169 std::vector<X>(_array.begin(), _array.end()), this->_offsets);
177 s <<
"<AdjacencyList> with " + std::to_string(this->
num_nodes()) +
" nodes"
179 for (std::size_t e = 0; e < _offsets.size() - 1; ++e)
181 s <<
" " << e <<
": [";
182 for (
auto link : this->
links(e))
184 s <<
"]" << std::endl;
191 std::vector<T> _array;
194 std::vector<std::int32_t> _offsets;
203 template <
typename T,
typename U>
207 assert(data.size() % degree == 0);
208 std::vector<std::int32_t> offsets(data.size() / degree + 1, 0);
209 for (std::size_t i = 1; i < offsets.size(); ++i)
210 offsets[i] = offsets[i - 1] + degree;
This class provides a static adjacency list data structure. It is commonly used to store directed gra...
Definition: AdjacencyList.h:46
tcb::span< const T > links(int node) const
Get the links (edges) for given node (const version)
Definition: AdjacencyList.h:138
bool operator==(const AdjacencyList &list) const
Equality operator.
Definition: AdjacencyList.h:106
std::string str() const
Return informal string representation (pretty-print)
Definition: AdjacencyList.h:174
AdjacencyList & operator=(AdjacencyList &&list)=default
Move assignment.
~AdjacencyList()=default
Destructor.
AdjacencyList(const AdjacencyList &list)=default
Copy constructor.
AdjacencyList(const std::int32_t n)
Construct trivial adjacency list where each of the n nodes is connected to itself.
Definition: AdjacencyList.h:51
const std::vector< std::int32_t > & offsets() const
Offset for each node in array() (const version)
Definition: AdjacencyList.h:151
tcb::span< T > links(int node)
Get the links (edges) for given node.
Definition: AdjacencyList.h:128
std::int32_t num_nodes() const
Get the number of nodes.
Definition: AdjacencyList.h:113
AdjacencyList(AdjacencyList &&list)=default
Move constructor.
decltype(auto) as_type() const
Copy of the Adjacency List if the specified type is different from the current type,...
Definition: AdjacencyList.h:156
std::vector< T > & array()
Return contiguous array of links for all nodes.
Definition: AdjacencyList.h:148
AdjacencyList(U &&data, V &&offsets)
Construct adjacency list from arrays of data.
Definition: AdjacencyList.h:66
const std::vector< T > & array() const
Return contiguous array of links for all nodes (const version)
Definition: AdjacencyList.h:145
int num_links(int node) const
Number of connections for given node.
Definition: AdjacencyList.h:118
AdjacencyList(const std::vector< X > &data)
Set all connections for all entities (T is a '2D' container, e.g. a std::vector<<std::vector<std::siz...
Definition: AdjacencyList.h:77
AdjacencyList & operator=(const AdjacencyList &list)=default
Assignment.
Graph data structures and algorithms.
Definition: AdjacencyList.h:18
auto create_adjacency_data(const Eigen::DenseBase< T > &matrix)
Construct adjacency list data for a problem with a fixed number of links (edges) for each node.
Definition: AdjacencyList.h:26
AdjacencyList< T > build_adjacency_list(U &&data, int degree)
Construct an adjacency list from array of data for a graph with constant degree (valency)....
Definition: AdjacencyList.h:204