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