class Overlap::Detect

Attributes

overlaps[R]

Public Class Methods

new(collection) click to toggle source
# File lib/overlap/detect.rb, line 6
def initialize(collection)
  @collection = collection
  @overlaps = []
  build!
end

Private Instance Methods

build!() click to toggle source
# File lib/overlap/detect.rb, line 14
def build!
  destructive_collection = @collection.dup.to_a
  current_segment = nil

  while (one = destructive_collection.shift) do
    current_segment = Segment.new(one.start_position, one.end_position)
    segments = []
    segments << one

    @collection.map do |other|
      next if current_segment.same?(other)
      if current_segment.overlap?(other)
        current_segment.merge!(other)
        segments << other
      end
    end.compact

    segments.each { |e| @collection.delete(e) }
    segments.each { |e| destructive_collection.delete(e) }

    @overlaps << { union: current_segment, segments: segments }
  end
end