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 attr _first
: E
24 # Get the last element.
25 readable attr _last
: E
27 # Get the element after the last one.
28 readable attr _after
: E
30 redef meth has
(item
) do return item
>= _first
and item
<= _last
32 redef meth has_only
(item
) do return _first
== item
and item
== _last
34 redef meth count
(item
)
43 redef meth iterator
do return new IteratorRange[E
](self)
47 var nb
= _first
.distance
(_after
)
55 redef meth 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 attr _item
: E
82 redef meth is_ok
do return _item
< _range
.after
84 redef meth next
do _item
= _item
.succ