Module: Patches::PatchIntervalTree::ForTree

Included in:
IntervalTree::Tree
Defined in:
lib/patches/patch_interval_tree.rb

Instance Method Summary collapse

Instance Method Details

#divide_intervals(intervals) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/patches/patch_interval_tree.rb', line 4

def divide_intervals(intervals)
  return nil if intervals.empty?
  x_center = center(intervals)
  s_center = []
  s_left = []
  s_right = []

  intervals.each do |k|
    case
    when k.end && k.end != Float::INFINITY && k.end.to_r < x_center
      s_left << k
    when k.begin && k.begin.to_r > x_center
      s_right << k
    else
      s_center << k
    end
  end

  IntervalTree::Node.new(x_center, s_center, divide_intervals(s_left), divide_intervals(s_right))
end

#search(query, options = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/patches/patch_interval_tree.rb', line 25

def search(query, options = {})
  options = IntervalTree::Tree::DEFAULT_OPTIONS.merge(options)

  return nil unless @top_node

  if query.respond_to?(:first)
    result = top_node.search(query)
    options[:unique] ? result.uniq : result
  else
    point_search(top_node, query, [], options[:unique])
  end
    .sort_by{|x| [x.begin || -Float::INFINITY, x.end || Float::INFINITY] }
end