Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_algorithm_sorting.d

raw
1 @safe unittest
2 {
3 import std.algorithm.sorting;
4
5 import std.range : assumeSorted;
6 int[] a = [ 1, 2, 3 ];
7 int[] b = [ 4, 0, 6, 5 ];
8 completeSort(assumeSorted(a), b);
9 assert(a == [ 0, 1, 2 ]);
10 assert(b == [ 3, 4, 5, 6 ]);
11 }
12
13 @safe unittest
14 {
15 import std.algorithm.sorting;
16
17 assert([1, 1, 2].isSorted);
18 // strictly monotonic doesn't allow duplicates
19 assert(![1, 1, 2].isStrictlyMonotonic);
20
21 int[] arr = [4, 3, 2, 1];
22 assert(!isSorted(arr));
23 assert(!isStrictlyMonotonic(arr));
24
25 assert(isSorted!"a > b"(arr));
26 assert(isStrictlyMonotonic!"a > b"(arr));
27
28 sort(arr);
29 assert(isSorted(arr));
30 assert(isStrictlyMonotonic(arr));
31 }
32
33 @safe unittest
34 {
35 import std.algorithm.sorting;
36
37 assert(ordered(42, 42, 43));
38 assert(!strictlyOrdered(43, 42, 45));
39 assert(ordered(42, 42, 43));
40 assert(!strictlyOrdered(42, 42, 43));
41 assert(!ordered(43, 42, 45));
42 // Ordered lexicographically
43 assert(ordered("Jane", "Jim", "Joe"));
44 assert(strictlyOrdered("Jane", "Jim", "Joe"));
45 // Incidentally also ordered by length decreasing
46 assert(ordered!((a, b) => a.length > b.length)("Jane", "Jim", "Joe"));
47 // ... but not strictly so: "Jim" and "Joe" have the same length
48 assert(!strictlyOrdered!((a, b) => a.length > b.length)("Jane", "Jim", "Joe"));
49 }
50
51 @safe unittest
52 {
53 import std.algorithm.sorting;
54
55 import std.algorithm.mutation : SwapStrategy;
56 import std.algorithm.searching : count, find;
57 import std.conv : text;
58 import std.range.primitives : empty;
59
60 auto Arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
61 auto arr = Arr.dup;
62 static bool even(int a) { return (a & 1) == 0; }
63 // Partition arr such that even numbers come first
64 auto r = partition!(even)(arr);
65 // Now arr is separated in evens and odds.
66 // Numbers may have become shuffled due to instability
67 assert(r == arr[5 .. $]);
68 assert(count!(even)(arr[0 .. 5]) == 5);
69 assert(find!(even)(r).empty);
70
71 // Can also specify the predicate as a string.
72 // Use 'a' as the predicate argument name
73 arr[] = Arr[];
74 r = partition!(q{(a & 1) == 0})(arr);
75 assert(r == arr[5 .. $]);
76
77 // Now for a stable partition:
78 arr[] = Arr[];
79 r = partition!(q{(a & 1) == 0}, SwapStrategy.stable)(arr);
80 // Now arr is [2 4 6 8 10 1 3 5 7 9], and r points to 1
81 assert(arr == [2, 4, 6, 8, 10, 1, 3, 5, 7, 9] && r == arr[5 .. $]);
82
83 // In case the predicate needs to hold its own state, use a delegate:
84 arr[] = Arr[];
85 int x = 3;
86 // Put stuff greater than 3 on the left
87 bool fun(int a) { return a > x; }
88 r = partition!(fun, SwapStrategy.semistable)(arr);
89 // Now arr is [4 5 6 7 8 9 10 2 3 1] and r points to 2
90 assert(arr == [4, 5, 6, 7, 8, 9, 10, 2, 3, 1] && r == arr[7 .. $]);
91 }
92
93 @safe nothrow unittest
94 {
95 import std.algorithm.sorting;
96
97 int[] a = [5, 3, 2, 6, 4, 1, 3, 7];
98 size_t pivot = pivotPartition(a, a.length / 2);
99 import std.algorithm.searching : all;
100 assert(a[0 .. pivot].all!(x => x <= a[pivot]));
101 assert(a[pivot .. $].all!(x => x >= a[pivot]));
102 }
103
104 @safe unittest
105 {
106 import std.algorithm.sorting;
107
108 int[] r = [ 1, 3, 5, 7, 8, 2, 4, ];
109 assert(isPartitioned!"a & 1"(r));
110 }
111
112 @safe unittest
113 {
114 import std.algorithm.sorting;
115
116 auto a = [ 8, 3, 4, 1, 4, 7, 4 ];
117 auto pieces = partition3(a, 4);
118 assert(pieces[0] == [ 1, 3 ]);
119 assert(pieces[1] == [ 4, 4, 4 ]);
120 assert(pieces[2] == [ 8, 7 ]);
121 }
122
123 @system unittest
124 {
125 import std.algorithm.sorting;
126
127 immutable(int[]) arr = [ 2, 3, 1, 5, 0 ];
128 // index using pointers
129 auto index1 = new immutable(int)*[arr.length];
130 makeIndex!("a < b")(arr, index1);
131 assert(isSorted!("*a < *b")(index1));
132 // index using offsets
133 auto index2 = new size_t[arr.length];
134 makeIndex!("a < b")(arr, index2);
135 assert(isSorted!
136 ((size_t a, size_t b){ return arr[a] < arr[b];})
137 (index2));
138 }
139
140 @safe pure nothrow unittest
141 {
142 import std.algorithm.sorting;
143
144 import std.algorithm.comparison : equal;
145 import std.range : retro;
146
147 int[] a = [1, 3, 5];
148 int[] b = [2, 3, 4];
149
150 assert(a.merge(b).equal([1, 2, 3, 3, 4, 5]));
151 assert(a.merge(b).retro.equal([5, 4, 3, 3, 2, 1]));
152 }
153
154 @safe pure nothrow unittest
155 {
156 import std.algorithm.sorting;
157
158 import std.algorithm.comparison : equal;
159 import std.range : retro;
160 import std.traits : CommonType;
161
162 alias S = short;
163 alias I = int;
164 alias D = double;
165
166 S[] a = [1, 2, 3];
167 I[] b = [50, 60];
168 D[] c = [10, 20, 30, 40];
169
170 auto m = merge(a, b, c);
171
172 static assert(is(typeof(m.front) == CommonType!(S, I, D)));
173
174 assert(equal(m, [1, 2, 3, 10, 20, 30, 40, 50, 60]));
175 assert(equal(m.retro, [60, 50, 40, 30, 20, 10, 3, 2, 1]));
176
177 m.popFront();
178 assert(equal(m, [2, 3, 10, 20, 30, 40, 50, 60]));
179 m.popBack();
180 assert(equal(m, [2, 3, 10, 20, 30, 40, 50]));
181 m.popFront();
182 assert(equal(m, [3, 10, 20, 30, 40, 50]));
183 m.popBack();
184 assert(equal(m, [3, 10, 20, 30, 40]));
185 m.popFront();
186 assert(equal(m, [10, 20, 30, 40]));
187 m.popBack();
188 assert(equal(m, [10, 20, 30]));
189 m.popFront();
190 assert(equal(m, [20, 30]));
191 m.popBack();
192 assert(equal(m, [20]));
193 m.popFront();
194 assert(m.empty);
195 }
196
197 @safe unittest
198 {
199 import std.algorithm.sorting;
200
201 import std.algorithm.mutation : SwapStrategy;
202 static struct Point { int x, y; }
203 auto pts1 = [ Point(0, 0), Point(5, 5), Point(0, 1), Point(0, 2) ];
204 auto pts2 = [ Point(0, 0), Point(0, 1), Point(0, 2), Point(5, 5) ];
205 multiSort!("a.x < b.x", "a.y < b.y", SwapStrategy.unstable)(pts1);
206 assert(pts1 == pts2);
207 }
208
209 @safe pure nothrow unittest
210 {
211 import std.algorithm.sorting;
212
213 int[] array = [ 1, 2, 3, 4 ];
214
215 // sort in descending order
216 array.sort!("a > b");
217 assert(array == [ 4, 3, 2, 1 ]);
218
219 // sort in ascending order
220 array.sort();
221 assert(array == [ 1, 2, 3, 4 ]);
222
223 // sort with reusable comparator and chain
224 alias myComp = (x, y) => x > y;
225 assert(array.sort!(myComp).release == [ 4, 3, 2, 1 ]);
226 }
227
228 @safe unittest
229 {
230 import std.algorithm.sorting;
231
232 // Showcase stable sorting
233 import std.algorithm.mutation : SwapStrategy;
234 string[] words = [ "aBc", "a", "abc", "b", "ABC", "c" ];
235 sort!("toUpper(a) < toUpper(b)", SwapStrategy.stable)(words);
236 assert(words == [ "a", "aBc", "abc", "ABC", "b", "c" ]);
237 }
238
239 @safe unittest
240 {
241 import std.algorithm.sorting;
242
243 // Sorting floating-point numbers in presence of NaN
244 double[] numbers = [-0.0, 3.0, -2.0, double.nan, 0.0, -double.nan];
245
246 import std.algorithm.comparison : equal;
247 import std.math.operations : cmp;
248 import std.math.traits : isIdentical;
249
250 sort!((a, b) => cmp(a, b) < 0)(numbers);
251
252 double[] sorted = [-double.nan, -2.0, -0.0, 0.0, 3.0, double.nan];
253 assert(numbers.equal!isIdentical(sorted));
254 }
255
256 @safe pure unittest
257 {
258 import std.algorithm.sorting;
259
260 import std.algorithm.iteration : map;
261 import std.numeric : entropy;
262
263 auto lowEnt = [ 1.0, 0, 0 ],
264 midEnt = [ 0.1, 0.1, 0.8 ],
265 highEnt = [ 0.31, 0.29, 0.4 ];
266 auto arr = new double[][3];
267 arr[0] = midEnt;
268 arr[1] = lowEnt;
269 arr[2] = highEnt;
270
271 schwartzSort!(entropy, "a > b")(arr);
272
273 assert(arr[0] == highEnt);
274 assert(arr[1] == midEnt);
275 assert(arr[2] == lowEnt);
276 assert(isSorted!("a > b")(map!(entropy)(arr)));
277 }
278
279 @system unittest
280 {
281 import std.algorithm.sorting;
282
283 int[] a = [ 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 ];
284 partialSort(a, 5);
285 assert(a[0 .. 5] == [ 0, 1, 2, 3, 4 ]);
286 }
287
288 @system unittest
289 {
290 import std.algorithm.sorting;
291
292 int[] a = [5, 7, 2, 6, 7];
293 int[] b = [2, 1, 5, 6, 7, 3, 0];
294
295 partialSort(a, b);
296 assert(a == [0, 1, 2, 2, 3]);
297 }
298
299 @safe unittest
300 {
301 import std.algorithm.sorting;
302
303 int[] v = [ 25, 7, 9, 2, 0, 5, 21 ];
304 topN!"a < b"(v, 100);
305 assert(v == [ 25, 7, 9, 2, 0, 5, 21 ]);
306 auto n = 4;
307 topN!((a, b) => a < b)(v, n);
308 assert(v[n] == 9);
309 }
310
311 @system unittest
312 {
313 import std.algorithm.sorting;
314
315 int[] a = [ 5, 7, 2, 6, 7 ];
316 int[] b = [ 2, 1, 5, 6, 7, 3, 0 ];
317 topN(a, b);
318 sort(a);
319 assert(a == [0, 1, 2, 2, 3]);
320 }
321
322 @system unittest
323 {
324 import std.algorithm.sorting;
325
326 import std.typecons : Yes;
327
328 int[] a = [ 10, 16, 2, 3, 1, 5, 0 ];
329 int[] b = new int[3];
330 topNCopy(a, b, Yes.sortOutput);
331 assert(b == [ 0, 1, 2 ]);
332 }
333
334 @system unittest
335 {
336 import std.algorithm.sorting;
337
338 import std.typecons : Yes;
339
340 // Construct index to top 3 elements using numerical indices:
341 int[] a = [ 10, 2, 7, 5, 8, 1 ];
342 int[] index = new int[3];
343 topNIndex(a, index, Yes.sortOutput);
344 assert(index == [5, 1, 3]); // because a[5]==1, a[1]==2, a[3]==5
345
346 // Construct index to top 3 elements using pointer indices:
347 int*[] ptrIndex = new int*[3];
348 topNIndex(a, ptrIndex, Yes.sortOutput);
349 assert(ptrIndex == [ &a[5], &a[1], &a[3] ]);
350 }
351
352 @safe unittest
353 {
354 import std.algorithm.sorting;
355
356 // Step through all permutations of a sorted array in lexicographic order
357 int[] a = [1,2,3];
358 assert(nextPermutation(a) == true);
359 assert(a == [1,3,2]);
360 assert(nextPermutation(a) == true);
361 assert(a == [2,1,3]);
362 assert(nextPermutation(a) == true);
363 assert(a == [2,3,1]);
364 assert(nextPermutation(a) == true);
365 assert(a == [3,1,2]);
366 assert(nextPermutation(a) == true);
367 assert(a == [3,2,1]);
368 assert(nextPermutation(a) == false);
369 assert(a == [1,2,3]);
370 }
371
372 @safe unittest
373 {
374 import std.algorithm.sorting;
375
376 // Step through permutations of an array containing duplicate elements:
377 int[] a = [1,1,2];
378 assert(nextPermutation(a) == true);
379 assert(a == [1,2,1]);
380 assert(nextPermutation(a) == true);
381 assert(a == [2,1,1]);
382 assert(nextPermutation(a) == false);
383 assert(a == [1,1,2]);
384 }
385
386 @safe unittest
387 {
388 import std.algorithm.sorting;
389
390 // Step through even permutations of a sorted array in lexicographic order
391 int[] a = [1,2,3];
392 assert(nextEvenPermutation(a) == true);
393 assert(a == [2,3,1]);
394 assert(nextEvenPermutation(a) == true);
395 assert(a == [3,1,2]);
396 assert(nextEvenPermutation(a) == false);
397 assert(a == [1,2,3]);
398 }
399
400 @safe unittest
401 {
402 import std.algorithm.sorting;
403
404 import std.math.algebraic : sqrt;
405
406 // Print the 60 vertices of a uniform truncated icosahedron (soccer ball)
407 enum real Phi = (1.0 + sqrt(5.0)) / 2.0; // Golden ratio
408 real[][] seeds = [
409 [0.0, 1.0, 3.0*Phi],
410 [1.0, 2.0+Phi, 2.0*Phi],
411 [Phi, 2.0, Phi^^3]
412 ];
413 size_t n;
414 foreach (seed; seeds)
415 {
416 // Loop over even permutations of each seed
417 do
418 {
419 // Loop over all sign changes of each permutation
420 size_t i;
421 do
422 {
423 // Generate all possible sign changes
424 for (i=0; i < seed.length; i++)
425 {
426 if (seed[i] != 0.0)
427 {
428 seed[i] = -seed[i];
429 if (seed[i] < 0.0)
430 break;
431 }
432 }
433 n++;
434 } while (i < seed.length);
435 } while (nextEvenPermutation(seed));
436 }
437 assert(n == 60);
438 }
439
440 pure @safe unittest
441 {
442 import std.algorithm.sorting;
443
444 auto src = [0, 1, 2, 3, 4, 5, 6];
445 auto rslt = [4, 0, 6, 2, 1, 3, 5];
446
447 src = nthPermutation(src, 2982);
448 assert(src == rslt);
449 }
450
451 pure @safe unittest
452 {
453 import std.algorithm.sorting;
454
455 auto src = [0, 1, 2, 3, 4, 5, 6];
456 auto rslt = [4, 0, 6, 2, 1, 3, 5];
457
458 bool worked = nthPermutationImpl(src, 2982);
459 assert(worked);
460 assert(src == rslt);
461 }
462