Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testSplayRk1.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
33#include <iostream>
34#include <cstdlib>
35#include <cstdint>
36#include <ctime>
37#include <random>
38#include <cerrno>
39#include <climits>
40#include <tpl_splay_treeRk.H>
41#include <tpl_binNodeXt.H>
42
43using namespace std;
44
45int
46main (int argc, char *argv[])
47{
48 int n = 1000;
49 if (argc > 1)
50 {
51 char *endptr = nullptr;
52 errno = 0;
53 const long parsed_n = strtol (argv[1], &endptr, 10);
54 if (errno != 0 or endptr == argv[1] or *endptr != '\0' or
56 {
57 cerr << "Invalid value for n: " << argv[1] << " (must be between 1 and 1,000,000)" << endl;
58 exit (1);
59 }
60 n = static_cast<int>(parsed_n);
61 }
62
63 auto t = static_cast<unsigned int> (time (nullptr));
64 if (argc > 2)
65 {
66 if (argv[2][0] == '-')
67 {
68 cerr << "Invalid value for t (must be non-negative): " << argv[2] << endl;
69 exit(1);
70 }
71 char *endptr = nullptr;
72 errno = 0;
73 const unsigned long parsed_t = strtoul (argv[2], &endptr, 10);
74 if (errno != 0 or endptr == argv[2] or *endptr != '\0' or parsed_t > UINT_MAX)
75 {
76 cerr << "Invalid value for t: " << argv[2] << endl;
77 exit (1);
78 }
79 t = static_cast<unsigned int>(parsed_t);
80 }
81
82 std::mt19937 rng(t);
83
84 // Safely compute upper bound for random distribution
85 const uint64_t upper_64 = static_cast<uint64_t>(n) * 100;
86 const int upper = (upper_64 > static_cast<uint64_t>(INT_MAX)) ?
87 INT_MAX : static_cast<int>(upper_64);
88 std::uniform_int_distribution<int> dist(1, upper);
89
90 cout << "testSplayTree " << n << " " << t << endl;
91
93
94 cout << "Inserting " << n << " random values in tree ...\n";
95
96 unsigned int insCount = 0;
97
98 while (insCount < static_cast<unsigned int>(n))
99 {
100 const int value = dist(rng);
101 if (Splay_Tree_Rk<int>::Node *node = tree.search (value); node == nullptr)
102 {
103 node = new Splay_Tree_Rk<int>::Node (value);
104 tree.insert (node);
105 insCount++;
106 }
107 }
108
109 cout << insCount << " Items inserted" << endl;
110
111 if (tree.verify ())
112 cout << "arbol equilibrado" << endl;
113 else
114 {
115 cout << "error de equilibrio en el arbol" << endl;
116 destroyRec (tree.getRoot ());
117 return EXIT_FAILURE;
118 }
119
120 destroyRec (tree.getRoot ());
121 cout << "testSplayTree " << n << " " << t << endl;
122}
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
Node * insert(Node *p)
Inserts a node in a top down splay tree.
NodeType< Key > Node
Node * search(const Key &key)
Searches a key in a top down splay tree.
Node *& getRoot()
Get the top-down splay tree's root.
static mt19937 rng
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
void destroyRec(Node *&root) noexcept
Free recursively all the memory occupied by the tree root
void exit(const char *file, int line, const char *format,...)
Print a message and exit the program.
Definition ahDefs.C:125
STL namespace.
Extended binary node with subtree count.
Top-down splay tree with rank support.