Aleph-w 3.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
129
130public:
131
134 {
135 // empty
136 }
137
145 {
146 return idx_node.insert_in_graph(info);
147 }
148
160 const GT_Arc_Type & info = GT_Arc_Type())
161 {
162 ah_domain_error_if(idx_node.search(src) == nullptr)
163 << "src node not in index";
164
165 ah_domain_error_if(idx_node.search(tgt) == nullptr)
166 << "tgt node not in index";
167
168 return idx_arc.insert_in_graph(src, tgt, info);
169 }
170
178 {
179 return idx_node.search(p);
180 }
181
192 {
193 return idx_node.search(info);
194 }
195
204 {
205 return idx_arc.search(src, tgt);
206 }
207
213 {
214 // remove from the index the arcs emanating from the node (they
215 // will be removed by the node's removal from the graph)
216 for (typename GT::Node_Arc_Iterator it(p); it.has_curr(); it.next_ne())
217 idx_arc.remove(it.get_curr());
218
219 return idx_node.remove_from_graph(p);
220 }
221
224 {
225 return idx_arc.remove_from_graph(a);
226 }
227
229 size_t get_num_arcs() const { return idx_arc.size(); }
230
232 size_t get_num_nodes() const { return idx_node.size(); }
233};
234
235 template <AlephGraph GT> inline
236 bool are_equal(const GT & g1, const GT & g2)
237 {
238 if (g1.vsize() != g2.vsize() or g1.esize() != g2.esize())
239 return false;
240
241 {
242 IndexNode<GT> t2(const_cast<GT&>(g2));
243 if (not g1.all_nodes([&t2] (auto p)
244 {
245 auto q = t2.search(p);
246 if (q == nullptr)
247 return false;
248 GT::map_nodes(p, q);
249 return true;
250 }))
251 return false;
252 }
253
254 IndexArc<GT> t2(const_cast<GT&>(g2));
255 return g1.all_arcs([idx = &t2, &g1] (auto a)
256 {
257 auto s1 = g1.get_src_node(a);
258 auto t1 = g1.get_tgt_node(a);
259 auto s2 = mapped_node<GT>(s1);
260 auto t2 = mapped_node<GT>(t1);
261 auto a2 = idx->search(s2, t2);
262 if (a2 == nullptr)
263 return false;
264 return a2->get_info() == a->get_info();
265 });
266 }
267
268}
269
270# endif // TPL_INDEXGRAPH
271
272
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:527
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.
Index_Graph(GT &g)
Creates an index of the graph: nodes and arcs are indexed.
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
typename Node::Node_Type Node_Type
The arc class type.
Definition tpl_graph.H:437
typename Arc::Arc_Type Arc_Type
The type of data stored in the arc.
Definition tpl_graph.H:440
QuadTree - Hierarchical spatial index for 2D points.
Definition quadtree.H:126
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
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:614
Arc indexing for fast lookup by endpoint nodes.
Node indexing for fast O(log n) lookup by key.