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 module abstract_collection
18 # The root of the collection hierarchy.
20 # Collections modelize finite groups of objects, called elements.
22 # The specific behavior and representation of collections is determined
23 # by the subclasses of the hierarchy.
25 # The main service of Collection is to provide a stable `iterator`
26 # method usable to retrieve all the elements of the collection.
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.
33 # Because of the `iterator` method, Collections instances can use
34 # the `for` control structure:
36 # var x: Collection[U]
43 # that is equivalent with
45 # var x: Collection[U]
49 # var u = i.item # u is a U
53 interface Collection[E
]
54 # Get a new iterator on the collection.
55 fun iterator
: Iterator[E
] is abstract
57 # Is there no item in the collection?
59 # assert [1,2,3].is_empty == false
60 # assert [1..1[.is_empty == true
61 fun is_empty
: Bool do return length
== 0
63 # Number of items in the collection.
65 # assert [10,20,30].length == 3
66 # assert [20..30[.length == 10
70 for i
in self do nb
+= 1
75 # Is `item` in the collection ?
76 # Comparisons are done with ==
78 # assert [1,2,3].has(2) == true
79 # assert [1,2,3].has(9) == false
80 # assert [1..5[.has(2) == true
81 # assert [1..5[.has(9) == false
82 fun has
(item
: E
): Bool
84 for i
in self do if i
== item
then return true
88 # Is the collection contain only `item`?
89 # Comparisons are done with ==
90 # Return true if the collection is empty.
92 # assert [1,1,1].has_only(1) == true
93 # assert [1,2,3].has_only(1) == false
94 # assert [1..1].has_only(1) == true
95 # assert [1..3].has_only(1) == false
96 # assert [3..3[.has_only(1) == true # empty collection
98 # ENSURE `is_empty implies result == true`
99 fun has_only
(item
: E
): Bool
101 for i
in self do if i
!= item
then return false
105 # How many occurrences of `item` are in the collection?
106 # Comparisons are done with ==
108 # assert [10,20,10].count(10) == 2
109 fun count
(item
: E
): Int
112 for i
in self do if i
== item
then nb
+= 1
116 # Return the first item of the collection
118 # assert [1,2,3].first == 1
125 # Is the collection contains all the elements of `other`?
127 # assert [1,1,1].has_all([1]) == true
128 # assert [1,1,1].has_all([1,2]) == false
129 # assert [1,3,4,2].has_all([1..2]) == true
130 # assert [1,3,4,2].has_all([1..5]) == false
131 fun has_all
(other
: Collection[E
]): Bool
133 for x
in other
do if not has
(x
) then return false
138 # Instances of the Iterator class generates a series of elements, one at a time.
139 # They are mainly used with collections.
140 interface Iterator[E
]
143 fun item
: E
is abstract
145 # Jump to the next item.
149 # Is there a current item ?
150 fun is_ok
: Bool is abstract
153 # A collection that contains only one item.
154 # Used to pass arguments by reference
158 redef fun first
do return _item
160 redef fun is_empty
do return false
162 redef fun length
do return 1
164 redef fun has
(an_item
) do return _item
== an_item
166 redef fun has_only
(an_item
) do return _item
== an_item
168 redef fun count
(an_item
)
170 if _item
== an_item
then
177 redef fun iterator
do return new ContainerIterator[E
](self)
179 # Create a new instance with a given initial value.
180 init(e
: E
) do _item
= e
183 readable writable var _item
: E
186 # This iterator is quite stupid since it is used for only one item.
187 class ContainerIterator[E
]
189 redef fun item
do return _container
.item
191 redef fun next
do _is_ok
= false
193 init(c
: Container[E
]) do _container
= c
195 redef readable var _is_ok
: Bool = true
197 var _container
: Container[E
]
200 # Items can be removed from this collection
201 interface RemovableCollection[E
]
204 fun clear
is abstract
206 # Remove an occucence of `item`
207 fun remove
(item
: E
) is abstract
209 # Remove all occurences of `item`
210 fun remove_all
(item
: E
) do while has
(item
) do remove
(item
)
213 # Items can be added to these collections.
214 interface SimpleCollection[E
]
215 super RemovableCollection[E
]
216 # Add an item in a collection.
217 # Ensure col.has(item)
218 fun add
(item
: E
) is abstract
220 # Add each item of `coll`.
221 fun add_all
(coll
: Collection[E
]) do for i
in coll
do add
(i
)
226 # Set contains contains only one element with the same value (according to ==).
227 # var s: Set[String] = new ArraySet[String]
229 # var b = "Hel" + "lo"
232 # assert s.has(b) == true
233 interface Set[E
: Object]
234 super SimpleCollection[E
]
236 redef fun has_only
(item
)
249 redef fun count
(item
)
258 # Synonym of remove since there is only one item
259 redef fun remove_all
(item
) do remove
(item
)
261 # Equality is defined on set and means that each set contains the same elements
264 if not other
isa Set[Object] then return false
265 if other
.length
!= length
then return false
266 return has_all
(other
)
269 # because of the law between `==` and `hash`, hash is redefined to be the sum of the hash of the elements
273 for e
in self do res
+= res
.hash
278 # MapRead are abstract associative collections: `key` -> `item`.
279 interface MapRead[K
: Object, E
]
280 # Get the item at `key`.
281 fun [](key
: K
): E
is abstract
283 # Get the item at `key` or return `default` if not in map
284 fun get_or_default
(key
: K
, default
: E
): E
286 if has_key
(key
) then return self[key
]
290 # Depreciated alias for `keys.has`
291 fun has_key
(key
: K
): Bool do return self.keys
.has
(key
)
293 # Get a new iterator on the map.
294 fun iterator
: MapIterator[K
, E
] is abstract
296 # Return the point of view of self on the values only.
297 # Note that `self` and `values` are views on the same data;
298 # therefore any modification of one is visible on the other.
299 fun values
: Collection[E
] is abstract
301 # Return the point of view of self on the keys only.
302 # Note that `self` and `keys` are views on the same data;
303 # therefore any modification of one is visible on the other.
304 fun keys
: Collection[K
] is abstract
306 # Is there no item in the collection?
307 fun is_empty
: Bool is abstract
309 # Number of items in the collection.
310 fun length
: Int is abstract
313 # Maps are associative collections: `key` -> `item`.
315 # The main operator over maps is [].
317 # var map: Map[String, Int] = new ArrayMap[String, Int]
319 # map["one"] = 1 # Associate 'one' to '1'
320 # map["two"] = 2 # Associate 'two' to '2'
321 # assert map["one"] == 1
322 # assert map["two"] == 2
324 # Instances of maps can be used with the for structure
326 # for key, value in map do
327 # assert (key == "one" and value == 1) or (key == "two" and value == 2)
330 # The keys and values in the map can also be manipulated directly with the `keys` and `values` methods.
332 # assert map.keys.has("one") == true
333 # assert map.keys.has("tree") == false
334 # assert map.values.has(1) == true
335 # assert map.values.has(3) == false
337 interface Map[K
: Object, E
]
339 # Set the`item` at `key`.
340 fun []=(key
: K
, item
: E
) is abstract
342 # Add each (key,value) of `map` into `self`.
343 # If a same key exists in `map` and `self`, then the value in self is discarded.
344 fun recover_with
(map
: Map[K
, E
])
354 fun clear
is abstract
356 redef fun values
: RemovableCollection[E
] is abstract
358 redef fun keys
: RemovableCollection[K
] is abstract
362 interface MapIterator[K
: Object, E
]
365 fun item
: E
is abstract
367 # The key of the current item.
369 fun key
: K
is abstract
371 # Jump to the next item.
375 # Is there a current item ?
376 fun is_ok
: Bool is abstract
378 # Set a new `item` at `key`.
379 #fun item=(item: E) is abstract
382 # Iterator on a 'keys' point of view of a map
383 class MapKeysIterator[K
: Object, V
]
385 # The original iterator
386 var iterator
: MapIterator[K
, V
]
388 redef fun is_ok
do return self.iterator
.is_ok
389 redef fun next
do self.iterator
.next
390 redef fun item
do return self.iterator
.key
393 # Iterator on a 'values' point of view of a map
394 class MapValuesIterator[K
: Object, V
]
396 # The original iterator
397 var iterator
: MapIterator[K
, V
]
399 redef fun is_ok
do return self.iterator
.is_ok
400 redef fun next
do self.iterator
.next
401 redef fun item
do return self.iterator
.item
404 # Sequences are indexed collections.
405 # The first item is 0. The last is `length-1`.
406 interface SequenceRead[E
]
408 # Get the first item.
409 # Is equivalent with `self[0]`.
412 assert not_empty
: not is_empty
416 # Return the index=th element of the sequence.
417 # The first element is 0 and the last if `length-1`
418 # If index is invalid, the program aborts
419 fun [](index
: Int): E
is abstract
422 # Is equivalent with `self[length-1]`.
425 assert not_empty
: not is_empty
426 return self[length-1
]
429 # Return the index of the first occurrence of `item`.
430 # Return -1 if `item` is not found
431 # Comparison is done with ==
432 fun index_of
(item
: E
): Int
436 if i
.item
== item
then return i
.index
442 redef fun iterator
: IndexedIterator[E
] is abstract
444 # Two sequences are equals if they have the same items in the same order.
447 if not o
isa SequenceRead[nullable Object] then return false
449 if o
.length
!= l
then return false
452 if self[i
] != o
[i
] then return false
458 # because of the law between `==` and `hash`, hash is redefined to be the sum of the hash of the elements
462 for e
in self do res
+= res
.hash
467 # Sequence are indexed collection.
468 # The first item is 0. The last is `length-1`.
469 interface Sequence[E
]
470 super SequenceRead[E
]
471 super SimpleCollection[E
]
473 # Set the first item.
474 # Is equivalent with `self[0] = item`.
476 do self[0] = item
end
479 # Is equivalent with `self[length-1] = item`.
490 # A synonym of `push`
491 redef fun add
(e
) do push
(e
)
493 # Add an item after the last.
494 fun push
(e
: E
) is abstract
496 # Add each item of `coll` after the last.
497 fun append
(coll
: Collection[E
]) do for i
in coll
do push
(i
)
499 # Remove the last item.
500 fun pop
: E
is abstract
502 # Add an item before the last.
503 fun unshift
(e
: E
) is abstract
505 # Remove the first item.
506 # The second item become the first.
507 fun shift
: E
is abstract
509 # Set the `item` at `index`.
510 fun []=(index
: Int, item
: E
) is abstract
512 # Remove the item at `index` and shift all following elements
513 fun remove_at
(index
: Int) is abstract
516 # Iterators on indexed collections.
517 interface IndexedIterator[E
]
519 # The index of the current item.
520 fun index
: Int is abstract
523 # Associative arrays that internally uses couples to represent each (key, value) pairs.
524 interface CoupleMap[K
: Object, E
]
526 # Return the couple of the corresponding key
527 # Return null if the key is no associated element
528 protected fun couple_at
(key
: K
): nullable Couple[K
, E
] is abstract
532 var c
= couple_at
(key
)
541 # Iterator on CoupleMap
543 # Actually is is a wrapper around an iterator of the internal array of the map.
544 class CoupleMapIterator[K
: Object, E
]
545 super MapIterator[K
, E
]
546 redef fun item
do return _iter
.item
.second
548 #redef fun item=(e) do _iter.item.second = e
550 redef fun key
do return _iter
.item
.first
552 redef fun is_ok
do return _iter
.is_ok
559 var _iter
: Iterator[Couple[K
,E
]]
561 init(i
: Iterator[Couple[K
,E
]]) do _iter
= i
564 # Some tools ###################################################################
566 # Two objects in a simple structure.
569 # The first element of the couple.
570 readable writable var _first
: F
572 # The second element of the couple.
573 readable writable var _second
: S
575 # Create a new instance with a first and a second object.