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 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <https://www.gnu.org/licenses/>.
26*/
27# include <ctime>
28# include <cstdlib>
29# include <cassert>
30# include <cstdio>
31# include <random>
32
33# include <iostream>
34# include <string>
35# include <tpl_dnode.H>
36
38
39using namespace std;
40
41void print_list(Test * link)
42{
43 Test* ptr;
44 for (Test::Iterator it(link); it.has_curr(); it.next())
45 {
46 ptr = it.get_curr();
47 cout << ptr->get_data() << " ";
48 }
49 cout << endl;
50}
51
52
53void create_random_list(Test * link, int n, unsigned int seed)
54{
55 std::mt19937 rng(seed);
56 std::uniform_int_distribution<int> dist(1, 10 * n);
58 for (int i = 0; i < n; i++)
59 {
60 testPtr = new Test (dist(rng));
61 link->append(testPtr);
62 }
63}
64
65int main(int argc, char *argv[])
66{
67 int n = 1000;
68 unsigned int t = std::time(0);
69
70 try
71 {
72 if (argc > 1)
73 n = std::stoi(argv[1]);
74
75 if (argc > 2)
76 t = std::stoi(argv[2]);
77 }
78 catch (...)
79 {
80 // ignore
81 }
82
83 if (n <= 0)
84 {
85 cout << "n must be positive" << endl;
86 return 1;
87 }
88
89 srand(t);
90
91 cout << argv[0] << " " << n << " " << t << endl;
92
95 Test* tmpPtr;
96 Test head;
97
98 headPtr = &head;
99
100 unsigned long i;
101
102 for (i = 0; i < (unsigned long) n; i++)
103 {
104 testPtr = new Test;
105
106 testPtr->get_data() = i;
107 headPtr->insert(testPtr);
108 cout << i << " ";
109 }
110
111 cout << endl;
112
113 for (i = 0, testPtr = headPtr->get_next(); i < (unsigned long) n;
114 i++, testPtr = testPtr->get_next())
115 cout << testPtr->get_data() << " ";
116
117 cout << endl;
118
119 for (testPtr = headPtr->get_prev(); testPtr != headPtr;
120 testPtr = testPtr->get_prev())
121 cout << testPtr->get_data() << " ";
122
123 cout << endl;
124
125 while (not headPtr->is_empty())
126 {
127 testPtr = headPtr->get_next();
128 cout << testPtr->get_data() << " ";
129 headPtr->remove_next();
130 delete testPtr;
131 }
132
133 cout << endl;
134
135 for (i = 0; i < (unsigned long) n; i++)
136 {
137 testPtr = new Test;
138
139 testPtr->get_data() = i;
140 headPtr->insert(testPtr);
141 cout << i << " ";
142 }
143
144 cout << endl;
145
146 for (testPtr = headPtr->get_next(); testPtr not_eq headPtr;)
147 {
148 tmpPtr = testPtr;
149 testPtr = testPtr->get_next();
150 cout << testPtr->get_data() << " ";
151 tmpPtr->del();
152 delete tmpPtr;
153 }
154
155 cout << endl;
156
157 for (i = 0; i < (unsigned long) n; i++)
158 {
159 testPtr = new Test;
160
161 testPtr->get_data() = i;
162 headPtr->insert(testPtr);
163 cout << i << " ";
164 }
165
167 delete itor.del();
168
169 assert(headPtr == headPtr->get_prev() and headPtr == headPtr->get_next());
170
171 cout << endl << endl;
172
173 Test list;
174
175 create_random_list(&list, n, t);
176
177 print_list(&list);
178
179 print_list(&list);
180
181 list.remove_all_and_delete();
182
183 printf("Ending .... \n\n");
184}
185
186
Iterator on a list of Dnode objects.
Definition tpl_dnode.H:261
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
and
Check uniqueness with explicit hash + equality functors.
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
STL namespace.
void print_list(Test *link)
Definition testDnode.C:41
Dnode< int > Test
Definition testDnode.C:37
void create_random_list(Test *link, int n, unsigned int seed)
Definition testDnode.C:53
ValueArg< size_t > seed
Definition testHash.C:48
Doubly linked list node with typed data.