tests: add alt to base_init_autoinit3 to check warning on `noautoinit`
[nit.git] / lib / serialization / engine_tools.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 # Advanced services for serialization engines
16 module engine_tools
17
18 import serialization
19
20 # Maps instances to a value, uses `is_same_instance`
21 #
22 # Warning: This class does not implement all the services from `Map`.
23 class StrictHashMap[K, V]
24 super Map[K, V]
25
26 # private
27 var map = new HashMap[K, Array[Couple[K, V]]]
28
29 redef var length = 0
30
31 redef fun is_empty do return length == 0
32
33 # private
34 fun node_at(key: K): nullable Couple[K, V]
35 do
36 if not map.keys.has(key) then return null
37
38 var arr = map[key]
39 for couple in arr do
40 if couple.first.is_same_serialized(key) then
41 return couple
42 end
43 end
44
45 return null
46 end
47
48 redef fun [](key)
49 do
50 var node = node_at(key)
51 assert node != null
52 return node.second
53 end
54
55 redef fun []=(key, value)
56 do
57 var node = node_at(key)
58 if node != null then
59 node.second = value
60 return
61 end
62
63 var arr
64 if not map.keys.has(key) then
65 arr = new Array[Couple[K, V]]
66 map[key] = arr
67 else arr = map[key]
68
69 arr.add new Couple[K, V](key, value)
70 self.length += 1
71 end
72
73 redef fun has_key(key) do return node_at(key) != null
74 end