Module: RangesMergerDividerEqInterval

Included in:
RangesMerger
Defined in:
lib/ranges_merger/ranges_merger_divider_eq_interval.rb

Instance Method Summary collapse

Instance Method Details

#divide_eqi(_array, interval, total_min) ⇒ Object

Divide with equal interval



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

def divide_eqi(_array, interval, total_min)
  # calculate for whole range
  joined_array = merge(_array)

  # anti-nuke
  return [] if joined_array.size == 0 or joined_array.nil?

  min = joined_array.first[0]
  min = total_min if total_min < min and not total_min.nil? # before merging
  max = joined_array.last[1]
  divided_array = divide([[min, max]], interval)

  # remove sub-ranges
  filtered_array = Array.new
  joined_array.each do |ja|
    filtered_array += divided_array.select{|da| ja[0] <= da[0] and ja[1] >= da[1] }
  end
  filtered_array = filtered_array.sort.uniq

  return filtered_array
end