Class: SegmentTree::Segment
- Inherits:
-
Object
- Object
- SegmentTree::Segment
- Defined in:
- lib/segment_tree.rb
Overview
An elementary interval
Instance Attribute Summary collapse
-
#range ⇒ Object
readonly
:nodoc:all:.
-
#value ⇒ Object
readonly
:nodoc:all:.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
segments are sorted from left to right, from shortest to longest.
- #==(other) ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(range, value) ⇒ Segment
constructor
A new instance of Segment.
- #marshal_dump ⇒ Object
- #marshal_load(serialized_tree) ⇒ Object
Constructor Details
#initialize(range, value) ⇒ Segment
Returns a new instance of Segment.
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
#range ⇒ Object (readonly)
:nodoc:all:
19 20 21 |
# File 'lib/segment_tree.rb', line 19 def range @range end |
#value ⇒ Object (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
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 |
#hash ⇒ Object
47 48 49 |
# File 'lib/segment_tree.rb', line 47 def hash [@range, @value].hash end |
#marshal_dump ⇒ Object
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 |