1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
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
13 # This module define several abstract collection classes.
14 package abstract_collection
18 # The root of the collection hierarchy.
20 # Instances of this class offers an iterator method.
22 # Collections instances can use the "for" structure:
23 # var x: Collection[U]
29 # that is equivalent with
30 # var x: Collection[U]
34 # var u = i.item # u is a U
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
45 # Iterate over each element of the collection
56 # Is there no item in the collection?
58 # assert [1,2,3].is_empty == false
59 # assert [1..1[.is_empty == true
60 fun is_empty
: Bool is abstract
62 # Number of items in the collection.
64 # assert [10,20,30].length == 3
65 # assert [20..30[.length == 10
66 fun length
: Int is abstract
68 # Is `item` in the collection ?
69 # Comparisons are done with ==
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
77 # Is the collection contain only `item`?
78 # Comparisons are done with ==
79 # Return true if the collection is empty.
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
87 # ENSURE `is_empty implies (return) == true`
88 fun has_only
(item
: E
): Bool is abstract
90 # How many occurrences of `item` are in the collection?
91 # Comparisons are done with ==
93 # assert [10,20,10].count(10) == 2
94 fun count
(item
: E
): Int is abstract
96 # Return one the item of the collection
98 # assert [1,2,3].first == 1
99 fun first
: E
is abstract
102 # Naive implementation of collections method
103 # You only have to define iterator!
104 interface NaiveCollection[E
]
106 redef fun is_empty
do return length
== 0
111 for i
in self do nb
+= 1
117 for i
in self do if i
== item
then return true
121 redef fun has_only
(item
)
123 for i
in self do if i
!= item
then return false
127 redef fun count
(item
)
130 for i
in self do if i
== item
then nb
+= 1
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
]
146 fun item
: E
is abstract
148 # Jump to the next item.
152 # Is there a current item ?
153 fun is_ok
: Bool is abstract
156 # A collection that contains only one item.
160 redef fun first
do return _item
162 redef fun is_empty
do return false
164 redef fun length
do return 1
166 redef fun has
(an_item
) do return _item
== an_item
168 redef fun has_only
(an_item
) do return _item
== an_item
170 redef fun count
(an_item
)
172 if _item
== an_item
then
179 redef fun iterator
do return new ContainerIterator[E
](self)
181 # Create a new instance with a given initial value.
182 init(e
: E
) do _item
= e
185 readable writable var _item
: E
188 # This iterator is quite stupid since it is used for only one item.
189 class ContainerIterator[E
]
191 redef fun item
do return _container
.item
193 redef fun next
do _is_ok
= false
195 init(c
: Container[E
]) do _container
= c
197 redef readable var _is_ok
: Bool = true
199 var _container
: Container[E
]
202 # Items can be removed from this collection
203 interface RemovableCollection[E
]
206 fun clear
is abstract
208 # Remove an occucence of `item`
209 fun remove
(item
: E
) is abstract
211 # Remove all occurences of `item`
212 fun remove_all
(item
: E
) do while has
(item
) do remove
(item
)
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
222 # Add each item of `coll`.
223 fun add_all
(coll
: Collection[E
]) do for i
in coll
do add
(i
)
228 # Set contains contains only one element with the same value (according to ==).
229 # var s: Set[String] = new ArraySet[String]
231 # var b = "Hel" + "lo"
234 # assert s.has(b) == true
235 interface Set[E
: Object]
236 super SimpleCollection[E
]
238 redef fun has_only
(item
)
251 redef fun count
(item
)
260 # Synonym of remove since there is only one item
261 redef fun remove_all
(item
) do remove
(item
)
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
269 # Depreciated alias for `keys.has`
270 fun has_key
(key
: K
): Bool do return self.keys
.has
(key
)
272 # Get a new iterator on the map.
273 fun iterator
: MapIterator[K
, E
] is abstract
275 # Iterate over each element of the collection
286 # Return the point of view of self on the values only.
287 # Note that `self` and `values` are views on the same data;
288 # therefore any modification of one is visible on the other.
289 fun values
: Collection[E
] is abstract
291 # Return the point of view of self on the keys only.
292 # Note that `self` and `keys` are views on the same data;
293 # therefore any modification of one is visible on the other.
294 fun keys
: Collection[K
] is abstract
296 # Is there no item in the collection?
297 fun is_empty
: Bool is abstract
299 # Number of items in the collection.
300 fun length
: Int is abstract
303 # Maps are associative collections: `key` -> `item`.
305 # The main operator over maps is [].
307 # var map: Map[String, Int] = new ArrayMap[String, Int]
309 # map["one"] = 1 # Associate 'one' to '1'
310 # map["two"] = 2 # Associate 'two' to '2'
311 # assert map["one"] == 1
312 # assert map["two"] == 2
314 # Instances of maps can be used with the for structure
316 # for key, value in map do
317 # assert (key == "one" and value == 1) or (key == "two" and value == 2)
320 # The keys and values in the map can also be manipulated directly with the `keys` and `values` methods.
322 # assert map.keys.has("one") == true
323 # assert map.keys.has("tree") == false
324 # assert map.values.has(1) == true
325 # assert map.values.has(3) == false
327 interface Map[K
: Object, E
]
329 # Set the`item` at `key`.
330 fun []=(key
: K
, item
: E
) is abstract
332 # Add each (key,value) of `map` into `self`.
333 # If a same key exists in `map` and `self`, then the value in self is discarded.
334 fun recover_with
(map
: Map[K
, E
])
344 fun clear
is abstract
346 redef fun values
: RemovableCollection[E
] is abstract
348 redef fun keys
: RemovableCollection[K
] is abstract
352 interface MapIterator[K
: Object, E
]
355 fun item
: E
is abstract
357 # The key of the current item.
359 fun key
: K
is abstract
361 # Jump to the next item.
365 # Is there a current item ?
366 fun is_ok
: Bool is abstract
368 # Set a new `item` at `key`.
369 #fun item=(item: E) is abstract
372 # Iterator on a 'keys' point of view of a map
373 class MapKeysIterator[K
: Object, V
]
375 # The original iterator
376 var iterator
: MapIterator[K
, V
]
378 redef fun is_ok
do return self.iterator
.is_ok
379 redef fun next
do self.iterator
.next
380 redef fun item
do return self.iterator
.key
383 # Iterator on a 'values' point of view of a map
384 class MapValuesIterator[K
: Object, V
]
386 # The original iterator
387 var iterator
: MapIterator[K
, V
]
389 redef fun is_ok
do return self.iterator
.is_ok
390 redef fun next
do self.iterator
.next
391 redef fun item
do return self.iterator
.item
394 # Sequences are indexed collections.
395 # The first item is 0. The last is `length-1`.
396 interface SequenceRead[E
]
398 # Get the first item.
399 # Is equivalent with `self[0]`.
402 assert not_empty
: not is_empty
406 # Return the index=th element of the sequence.
407 # The first element is 0 and the last if `length-1`
408 # If index is invalid, the program aborts
409 fun [](index
: Int): E
is abstract
412 # Is equivalent with `self[length-1]`.
415 assert not_empty
: not is_empty
416 return self[length-1
]
419 # Return the index of the first occurrence of `item`.
420 # Return -1 if `item` is not found
421 # Comparison is done with ==
422 fun index_of
(item
: E
): Int
426 if i
.item
== item
then return i
.index
432 redef fun iterator
: IndexedIterator[E
] is abstract
435 # Sequence are indexed collection.
436 # The first item is 0. The last is `length-1`.
437 interface Sequence[E
]
438 super SequenceRead[E
]
439 super SimpleCollection[E
]
441 # Set the first item.
442 # Is equivalent with `self[0] = item`.
444 do self[0] = item
end
447 # Is equivalent with `self[length-1] = item`.
458 # A synonym of `push`
459 redef fun add
(e
) do push
(e
)
461 # Add an item after the last.
462 fun push
(e
: E
) is abstract
464 # Add each item of `coll` after the last.
465 fun append
(coll
: Collection[E
]) do for i
in coll
do push
(i
)
467 # Remove the last item.
468 fun pop
: E
is abstract
470 # Add an item before the last.
471 fun unshift
(e
: E
) is abstract
473 # Remove the first item.
474 # The second item become the first.
475 fun shift
: E
is abstract
477 # Set the `item` at `index`.
478 fun []=(index
: Int, item
: E
) is abstract
480 # Remove the item at `index` and shift all following elements
481 fun remove_at
(index
: Int) is abstract
484 # Iterators on indexed collections.
485 interface IndexedIterator[E
]
487 # The index of the current item.
488 fun index
: Int is abstract
491 # Associative arrays that internally uses couples to represent each (key, value) pairs.
492 interface CoupleMap[K
: Object, E
]
494 # Return the couple of the corresponding key
495 # Return null if the key is no associated element
496 protected fun couple_at
(key
: K
): nullable Couple[K
, E
] is abstract
500 var c
= couple_at
(key
)
509 # Iterator on CoupleMap
511 # Actually is is a wrapper around an iterator of the internal array of the map.
512 class CoupleMapIterator[K
: Object, E
]
513 super MapIterator[K
, E
]
514 redef fun item
do return _iter
.item
.second
516 #redef fun item=(e) do _iter.item.second = e
518 redef fun key
do return _iter
.item
.first
520 redef fun is_ok
do return _iter
.is_ok
527 var _iter
: Iterator[Couple[K
,E
]]
529 init(i
: Iterator[Couple[K
,E
]]) do _iter
= i
532 # Some tools ###################################################################
534 # Two objects in a simple structure.
537 # The first element of the couple.
538 readable writable var _first
: F
540 # The second element of the couple.
541 readable writable var _second
: S
543 # Create a new instance with a first and a second object.