3628f5e2d93c5adce755743e576514a76e4840bb
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 # Abstract collection classes and services.
15 # TODO specify the behavior on iterators when collections are modified.
16 module abstract_collection
20 # The root of the collection hierarchy.
22 # Collections modelize finite groups of objects, called elements.
24 # The specific behavior and representation of collections is determined
25 # by the subclasses of the hierarchy.
27 # The main service of Collection is to provide a stable `iterator`
28 # method usable to retrieve all the elements of the collection.
30 # Additional services are provided.
31 # For an implementation point of view, Collection provide a basic
32 # implementation of these services using the `iterator` method.
33 # Subclasses often provide a more efficient implementation.
35 # Because of the `iterator` method, Collections instances can use
36 # the `for` control structure:
38 # var x: Collection[U]
45 # that is equivalent with
47 # var x: Collection[U]
51 # var u = i.item # u is a U
55 interface Collection[E
]
56 # Get a new iterator on the collection.
57 fun iterator
: Iterator[E
] is abstract
59 # Is there no item in the collection?
61 # assert [1,2,3].is_empty == false
62 # assert [1..1[.is_empty == true
63 fun is_empty
: Bool do return length
== 0
65 # Number of items in the collection.
67 # assert [10,20,30].length == 3
68 # assert [20..30[.length == 10
72 for i
in self do nb
+= 1
76 # Is `item` in the collection ?
77 # Comparisons are done with ==
79 # assert [1,2,3].has(2) == true
80 # assert [1,2,3].has(9) == false
81 # assert [1..5[.has(2) == true
82 # assert [1..5[.has(9) == false
83 fun has
(item
: E
): Bool
85 for i
in self do if i
== item
then return true
89 # Is the collection contain only `item`?
90 # Comparisons are done with ==
91 # Return true if the collection is empty.
93 # assert [1,1,1].has_only(1) == true
94 # assert [1,2,3].has_only(1) == false
95 # assert [1..1].has_only(1) == true
96 # assert [1..3].has_only(1) == false
97 # assert [3..3[.has_only(1) == true # empty collection
99 # ENSURE `is_empty implies result == true`
100 fun has_only
(item
: E
): Bool
102 for i
in self do if i
!= item
then return false
106 # How many occurrences of `item` are in the collection?
107 # Comparisons are done with ==
109 # assert [10,20,10].count(10) == 2
110 fun count
(item
: E
): Int
113 for i
in self do if i
== item
then nb
+= 1
117 # Return the first item of the collection
119 # assert [1,2,3].first == 1
126 # Is the collection contains all the elements of `other`?
128 # assert [1,1,1].has_all([1]) == true
129 # assert [1,1,1].has_all([1,2]) == false
130 # assert [1,3,4,2].has_all([1..2]) == true
131 # assert [1,3,4,2].has_all([1..5]) == false
132 fun has_all
(other
: Collection[E
]): Bool
134 for x
in other
do if not has
(x
) then return false
139 # Instances of the Iterator class generates a series of elements, one at a time.
140 # They are mainly used with collections.
141 interface Iterator[E
]
144 fun item
: E
is abstract
146 # Jump to the next item.
150 # Is there a current item ?
151 fun is_ok
: Bool is abstract
154 # A collection that contains only one item.
156 # Used to pass arguments by reference.
158 # Also used when one want to give asingle element when a full
159 # collection is expected
163 redef fun first
do return _item
165 redef fun is_empty
do return false
167 redef fun length
do return 1
169 redef fun has
(an_item
) do return _item
== an_item
171 redef fun has_only
(an_item
) do return _item
== an_item
173 redef fun count
(an_item
)
175 if _item
== an_item
then
182 redef fun iterator
do return new ContainerIterator[E
](self)
184 # Create a new instance with a given initial value.
185 init(e
: E
) do _item
= e
188 readable writable var _item
: E
191 # This iterator is quite stupid since it is used for only one item.
192 class ContainerIterator[E
]
194 redef fun item
do return _container
.item
196 redef fun next
do _is_ok
= false
198 init(c
: Container[E
]) do _container
= c
200 redef readable var _is_ok
: Bool = true
202 var _container
: Container[E
]
205 # Items can be removed from this collection
206 interface RemovableCollection[E
]
213 # assert a.length == 0
216 fun clear
is abstract
218 # Remove an occucence of `item`
220 # var a = [1,2,3,1,2,3]
222 # assert a == [1,3,1,2,3]
223 fun remove
(item
: E
) is abstract
225 # Remove all occurences of `item`
227 # var a = [1,2,3,1,2,3]
229 # assert a == [1,3,1,3]
230 fun remove_all
(item
: E
) do while has
(item
) do remove
(item
)
233 # Items can be added to these collections.
234 interface SimpleCollection[E
]
235 super RemovableCollection[E
]
237 # Add an item in a collection.
241 # assert a.has(3) == true
242 # assert a.has(10) == false
244 # Ensure col.has(item)
245 fun add
(item
: E
) is abstract
247 # Add each item of `coll`.
250 # assert a.has(4) == true
251 # assert a.has(10) == false
252 fun add_all
(coll
: Collection[E
]) do for i
in coll
do add
(i
)
257 # Set is a collection without duplicates (according to `==`)
259 # var s: Set[String] = new ArraySet[String]
261 # var b = "Hel" + "lo"
264 # assert s.has(b) == true
265 interface Set[E
: Object]
266 super SimpleCollection[E
]
268 redef fun has_only
(item
)
281 redef fun count
(item
)
290 # Synonym of remove since there is only one item
291 redef fun remove_all
(item
) do remove
(item
)
293 # Equality is defined on set and means that each set contains the same elements
296 if not other
isa Set[Object] then return false
297 if other
.length
!= length
then return false
298 return has_all
(other
)
301 # Because of the law between `==` and `hash`, `hash` is redefined to be the sum of the hash of the elements
305 for e
in self do res
+= res
.hash
310 # MapRead are abstract associative collections: `key` -> `item`.
311 interface MapRead[K
: Object, E
]
312 # Get the item at `key`
314 # var x = new HashMap[String, Int]
316 # assert x["four"] == 4
317 # # assert x["five"] #=> abort
319 # If the key is not in the map, `provide_default_value` is called (that aborts by default)
320 # See `get_or_null` and `get_or_default` for safe variations.
321 fun [](key
: K
): E
is abstract
323 # Get the item at `key` or null if `key` is not in the map.
325 # var x = new HashMap[String, Int]
327 # assert x.get_or_null("four") == 4
328 # assert x.get_or_null("five") == null
330 # Note: use `has_key` and `[]` if you need the distinction bewteen a key associated with null, and no key.
331 fun get_or_null
(key
: K
): nullable E
333 if has_key
(key
) then return self[key
]
337 # Get the item at `key` or return `default` if not in map
339 # var x = new HashMap[String, Int]
341 # assert x.get_or_default("four", 40) == 4
342 # assert x.get_or_default("five", 50) == 50
344 fun get_or_default
(key
: K
, default
: E
): E
346 if has_key
(key
) then return self[key
]
350 # Depreciated alias for `keys.has`
351 fun has_key
(key
: K
): Bool do return self.keys
.has
(key
)
353 # Get a new iterator on the map.
354 fun iterator
: MapIterator[K
, E
] is abstract
356 # Return the point of view of self on the values only.
357 # Note that `self` and `values` are views on the same data;
358 # therefore any modification of one is visible on the other.
360 # var x = new HashMap[String, Int]
362 # assert x.values.has(4) == true
363 # assert x.values.has(5) == false
364 fun values
: Collection[E
] is abstract
366 # Return the point of view of self on the keys only.
367 # Note that `self` and `keys` are views on the same data;
368 # therefore any modification of one is visible on the other.
370 # var x = new HashMap[String, Int]
372 # assert x.keys.has("four") == true
373 # assert x.keys.has("five") == false
374 fun keys
: Collection[K
] is abstract
376 # Is there no item in the collection?
378 # var x = new HashMap[String, Int]
379 # assert x.is_empty == true
381 # assert x.is_empty == false
382 fun is_empty
: Bool is abstract
384 # Number of items in the collection.
386 # var x = new HashMap[String, Int]
387 # assert x.length == 0
389 # assert x.length == 1
391 # assert x.length == 2
392 fun length
: Int is abstract
394 # Called by the underling implementation of `[]` to provide a default value when a `key` has no value
395 # By default the behavior is to abort.
397 # Note: the value is returned *as is*, implementations may want to store the value in the map before returning it
399 protected fun provide_default_value
(key
: K
): E
do abort
402 # Maps are associative collections: `key` -> `item`.
404 # The main operator over maps is [].
406 # var map: Map[String, Int] = new ArrayMap[String, Int]
408 # map["one"] = 1 # Associate 'one' to '1'
409 # map["two"] = 2 # Associate 'two' to '2'
410 # assert map["one"] == 1
411 # assert map["two"] == 2
413 # Instances of maps can be used with the for structure
415 # for key, value in map do
416 # assert (key == "one" and value == 1) or (key == "two" and value == 2)
419 # The keys and values in the map can also be manipulated directly with the `keys` and `values` methods.
421 # assert map.keys.has("one") == true
422 # assert map.keys.has("tree") == false
423 # assert map.values.has(1) == true
424 # assert map.values.has(3) == false
426 interface Map[K
: Object, E
]
429 # Set the `value` at `key`.
431 # Values can then get retrieved with `[]`.
433 # var x = new HashMap[String, Int]
435 # assert x["four"] == 4
437 # If the key was associated with a value, this old value is discarted
438 # and replaced with the new one.
441 # assert x["four"] == 40
442 # assert x.values.has(4) == false
444 fun []=(key
: K
, value
: E
) is abstract
446 # Add each (key,value) of `map` into `self`.
447 # If a same key exists in `map` and `self`, then the value in self is discarded.
449 # It is the analogous of `SimpleCollection::add_all`
451 # var x = new HashMap[String, Int]
454 # var y = new HashMap[String, Int]
458 # assert x["four"] == 40
459 # assert x["five"] == 5
460 # assert x["nine"] == 90
461 fun recover_with
(map
: Map[K
, E
])
472 # var x = new HashMap[String, Int]
475 # x.keys.has("four") == false
478 fun clear
is abstract
480 redef fun values
: RemovableCollection[E
] is abstract
482 redef fun keys
: RemovableCollection[K
] is abstract
486 interface MapIterator[K
: Object, E
]
489 fun item
: E
is abstract
491 # The key of the current item.
493 fun key
: K
is abstract
495 # Jump to the next item.
499 # Is there a current item ?
500 fun is_ok
: Bool is abstract
502 # Set a new `item` at `key`.
503 #fun item=(item: E) is abstract
506 # Iterator on a 'keys' point of view of a map
507 class MapKeysIterator[K
: Object, V
]
509 # The original iterator
510 var iterator
: MapIterator[K
, V
]
512 redef fun is_ok
do return self.iterator
.is_ok
513 redef fun next
do self.iterator
.next
514 redef fun item
do return self.iterator
.key
517 # Iterator on a 'values' point of view of a map
518 class MapValuesIterator[K
: Object, V
]
520 # The original iterator
521 var iterator
: MapIterator[K
, V
]
523 redef fun is_ok
do return self.iterator
.is_ok
524 redef fun next
do self.iterator
.next
525 redef fun item
do return self.iterator
.item
528 # Sequences are indexed collections.
529 # The first item is 0. The last is `length-1`.
531 # The order is the main caracteristic of sequence
532 # and all concrete implementation of sequences are basically interchangeable.
533 interface SequenceRead[E
]
536 # Get the first item.
537 # Is equivalent with `self[0]`.
540 # assert a.first == 1
542 # REQUIRE `not is_empty`
545 assert not_empty
: not is_empty
549 # Return the index-th element of the sequence.
550 # The first element is 0 and the last is `length-1`
551 # If index is invalid, the program aborts
558 # REQUIRE `index >= 0 and index < length`
559 fun [](index
: Int): E
is abstract
562 # Is equivalent with `self[length-1]`.
567 # REQUIRE `not is_empty`
570 assert not_empty
: not is_empty
571 return self[length-1
]
574 # The index of the first occurrence of `item`.
575 # Return -1 if `item` is not found.
576 # Comparison is done with `==`.
578 # var a = [10,20,30,10,20,30]
579 # assert a.index_of(20) == 1
580 # assert a.index_of(40) == -1
581 fun index_of
(item
: E
): Int do return index_of_from
(item
, 0)
583 # The index of the last occurrence of `item`.
584 # Return -1 if `item` is not found.
585 # Comparison is done with `==`.
587 # var a = [10,20,30,10,20,30]
588 # assert a.last_index_of(20) == 4
589 # assert a.last_index_of(40) == -1
590 fun last_index_of
(item
: E
): Int do return last_index_of_from
(item
, length-1
)
592 # The index of the first occurrence of `item`, starting from pos.
593 # Return -1 if `item` is not found.
594 # Comparison is done with `==`.
596 # var a = [10,20,30,10,20,30]
597 # assert a.index_of_from(20, 3) == 4
598 # assert a.index_of_from(20, 4) == 4
599 # assert a.index_of_from(20, 5) == -1
600 fun index_of_from
(item
: E
, pos
: Int): Int
605 if p
>pos
and i
.item
== item
then return i
.index
612 # The index of the last occurrence of `item` starting from `pos` and decrementing.
613 # Return -1 if `item` is not found.
614 # Comparison is done with `==`.
616 # var a = [10,20,30,10,20,30]
617 # assert a.last_index_of_from(20, 2) == 1
618 # assert a.last_index_of_from(20, 1) == 1
619 # assert a.last_index_of_from(20, 0) == -1
620 fun last_index_of_from
(item
: E
, pos
: Int): Int
627 if i
.item
== item
then res
= p
634 redef fun iterator
: IndexedIterator[E
] is abstract
636 # Two sequences are equals if they have the same items in the same order.
638 # var a = new List[Int]
642 # assert a == [1,2,3]
643 # assert a != [1,3,2]
646 if not o
isa SequenceRead[nullable Object] then return false
648 if o
.length
!= l
then return false
651 if self[i
] != o
[i
] then return false
657 # Because of the law between `==` and `hash`, `hash` is redefined to be the sum of the hash of the elements
661 for e
in self do res
+= res
.hash
666 # Sequence are indexed collection.
667 # The first item is 0. The last is `length-1`.
668 interface Sequence[E
]
669 super SequenceRead[E
]
670 super SimpleCollection[E
]
672 # Set the first item.
673 # Is equivalent with `self[0] = item`.
677 # assert a == [10,2,3]
679 do self[0] = item
end
682 # Is equivalent with `self[length-1] = item`.
686 # assert a == [1,2,10]
688 # If the sequence is empty, `last=` is equivalent with `self[0]=` (thus with `first=`)
690 # var b = new Array[Int]
703 # A synonym of `push`
704 redef fun add
(e
) do push
(e
)
706 # Add an item after the last one.
711 # assert a == [1,2,3,10,20]
712 fun push
(e
: E
) is abstract
714 # Add each item of `coll` after the last.
718 # assert a == [1,2,3,7,8,9]
719 fun append
(coll
: Collection[E
]) do for i
in coll
do push
(i
)
721 # Remove the last item.
728 # REQUIRE `not is_empty`
729 fun pop
: E
is abstract
731 # Add an item before the first one.
736 # assert a == [20,10,1,2,3]
737 fun unshift
(e
: E
) is abstract
739 # Remove the first item.
740 # The second item thus become the first.
743 # assert a.shift == 1
744 # assert a.shift == 2
747 # REQUIRE `not is_empty`
748 fun shift
: E
is abstract
750 # Set the `item` at `index`.
754 # assert a == [10,200,30]
756 # like with `[]`, index should be between `0` and `length-1`
757 # However, if `index==length`, `[]=` works like `push`.
760 # assert a == [10,200,30,400]
762 # REQUIRE `index >= 0 and index <= length`
763 fun []=(index
: Int, item
: E
) is abstract
765 # Insert an element at a given position, following elements are shifted.
767 # var a = [10, 20, 30, 40]
769 # assert a == [10, 20, 100, 30, 40]
771 # REQUIRE `index >= 0 and index < length`
772 # ENSURE `self[index] == item`
773 fun insert
(item
: E
, index
: Int) is abstract
775 # Remove the item at `index` and shift all following elements
779 # assert a == [10,30]
781 # REQUIRE `index >= 0 and index < length`
782 fun remove_at
(index
: Int) is abstract
785 # Iterators on indexed collections.
786 interface IndexedIterator[E
]
788 # The index of the current item.
789 fun index
: Int is abstract
792 # Associative arrays that internally uses couples to represent each (key, value) pairs.
793 # This is an helper class that some specific implementation of Map may implements.
794 interface CoupleMap[K
: Object, E
]
797 # Return the couple of the corresponding key
798 # Return null if the key is no associated element
799 protected fun couple_at
(key
: K
): nullable Couple[K
, E
] is abstract
801 # Return a new iteralot on all couples
802 # Used to provide `iterator` and others
803 protected fun couple_iterator
: Iterator[Couple[K
,E
]] is abstract
805 redef fun iterator
do return new CoupleMapIterator[K
,E
](couple_iterator
)
809 var c
= couple_at
(key
)
811 return provide_default_value
(key
)
818 # Iterator on CoupleMap
820 # Actually it is a wrapper around an iterator of the internal array of the map.
821 private class CoupleMapIterator[K
: Object, E
]
822 super MapIterator[K
, E
]
823 redef fun item
do return _iter
.item
.second
825 #redef fun item=(e) do _iter.item.second = e
827 redef fun key
do return _iter
.item
.first
829 redef fun is_ok
do return _iter
.is_ok
836 var _iter
: Iterator[Couple[K
,E
]]
838 init(i
: Iterator[Couple[K
,E
]]) do _iter
= i
841 # Some tools ###################################################################
843 # Two objects in a simple structure.
846 # The first element of the couple.
847 readable writable var _first
: F
849 # The second element of the couple.
850 readable writable var _second
: S
852 # Create a new instance with a first and a second object.