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 # Is there no item in the collection ?
46 fun is_empty
: Bool is abstract
48 # Number of items in the collection.
49 fun length
: Int is abstract
51 # Is `item' in the collection ?
52 # Comparaisons are done with ==
53 fun has
(item
: E
): Bool is abstract
55 # Is the collection contain only `item' ?
56 # Comparaisons are done with ==
57 # Return true if the collection is empty.
58 fun has_only
(item
: E
): Bool is abstract
60 # How many occurences of `item' are in the collection ?
61 # Comparaisons are done with ==
62 fun count
(item
: E
): Int is abstract
64 # Return one the item of the collection
65 fun first
: E
is abstract
68 # Naive implementation of collections method
69 # You only have to define iterator!
70 interface NaiveCollection[E
]
72 redef fun is_empty
do return length
== 0
77 for i
in self do nb
+= nb
83 for i
in self do if i
== item
then return true
87 redef fun has_only
(item
)
89 for i
in self do if i
!= item
then return false
96 for i
in self do if i
== item
then nb
+= 1
107 # Instances of the Iterator class generates a series of elements, one at a time.
108 # They are mainly used with collections.
109 interface Iterator[E
]
112 fun item
: E
is abstract
114 # Jump to the next item.
118 # Is there a current item ?
119 fun is_ok
: Bool is abstract
122 # A collection that contains only one item.
124 special Collection[E
]
126 redef fun first
do return _item
128 redef fun is_empty
do return false
130 redef fun length
do return 1
132 redef fun has
(an_item
) do return _item
== an_item
134 redef fun has_only
(an_item
) do return _item
== an_item
136 redef fun count
(an_item
)
138 if _item
== an_item
then
145 redef fun iterator
do return new ContainerIterator[E
](self)
147 # Create a new instance with a given initial value.
148 init(e
: E
) do _item
= e
151 readable writable var _item
: E
154 # This iterator is quite stupid since it is used for only one item.
155 class ContainerIterator[E
]
157 redef fun item
do return _container
.item
159 redef fun next
do _is_ok
= false
161 init(c
: Container[E
]) do _container
= c
163 redef readable var _is_ok
: Bool = true
165 var _container
: Container[E
]
168 # Items can be removed from this collection
169 interface RemovableCollection[E
]
170 special Collection[E
]
172 fun clear
is abstract
174 # Remove an occucence of `item'
175 fun remove
(item
: E
) is abstract
177 # Remove all occurences of `item'
178 fun remove_all
(item
: E
) do while has
(item
) do remove
(item
)
181 # Items can be added to these collections.
182 interface SimpleCollection[E
]
183 special RemovableCollection[E
]
184 # Add an item in a collection.
185 # Ensure col.has(item)
186 fun add
(item
: E
) is abstract
188 # Add each item of `coll`.
189 fun add_all
(coll
: Collection[E
]) do for i
in coll
do add
(i
)
194 # Set contains contains only one element with the same value (according to =).
200 # s.has(b) # --> true
202 special SimpleCollection[E
]
204 redef fun has_only
(item
)
217 redef fun count
(item
)
226 # Synonym of remove since there is only one item
227 redef fun remove_all
(item
) do remove
(item
)
230 interface MapRead[K
, E
]
231 special Collection[E
]
232 # Get the item at `key'.
233 fun [](key
: K
): E
is abstract
235 # Is there an item at `key'.
236 fun has_key
(key
: K
): Bool is abstract
238 redef fun iterator
: MapIterator[K
, E
] is abstract
241 # Maps are associative collections: `key' -> `item'.
243 # The main operator over maps is [].
247 # map[u1] = v1 # Associate 'v1' to 'u1'
248 # map[u2] = v2 # Associate 'v2' to 'u2'
251 # map.has_key(u1) # -> true
252 # map.has_key(u3) # -> false
254 special RemovableCollection[E
]
255 special MapRead[K
, E
]
256 # Set the`item' at `key'.
257 fun []=(key
: K
, item
: E
) is abstract
259 # Remove the item at `key'
260 fun remove_at
(key
: K
) is abstract
262 # Add each (key,value) of `map' into `self'.
263 # If a same key exists in `map' and `self', then the value in self is discarded.
264 fun recover_with
(map
: Map[K
, E
])
275 interface MapIterator[K
, E
]
277 # The key of the current item.
278 fun key
: K
is abstract
280 # Set a new `item' at `key'.
281 #fun item=(item: E) is abstract
284 # Indexed collection are ordoned collections.
285 # The first item is 0. The last is `length'-1.
286 interface IndexedCollectionRead[E
]
287 special MapRead[Int, E
]
288 # Get the first item.
289 # Is equivalent with `self'[0].
292 assert not_empty
: not is_empty
297 # Is equivalent with `self'[`length'-1].
300 assert not_empty
: not is_empty
301 return self[length-1
]
304 # Return the index of the first occurence of `item'.
305 # Return -1 if `item' is not found
306 fun index_of
(item
: E
): Int
310 if i
.item
== item
then return i
.index
316 redef fun iterator
: IndexedIterator[E
] is abstract
319 # Indexed collection are ordoned collections.
320 # The first item is 0. The last is `length'-1.
321 interface IndexedCollection[E
]
322 special IndexedCollectionRead[E
]
324 special SimpleCollection[E
]
325 # Set the first item.
326 # Is equivalent with `self'[0] = `item'.
328 do self[0] = item
end
331 # Is equivalent with `self'[length-1] = `item'.
342 # A synonym of `push'
343 redef fun add
(e
) do push
(e
)
345 # Add an item after the last.
346 fun push
(e
: E
) is abstract
348 # Add each item of `coll` after the last.
349 fun append
(coll
: Collection[E
]) do for i
in coll
do push
(i
)
351 # Remove the last item.
352 fun pop
: E
is abstract
354 # Add an item before the last.
355 fun unshift
(e
: E
) is abstract
357 # Remove the first item.
358 # The second item become the first.
359 fun shift
: E
is abstract
363 # Iterators on indexed collections.
364 interface IndexedIterator[E
]
365 special MapIterator[Int, E
]
366 # The index of the current item.
367 fun index
: Int is abstract
369 # A synonym of index.
370 redef fun key
do return index
373 # Associatives arrays that internally uses couples to represent each (key, value) pairs.
374 interface CoupleMap[K
, E
]
376 # Return the couple of the corresponding key
377 # Return null if the key is no associated element
378 protected fun couple_at
(key
: K
): nullable Couple[K
, E
] is abstract
382 var c
= couple_at
(key
)
390 redef fun has_key
(key
) do return couple_at
(key
) != null
393 # Iterator on CoupleMap
395 # Actually is is a wrapper around an iterator of the internal array of the map.
396 class CoupleMapIterator[K
, E
]
397 special MapIterator[K
, E
]
398 redef fun item
do return _iter
.item
.second
400 #redef fun item=(e) do _iter.item.second = e
402 redef fun key
do return _iter
.item
.first
404 redef fun is_ok
do return _iter
.is_ok
411 var _iter
: Iterator[Couple[K
,E
]]
413 init(i
: Iterator[Couple[K
,E
]]) do _iter
= i
416 # Some tools ###################################################################
418 # Two objects in a simple structure.
421 # The first element of the couple.
422 readable writable var _first
: F
424 # The second element of the couple.
425 readable writable var _second
: S
427 # Create a new instance with a first and a second object.