First NIT release and new clean mercurial repository
[nit.git] / lib / standard / abstract_collection.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2004-2008 Jean Privat <jean@pryen.org>
4 #
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
11 # another product.
12
13 # This module define several abtract collection classes.
14 package abstract_collection
15
16 import kernel
17
18 # The root of the collection hierarchy.
19 #
20 # Instances of this class offers an iterator method.
21 #
22 # Colections instances can use the "for" structure:
23 # var x: Collection[U]
24 # ...
25 # for u in x do
26 # # u is a U
27 # ...
28 # end
29 # that is equivalent with
30 # var x: Collection[U]
31 # ...
32 # var i = x.iterator
33 # while i.is_ok do
34 # var u = i.item # u is a U
35 # ...
36 # i.next
37 # end
38 #
39 # This abstract class implements its others methods with an iterator.
40 # Subclasses may redefine them with an efficient implementation.
41 class Collection[E]
42 # Get a new iterator on the collection.
43 meth iterator: Iterator[E] is abstract
44
45 # Is there no item in the collection ?
46 meth is_empty: Bool is abstract
47
48 # Number of items in the collection.
49 meth length: Int is abstract
50
51 # Is `item' in the collection ?
52 # Comparaisons are done with ==
53 meth has(item: E): Bool is abstract
54
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
59
60 # How many occurences of `item' are in the collection ?
61 # Comparaisons are done with ==
62 meth count(item: E): Int is abstract
63
64 # Return one the item of the collection
65 meth first: E is abstract
66 end
67
68 # Naive implementation of collections method
69 # You only have to define iterator!
70 class NaiveCollection[E: Object]
71 special Collection[E]
72 redef meth is_empty do return length == 0
73
74 redef meth length
75 do
76 var nb = 0
77 for i in self do nb += nb
78 return nb
79 end
80
81 redef meth has(item)
82 do
83 for i in self do if i == item then return true
84 return false
85 end
86
87 redef meth has_only(item)
88 do
89 for i in self do if i != item then return false
90 return true
91 end
92
93 redef meth count(item)
94 do
95 var nb = 0
96 for i in self do if i == item then nb += 1
97 return nb
98 end
99
100 redef meth first
101 do
102 assert length > 0
103 return iterator.item
104 end
105 end
106
107 # Instances of the Iterator class generates a series of elements, one at a time.
108 # They are mainly used with collections.
109 class Iterator[E]
110 # The current item.
111 # Require `is_ok'.
112 meth item: E is abstract
113
114 # Jump to the next item.
115 # Require `is_ok'.
116 meth next is abstract
117
118 # Is there a current item ?
119 meth is_ok: Bool is abstract
120 end
121
122 # A collection that contains only one item.
123 class Container[E]
124 special Collection[E]
125
126 redef meth first do return _item
127
128 redef meth is_empty do return false
129
130 redef meth length do return 1
131
132 redef meth has(an_item) do return _item == an_item
133
134 redef meth has_only(an_item) do return _item == an_item
135
136 redef meth count(an_item)
137 do
138 if _item == an_item then
139 return 1
140 else
141 return 0
142 end
143 end
144
145 redef meth iterator do return new ContainerIterator[E](self)
146
147 # Create a new instance with a given initial value.
148 init(e: E) do _item = e
149
150 # The stored item
151 readable writable attr _item: E
152 end
153
154 # This iterator is quite stupid since it is used for only one item.
155 class ContainerIterator[E]
156 special Iterator[E]
157 redef meth item do return _container.item
158
159 redef meth next do _is_ok = false
160
161 init(c: Container[E]) do _container = c
162
163 redef readable attr _is_ok: Bool = true
164
165 attr _container: Container[E]
166 end
167
168 # Items can be removed from this collection
169 class RemovableCollection[E]
170 special Collection[E]
171 # Remove all items
172 meth clear is abstract
173
174 # Remove an occucence of `item'
175 meth remove(item: E) is abstract
176
177 # Remove all occurences of `item'
178 meth remove_all(item: E) do while has(item) do remove(item)
179 end
180
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
187
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)
190 end
191
192 # Abstract sets.
193 #
194 # Set contains contains only one element with the same value (according to =).
195 # var s : Set[E]
196 # var a = "Hello"
197 # var b = "Hello"
198 # ...
199 # s.add(a)
200 # s.has(b) # --> true
201 class Set[E]
202 special SimpleCollection[E]
203
204 redef meth has_only(item)
205 do
206 var l = length
207 if l == 1 then
208 return has(item)
209 else if l == 0 then
210 return true
211 else
212 return false
213 end
214 end
215
216 # Only 0 or 1
217 redef meth count(item)
218 do
219 if has(item) then
220 return 1
221 else
222 return 0
223 end
224 end
225
226 # Synonym of remove since there is only one item
227 redef meth remove_all(item) do remove(item)
228 end
229
230 # Maps are associative collections: `key' -> `item'.
231 #
232 # The main operator over maps is [].
233 #
234 # var map: Map[U, V]
235 # ...
236 # map[u1] = v1 # Associate 'v1' to 'u1'
237 # map[u2] = v2 # Associate 'v2' to 'u2'
238 # map[u1] # -> v1
239 # map[u2] # -> v2
240 # map.has_key(u1) # -> true
241 # map.has_key(u3) # -> false
242 class Map[K, E]
243 special RemovableCollection[E]
244 # Get the item at `key'.
245 meth [](key: K): E is abstract
246
247 # Set the`item' at `key'.
248 meth []=(key: K, item: E) is abstract
249
250 # Is there an item at `key'.
251 meth has_key(key: K): Bool is abstract
252
253 # Remove the item at `key'
254 meth remove_at(key: K) is abstract
255
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])
259 do
260 var i = map.iterator
261 while i.is_ok do
262 self[i.key] = i.item
263 i.next
264 end
265 end
266
267 redef meth iterator: MapIterator[K, E] is abstract
268 end
269
270 # Iterators for Map.
271 class MapIterator[K, E]
272 special Iterator[E]
273 # The key of the current item.
274 meth key: K is abstract
275
276 # Set a new `item' at `key'.
277 meth item=(item: E) is abstract
278 end
279
280 # Indexed collection are ordoned collections.
281 # The first item is 0. The last is `length'-1.
282 class IndexedCollection[E]
283 special Map[Int, E]
284 special SimpleCollection[E]
285 # Get the first item.
286 # Is equivalent with `self'[0].
287 redef meth first
288 do
289 assert not_empty: not is_empty
290 return self[0]
291 end
292
293 # Set the first item.
294 # Is equivalent with `self'[0] = `item'.
295 meth first=(item: E)
296 do self[0] = item end
297
298 # Get the last item.
299 # Is equivalent with `self'[`length'-1].
300 meth last: E
301 do
302 assert not_empty: not is_empty
303 return self[length-1]
304 end
305
306 # Set the last item.
307 # Is equivalent with `self'[length-1] = `item'.
308 meth last=(item: E)
309 do
310 var l = length
311 if l > 0 then
312 self[l-1] = item
313 else
314 self[0] = item
315 end
316 end
317
318 # A synonym of `push'
319 redef meth add(e) do push(e)
320
321 # Add an item after the last.
322 meth push(e: E) is abstract
323
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)
326
327 # Remove the last item.
328 meth pop: E is abstract
329
330 # Add an item before the last.
331 meth unshift(e: E) is abstract
332
333 # Remove the first item.
334 # The second item become the first.
335 meth shift: E is abstract
336
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
340 do
341 var i = iterator
342 while i.is_ok do
343 if i.item == item then return i.index
344 i.next
345 end
346 return -1
347 end
348
349 redef meth iterator: IndexedIterator[E] is abstract
350 end
351
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
357
358 # A synonym of index.
359 redef meth key do return index
360 end
361
362 # Associatives arrays that internally uses couples to represent each (key, value) pairs.
363 class CoupleMap[K, E]
364 special Map[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
368
369 redef meth [](key)
370 do
371 var c = couple_at(key)
372 if c == null then
373 return null
374 else
375 return c.second
376 end
377 end
378
379 redef meth has_key(key) do return couple_at(key) != null
380 end
381
382 # Iterator on CoupleMap
383 #
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
388
389 redef meth item=(e) do _iter.item.second = e
390
391 redef meth key do return _iter.item.first
392
393 redef meth is_ok do return _iter.is_ok
394
395 redef meth next
396 do
397 _iter.next
398 while _iter.is_ok and _iter.item == null do _iter.next
399 end
400
401 attr _iter: Iterator[Couple[K,E]]
402
403 init(i: Iterator[Couple[K,E]]) do _iter = i
404 end
405
406 # Some tools ###################################################################
407
408 # Two objects in a simple structure.
409 class Couple[F, S]
410
411 # The first element of the couple.
412 readable writable attr _first: F
413
414 # The second element of the couple.
415 readable writable attr _second: S
416
417 # Create a new instance with a first and a second object.
418 init(f: F, s: S)
419 do
420 _first = f
421 _second = s
422 end
423 end