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
is serialize
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
]
35 super HashMap[K
, Array[V
]]
37 # Add `v` to the array associated with `k`.
38 # If there is no array associated, then create it.
39 fun add_one
(k
: K
, v
: V
)
41 var x
= self.get_or_null
(k
)
49 redef fun provide_default_value
(key
) do
50 var res
= new Array[V
]
56 # Simple way to store an `HashMap[K1, HashMap[K2, V]]`
59 # var hm2 = new HashMap2[Int, String, Float]
62 # assert hm2[1, "one"] == 1.0
63 # assert hm2[2, "not-two"] == null
65 class HashMap2[K1, K2, V
]
67 private var level1
= new HashMap[K1, HashMap[K2, V
]]
69 # Return the value associated to the keys `k1` and `k2`.
70 # Return `null` if no such a value.
71 fun [](k1
: K1, k2
: K2): nullable V
73 var level1
= self.level1
74 var level2
= level1
.get_or_null
(k1
)
75 if level2
== null then return null
76 return level2
.get_or_null
(k2
)
79 # Set `v` the value associated to the keys `k1` and `k2`.
80 fun []=(k1
: K1, k2
: K2, v
: V
)
82 var level1
= self.level1
83 var level2
= level1
.get_or_null
(k1
)
84 if level2
== null then
85 level2
= new HashMap[K2, V
]
91 # Remove the item at `k1` and `k2`
92 fun remove_at
(k1
: K1, k2
: K2)
94 var level1
= self.level1
95 var level2
= level1
.get_or_null
(k1
)
96 if level2
== null then return
97 level2
.keys
.remove
(k2
)
101 fun clear
do level1
.clear
104 # Simple way to store an `HashMap[K1, HashMap[K2, HashMap[K3, V]]]`
107 # var hm3 = new HashMap3[Int, String, Int, Float]
108 # hm3[1, "one", 11] = 1.0
109 # hm3[2, "two", 22] = 2.0
110 # assert hm3[1, "one", 11] == 1.0
111 # assert hm3[2, "not-two", 22] == null
113 class HashMap3[K1, K2, K3, V
]
115 private var level1
= new HashMap[K1, HashMap2[K2, K3, V
]]
117 # Return the value associated to the keys `k1`, `k2`, and `k3`.
118 # Return `null` if no such a value.
119 fun [](k1
: K1, k2
: K2, k3
: K3): nullable V
121 var level1
= self.level1
122 var level2
= level1
.get_or_null
(k1
)
123 if level2
== null then return null
124 return level2
[k2
, k3
]
127 # Set `v` the value associated to the keys `k1`, `k2`, and `k3`.
128 fun []=(k1
: K1, k2
: K2, k3
: K3, v
: V
)
130 var level1
= self.level1
131 var level2
= level1
.get_or_null
(k1
)
132 if level2
== null then
133 level2
= new HashMap2[K2, K3, V
]
139 # Remove the item at `k1`, `k2` and `k3`
140 fun remove_at
(k1
: K1, k2
: K2, k3
: K3)
142 var level1
= self.level1
143 var level2
= level1
.get_or_null
(k1
)
144 if level2
== null then return
145 level2
.remove_at
(k2
, k3
)
149 fun clear
do level1
.clear
152 # A map with a default value.
155 # var dm = new DefaultMap[String, Int](10)
156 # assert dm["a"] == 10
159 # The default value is used when the key is not present.
160 # And getting a default value does not register the key.
163 # assert dm["a"] == 10
164 # assert dm.length == 0
165 # assert dm.has_key("a") == false
168 # It also means that removed key retrieve the default value.
172 # assert dm["a"] == 2
173 # dm.keys.remove("a")
174 # assert dm["a"] == 10
177 # Warning: the default value is used as is, so using mutable object might
178 # cause side-effects.
181 # var dma = new DefaultMap[String, Array[Int]](new Array[Int])
184 # assert dma["a"] == [65]
185 # assert dma.default == [65]
186 # assert dma["c"] == [65]
189 # assert dma["b"] == [65, 66]
190 # assert dma.default == [65]
192 class DefaultMap[K
, V
]
198 redef fun provide_default_value
(key
) do return default
201 # An unrolled linked list
203 # A sequence implemented as a linked list of arrays.
205 # This data structure is similar to the `List` but it can benefit from
206 # better cache performance, lower data overhead for the nodes metadata and
207 # it spares the GC to allocate many small nodes.
208 class UnrolledList[E
]
211 # Desired capacity for each nodes
213 # By default at `32`, it can be increased for very large lists.
215 # It can be modified anytime, but newly created nodes may still be assigned
216 # the same capacity of old nodes when created by `insert`.
217 var nodes_length
= 32 is writable
219 private var head_node
: UnrolledNode[E
] = new UnrolledNode[E
](nodes_length
)
221 private var tail_node
: UnrolledNode[E
] = head_node
227 head_node
= new UnrolledNode[E
](nodes_length
)
228 tail_node
= head_node
232 # Out parameter of `node_at`
233 private var index_within_node
= 0
235 private fun node_at
(index
: Int): UnrolledNode[E
]
237 assert index
>= 0 and index
< length
240 while index
>= node
.length
do
242 node
= node
.next
.as(not null)
245 index_within_node
= index
249 private fun insert_node
(node
: UnrolledNode[E
], prev
, next
: nullable UnrolledNode[E
])
253 else head_node
= node
257 else tail_node
= node
265 var node
= node_at
(index
)
266 index
= index_within_node
+ node
.head_index
267 return node
.items
[index
]
270 redef fun []=(index
, value
)
272 var node
= node_at
(index
)
273 index
= index_within_node
+ node
.head_index
274 node
.items
[index
] = value
280 if not node
.full
then
281 if node
.tail_index
< node
.capacity
then
282 # There's room at the tail
285 # Move everything over by `d`
286 assert node
.head_index
> 0
287 var d
= node
.head_index
/ 2 + 1
288 node
.move_head
(node
.length
, d
)
289 for i
in d
.times
do node
.items
[node
.tail_index
- i
] = null
291 node
.tail_index
+= -d
+1
293 node
.items
[node
.tail_index-1
] = item
296 node
= new UnrolledNode[E
](nodes_length
)
297 insert_node
(node
, tail_node
, null)
304 redef fun unshift
(item
)
307 if not node
.full
then
308 if node
.head_index
> 0 then
309 # There's room at the head
312 # Move everything over by `d`
313 assert node
.tail_index
< node
.capacity
314 var d
= (node
.capacity-node
.tail_index
) / 2 + 1
316 for i
in d
.times
do node
.items
[node
.head_index
+i
] = null
317 node
.head_index
+= d-1
320 node
.items
[node
.head_index
] = item
323 node
= new UnrolledNode[E
](nodes_length
)
324 insert_node
(node
, null, head_node
)
325 node
.head_index
= node
.capacity-1
326 node
.tail_index
= node
.capacity
327 node
.items
[node
.capacity-1
] = item
337 while node
.length
== 0 do
339 var nullable_node
= node
.prev
340 assert is_not_empty
: nullable_node
!= null
343 self.tail_node
= node
346 var item
= node
.items
[node
.tail_index-1
]
357 while node
.length
== 0 do
359 var nullable_node
= node
.next
360 assert is_not_empty
: nullable_node
!= null
363 self.head_node
= node
366 var item
= node
.items
[node
.head_index
]
372 redef fun insert
(item
, index
)
374 if index
== length
then
379 var node
= node_at
(index
)
380 index
= index_within_node
382 # Move half to a new node
383 var new_node
= new UnrolledNode[E
](nodes_length
.max
(node
.capacity
))
385 # Plug in the new node
386 var next_node
= node
.next
387 insert_node
(new_node
, node
, next_node
)
389 # Move items at and after `index` to the new node
390 var to_displace
= node
.length-index
391 var offset
= (new_node
.capacity-to_displace
)/2
392 for i
in [0..to_displace
[ do
393 new_node
.items
[offset
+i
] = node
.items
[index
+i
]
394 node
.items
[index
+i
] = null
396 new_node
.head_index
= offset
397 new_node
.tail_index
= offset
+ to_displace
398 node
.tail_index
-= to_displace
401 if index
> node
.capacity
/ 2 then
402 new_node
.items
[offset-1
] = item
403 new_node
.head_index
-= 1
405 node
.items
[node
.head_index
+index
] = item
409 if node
.tail_index
< node
.capacity
then
410 # Move items towards the tail
411 node
.move_tail
(index
, 1)
413 node
.items
[node
.head_index
+ index
] = item
415 # Move items towards the head
416 node
.move_head
(index
, 1)
417 node
.items
[node
.head_index
+ index-1
] = item
424 redef fun remove_at
(index
)
426 var node
= node_at
(index
)
427 index
= index_within_node
+ node
.head_index
429 # Shift left all the elements after `index`
430 for i
in [index
+1 .. node
.tail_index
[ do
431 node
.items
[i-1
] = node
.items
[i
]
434 node
.items
[node
.tail_index
] = null
438 var next_node
= node
.next
439 var prev_node
= node
.prev
440 if node
.is_empty
and (next_node
!= null or prev_node
!= null) then
441 # Empty and non-head or tail node, delete
442 if next_node
!= null then
443 next_node
.prev
= node
.prev
444 else tail_node
= prev_node
.as(not null)
446 if prev_node
!= null then
447 prev_node
.next
= node
.next
448 else head_node
= next_node
.as(not null)
452 redef fun iterator
do return new UnrolledIterator[E
](self)
455 # Node composing an `UnrolledList`
457 # Stores the elements in the `items` array. The elements in the `items` array
458 # begin at `head_index` and end right before `tail_index`. The data is contiguous,
459 # but there can be empty cells at the beginning and the end of the array.
460 private class UnrolledNode[E
]
462 var prev
: nullable UnrolledNode[E
] = null
464 var next
: nullable UnrolledNode[E
] = null
466 # Desired length of `items`
469 # `Array` of items in this node, filled with `null`
470 var items
= new Array[nullable E
].filled_with
(null, capacity
) is lazy
472 # Index of the first element in `items`
475 # Index after the last element in `items`
478 fun length
: Int do return tail_index
- head_index
480 fun full
: Bool do return length
== capacity
482 fun is_empty
: Bool do return tail_index
== head_index
484 # Move towards the head all elements before `index` of `displace` cells
485 fun move_tail
(index
, displace
: Int)
487 for i
in [tail_index-1
..head_index
+index
].step
(-1) do
488 items
[i
+displace
] = items
[i
]
492 # Move towards the tail all elements at and after `index` of `displace` cells
493 fun move_head
(index
, displace
: Int)
495 for i
in [head_index
..head_index
+index
[ do
496 items
[i-displace
] = items
[i
]
501 private class UnrolledIterator[E
]
502 super IndexedIterator[E
]
504 var list
: UnrolledList[E
]
506 var node
: nullable UnrolledNode[E
] = list
.head_node
is lazy
508 # Index of the current `item`
511 # Index within the current `node`
512 var index_in_node
: Int = node
.head_index
is lazy
514 redef fun item
do return node
.items
[index_in_node
]
516 redef fun is_ok
do return index
< list
.length
523 if index_in_node
>= node
.tail_index
then
525 if node
!= null then index_in_node
= node
.head_index