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]
22 redef readable var _first
: E
24 # Get the last element.
27 # Get the element after the last one.
28 readable var _after
: E
30 redef fun has
(item
) do return item
>= _first
and item
<= _last
32 redef fun has_only
(item
) do return _first
== item
and item
== _last
or is_empty
43 redef fun iterator
do return new IteratorRange[E
](self)
47 var nb
= _first
.distance
(_after
)
55 redef fun is_empty
do return _first
>= _after
57 # Create a range [`from`, `to`].
58 # The syntax `[from..to[` is equivalent.
66 # Create a range [`from`, `to`[.
67 # The syntax `[from..to[` is equivalent.
68 init without_last
(from
: E
, to
: E
)
76 class IteratorRange[E
: Discrete]
80 redef readable var _item
: E
82 redef fun is_ok
do return _item
< _range
.after
84 redef fun next
do _item
= _item
.succ
94 # Returns the range from 0 to `self-1`, is used to do:
96 # var s = new Array[String]
97 # for i in 3.times do s.add "cool"
98 # assert s.join(" ") == "cool cool cool"
101 # for i in 10.times do s.add(i.to_s)
102 # assert s.to_s == "0123456789"
103 fun times
: Range[Int] do return [0 .. self[