1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Highly specific, but useful, collections-related classes.
16 module more_collections
18 # Simple way to store an `HashMap[K, Array[V]]`
20 # Unlike standard HashMap, MultiHashMap provides a new
21 # empty array on the first access on a unknown key.
23 # var m = new MultiHashMap[String, Char]
24 # assert not m.has_key("four")
29 # assert m.has_key("four")
30 # assert m["four"] == ['i', 'i', 'i', 'i']
31 # assert m["zzz"] == new Array[Char]
32 class MultiHashMap[K
: Object, V
]
33 super HashMap[K
, Array[V
]]
35 # Add `v` to the array associated with `k`.
36 # If there is no array associated, then create it.
37 fun add_one
(k
: K
, v
: V
)
39 if self.has_key
(k
) then
46 redef fun provide_default_value
(key
) do
47 var res
= new Array[V
]
53 # Simple way to store an `HashMap[K1, HashMap[K2, V]]`
56 # var hm2 = new HashMap2[Int, String, Float]
59 # assert hm2[1, "one"] == 1.0
60 # assert hm2[2, "not-two"] == null
62 class HashMap2[K1: Object, K2: Object, V
]
63 private var level1
= new HashMap[K1, HashMap[K2, V
]]
65 # Return the value associated to the keys `k1` and `k2`.
66 # Return `null` if no such a value.
67 fun [](k1
: K1, k2
: K2): nullable V
69 var level1
= self.level1
70 if not level1
.has_key
(k1
) then return null
71 var level2
= level1
[k1
]
72 if not level2
.has_key
(k2
) then return null
76 # Set `v` the value associated to the keys `k1` and `k2`.
77 fun []=(k1
: K1, k2
: K2, v
: V
)
79 var level1
= self.level1
80 var level2
: HashMap[K2, V
]
81 if not level1
.has_key
(k1
) then
82 level2
= new HashMap[K2, V
]
91 # Simple way to store an `HashMap[K1, HashMap[K2, HashMap[K3, V]]]`
94 # var hm3 = new HashMap3[Int, String, Int, Float]
95 # hm3[1, "one", 11] = 1.0
96 # hm3[2, "two", 22] = 2.0
97 # assert hm3[1, "one", 11] == 1.0
98 # assert hm3[2, "not-two", 22] == null
100 class HashMap3[K1: Object, K2: Object, K3: Object, V
]
101 private var level1
= new HashMap[K1, HashMap2[K2, K3, V
]]
103 # Return the value associated to the keys `k1`, `k2`, and `k3`.
104 # Return `null` if no such a value.
105 fun [](k1
: K1, k2
: K2, k3
: K3): nullable V
107 var level1
= self.level1
108 if not level1
.has_key
(k1
) then return null
109 var level2
= level1
[k1
]
110 return level2
[k2
, k3
]
113 # Set `v` the value associated to the keys `k1`, `k2`, and `k3`.
114 fun []=(k1
: K1, k2
: K2, k3
: K3, v
: V
)
116 var level1
= self.level1
117 var level2
: HashMap2[K2, K3, V
]
118 if not level1
.has_key
(k1
) then
119 level2
= new HashMap2[K2, K3, V
]
128 # A map with a default value.
131 # var dm = new DefaultMap[String, Int](10)
132 # assert dm["a"] == 10
135 # The default value is used when the key is not present.
136 # And getting a default value does not register the key.
139 # assert dm["a"] == 10
140 # assert dm.length == 0
141 # assert dm.has_key("a") == false
144 # It also means that removed key retrieve the default value.
148 # assert dm["a"] == 2
149 # dm.keys.remove("a")
150 # assert dm["a"] == 10
153 # Warning: the default value is used as is, so using mutable object might
154 # cause side-effects.
157 # var dma = new DefaultMap[String, Array[Int]](new Array[Int])
160 # assert dma["a"] == [65]
161 # assert dma.default == [65]
162 # assert dma["c"] == [65]
165 # assert dma["b"] == [65, 66]
166 # assert dma.default == [65]
168 class DefaultMap[K
: Object, V
]
174 redef fun provide_default_value
(key
) do return default