value
at key
.Values can then get retrieved with []
.
var x = new HashMap[String, Int]
x["four"] = 4
assert x["four"] == 4
If the key was associated with a value, this old value is discarded and replaced with the new one.
x["four"] = 40
assert x["four"] == 40
assert x.values.has(4) == false
# Set the `value` at `key`.
#
# Values can then get retrieved with `[]`.
#
# var x = new HashMap[String, Int]
# x["four"] = 4
# assert x["four"] == 4
#
# If the key was associated with a value, this old value is discarded
# and replaced with the new one.
#
# x["four"] = 40
# assert x["four"] == 40
# assert x.values.has(4) == false
#
fun []=(key: K, value: V) is abstract
lib/core/collection/abstract_collection.nit:711,2--726,38
redef fun []=(key, value) do
var children = root.children
for i in [0..key.length[ do
var c = key.chars[i]
var node
if children.has_key(c) then
node = children[c]
else
node = new TrieNode[E](c)
children[c] = node
end
children = node.children
if i == key.length - 1 then
node.is_leaf = true
node.value = value
end
end
end
lib/trees/trie.nit:71,2--91,4
# Insert a new node in tree using `key` and `item`
# O(n) in worst case, average is O(h) with h: tree height
#
# var tree = new BinTreeMap[Int, String]
# tree[1] = "n1"
# assert tree.max == "n1"
# tree[3] = "n3"
# assert tree.max == "n3"
redef fun []=(key, item) do
insert_node(new BinTreeNode[K, E](key, item))
end
lib/trees/bintree.nit:144,2--154,4
redef fun []=(key, item) do
insert_node(new RBTreeNode[K, E](key, item))
end
lib/trees/rbtree.nit:51,2--53,4
# Set the `value` for the property locaated at `key`
#
# ~~~
# var ini = new IniFile
# ini["key"] = "value1"
# ini["section1.key"] = "value2"
# ini["section2.key"] = "value3"
#
# assert ini["key"] == "value1"
# assert ini["section1.key"] == "value2"
# assert ini["section2.key"] == "value3"
# assert ini.section("section1").name == "section1"
# assert ini.section("section2")["key"] == "value3"
# ~~~
redef fun []=(key, value) do
if value == null then return
var parts = key.split_once_on(".")
# No dot notation, store value in root
if parts.length == 1 then
super(key.trim, value.trim)
return
end
# First part matches a section, store value in it
var section = self.section(parts.first.trim)
if section != null then
section[parts.last.trim] = value.trim
return
end
# No section matched, create a new one and store value in it
section = new IniSection(parts.first.trim)
section[parts.last.trim] = value.trim
sections.add section
end
lib/ini/ini.nit:235,2--270,4