Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions lib/interval_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,24 @@ def center(intervals)
) / 2
end

def point_search(node, point, result, unique = true)
node.s_center.each do |k|
if k.include?(point)
result << k
def point_search(node, point, result, unique)
stack = [node]

until stack.empty?
node = stack.pop

node.s_center.each do |k|
if k.begin <= point && point < k.end
result << k
end
end
end
if node.left_node && ( point.to_r < node.x_center )
point_search(node.left_node, point, []).each{|k|result << k}
end
if node.right_node && ( point.to_r >= node.x_center )
point_search(node.right_node, point, []).each{|k|result << k}
if node.left_node && ( point.to_r < node.x_center )
stack << node.left_node

elsif node.right_node && ( point.to_r >= node.x_center )
stack << node.right_node
end

end
if unique
result.uniq
Expand Down
40 changes: 37 additions & 3 deletions spec/interval_tree_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,18 @@ def initialize(l, r, value = nil)
end
end

context 'with unique defaulting to true' do
context 'given intervals with duplicates' do
it 'returns the duplicates in the result' do
context 'with unique defaulting to true, given intervals with duplicates' do
context 'given [(1...3), (3...5), (3...9), (4...8), (4...8)] and a query by (3...5)]' do
it 'removes the duplicates in the result' do
itv = [(1...3), (3...5), (3...9), (4...8), (4...8)]
t = IntervalTree::Tree.new(itv)
results = t.search((3...5))
expect(results).to match_array([(3...5), (3...9), (4...8)])
end
end

context 'given [(0...3), (1...4), (3...5), (0...3)] and a query by (2)' do
it 'removes the duplicates in the result' do
itv = [(0...3), (1...4), (3...5), (0...3)]
t = IntervalTree::Tree.new(itv)
results = t.search(2)
Expand All @@ -303,6 +312,31 @@ def initialize(l, r, value = nil)
expect(results).to match_array([(2...4)])
end
end

context 'given [(0...5), (1...5), (3...5), (3...5)] and a query by (3)' do
it 'returns [(0...5), (1...5), (3...5), (3...5)]' do
itvs = [(0...5), (1...5), (3...5), (3...5)]
results = IntervalTree::Tree.new(itvs).search(3, unique: false)
expect(results).to match_array([(0...5), (1...5), (3...5), (3...5)])
end
end

context 'given [(0...3), (1...4), (3...4), (3...4), (3...5)] and a query by (3)' do
it 'returns [(1...4), (3...4), (3...4), (3...5)]' do
itvs = [(0...3), (1...4), (3...4), (3...4), (3...5)]
results = IntervalTree::Tree.new(itvs).search(3, unique: false)
expect(results).to match_array([(1...4), (3...4), (3...4), (3...5)])
end
end

context 'given [(0...2), (0...2), (1...2), (1...2), (2...5)] and a query by (1)' do
it 'returns [(0...2), (0...2), (1...2), (1...2)]' do
itvs = [(0...2), (0...2), (1...2), (1...2), (2...5)]
results = IntervalTree::Tree.new(itvs).search(1, unique: false)
expect(results).to match_array([(0...2), (0...2), (1...2), (1...2)])
end
end

end

context "when concerned with performance" do
Expand Down