Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Dominators.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
31
105# ifndef DOMINATORS_H
106# define DOMINATORS_H
107
108# include <ah-graph-concepts.H>
109
110# include <utility>
111# include <tpl_graph.H>
112# include <tpl_graph_utils.H>
113# include <tpl_array.H>
114# include <htlist.H>
115# include <tpl_dynListStack.H>
116# include <ah-errors.H>
117# include <tpl_dynSetTree.H>
118
119namespace Aleph
120{
154 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
156 {
157 SA sa;
158
159 using Node = typename GT::Node;
160 using Arc = typename GT::Arc;
161
162 GT *gptr = nullptr;
163 Node *root_ptr = nullptr;
164 bool is_computed = false;
166
167 // Internal arrays indexed by DFS number
168 Array<Node *> vertex; // DFS number -> node pointer
169 Array<long> par; // DFS parent (as DFS number)
170 Array<long> semi; // semi-dominator (as DFS number)
171 Array<long> idom_arr; // immediate dominator (as DFS number)
172 Array<long> anc; // Union-Find ancestor
173 Array<long> label_arr; // Union-Find label (best semi on path)
174 Array<DynList<long>> pred; // predecessors[i] = DFS numbers of predecessors of node i
175
184 void compress(long v)
185 {
186 if (anc[v] == -1 or anc[anc[v]] == -1)
187 return;
188
189 // First pass: find the path to the root
190 Array<long> path;
191 long root = v;
192 while (anc[root] != -1 and anc[anc[root]] != -1)
193 {
194 path.append(root);
195 root = anc[root];
196 }
197
198 // Second pass: compress path and update labels
199 // Iterate from root-child down to v
200 while (not path.is_empty())
201 {
202 const long u = path.remove_last();
203 if (semi[label_arr[anc[u]]] < semi[label_arr[u]])
204 label_arr[u] = label_arr[anc[u]];
205 anc[u] = anc[anc[u]];
206 }
207 }
208
214 {
215 struct Frame
216 {
217 Node *u;
219 };
220
222
223 // Initialize root
224 const long root_num = num_reachable++;
225 NODE_COUNTER(root_ptr) = root_num;
226 vertex[root_num] = root_ptr;
227 par[root_num] = -1;
228 semi[root_num] = root_num;
229 label_arr[root_num] = root_num;
230 anc[root_num] = -1;
231
233
234 while (not stack.is_empty())
235 {
236 if (Frame & top = stack.top(); top.it.has_curr())
237 {
238 Node *v = gptr->get_tgt_node(top.it.get_curr());
239 top.it.next_ne();
240
241 if (NODE_COUNTER(v) == -1)
242 {
243 const long v_num = num_reachable++;
244 NODE_COUNTER(v) = v_num;
245 vertex[v_num] = v;
246 par[v_num] = NODE_COUNTER(top.u);
247 semi[v_num] = v_num;
248 label_arr[v_num] = v_num;
249 anc[v_num] = -1;
250
251 stack.push(Frame{v, Out_Iterator<GT, SA>(v, sa)});
252 }
253 }
254 else
255 static_cast<void>(stack.pop());
256 }
257 }
258
267 [[nodiscard]] long eval(const long v)
268 {
269 if (anc[v] == -1)
270 return v;
271 compress(v);
272 return label_arr[v];
273 }
274
284 void do_compute(GT & g, Node *root)
285 {
286 gptr = &g;
287 root_ptr = root;
288 is_computed = false;
289 num_reachable = 0;
290
291 const size_t N = g.get_num_nodes();
292 if (N == 0)
293 {
294 is_computed = true;
295 return;
296 }
297
298 // Allocate internal arrays
299 vertex = Array<Node *>(N, nullptr);
300 par = Array<long>(N, -1L);
301 semi = Array<long>(N, -1L);
302 idom_arr = Array<long>(N, -1L);
303 anc = Array<long>(N, -1L);
304 label_arr = Array<long>(N, -1L);
306
307 // Mark all nodes as unvisited
308 gptr->for_each_node([](Node *p) { NODE_COUNTER(p) = -1; });
309
310 // Phase 1: DFS from root
311 dfs(root); // dfs updates num_reachable
312
313 // Build predecessor lists by iterating over all arcs.
314 // In_Iterator does not work on List_Digraph because arcs are
315 // only stored in the source node's adjacency list. A root reached
316 // alone may still have a loop, which the frontiers need.
317 pred = Array<DynList<long>>(static_cast<size_t>(num_reachable), DynList<long>());
318 for (typename GT::Arc_Iterator ait(g); ait.has_curr(); ait.next_ne())
319 {
320 auto arc = ait.get_curr();
321 if (not sa(arc))
322 continue;
323 long s = NODE_COUNTER(gptr->get_src_node(arc));
324 long t = NODE_COUNTER(gptr->get_tgt_node(arc));
325 if (s >= 0 and s < num_reachable and t >= 0 and t < num_reachable)
326 pred[t].append(s);
327 }
328
329 if (num_reachable <= 1)
330 {
331 if (num_reachable == 1)
332 idom_arr[0] = -1; // root has no dominator
333 is_computed = true;
334 return;
335 }
336
337 // Allocate buckets (one per reachable node)
338 Array<DynList<long>> bucket(static_cast<size_t>(num_reachable), DynList<long>());
339
340 // Phase 2: Semi-dominators (process in reverse DFS order)
341 for (long w = num_reachable - 1; w >= 1; --w)
342 {
343 // For each predecessor of w
344 for (auto pit = pred[w].get_it(); pit.has_curr(); pit.next_ne())
345 {
346 const long v_num = pit.get_curr();
347 const long u = eval(v_num);
348 if (semi[u] < semi[w])
349 semi[w] = semi[u];
350 }
351
352 bucket[semi[w]].append(w);
353 anc[w] = par[w]; // LINK(parent[w], w)
354
355 // Process bucket of parent[w]
356 while (not bucket[par[w]].is_empty())
357 {
358 const long v = bucket[par[w]].remove_first();
359 long u = eval(v);
360 idom_arr[v] = (semi[u] == semi[v]) ? par[w] : u;
361 }
362 }
363
364 // Phase 3: Resolve deferred idom assignments
365 for (long w = 1; w < num_reachable; ++w)
366 {
367 if (idom_arr[w] != semi[w])
369 }
370
371 idom_arr[0] = -1; // root has no dominator
372 is_computed = true;
373 }
374
384 {
385 ah_domain_error_if(root == nullptr)
386 << "Lengauer_Tarjan_Dominators: root cannot be null";
387
388 if (is_computed and gptr == &g and root_ptr == root)
389 return;
390
391 do_compute(g, root);
392 }
393
394 public:
402 : sa(std::move(__sa))
403 { /* empty */
404 }
405
408
410
413
415
428 {
430
432 for (long i = 0; i < num_reachable; ++i)
433 {
434 Node *idom_node = (idom_arr[i] >= 0) ? vertex[idom_arr[i]] : nullptr;
435 result.append(std::make_pair(vertex[i], idom_node));
436 }
437 return result;
438 }
439
452 void build_tree(GT & g, Node *root, GT & tree)
453 {
455
456 clear_graph(tree);
457
458 if (num_reachable == 0)
459 return;
460
461 // Create tree nodes and establish mapping
462 Array<Node *> tree_nodes(num_reachable, nullptr);
463 for (long i = 0; i < num_reachable; ++i)
464 {
465 auto tn = tree.insert_node(vertex[i]->get_info());
466 NODE_COOKIE(vertex[i]) = nullptr; // else it would compose with a former tree
467 GT::map_nodes(vertex[i], tn);
468 tree_nodes[i] = tn;
469 }
470
471 // Create arcs: idom(v) -> v
472 for (long i = 1; i < num_reachable; ++i)
473 if (idom_arr[i] >= 0)
474 tree.insert_arc(tree_nodes[idom_arr[i]], tree_nodes[i]);
475 }
476
486 [[nodiscard]] Node * get_idom(GT & g, Node *root, Node *node)
487 {
488 ah_domain_error_if(node == nullptr)
489 << "Lengauer_Tarjan_Dominators::get_idom: node cannot be null";
491
492 long v = NODE_COUNTER(node);
493 if (v < 0 or v >= num_reachable or vertex[v] != node)
494 return nullptr; // unreachable node
495
496 return (idom_arr[v] >= 0) ? vertex[idom_arr[v]] : nullptr;
497 }
498
510 [[nodiscard]] bool dominates(GT & g, Node *root, Node *d, Node *v)
511 {
513
514 if (d == v)
515 return true; // reflexive
516
517 long d_num = NODE_COUNTER(d);
518 long v_num = NODE_COUNTER(v);
519
520 if (d_num < 0 or d_num >= num_reachable or vertex[d_num] != d)
521 return false;
522 if (v_num < 0 or v_num >= num_reachable or vertex[v_num] != v)
523 return false;
524
525 // Walk up the idom chain from v
526 long curr = idom_arr[v_num];
527 while (curr != -1)
528 {
529 if (curr == d_num)
530 return true;
531 curr = idom_arr[curr];
532 }
533 return false;
534 }
535
554 {
556
558
559 // Initialize empty frontiers for all reachable nodes
560 for (long i = 0; i < num_reachable; ++i)
562
563 // For each node with 2+ predecessors, walk up from each pred. A node
564 // with one predecessor p has p as idom, and gets in no frontier
565 // through it, unless it is the root, which has no idom: the walk
566 // from p goes up to the root.
567 for (long b = 0; b < num_reachable; ++b)
568 {
569 if (pred[b].size() < (b == 0 ? 1U : 2U))
570 continue;
571
572 Node *bnode = vertex[b];
573
574 // Walk up from each predecessor to idom(b)
575 for (auto it = pred[b].get_it(); it.has_curr(); it.next_ne())
576 {
577 long runner = it.get_curr();
578 while (runner != idom_arr[b] and runner != -1)
579 {
580 df.find(vertex[runner]).insert(bnode);
581 runner = idom_arr[runner];
582 }
583 }
584 }
585
586 return df;
587 }
588
595 void operator()(GT & g, Node *root, GT & tree)
596 {
597 build_tree(g, root, tree);
598 }
599
602
604 [[nodiscard]] SA &get_filter() noexcept { return sa; }
605
607 [[nodiscard]] const SA &get_filter() const noexcept { return sa; }
608
610 [[nodiscard]] bool has_computation() const noexcept { return is_computed; }
611
614 void invalidate() noexcept
615 {
616 is_computed = false;
617 gptr = nullptr;
618 root_ptr = nullptr;
619 }
620
622 [[nodiscard]] GT * get_graph() const noexcept { return gptr; }
623
625 [[nodiscard]] Node * get_root() const noexcept { return root_ptr; }
626
628 [[nodiscard]] long get_num_reachable() const noexcept
629 {
630 return num_reachable;
631 }
632
634 };
635
636
637 // =========================================================================
638 // Free functions
639 // =========================================================================
640
655 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
657 compute_dominators(GT & g, typename GT::Node *root, SA sa = SA())
658 {
660 }
661
677 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
679 GT & tree, SA sa = SA())
680 {
682 }
683
698 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
701 SA sa = SA())
702 {
704 }
705
706 // =========================================================================
707 // Post-dominator computation (Lengauer-Tarjan on the reversed graph)
708 // =========================================================================
709
747 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
749 {
750 SA sa;
751
752 using Node = typename GT::Node;
753 using Arc = typename GT::Arc;
754
757
758 GT *gptr = nullptr;
759 Node *exit_ptr = nullptr;
760 bool is_computed = false;
761
762 // Saved node mappings (orig ↔ rev) to avoid NODE_COOKIE conflicts
765
767 [[nodiscard]] Node * to_rev(Node *orig) const
768 {
769 auto *p = orig_to_rev.search(orig);
770 return p ? p->second : nullptr;
771 }
772
774 [[nodiscard]] Node * to_orig(Node *rev_n) const
775 {
776 auto *p = rev_to_orig.search(rev_n);
777 return p ? p->second : nullptr;
778 }
779
794 void ensure_computed(const GT & g, Node *exit_node)
795 {
796 ah_domain_error_if(exit_node == nullptr)
797 << "Lengauer_Tarjan_Post_Dominators: exit node cannot be null";
798
799 gptr = &const_cast<GT &>(g);
800 exit_ptr = exit_node;
801 is_computed = false;
802
803 // Step 1: Invert the digraph (sets NODE_COOKIE for mapping)
804 rev = invert_digraph(g);
805
806 // Step 2: Save orig↔rev mappings before dom overwrites anything
809 for (auto it = g.get_node_it(); it.has_curr(); it.next_ne())
810 {
811 auto orig = it.get_curr();
812 auto rev_n = mapped_node<GT>(orig);
813 orig_to_rev.insert(orig, rev_n);
814 rev_to_orig.insert(rev_n, orig);
815 }
816
817 // Step 3: Run forward dominators on reversed graph from exit
818 // (discard return value; we only need the cached state)
819 auto rev_exit = to_rev(exit_node);
820 (void) dom.compute_idom(rev, rev_exit);
821
822 is_computed = true;
823 }
824
825 public:
833 : sa(std::move(__sa)), dom(sa)
834 { /* empty */
835 }
836
839
842
845
848
860 compute_ipdom(const GT & g, Node *exit_node)
861 {
862 ensure_computed(g, exit_node);
863
864 auto rev_exit = to_rev(exit_node);
865 auto rev_idoms = dom.compute_idom(rev, rev_exit);
866
868 for (auto it = rev_idoms.get_it(); it.has_curr(); it.next_ne())
869 {
870 auto [rev_n, rev_id] = it.get_curr();
871 auto orig_n = to_orig(rev_n);
872 auto orig_id = rev_id ? to_orig(rev_id) : nullptr;
873 result.append(std::make_pair(orig_n, orig_id));
874 }
875 return result;
876 }
877
890 void build_tree(const GT & g, Node *exit_node, GT & tree)
891 {
892 ensure_computed(g, exit_node);
893
894 clear_graph(tree);
895
896 auto rev_exit = to_rev(exit_node);
897 auto rev_idoms = dom.compute_idom(rev, rev_exit);
898
899 if (rev_idoms.is_empty())
900 return;
901
902 // Create tree nodes with mapping to original graph.
903 // NODE_COOKIE(orig) may still point to a reversed-graph node
904 // left by invert_digraph; clear it so that map_nodes creates
905 // a clean orig↔tree bidirectional mapping.
906 DynMapTree<Node *, Node *> orig_to_tree;
907 for (auto it = rev_idoms.get_it(); it.has_curr(); it.next_ne())
908 {
909 auto [rev_n, rev_id] = it.get_curr();
910 auto orig = to_orig(rev_n);
911 auto tn = tree.insert_node(orig->get_info());
912 NODE_COOKIE(orig) = nullptr;
913 GT::map_nodes(orig, tn);
914 orig_to_tree.insert(orig, tn);
915 }
916
917 // Create arcs: ipdom(v) -> v
918 for (auto it = rev_idoms.get_it(); it.has_curr(); it.next_ne())
919 {
920 auto [rev_n, rev_id] = it.get_curr();
921 if (rev_id == nullptr)
922 continue; // exit node has no ipdom
923 auto orig = to_orig(rev_n);
924 auto orig_id = to_orig(rev_id);
925 tree.insert_arc(orig_to_tree.find(orig_id),
926 orig_to_tree.find(orig));
927 }
928 }
929
939 [[nodiscard]] Node * get_ipdom(const GT & g, Node *exit_node, Node *node)
940 {
941 ah_domain_error_if(node == nullptr)
942 << "Lengauer_Tarjan_Post_Dominators::get_ipdom: node cannot be null";
943 ensure_computed(g, exit_node);
944
945 auto rev_n = to_rev(node);
946 if (rev_n == nullptr)
947 return nullptr; // node not in graph
948
949 auto rev_exit = to_rev(exit_node);
950 auto rev_idom = dom.get_idom(rev, rev_exit, rev_n);
951 return rev_idom ? to_orig(rev_idom) : nullptr;
952 }
953
965 [[nodiscard]] bool
966 post_dominates(const GT & g, Node *exit_node, Node *d, Node *v)
967 {
968 ensure_computed(g, exit_node);
969
970 if (d == v)
971 return true; // reflexive
972
973 auto rev_d = to_rev(d);
974 auto rev_v = to_rev(v);
975 if (rev_d == nullptr or rev_v == nullptr)
976 return false;
977
978 auto rev_exit = to_rev(exit_node);
979 return dom.dominates(rev, rev_exit, rev_d, rev_v);
980 }
981
995 post_dominance_frontiers(const GT & g, Node *exit_node)
996 {
997 ensure_computed(g, exit_node);
998
999 auto rev_exit = to_rev(exit_node);
1000 auto rev_df = dom.dominance_frontiers(rev, rev_exit);
1001
1003 for (auto it = rev_df.get_it(); it.has_curr(); it.next_ne())
1004 {
1005 auto & [rev_n, rev_frontier] = it.get_curr();
1006 auto orig = to_orig(rev_n);
1007 DynSetTree<Node *> orig_frontier;
1008 for (typename DynSetTree<Node *>::Iterator fi(rev_frontier); fi.has_curr(); fi.next_ne())
1009 orig_frontier.insert(to_orig(fi.get_curr()));
1010 result.insert(orig, std::move(orig_frontier));
1011 }
1012 return result;
1013 }
1014
1021 void operator()(const GT & g, Node *exit_node, GT & tree)
1022 {
1023 build_tree(g, exit_node, tree);
1024 }
1025
1028
1030 [[nodiscard]] SA &get_filter() noexcept { return sa; }
1031
1033 [[nodiscard]] const SA &get_filter() const noexcept { return sa; }
1034
1036 [[nodiscard]] bool has_computation() const noexcept { return is_computed; }
1037
1039 [[nodiscard]] GT * get_graph() const noexcept { return gptr; }
1040
1042 [[nodiscard]] Node * get_exit() const noexcept { return exit_ptr; }
1043
1045 };
1046
1047
1048 // =========================================================================
1049 // Post-dominator free functions
1050 // =========================================================================
1051
1066 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
1068 compute_post_dominators(const GT & g, typename GT::Node *exit_node,
1069 SA sa = SA())
1070 {
1072 .compute_ipdom(g, exit_node);
1073 }
1074
1090 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
1091 void build_post_dominator_tree(const GT & g, typename GT::Node *exit_node,
1092 GT & tree, SA sa = SA())
1093 {
1095 .build_tree(g, exit_node, tree);
1096 }
1097
1112 template <AlephGraph GT, ArcFilter<GT> SA = Dft_Show_Arc<GT>>
1115 typename GT::Node *exit_node,
1116 SA sa = SA())
1117 {
1119 .post_dominance_frontiers(g, exit_node);
1120 }
1121} // end namespace Aleph
1122
1123# endif // DOMINATORS_H
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
C++20 concepts for the protocol shared by graph algorithms.
EepicNode< long > * build_tree()
Definition btreepic.C:1434
long double w
Definition btreepic.C:153
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
constexpr bool is_empty() const noexcept
Checks if the container is empty.
Definition tpl_array.H:444
T & insert(const T &data)
insert a copy of data at the beginning of the array.
Definition tpl_array.H:368
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
Dynamic stack of elements of generic type T based on a singly linked list.
T & top()
Return a modifiable reference to the top item of the stack.
bool is_empty() const noexcept
Check if the stack is empty.
T pop()
Remove and return the top item of the stack.
T & push(const T &data)
Push an item by copy onto the top of the stack.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
Generic key-value map implemented on top of a binary search tree.
Pair * search(const Key &key) noexcept(nothrow_search)
Collect all keys.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
Data & find(const Key &key)
Find the value associated with key.
Dynamic set backed by balanced binary search trees with automatic memory management.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
Computes dominator tree and dominance frontiers of a digraph using the Lengauer-Tarjan algorithm.
Definition Dominators.H:156
Lengauer_Tarjan_Dominators(const Lengauer_Tarjan_Dominators &)=delete
Dominator instances should not be copied (they hold internal state)
Lengauer_Tarjan_Dominators(Lengauer_Tarjan_Dominators &&)=default
Move is allowed.
DynMapTree< Node *, DynSetTree< Node * > > dominance_frontiers(GT &g, Node *root)
Compute dominance frontiers for all reachable nodes.
Definition Dominators.H:553
void build_tree(GT &g, Node *root, GT &tree)
Build the dominator tree as a separate graph.
Definition Dominators.H:452
bool has_computation() const noexcept
Check if a computation has been performed.
Definition Dominators.H:610
void ensure_computed(GT &g, Node *root)
Ensure computation is up to date.
Definition Dominators.H:383
SA & get_filter() noexcept
Get the arc filter.
Definition Dominators.H:604
Lengauer_Tarjan_Dominators & operator=(Lengauer_Tarjan_Dominators &&)=default
Lengauer_Tarjan_Dominators & operator=(const Lengauer_Tarjan_Dominators &)=delete
void dfs(Node *root_ptr)
Iterative DFS to number nodes and build DFS tree.
Definition Dominators.H:213
void operator()(GT &g, Node *root, GT &tree)
Build dominator tree (operator() alias).
Definition Dominators.H:595
long get_num_reachable() const noexcept
Get the number of reachable nodes in the last computation.
Definition Dominators.H:628
long eval(const long v)
EVAL operation for the Union-Find forest.
Definition Dominators.H:267
bool dominates(GT &g, Node *root, Node *d, Node *v)
Test whether node d dominates node v.
Definition Dominators.H:510
void invalidate() noexcept
Drop the cached computation, so that the next query computes the dominators again.
Definition Dominators.H:614
Lengauer_Tarjan_Dominators(SA __sa=SA())
Construct a dominator computation instance.
Definition Dominators.H:401
Array< DynList< long > > pred
Definition Dominators.H:174
Node * get_idom(GT &g, Node *root, Node *node)
Get the immediate dominator of a specific node.
Definition Dominators.H:486
void do_compute(GT &g, Node *root)
Core computation of dominators.
Definition Dominators.H:284
DynList< std::pair< Node *, Node * > > compute_idom(GT &g, Node *root)
Compute immediate dominators.
Definition Dominators.H:427
Node * get_root() const noexcept
Get the root of the last computation.
Definition Dominators.H:625
const SA & get_filter() const noexcept
Get the arc filter (const).
Definition Dominators.H:607
GT * get_graph() const noexcept
Get the graph of the last computation.
Definition Dominators.H:622
void compress(long v)
Path compression for Union-Find (iterative).
Definition Dominators.H:184
Computes post-dominator tree and post-dominance frontiers of a digraph using the Lengauer-Tarjan algo...
Definition Dominators.H:749
bool has_computation() const noexcept
Check if a computation has been performed.
Lengauer_Tarjan_Post_Dominators(const Lengauer_Tarjan_Post_Dominators &)=delete
Post-dominator instances should not be copied (they hold internal state)
SA & get_filter() noexcept
Get the arc filter.
DynMapTree< Node *, DynSetTree< Node * > > post_dominance_frontiers(const GT &g, Node *exit_node)
Compute post-dominance frontiers for all reachable nodes.
Definition Dominators.H:995
void build_tree(const GT &g, Node *exit_node, GT &tree)
Build the post-dominator tree as a separate graph.
Definition Dominators.H:890
void ensure_computed(const GT &g, Node *exit_node)
Ensure computation is up to date.
Definition Dominators.H:794
Node * exit_ptr
Definition Dominators.H:759
Lengauer_Tarjan_Post_Dominators & operator=(const Lengauer_Tarjan_Post_Dominators &)=delete
Lengauer_Tarjan_Post_Dominators & operator=(Lengauer_Tarjan_Post_Dominators &&)=default
const SA & get_filter() const noexcept
Get the arc filter (const).
void operator()(const GT &g, Node *exit_node, GT &tree)
Build post-dominator tree (operator() alias).
Lengauer_Tarjan_Post_Dominators(SA __sa=SA())
Construct a post-dominator computation instance.
Definition Dominators.H:832
GT rev
Definition Dominators.H:755
Lengauer_Tarjan_Dominators< GT, SA > dom
Definition Dominators.H:756
bool is_computed
Definition Dominators.H:760
typename GT::Node Node
Definition Dominators.H:752
SA sa
Definition Dominators.H:750
typename GT::Arc Arc
Definition Dominators.H:753
GT * get_graph() const noexcept
Get the graph of the last computation.
DynList< std::pair< Node *, Node * > > compute_ipdom(const GT &g, Node *exit_node)
Compute immediate post-dominators.
Definition Dominators.H:860
Lengauer_Tarjan_Post_Dominators(Lengauer_Tarjan_Post_Dominators &&)=default
Move is allowed.
GT * gptr
Definition Dominators.H:758
DynMapTree< Node *, Node * > rev_to_orig
Definition Dominators.H:764
Node * to_rev(Node *orig) const
Map original node to reversed graph node.
Definition Dominators.H:767
bool post_dominates(const GT &g, Node *exit_node, Node *d, Node *v)
Test whether node d post-dominates node v.
Definition Dominators.H:966
Node * get_exit() const noexcept
Get the exit node of the last computation.
Node * get_ipdom(const GT &g, Node *exit_node, Node *node)
Get the immediate post-dominator of a specific node.
Definition Dominators.H:939
Node * to_orig(Node *rev_n) const
Map reversed graph node to original node.
Definition Dominators.H:774
DynMapTree< Node *, Node * > orig_to_rev
Definition Dominators.H:763
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
Filtered iterator for outcoming arcs of a node.
Definition tpl_graph.H:1942
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:975
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:1017
void for_each_node(Operation &operation) const
Unconditionally traverse all the nodes of graph and on each one perform an operation.
Definition graph-dry.H:1765
auto get_node_it() const noexcept
Obtains an iterator to the nodes of graph.
Definition graph-dry.H:3122
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:1023
static void map_nodes(N1 *p, N2 *q) noexcept
Map the nodes through their cookies.
Definition graph-dry.H:1281
#define N
Definition fib.C:294
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Definition gmpfrxx.h:4138
void build_post_dominator_tree(const GT &g, typename GT::Node *exit_node, GT &tree, SA sa=SA())
Build the post-dominator tree of a digraph.
#define NODE_COUNTER(p)
Get the counter of a node.
GT invert_digraph(const GT &g)
Compute the transpose (arc-reversed) digraph.
void build_dominator_tree(GT &g, typename GT::Node *root, GT &tree, SA sa=SA())
Build the dominator tree of a digraph.
Definition Dominators.H:678
#define NODE_COOKIE(p)
Return the node cookie
DynList< std::pair< typename GT::Node *, typename GT::Node * > > compute_dominators(GT &g, typename GT::Node *root, SA sa=SA())
Compute immediate dominators of a digraph from a root node.
Definition Dominators.H:657
DynList< std::pair< typename GT::Node *, typename GT::Node * > > compute_post_dominators(const GT &g, typename GT::Node *exit_node, SA sa=SA())
Compute immediate post-dominators of a digraph from an exit node.
void clear_graph(GT &g)
Clean a graph: all its nodes and arcs are removed and freed.
Definition tpl_graph.H:4776
DynMapTree< typename GT::Node *, DynSetTree< typename GT::Node * > > compute_post_dominance_frontiers(const GT &g, typename GT::Node *exit_node, SA sa=SA())
Compute post-dominance frontiers of a digraph.
DynMapTree< typename GT::Node *, DynSetTree< typename GT::Node * > > compute_dominance_frontiers(GT &g, typename GT::Node *root, SA sa=SA())
Compute dominance frontiers of a digraph.
Definition Dominators.H:700
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
and
Check uniqueness with explicit hash + equality functors.
static long & df(typename GT::Node *p)
Internal helper: DFS discovery time stored in NODE_COUNTER(p).
STL namespace.
Dynamic array container with automatic resizing.
Dynamic stack implementation based on linked lists.
Dynamic set implementations based on balanced binary search trees.
Generic graph and digraph implementations.
Utility algorithms and operations for graphs.