Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testQueue.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
28# include <iostream>
29# include <string>
30# include <cstdlib>
31# include <cerrno>
32# include <climits>
33# include <stdexcept>
34# include <tpl_arrayQueue.H>
35
36using namespace std;
37
38int g_count = -1;
39
40struct Foo
41{
42 int * ptr;
43
44 void swap(Foo & f)
45 {
46 std::swap(ptr, f.ptr);
47 }
48
50 {
51 ptr = new int;
52 *ptr = ::g_count--;
53 }
54
55 Foo(int i) : ptr(nullptr)
56 {
57 ptr = new int;
58 *ptr = i;
59 }
60
61 Foo(const Foo & f) : ptr(nullptr)
62 {
63 if (f.ptr != nullptr)
64 {
65 ptr = new int;
66 *ptr = *f.ptr;
67 }
68 }
69
70 Foo(Foo && f) : ptr(nullptr)
71 {
72 std::swap(ptr, f.ptr);
73 }
74
75 Foo & operator = (const Foo & f)
76 {
77 if (this == &f)
78 return *this;
79
80 // Handle null pointers safely
81 if (f.ptr == nullptr)
82 {
83 delete ptr;
84 ptr = nullptr;
85 }
86 else if (ptr == nullptr)
87 {
88 ptr = new int;
89 *ptr = *f.ptr;
90 }
91 else
92 {
93 *ptr = *f.ptr;
94 }
95
96 return *this;
97 }
98
100 {
101 swap(f);
102 return *this;
103 }
104
106 {
107 if (ptr != nullptr)
108 {
109 delete ptr;
110 ptr = nullptr;
111 }
112 }
113
114 // operator int () { return *ptr; }
115
116 operator int () const
117 {
118 if (ptr == nullptr)
119 throw std::logic_error("attempt to access value of moved-from Foo");
120 return *ptr;
121 }
122};
123
124template <typename T>
125void print(const ArrayQueue<T> & q)
126{
127 cout << "capacity = " << q.capacity() << endl
128 << "size = " << q.size() << endl;
129
130 for (size_t i = 0; i < q.size(); ++i)
131 cout << (T) q.front(i) << " ";
132 cout << endl ;
133
134 for (size_t i = 0; i < q.size(); ++i)
135 cout << (T) q.rear(i) << " ";
136 cout << endl
137 << endl;
138}
139
140template <typename T>
142{
143 cout << "Creating rval queue ";
145 for (int i = 0; i < n; ++i)
146 cout << q.put(T(i)) << " ";
147 cout << endl;
148
149 return q;
150}
151
152int main(int argc, char * argv[])
153{
154 if (argc == 1)
155 {
156 cout << "testQueue -- exercises ArrayQueue with insert, consult, and delete operations\n"
157 << "\n"
158 << "Creates an ArrayQueue<int> of the given size, fills it, reads back\n"
159 << "elements, drains in steps of 3 until underflow, refills, then deletes\n"
160 << "a specified number of items. Finally tests copy and move constructors\n"
161 << "with both int and a heap-allocating Foo type.\n"
162 << "\n"
163 << "Usage:\n"
164 << " " << argv[0] << " <queue-size> <items-to-delete>\n"
165 << "\n"
166 << "Arguments:\n"
167 << " queue-size Capacity of the queue and number of items to insert\n"
168 << " items-to-delete Number of items to remove in the second phase\n"
169 << "\n"
170 << "Example:\n"
171 << " " << argv[0] << " 20 7\n"
172 << " Creates a queue of capacity 20, inserts 20 items, then deletes 7.\n";
173 return 0;
174 }
175
176 if (argc < 3)
177 {
178 cerr << "Usage: " << argv[0] << " <queue-size> <items-to-delete>" << endl;
179 cerr << "Both arguments must be non-negative integers." << endl;
180 return 1;
181 }
182
183 // Parse first argument (queue size)
184 char *endptr1;
185 errno = 0;
186 long n_long = strtol(argv[1], &endptr1, 10);
187
188 // Validate first argument
190 {
191 cerr << "Error: Invalid queue size argument. Must be a non-negative integer fitting in int." << endl;
192 return 1;
193 }
194
195 int n = static_cast<int>(n_long);
196 ArrayQueue<int> q(n);
197
198 print(q);
199
200 cout << "Inserting " << n << " values ";
201 for (size_t i = 0; i < n; ++i)
202 cout << q.put(i) << " ";
203 cout << " done!" << endl << endl;
204
205 print(q);
206
207 cout << "Consulting all values until underflow ";
208 for (int i = 0; 1; i++)
209 {
210 try
211 {
212 int val = q.rear(i);
213 cout << val << " " ;
214 }
215 catch (std::range_error & exc)
216 {
217 cout << endl << exc.what() << endl;
218 break;
219 }
220 }
221 cout << " done! " << endl << endl;
222
223 cout << "Deleting all values in steps of 3 until underflow ";
224 while (1)
225 {
226 try
227 {
228 printf("%d ", q.getn(3));
229 }
230 catch (std::underflow_error & exc)
231 {
232 cout << endl << exc.what() << endl;
233 break;
234 }
235 }
236 cout << " done! " << endl << endl;
237
238 print(q);
239
240 cout << "Inserting " << n << " values " << endl;
241 for (size_t i = 0; i < n; ++i)
242 cout << q.put(i) << " ";
243 cout << " done!" << endl << endl;
244
245 print(q);
246
247 // Parse second argument (items to delete)
248 char *endptr2;
249 errno = 0;
250 long m_long = strtol(argv[2], &endptr2, 10);
251
252 // Validate second argument
254 {
255 cerr << "Error: Invalid items-to-delete argument. Must be a non-negative integer fitting in int." << endl;
256 return 1;
257 }
258
259 int m = static_cast<int>(m_long);
260
261 cout << "Deleting " << m << " items" << endl;
262 for (int i = 0; i < m; i++)
263 {
264 try
265 {
266 cout << q.get() << " ";
267 }
268 catch (std::underflow_error & exc)
269 {
270 cout << endl << exc.what() << endl;
271 break;
272 }
273 }
274
275 cout << "done!" << endl << endl;
276
277 cout << "q = " << endl;
278 print(q);
279
280 cout << "Testing constructors ... " << endl;
281
282 ArrayQueue<int> q1 = q;
283
284 print(q1);
285
287 print(q2);
288
289
291 print(q3);
292
293 printf("Ended\n");
294}
Queue implemented with a single dynamic array.
T & front(const size_t i=0) const
Return the i-th oldest item of the queue.
T & getn(const size_t i)
Remove the i oldest items of the queue.
T & put(const T &item)
Copy and put an item in the queue.
T & rear(const size_t i=0) const
Return the i-th youngest item of the queue.
T get()
Remove the oldest item of the queue and return a copy.
size_t size() const noexcept
Return the number of elements.
constexpr size_t capacity() const noexcept
The type of element of array.
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.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:105
STL namespace.
int i
void swap(Foo &f)
Definition testQueue.C:44
Foo()
Definition testQueue.C:49
std::unique_ptr< int > ptr
Foo(Foo &&f)
Definition testQueue.C:70
Foo(const Foo &f)
Definition testQueue.C:61
~Foo()
Definition testQueue.C:105
int * ptr
Definition testQueue.C:42
Foo & operator=(const Foo &f)
Foo(int i)
Definition testQueue.C:55
int g_count
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
int g_count
Definition testQueue.C:38
ArrayQueue< T > create_queue(int n)
Definition testQueue.C:141
void print(const ArrayQueue< T > &q)
Definition testQueue.C:125
Circular queue implementations backed by arrays.