standard: add `Range::reverse_iterator`
[nit.git] / lib / standard / collection / range.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 # Module for range of discrete objects.
14 module range
15
16 import abstract_collection
17
18 # Range of discrete objects.
19 class Range[E: Discrete]
20 super Collection[E]
21
22 redef var first: E
23
24 # Get the last element.
25 var last: E
26
27 # Get the element after the last one.
28 var after: E
29
30 # assert [1..10].has(5)
31 # assert [1..10].has(10)
32 # assert not [1..10[.has(10)
33 redef fun has(item) do return item >= first and item <= last
34
35 # assert [1..1].has_only(1)
36 # assert not [1..10].has_only(1)
37 redef fun has_only(item) do return first == item and item == last or is_empty
38
39 # assert [1..10].count(1) == 1
40 # assert [1..10].count(0) == 0
41 redef fun count(item)
42 do
43 if has(item) then
44 return 1
45 else
46 return 0
47 end
48 end
49
50 redef fun iterator do return new IteratorRange[E](self)
51
52 # Gets an iterator starting at the end and going backwards
53 #
54 # var reviter = [1..4].reverse_iterator
55 # assert reviter.to_a == [4,3,2,1]
56 #
57 # reviter = [1..4[.reverse_iterator
58 # assert reviter.to_a == [3,2,1]
59 fun reverse_iterator: Iterator[E] do return new ReverseIteratorRange[E](self)
60
61 # assert [1..10].length == 10
62 # assert [1..10[.length == 9
63 # assert [1..1].length == 1
64 # assert [1..-10].length == 0
65 redef fun length
66 do
67 if is_empty then return 0
68 var nb = first.distance(after)
69 if nb > 0 then
70 return nb
71 else
72 return 0
73 end
74 end
75
76 # assert not [1..10[.is_empty
77 # assert not [1..1].is_empty
78 # assert [1..-10].is_empty
79 redef fun is_empty do return first >= after
80
81 # Create a range [`from`, `to`].
82 # The syntax `[from..to]` is equivalent.
83 #
84 # var a = [10..15]
85 # var b = new Range[Int] (10,15)
86 # assert a == b
87 # assert a.to_a == [10, 11, 12, 13, 14, 15]
88 init(from: E, to: E) is old_style_init do
89 first = from
90 last = to
91 after = to.successor(1)
92 end
93
94 # Create a range [`from`, `to`[.
95 # The syntax `[from..to[` is equivalent.
96 #
97 # var a = [10..15[
98 # var b = new Range[Int].without_last(10,15)
99 # assert a == b
100 # assert a.to_a == [10, 11, 12, 13, 14]
101 init without_last(from: E, to: E)
102 do
103 first = from
104 last = to.predecessor(1)
105 after = to
106 end
107
108 # Two ranges are equals if they have the same first and last elements.
109 #
110 # var a = new Range[Int](10, 15)
111 # var b = new Range[Int].without_last(10, 15)
112 # assert a == [10..15]
113 # assert a == [10..16[
114 # assert not a == [10..15[
115 # assert b == [10..15[
116 # assert b == [10..14]
117 # assert not b == [10..15]
118 redef fun ==(o) do
119 return o isa Range[E] and self.first == o.first and self.last == o.last
120 end
121
122 # var a = new Range[Int](10, 15)
123 # assert a.hash == 455
124 # var b = new Range[Int].without_last(10, 15)
125 # assert b.hash == 432
126 redef fun hash do
127 # 11 and 23 are magic numbers empirically determined to be not so bad.
128 return first.hash * 11 + last.hash * 23
129 end
130 end
131
132 # Iterator on ranges.
133 private class IteratorRange[E: Discrete]
134 super Iterator[E]
135 var range: Range[E]
136 redef var item is noinit
137
138 redef fun is_ok do return _item < _range.after
139
140 redef fun next do _item = _item.successor(1)
141
142 init
143 do
144 _item = _range.first
145 end
146 end
147
148 # Reverse iterator on ranges.
149 private class ReverseIteratorRange[E: Discrete]
150 super Iterator[E]
151 var range: Range[E]
152 redef var item is noinit
153
154 redef fun is_ok do return _item >= _range.first
155
156 redef fun next do _item = _item.predecessor(1)
157
158 init
159 do
160 _item = _range.last
161 end
162 end
163
164 redef class Int
165 # Returns the range from 0 to `self-1`, is used to do:
166 #
167 # var s = new Array[String]
168 # for i in 3.times do s.add "cool"
169 # assert s.join(" ") == "cool cool cool"
170 #
171 # s.clear
172 # for i in 10.times do s.add(i.to_s)
173 # assert s.to_s == "0123456789"
174 fun times: Range[Int] do return [0 .. self[
175 end