Class: Adarwin::Interval

Inherits:
Object
  • Object
show all
Defined in:
lib/adarwin/interval.rb

Overview

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+)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a, b, loops) ⇒ Interval

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



17
18
19
20
21
22
23
24
25
26
# File 'lib/adarwin/interval.rb', line 17

def initialize(a,b,loops)
	@loops = loops
	a = simplify(a.to_s)
	b = simplify(b.to_s)
	case compare(a,b,@loops)
		when 'lt' || 'eq' then @a = a; @b = b
		when 'gt' then @a = b; @b = a
		else @a = a; @b = b
	end
end

Instance Attribute Details

#aObject

Returns the value of attribute a.



11
12
13
# File 'lib/adarwin/interval.rb', line 11

def a
  @a
end

#bObject

Returns the value of attribute b.



11
12
13
# File 'lib/adarwin/interval.rb', line 11

def b
  @b
end

Instance Method Details

#lengthObject

Method to compute the length of the interval. For example, the length of [a..b] is equal to (b-a+1).



52
53
54
# File 'lib/adarwin/interval.rb', line 52

def length
	simplify("(#{@b})-(#{a})+1")
end

#merge(other_interval) ⇒ Object

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



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/adarwin/interval.rb', line 37

def merge(other_interval)
	@a = case compare(@a,other_interval.a,@loops)
		when 'gt' || 'eq' then other_interval.a
		when 'lt' then @a
		else other_interval.a
	end
	@b = case compare(@b,other_interval.b,@loops)
		when 'gt' || 'eq' then @b
		when 'lt' then other_interval.b
		else @b
	end
end

#to_sObject

Print the interval as a string (e.g. [4..9]).



29
30
31
# File 'lib/adarwin/interval.rb', line 29

def to_s
	@a+RANGE_SEP+@b
end