lib: fix NaiveCollection::length
[nit.git] / lib / standard / collection / 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 # Iterate over each element of the collection
46 fun iterate
47 !each(e: E)
48 do
49 var i = iterator
50 while i.is_ok do
51 each(i.item)
52 i.next
53 end
54 end
55
56 # Is there no item in the collection ?
57 fun is_empty: Bool is abstract
58
59 # Number of items in the collection.
60 fun length: Int is abstract
61
62 # Is `item' in the collection ?
63 # Comparaisons are done with ==
64 fun has(item: E): Bool is abstract
65
66 # Is the collection contain only `item' ?
67 # Comparaisons are done with ==
68 # Return true if the collection is empty.
69 fun has_only(item: E): Bool is abstract
70
71 # How many occurences of `item' are in the collection ?
72 # Comparaisons are done with ==
73 fun count(item: E): Int is abstract
74
75 # Return one the item of the collection
76 fun first: E is abstract
77 end
78
79 # Naive implementation of collections method
80 # You only have to define iterator!
81 interface NaiveCollection[E]
82 special Collection[E]
83 redef fun is_empty do return length == 0
84
85 redef fun length
86 do
87 var nb = 0
88 for i in self do nb += 1
89 return nb
90 end
91
92 redef fun has(item)
93 do
94 for i in self do if i == item then return true
95 return false
96 end
97
98 redef fun has_only(item)
99 do
100 for i in self do if i != item then return false
101 return true
102 end
103
104 redef fun count(item)
105 do
106 var nb = 0
107 for i in self do if i == item then nb += 1
108 return nb
109 end
110
111 redef fun first
112 do
113 assert length > 0
114 return iterator.item
115 end
116 end
117
118 # Instances of the Iterator class generates a series of elements, one at a time.
119 # They are mainly used with collections.
120 interface Iterator[E]
121 # The current item.
122 # Require `is_ok'.
123 fun item: E is abstract
124
125 # Jump to the next item.
126 # Require `is_ok'.
127 fun next is abstract
128
129 # Is there a current item ?
130 fun is_ok: Bool is abstract
131 end
132
133 # A collection that contains only one item.
134 class Container[E]
135 special Collection[E]
136
137 redef fun first do return _item
138
139 redef fun is_empty do return false
140
141 redef fun length do return 1
142
143 redef fun has(an_item) do return _item == an_item
144
145 redef fun has_only(an_item) do return _item == an_item
146
147 redef fun count(an_item)
148 do
149 if _item == an_item then
150 return 1
151 else
152 return 0
153 end
154 end
155
156 redef fun iterator do return new ContainerIterator[E](self)
157
158 # Create a new instance with a given initial value.
159 init(e: E) do _item = e
160
161 # The stored item
162 readable writable var _item: E
163 end
164
165 # This iterator is quite stupid since it is used for only one item.
166 class ContainerIterator[E]
167 special Iterator[E]
168 redef fun item do return _container.item
169
170 redef fun next do _is_ok = false
171
172 init(c: Container[E]) do _container = c
173
174 redef readable var _is_ok: Bool = true
175
176 var _container: Container[E]
177 end
178
179 # Items can be removed from this collection
180 interface RemovableCollection[E]
181 special Collection[E]
182 # Remove all items
183 fun clear is abstract
184
185 # Remove an occucence of `item'
186 fun remove(item: E) is abstract
187
188 # Remove all occurences of `item'
189 fun remove_all(item: E) do while has(item) do remove(item)
190 end
191
192 # Items can be added to these collections.
193 interface SimpleCollection[E]
194 special RemovableCollection[E]
195 # Add an item in a collection.
196 # Ensure col.has(item)
197 fun add(item: E) is abstract
198
199 # Add each item of `coll`.
200 fun add_all(coll: Collection[E]) do for i in coll do add(i)
201 end
202
203 # Abstract sets.
204 #
205 # Set contains contains only one element with the same value (according to =).
206 # var s : Set[E]
207 # var a = "Hello"
208 # var b = "Hello"
209 # ...
210 # s.add(a)
211 # s.has(b) # --> true
212 interface Set[E: Object]
213 special SimpleCollection[E]
214
215 redef fun has_only(item)
216 do
217 var l = length
218 if l == 1 then
219 return has(item)
220 else if l == 0 then
221 return true
222 else
223 return false
224 end
225 end
226
227 # Only 0 or 1
228 redef fun count(item)
229 do
230 if has(item) then
231 return 1
232 else
233 return 0
234 end
235 end
236
237 # Synonym of remove since there is only one item
238 redef fun remove_all(item) do remove(item)
239 end
240
241 interface MapRead[K: Object, E]
242 special Collection[E]
243 # Get the item at `key'.
244 fun [](key: K): E is abstract
245
246 # Is there an item at `key'.
247 fun has_key(key: K): Bool is abstract
248
249 redef fun iterator: MapIterator[K, E] is abstract
250 end
251
252 # Maps are associative collections: `key' -> `item'.
253 #
254 # The main operator over maps is [].
255 #
256 # var map: Map[U, V]
257 # ...
258 # map[u1] = v1 # Associate 'v1' to 'u1'
259 # map[u2] = v2 # Associate 'v2' to 'u2'
260 # map[u1] # -> v1
261 # map[u2] # -> v2
262 # map.has_key(u1) # -> true
263 # map.has_key(u3) # -> false
264 interface Map[K: Object, E]
265 special RemovableCollection[E]
266 special MapRead[K, E]
267 # Set the`item' at `key'.
268 fun []=(key: K, item: E) is abstract
269
270 # Remove the item at `key'
271 fun remove_at(key: K) is abstract
272
273 # Add each (key,value) of `map' into `self'.
274 # If a same key exists in `map' and `self', then the value in self is discarded.
275 fun recover_with(map: Map[K, E])
276 do
277 var i = map.iterator
278 while i.is_ok do
279 self[i.key] = i.item
280 i.next
281 end
282 end
283 end
284
285 # Iterators for Map.
286 interface MapIterator[K: Object, E]
287 special Iterator[E]
288 # The key of the current item.
289 fun key: K is abstract
290
291 # Set a new `item' at `key'.
292 #fun item=(item: E) is abstract
293 end
294
295 # Indexed collection are ordoned collections.
296 # The first item is 0. The last is `length'-1.
297 interface SequenceRead[E]
298 special MapRead[Int, E]
299 # Get the first item.
300 # Is equivalent with `self'[0].
301 redef fun first
302 do
303 assert not_empty: not is_empty
304 return self[0]
305 end
306
307 # Get the last item.
308 # Is equivalent with `self'[`length'-1].
309 fun last: E
310 do
311 assert not_empty: not is_empty
312 return self[length-1]
313 end
314
315 # Return the index of the first occurence of `item'.
316 # Return -1 if `item' is not found
317 fun index_of(item: E): Int
318 do
319 var i = iterator
320 while i.is_ok do
321 if i.item == item then return i.index
322 i.next
323 end
324 return -1
325 end
326
327 redef fun iterator: IndexedIterator[E] is abstract
328 end
329
330 # Indexed collection are ordoned collections.
331 # The first item is 0. The last is `length'-1.
332 interface Sequence[E]
333 special SequenceRead[E]
334 special Map[Int, E]
335 special SimpleCollection[E]
336 # Set the first item.
337 # Is equivalent with `self'[0] = `item'.
338 fun first=(item: E)
339 do self[0] = item end
340
341 # Set the last item.
342 # Is equivalent with `self'[length-1] = `item'.
343 fun last=(item: E)
344 do
345 var l = length
346 if l > 0 then
347 self[l-1] = item
348 else
349 self[0] = item
350 end
351 end
352
353 # A synonym of `push'
354 redef fun add(e) do push(e)
355
356 # Add an item after the last.
357 fun push(e: E) is abstract
358
359 # Add each item of `coll` after the last.
360 fun append(coll: Collection[E]) do for i in coll do push(i)
361
362 # Remove the last item.
363 fun pop: E is abstract
364
365 # Add an item before the last.
366 fun unshift(e: E) is abstract
367
368 # Remove the first item.
369 # The second item become the first.
370 fun shift: E is abstract
371
372 end
373
374 # Iterators on indexed collections.
375 interface IndexedIterator[E]
376 special MapIterator[Int, E]
377 # The index of the current item.
378 fun index: Int is abstract
379
380 # A synonym of index.
381 redef fun key do return index
382 end
383
384 # Associatives arrays that internally uses couples to represent each (key, value) pairs.
385 interface CoupleMap[K: Object, E]
386 special Map[K, E]
387 # Return the couple of the corresponding key
388 # Return null if the key is no associated element
389 protected fun couple_at(key: K): nullable Couple[K, E] is abstract
390
391 redef fun [](key)
392 do
393 var c = couple_at(key)
394 if c == null then
395 abort
396 else
397 return c.second
398 end
399 end
400
401 redef fun has_key(key) do return couple_at(key) != null
402 end
403
404 # Iterator on CoupleMap
405 #
406 # Actually is is a wrapper around an iterator of the internal array of the map.
407 class CoupleMapIterator[K: Object, E]
408 special MapIterator[K, E]
409 redef fun item do return _iter.item.second
410
411 #redef fun item=(e) do _iter.item.second = e
412
413 redef fun key do return _iter.item.first
414
415 redef fun is_ok do return _iter.is_ok
416
417 redef fun next
418 do
419 _iter.next
420 end
421
422 var _iter: Iterator[Couple[K,E]]
423
424 init(i: Iterator[Couple[K,E]]) do _iter = i
425 end
426
427 # Some tools ###################################################################
428
429 # Two objects in a simple structure.
430 class Couple[F, S]
431
432 # The first element of the couple.
433 readable writable var _first: F
434
435 # The second element of the couple.
436 readable writable var _second: S
437
438 # Create a new instance with a first and a second object.
439 init(f: F, s: S)
440 do
441 _first = f
442 _second = s
443 end
444 end