forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ordering.jl
53 lines (40 loc) · 1.33 KB
/
ordering.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
module Order
## notions of element ordering ##
export # not exported by Base
Ordering, Forward, By, Lt, Perm,
ReverseOrdering, ForwardOrdering,
lt, ord
# Reverse, # TODO: clashes with Reverse iterator
abstract Ordering
immutable ForwardOrdering <: Ordering end
immutable ReverseOrdering{Fwd<:Ordering} <: Ordering
fwd::Fwd
end
ReverseOrdering(rev::ReverseOrdering) = rev.fwd
ReverseOrdering{Fwd}(fwd::Fwd) = ReverseOrdering{Fwd}(fwd)
immutable By <: Ordering
by::Function
end
immutable Lt <: Ordering
lt::Function
end
const Forward = ForwardOrdering()
const Reverse = ReverseOrdering(Forward)
lt(o::ForwardOrdering, a, b) = isless(a,b)
lt(o::ReverseOrdering, a, b) = lt(o.fwd,b,a)
lt(o::By, a, b) = isless(o.by(a),o.by(b))
lt(o::Lt, a, b) = o.lt(a,b)
immutable Perm{O<:Ordering,V<:AbstractVector} <: Ordering
order::O
data::V
end
Perm{O<:Ordering,V<:AbstractVector}(o::O,v::V) = Perm{O,V}(o,v)
lt(p::Perm, a, b) = lt(p.order, p.data[a], p.data[b])
function ord(lt::Function, by::Function, rev::Bool, order::Ordering=Forward)
o = (lt===isless) & (by===identity) ? order :
(lt===isless) & (by!==identity) ? By(by) :
(lt!==isless) & (by===identity) ? Lt(lt) :
Lt((x,y)->lt(by(x),by(y)))
rev ? ReverseOrdering(o) : o
end
end