1217a70175304fca1b95fa23d5644e3924efe8bb
[nit.git] / lib / standard / collection / abstract_collection.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 #
5 # This file is free software, which comes along with NIT. This software is
6 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
7 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
8 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
9 # is kept unaltered, and a notification of the changes is added.
10 # You are allowed to redistribute it and sell it, alone or is a part of
11 # another product.
12
13 # This module define several abstract collection classes.
14 package abstract_collection
15
16 import kernel
17
18 # The root of the collection hierarchy.
19 #
20 # Instances of this class offers an iterator method.
21 #
22 # Collections instances can use the "for" structure:
23 # var x: Collection[U]
24 # # ...
25 # for u in x do
26 # # u is a U
27 # # ...
28 # end
29 # that is equivalent with
30 # var x: Collection[U]
31 # # ...
32 # var i = x.iterator
33 # while i.is_ok do
34 # var u = i.item # u is a U
35 # # ...
36 # i.next
37 # end
38 #
39 # This abstract class implements its others methods with an iterator.
40 # Subclasses may redefine them with an efficient implementation.
41 interface Collection[E]
42 # Get a new iterator on the collection.
43 fun iterator: Iterator[E] is abstract
44
45 # Iterate over each element of the collection
46 fun iterate
47 !each(e: E)
48 do
49 var i = iterator
50 while i.is_ok do
51 each(i.item)
52 i.next
53 end
54 end
55
56 # Is there no item in the collection?
57 #
58 # assert [1,2,3].is_empty == false
59 # assert [1..1[.is_empty == true
60 fun is_empty: Bool is abstract
61
62 # Number of items in the collection.
63 #
64 # assert [10,20,30].length == 3
65 # assert [20..30[.length == 10
66 fun length: Int is abstract
67
68 # Is `item` in the collection ?
69 # Comparisons are done with ==
70 #
71 # assert [1,2,3].has(2) == true
72 # assert [1,2,3].has(9) == false
73 # assert [1..5[.has(2) == true
74 # assert [1..5[.has(9) == false
75 fun has(item: E): Bool is abstract
76
77 # Is the collection contain only `item`?
78 # Comparisons are done with ==
79 # Return true if the collection is empty.
80 #
81 # assert [1,1,1].has_only(1) == true
82 # assert [1,2,3].has_only(1) == false
83 # assert [1..1].has_only(1) == true
84 # assert [1..3].has_only(1) == false
85 # assert [3..3[.has_only(1) == true # empty collection
86 #
87 # ENSURE `is_empty implies result == true`
88 fun has_only(item: E): Bool is abstract
89
90 # How many occurrences of `item` are in the collection?
91 # Comparisons are done with ==
92 #
93 # assert [10,20,10].count(10) == 2
94 fun count(item: E): Int is abstract
95
96 # Return one the item of the collection
97 #
98 # assert [1,2,3].first == 1
99 fun first: E is abstract
100 end
101
102 # Naive implementation of collections method
103 # You only have to define iterator!
104 interface NaiveCollection[E]
105 super Collection[E]
106 redef fun is_empty do return length == 0
107
108 redef fun length
109 do
110 var nb = 0
111 for i in self do nb += 1
112 return nb
113 end
114
115 redef fun has(item)
116 do
117 for i in self do if i == item then return true
118 return false
119 end
120
121 redef fun has_only(item)
122 do
123 for i in self do if i != item then return false
124 return true
125 end
126
127 redef fun count(item)
128 do
129 var nb = 0
130 for i in self do if i == item then nb += 1
131 return nb
132 end
133
134 redef fun first
135 do
136 assert length > 0
137 return iterator.item
138 end
139 end
140
141 # Instances of the Iterator class generates a series of elements, one at a time.
142 # They are mainly used with collections.
143 interface Iterator[E]
144 # The current item.
145 # Require `is_ok`.
146 fun item: E is abstract
147
148 # Jump to the next item.
149 # Require `is_ok`.
150 fun next is abstract
151
152 # Is there a current item ?
153 fun is_ok: Bool is abstract
154 end
155
156 # A collection that contains only one item.
157 class Container[E]
158 super Collection[E]
159
160 redef fun first do return _item
161
162 redef fun is_empty do return false
163
164 redef fun length do return 1
165
166 redef fun has(an_item) do return _item == an_item
167
168 redef fun has_only(an_item) do return _item == an_item
169
170 redef fun count(an_item)
171 do
172 if _item == an_item then
173 return 1
174 else
175 return 0
176 end
177 end
178
179 redef fun iterator do return new ContainerIterator[E](self)
180
181 # Create a new instance with a given initial value.
182 init(e: E) do _item = e
183
184 # The stored item
185 readable writable var _item: E
186 end
187
188 # This iterator is quite stupid since it is used for only one item.
189 class ContainerIterator[E]
190 super Iterator[E]
191 redef fun item do return _container.item
192
193 redef fun next do _is_ok = false
194
195 init(c: Container[E]) do _container = c
196
197 redef readable var _is_ok: Bool = true
198
199 var _container: Container[E]
200 end
201
202 # Items can be removed from this collection
203 interface RemovableCollection[E]
204 super Collection[E]
205 # Remove all items
206 fun clear is abstract
207
208 # Remove an occucence of `item`
209 fun remove(item: E) is abstract
210
211 # Remove all occurences of `item`
212 fun remove_all(item: E) do while has(item) do remove(item)
213 end
214
215 # Items can be added to these collections.
216 interface SimpleCollection[E]
217 super RemovableCollection[E]
218 # Add an item in a collection.
219 # Ensure col.has(item)
220 fun add(item: E) is abstract
221
222 # Add each item of `coll`.
223 fun add_all(coll: Collection[E]) do for i in coll do add(i)
224 end
225
226 # Abstract sets.
227 #
228 # Set contains contains only one element with the same value (according to ==).
229 # var s: Set[String] = new ArraySet[String]
230 # var a = "Hello"
231 # var b = "Hel" + "lo"
232 # # ...
233 # s.add(a)
234 # assert s.has(b) == true
235 interface Set[E: Object]
236 super SimpleCollection[E]
237
238 redef fun has_only(item)
239 do
240 var l = length
241 if l == 1 then
242 return has(item)
243 else if l == 0 then
244 return true
245 else
246 return false
247 end
248 end
249
250 # Only 0 or 1
251 redef fun count(item)
252 do
253 if has(item) then
254 return 1
255 else
256 return 0
257 end
258 end
259
260 # Synonym of remove since there is only one item
261 redef fun remove_all(item) do remove(item)
262 end
263
264 # MapRead are abstract associative collections: `key` -> `item`.
265 interface MapRead[K: Object, E]
266 # Get the item at `key`.
267 fun [](key: K): E is abstract
268
269 # Get the item at `key` or return `default` if not in map
270 fun get_or_default(key: K, default: E): E
271 do
272 if has_key(key) then return self[key]
273 return default
274 end
275
276 # Depreciated alias for `keys.has`
277 fun has_key(key: K): Bool do return self.keys.has(key)
278
279 # Get a new iterator on the map.
280 fun iterator: MapIterator[K, E] is abstract
281
282 # Iterate over each element of the collection
283 fun iterate
284 !each(k: K, v: E)
285 do
286 var i = iterator
287 while i.is_ok do
288 each(i.key, i.item)
289 i.next
290 end
291 end
292
293 # Return the point of view of self on the values only.
294 # Note that `self` and `values` are views on the same data;
295 # therefore any modification of one is visible on the other.
296 fun values: Collection[E] is abstract
297
298 # Return the point of view of self on the keys only.
299 # Note that `self` and `keys` are views on the same data;
300 # therefore any modification of one is visible on the other.
301 fun keys: Collection[K] is abstract
302
303 # Is there no item in the collection?
304 fun is_empty: Bool is abstract
305
306 # Number of items in the collection.
307 fun length: Int is abstract
308 end
309
310 # Maps are associative collections: `key` -> `item`.
311 #
312 # The main operator over maps is [].
313 #
314 # var map: Map[String, Int] = new ArrayMap[String, Int]
315 # # ...
316 # map["one"] = 1 # Associate 'one' to '1'
317 # map["two"] = 2 # Associate 'two' to '2'
318 # assert map["one"] == 1
319 # assert map["two"] == 2
320 #
321 # Instances of maps can be used with the for structure
322 #
323 # for key, value in map do
324 # assert (key == "one" and value == 1) or (key == "two" and value == 2)
325 # end
326 #
327 # The keys and values in the map can also be manipulated directly with the `keys` and `values` methods.
328 #
329 # assert map.keys.has("one") == true
330 # assert map.keys.has("tree") == false
331 # assert map.values.has(1) == true
332 # assert map.values.has(3) == false
333 #
334 interface Map[K: Object, E]
335 super MapRead[K, E]
336 # Set the`item` at `key`.
337 fun []=(key: K, item: E) is abstract
338
339 # Add each (key,value) of `map` into `self`.
340 # If a same key exists in `map` and `self`, then the value in self is discarded.
341 fun recover_with(map: Map[K, E])
342 do
343 var i = map.iterator
344 while i.is_ok do
345 self[i.key] = i.item
346 i.next
347 end
348 end
349
350 # Remove all items
351 fun clear is abstract
352
353 redef fun values: RemovableCollection[E] is abstract
354
355 redef fun keys: RemovableCollection[K] is abstract
356 end
357
358 # Iterators for Map.
359 interface MapIterator[K: Object, E]
360 # The current item.
361 # Require `is_ok`.
362 fun item: E is abstract
363
364 # The key of the current item.
365 # Require `is_ok`.
366 fun key: K is abstract
367
368 # Jump to the next item.
369 # Require `is_ok`.
370 fun next is abstract
371
372 # Is there a current item ?
373 fun is_ok: Bool is abstract
374
375 # Set a new `item` at `key`.
376 #fun item=(item: E) is abstract
377 end
378
379 # Iterator on a 'keys' point of view of a map
380 class MapKeysIterator[K: Object, V]
381 super Iterator[K]
382 # The original iterator
383 var iterator: MapIterator[K, V]
384
385 redef fun is_ok do return self.iterator.is_ok
386 redef fun next do self.iterator.next
387 redef fun item do return self.iterator.key
388 end
389
390 # Iterator on a 'values' point of view of a map
391 class MapValuesIterator[K: Object, V]
392 super Iterator[V]
393 # The original iterator
394 var iterator: MapIterator[K, V]
395
396 redef fun is_ok do return self.iterator.is_ok
397 redef fun next do self.iterator.next
398 redef fun item do return self.iterator.item
399 end
400
401 # Sequences are indexed collections.
402 # The first item is 0. The last is `length-1`.
403 interface SequenceRead[E]
404 super Collection[E]
405 # Get the first item.
406 # Is equivalent with `self[0]`.
407 redef fun first
408 do
409 assert not_empty: not is_empty
410 return self[0]
411 end
412
413 # Return the index=th element of the sequence.
414 # The first element is 0 and the last if `length-1`
415 # If index is invalid, the program aborts
416 fun [](index: Int): E is abstract
417
418 # Get the last item.
419 # Is equivalent with `self[length-1]`.
420 fun last: E
421 do
422 assert not_empty: not is_empty
423 return self[length-1]
424 end
425
426 # Return the index of the first occurrence of `item`.
427 # Return -1 if `item` is not found
428 # Comparison is done with ==
429 fun index_of(item: E): Int
430 do
431 var i = iterator
432 while i.is_ok do
433 if i.item == item then return i.index
434 i.next
435 end
436 return -1
437 end
438
439 redef fun iterator: IndexedIterator[E] is abstract
440 end
441
442 # Sequence are indexed collection.
443 # The first item is 0. The last is `length-1`.
444 interface Sequence[E]
445 super SequenceRead[E]
446 super SimpleCollection[E]
447
448 # Set the first item.
449 # Is equivalent with `self[0] = item`.
450 fun first=(item: E)
451 do self[0] = item end
452
453 # Set the last item.
454 # Is equivalent with `self[length-1] = item`.
455 fun last=(item: E)
456 do
457 var l = length
458 if l > 0 then
459 self[l-1] = item
460 else
461 self[0] = item
462 end
463 end
464
465 # A synonym of `push`
466 redef fun add(e) do push(e)
467
468 # Add an item after the last.
469 fun push(e: E) is abstract
470
471 # Add each item of `coll` after the last.
472 fun append(coll: Collection[E]) do for i in coll do push(i)
473
474 # Remove the last item.
475 fun pop: E is abstract
476
477 # Add an item before the last.
478 fun unshift(e: E) is abstract
479
480 # Remove the first item.
481 # The second item become the first.
482 fun shift: E is abstract
483
484 # Set the `item` at `index`.
485 fun []=(index: Int, item: E) is abstract
486
487 # Remove the item at `index` and shift all following elements
488 fun remove_at(index: Int) is abstract
489 end
490
491 # Iterators on indexed collections.
492 interface IndexedIterator[E]
493 super Iterator[E]
494 # The index of the current item.
495 fun index: Int is abstract
496 end
497
498 # Associative arrays that internally uses couples to represent each (key, value) pairs.
499 interface CoupleMap[K: Object, E]
500 super Map[K, E]
501 # Return the couple of the corresponding key
502 # Return null if the key is no associated element
503 protected fun couple_at(key: K): nullable Couple[K, E] is abstract
504
505 redef fun [](key)
506 do
507 var c = couple_at(key)
508 if c == null then
509 abort
510 else
511 return c.second
512 end
513 end
514 end
515
516 # Iterator on CoupleMap
517 #
518 # Actually is is a wrapper around an iterator of the internal array of the map.
519 class CoupleMapIterator[K: Object, E]
520 super MapIterator[K, E]
521 redef fun item do return _iter.item.second
522
523 #redef fun item=(e) do _iter.item.second = e
524
525 redef fun key do return _iter.item.first
526
527 redef fun is_ok do return _iter.is_ok
528
529 redef fun next
530 do
531 _iter.next
532 end
533
534 var _iter: Iterator[Couple[K,E]]
535
536 init(i: Iterator[Couple[K,E]]) do _iter = i
537 end
538
539 # Some tools ###################################################################
540
541 # Two objects in a simple structure.
542 class Couple[F, S]
543
544 # The first element of the couple.
545 readable writable var _first: F
546
547 # The second element of the couple.
548 readable writable var _second: S
549
550 # Create a new instance with a first and a second object.
551 init(f: F, s: S)
552 do
553 _first = f
554 _second = s
555 end
556 end