class Adarwin::Interval
This class represents an interval [a..b] including a and b. The class has the following methods:
-
Initialise the interval (
initialize
) -
Print the interval (
to_s
) -
Merge an interval with another interval (
merge
) -
Return the length of the interval (
length
)
Attributes
Public Class Methods
Initialise the interval. This method performs a comparison to see whether a or b is the upper-bound. This comparison is based on guesses made by the compare
method. This method uses loop information if needed. FIXME: Uses the compare
method which might be based on a guess
# File lib/adarwin/interval.rb 17 def initialize(a,b,loops) 18 @loops = loops 19 a = simplify(a.to_s) 20 b = simplify(b.to_s) 21 case compare(a,b,@loops) 22 when 'lt' || 'eq' then @a = a; @b = b 23 when 'gt' then @a = b; @b = a 24 else @a = a; @b = b 25 end 26 end
Public Instance Methods
Method to compute the length of the interval. For example, the length of
- a..b
-
is equal to (b-a+1).
# File lib/adarwin/interval.rb 52 def length 53 simplify("(#{@b})-(#{a})+1") 54 end
Merge this interval with another interval. This is based on a comparison made by the compare
method, which is an approximation based on loop information. FIXME: Uses the compare
method which might be based on a guess
# File lib/adarwin/interval.rb 37 def merge(other_interval) 38 @a = case compare(@a,other_interval.a,@loops) 39 when 'gt' || 'eq' then other_interval.a 40 when 'lt' then @a 41 else other_interval.a 42 end 43 @b = case compare(@b,other_interval.b,@loops) 44 when 'gt' || 'eq' then @b 45 when 'lt' then other_interval.b 46 else @b 47 end 48 end
Print the interval as a string (e.g. [4..9]).
# File lib/adarwin/interval.rb 29 def to_s 30 @a+RANGE_SEP+@b 31 end