Aleph-w 3.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
135
139 GT & g;
140
145
149 void init()
150 {
151 for (Node_Iterator<GT, SN> it(g, sn); it.has_curr(); it.next_ne())
152 this->insert(it.get_curr());
153 }
154
155public:
162 Nodes_Index(GT & _g, Compare & cmp, SN & _sn)
163 : Tree_Type(cmp), g(_g), sn(_sn)
164 {
165 init();
166 }
167
174 Nodes_Index(GT & _g, Compare && cmp = Compare(), SN && _sn = SN())
175 : Tree_Type(cmp), g(_g), sn(_sn)
176 {
177 init();
178 }
179
187 {
188 g.insert_node(p);
189
190 if (this->insert(p) == nullptr)
191 {
192 g.remove_node(p);
193 return nullptr;
194 }
195
196 return p;
197 }
198
206 {
207 g.insert_node(p);
208 GT_Node ** ptr_node = this->search_or_insert(p);
209 GT_Node * q = *ptr_node;
210
211 if (p != q)
212 g.remove_node(p);
213
214 return q;
215 }
216
225 {
226 GT_Node * p = g.insert_node(info);
227
228 if (this->insert(p) == nullptr)
229 {
230 g.remove_node(p);
231 return nullptr;
232 }
233
234 return p;
235 }
236
245 {
246 GT_Node * p = g.insert_node(info);
247 GT_Node ** ptr_node = this->search_or_insert(p);
248 GT_Node * q = *ptr_node;
249
250 if (p != q)
251 g.remove_node(p);
252
253 return q;
254 }
255
264 {
265 return insert_in_graph(info);
266 }
267
275 {
277
278 if (ret_val == nullptr)
279 return nullptr;
280
281 return *ret_val;
282 }
283
297
305 {
306 Tree_Type::find(p); // throws exception if p is not in the index
308 g.remove_node(p);
309 }
310};
311
330template <class GT, class Compare = Dft_Arc_Cmp<GT>,
331 template <typename, class> class Tree = Treap,
332 class SA = Dft_Show_Arc<GT>>
333class Arcs_Index : public DynSetTree<typename GT::Arc *, Tree, Compare>
334{
338 typedef typename GT::Node GT_Node;
339
343 typedef typename GT::Node_Type GT_Node_Info;
344
348 typedef typename GT::Arc GT_Arc;
349
353 typedef typename GT::Arc_Type GT_Arc_Info;
354
359
363 GT & g;
364
369
373 void init()
374 {
375 for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next_ne())
376 this->insert(it.get_curr());
377 }
378
379public:
387 Arcs_Index(GT & _g, Compare & cmp, SA & _sa)
388 : Tree_Type(cmp), g(_g), sa(_sa)
389 {
390 init();
391 }
392
400 Arcs_Index(GT & _g, Compare && cmp = Compare(), SA && _sa = SA())
401 : Tree_Type(cmp), g(_g), sa(_sa)
402 {
403 init();
404 }
405
416 const GT_Arc_Info & info)
417 {
418 GT_Arc * a = g.insert_arc(src, tgt, info);
419
420 if (this->insert(a) == nullptr)
421 {
422 g.remove_arc(a);
423 return nullptr;
424 }
425
426 return a;
427 }
428
439 const GT_Arc_Info && info = GT_Arc_Info())
440 {
441 return insert_in_graph(src, tgt, info);
442 }
443
452 GT_Arc * search(GT_Node * src, GT_Node * tgt, const GT_Arc_Info & info)
453 {
454 // Create persistent key for search (avoid stack allocation)
455 auto * key = new GT_Arc(info);
456 key->src_node = src;
457 key->tgt_node = tgt;
458
460
461 if (ret_val != nullptr)
462 {
463 GT_Arc* found = *ret_val;
464 delete key;
465 return found;
466 }
467
468 if (g.is_digraph())
469 {
470 delete key;
471 return nullptr;
472 }
473
474 // Try reverse direction for undirected graphs
475 std::swap(key->src_node, key->tgt_node);
477
478 if (ret_val == nullptr)
479 {
480 delete key;
481 return nullptr;
482 }
483
484 // Validate arc direction consistency
485 GT_Arc* found = *ret_val;
486 assert(((src == found->src_node) && (tgt == found->tgt_node)) ||
487 ((tgt == found->src_node) && (src == found->tgt_node)));
488
489 delete key;
490 return found;
491 }
492
501 GT_Arc * search(GT_Node * src, GT_Node * tgt,
502 const GT_Arc_Info && info = GT_Arc_Info())
503 {
504 return search(src, tgt, info);
505 }
506
514 {
517 g.remove_arc(a);
518 }
519
520};
521
522} // End namespace Aleph
523
524# 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.
SA & sa
Reference to the arc iterator filter object.
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 remove_from_graph(GT_Arc *a)
Removes an arc from the graph and index.
void init()
Initializes the index by inserting all arcs from the graph.
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 * insert(const Key &key)
Inserts a key into the dynamic set.
Key & find(const Key &key) const
Returns a modifiable reference to an element within the set.
size_t remove(const Key &key)
Removes a key from the dynamic set.
Key * search(const Key &key) const
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
Advances the iterator to the next filtered element (noexcept version).
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:524
virtual void remove_node(Node *node) noexcept
Remove a node from the graph and free its memory.
Definition tpl_graph.H:543
typename Node::Node_Type Node_Type
The arc class type.
Definition tpl_graph.H:436
virtual void remove_arc(Arc *arc) noexcept
Remove an arc from the graph and free it.
Definition tpl_graph.H:649
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:604
typename Arc::Arc_Type Arc_Type
The type of data stored in the arc.
Definition tpl_graph.H:439
Filtered iterator on the nodes of a graph.
Definition tpl_graph.H:1206
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).
void init()
Initialize the index by inserting all existing graph nodes.
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.
SN & sn
Reference to the node iterator filter object.
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.
GT_Node * insert_in_graph(const GT_Node_Info &info)
Inserts a node with generic content into the graph and index.
Aleph::DynList< __T > maps(Operation &op) const
Map the elements of the container.
Definition ah-dry.H:904
void * tgt_node
Please don't use.
Definition graph-dry.H:534
void * src_node
Definition graph-dry.H:533
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:494
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:657
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4118
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
Filtered iterator on all the arcs of a graph.
Definition tpl_graph.H:1164
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:1000
Default filter for the graph nodes.
Definition tpl_graph.H:1192
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:222
Treap (a special type of randomized binary search tree) using nodes without virtual destructor.
Definition tpl_treap.H:611
Dynamic set implementations based on balanced binary search trees.
Generic graph and digraph implementations.