Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_indexGraph.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
67# ifndef TPL_INDEXGRAPH
68# define TPL_INDEXGRAPH
69
70# include <tpl_indexNode.H>
71# include <tpl_indexArc.H>
72
73# include <ah-errors.H>
74
75namespace Aleph
76{
77
116template <class GT, class Compare = Dft_Node_Cmp<GT>,
117 template <class /* Key */, class /* Compare */> class Tree = Treap>
119{
120private:
121
122 typedef typename GT::Arc GT_Arc;
123 typedef typename GT::Node GT_Node;
124 typedef typename GT::Arc_Type GT_Arc_Type;
125 typedef typename GT::Node_Type GT_Node_Type;
126
127 GT & g;
130
131public:
132
134 Index_Graph(GT & __g) : g(__g), idx_node(__g), idx_arc(__g)
135 {
136 // empty
137 }
138
146 {
147 return idx_node.insert_in_graph(info);
148 }
149
161 const GT_Arc_Type & info = GT_Arc_Type())
162 {
163 ah_domain_error_if(idx_node.search(src) == nullptr)
164 << "src node not in index";
165
166 ah_domain_error_if(idx_node.search(tgt) == nullptr)
167 << "tgt node not in index";
168
169 return idx_arc.insert_in_graph(src, tgt, info);
170 }
171
179 {
180 return idx_node.search(p);
181 }
182
193 {
194 return idx_node.search(info);
195 }
196
205 {
206 return idx_arc.search(src, tgt);
207 }
208
214 {
215 // remove from the index the arcs that the removal of the node from
216 // the graph frees: those of its list and, in a digraph, whose lists
217 // hold the arcs out, those that come in from other nodes
218 for (typename GT::Node_Arc_Iterator it(p); it.has_curr(); it.next_ne())
219 idx_arc.remove(it.get_curr());
220 if (g.is_digraph())
221 for (typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
222 if (auto a = it.get_curr(); g.get_tgt_node(a) == p and g.get_src_node(a) != p)
223 idx_arc.remove(a);
224
225 return idx_node.remove_from_graph(p);
226 }
227
230 {
231 return idx_arc.remove_from_graph(a);
232 }
233
235 size_t get_num_arcs() const { return idx_arc.size(); }
236
238 size_t get_num_nodes() const { return idx_node.size(); }
239};
240
241 template <AlephGraph GT> inline
242 bool are_equal(const GT & g1, const GT & g2)
243 {
244 if (g1.vsize() != g2.vsize() or g1.esize() != g2.esize())
245 return false;
246
247 {
248 IndexNode<GT> t2(const_cast<GT&>(g2));
249 if (not g1.all_nodes([&t2] (auto p)
250 {
251 auto q = t2.search(p);
252 if (q == nullptr)
253 return false;
254 GT::map_nodes(p, q);
255 return true;
256 }))
257 return false;
258 }
259
260 IndexArc<GT> t2(const_cast<GT&>(g2));
261 return g1.all_arcs([idx = &t2, &g1] (auto a)
262 {
263 auto s1 = g1.get_src_node(a);
264 auto t1 = g1.get_tgt_node(a);
265 auto s2 = mapped_node<GT>(s1);
266 auto t2 = mapped_node<GT>(t1);
267 auto a2 = idx->search(s2, t2);
268 if (a2 == nullptr)
269 return false;
270 return a2->get_info() == a->get_info();
271 });
272 }
273
274}
275
276# endif // TPL_INDEXGRAPH
277
278
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
Index for fast arc lookup by its endpoint nodes.
Builds an index of nodes for fast search and retrieval.
Builds node and arc indices for fast lookup and retrieval.
size_t get_num_arcs() const
Returns the number of arcs the index contains.
GT_Node * search_node(GT_Node *p)
Looks up a node in the index.
GT_Node * search_node(const GT_Node_Type &info)
Looks up a node in the index.
IndexArc< GT, Tree > idx_arc
GT_Arc * search_arc(GT_Node *src, GT_Node *tgt)
Looks up an arc in the index given its two nodes.
void remove_node(GT_Node *p)
Removes node p from the graph and from the index.
GT::Arc_Type GT_Arc_Type
size_t get_num_nodes() const
Returns the number of nodes the index contains.
GT_Node * insert_node(const GT_Node_Type &info)
Creates a new node and inserts it into the graph and the index.
IndexNode< GT, Compare, Tree > idx_node
void remove_arc(GT_Arc *a)
Removes arc a from the graph and from the index.
GT_Arc * insert_arc(GT_Node *src, GT_Node *tgt, const GT_Arc_Type &info=GT_Arc_Type())
Creates a new arc between two nodes and inserts it into the graph and the index.
GT::Node_Type GT_Node_Type
Index_Graph(GT &__g)
Creates an index of the graph: nodes and arcs are indexed.
typename Node::Node_Type Node_Type
The arc class type.
Definition tpl_graph.H:447
typename Arc::Arc_Type Arc_Type
The type of data stored in the arc.
Definition tpl_graph.H:450
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:1017
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:937
bool all_arcs(Operation &op) const
Check if all the arcs of graph satisfy a boolean condition.
Definition graph-dry.H:1943
bool all_nodes(Operation &op) const
Check if all the nodes of graph satisfy an boolean condition.
Definition graph-dry.H:1899
constexpr size_t vsize() const noexcept
See get_num_nodes().
Definition graph-dry.H:984
size_t esize() const noexcept
Return the total of arcs of graph.
Definition graph-dry.H:1078
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:1023
QuadTree - Hierarchical spatial index for 2D points.
Definition quadtree.H:126
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
bool are_equal(const GT &g1, const GT &g2)
Fast graph comparison.
Treap (a special type of randomized binary search tree) using nodes without virtual destructor.
Definition tpl_treap.H:797
Arc indexing for fast lookup by endpoint nodes.
Node indexing for fast O(log n) lookup by key.