10fafb7fb6a4ef5501bb4933a2df2321322adb2d
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 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 interface NaiveCollection[E
]
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.
109 interface Iterator[E
]
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 interface 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 interface 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 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 interface MapRead[K
, E
]
231 special Collection[E
]
232 # Get the item at `key'.
233 meth
[](key
: K
): E
is abstract
235 # Is there an item at `key'.
236 meth has_key
(key
: K
): Bool is abstract
238 redef meth 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 meth
[]=(key
: K
, item
: E
) is abstract
259 # Remove the item at `key'
260 meth 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 meth recover_with
(map
: Map[K
, E
])
275 interface MapIterator[K
, E
]
277 # The key of the current item.
278 meth key
: K
is abstract
280 # Set a new `item' at `key'.
281 #meth 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 meth index_of
(item
: E
): Int
310 if i
.item
== item
then return i
.index
316 redef meth 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 meth add
(e
) do push
(e
)
345 # Add an item after the last.
346 meth push
(e
: E
) is abstract
348 # Add each item of `coll` after the last.
349 meth append
(coll
: Collection[E
]) do for i
in coll
do push
(i
)
351 # Remove the last item.
352 meth pop
: E
is abstract
354 # Add an item before the last.
355 meth unshift
(e
: E
) is abstract
357 # Remove the first item.
358 # The second item become the first.
359 meth 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 meth index
: Int is abstract
369 # A synonym of index.
370 redef meth 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 meth couple_at
(key
: K
): nullable Couple[K
, E
] is abstract
382 var c
= couple_at
(key
)
390 redef meth 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 meth item
do return _iter
.item
.second
400 #redef meth item=(e) do _iter.item.second = e
402 redef meth key
do return _iter
.item
.first
404 redef meth is_ok
do return _iter
.is_ok
411 attr _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 attr _first
: F
424 # The second element of the couple.
425 readable writable attr _second
: S
427 # Create a new instance with a first and a second object.