From 8e2927b2d3969719059dd4025bc9e94266afe7c3 Mon Sep 17 00:00:00 2001 From: Daniel Petri Rocha Date: Thu, 16 Sep 2021 23:19:19 +0200 Subject: [PATCH] Linting with Rubocop --- lib/interval_tree.rb | 58 ++++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 29 deletions(-) mode change 100644 => 100755 lib/interval_tree.rb diff --git a/lib/interval_tree.rb b/lib/interval_tree.rb old mode 100644 new mode 100755 index 7160ba4..78bc9ab --- a/lib/interval_tree.rb +++ b/lib/interval_tree.rb @@ -1,10 +1,10 @@ #!/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 @@ -12,16 +12,16 @@ def initialize(ranges, &range_factory) 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 @@ -29,13 +29,13 @@ def divide_intervals(intervals) 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) @@ -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) @@ -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) @@ -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 @@ -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) @@ -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 @@ -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