Aleph-w 3.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 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 <gsl/gsl_rng.h>
29# include <cassert>
30# include <cerrno>
31# include <climits>
32# include <cstdlib>
33# include <iostream>
34# include <ahSort.H>
35# include <tpl_sort_utils.H>
36
37# include <ctime>
38using namespace std;
39
41
42template <template <typename T> class List,
43 typename T = long>
45{
47 for (int i = 0; i < n; ++i)
49
50 return ret_val;
51}
52
53template <template <typename T> class List,
54 typename T>
55bool verify_sort(const List<T> & list)
56{
57 T value = numeric_limits<T>::min();
58 return list.all([&value] (const T & item)
59 {
60 bool ret = value <= item;
61 value = item;
62 return ret;
63 });
64}
65
66template <template <typename T> class List,
67 typename T = long>
69{
71 for (int i = 0; i < n; ++i)
73
74 return ret_val;
75}
76
77template <template <typename T> class List,
78 typename T >
79bool verify_sort(const List<T*> & list)
80{
81 T value = numeric_limits<T>::min();
82 return list.all([&value] (T * ptr)
83 {
84 bool ret = value <= *ptr;
85 value = *ptr;
86 return ret;
87 });
88}
89
90template <template <typename T> class List, typename T>
92{
93 list.for_each([] (T * ptr)
94 {
95 delete ptr;
96 });
97}
98
99int main(int argc, char *argv[])
100{
101 unsigned long n = 1000;
102 if (argc > 1)
103 {
104 errno = 0;
105 char * endptr = nullptr;
106 const unsigned long parsed = strtoul(argv[1], &endptr, 10);
107 if (errno != 0 or endptr == argv[1] or *endptr != '\0' or
108 parsed > static_cast<unsigned long>(INT_MAX))
109 {
110 cerr << "Invalid n: must be a non-negative integer <= "
111 << INT_MAX << endl;
112 return 1;
113 }
114 n = parsed;
115 }
116
117 unsigned int t = std::time(NULL);
118 if (argc > 2)
119 {
120 errno = 0;
121 char * endptr = nullptr;
122 const unsigned long parsed_t = strtoul(argv[2], &endptr, 10);
123 if (errno != 0 or endptr == argv[2] or *endptr != '\0' or
125 {
126 cerr << "Invalid t: " << argv[2] << endl;
127 return 1;
128 }
129 t = static_cast<unsigned int>(parsed_t);
130 }
131
132 cout << argv[0] << " " << n << " " << t << endl;
133
135 gsl_rng_set(r, t % gsl_rng_max(r));
136
137 {
138 cout << "Testing quicksort on single lists" << endl
139 << "Building list ... " << endl;
141 cout << "sorting it ..." << endl;
142 quicksort(l);
143 cout << "done! " << endl
144 << "Verifying ... " << endl;
146 assert(l.length() == n);
147 cout << "done!" << endl
148 << endl;
149 }
150
151 {
152 cout << "Testing quicksort on single lists of pointers" << endl
153 << "Building list ... " << endl;
155 cout << "sorting it ..." << endl;
156 quicksort(l, [] (long * x, long * y)
157 {
158 return *x < *y;
159 });
160 cout << "done! " << endl
161 << "Verifying ... " << endl;
163 assert(l.length() == n);
164 cout << "done!" << endl
165 << endl;
167 }
168
169 {
170 cout << "Testing mergesort on single lists" << endl
171 << "Building list ... " << endl;
173 cout << "sorting it ..." << endl;
174 mergesort(l);
175 cout << "done! " << endl
176 << "Verifying ... " << endl;
178 assert(l.length() == n);
179 cout << "done!" << endl
180 << endl;
181 }
182
183 {
184 cout << "Testing mergesort on single lists of pointers" << endl
185 << "Building list ... " << endl;
187 cout << "sorting it ..." << endl;
188 mergesort(l, [] (long * x, long * y)
189 {
190 return *x < *y;
191 });
192 cout << "done! " << endl
193 << "Verifying ... " << endl;
195 assert(l.length() == n);
196 cout << "done!" << endl
197 << endl;
199 }
200
201 {
202 cout << "Testing default sort method on single lists" << endl
203 << "Building list ... " << endl;
205 cout << "sorting it ..." << endl;
206 auto sorted = sort(l);
207 cout << "done! " << endl
208 << "Verifying ... " << endl;
210 assert(sorted.length() == n);
211 cout << "done!" << endl
212 << endl;
213 }
214
215 gsl_rng_free(r);
216 return 0;
217}
High-level sorting functions for Aleph containers.
Node belonging to a double circular linked list with header node.
Definition tpl_dnode.H:106
Dynamic singly linked list with functional programming support.
Definition htlist.H:1155
size_t length() const noexcept
Count the number of elements of a container.
Definition ah-dry.H:1598
static mpfr_t y
Definition mpfr_mul_d.c:3
void mergesort(T *a, const long l, const long r, std::vector< T > &buf, Compare cmp)
Sort an array using merge sort with a reusable buffer.
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
DynArray< T > sort(const DynArray< T > &a, Cmp &&cmp=Cmp())
Returns a sorted copy of a DynArray.
Definition ahSort.H:227
void quicksort(T *a, const long l, const long r, const Compare &cmp=Compare())
Sort an array using iterative quicksort with optimizations.
STL namespace.
Dnode< unsigned > List
Definition testMerge.C:34
gsl_rng * r
List< T > build_int_list(int n)
List< T * > build_ptr_list(int n)
void free_ptr_list(List< T * > &list)
bool verify_sort(const List< T > &list)
Comprehensive sorting algorithms and search utilities for Aleph-w.
DynList< int > l