syntax: 'meth' -> 'fun', 'attr' -> 'var'
[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 interface Collection[E]
42 # Get a new iterator on the collection.
43 fun iterator: Iterator[E] is abstract
44
45 # Is there no item in the collection ?
46 fun is_empty: Bool is abstract
47
48 # Number of items in the collection.
49 fun length: Int is abstract
50
51 # Is `item' in the collection ?
52 # Comparaisons are done with ==
53 fun 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 fun has_only(item: E): Bool is abstract
59
60 # How many occurences of `item' are in the collection ?
61 # Comparaisons are done with ==
62 fun count(item: E): Int is abstract
63
64 # Return one the item of the collection
65 fun first: E is abstract
66 end
67
68 # Naive implementation of collections method
69 # You only have to define iterator!
70 interface NaiveCollection[E]
71 special Collection[E]
72 redef fun is_empty do return length == 0
73
74 redef fun length
75 do
76 var nb = 0
77 for i in self do nb += nb
78 return nb
79 end
80
81 redef fun has(item)
82 do
83 for i in self do if i == item then return true
84 return false
85 end
86
87 redef fun 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 fun 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 fun 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 interface Iterator[E]
110 # The current item.
111 # Require `is_ok'.
112 fun item: E is abstract
113
114 # Jump to the next item.
115 # Require `is_ok'.
116 fun next is abstract
117
118 # Is there a current item ?
119 fun 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 fun first do return _item
127
128 redef fun is_empty do return false
129
130 redef fun length do return 1
131
132 redef fun has(an_item) do return _item == an_item
133
134 redef fun has_only(an_item) do return _item == an_item
135
136 redef fun 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 fun 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 var _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 fun item do return _container.item
158
159 redef fun next do _is_ok = false
160
161 init(c: Container[E]) do _container = c
162
163 redef readable var _is_ok: Bool = true
164
165 var _container: Container[E]
166 end
167
168 # Items can be removed from this collection
169 interface RemovableCollection[E]
170 special Collection[E]
171 # Remove all items
172 fun clear is abstract
173
174 # Remove an occucence of `item'
175 fun remove(item: E) is abstract
176
177 # Remove all occurences of `item'
178 fun remove_all(item: E) do while has(item) do remove(item)
179 end
180
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
187
188 # Add each item of `coll`.
189 fun add_all(coll: Collection[E]) do 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 interface Set[E]
202 special SimpleCollection[E]
203
204 redef fun 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 fun 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 fun remove_all(item) do remove(item)
228 end
229
230 interface MapRead[K, E]
231 special Collection[E]
232 # Get the item at `key'.
233 fun [](key: K): E is abstract
234
235 # Is there an item at `key'.
236 fun has_key(key: K): Bool is abstract
237
238 redef fun iterator: MapIterator[K, E] is abstract
239 end
240
241 # Maps are associative collections: `key' -> `item'.
242 #
243 # The main operator over maps is [].
244 #
245 # var map: Map[U, V]
246 # ...
247 # map[u1] = v1 # Associate 'v1' to 'u1'
248 # map[u2] = v2 # Associate 'v2' to 'u2'
249 # map[u1] # -> v1
250 # map[u2] # -> v2
251 # map.has_key(u1) # -> true
252 # map.has_key(u3) # -> false
253 interface Map[K, E]
254 special RemovableCollection[E]
255 special MapRead[K, E]
256 # Set the`item' at `key'.
257 fun []=(key: K, item: E) is abstract
258
259 # Remove the item at `key'
260 fun remove_at(key: K) is abstract
261
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])
265 do
266 var i = map.iterator
267 while i.is_ok do
268 self[i.key] = i.item
269 i.next
270 end
271 end
272 end
273
274 # Iterators for Map.
275 interface MapIterator[K, E]
276 special Iterator[E]
277 # The key of the current item.
278 fun key: K is abstract
279
280 # Set a new `item' at `key'.
281 #fun item=(item: E) is abstract
282 end
283
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].
290 redef fun first
291 do
292 assert not_empty: not is_empty
293 return self[0]
294 end
295
296 # Get the last item.
297 # Is equivalent with `self'[`length'-1].
298 fun last: E
299 do
300 assert not_empty: not is_empty
301 return self[length-1]
302 end
303
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
307 do
308 var i = iterator
309 while i.is_ok do
310 if i.item == item then return i.index
311 i.next
312 end
313 return -1
314 end
315
316 redef fun iterator: IndexedIterator[E] is abstract
317 end
318
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]
323 special Map[Int, E]
324 special SimpleCollection[E]
325 # Set the first item.
326 # Is equivalent with `self'[0] = `item'.
327 fun first=(item: E)
328 do self[0] = item end
329
330 # Set the last item.
331 # Is equivalent with `self'[length-1] = `item'.
332 fun last=(item: E)
333 do
334 var l = length
335 if l > 0 then
336 self[l-1] = item
337 else
338 self[0] = item
339 end
340 end
341
342 # A synonym of `push'
343 redef fun add(e) do push(e)
344
345 # Add an item after the last.
346 fun push(e: E) is abstract
347
348 # Add each item of `coll` after the last.
349 fun append(coll: Collection[E]) do for i in coll do push(i)
350
351 # Remove the last item.
352 fun pop: E is abstract
353
354 # Add an item before the last.
355 fun unshift(e: E) is abstract
356
357 # Remove the first item.
358 # The second item become the first.
359 fun shift: E is abstract
360
361 end
362
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
368
369 # A synonym of index.
370 redef fun key do return index
371 end
372
373 # Associatives arrays that internally uses couples to represent each (key, value) pairs.
374 interface CoupleMap[K, E]
375 special Map[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
379
380 redef fun [](key)
381 do
382 var c = couple_at(key)
383 if c == null then
384 abort
385 else
386 return c.second
387 end
388 end
389
390 redef fun has_key(key) do return couple_at(key) != null
391 end
392
393 # Iterator on CoupleMap
394 #
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
399
400 #redef fun item=(e) do _iter.item.second = e
401
402 redef fun key do return _iter.item.first
403
404 redef fun is_ok do return _iter.is_ok
405
406 redef fun next
407 do
408 _iter.next
409 end
410
411 var _iter: Iterator[Couple[K,E]]
412
413 init(i: Iterator[Couple[K,E]]) do _iter = i
414 end
415
416 # Some tools ###################################################################
417
418 # Two objects in a simple structure.
419 class Couple[F, S]
420
421 # The first element of the couple.
422 readable writable var _first: F
423
424 # The second element of the couple.
425 readable writable var _second: S
426
427 # Create a new instance with a first and a second object.
428 init(f: F, s: S)
429 do
430 _first = f
431 _second = s
432 end
433 end