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.
42 # Get a new iterator on the collection.
43 meth iterator
: Iterator[E
] is abstract
45 # Is there no item in the collection ?
46 meth is_empty
: Bool is abstract
48 # Number of items in the collection.
49 meth length
: Int is abstract
51 # Is `item' in the collection ?
52 # Comparaisons are done with ==
53 meth 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 meth has_only
(item
: E
): Bool is abstract
60 # How many occurences of `item' are in the collection ?
61 # Comparaisons are done with ==
62 meth count
(item
: E
): Int is abstract
64 # Return one the item of the collection
65 meth first
: E
is abstract
68 # Naive implementation of collections method
69 # You only have to define iterator!
70 class NaiveCollection[E
: Object]
72 redef meth 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 meth has_only
(item
)
89 for i
in self do if i
!= item
then return false
93 redef meth count
(item
)
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.
112 meth item
: E
is abstract
114 # Jump to the next item.
116 meth next
is abstract
118 # Is there a current item ?
119 meth is_ok
: Bool is abstract
122 # A collection that contains only one item.
124 special Collection[E
]
126 redef meth first
do return _item
128 redef meth is_empty
do return false
130 redef meth length
do return 1
132 redef meth has
(an_item
) do return _item
== an_item
134 redef meth has_only
(an_item
) do return _item
== an_item
136 redef meth count
(an_item
)
138 if _item
== an_item
then
145 redef meth 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 attr _item
: E
154 # This iterator is quite stupid since it is used for only one item.
155 class ContainerIterator[E
]
157 redef meth item
do return _container
.item
159 redef meth next
do _is_ok
= false
161 init(c
: Container[E
]) do _container
= c
163 redef readable attr _is_ok
: Bool = true
165 attr _container
: Container[E
]
168 # Items can be removed from this collection
169 class RemovableCollection[E
]
170 special Collection[E
]
172 meth clear
is abstract
174 # Remove an occucence of `item'
175 meth remove
(item
: E
) is abstract
177 # Remove all occurences of `item'
178 meth remove_all
(item
: E
) do while has
(item
) do remove
(item
)
181 # Items can be added to these collections.
182 class SimpleCollection[E
]
183 special RemovableCollection[E
]
184 # Add an item in a collection.
185 # Ensure col.has(item)
186 meth add
(item
: E
) is abstract
188 # Add each item of `coll`.
189 meth add_all
(coll
: Collection[E
]) do if coll
!= null then 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 meth has_only
(item
)
217 redef meth count
(item
)
226 # Synonym of remove since there is only one item
227 redef meth remove_all
(item
) do remove
(item
)
230 # Maps are associative collections: `key' -> `item'.
232 # The main operator over maps is [].
236 # map[u1] = v1 # Associate 'v1' to 'u1'
237 # map[u2] = v2 # Associate 'v2' to 'u2'
240 # map.has_key(u1) # -> true
241 # map.has_key(u3) # -> false
243 special RemovableCollection[E
]
244 # Get the item at `key'.
245 meth
[](key
: K
): E
is abstract
247 # Set the`item' at `key'.
248 meth
[]=(key
: K
, item
: E
) is abstract
250 # Is there an item at `key'.
251 meth has_key
(key
: K
): Bool is abstract
253 # Remove the item at `key'
254 meth remove_at
(key
: K
) is abstract
256 # Add each (key,value) of `map' into `self'.
257 # If a same key exists in `map' and `self', then the value in self is discarded.
258 meth recover_with
(map
: Map[K
, E
])
267 redef meth iterator
: MapIterator[K
, E
] is abstract
271 class MapIterator[K
, E
]
273 # The key of the current item.
274 meth key
: K
is abstract
276 # Set a new `item' at `key'.
277 meth item
=(item
: E
) is abstract
280 # Indexed collection are ordoned collections.
281 # The first item is 0. The last is `length'-1.
282 class IndexedCollection[E
]
284 special SimpleCollection[E
]
285 # Get the first item.
286 # Is equivalent with `self'[0].
289 assert not_empty
: not is_empty
293 # Set the first item.
294 # Is equivalent with `self'[0] = `item'.
296 do self[0] = item
end
299 # Is equivalent with `self'[`length'-1].
302 assert not_empty
: not is_empty
303 return self[length-1
]
307 # Is equivalent with `self'[length-1] = `item'.
318 # A synonym of `push'
319 redef meth add
(e
) do push
(e
)
321 # Add an item after the last.
322 meth push
(e
: E
) is abstract
324 # Add each item of `coll` after the last.
325 meth append
(coll
: Collection[E
]) do if coll
!= null then for i
in coll
do push
(i
)
327 # Remove the last item.
328 meth pop
: E
is abstract
330 # Add an item before the last.
331 meth unshift
(e
: E
) is abstract
333 # Remove the first item.
334 # The second item become the first.
335 meth shift
: E
is abstract
337 # Return the index of the first occurence of `item'.
338 # Return -1 if `item' is not found
339 meth index_of
(item
: E
): Int
343 if i
.item
== item
then return i
.index
349 redef meth iterator
: IndexedIterator[E
] is abstract
352 # Iterators on indexed collections.
353 class IndexedIterator[E
]
354 special MapIterator[Int, E
]
355 # The index of the current item.
356 meth index
: Int is abstract
358 # A synonym of index.
359 redef meth key
do return index
362 # Associatives arrays that internally uses couples to represent each (key, value) pairs.
363 class CoupleMap[K
, E
]
365 # Return the couple of the corresponding key
366 # Return null if the key is no associated element
367 protected meth couple_at
(key
: K
): Couple[K
, E
] is abstract
371 var c
= couple_at
(key
)
379 redef meth has_key
(key
) do return couple_at
(key
) != null
382 # Iterator on CoupleMap
384 # Actually is is a wrapper around an iterator of the internal array of the map.
385 class CoupleMapIterator[K
, E
]
386 special MapIterator[K
, E
]
387 redef meth item
do return _iter
.item
.second
389 redef meth item
=(e
) do _iter
.item
.second
= e
391 redef meth key
do return _iter
.item
.first
393 redef meth is_ok
do return _iter
.is_ok
398 while _iter
.is_ok
and _iter
.item
== null do _iter
.next
401 attr _iter
: Iterator[Couple[K
,E
]]
403 init(i
: Iterator[Couple[K
,E
]]) do _iter
= i
406 # Some tools ###################################################################
408 # Two objects in a simple structure.
411 # The first element of the couple.
412 readable writable attr _first
: F
414 # The second element of the couple.
415 readable writable attr _second
: S
417 # Create a new instance with a first and a second object.