1
1
/+ +
2
2
This is a submodule of $(MREF mir,ndslice).
3
3
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
+
4
13
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
5
14
Copyright: Andrei Alexandrescu 2008-2016, Ilya Yaroshenko 2016-,
6
15
Authors: Ilya Yaroshenko, Andrei Alexandrescu
16
+
17
+ Macros:
18
+ SUBREF = $(REF_ALTTEXT $(TT $2), $2, mir, ndslice, $1)$(NBSP)
7
19
+/
8
20
module mir.ndslice.sorting ;
9
21
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
+
10
46
import mir.ndslice.slice;
11
47
import mir.internal.utility;
12
48
13
49
@fastmath:
14
50
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
+ ` )
30
55
template isSorted (alias less = " !(a >= b)" )
31
56
{
32
57
import mir.functional: naryFun;
33
58
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
- +/
40
59
@fastmath bool isSorted(SliceKind kind, size_t [] packs, Iterator)
41
60
(Slice! (kind, packs, Iterator) slice)
42
61
if (packs.length == 1 )
43
62
{
44
- import mir.functional: reverseArgs;
63
+ import mir.functional: reverseArgs, not ;
45
64
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;
48
67
}
49
68
else
50
69
alias isSorted = .isSorted! (naryFun! less);
51
70
}
52
71
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
+ ` )
54
76
template isStrictlyMonotonic (alias less = " a < b" )
55
77
{
56
78
import mir.functional: naryFun;
57
79
static if (__traits(isSame, naryFun! less, less))
58
- // /
59
80
@fastmath bool isStrictlyMonotonic(SliceKind kind, size_t [] packs, Iterator)
60
81
(Slice! (kind, packs, Iterator) slice)
61
82
if (packs.length == 1 )
62
83
{
63
84
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;
66
87
}
67
88
else
68
89
alias isStrictlyMonotonic = .isStrictlyMonotonic! (naryFun! less);
69
90
}
70
91
71
-
72
- // /
73
92
unittest
74
93
{
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;
78
96
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
- {
90
97
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 );
94
101
auto b = [1 , 3 , 2 ].sliced;
95
- assert (! isSorted(b) );
102
+ assert (! b.pairwise ! " a <= b " .all );
96
103
97
104
// ignores duplicates
98
105
auto c = [1 , 1 , 2 ].sliced;
99
- assert (isSorted(c) );
106
+ assert (c.pairwise ! " a <= b " .all );
100
107
}
101
108
102
109
unittest
103
110
{
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);
109
119
}
110
120
111
121
@@ -130,13 +140,16 @@ template sort(alias less = "a < b")
130
140
// /
131
141
unittest
132
142
{
143
+ import mir.ndslice.algorithm: all;
133
144
import mir.ndslice.slice;
145
+ import mir.ndslice.sorting: sort;
146
+ import mir.ndslice.topology: pairwise;
134
147
135
148
int [10 ] arr = [7 ,1 ,3 ,2 ,9 ,0 ,5 ,4 ,8 ,6 ];
136
149
137
150
auto data = arr[].ptr.sliced(arr.length);
138
151
data.sort();
139
- assert (data.isSorted );
152
+ assert (data.pairwise ! " a <= b " .all );
140
153
}
141
154
142
155
void quickSortImpl (alias less, Iterator)(Slice! (Contiguous, [1 ], Iterator) slice)
0 commit comments