11/+ +
22This is a submodule of $(MREF mir,ndslice).
33
4+ Note:
5+ The combination of
6+ $(SUBREF topology, pairwise) with lambda `"a <= b"` (`"a < b"`) and $(SUBREF algorithm, all) can be used
7+ to check if an ndslice is sorted (strictly monotonic). See also the examples in the module.
8+
9+ See_also: $(SUBREF topology, flattened)
10+
11+ `isSorted` and `isStrictlyMonotonic`
12+
413License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
514Copyright: Andrei Alexandrescu 2008-2016, Ilya Yaroshenko 2016-,
615Authors: Ilya Yaroshenko, Andrei Alexandrescu
16+
17+ Macros:
18+ SUBREF = $(REF_ALTTEXT $(TT $2), $2, mir, ndslice, $1)$(NBSP)
719+/
820module mir.ndslice.sorting ;
921
22+ // /
23+ unittest
24+ {
25+ import mir.ndslice.algorithm: all;
26+ import mir.ndslice.slice;
27+ import mir.ndslice.sorting: sort;
28+ import mir.ndslice.topology: pairwise;
29+
30+ auto arr = [1 , 1 , 2 ].sliced;
31+
32+ assert (arr.pairwise! " a <= b" .all);
33+ assert (! arr.pairwise! " a < b" .all);
34+
35+ arr = [4 , 3 , 2 , 1 ].sliced;
36+
37+ assert (! arr.pairwise! " a <= b" .all);
38+ assert (! arr.pairwise! " a < b" .all);
39+
40+ sort(arr);
41+
42+ assert (arr.pairwise! " a <= b" .all);
43+ assert (arr.pairwise! " a < b" .all);
44+ }
45+
1046import mir.ndslice.slice;
1147import mir.internal.utility;
1248
1349@fastmath:
1450
15- /+ +
16- Checks whether a slice is sorted according to the comparison
17- operation $(D less). Performs $(BIGOH ndslice.elementsCount) evaluations of `less`.
18- Unlike `isSorted`, $(LREF _isStrictlyMonotonic) does not allow for equal values,
19- i.e. values for which both `less(a, b)` and `less(b, a)` are false.
20- With either function, the predicate must be a strict ordering just like with
21- `isSorted`. For example, using `"a <= b"` instead of `"a < b"` is
22- incorrect and will cause failed assertions.
23-
24- Params:
25- less = Predicate the ndslice should be sorted by.
26- Note:
27- isSorted requires predicates for floating point types looks like `!(cmp_condition)`
28- to return false if the ndslice contains NaNs.
29- +/
51+ deprecated (` Use 'yourSlice.pairwise!"a <= b".all' instead. Imports:
52+ import mir.ndslice.algorithm: all;
53+ import mir.ndslice.topology: pairwise;
54+ ` )
3055template isSorted (alias less = " !(a >= b)" )
3156{
3257 import mir.functional: naryFun;
3358 static if (__traits(isSame, naryFun! less, less))
34- /+ +
35- slice = A slice to check for sortedness.
36- Returns:
37- `true` if the ndslice is sorted, false otherwise. `isSorted` allows
38- duplicates, $(LREF _isStrictlyMonotonic) not.
39- +/
4059 @fastmath bool isSorted(SliceKind kind, size_t [] packs, Iterator)
4160 (Slice! (kind, packs, Iterator) slice)
4261 if (packs.length == 1 )
4362 {
44- import mir.functional: reverseArgs;
63+ import mir.functional: reverseArgs, not ;
4564 import mir.ndslice.algorithm: all;
46- import mir.ndslice.topology: flattened, slide ;
47- return slice.flattened.slide ! ( 2 , reverseArgs! less).all! " !a " ;
65+ import mir.ndslice.topology: flattened, pairwise ;
66+ return slice.flattened.pairwise ! (not ! ( reverseArgs! less)) .all;
4867 }
4968 else
5069 alias isSorted = .isSorted! (naryFun! less);
5170}
5271
53- // / ditto
72+ deprecated (` Use 'yourSlice.pairwise!"a < b".all' instead. Imports:
73+ import mir.ndslice.algorithm: all;
74+ import mir.ndslice.topology: pairwise;
75+ ` )
5476template isStrictlyMonotonic (alias less = " a < b" )
5577{
5678 import mir.functional: naryFun;
5779 static if (__traits(isSame, naryFun! less, less))
58- // /
5980 @fastmath bool isStrictlyMonotonic(SliceKind kind, size_t [] packs, Iterator)
6081 (Slice! (kind, packs, Iterator) slice)
6182 if (packs.length == 1 )
6283 {
6384 import mir.ndslice.algorithm: all;
64- import mir.ndslice.topology: flattened, slide ;
65- return slice.flattened.slide ! ( 2 , less) .all! " a " ;
85+ import mir.ndslice.topology: flattened, pairwise ;
86+ return slice.flattened.pairwise ! less.all;
6687 }
6788 else
6889 alias isStrictlyMonotonic = .isStrictlyMonotonic! (naryFun! less);
6990}
7091
71-
72- // /
7392unittest
7493{
75- assert ([1 , 1 , 2 ].sliced.isSorted);
76- // strictly monotonic doesn't allow duplicates
77- assert (! [1 , 1 , 2 ].sliced.isStrictlyMonotonic);
94+ import mir.ndslice.algorithm: all;
95+ import mir.ndslice.topology: pairwise;
7896
79- auto arr = [4 , 3 , 2 , 1 ].sliced;
80- assert (! isSorted(arr));
81- assert (! isStrictlyMonotonic(arr));
82-
83- sort(arr);
84- assert (isSorted(arr));
85- assert (isStrictlyMonotonic(arr));
86- }
87-
88- unittest
89- {
9097 auto a = [1 , 2 , 3 ].sliced;
91- assert (isSorted( a[0 .. 0 ]) );
92- assert (isSorted( a[0 .. 1 ]) );
93- assert (isSorted(a) );
98+ assert (a[0 .. 0 ].pairwise ! " a <= b " .all );
99+ assert (a[0 .. 1 ].pairwise ! " a <= b " .all );
100+ assert (a.pairwise ! " a <= b " .all );
94101 auto b = [1 , 3 , 2 ].sliced;
95- assert (! isSorted(b) );
102+ assert (! b.pairwise ! " a <= b " .all );
96103
97104 // ignores duplicates
98105 auto c = [1 , 1 , 2 ].sliced;
99- assert (isSorted(c) );
106+ assert (c.pairwise ! " a <= b " .all );
100107}
101108
102109unittest
103110{
104- assert ([1 , 2 , 3 ][0 .. 0 ].sliced.isStrictlyMonotonic);
105- assert ([1 , 2 , 3 ][0 .. 1 ].sliced.isStrictlyMonotonic);
106- assert ([1 , 2 , 3 ].sliced.isStrictlyMonotonic);
107- assert (! [1 , 3 , 2 ].sliced.isStrictlyMonotonic);
108- assert (! [1 , 1 , 2 ].sliced.isStrictlyMonotonic);
111+ import mir.ndslice.algorithm: all;
112+ import mir.ndslice.topology: pairwise;
113+
114+ assert ([1 , 2 , 3 ][0 .. 0 ].sliced.pairwise! " a < b" .all);
115+ assert ([1 , 2 , 3 ][0 .. 1 ].sliced.pairwise! " a < b" .all);
116+ assert ([1 , 2 , 3 ].sliced.pairwise! " a < b" .all);
117+ assert (! [1 , 3 , 2 ].sliced.pairwise! " a < b" .all);
118+ assert (! [1 , 1 , 2 ].sliced.pairwise! " a < b" .all);
109119}
110120
111121
@@ -130,13 +140,16 @@ template sort(alias less = "a < b")
130140// /
131141unittest
132142{
143+ import mir.ndslice.algorithm: all;
133144 import mir.ndslice.slice;
145+ import mir.ndslice.sorting: sort;
146+ import mir.ndslice.topology: pairwise;
134147
135148 int [10 ] arr = [7 ,1 ,3 ,2 ,9 ,0 ,5 ,4 ,8 ,6 ];
136149
137150 auto data = arr[].ptr.sliced(arr.length);
138151 data.sort();
139- assert (data.isSorted );
152+ assert (data.pairwise ! " a <= b " .all );
140153}
141154
142155void quickSortImpl (alias less, Iterator)(Slice! (Contiguous, [1 ], Iterator) slice)
0 commit comments