Aleph-w 7.10.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
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
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:
276 using Node = __Graph_Node;
277 using Arc = __Graph_Arc;
278 using Node_Type = typename Node::Node_Type;
279 using Arc_Type = typename Arc::Arc_Type;
280
281 friend class GraphCommon<List_SGraph<__Graph_Node, __Graph_Arc>,
282 __Graph_Node, __Graph_Arc>;
283
285 __Graph_Node, __Graph_Arc>;
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
324 class Node_Iterator : public DynSetNode::Iterator
325 {
326 public:
328 using Item_Type = Node *;
329
332
333 Node_Iterator() = default;
334
335 // The iterator hands out pointers to nodes and never modifies the
336 // set of the graph: it is built on the set through a const_cast, and
337 // get_curr() and get_curr_ne() hide the accessors of the set iterator,
338 // which hand out a modifiable reference to the stored pointer.
339 Node_Iterator(const List_SGraph & g) noexcept
340 : DynSetNode::Iterator(const_cast<DynSetNode &>(g.node_list))
341 {
342 // empty
343 }
344
346 Node * get_curr_ne() const noexcept
347 {
348 return this->DynSetNode::Iterator::get_curr_ne();
349 }
350
352 Node * get_curr() const
353 {
354 return this->DynSetNode::Iterator::get_curr();
355 }
356
359 {
360 return this->get_curr();
361 }
362
363 Node * get_current_node_ne() const noexcept
364 {
365 return this->get_curr_ne();
366 }
367 };
368
380 class Node_Arc_Iterator : public DynList<void *>::Iterator
381 {
383
384 public:
386 using Item_Type = Arc *;
387
389 using Set_Type = Node *;
390
392 Node_Arc_Iterator() = default;
393
395 Node_Arc_Iterator(Node *src) noexcept
396 : DynList<void *>::Iterator(src->arc_list), src_node(src)
397 {
398 // empty
399 }
400
402 Arc * get_curr_ne() const noexcept
403 {
404 return static_cast<Arc *>(this->Iterator::get_curr_ne());
405 }
406
408 Arc * get_curr() const
409 {
410 return static_cast<Arc *>(this->Iterator::get_curr());
411 }
412
413 Arc * get_current_arc_ne() const noexcept
414 {
415 return get_curr_ne();
416 }
417
419 {
420 return get_curr();
421 }
422
424 Node * get_tgt_node_ne() const noexcept
425 {
426 Arc *a = get_curr_ne();
427 return static_cast<Node *>(a->get_connected_node(src_node));
428 }
429
432 {
433 Arc *a = get_curr();
434 return static_cast<Node *>(a->get_connected_node(src_node));
435 }
436 };
437
448 class Arc_Iterator : public DynSetArc::Iterator
449 {
450 public:
452 using Item_Type = Arc *;
453
456
457 Arc_Iterator() = default;
458
459 // The iterator hands out pointers to arcs and never modifies the set
460 // of the graph: it is built on the set through a const_cast, and
461 // get_curr() and get_curr_ne() hide the accessors of the set iterator,
462 // which hand out a modifiable reference to the stored pointer.
463 Arc_Iterator(const List_SGraph & _g) noexcept
464 : DynSetArc::Iterator(const_cast<DynSetArc &>(_g.arc_list))
465 {
466 // empty
467 }
468
470 Arc * get_curr_ne() const noexcept
471 {
472 return this->DynSetArc::Iterator::get_curr_ne();
473 }
474
476 Arc * get_curr() const
477 {
478 return this->DynSetArc::Iterator::get_curr();
479 }
480
482 Arc * get_current_arc_ne() const noexcept
483 {
484 return this->get_curr_ne();
485 }
486
488 Node * get_src_node_ne() const noexcept
489 {
490 return static_cast<Node *>(get_current_arc_ne()->src_node);
491 }
492
494 Node * get_tgt_node_ne() const noexcept
495 {
496 return static_cast<Node *>(get_current_arc_ne()->tgt_node);
497 }
498
501 {
502 return this->get_curr();
503 }
504
506 Node * get_src_node() const { return static_cast<Node *>(get_current_arc()->src_node); }
507
509 Node * get_tgt_node() const { return static_cast<Node *>(get_current_arc()->tgt_node); }
510 };
511
512
532 virtual Node * insert_node(Node *p)
533 {
534 assert(p->arc_list.is_empty());
535
536 try
537 {
538 node_list.append(p);
539 }
540 catch (...)
541 {
542 delete p;
543 throw;
544 }
545 ++this->num_nodes;
546 return p;
547 }
548
549 private:
550 // If a list or the set of arcs cannot take the arc, those that took it
551 // give it back, so that the graph is as it was: the caller frees the arc
552 Arc * insert_arc(Node *src, Node *tgt, void *a)
553 {
554 Arc *arc = static_cast<Arc *>(a);
555
556 arc->src_node = src;
557 arc->tgt_node = tgt;
558
559 const bool in_tgt = not this->digraph and src != tgt;
560 auto is_arc = [a](void *x) { return x == a; };
561
562 src->arc_list.append(a);
563 try
564 {
565 if (in_tgt)
566 tgt->arc_list.append(a);
567 }
568 catch (...)
569 {
570 src->arc_list.remove_ne(is_arc);
571 throw;
572 }
573
574 try
575 {
576 arc_list.append(arc);
577 }
578 catch (...)
579 {
580 src->arc_list.remove_ne(is_arc);
581 if (in_tgt)
582 tgt->arc_list.remove_ne(is_arc);
583 throw;
584 }
585
586 ++src->num_arcs;
587 if (in_tgt)
588 ++tgt->num_arcs;
589 ++this->num_arcs;
590
591 return arc;
592 }
593
595 {
596 Node *src = static_cast<Node *>(arc->src_node);
597 Node *tgt = static_cast<Node *>(arc->tgt_node);
598
599 src->arc_list.remove_ne([arc](auto a) { return a == arc; });
600 --src->num_arcs;
601
602 if (not this->digraph and src != tgt)
603 {
604 tgt->arc_list.remove_ne([arc](auto a) { return a == arc; });
605 --tgt->num_arcs;
606 }
607 }
608
609 public:
620 virtual void remove_arc(Arc *arc) noexcept
621 {
622 disconnect_arc(arc);
623 arc_list.remove(arc);
624 --this->num_arcs;
625 delete arc;
626 }
627
634 virtual void remove_node(Node *p)
635 {
636 DynList<Arc *> arcs_to_remove;
637 arc_list.for_each([this, &arcs_to_remove, p](auto arc)
638 {
639 if (this->get_src_node(arc) == p or this->get_tgt_node(arc) == p)
640 arcs_to_remove.append(arc);
641 });
642
643 arcs_to_remove.for_each([this](auto a)
644 {
645 this->disconnect_arc(a);
646 --this->num_arcs;
647 arc_list.remove(a);
648 delete a;
649 });
650 node_list.remove(p);
651 --this->num_nodes;
652
653 delete p;
654 }
655
657 {
658 return static_cast<Node *>(node_list.get_first());
659 }
660
662 {
663 return static_cast<Arc *>(arc_list.get_first());
664 }
665
669 {
670 ah_range_error_if(this->get_num_arcs(p) == 0) << "Node has no arcs";
671 return static_cast<Arc *>(p->arc_list.get_first());
672 }
673
674 private:
675 void clear() noexcept
676 {
677 arc_list.for_each([](Arc *p) { delete p; });
678 node_list.for_each([](Node *p) { delete p; });
679 }
680 };
681
702 template <typename __Graph_Node = Graph_Snode<int>,
703 typename __Graph_Arc = Graph_Sarc<int>>
705} // end namespace Aleph
706
707# 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:356
string Node_Info
int num_nodes
Definition btreepic.C:410
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:4377
Iterator on the items of list.
Definition htlist.H:1614
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
const Key & get_first() const
See min.
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.
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:449
Arc * get_curr() const
Returns the current arc.
Definition tpl_sgraph.H:476
Node * get_src_node_ne() const noexcept
Returns the source node of the current arc (only meaningful for digraphs)
Definition tpl_sgraph.H:488
Node * get_tgt_node() const
Returns the target node of the current arc (only meaningful for digraphs)
Definition tpl_sgraph.H:509
Arc * get_current_arc_ne() const noexcept
Returns a pointer to the current arc.
Definition tpl_sgraph.H:482
Node * get_tgt_node_ne() const noexcept
Returns the target node of the current arc (only meaningful for digraphs)
Definition tpl_sgraph.H:494
Arc * Item_Type
Data type returned by get_curr()
Definition tpl_sgraph.H:452
Arc * get_current_arc() const
Returns a pointer to the current arc.
Definition tpl_sgraph.H:500
Arc * get_curr_ne() const noexcept
Returns the current arc without checking that there is one.
Definition tpl_sgraph.H:470
Arc_Iterator(const List_SGraph &_g) noexcept
Definition tpl_sgraph.H:463
Node * get_src_node() const
Returns the source node of the current arc (only meaningful for digraphs)
Definition tpl_sgraph.H:506
Arc iterator for a graph node.
Definition tpl_sgraph.H:381
Arc * get_current_arc_ne() const noexcept
Definition tpl_sgraph.H:413
Node * get_tgt_node() const
Returns the target node of the current arc.
Definition tpl_sgraph.H:431
Node_Arc_Iterator()=default
Instantiate an empty (invalid) iterator.
Arc * Item_Type
Data type returned by get_curr()
Definition tpl_sgraph.H:386
Arc * get_curr() const
Return get_current_arc()
Definition tpl_sgraph.H:408
Node * Set_Type
The set type over which iteration is performed.
Definition tpl_sgraph.H:389
Node * get_tgt_node_ne() const noexcept
Returns the target node of the current arc.
Definition tpl_sgraph.H:424
Arc * get_curr_ne() const noexcept
Return get_current_arc() without exception.
Definition tpl_sgraph.H:402
Node_Arc_Iterator(Node *src) noexcept
Instantiate an iterator over node src.
Definition tpl_sgraph.H:395
Node iterator for a graph.
Definition tpl_sgraph.H:325
Node * get_curr() const
Returns the current node.
Definition tpl_sgraph.H:352
Node * get_current_node_ne() const noexcept
Definition tpl_sgraph.H:363
Node * get_curr_ne() const noexcept
Returns the current node without checking that there is one.
Definition tpl_sgraph.H:346
Node * get_current_node() const
Returns the current node.
Definition tpl_sgraph.H:358
Node_Iterator(const List_SGraph &g) noexcept
Definition tpl_sgraph.H:339
Node * Item_Type
Data type returned by get_curr()
Definition tpl_sgraph.H:328
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
Arc * insert_arc(Node *src, Node *tgt, void *a)
Definition tpl_sgraph.H:552
void clear() noexcept
Definition tpl_sgraph.H:675
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:656
Arc * get_first_arc(Node *p) const
Returns the first arc of node p.
Definition tpl_sgraph.H:668
__Graph_Arc Arc
Definition tpl_sgraph.H:277
void disconnect_arc(Arc *arc)
Definition tpl_sgraph.H:594
__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:620
virtual Node * insert_node(Node *p)
Insertion of a node whose memory has already been allocated.
Definition tpl_sgraph.H:532
virtual void remove_node(Node *p)
Removes the node and its arcs, and frees them.
Definition tpl_sgraph.H:634
typename Node::Node_Type Node_Type
Definition tpl_sgraph.H:278
Arc * get_first_arc() const
Definition tpl_sgraph.H:661
virtual ~List_SGraph()
Definition tpl_sgraph.H:300
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:1475
Common methods for the arc of a graph.
Definition graph-dry.H:787
ArcInfo arc_info
Definition graph-dry.H:801
Common attributes and methods for nodes (vertexes) belonging to graphs.
Definition graph-dry.H:694
NodeInfo node_info
Definition graph-dry.H:702
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:753
Common methods to the Aleph-w ( ) graph classes.
Definition graph-dry.H:877
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:1402
void common_swap(GT &g) noexcept
Definition graph-dry.H:921
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:1479
#define ALEPH_GRAPH_COPY_MOVE_CTORS(GraphClass)
Macro to generate copy/move constructors and assignment operators.
Definition graph-dry.H:4311
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
auto get_curr_ne() const noexcept(zip_detail::nothrow_curr_copy< Aleph::const_iterator_t< HeadC >, T > and zip_detail::nothrow_item_copy< T > and zip_detail::nothrow_tail_curr< ZipIterator< TailC... > >)
Return the current tuple (unchecked variant).
Definition ah-zip.H:216
auto get_curr() const
Return the current tuple (bounds-checked).
Definition ah-zip.H:192
and
Check uniqueness with explicit hash + equality functors.
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.