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 # Advanced services for serialization engines
18 import serialization_core
19 intrude import core
::collection
::hash_collection
21 # Maps instances to a value, uses `is_same_serialized` and `serialization_hash`.
22 class StrictHashMap[K
, V
]
27 if k
== null then return 0
29 var i
= k
.serialization_hash
% _capacity
34 redef fun node_at_idx
(i
, k
)
40 if ck
.is_same_serialized
(k
) then
43 c
= c
._next_in_bucklet
49 redef interface Object
50 # Is `self` the same as `other` in a serialization context?
52 # Used to determine if an object has already been serialized.
53 fun is_same_serialized
(other
: nullable Object): Bool do return is_same_instance
(other
)
55 # Hash value use for serialization
57 # Used in combination with `is_same_serialized`. If two objects are the same
58 # in a serialization context, they must have the same `serialization_hash`.
59 fun serialization_hash
: Int do return object_id
63 redef fun serialization_hash
do return hash
64 redef fun is_same_serialized
(o
) do return self == o
69 # Strip the `nullable` prefix from the type name `self`
72 # assert "String".strip_nullable == "String"
73 # assert "nullable Array[Int]".strip_nullable == "Array[Int]"
74 # assert "Map[Set[String], Set[Int]]".strip_nullable == "Map[Set[String], Set[Int]]"
76 fun strip_nullable
: Text
78 var prefix
= "nullable "
79 return if has_prefix
(prefix
) then substring_from
(prefix
.length
) else self
82 # Strip the `nullable` prefix and the params from the type name `self`
85 # assert "String".strip_nullable_and_params == "String"
86 # assert "nullable Array[Int]".strip_nullable_and_params == "Array"
87 # assert "Map[Set[String], Set[Int]]".strip_nullable_and_params == "Map"
89 fun strip_nullable_and_params
: Text
91 var class_name
= strip_nullable
93 var bracket_index
= class_name
.index_of
('[')
94 if bracket_index
== -1 then return class_name
95 return class_name
.substring
(0, bracket_index
)