forked from crystal-lang/crystal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomparable.cr
121 lines (113 loc) · 3.88 KB
/
comparable.cr
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# The `Comparable` mixin is used by classes whose objects may be ordered.
#
# Including types must provide an `<=>` method, which compares the receiver against
# another object, returning:
# - a negative number if `self` is less than the other object
# - a positive number if `self` is greater than the other object
# - `0` if `self` is equal to the other object
# - `nil` if `self` and the other object are not comparable
#
# `Comparable` uses `<=>` to implement the conventional comparison operators
# (`<`, `<=`, `==`, `>=`, and `>`). All of these return `false` when `<=>`
# returns `nil`.
#
# Note that returning `nil` is only useful when defining a partial comparable
# relationship. One such example is float values: they are generally comparable,
# except for `NaN`. If none of the values of a type are comparable between each
# other, `Comparable` shouldn't be included.
#
# NOTE: When `nil` is returned from `<=>`, `Array#sort` and related sorting
# methods will perform slightly slower.
module Comparable(T)
# Compares this object to *other* based on the receiver’s `<=>` method,
# returning `true` if it returns a negative number.
def <(other : T) : Bool
cmp = self <=> other
cmp ? cmp < 0 : false
end
# Compares this object to *other* based on the receiver’s `<=>` method,
# returning `true` if it returns a value equal or less then `0`.
def <=(other : T)
cmp = self <=> other
cmp ? cmp <= 0 : false
end
# Compares this object to *other* based on the receiver’s `<=>` method,
# returning `true` if it returns `0`.
#
# Also returns `true` if this and *other* are the same object.
def ==(other : T)
if self.is_a?(Reference)
# Need to do two different comparisons because the compiler doesn't yet
# restrict something like `other.is_a?(Reference) || other.is_a?(Nil)`.
# See #2461
return true if other.is_a?(Reference) && self.same?(other)
return true if other.is_a?(Nil) && self.same?(other)
end
cmp = self <=> other
cmp ? cmp == 0 : false
end
# Compares this object to *other* based on the receiver’s `<=>` method,
# returning `true` if it returns a value greater then `0`.
def >(other : T) : Bool
cmp = self <=> other
cmp ? cmp > 0 : false
end
# Compares this object to *other* based on the receiver’s `<=>` method,
# returning `true` if it returns a value equal or greater than `0`.
def >=(other : T)
cmp = self <=> other
cmp ? cmp >= 0 : false
end
# The comparison operator. Returns `0` if the two objects are equal,
# a negative number if this object is considered less than *other*,
# a positive number if this object is considered greater than *other*,
# or `nil` if the two objects are not comparable.
#
# Subclasses define this method to provide class-specific ordering.
#
# The comparison operator is usually used to sort values:
#
# ```
# # Sort in a descending way:
# [3, 1, 2].sort { |x, y| y <=> x } # => [3, 2, 1]
#
# # Sort in an ascending way:
# [3, 1, 2].sort { |x, y| x <=> y } # => [1, 2, 3]
# ```
abstract def <=>(other : T)
# Clamps a value within *range*.
#
# ```
# 5.clamp(10..100) # => 10
# 50.clamp(10..100) # => 50
# 500.clamp(10..100) # => 100
#
# 5.clamp(10..) # => 10
# 50.clamp(10..) # => 50
#
# 5.clamp(..10) # => 5
# 50.clamp(..10) # => 10
# ```
def clamp(range : Range)
raise ArgumentError.new("Can't clamp an exclusive range") if !range.end.nil? && range.exclusive?
clamp range.begin, range.end
end
# Clamps a value between *min* and *max*.
#
# ```
# 5.clamp(10, 100) # => 10
# 50.clamp(10, 100) # => 50
# 500.clamp(10, 100) # => 100
#
# 5.clamp(10, nil) # => 10
# 50.clamp(10, nil) # => 50
#
# 5.clamp(nil, 10) # => 5
# 50.clamp(nil, 10) # => 10
# ```
def clamp(min, max)
return max if !max.nil? && self > max
return min if !min.nil? && self < min
self
end
end