Class: IntervalNotation::Combiner

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

Overview

Combiner is an internal helper class for combining interval sets using sweep line. It starts moving from -∞ to +∞ and keep which intervals are crossed by sweep line. Class helps to effectively recalculate number of crossed intervals without rechecking all intervals each time, and dramatically reduces speed of operations on large number of intervals

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(num_interval_sets) ⇒ Combiner

Returns a new instance of Combiner.



12
13
14
15
16
# File 'lib/interval_notation/combiners.rb', line 12

def initialize(num_interval_sets)
  @num_interval_sets = num_interval_sets
  @inside = Array.new(num_interval_sets, false)
  @num_intervals_inside = 0 # number of intervals, we are inside (for efficiency)
end

Instance Attribute Details

#num_interval_setsObject (readonly)

Returns the value of attribute num_interval_sets.



9
10
11
# File 'lib/interval_notation/combiners.rb', line 9

def num_interval_sets
  @num_interval_sets
end

#previous_stateObject (readonly)

Returns the value of attribute previous_state.



10
11
12
# File 'lib/interval_notation/combiners.rb', line 10

def previous_state
  @previous_state
end

Instance Method Details

#pass(points_on_place) ⇒ Object

When sweep line pass several interval boundaries, #pass should get all those points at once and update status of crossing sweep line. It also stores previous state, because it’s actively used downstream.



21
22
23
24
# File 'lib/interval_notation/combiners.rb', line 21

def pass(points_on_place)
  @previous_state = state
  pass_recalculation(points_on_place)
end