Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testDnode.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 <ctime>
33# include <cstdlib>
34# include <cassert>
35# include <cstdio>
36# include <random>
37
38# include <iostream>
39# include <string>
40# include <tpl_dnode.H>
41
43
44using namespace std;
45
46void print_list(Test * link)
47{
48 Test* ptr;
49 for (Test::Iterator it(link); it.has_curr(); it.next())
50 {
51 ptr = it.get_curr();
52 cout << ptr->get_data() << " ";
53 }
54 cout << endl;
55}
56
57
58void create_random_list(Test * link, int n, unsigned int seed)
59{
60 std::mt19937 rng(seed);
61 std::uniform_int_distribution<int> dist(1, 10 * n);
63 for (int i = 0; i < n; i++)
64 {
65 testPtr = new Test (dist(rng));
66 link->append(testPtr);
67 }
68}
69
70int main(int argc, char *argv[])
71{
72 int n = 1000;
73 unsigned int t = std::time(0);
74
75 try
76 {
77 if (argc > 1)
78 n = std::stoi(argv[1]);
79
80 if (argc > 2)
81 t = std::stoi(argv[2]);
82 }
83 catch (...)
84 {
85 // ignore
86 }
87
88 if (n <= 0)
89 {
90 cout << "n must be positive" << endl;
91 return 1;
92 }
93
94 srand(t);
95
96 cout << argv[0] << " " << n << " " << t << endl;
97
100 Test* tmpPtr;
101 Test head;
102
103 headPtr = &head;
104
105 unsigned long i;
106
107 for (i = 0; i < (unsigned long) n; i++)
108 {
109 testPtr = new Test;
110
111 testPtr->get_data() = i;
112 headPtr->insert(testPtr);
113 cout << i << " ";
114 }
115
116 cout << endl;
117
118 for (i = 0, testPtr = headPtr->get_next(); i < (unsigned long) n;
119 i++, testPtr = testPtr->get_next())
120 cout << testPtr->get_data() << " ";
121
122 cout << endl;
123
124 for (testPtr = headPtr->get_prev(); testPtr != headPtr;
125 testPtr = testPtr->get_prev())
126 cout << testPtr->get_data() << " ";
127
128 cout << endl;
129
130 while (not headPtr->is_empty())
131 {
132 testPtr = headPtr->get_next();
133 cout << testPtr->get_data() << " ";
134 headPtr->remove_next();
135 delete testPtr;
136 }
137
138 cout << endl;
139
140 for (i = 0; i < (unsigned long) n; i++)
141 {
142 testPtr = new Test;
143
144 testPtr->get_data() = i;
145 headPtr->insert(testPtr);
146 cout << i << " ";
147 }
148
149 cout << endl;
150
151 for (testPtr = headPtr->get_next(); testPtr not_eq headPtr;)
152 {
153 tmpPtr = testPtr;
154 testPtr = testPtr->get_next();
155 cout << testPtr->get_data() << " ";
156 tmpPtr->del();
157 delete tmpPtr;
158 }
159
160 cout << endl;
161
162 for (i = 0; i < (unsigned long) n; i++)
163 {
164 testPtr = new Test;
165
166 testPtr->get_data() = i;
167 headPtr->insert(testPtr);
168 cout << i << " ";
169 }
170
172 delete itor.del();
173
174 assert(headPtr == headPtr->get_prev() and headPtr == headPtr->get_next());
175
176 cout << endl << endl;
177
178 Test list;
179
180 create_random_list(&list, n, t);
181
182 print_list(&list);
183
184 print_list(&list);
185
186 list.remove_all_and_delete();
187
188 printf("Ending .... \n\n");
189}
190
191
int main()
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
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
and
Check uniqueness with explicit hash + equality functors.
STL namespace.
void print_list(Test *link)
Definition testDnode.C:46
Dnode< int > Test
Definition testDnode.C:42
void create_random_list(Test *link, int n, unsigned int seed)
Definition testDnode.C:58
ValueArg< size_t > seed
Definition testHash.C:53
Doubly linked list node with typed data.