@@ -18,9 +18,26 @@ export # not exported by Base
18
18
DirectOrdering,
19
19
lt, ord, ordtype
20
20
21
+ """
22
+ Base.Order.Ordering
23
+
24
+ Abstract type which represents a total order on some set of elements.
25
+
26
+ Use [`Base.Order.lt`](@ref) to compare two elements according to the ordering.
27
+ """
21
28
abstract type Ordering end
22
29
23
30
struct ForwardOrdering <: Ordering end
31
+
32
+ """
33
+ ReverseOrdering(fwd::Ordering=Forward)
34
+
35
+ A wrapper which reverses an ordering.
36
+
37
+ For a given `Ordering` `o`, the following holds for all `a`, `b`:
38
+
39
+ lt(ReverseOrdering(o), a, b) == lt(o, b, a)
40
+ """
24
41
struct ReverseOrdering{Fwd<: Ordering } <: Ordering
25
42
fwd:: Fwd
26
43
end
@@ -31,9 +48,26 @@ ReverseOrdering() = ReverseOrdering(ForwardOrdering())
31
48
32
49
const DirectOrdering = Union{ForwardOrdering,ReverseOrdering{ForwardOrdering}}
33
50
51
+ """
52
+ Base.Order.Forward
53
+
54
+ Default ordering according to [`isless`](@ref).
55
+ """
34
56
const Forward = ForwardOrdering ()
57
+
58
+ """
59
+ Base.Order.Reverse
60
+
61
+ Reverse ordering according to [`isless`](@ref).
62
+ """
35
63
const Reverse = ReverseOrdering ()
36
64
65
+ """
66
+ By(by, order::Ordering=Forward)
67
+
68
+ `Ordering` which applies `order` to elements after they have been transformed
69
+ by the function `by`.
70
+ """
37
71
struct By{T, O} <: Ordering
38
72
by:: T
39
73
order:: O
42
76
# backwards compatibility with VERSION < v"1.5-"
43
77
By (by) = By (by, Forward)
44
78
79
+ """
80
+ Lt(lt)
81
+
82
+ `Ordering` which calls `lt(a, b)` to compare elements. `lt` should
83
+ obey the same rules as implementations of [`isless`](@ref).
84
+ """
45
85
struct Lt{T} <: Ordering
46
86
lt:: T
47
87
end
48
88
89
+ """
90
+ Perm(order::Ordering, data::AbstractVector)
91
+
92
+ `Ordering` on the indices of `data` where `i` is less than `j` if `data[i]` is
93
+ less than `data[j]` according to `order`. In the case that `data[i]` and
94
+ `data[j]` are equal, `i` and `j` are compared by numeric value.
95
+ """
49
96
struct Perm{O<: Ordering ,V<: AbstractVector } <: Ordering
50
97
order:: O
51
98
data:: V
54
101
ReverseOrdering (by:: By ) = By (by. by, ReverseOrdering (by. order))
55
102
ReverseOrdering (perm:: Perm ) = Perm (ReverseOrdering (perm. order), perm. data)
56
103
104
+ """
105
+ lt(o::Ordering, a, b)
106
+
107
+ Test whether `a` is less than `b` according to the ordering `o`.
108
+ """
57
109
lt (o:: ForwardOrdering , a, b) = isless (a,b)
58
110
lt (o:: ReverseOrdering , a, b) = lt (o. fwd,b,a)
59
111
lt (o:: By , a, b) = lt (o. order,o. by (a),o. by (b))
@@ -78,6 +130,22 @@ function _ord(lt, by, order::Ordering)
78
130
end
79
131
end
80
132
133
+ """
134
+ ord(lt, by, rev::Union{Bool, Nothing}, order::Ordering=Forward)
135
+
136
+ Construct an [`Ordering`](@ref) object from the same arguments used by
137
+ [`sort!`](@ref).
138
+ Elements are first transformed by the function `by` (which may be
139
+ [`identity`](@ref)) and are then compared according to either the function `lt`
140
+ or an existing ordering `order`. `lt` should be [`isless`](@ref) or a function
141
+ which obeys similar rules. Finally, the resulting order is reversed if
142
+ `rev=true`.
143
+
144
+ Passing an `lt` other than `isless` along with an `order` other than
145
+ [`Base.Order.Forward`](@ref) or [`Base.Order.Reverse`](@ref) is not permitted,
146
+ otherwise all options are independent and can be used together in all possible
147
+ combinations.
148
+ """
81
149
ord (lt, by, rev:: Nothing , order:: Ordering = Forward) = _ord (lt, by, order)
82
150
83
151
function ord (lt, by, rev:: Bool , order:: Ordering = Forward)
0 commit comments