to_s
to compare thingsThe comparaison call to_s
on object and use the result to order things.
var a = [1, 2, 3, 10, 20]
(new CachedAlphaComparator).sort(a)
assert a == [1, 10, 2, 20, 3]
Internally the result of to_s
is cached in a HashMap to counter
uneficient implementation of to_s
.
Note: it caching is not usefull, see alpha_comparator
core $ CachedAlphaComparator :: SELF
Type of this instance, automatically specialized in every classcore :: Comparator :: bubble_sort
Bubble-sortarray
between from
and to
indices
core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
core :: Comparator :: defaultinit
core :: Object :: defaultinit
core :: Comparator :: insertion_sort
Insertion-sortarray
between from
and to
indices
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 :: Comparator :: merge_sort
Merge-sortarray
between from
and to
indices
core :: Object :: output_class_name
Display class name on stdout (debug only).core :: Comparator :: quick_sort
Quick-sortarray
between from
and to
indices
# Comparator that efficienlty use `to_s` to compare things
#
# The comparaison call `to_s` on object and use the result to order things.
#
# ~~~
# var a = [1, 2, 3, 10, 20]
# (new CachedAlphaComparator).sort(a)
# assert a == [1, 10, 2, 20, 3]
# ~~~
#
# Internally the result of `to_s` is cached in a HashMap to counter
# uneficient implementation of `to_s`.
#
# Note: it caching is not usefull, see `alpha_comparator`
class CachedAlphaComparator
super Comparator
redef type COMPARED: Object
private var cache = new HashMap[Object, String]
private fun do_to_s(a: Object): String do
if cache.has_key(a) then return cache[a]
var res = a.to_s
cache[a] = res
return res
end
redef fun compare(a, b) do
return do_to_s(a) <=> do_to_s(b)
end
end
lib/core/text/abstract_text.nit:2464,1--2494,3