Skip to content

Commit

Permalink
Preliminary implementation of stabbing query excluding endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
nathansobo committed Jan 20, 2008
1 parent 0489239 commit d24a646
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
13 changes: 11 additions & 2 deletions lib/treetop/runtime/interval_skip_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,16 @@ def empty?
end

def containing(n)
[]
containing = []
cur_node = head
(max_height - 1).downto(0) do |cur_level|
while (next_node = cur_node.forward[cur_level]) && next_node.key <= n
cur_node = next_node
return containing if cur_node.key == n
end
containing.concat(cur_node.markers[cur_level])
end
containing
end

def insert(range, marker)
Expand Down Expand Up @@ -54,7 +63,7 @@ def delete(key)
found_node = find(key, path)
found_node.remove(path) if found_node.key == key
end

def find(key, path)
cur_node = head
(max_height - 1).downto(0) do |cur_level|
Expand Down
48 changes: 45 additions & 3 deletions spec/runtime/interval_skip_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,44 @@ def expected_node_heights
[2, 3, 2, 3, 1]
end

def confirm_containing_intervals(range, *markers)
(range.begin).upto(range.end) do |i|
list.containing(i).should have_markers(*markers)
end
end

def contain_marker(marker)
ContainMarkers.new(list, [marker])
end

def contain_markers(*markers)
ContainMarkers.new(list, markers)
end

class ContainMarkers
attr_reader :failure_message

def initialize(list, expected_markers)
@list = list
@expected_markers = expected_markers
end

def matches?(target_range)
@target_range = target_range

@target_range.each do |i|
markers = @list.containing(i)
@expected_markers.each do |expected_marker|
unless markers.include?(expected_marker)
@failure_message = "Expected #{expected_marker.inspect} to contain #{i}, but it doesn't. #{i} is contained by: #{markers.inspect}."
return false
end
end
end
true
end
end

def have_markers(*markers)
HaveMarkers.new(markers)
end
Expand All @@ -67,7 +105,7 @@ def matches?(target)
end

def failure_message
"expected #{@target.inspect} to include only #{@expected_markers.inspect}"
"Expected #{@target.inspect} to include only #{@expected_markers.inspect}"
end
end

Expand All @@ -76,8 +114,12 @@ def failure_message
list.insert(1..7, :a)
end

describe "#containing" do
it "returns nothing for 1 or 7" do
describe ", #containing" do
it "returns only :a from 2 through 6" do
(2..6).should contain_marker(:a)
end

it "returns nothing at 1 and 7" do
list.containing(1).should be_empty
list.containing(7).should be_empty
end
Expand Down

0 comments on commit d24a646

Please sign in to comment.