Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
test_sort_lists.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 <ah-random.H>
34# include <cassert>
35# include <cerrno>
36# include <climits>
37# include <cstdlib>
38# include <iostream>
39# include <ahSort.H>
40# include <tpl_sort_utils.H>
41
42# include <ctime>
43using namespace std;
44
46
47template <template <typename T> class List,
48 typename T = long>
50{
51 List<T> ret_val;
52 for (int i = 0; i < n; ++i)
53 ret_val.append(r.get());
54
55 return ret_val;
56}
57
58template <template <typename T> class List,
59 typename T>
60bool verify_sort(const List<T> & list)
61{
62 T value = numeric_limits<T>::min();
63 return list.all([&value] (const T & item)
64 {
65 bool ret = value <= item;
66 value = item;
67 return ret;
68 });
69}
70
71template <template <typename T> class List,
72 typename T = long>
74{
75 List<T*> ret_val;
76 for (int i = 0; i < n; ++i)
77 ret_val.append(new T(r.get()));
78
79 return ret_val;
80}
81
82template <template <typename T> class List,
83 typename T >
84bool verify_sort(const List<T*> & list)
85{
86 T value = numeric_limits<T>::min();
87 return list.all([&value] (T * ptr)
88 {
89 bool ret = value <= *ptr;
90 value = *ptr;
91 return ret;
92 });
93}
94
95template <template <typename T> class List, typename T>
97{
98 list.for_each([] (T * ptr)
99 {
100 delete ptr;
101 });
102}
103
104int main(int argc, char *argv[])
105{
106 unsigned long n = 1000;
107 if (argc > 1)
108 {
109 errno = 0;
110 char * endptr = nullptr;
111 const unsigned long parsed = strtoul(argv[1], &endptr, 10);
112 if (errno != 0 or endptr == argv[1] or *endptr != '\0' or
113 parsed > static_cast<unsigned long>(INT_MAX))
114 {
115 cerr << "Invalid n: must be a non-negative integer <= "
116 << INT_MAX << endl;
117 return 1;
118 }
119 n = parsed;
120 }
121
122 unsigned int t = std::time(NULL);
123 if (argc > 2)
124 {
125 errno = 0;
126 char * endptr = nullptr;
127 const unsigned long parsed_t = strtoul(argv[2], &endptr, 10);
128 if (errno != 0 or endptr == argv[2] or *endptr != '\0' or
129 parsed_t > UINT_MAX)
130 {
131 cerr << "Invalid t: " << argv[2] << endl;
132 return 1;
133 }
134 t = static_cast<unsigned int>(parsed_t);
135 }
136
137 cout << argv[0] << " " << n << " " << t << endl;
138
140
141 {
142 cout << "Testing quicksort on single lists" << endl
143 << "Building list ... " << endl;
144 DynList<long> l = build_int_list<DynList>(n);
145 cout << "sorting it ..." << endl;
146 quicksort(l);
147 cout << "done! " << endl
148 << "Verifying ... " << endl;
149 assert(verify_sort(l));
150 assert(l.length() == n);
151 cout << "done!" << endl
152 << endl;
153 }
154
155 {
156 cout << "Testing quicksort on single lists of pointers" << endl
157 << "Building list ... " << endl;
158 DynList<long*> l = build_ptr_list<DynList>(n);
159 cout << "sorting it ..." << endl;
160 quicksort(l, [] (long * x, long * y)
161 {
162 return *x < *y;
163 });
164 cout << "done! " << endl
165 << "Verifying ... " << endl;
166 assert(verify_sort(l));
167 assert(l.length() == n);
168 cout << "done!" << endl
169 << endl;
171 }
172
173 {
174 cout << "Testing mergesort on single lists" << endl
175 << "Building list ... " << endl;
176 DynList<long> l = build_int_list<DynList>(n);
177 cout << "sorting it ..." << endl;
178 mergesort(l);
179 cout << "done! " << endl
180 << "Verifying ... " << endl;
181 assert(verify_sort(l));
182 assert(l.length() == n);
183 cout << "done!" << endl
184 << endl;
185 }
186
187 {
188 cout << "Testing mergesort on single lists of pointers" << endl
189 << "Building list ... " << endl;
190 DynList<long*> l = build_ptr_list<DynList>(n);
191 cout << "sorting it ..." << endl;
192 mergesort(l, [] (long * x, long * y)
193 {
194 return *x < *y;
195 });
196 cout << "done! " << endl
197 << "Verifying ... " << endl;
198 assert(verify_sort(l));
199 assert(l.length() == n);
200 cout << "done!" << endl
201 << endl;
203 }
204
205 {
206 cout << "Testing default sort method on single lists" << endl
207 << "Building list ... " << endl;
208 DynList<long> l = build_int_list<DynList>(n);
209 cout << "sorting it ..." << endl;
210 auto sorted = sort(l);
211 cout << "done! " << endl
212 << "Verifying ... " << endl;
213 assert(verify_sort(sorted));
214 assert(sorted.length() == n);
215 cout << "done!" << endl
216 << endl;
217 }
218
219 return 0;
220}
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
High-level sorting functions for Aleph containers.
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
Node belonging to a double circular linked list with header node.
Definition tpl_dnode.H:106
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
A random number generator with the sequences of GSL's gsl_rng_mt19937.
Definition ah-random.H:147
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^32 - 1.
Definition ah-random.H:259
void seed(const unsigned long s) noexcept
Seeds again, as gsl_rng_set(r, s).
Definition ah-random.H:277
unsigned long get() noexcept
The next number, as operator()().
Definition ah-random.H:289
size_t length() const noexcept(Aleph::dry_detail::const_traversal_is_noexcept< Container, T >)
Count the number of elements of a container.
Definition ah-dry.H:2654
static mpfr_t y
Definition mpfr_mul_d.c:3
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
DynArray< T > sort(const DynArray< T > &a, Cmp &&cmp=Cmp())
Returns a sorted copy of a DynArray.
Definition ahSort.H:234
void quicksort(T *a, const long l, const long r, const Compare &cmp=Compare())
Sort an array using iterative quicksort with optimizations.
void mergesort(T *a, const long l, const long r, Array< T > &buf, Compare cmp)
Sort an array using merge sort with a reusable buffer.
STL namespace.
Dnode< unsigned > List
Definition testMerge.C:39
List< T > build_int_list(int n)
List< T * > build_ptr_list(int n)
void free_ptr_list(List< T * > &list)
Gsl_Compatible_Rng r
bool verify_sort(const List< T > &list)
Comprehensive sorting algorithms and search utilities for Aleph-w.
DynList< int > l