Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_sgraph.H
Go to the documentation of this file.
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
44# ifndef TPL_SGRAPH_H
45# define TPL_SGRAPH_H
46
47# include <memory>
48# include <htlist.H>
49# include <tpl_graph.H>
50# include <tpl_dynSetTree.H>
51# include <ah-errors.H>
52
53namespace Aleph
54{
91 template <typename Node_Info = Empty_Class>
92 struct Graph_Snode : public GTNodeCommon<Node_Info>
93 {
95 friend class GTNodeCommon<Node_Info>;
96
98
112 Graph_Snode(const Node_Info & info)
113 : Base(info)
114 {
115 /* empty */
116 }
117
118 Graph_Snode(Node_Info && info = Node_Info())
119 : Base(std::move(info))
120 {
121 /* empty */
122 }
123
125 : Graph_Snode(node.node_info)
126 {
127 /* empty */
128 }
129
131 {
132 if (&node == this)
133 return *this;
134 this->node_info = node.node_info;
135 return *this;
136 }
137
153 : Base(node->get_info())
154 {
155 /* empty */
156 }
157 };
158
195 template <typename Arc_Info = Empty_Class>
196 class Graph_Sarc : public GTArcCommon<Arc_Info>
197 {
199
200 public:
201 Graph_Sarc(const Arc_Info & info)
202 : Base(info)
203 {
204 /* empty */
205 }
206
207 Graph_Sarc(Arc_Info && info = Arc_Info())
208 : Base(std::move(info))
209 {
210 /* empty */
211 }
212
214 : Graph_Sarc(arc.arc_info)
215 {
216 /* empty */
217 }
218
220 {
221 if (&arc == this)
222 return *this;
223 this->arc_info = arc.arc_info;
224 return *this;
225 }
226
227 Graph_Sarc(void *src, void *tgt, const Arc_Info & data)
228 : Base(src, tgt, data)
229 {
230 // empty
231 }
232
233 Graph_Sarc(void *src, void *tgt, Arc_Info && data)
234 : Base(src, tgt, std::move(data))
235 {
236 // empty
237 }
238 };
239
240
269 template <typename __Graph_Node = Graph_Snode<unsigned long>,
270 typename __Graph_Arc = Graph_Sarc<unsigned long>>
272 : public GraphCommon<List_SGraph<__Graph_Node, __Graph_Arc>,
273 __Graph_Node, __Graph_Arc>
274 {
275 public:
278 using Node_Type = typename Node::Node_Type;
279 using Arc_Type = typename Arc::Arc_Type;
280
283
286
289
290 private:
293
296
297 public:
298 List_SGraph() = default;
299
300 virtual ~List_SGraph()
301 {
302 clear();
303 }
304
305 void swap(List_SGraph & g) noexcept
306 {
307 this->common_swap(g);
308 node_list.swap(g.node_list);
309 arc_list.swap(g.arc_list);
310 }
311
314
315
322 {
323 public:
325 using Item_Type = Node *;
326
329
330 Node_Iterator() = default;
331
333 : DynSetNode::Iterator(g.node_list)
334 {
335 // empty
336 }
337
340 {
341 return this->get_curr();
342 }
343
345 {
346 return this->get_curr_ne();
347 }
348 };
349
357 class Node_Arc_Iterator : public DynList<void *>::Iterator
358 {
360
361 public:
363 using Item_Type = Arc *;
364
366 using Set_Type = Node *;
367
369 Node_Arc_Iterator() = default;
370
373 : DynList<void *>::Iterator(src->arc_list), src_node(src)
374 {
375 // empty
376 }
377
380 {
381 return static_cast<Arc *>(this->Iterator::get_curr_ne());
382 }
383
385 Arc * get_curr() const
386 {
387 return static_cast<Arc *>(this->Iterator::get_curr());
388 }
389
391 {
392 return get_curr_ne();
393 }
394
396 {
397 return get_curr();
398 }
399
402 {
403 Arc *a = get_curr_ne();
404 return static_cast<Node *>(a->get_connected_node(src_node));
405 }
406
409 {
410 Arc *a = get_curr();
411 return static_cast<Node *>(a->get_connected_node(src_node));
412 }
413 };
414
422 class Arc_Iterator : public DynSetArc::Iterator
423 {
424 public:
426 using Item_Type = Arc *;
427
430
431 Arc_Iterator() = default;
432
434 : DynSetArc::Iterator(_g.arc_list)
435 {
436 // empty
437 }
438
441 {
442 return this->get_curr_ne();
443 }
444
447 {
448 return static_cast<Node *>(get_current_arc_ne()->src_node);
449 }
450
453 {
454 return static_cast<Node *>(get_current_arc_ne()->tgt_node);
455 }
456
459 {
460 return this->get_curr();
461 }
462
464 Node * get_src_node() const { return static_cast<Node *>(get_current_arc()->src_node); }
465
467 Node * get_tgt_node() const { return static_cast<Node *>(get_current_arc()->tgt_node); }
468 };
469
470
487 virtual Node * insert_node(Node *p)
488 {
489 assert(p->arc_list.is_empty());
490
491 ++this->num_nodes;
492 node_list.append(p);
493 return p;
494 }
495
496 private:
497 Arc * insert_arc(Node *src, Node *tgt, void *a)
498 {
499 Arc *arc = static_cast<Arc *>(a);
500
501 arc->src_node = src;
502 arc->tgt_node = tgt;
503
504 src->arc_list.append(a);
505 ++src->num_arcs;
506 if (not this->digraph and src != tgt)
507 {
508 tgt->arc_list.append(a);
509 ++tgt->num_arcs;
510 }
511
512 arc_list.append(arc);
513 ++this->num_arcs;
514
515 return arc;
516 }
517
519 {
520 Node *src = static_cast<Node *>(arc->src_node);
521 Node *tgt = static_cast<Node *>(arc->tgt_node);
522
523 src->arc_list.remove_ne([arc](auto a) { return a == arc; });
524 --src->num_arcs;
525
526 if (not this->digraph and src != tgt)
527 {
528 tgt->arc_list.remove_ne([arc](auto a) { return a == arc; });
529 --tgt->num_arcs;
530 }
531 }
532
533 public:
544 virtual void remove_arc(Arc *arc) noexcept
545 {
546 disconnect_arc(arc);
547 arc_list.remove(arc);
548 --this->num_arcs;
549 delete arc;
550 }
551
552 virtual void remove_node(Node *p) noexcept
553 {
555 arc_list.for_each([this, &arcs_to_remove, p](auto arc)
556 {
557 if (this->get_src_node(arc) == p or this->get_tgt_node(arc) == p)
558 {
559 this->disconnect_arc(arc);
560 --this->num_arcs;
562 }
563 });
564
565 arcs_to_remove.for_each([this](auto a)
566 {
567 arc_list.remove(a);
568 delete a;
569 });
570 node_list.remove(p);
571 --this->num_nodes;
572
573 delete p;
574 }
575
577 {
578 return static_cast<Node *>(node_list.get_first());
579 }
580
582 {
583 return static_cast<Arc *>(arc_list.get_first());
584 }
585
589 {
590 ah_range_error_if(this->get_num_arcs(p) == 0) << "Node has no arcs";
591 return static_cast<Arc *>(p->arc_list.get_first());
592 }
593
594 private:
596 {
597 arc_list.for_each([](Arc *p) { delete p; });
598 node_list.for_each([](Node *p) { delete p; });
599 }
600
601 public:
612 template <class Compare>
613 void sort_arcs(Compare & cmp)
614 {
616 arc_list.for_each([&temp_list](Arc *a) { temp_list.append(a); });
617 arc_list.empty();
618
619 // Sort the temporary list
621
622 // Rebuild the arc_list from sorted order
623 temp_list.for_each([this](Arc *a) { arc_list.append(a); });
624 }
625
627 template <class Compare>
628 void sort_arcs(Compare && cmp = Compare())
629 {
630 sort_arcs(cmp);
631 }
632 };
633
654 template <typename __Graph_Node = Graph_Snode<int>,
655 typename __Graph_Arc = Graph_Sarc<int>>
657} // end namespace Aleph
658
659# endif /* TPL_SGRAPH_H */
Exception handling system with formatted messages for Aleph-w.
#define ah_range_error_if(C)
Throws std::range_error if condition holds.
Definition ah-errors.H:207
int num_nodes
Definition btreepic.C:410
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:3848
Iterator on the items of list.
Definition htlist.H:1714
Dynamic singly linked list with functional programming support.
Definition htlist.H:1423
T & append(const T &item)
Append a new item by copy.
Definition htlist.H:1562
const Key & get_first() const
Key * append(const Key &key)
void swap(DynSetTree &dset) noexcept(noexcept(tree.swap(dset.tree)) and noexcept(std::swap(num_nodes, dset.num_nodes)) and noexcept(std::swap(arena_allocator, dset.arena_allocator)))
Exchange all elements of this set with dset in constant time (and extremely fast).
size_t remove(const Key &key)
Removes a key from the dynamic set.
void empty()
remove all elements from the set
Arc for graphs implemented with simple adjacency lists.
Definition tpl_sgraph.H:197
Graph_Sarc(const Arc_Info &info)
Definition tpl_sgraph.H:201
Graph_Sarc(const Graph_Sarc &arc)
Definition tpl_sgraph.H:213
Graph_Sarc(void *src, void *tgt, Arc_Info &&data)
Definition tpl_sgraph.H:233
Graph_Sarc & operator=(const Graph_Sarc &arc)
Definition tpl_sgraph.H:219
Graph_Sarc(void *src, void *tgt, const Arc_Info &data)
Definition tpl_sgraph.H:227
Graph_Sarc(Arc_Info &&info=Arc_Info())
Definition tpl_sgraph.H:207
Iterator over arcs of a graph.
Definition tpl_sgraph.H:423
Node * get_src_node_ne() const noexcept
Returns the source node of the current arc (only meaningful for digraphs)
Definition tpl_sgraph.H:446
Node * get_tgt_node() const
Returns the target node of the current arc (only meaningful for digraphs)
Definition tpl_sgraph.H:467
Arc * get_current_arc_ne() const noexcept
Returns a pointer to the current arc.
Definition tpl_sgraph.H:440
Node * get_tgt_node_ne() const noexcept
Returns the target node of the current arc (only meaningful for digraphs)
Definition tpl_sgraph.H:452
Arc * Item_Type
Data type returned by get_curr()
Definition tpl_sgraph.H:426
Arc * get_current_arc() const
Returns a pointer to the current arc.
Definition tpl_sgraph.H:458
Arc_Iterator(const List_SGraph &_g) noexcept
Definition tpl_sgraph.H:433
Node * get_src_node() const
Returns the source node of the current arc (only meaningful for digraphs)
Definition tpl_sgraph.H:464
Arc iterator for a graph node.
Definition tpl_sgraph.H:358
Arc * get_current_arc_ne() const noexcept
Definition tpl_sgraph.H:390
Node * get_tgt_node() const
Returns the target node of the current arc.
Definition tpl_sgraph.H:408
Node_Arc_Iterator()=default
Instantiate an empty (invalid) iterator.
Arc * Item_Type
Data type returned by get_curr()
Definition tpl_sgraph.H:363
Arc * get_curr() const
Return get_current_arc()
Definition tpl_sgraph.H:385
Node * Set_Type
The set type over which iteration is performed.
Definition tpl_sgraph.H:366
Node * get_tgt_node_ne() const noexcept
Returns the target node of the current arc.
Definition tpl_sgraph.H:401
Arc * get_curr_ne() const noexcept
Return get_current_arc() without exception.
Definition tpl_sgraph.H:379
Node_Arc_Iterator(Node *src) noexcept
Instantiate an iterator over node src.
Definition tpl_sgraph.H:372
Node iterator for a graph.
Definition tpl_sgraph.H:322
Node * get_current_node_ne() const noexcept
Definition tpl_sgraph.H:344
Node * get_current_node() const
Returns the current node.
Definition tpl_sgraph.H:339
Node_Iterator(const List_SGraph &g) noexcept
Definition tpl_sgraph.H:332
Node * Item_Type
Data type returned by get_curr()
Definition tpl_sgraph.H:325
Graph class implemented with singly-linked adjacency lists.
Definition tpl_sgraph.H:274
DynSetArc arc_list
Set of arcs in the graph.
Definition tpl_sgraph.H:295
void sort_arcs(Compare &cmp)
Sort all arcs of the graph according to a comparison criteria.
Definition tpl_sgraph.H:613
Arc * insert_arc(Node *src, Node *tgt, void *a)
Definition tpl_sgraph.H:497
void clear() noexcept
Definition tpl_sgraph.H:595
virtual void remove_node(Node *p) noexcept
Definition tpl_sgraph.H:552
void swap(List_SGraph &g) noexcept
Definition tpl_sgraph.H:305
DynSetNode node_list
Set of nodes in the graph.
Definition tpl_sgraph.H:294
Node * get_first_node() const
Definition tpl_sgraph.H:576
Arc * get_first_arc(Node *p) const
Returns the first arc of node p.
Definition tpl_sgraph.H:588
__Graph_Arc Arc
Definition tpl_sgraph.H:277
void disconnect_arc(Arc *arc)
Definition tpl_sgraph.H:518
void sort_arcs(Compare &&cmp=Compare())
Definition tpl_sgraph.H:628
__Graph_Node Node
Definition tpl_sgraph.H:276
typename Arc::Arc_Type Arc_Type
Definition tpl_sgraph.H:279
List_SGraph()=default
virtual void remove_arc(Arc *arc) noexcept
Removes the arc.
Definition tpl_sgraph.H:544
virtual Node * insert_node(Node *p)
Insertion of a node whose memory has already been allocated.
Definition tpl_sgraph.H:487
typename Node::Node_Type Node_Type
Definition tpl_sgraph.H:278
Arc * get_first_arc() const
Definition tpl_sgraph.H:581
virtual ~List_SGraph()
Definition tpl_sgraph.H:300
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:685
Common methods for the arc of a graph.
Definition graph-dry.H:528
ArcInfo arc_info
Definition graph-dry.H:542
Common attributes and methods for nodes (vertexes) belonging to graphs.
Definition graph-dry.H:435
NodeInfo node_info
Definition graph-dry.H:443
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:494
Common methods to the Aleph-w ( ) graph classes.
Definition graph-dry.H:618
Node * insert_node(const Node_Type &node_info)
Allocate a new node, set by copy its data content and insert it into the graph.
Definition graph-dry.H:1116
void common_swap(GT &g) noexcept
Definition graph-dry.H:641
Arc * insert_arc(Node *src, Node *tgt, const Arc_Type &arc_info)
Create and insert a new arc linking two nodes and copying data.
Definition graph-dry.H:1193
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4118
#define ALEPH_GRAPH_COPY_MOVE_CTORS(GraphClass)
Macro to generate copy/move constructors and assignment operators.
Definition graph-dry.H:3786
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
void mergesort(T *a, const long l, const long r, std::vector< T > &buf, Compare cmp)
auto get_curr() const
Return the current tuple (bounds-checked).
Definition ah-zip.H:149
auto get_curr_ne() const noexcept
Return the current tuple (no-throw variant).
Definition ah-zip.H:159
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
Node for graphs implemented with simple adjacency lists.
Definition tpl_sgraph.H:93
Graph_Snode(Node_Info &&info=Node_Info())
Definition tpl_sgraph.H:118
DynList< void * > arc_list
Adjacency list of arcs incident to this node.
Definition tpl_sgraph.H:97
Graph_Snode(const Graph_Snode &node)
Definition tpl_sgraph.H:124
Graph_Snode(const Node_Info &info)
Constructor that assigns an attribute value.
Definition tpl_sgraph.H:112
Graph_Snode & operator=(const Graph_Snode &node)
Definition tpl_sgraph.H:130
Graph_Snode(Graph_Snode *node)
Copy constructor from a node pointer.
Definition tpl_sgraph.H:152
Dynamic set implementations based on balanced binary search trees.
Generic graph and digraph implementations.