18 void check(
bool ok,
const std::string& what)
21 std::cerr <<
"FAILED: " << what <<
'\n';
27 void make_graph(
const std::vector<std::vector<SF_int>>& adj,
28 vector<SF_int>& cnt, vector<SF_int>& dsp, vector<SF_int>& con)
30 cnt.resize(adj.size());
31 for (
size_t i = 0; i < adj.size(); i++) {
32 cnt[i] =
SF_int(adj[i].size());
33 for (
const SF_int c : adj[i]) con.push_back(c);
40 bool is_permutation(
const perm_map& old2new,
size_t n)
42 if (old2new.size() != n)
return false;
44 std::vector<bool> seen(n,
false);
45 for (
size_t i = 0; i < n; i++) {
47 if (!old2new.count(key))
return false;
48 const SF_int val = old2new.at(key);
49 if (val < 0 ||
size_t(val) >= n || seen[val])
return false;
56 std::vector<std::vector<SF_int>> path_graph(
size_t n)
58 std::vector<std::vector<SF_int>> adj(n);
59 for (
size_t i = 0; i < n; i++) {
60 if (i > 0) adj[i].push_back(
SF_int(i - 1));
61 adj[i].push_back(
SF_int(i));
62 if (i + 1 < n) adj[i].push_back(
SF_int(i + 1));
67 void test_path_reverse()
70 vector<SF_int> cnt, dsp, con;
72 make_graph(path_graph(n), cnt, dsp, con);
76 check(is_permutation(old2new, n),
"path graph, reverse: result is a permutation");
78 for (
size_t i = 0; i < n; i++)
79 check(old2new.at(
SF_int(i)) ==
SF_int(n - 1 - i),
"path graph, reverse: expected index");
82 void test_path_forward()
85 vector<SF_int> cnt, dsp, con;
87 make_graph(path_graph(n), cnt, dsp, con);
91 check(is_permutation(old2new, n),
"path graph, forward: result is a permutation");
92 for (
size_t i = 0; i < n; i++)
93 check(old2new.at(
SF_int(i)) ==
SF_int(i),
"path graph, forward: expected index");
97 void test_empty_graph()
99 vector<SF_int> cnt, dsp, con;
104 check(old2new.empty(),
"empty graph: no indices produced");
109 void test_disconnected_components()
112 std::vector<std::vector<SF_int>> adj(n);
113 adj[0].push_back(1); adj[1].push_back(0);
114 adj[2].push_back(3); adj[3].push_back(2);
116 vector<SF_int> cnt, dsp, con;
118 make_graph(adj, cnt, dsp, con);
122 check(is_permutation(old2new, n),
"disconnected components: result is a permutation");
128 void test_isolated_node()
131 std::vector<std::vector<SF_int>> adj(n);
132 adj[0].push_back(0); adj[0].push_back(1);
133 adj[1].push_back(0); adj[1].push_back(1);
137 vector<SF_int> cnt, dsp, con;
139 make_graph(adj, cnt, dsp, con);
143 check(is_permutation(old2new, n),
"isolated node: result is a permutation");
153 test_disconnected_components();
154 test_isolated_node();
156 return failures ? 1 : 0;
opencarp::global_index_t SF_int
Global algebraic index type.
Classes related to mesh node renumbering.
Basic utility structs and functions, mostly IO related.
A vector storing arbitrary data.
void dsp_from_cnt(const vector< T > &cnt, vector< T > &dsp)
Compute displacements from counts.
void reindex_cuthill_mckee(const vector< T > &n2n_cnt, const vector< T > &n2n_dsp, const vector< T > &n2n_con, const bool reverse, hashmap::unordered_map< T, T > &old2new)