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 abtract collection classes.
14 package abstract_collection
18 # The root of the collection hierarchy.
20 # Instances of this class offers an iterator method.
22 # Colections 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 ?
57 fun is_empty
: Bool is abstract
59 # Number of items in the collection.
60 fun length
: Int is abstract
62 # Is `item' in the collection ?
63 # Comparaisons are done with ==
64 fun has
(item
: E
): Bool is abstract
66 # Is the collection contain only `item' ?
67 # Comparaisons are done with ==
68 # Return true if the collection is empty.
69 fun has_only
(item
: E
): Bool is abstract
71 # How many occurences of `item' are in the collection ?
72 # Comparaisons are done with ==
73 fun count
(item
: E
): Int is abstract
75 # Return one the item of the collection
76 fun first
: E
is abstract
79 # Naive implementation of collections method
80 # You only have to define iterator!
81 interface NaiveCollection[E
]
83 redef fun is_empty
do return length
== 0
88 for i
in self do nb
+= 1
94 for i
in self do if i
== item
then return true
98 redef fun has_only
(item
)
100 for i
in self do if i
!= item
then return false
104 redef fun count
(item
)
107 for i
in self do if i
== item
then nb
+= 1
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
]
123 fun item
: E
is abstract
125 # Jump to the next item.
129 # Is there a current item ?
130 fun is_ok
: Bool is abstract
133 # A collection that contains only one item.
137 redef fun first
do return _item
139 redef fun is_empty
do return false
141 redef fun length
do return 1
143 redef fun has
(an_item
) do return _item
== an_item
145 redef fun has_only
(an_item
) do return _item
== an_item
147 redef fun count
(an_item
)
149 if _item
== an_item
then
156 redef fun iterator
do return new ContainerIterator[E
](self)
158 # Create a new instance with a given initial value.
159 init(e
: E
) do _item
= e
162 readable writable var _item
: E
165 # This iterator is quite stupid since it is used for only one item.
166 class ContainerIterator[E
]
168 redef fun item
do return _container
.item
170 redef fun next
do _is_ok
= false
172 init(c
: Container[E
]) do _container
= c
174 redef readable var _is_ok
: Bool = true
176 var _container
: Container[E
]
179 # Items can be removed from this collection
180 interface RemovableCollection[E
]
183 fun clear
is abstract
185 # Remove an occucence of `item'
186 fun remove
(item
: E
) is abstract
188 # Remove all occurences of `item'
189 fun remove_all
(item
: E
) do while has
(item
) do remove
(item
)
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
199 # Add each item of `coll`.
200 fun add_all
(coll
: Collection[E
]) do for i
in coll
do add
(i
)
205 # Set contains contains only one element with the same value (according to =).
211 # s.has(b) # --> true
212 interface Set[E
: Object]
213 super SimpleCollection[E
]
215 redef fun has_only
(item
)
228 redef fun count
(item
)
237 # Synonym of remove since there is only one item
238 redef fun remove_all
(item
) do remove
(item
)
241 interface MapRead[K
: Object, E
]
242 # Get the item at `key'.
243 fun [](key
: K
): E
is abstract
245 # Is there an item at `key'.
246 fun has_key
(key
: K
): Bool is abstract
248 # Get a new iterator on the map.
249 fun iterator
: MapIterator[K
, E
] is abstract
251 # Iterate over each element of the collection
262 # Is there no item in the collection ?
263 fun is_empty
: Bool is abstract
265 # Number of items in the collection.
266 fun length
: Int is abstract
268 # Is `item' in the collection ?
269 # Comparaisons are done with ==
270 fun has
(item
: E
): Bool is abstract
272 # Is the collection contain only `item' ?
273 # Comparaisons are done with ==
274 # Return true if the collection is empty.
275 fun has_only
(item
: E
): Bool is abstract
277 # How many occurences of `item' are in the collection ?
278 # Comparaisons are done with ==
279 fun count
(item
: E
): Int is abstract
281 # Return one the item of the collection
282 fun first
: E
is abstract
285 # Maps are associative collections: `key' -> `item'.
287 # The main operator over maps is [].
291 # map[u1] = v1 # Associate 'v1' to 'u1'
292 # map[u2] = v2 # Associate 'v2' to 'u2'
295 # map.has_key(u1) # -> true
296 # map.has_key(u3) # -> false
297 interface Map[K
: Object, E
]
299 # Set the`item' at `key'.
300 fun []=(key
: K
, item
: E
) is abstract
302 # Remove the item at `key'
303 fun remove_at
(key
: K
) is abstract
305 # Add each (key,value) of `map' into `self'.
306 # If a same key exists in `map' and `self', then the value in self is discarded.
307 fun recover_with
(map
: Map[K
, E
])
317 fun clear
is abstract
319 # Remove an occucence of `item'
320 fun remove
(item
: E
) is abstract
322 # Remove all occurences of `item'
323 fun remove_all
(item
: E
) do while has
(item
) do remove
(item
)
327 interface MapIterator[K
: Object, E
]
330 fun item
: E
is abstract
332 # The key of the current item.
334 fun key
: K
is abstract
336 # Jump to the next item.
340 # Is there a current item ?
341 fun is_ok
: Bool is abstract
343 # Set a new `item' at `key'.
344 #fun item=(item: E) is abstract
347 # Indexed collection are ordoned collections.
348 # The first item is 0. The last is `length'-1.
349 interface SequenceRead[E
]
351 # Get the first item.
352 # Is equivalent with `self'[0].
355 assert not_empty
: not is_empty
359 fun [](index
: Int): E
is abstract
362 # Is equivalent with `self'[`length'-1].
365 assert not_empty
: not is_empty
366 return self[length-1
]
369 # Return the index of the first occurence of `item'.
370 # Return -1 if `item' is not found
371 fun index_of
(item
: E
): Int
375 if i
.item
== item
then return i
.index
381 redef fun iterator
: IndexedIterator[E
] is abstract
384 # Indexed collection are ordoned collections.
385 # The first item is 0. The last is `length'-1.
386 interface Sequence[E
]
387 super SequenceRead[E
]
388 super SimpleCollection[E
]
389 # Set the first item.
390 # Is equivalent with `self'[0] = `item'.
392 do self[0] = item
end
395 # Is equivalent with `self'[length-1] = `item'.
406 # A synonym of `push'
407 redef fun add
(e
) do push
(e
)
409 # Add an item after the last.
410 fun push
(e
: E
) is abstract
412 # Add each item of `coll` after the last.
413 fun append
(coll
: Collection[E
]) do for i
in coll
do push
(i
)
415 # Remove the last item.
416 fun pop
: E
is abstract
418 # Add an item before the last.
419 fun unshift
(e
: E
) is abstract
421 # Remove the first item.
422 # The second item become the first.
423 fun shift
: E
is abstract
425 # Set the`item' at `index'.
426 fun []=(index
: Int, item
: E
) is abstract
428 # Remove the item at `index' and shift all following elements
429 fun remove_at
(index
: Int) is abstract
432 # Iterators on indexed collections.
433 interface IndexedIterator[E
]
435 # The index of the current item.
436 fun index
: Int is abstract
439 # Associatives arrays that internally uses couples to represent each (key, value) pairs.
440 interface CoupleMap[K
: Object, E
]
442 # Return the couple of the corresponding key
443 # Return null if the key is no associated element
444 protected fun couple_at
(key
: K
): nullable Couple[K
, E
] is abstract
448 var c
= couple_at
(key
)
456 redef fun has_key
(key
) do return couple_at
(key
) != null
459 # Iterator on CoupleMap
461 # Actually is is a wrapper around an iterator of the internal array of the map.
462 class CoupleMapIterator[K
: Object, E
]
463 super MapIterator[K
, E
]
464 redef fun item
do return _iter
.item
.second
466 #redef fun item=(e) do _iter.item.second = e
468 redef fun key
do return _iter
.item
.first
470 redef fun is_ok
do return _iter
.is_ok
477 var _iter
: Iterator[Couple[K
,E
]]
479 init(i
: Iterator[Couple[K
,E
]]) do _iter
= i
482 # Some tools ###################################################################
484 # Two objects in a simple structure.
487 # The first element of the couple.
488 readable writable var _first
: F
490 # The second element of the couple.
491 readable writable var _second
: S
493 # Create a new instance with a first and a second object.