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