Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
test_random_search.C
Go to the documentation of this file.
1
2/* Aleph-w
3
4 / \ | | ___ _ __ | |__ __ __
5 / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6 / ___ \| | __/ |_) | | | |_____\ V V / version 1.9c
7 /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8 |_|
9
10 This file is part of Aleph-w library
11
12 Copyright (c) 2002-2018 Leandro Rabindranath Leon
13
14 Permission is hereby granted, free of charge, to any person obtaining a copy
15 of this software and associated documentation files (the "Software"), to deal
16 in the Software without restriction, including without limitation the rights
17 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 copies of the Software, and to permit persons to whom the Software is
19 furnished to do so, subject to the following conditions:
20
21 The above copyright notice and this permission notice shall be included in all
22 copies or substantial portions of the Software.
23
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 SOFTWARE.
31*/
32# include <iostream>
33# include <tpl_sort_utils.H>
34
35# include <ctime>
36# include <cstdlib>
37# include <cassert>
38using namespace std;
39
40struct Cmp
41{
42 bool operator () (Dlink * p, Dlink * q) const
43 {
44 return (static_cast<Dnode<int>*>(p)->get_data() <
45 static_cast<Dnode<int>*>(q)->get_data());
46 }
47};
48
49
50int main(int argc, char *argv[])
51{
52 int n = 1000;
53 if (argc > 1)
54 {
55 try { n = stoi(argv[1]); }
56 catch (...) { n = 1000; }
57 }
58
59 // Validate input to prevent invalid operations
60 if (n <= 0)
61 {
62 cerr << "Error: n must be a positive integer." << endl;
63 return 1;
64 }
65
66 unsigned int t = std::time(0);
67
68 if (argc > 2)
69 {
70 try { t = static_cast<unsigned int>(stoul(argv[2])); }
71 catch (...) { t = std::time(0); }
72 }
73
74 srand(t);
75
76 cout << argv[0] << " " << n << " " << t << endl;
77
78 Dnode<int> list;
79
80 for (int i = 0; i < n; ++i)
81 {
82 int value = 1 + (int) (1.0 * n * (rand() / (RAND_MAX + 1.0)));
83
84 list.append(new Dnode<int>(value));
85 }
86
87 for (Dnode<int>::Iterator it(list); it.has_curr(); it.next())
88 cout << it.get_curr()->get_data() << " ";
89 cout << endl;
90
91# ifdef nada
92 if (Aleph::random_search<int, Cmp>(list, n/2) != NULL)
93 cout << n/2 << " se encuentra en la lista" << endl;
94 else
95 cout << n/2 << " no se encuentra en la lista" << endl;
96# endif
97
98 assert(list.check());
99
100 if (Aleph::random_search(list, n/2) != NULL)
101 cout << n/2 << " se encuentra en la lista" << endl;
102 else
103 cout << n/2 << " no se encuentra en la lista" << endl;
104
105 assert(list.check());
106
107 for (Dnode<int>::Iterator it(list); it.has_curr(); it.next())
108 cout << it.get_curr()->get_data() << " ";
109 cout << endl;
110
111 assert(list.check());
112
113 Dnode<int> * ptr = Aleph::random_select <int> (list, n/2);
114
115 cout << "El elemento " << n/2 << " es: " << ptr->get_data() << endl;
116
118
119}
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
Iterator on a list of Dnode objects.
Definition tpl_dnode.H:260
Node belonging to a double circular linked list with header node.
Definition tpl_dnode.H:106
T & get_data() noexcept
Return a modifiable reference to the data contained in the node.
Definition tpl_dnode.H:232
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
long random_search(T *a, const T &x, const long l, const long r, const Compare &cmp=Compare())
Random search for an element in an array.
STL namespace.
bool operator()(Dlink *p, Dlink *q) const
Comprehensive sorting algorithms and search utilities for Aleph-w.