Class: IntervalNotation::Segmentation
- Inherits:
-
Struct
- Object
- Struct
- IntervalNotation::Segmentation
- Defined in:
- lib/interval_notation/segmentation.rb,
lib/interval_notation/segmentation.rb
Overview
Wqe have to reopen class to make a lexical scope for Segmentation::Segment
Defined Under Namespace
Classes: Segment
Instance Attribute Summary collapse
-
#segments ⇒ Object
Returns the value of attribute segments.
Instance Method Summary collapse
-
#boolean_segmentation(&block) ⇒ Object
Make true/false segmentation based on block result.
-
#initialize(segments, skip_validation: false, skip_normalization: false, &block) ⇒ Segmentation
constructor
Don’t skip validation unless you’re sure, that Segmentation is a correct one Don’t skip normalization unless you’re sure, that all adjacent intervals are glued together: without normalization step comparison of two segmentations will give wrong results.
- #inspect ⇒ Object
-
#make_interval_set(&block) ⇒ Object
Transform segmentation into interval set according with block result.
-
#map_state(&block) ⇒ Object
Method ‘#map_state` returns a new segmentation with the same boundaries and different states Block for `#map_state` takes a segment and returns new state of segment.
-
#segment_covering_point(value) ⇒ Object
Find a segment in which specifying point falls.
- #to_s ⇒ Object
Constructor Details
#initialize(segments, skip_validation: false, skip_normalization: false, &block) ⇒ Segmentation
Don’t skip validation unless you’re sure, that Segmentation is a correct one Don’t skip normalization unless you’re sure, that all adjacent intervals are glued together: without normalization step comparison of two segmentations will give wrong results.
33 34 35 36 37 38 39 |
# File 'lib/interval_notation/segmentation.rb', line 33 def initialize(segments, skip_validation: false, skip_normalization: false, &block) super(segments, &block) unless skip_validation raise 'Segmentation is not valid' unless valid? end join_same_state_segments! unless skip_normalization end |
Instance Attribute Details
#segments ⇒ Object
Returns the value of attribute segments
13 14 15 |
# File 'lib/interval_notation/segmentation.rb', line 13 def segments @segments end |
Instance Method Details
#boolean_segmentation(&block) ⇒ Object
Make true/false segmentation based on block result. Block takes a Segment. If block not specified, state is converted to boolean. If block specified, its result is converted to boolean.
79 80 81 82 83 84 85 |
# File 'lib/interval_notation/segmentation.rb', line 79 def boolean_segmentation(&block) if block_given? map_state{|segment| !!block.call(segment) } else map_state{|segment| !!segment.state } end end |
#inspect ⇒ Object
74 |
# File 'lib/interval_notation/segmentation.rb', line 74 def inspect; to_s; end |
#make_interval_set(&block) ⇒ Object
Transform segmentation into interval set according with block result. Block takes a Segment and its result’d indicate whether to include corresponding interval into interval set
89 90 91 92 |
# File 'lib/interval_notation/segmentation.rb', line 89 def make_interval_set(&block) intervals = boolean_segmentation(&block).segments.select(&:state).map(&:interval) IntervalSet.new( intervals ) end |
#map_state(&block) ⇒ Object
Method ‘#map_state` returns a new segmentation with the same boundaries and different states Block for `#map_state` takes a segment and returns new state of segment
96 97 98 99 |
# File 'lib/interval_notation/segmentation.rb', line 96 def map_state(&block) new_segments = segments.map{|segment| Segment.new(segment.interval, block.call(segment)) } Segmentation.new(new_segments, skip_validation: true) # here we can skip validation but not normalization end |
#segment_covering_point(value) ⇒ Object
Find a segment in which specifying point falls. It always exist, so it can’t return nil.
102 103 104 105 106 107 108 109 110 |
# File 'lib/interval_notation/segmentation.rb', line 102 def segment_covering_point(value) segment_index = (0...segments.size).bsearch{|segment_index| value <= segments[segment_index].interval.to } segment = segments[segment_index] if segment.interval.include_position?(value) segment else segments[segment_index + 1] end end |
#to_s ⇒ Object
73 |
# File 'lib/interval_notation/segmentation.rb', line 73 def to_s; "Segmentation: #{segments}"; end |