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
20 # Simple way to store an `HashMap[K, Array[V]]`
22 # Unlike standard HashMap, MultiHashMap provides a new
23 # empty array on the first access on a unknown key.
25 # var m = new MultiHashMap[String, Char]
26 # assert not m.has_key("four")
31 # assert m.has_key("four")
32 # assert m["four"] == ['i', 'i', 'i', 'i']
33 # assert m["zzz"] == new Array[Char]
34 class MultiHashMap[K
, V
]
36 super HashMap[K
, Array[V
]]
38 # Add `v` to the array associated with `k`.
39 # If there is no array associated, then create it.
40 fun add_one
(k
: K
, v
: V
)
42 var x
= self.get_or_null
(k
)
50 redef fun provide_default_value
(key
) do
51 var res
= new Array[V
]
57 # Simple way to store an `HashMap[K1, HashMap[K2, V]]`
60 # var hm2 = new HashMap2[Int, String, Float]
63 # assert hm2[1, "one"] == 1.0
64 # assert hm2[2, "not-two"] == null
66 class HashMap2[K1, K2, V
]
69 private var level1
= new HashMap[K1, HashMap[K2, V
]]
71 # Return the value associated to the keys `k1` and `k2`.
72 # Return `null` if no such a value.
73 fun [](k1
: K1, k2
: K2): nullable V
75 var level1
= self.level1
76 var level2
= level1
.get_or_null
(k1
)
77 if level2
== null then return null
78 return level2
.get_or_null
(k2
)
81 # Set `v` the value associated to the keys `k1` and `k2`.
82 fun []=(k1
: K1, k2
: K2, v
: V
)
84 var level1
= self.level1
85 var level2
= level1
.get_or_null
(k1
)
86 if level2
== null then
87 level2
= new HashMap[K2, V
]
93 # Remove the item at `k1` and `k2`
94 fun remove_at
(k1
: K1, k2
: K2)
96 var level1
= self.level1
97 var level2
= level1
.get_or_null
(k1
)
98 if level2
== null then return
99 level2
.keys
.remove
(k2
)
103 fun clear
do level1
.clear
106 # Simple way to store an `HashMap[K1, HashMap[K2, HashMap[K3, V]]]`
109 # var hm3 = new HashMap3[Int, String, Int, Float]
110 # hm3[1, "one", 11] = 1.0
111 # hm3[2, "two", 22] = 2.0
112 # assert hm3[1, "one", 11] == 1.0
113 # assert hm3[2, "not-two", 22] == null
115 class HashMap3[K1, K2, K3, V
]
118 private var level1
= new HashMap[K1, HashMap2[K2, K3, V
]]
120 # Return the value associated to the keys `k1`, `k2`, and `k3`.
121 # Return `null` if no such a value.
122 fun [](k1
: K1, k2
: K2, k3
: K3): nullable V
124 var level1
= self.level1
125 var level2
= level1
.get_or_null
(k1
)
126 if level2
== null then return null
127 return level2
[k2
, k3
]
130 # Set `v` the value associated to the keys `k1`, `k2`, and `k3`.
131 fun []=(k1
: K1, k2
: K2, k3
: K3, v
: V
)
133 var level1
= self.level1
134 var level2
= level1
.get_or_null
(k1
)
135 if level2
== null then
136 level2
= new HashMap2[K2, K3, V
]
142 # Remove the item at `k1`, `k2` and `k3`
143 fun remove_at
(k1
: K1, k2
: K2, k3
: K3)
145 var level1
= self.level1
146 var level2
= level1
.get_or_null
(k1
)
147 if level2
== null then return
148 level2
.remove_at
(k2
, k3
)
152 fun clear
do level1
.clear
155 # A map with a default value.
158 # var dm = new DefaultMap[String, Int](10)
159 # assert dm["a"] == 10
162 # The default value is used when the key is not present.
163 # And getting a default value does not register the key.
166 # assert dm["a"] == 10
167 # assert dm.length == 0
168 # assert dm.has_key("a") == false
171 # It also means that removed key retrieve the default value.
175 # assert dm["a"] == 2
176 # dm.keys.remove("a")
177 # assert dm["a"] == 10
180 # Warning: the default value is used as is, so using mutable object might
181 # cause side-effects.
184 # var dma = new DefaultMap[String, Array[Int]](new Array[Int])
187 # assert dma["a"] == [65]
188 # assert dma.default == [65]
189 # assert dma["c"] == [65]
192 # assert dma["b"] == [65, 66]
193 # assert dma.default == [65]
195 class DefaultMap[K
, V
]
202 redef fun provide_default_value
(key
) do return default