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

#==(other) ⇒ Object



35
36
37
38
39
# File 'lib/segment_tree.rb', line 35

def ==(other)
  other.is_a?(self.class) &&
    @range == other.range &&
    @value == other.value
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/segment_tree.rb', line 41

def eql?(other)
  other.is_a?(self.class) &&
    @range.eql?(other.range) &&
    @value.eql?(other.value)
end

#hashObject



47
48
49
# File 'lib/segment_tree.rb', line 47

def hash
  [@range, @value].hash
end

#marshal_dumpObject



51
52
53
54
55
56
# File 'lib/segment_tree.rb', line 51

def marshal_dump
  {
    range: @range,
    value: @value,
  }
end

#marshal_load(serialized_tree) ⇒ Object



58
59
60
61
# File 'lib/segment_tree.rb', line 58

def marshal_load(serialized_tree)
  @range = serialized_tree[:range]
  @value = serialized_tree[:value]
end