more_collections: Add `DefaultMap`.
[nit.git] / lib / more_collections.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Highly specific, but useful, collections-related classes.
16 module more_collections
17
18 # Simple way to store an `HashMap[K, Array[V]]`
19 #
20 # Unlike standard HashMap, MultiHashMap provides a new
21 # empty array on the first access on a unknown key.
22 #
23 # var m = new MultiHashMap[String, Char]
24 # assert not m.has_key("four")
25 # m["four"].add('i')
26 # m["four"].add('i')
27 # m["four"].add('i')
28 # m["four"].add('i')
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]]
34
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)
38 do
39 if self.has_key(k) then
40 self[k].add(v)
41 else
42 self[k] = [v]
43 end
44 end
45
46 redef fun provide_default_value(key) do
47 var res = new Array[V]
48 self[key] = res
49 return res
50 end
51
52 init do end
53 end
54
55 # Simple way to store an `HashMap[K1, HashMap[K2, V]]`
56 class HashMap2[K1: Object, K2: Object, V]
57 private var level1: HashMap[K1, HashMap[K2, V]] = new HashMap[K1, HashMap[K2, V]]
58
59 # Return the value associated to the keys `k1` and `k2`.
60 # Return `null` if no such a value.
61 fun [](k1: K1, k2: K2): nullable V
62 do
63 var level1 = self.level1
64 if not level1.has_key(k1) then return null
65 var level2 = level1[k1]
66 if not level2.has_key(k2) then return null
67 return level2[k2]
68 end
69
70 # Set `v` the value associated to the keys `k1` and `k2`.
71 fun []=(k1: K1, k2: K2, v: V)
72 do
73 var level1 = self.level1
74 var level2: HashMap[K2, V]
75 if not level1.has_key(k1) then
76 level2 = new HashMap[K2, V]
77 level1[k1] = level2
78 else
79 level2 = level1[k1]
80 end
81 level2[k2] = v
82 end
83 end
84
85 # Simple way to store an `HashMap[K1, HashMap[K2, HashMap[K3, V]]]`
86 class HashMap3[K1: Object, K2: Object, K3: Object, V]
87 private var level1: HashMap[K1, HashMap2[K2, K3, V]] = new HashMap[K1, HashMap2[K2, K3, V]]
88
89 # Return the value associated to the keys `k1`, `k2`, and `k3`.
90 # Return `null` if no such a value.
91 fun [](k1: K1, k2: K2, k3: K3): nullable V
92 do
93 var level1 = self.level1
94 if not level1.has_key(k1) then return null
95 var level2 = level1[k1]
96 return level2[k2, k3]
97 end
98
99 # Set `v` the value associated to the keys `k1`, `k2`, and `k3`.
100 fun []=(k1: K1, k2: K2, k3: K3, v: V)
101 do
102 var level1 = self.level1
103 var level2: HashMap2[K2, K3, V]
104 if not level1.has_key(k1) then
105 level2 = new HashMap2[K2, K3, V]
106 level1[k1] = level2
107 else
108 level2 = level1[k1]
109 end
110 level2[k2, k3] = v
111 end
112 end
113
114 # A map with a default value.
115 class DefaultMap[K: Object, V]
116 super HashMap[K, V]
117
118 # The default value.
119 var default: V
120
121 redef fun provide_default_value(key) do return default
122 end