Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_indexArc.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
68# ifndef TPL_INDEXARC_H
69# define TPL_INDEXARC_H
70
71# include <cassert>
72# include <functional>
73# include <stdexcept>
74# include <utility>
75
76# include <tpl_dynSetTree.H>
77# include <tpl_graph.H>
78# include <ah-errors.H>
79
80using namespace Aleph;
81
82namespace Aleph
83{
84
85
114 template <
115 class GT,
116 template <class /* Key */, class /* Compare */> class Tree = Rand_Tree,
117 class SA = Dft_Show_Arc<GT>
118 >
120 {
121 private:
122
123 typedef typename GT::Arc GT_Arc;
124 typedef typename GT::Node GT_Node;
125 typedef typename GT::Arc_Type GT_Arc_Type;
126 typedef typename GT::Node_Type GT_Node_Type;
127
128 struct Cmp_Arc
129 {
130 bool directed = false;
131
132 static bool ptr_less(void * a, void * b) noexcept
133 {
134 return std::less<void*>{}(a, b);
135 }
136
137 Cmp_Arc() = default;
138
140
141 static std::pair<void*, void*> endpoints(const GT_Arc * a, const bool directed) noexcept
142 {
143 if (directed)
144 return { a->src_node, a->tgt_node };
145
146 auto u = a->src_node;
147 auto v = a->tgt_node;
148 if (ptr_less(v, u))
149 std::swap(u, v);
150 return {u, v};
151 }
152
153 bool operator () (const GT_Arc * a1, const GT_Arc * a2) const
154 {
155 const auto e1 = endpoints(a1, directed);
156 const auto e2 = endpoints(a2, directed);
157 if (ptr_less(e1.first, e2.first))
158 return true;
159 if (ptr_less(e2.first, e1.first))
160 return false;
161 return ptr_less(e1.second, e2.second);
162 }
163 };
164
165 GT & g;
168
169 public:
170
180 {
181 auto p = index.contains_or_insert(e);
182 return *p.first;
183 }
184
195 GT_Arc * search(void * src, void * tgt) const
196 {
197 GT_Arc arc;
198 arc.src_node = src;
199 arc.tgt_node = tgt;
200
201 GT_Arc ** ret_val = index.search(&arc);
202
203 return ret_val != nullptr ? *ret_val : nullptr;
204 }
205
216 GT_Arc * search_directed(void * src, void * tgt) const
217 {
218 GT_Arc arc;
219 arc.src_node = src;
220 arc.tgt_node = tgt;
221
222 GT_Arc ** ret_val = index.search(&arc);
223
224 return ret_val != nullptr ? *ret_val : nullptr;
225 }
226
232 GT_Arc * search(GT_Arc * a) const
233 {
234 return search(a->src_node, a->tgt_node);
235 }
236
251 const GT_Arc_Type & info)
252 {
253 GT_Arc * a = search(src, tgt);
254
255 ah_domain_error_if(a != nullptr)
256 << "There is already an arc between these nodes";
257
258 a = g.insert_arc(src, tgt, info);
259 insert(a);
260
261 return a;
262 }
263
267 {
268 GT_Arc * a = search(src, tgt);
269
270 ah_domain_error_if(a != nullptr)
271 << "There is already an arc between these nodes";
272
273 a = g.insert_arc(src, tgt, std::forward<GT_Arc_Type>(info));
274 insert(a);
275
276 return a;
277 }
278
280 void remove(GT_Arc * e)
281 {
282 index.remove(e);
283 }
284
287 {
288 remove(a);
289 g.remove_arc(a);
290 }
291
294 {
295 index.empty();
296 }
297
300 {
301 for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next_ne())
302 if (GT_Arc * a = it.get_curr(); search(a) == nullptr)
303 insert(a);
304 }
305
306 private:
307
308 void init()
309 {
310 for (Arc_Iterator<GT, SA> it(g, sa); it.has_curr(); it.next_ne())
311 insert(it.get_curr());
312 }
313
314 public:
315
324 IndexArc(GT & __g, bool with_init = true, SA && __sa = SA())
325 : g(__g), index(Cmp_Arc(__g.is_digraph())), sa(std::move(__sa))
326 {
327 if (with_init)
328 init();
329 }
330
340 : g(__g), index(Cmp_Arc(__g.is_digraph())), sa(__sa)
341 {
342 if (with_init)
343 init();
344 }
345
347 size_t size() const { return index.size(); }
348 };
349
350}
351
352# endif
353
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:522
void next_ne() noexcept
Advances the iterator to the next filtered element (noexcept version).
Index for fast arc lookup by its endpoint nodes.
GT_Arc * search(void *src, void *tgt) const
Search an arc that connects two nodes.
void build_index()
Insert into the index all arcs currently present in the graph.
GT_Arc * insert_in_graph(GT_Node *src, GT_Node *tgt, GT_Arc_Type &&info=GT_Arc_Type())
void remove_from_graph(GT_Arc *a)
Remove an arc from both index and graph.
void remove(GT_Arc *e)
Remove an arc from the index.
size_t size() const
Return the number of arcs currently stored in the index.
GT_Arc * search_directed(void *src, void *tgt) const
Search an arc that connects two nodes as a directed pair.
IndexArc(GT &__g, bool with_init, SA &__sa)
Construct an arc index for a graph (lvalue filter overload).
void clear_index()
Remove all arcs from the index.
GT_Arc * insert_in_graph(GT_Node *src, GT_Node *tgt, const GT_Arc_Type &info)
Create a new arc between two nodes, insert it into the graph and index it.
DynSetTree< GT_Arc *, Tree, Cmp_Arc > index
IndexArc(GT &__g, bool with_init=true, SA &&__sa=SA())
Construct an arc index for a graph.
GT_Arc * insert(GT_Arc *e)
Insert an arc pointer into the index.
GT::Node_Type GT_Node_Type
GT::Arc_Type GT_Arc_Type
GT_Arc * search(GT_Arc *a) const
Search an arc by using its endpoint pointers.
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
void * tgt_node
Please don't use.
Definition graph-dry.H:534
void * src_node
Definition graph-dry.H:533
List_Graph< Graph_Node< int >, Graph_Arc< int > > GT
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
Filtered iterator on all the arcs of a graph.
Definition tpl_graph.H:1164
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1000
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:222
bool operator()(const GT_Arc *a1, const GT_Arc *a2) const
static std::pair< void *, void * > endpoints(const GT_Arc *a, const bool directed) noexcept
Cmp_Arc(bool __directed) noexcept
static bool ptr_less(void *a, void *b) noexcept
Dynamic set implementations based on balanced binary search trees.
Generic graph and digraph implementations.