Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_graph_indexes.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
39# ifndef TPL_GRAPH_INDEXES_H
40# define TPL_GRAPH_INDEXES_H
41
42# include <tpl_dynSetTree.H>
43# include <tpl_graph.H>
44
45using namespace Aleph;
46
47namespace Aleph
48{
49
57template <class GT>
59{
67 bool operator () (typename GT::Node * p1, typename GT::Node * p2) const
68 {
69 return p1->get_info() < p2->get_info();
70 }
71};
72
80template <class GT>
82{
91 bool operator () (typename GT::Arc * a1, typename GT::Arc * a2) const
92 {
93 if (a1->src_node < a2->src_node)
94 return true;
95
96 return not (a2->src_node < a1->src_node) and a1->tgt_node < a2->tgt_node;
97 }
98};
99
116template <class GT, class Compare = Dft_Node_Cmp<GT>,
117 template <typename, class> class Tree = Treap,
118 class SN = Dft_Show_Node<GT>>
119class Nodes_Index : public DynSetTree<typename GT::Node *, Tree, Compare>
120{
124 typedef typename GT::Node GT_Node;
125
129 typedef typename GT::Node_Type GT_Node_Info;
130
134 typedef DynSetTree <GT_Node *, Tree, Compare> Tree_Type;
135
139 GT & g;
140
145 void init(SN & sn)
146 {
147 for (Node_Iterator<GT, SN> it(g, sn); it.has_curr(); it.next_ne())
148 this->insert(it.get_curr());
149 }
150
151public:
158 Nodes_Index(GT & _g, Compare & cmp, SN & _sn)
159 : Tree_Type(cmp), g(_g)
160 {
161 init(_sn);
162 }
163
170 Nodes_Index(GT & _g, Compare && cmp = Compare(), SN && _sn = SN())
171 : Tree_Type(cmp), g(_g)
172 {
173 init(_sn);
174 }
175
183 {
184 g.insert_node(p);
185
186 if (this->insert(p) == nullptr)
187 {
188 g.remove_node(p);
189 return nullptr;
190 }
191
192 return p;
193 }
194
202 {
203 g.insert_node(p);
204 GT_Node ** ptr_node = this->search_or_insert(p);
205 GT_Node * q = *ptr_node;
206
207 if (p != q)
208 g.remove_node(p);
209
210 return q;
211 }
212
221 {
222 GT_Node * p = g.insert_node(info);
223
224 if (this->insert(p) == nullptr)
225 {
226 g.remove_node(p);
227 return nullptr;
228 }
229
230 return p;
231 }
232
241 {
242 GT_Node * p = g.insert_node(info);
243 GT_Node ** ptr_node = this->search_or_insert(p);
244 GT_Node * q = *ptr_node;
245
246 if (p != q)
247 g.remove_node(p);
248
249 return q;
250 }
251
260 {
261 return insert_in_graph(info);
262 }
263
271 {
272 GT_Node ** ret_val = Tree_Type::search(p);
273
274 if (ret_val == nullptr)
275 return nullptr;
276
277 return *ret_val;
278 }
279
287 {
288 GT_Node dummy_node(info);
289 GT_Node * dummy_node_ptr = &dummy_node;
290
291 return search(dummy_node_ptr);
292 }
293
301 {
302 Tree_Type::find(p); // throws exception if p is not in the index
304 g.remove_node(p);
305 }
306};
307
326template <class GT, class Compare = Dft_Arc_Cmp<GT>,
327 template <typename, class> class Tree = Treap,
328 class SA = Dft_Show_Arc<GT>>
329class Arcs_Index : public DynSetTree<typename GT::Arc *, Tree, Compare>
330{
334 typedef typename GT::Node GT_Node;
335
339 typedef typename GT::Node_Type GT_Node_Info;
340
344 typedef typename GT::Arc GT_Arc;
345
349 typedef typename GT::Arc_Type GT_Arc_Info;
350
354 typedef DynSetTree <typename GT::Arc *, Tree, Compare> Tree_Type;
355
359 GT & g;
360
365 void init(SA & sa)
366 {
367 for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next_ne())
368 this->insert(it.get_curr());
369 }
370
371public:
379 Arcs_Index(GT & _g, Compare & cmp, SA & _sa)
380 : Tree_Type(cmp), g(_g)
381 {
382 init(_sa);
383 }
384
392 Arcs_Index(GT & _g, Compare && cmp = Compare(), SA && _sa = SA())
393 : Tree_Type(cmp), g(_g)
394 {
395 init(_sa);
396 }
397
408 const GT_Arc_Info & info)
409 {
410 GT_Arc * a = g.insert_arc(src, tgt, info);
411
412 if (this->insert(a) == nullptr)
413 {
414 g.remove_arc(a);
415 return nullptr;
416 }
417
418 return a;
419 }
420
431 const GT_Arc_Info && info = GT_Arc_Info())
432 {
433 return insert_in_graph(src, tgt, info);
434 }
435
444 GT_Arc * search(GT_Node * src, GT_Node * tgt, const GT_Arc_Info & info)
445 {
446 // Create persistent key for search (avoid stack allocation)
447 auto * key = new GT_Arc(info);
448 key->src_node = src;
449 key->tgt_node = tgt;
450
451 GT_Arc ** ret_val = Tree_Type::search(key);
452
453 if (ret_val != nullptr)
454 {
455 GT_Arc* found = *ret_val;
456 delete key;
457 return found;
458 }
459
460 if (g.is_digraph())
461 {
462 delete key;
463 return nullptr;
464 }
465
466 // Try reverse direction for undirected graphs
467 std::swap(key->src_node, key->tgt_node);
468 ret_val = Tree_Type::search(key);
469
470 if (ret_val == nullptr)
471 {
472 delete key;
473 return nullptr;
474 }
475
476 // Validate arc direction consistency
477 GT_Arc* found = *ret_val;
478 assert(((src == found->src_node) && (tgt == found->tgt_node)) ||
479 ((tgt == found->src_node) && (src == found->tgt_node)));
480
481 delete key;
482 return found;
483 }
484
493 GT_Arc * search(GT_Node * src, GT_Node * tgt,
494 const GT_Arc_Info && info = GT_Arc_Info())
495 {
496 return search(src, tgt, info);
497 }
498
506 {
509 g.remove_arc(a);
510 }
511
512};
513
514} // End namespace Aleph
515
516# endif // GRAPH_INDEXES_H
Builds an arc index for fast lookup by source/target nodes.
GT::Node_Type GT_Node_Info
Typedef for the graph's node info type.
DynSetTree< typename GT::Arc *, Tree, Compare > Tree_Type
Typedef for the internal tree type.
GT_Arc * insert_in_graph(GT_Node *src, GT_Node *tgt, const GT_Arc_Info &&info=GT_Arc_Info())
Inserts an arc into the graph and index (rvalue overload).
GT_Arc * search(GT_Node *src, GT_Node *tgt, const GT_Arc_Info &info)
Searches for an arc by its source, target, and content.
GT_Arc * insert_in_graph(GT_Node *src, GT_Node *tgt, const GT_Arc_Info &info)
Inserts an arc into the graph and index.
GT::Arc_Type GT_Arc_Info
Typedef for the graph's arc info type.
GT & g
Reference to the graph being indexed.
GT::Arc GT_Arc
Typedef for the graph's arc type.
GT::Node GT_Node
Typedef for the graph's node type.
GT_Arc * search(GT_Node *src, GT_Node *tgt, const GT_Arc_Info &&info=GT_Arc_Info())
Searches for an arc by its source, target, and content (rvalue overload).
void init(SA &sa)
Initializes the index by inserting the arcs of the graph that sa shows.
void remove_from_graph(GT_Arc *a)
Removes an arc from the graph and index.
Arcs_Index(GT &_g, Compare &cmp, SA &_sa)
Constructor.
Arcs_Index(GT &_g, Compare &&cmp=Compare(), SA &&_sa=SA())
Constructor with default comparison and filter objects.
Dynamic set backed by balanced binary search trees with automatic memory management.
Key & find(const Key &key)
Returns a modifiable reference to an element within the set.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
size_t remove(const Key &key)
Removes a key from the dynamic set.
Key * search(const Key &key)
Find an element in the set.
Key * search_or_insert(const Key &key)
Look for the key in the binary search tree or inserts it if it is not found.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
typename Node::Node_Type Node_Type
The arc class type.
Definition tpl_graph.H:447
virtual void remove_arc(Arc *arc) noexcept
Remove an arc from the graph and free it.
Definition tpl_graph.H:673
virtual void remove_node(Node *node)
Remove a node from the graph and free its memory.
Definition tpl_graph.H:557
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
typename Arc::Arc_Type Arc_Type
The type of data stored in the arc.
Definition tpl_graph.H:450
Filtered iterator on the nodes of a graph.
Definition tpl_graph.H:1287
Builds a node index for fast lookup and retrieval.
GT & g
Reference to the graph being indexed.
Nodes_Index(GT &_g, Compare &&cmp=Compare(), SN &&_sn=SN())
Constructor with rvalue references for comparison and filter.
GT::Node_Type GT_Node_Info
Typedef for the graph's node info type.
GT::Node GT_Node
Typedef for the graph's node type.
GT_Node * search_or_insert_in_graph(GT_Node *p)
Searches for a node in the index and graph, inserting it if not found.
void remove_from_graph(GT_Node *p)
Removes a node from the graph and index.
GT_Node * insert_in_graph(const GT_Node_Info &&info=GT_Node_Info())
Inserts a node with generic content into the graph and index (rvalue overload).
GT_Node * search(const GT_Node_Info &info)
Searches for a node by its content (info overload).
GT_Node * insert_in_graph(GT_Node *p)
Inserts a node into the graph and index.
Nodes_Index(GT &_g, Compare &cmp, SN &_sn)
Constructor that initializes the index with existing graph nodes.
GT_Node * search(GT_Node *p)
Searches for a node by its content.
DynSetTree< GT_Node *, Tree, Compare > Tree_Type
Typedef for the internal tree type.
GT_Node * search_or_insert_in_graph(const GT_Node_Info &info)
Searches for a node with generic content in the index and graph, inserting it if not found.
void init(SN &sn)
Initialize the index by inserting the graph nodes that sn shows.
GT_Node * insert_in_graph(const GT_Node_Info &info)
Inserts a node with generic content into the graph and index.
void * tgt_node
Please don't use.
Definition graph-dry.H:793
void * src_node
Definition graph-dry.H:792
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:753
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:937
QuadTree - Hierarchical spatial index for 2D points.
Definition quadtree.H:126
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4196
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
static std::atomic< bool > init
Definition hash-fct.C:54
Filtered iterator on all the arcs of a graph.
Definition tpl_graph.H:1200
Default arc comparison class for Arcs_Index.
bool operator()(typename GT::Arc *a1, typename GT::Arc *a2) const
Comparison operator.
Default node comparison class for Nodes_Index.
bool operator()(typename GT::Node *p1, typename GT::Node *p2) const
Comparison operator.
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1024
Default filter for the graph nodes.
Definition tpl_graph.H:1228
Treap (a special type of randomized binary search tree) using nodes without virtual destructor.
Definition tpl_treap.H:797
Dynamic set implementations based on balanced binary search trees.
Generic graph and digraph implementations.