core :: Comparable
In order to work, the method '<' has to be redefined.
core $ Comparable :: SELF
Type of this instance, automatically specialized in every classcore :: Object :: class_factory
Implementation used byget_class
to create the specific class.
core :: Comparable :: defaultinit
core :: Object :: defaultinit
core :: Object :: is_same_instance
Return true ifself
and other
are the same instance (i.e. same identity).
core :: Object :: is_same_serialized
Isself
the same as other
in a serialization context?
core :: Object :: is_same_type
Return true ifself
and other
have the same dynamic type.
core :: Object :: native_class_name
The class name of the object in CString format.core :: Object :: output_class_name
Display class name on stdout (debug only).Tree
implementation
trees :: RBTreeNode
RedBlackTree node (can be red or black)
# The ancestor of class where objects are in a total order.
# In order to work, the method '<' has to be redefined.
interface Comparable
# What `self` can be compared to?
type OTHER: Comparable
# Is `self` lesser than `other`?
fun <(other: OTHER): Bool is abstract
# not `other` < `self`
# Note, the implementation must ensure that: `(x<=y) == (x<y or x==y)`
fun <=(other: OTHER): Bool do return not other < self
# not `self` < `other`
# Note, the implementation must ensure that: `(x>=y) == (x>y or x==y)`
fun >=(other: OTHER): Bool do return not self < other
# `other` < `self`
fun >(other: OTHER): Bool do return other < self
# -1 if <, +1 if > and 0 otherwise
# Note, the implementation must ensure that: (x<=>y == 0) == (x==y)
fun <=>(other: OTHER): Int
do
if self < other then
return -1
else if other < self then
return 1
else
return 0
end
end
# c <= self <= d
fun is_between(c: OTHER, d: OTHER): Bool
do
return c <= self and self <= d
end
# The maximum between `self` and `other` (prefers `self` if equals).
fun max(other: OTHER): OTHER
do
if self < other then
return other
else
return self
end
end
# The minimum between `self` and `c` (prefer `self` if equals)
fun min(c: OTHER): OTHER
do
if c < self then
return c
else
return self
end
end
end
lib/core/kernel.nit:313,1--371,3
redef class Comparable
# Constraint `self` within `[min..max]`
#
# assert 1.clamp(5, 10) == 5
# assert 7.clamp(5, 10) == 7
# assert 15.clamp(5, 10) == 10
# assert 1.5.clamp(1.0, 2.0) == 1.5
# assert "a".clamp("b", "c") == "b"
fun clamp(min, max: OTHER): OTHER do return self.max(min).min(max)
end
lib/core/math.nit:423,1--432,3