Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
euclidian-graph-common.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
43# ifndef EUCLIDIAN_GRAPH_COMMON_H
44# define EUCLIDIAN_GRAPH_COMMON_H
45
46# include <cmath>
47# include <cstddef>
48# include <limits>
49
50# include <gsl/gsl_rng.h>
51
52# include <tpl_sgraph.H>
53# include <io_graph.H>
54# include <random_graph.H>
55
56namespace Aleph
57{
59 struct My_P
60 {
61 long x, y;
62 };
63
71 extern gsl_rng *rand_gen;
72
73 template <class GT>
74 struct Init_P
75 {
76 long W, H;
77
79
85 Init_P(const long w, const long h)
86 : W(w), H(h)
87 {
88 ah_domain_error_if(W <= 0 or H <= 0)
89 << "Init_P(): width and height must be > 0";
90 }
91
92 void operator ()(GT &, typename GT::Node *p)
93 {
94 long x, y;
95 while (true)
96 {
99 std::pair<int, int> q(x, y);
100 if (puntos.search(q) != nullptr)
101 continue;
102
103 puntos.insert(q);
104 break;
105 }
106
107 My_P & my_p = p->get_info();
108 my_p.x = x;
109 my_p.y = y;
110 }
111 };
112
113 template <class GT>
114 struct Init_Arc
115 {
117
118 Init_Arc(const int max) : max_offset(max) {}
119
120 void operator ()(GT & g, typename GT::Arc *a)
121 {
122 typename GT::Node *src = g.get_src_node(a);
123 typename GT::Node *tgt = g.get_tgt_node(a);
124
125 const My_P & psrc = src->get_info();
126 const My_P & ptgt = tgt->get_info();
127
128 const double dist = std::hypot(static_cast<double>(psrc.x - ptgt.x),
129 static_cast<double>(psrc.y - ptgt.y));
130
131 const int offset = max_offset > 0 ? static_cast<int>(gsl_rng_uniform_int(rand_gen, max_offset)) : 0;
132
133 a->get_info() = dist + offset;
134 }
135 };
136
137 template <class GT>
138 struct Wnode
139 {
140 void operator ()(std::ostream & output, GT &, typename GT::Node *p)
141 {
142 output << p->get_info().x << " " << p->get_info().y << '\n';
143 }
144 };
145
146 template <class GT>
147 struct Rnode
148 {
149 void operator ()(std::istream & input, GT &, typename GT::Node *p)
150 {
151 input >> p->get_info().x;
152 input >> p->get_info().y;
153 }
154 };
155
156 template <class GT>
157 struct Warc
158 {
159 void operator ()(std::ostream & output, GT &, typename GT::Arc *a)
160 {
161 output << a->get_info() << '\n';
162 }
163 };
164
165 template <class GT>
166 struct Rarc
167 {
168 void operator ()(std::istream & input, GT &, typename GT::Arc *a)
169 {
170 input >> a->get_info();
171 }
172 };
173
174
175 template <class GT>
176 inline
177 GT gen_random_euclidian_graph(size_t n, size_t m, int w, int h,
178 unsigned int seed)
179 {
180 ah_domain_error_if(w <= 0 or h <= 0)
181 << "gen_random_euclidian_graph(): width and height must be > 0";
182
183 const auto max_unique = static_cast<size_t>(w) * static_cast<size_t>(h);
185 << "gen_random_euclidian_graph(): requested n exceeds available unique grid points";
186
188 ah_bad_alloc_if(rand_gen == nullptr);
190
192 const int max_offset = std::max(1,
193 static_cast<int>(std::ceil(std::hypot(static_cast<double>(w),
194 static_cast<double>(h)))));
195 Init_Arc<GT> initarc(max_offset);
196
198
200 rand_gen = nullptr;
201
202 return g;
203 }
204} // end namespace Aleph
205
206# endif // EUCLIDIAN_GRAPH_COMMON_H
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:522
#define ah_bad_alloc_if(C)
Throws std::bad_alloc if condition holds.
Definition ah-errors.H:429
long double h
Definition btreepic.C:154
long double w
Definition btreepic.C:153
Dynamic set implemented using AVL binary search trees of type Avl_Tree<Key>.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
Key * search(const Key &key) const
Find an element in the set.
Random undirected graph generator.
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:595
NodeInfo & get_info() noexcept
Return a modifiable reference to the data contained in the node.
Definition graph-dry.H:494
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:731
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:737
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_max_function > > max(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4110
const long double offset[]
Offset values indexed by symbol string length (bounded by MAX_OFFSET_INDEX)
Graph serialization and deserialization utilities.
static mpfr_t y
Definition mpfr_mul_d.c:3
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
gsl_rng * rand_gen
Internal RNG handle used by the helper functors in this header.
GT gen_random_euclidian_graph(size_t n, size_t m, int w, int h, unsigned int seed)
DynList< T > maps(const C &c, Op op)
Classic map operation.
Random graph generation utilities.
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:222
void operator()(GT &g, typename GT::Arc *a)
Init_P(const long w, const long h)
Create a node initializer that assigns unique random positions.
void operator()(GT &, typename GT::Node *p)
DynSetAvlTree< std::pair< int, int > > puntos
Simple integer point used as node payload by the helper functors in this header.
void operator()(std::istream &input, GT &, typename GT::Arc *a)
void operator()(std::istream &input, GT &, typename GT::Node *p)
void operator()(std::ostream &output, GT &, typename GT::Arc *a)
void operator()(std::ostream &output, GT &, typename GT::Node *p)
Simple graph implementation with adjacency lists.
ofstream output
Definition writeHeap.C:213