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 # Module for range of discrete objects.
16 import abstract_collection
18 # Range of discrete objects.
19 class Range[E
: Discrete]
24 # Get the last element.
27 # Get the element after the last one.
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
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
39 # assert [1..10].count(1) == 1
40 # assert [1..10].count(0) == 0
50 redef fun iterator
do return new IteratorRange[E
](self)
52 # assert [1..10].length == 10
53 # assert [1..10[.length == 9
54 # assert [1..1].length == 1
55 # assert [1..-10].length == 0
58 if is_empty
then return 0
59 var nb
= first
.distance
(after
)
67 # assert not [1..10[.is_empty
68 # assert not [1..1].is_empty
69 # assert [1..-10].is_empty
70 redef fun is_empty
do return first
>= after
72 # Create a range [`from`, `to`].
73 # The syntax `[from..to]` is equivalent.
76 # var b = new Range[Int] (10,15)
78 # assert a.to_a == [10, 11, 12, 13, 14, 15]
79 init(from
: E
, to
: E
) is old_style_init
do
82 after
= to
.successor
(1)
85 # Create a range [`from`, `to`[.
86 # The syntax `[from..to[` is equivalent.
89 # var b = new Range[Int].without_last(10,15)
91 # assert a.to_a == [10, 11, 12, 13, 14]
92 init without_last
(from
: E
, to
: E
)
95 last
= to
.predecessor
(1)
99 # Two ranges are equals if they have the same first and last elements.
101 # var a = new Range[Int](10, 15)
102 # var b = new Range[Int].without_last(10, 15)
103 # assert a == [10..15]
104 # assert a == [10..16[
105 # assert not a == [10..15[
106 # assert b == [10..15[
107 # assert b == [10..14]
108 # assert not b == [10..15]
110 return o
isa Range[E
] and self.first
== o
.first
and self.last
== o
.last
113 # var a = new Range[Int](10, 15)
114 # assert a.hash == 455
115 # var b = new Range[Int].without_last(10, 15)
116 # assert b.hash == 432
118 # 11 and 23 are magic numbers empirically determined to be not so bad.
119 return first
.hash
* 11 + last
.hash
* 23
123 private class IteratorRange[E
: Discrete]
124 # Iterator on ranges.
127 redef var item
is noinit
129 redef fun is_ok
do return _item
< _range
.after
131 redef fun next
do _item
= _item
.successor
(1)
140 # Returns the range from 0 to `self-1`, is used to do:
142 # var s = new Array[String]
143 # for i in 3.times do s.add "cool"
144 # assert s.join(" ") == "cool cool cool"
147 # for i in 10.times do s.add(i.to_s)
148 # assert s.to_s == "0123456789"
149 fun times
: Range[Int] do return [0 .. self[