Skip to content

Commit

Permalink
Linting with Rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
danielpetri1 committed Sep 16, 2021
1 parent 44900ec commit 8e2927b
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions lib/interval_tree.rb
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

module IntervalTree

class Tree
def initialize(ranges, &range_factory)
range_factory = lambda { |l, r| (l ... r+1) } unless block_given?
range_factory = ->(l, r) { (l...r + 1) } unless block_given?
ranges_excl = ensure_exclusive_end([ranges].flatten, range_factory)
@top_node = divide_intervals(ranges_excl)
end
attr_reader :top_node

def divide_intervals(intervals)
return nil if intervals.empty?

x_center = center(intervals)
s_center = Array.new
s_left = Array.new
s_right = Array.new
s_center = []
s_left = []
s_right = []

intervals.each do |k|
case
when k.end.to_r < x_center
if k.end.to_r < x_center
s_left << k
when k.begin.to_r > x_center
elsif k.begin.to_r > x_center
s_right << k
else
s_center << k
end
end

s_center.sort_by! { |x| [x.begin, x.end] }

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

# Search by range or point
DEFAULT_OPTIONS = { unique: true }
DEFAULT_OPTIONS = { unique: true }.freeze
def search(query, options = {})
options = DEFAULT_OPTIONS.merge(options)

Expand All @@ -45,9 +45,9 @@ def search(query, options = {})
result = top_node.search(query)
options[:unique] ? result.uniq! : result
else
result = point_search(self.top_node, query, [], options[:unique])
result = point_search(top_node, query, [], options[:unique])
end
result
result
end

def ==(other)
Expand All @@ -58,10 +58,9 @@ def ==(other)

def ensure_exclusive_end(ranges, range_factory)
ranges.map do |range|
case
when !range.respond_to?(:exclude_end?)
if !range.respond_to?(:exclude_end?)
range
when range.exclude_end?
elsif range.exclude_end?
range
else
range_factory.call(range.begin, range.end)
Expand All @@ -88,13 +87,14 @@ def point_search(node, point, result, unique)

node.s_center.each do |k|
break if k.begin > point

result << k if point < k.end
end
if node_left_node && ( point_r < node_x_center )

if node_left_node && (point_r < node_x_center)
stack << node_left_node

elsif node_right_node && ( point_r >= node_x_center )
elsif node_right_node && (point_r >= node_x_center)
stack << node_right_node
end

Expand All @@ -105,7 +105,7 @@ def point_search(node, point, result, unique)
result
end
end
end # class Tree
end

class Node
def initialize(x_center, s_center, left_node, right_node)
Expand All @@ -118,9 +118,9 @@ def initialize(x_center, s_center, left_node, right_node)

def ==(other)
x_center == other.x_center &&
s_center == other.s_center &&
left_node == other.left_node &&
right_node == other.right_node
s_center == other.s_center &&
left_node == other.left_node &&
right_node == other.right_node
end

# Search by range only
Expand Down Expand Up @@ -149,26 +149,26 @@ def search_s_center(query)
next unless
(
# k is entirely contained within the query
(k_begin_gte_q_begin) &&
(k_end_lte_q_end)
k_begin_gte_q_begin &&
k_end_lte_q_end
) || (
# k's start overlaps with the query
(k_begin_gte_q_begin) &&
k_begin_gte_q_begin &&
(k_begin < query_end)
) || (
# k's end overlaps with the query
(k_end > query_begin) &&
(k_end_lte_q_end)
k_end_lte_q_end
) || (
# k is bigger than the query
(k_begin < query_begin) &&
(k_end > query_end)
)

result << k
end

result
end
end # class Node

end # module IntervalTree
end
end

0 comments on commit 8e2927b

Please sign in to comment.