var bests = new BestDistance[String](5)
bests.update(10, "Too big")
assert bests.best_items.is_empty
bests.update(5, "Just fine")
bests.update(5, "Another one")
assert bests.best_items.has_exactly(["Just fine", "Another one"])
bests.update(2, "A better one")
bests.update(4, "Not good enough")
assert bests.best_distance == 2
assert bests.best_items.has_exactly(["A better one"])
more_collections :: BestDistance :: best_distance=
Current smallest distancemore_collections :: BestDistance :: best_items
Known elements with the smallest distancemore_collections :: BestDistance :: best_items=
Known elements with the smallest distancemore_collections :: BestDistance :: update
Register acandidate
with a distance
more_collections $ BestDistance :: SELF
Type of this instance, automatically specialized in every classmore_collections $ BestDistance :: core_serialize_to
Actual serialization ofself
to serializer
more_collections $ BestDistance :: from_deserializer
Create an instance of this class from thedeserializer
serialization :: Serializable :: accept_json_serializer
Refinable service to customize the serialization of this class to JSONserialization :: Serializable :: accept_msgpack_attribute_counter
Hook to customize the behavior of theAttributeCounter
serialization :: Serializable :: accept_msgpack_serializer
Hook to customize the serialization of this class to MessagePackserialization :: Serializable :: add_to_bundle
Called by[]=
to dynamically choose the appropriate method according
more_collections :: BestDistance :: best_distance=
Current smallest distancemore_collections :: BestDistance :: best_items
Known elements with the smallest distancemore_collections :: BestDistance :: best_items=
Known elements with the smallest distancecore :: Object :: class_factory
Implementation used byget_class
to create the specific class.
serialization :: Serializable :: core_serialize_to
Actual serialization ofself
to serializer
core :: Object :: defaultinit
serialization :: Serializable :: from_deserializer
Create an instance of this class from thedeserializer
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.
serialization :: Serializable :: msgpack_extra_array_items
Hook to request a larger than usual metadata arraycore :: Object :: output_class_name
Display class name on stdout (debug only).serialization :: Serializable :: serialize_msgpack
Serializeself
to MessagePack bytes
serialization :: Serializable :: serialize_to
Serializeself
to serializer
serialization :: Serializable :: serialize_to_json
Serializeself
to JSON
serialization :: Serializable :: to_pretty_json
Serializeself
to plain pretty JSON
more_collections :: BestDistance :: update
Register acandidate
with a distance
Serializer::serialize
# Keep track of the best elements according to a distance value.
#
# ~~~
# var bests = new BestDistance[String](5)
# bests.update(10, "Too big")
# assert bests.best_items.is_empty
# bests.update(5, "Just fine")
# bests.update(5, "Another one")
# assert bests.best_items.has_exactly(["Just fine", "Another one"])
# bests.update(2, "A better one")
# bests.update(4, "Not good enough")
# assert bests.best_distance == 2
# assert bests.best_items.has_exactly(["A better one"])
# ~~~
class BestDistance[E]
# Current smallest distance
var best_distance: Int is writable
# Known elements with the smallest distance
var best_items = new Set[E] is writable
# Register a `candidate` with a `distance`
#
# * To high, it is ignored.
# * Equal to the current best, it is added
# * Better that them, is is the new best element
#
# Return `true` if the candidate is kept (alone or with other)
# returns `false` if the candidate is ignored.
fun update(distance: Int, candidate: E): Bool
do
if distance > best_distance then return false
if distance < best_distance then
best_distance = distance
best_items.clear
end
best_items.add candidate
return true
end
end
lib/more_collections/more_collections.nit:680,1--719,3