Class: SegmentTree::Segment

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

Overview

An elementary interval

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(range, value) ⇒ Segment

Returns a new instance of Segment.

Raises:

  • (ArgumentError)


21
22
23
24
25
# File 'lib/segment_tree.rb', line 21

def initialize(range, value)
  raise ArgumentError, 'Range expected, %s given' % range.class.name unless range.is_a?(Range)

  @range, @value = range, value
end

Instance Attribute Details

#rangeObject (readonly)

:nodoc:all:



19
20
21
# File 'lib/segment_tree.rb', line 19

def range
  @range
end

#valueObject (readonly)

:nodoc:all:



19
20
21
# File 'lib/segment_tree.rb', line 19

def value
  @value
end

Instance Method Details

#<=>(other) ⇒ Object

segments are sorted from left to right, from shortest to longest



28
29
30
31
32
33
# File 'lib/segment_tree.rb', line 28

def <=>(other)
  case cmp = @range.begin <=> other.range.begin
    when 0 then @range.end <=> other.range.end
    else cmp
  end
end