key
-> item
.core :: MapRead :: defaultinit
core :: MapRead :: filter_keys
Return all elements ofkeys
that have a value.
core :: MapRead :: get_or_default
Get the item atkey
or return default
if not in map
core :: MapRead :: get_or_null
Get the item atkey
or null if key
is not in the map.
core :: MapRead :: keys_sorted_by_values
Return an array of all keys sorted with their values usingcomparator
.
core :: MapRead :: lookup_all_values
Search all the values inpe.greaters
.
core :: MapRead :: lookup_values
Combine the values inpe.greaters
from the most smaller elements that have a value.
core :: MapRead :: provide_default_value
Called by the underling implementation of[]
to provide a default value when a key
has no value
core :: MapRead :: to_map_comparator
A comparator that compares things with their values in self.core :: MapRead :: values_sorted_by_key
Return an array of all values sorted with their keys usingcomparator
.
core :: Object :: class_factory
Implementation used byget_class
to create the specific class.
core :: Object :: defaultinit
core :: MapRead :: defaultinit
core :: MapRead :: filter_keys
Return all elements ofkeys
that have a value.
core :: MapRead :: get_or_default
Get the item atkey
or return default
if not in map
core :: MapRead :: get_or_null
Get the item atkey
or null if key
is not in the map.
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 :: MapRead :: keys_sorted_by_values
Return an array of all keys sorted with their values usingcomparator
.
core :: MapRead :: lookup_all_values
Search all the values inpe.greaters
.
core :: MapRead :: lookup_values
Combine the values inpe.greaters
from the most smaller elements that have a value.
core :: Object :: output_class_name
Display class name on stdout (debug only).core :: MapRead :: provide_default_value
Called by the underling implementation of[]
to provide a default value when a key
has no value
core :: MapRead :: to_map_comparator
A comparator that compares things with their values in self.core :: MapRead :: values_sorted_by_key
Return an array of all values sorted with their keys usingcomparator
.
json :: JsonMapRead
A map that can be translated into a JSON object.dot :: AttributeMap
Map of graph/node/edge attribute that can be rendered to dot.more_collections :: DefaultMap
A map with a default value.more_collections :: MultiHashMap
Simple way to store anHashMap[K, Array[V]]
ShaderVariable
instances by their name
serialization :: StrictHashMap
Maps instances to a value, usesis_same_serialized
and serialization_hash
.
# MapRead are abstract associative collections: `key` -> `item`.
interface MapRead[K, V]
# Get the item at `key`
#
# var x = new HashMap[String, Int]
# x["four"] = 4
# assert x["four"] == 4
# # assert x["five"] #=> abort
#
# If the key is not in the map, `provide_default_value` is called (that aborts by default)
# See `get_or_null` and `get_or_default` for safe variations.
fun [](key: nullable Object): V is abstract
# Get the item at `key` or null if `key` is not in the map.
#
# var x = new HashMap[String, Int]
# x["four"] = 4
# assert x.get_or_null("four") == 4
# assert x.get_or_null("five") == null
#
# Note: use `has_key` and `[]` if you need the distinction between a key associated with null, and no key.
fun get_or_null(key: nullable Object): nullable V
do
if has_key(key) then return self[key]
return null
end
# Get the item at `key` or return `default` if not in map
#
# var x = new HashMap[String, Int]
# x["four"] = 4
# assert x.get_or_default("four", 40) == 4
# assert x.get_or_default("five", 50) == 50
#
fun get_or_default(key: nullable Object, default: V): V
do
if has_key(key) then return self[key]
return default
end
# Is there an item associated with `key`?
#
# var x = new HashMap[String, Int]
# x["four"] = 4
# assert x.has_key("four") == true
# assert x.has_key("five") == false
#
# By default it is a synonymous to `keys.has` but could be redefined with a direct implementation.
fun has_key(key: nullable Object): Bool do return self.keys.has(key)
# Get a new iterator on the map.
fun iterator: MapIterator[K, V] is abstract
# Return the point of view of self on the values only.
# Note that `self` and `values` are views on the same data;
# therefore any modification of one is visible on the other.
#
# var x = new HashMap[String, Int]
# x["four"] = 4
# assert x.values.has(4) == true
# assert x.values.has(5) == false
fun values: Collection[V] is abstract
# Return the point of view of self on the keys only.
# Note that `self` and `keys` are views on the same data;
# therefore any modification of one is visible on the other.
#
# var x = new HashMap[String, Int]
# x["four"] = 4
# assert x.keys.has("four") == true
# assert x.keys.has("five") == false
fun keys: Collection[K] is abstract
# Is there no item in the collection?
#
# var x = new HashMap[String, Int]
# assert x.is_empty == true
# x["four"] = 4
# assert x.is_empty == false
fun is_empty: Bool is abstract
# Alias for `not is_empty`.
#
# Some people prefer to have conditions grammatically easier to read.
#
# var map = new HashMap[String, Int]
# assert map.not_empty == false
# map["one"] = 1
# assert map.not_empty == true
fun not_empty: Bool do return not self.is_empty
# Number of items in the collection.
#
# var x = new HashMap[String, Int]
# assert x.length == 0
# x["four"] = 4
# assert x.length == 1
# x["five"] = 5
# assert x.length == 2
fun length: Int is abstract
# Called by the underling implementation of `[]` to provide a default value when a `key` has no value
# By default the behavior is to abort.
#
# Note: the value is returned *as is*, implementations may want to store the value in the map before returning it
# @toimplement
protected fun provide_default_value(key: nullable Object): V do abort
# Does `self` and `other` have the same keys associated with the same values?
#
# ~~~
# var a = new HashMap[String, Int]
# var b = new ArrayMap[Object, Numeric]
# assert a == b
# a["one"] = 1
# assert a != b
# b["one"] = 1
# assert a == b
# b["one"] = 2
# assert a != b
# ~~~
redef fun ==(other)
do
if not other isa MapRead[nullable Object, nullable Object] then return false
if other.length != self.length then return false
for k, v in self do
if not other.has_key(k) then return false
if other[k] != v then return false
end
return true
end
# A hashcode based on the hashcode of the keys and the values.
#
# ~~~
# var a = new HashMap[String, Int]
# var b = new ArrayMap[Object, Numeric]
# a["one"] = 1
# b["one"] = 1
# assert a.hash == b.hash
# ~~~
redef fun hash
do
var res = length
for k, v in self do
if k != null then res += k.hash * 7
if v != null then res += v.hash * 11
end
return res
end
end
lib/core/collection/abstract_collection.nit:532,1--682,3
redef class MapRead[K,V]
# Return an array of all values sorted with their keys using `comparator`.
#
# ~~~
# var map = new HashMap[Int, String]
# map[10] = "ten"
# map[2] = "two"
# map[1] = "one"
# assert map.values_sorted_by_key(default_comparator) == ["one", "two", "ten"]
# assert map.values_sorted_by_key(alpha_comparator) == ["one", "ten", "two"]
# ~~~
fun values_sorted_by_key(comparator: Comparator): Array[V]
do
var keys = self.keys.to_a
comparator.sort(keys)
return [for k in keys do self[k]]
end
# Return an array of all keys sorted with their values using `comparator`.
#
# ~~~
# var map = new HashMap[String, Int]
# map["ten"] = 10
# map["two"] = 2
# map["one"] = 1
# assert map.keys_sorted_by_values(default_comparator) == ["one", "two", "ten"]
# assert map.keys_sorted_by_values(alpha_comparator) == ["one", "ten", "two"]
# ~~~
#
# See: `to_map_comparator` to get the comparator used internally.
fun keys_sorted_by_values(comparator: Comparator): Array[K]
do
var keys = self.keys.to_a
var map_cmp = to_map_comparator(comparator)
map_cmp.sort(keys)
return keys
end
# A comparator that compares things with their values in self.
#
# See `MapComparator` for details.
fun to_map_comparator(comparator: Comparator): MapComparator[K, V] do return new MapComparator[K,V](self, comparator)
end
lib/core/collection/sorter.nit:262,1--304,3
redef class MapRead[K, V]
# Return all elements of `keys` that have a value.
#
# ~~~
# var map = new Map[String, String]
# map["A"] = "a"
# map["B"] = "b"
# map["C"] = "c"
#
# assert map.filter_keys(["B"]) == ["B"]
# assert map.filter_keys(["A", "Z", "C"]) == ["A", "C"]
# assert map.filter_keys(["X", "Y", "Z"]).is_empty
# ~~~
#
# `has_key` is used to filter.
fun filter_keys(keys: Collection[nullable Object]): Array[K]
do
var res = new Array[K]
for e in keys do
if has_key(e) then res.add e
end
return res
end
# Search all the values in `pe.greaters`.
#
# Elements without values are ignored.
#
# Basically, values defined in all greater elements of `pe` are inherited.
#
# ~~~
# var pos = new POSet[String]
# pos.add_chain(["E", "D", "C", "B", "A"])
# pos.add_chain(["D", "X", "B"])
#
# var map = new HashMap[String, String]
# map["A"] = "a"
# map["C"] = "c"
# map["X"] = "x"
# map["E"] = "e"
#
# assert map.lookup_all_values(pos["B"]).has_exactly(["a"])
# assert map.lookup_all_values(pos["C"]).has_exactly(["a", "c"])
# assert map.lookup_all_values(pos["D"]).has_exactly(["a", "c", "x"])
# ~~~
fun lookup_all_values(pe: POSetElement[K]): Set[V]
do
var res = new Set[V]
for k in filter_keys(pe.greaters) do res.add self[k]
return res
end
# Combine the values in `pe.greaters` from the most smaller elements that have a value.
#
# Elements without values are ignored.
#
# Basically, values defined in nearest greater elements of `pe` are inherited.
#
# ~~~
# var pos = new POSet[String]
# pos.add_chain(["E", "D", "C", "B", "A"])
# pos.add_chain(["D", "X", "B"])
#
# var map = new HashMap[String, String]
# map["A"] = "a"
# map["C"] = "c"
# map["X"] = "x"
# map["E"] = "e"
#
# assert map.lookup_values(pos["B"]).has_exactly(["a"])
# assert map.lookup_values(pos["C"]).has_exactly(["c"])
# assert map.lookup_values(pos["D"]).has_exactly(["c", "x"])
# ~~~
fun lookup_values(pe: POSetElement[K]): Set[V]
do
var res = new Set[V]
for k in pe.poset.select_smallest(filter_keys(pe.greaters)) do res.add self[k]
return res
end
end
lib/poset/poset.nit:733,1--812,3