Skip to content

Commit

Permalink
Skip list insertion is complete but there are more specs to write jus…
Browse files Browse the repository at this point in the history
…t to be thorough
  • Loading branch information
nathansobo committed Jan 18, 2008
1 parent c67f87c commit 0618ced
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
43 changes: 36 additions & 7 deletions lib/treetop/runtime/interval_skip_list.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
class IntervalSkipList
attr_reader :nodes, :head
attr_reader :head

def initialize
@node_heights = [1, 2, 1, 3, 1, 2, 2]
@nodes = []
@head = Node.new(nil, max_height)
end

Expand All @@ -16,16 +15,46 @@ def empty?
end

def insert(value)
node = Node.new(value, next_node_height)
nodes << node
0.upto(node.height - 1) do |i|
head.next[i] = node
path = Array.new(max_height, nil)
found_node = find(value, path)
if found_node && found_node.value == value
return found_node
else
return make_node(value, path)
end
node
end

def nodes
nodes = []
cur_node = head.next[0]
until cur_node.nil?
nodes << cur_node
cur_node = cur_node.next[0]
end
nodes
end

protected
attr_reader :node_heights
def find(value, path)
cur_node = head
(max_height - 1).downto(0) do |cur_height|
while (next_node = cur_node.next[cur_height]) && next_node.value < value
cur_node = next_node
end
path[cur_height] = cur_node
end
cur_node.next[0]
end

def make_node(value, path)
new_node = Node.new(value, next_node_height)
0.upto(new_node.height - 1) do |i|
new_node.next[i] = path[i].next[i]
path[i].next[i] = new_node
end
return new_node
end

def next_node_height
height = node_heights.shift
Expand Down
16 changes: 13 additions & 3 deletions spec/runtime/interval_skip_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

before do
@list = IntervalSkipList.new
@expected_node_heights = [01, 2, 1, 3, 1, 2, 2]
@expected_node_heights = [1, 2, 1, 3, 1, 2, 2]
end

describe "when nothing has been inserted" do
Expand All @@ -34,7 +34,7 @@
head.height.should == list.max_height
end

it "has null next pointers" do
it "has nil next pointers" do
0.upto(list.max_height - 1) do |i|
head.next[i].should be_nil
end
Expand Down Expand Up @@ -84,7 +84,7 @@
inserted_node.value.should == 1
end

it "has null next pointers" do
it "has nil next pointers" do
inserted_node.next.each do |next_pointer|
next_pointer.should be_nil
end
Expand Down Expand Up @@ -121,6 +121,10 @@
it "has a height of the first expected node height" do
inserted_node.height.should == expected_node_heights[0]
end

it "has its single next pointer pointing at the second inserted node" do
inserted_node.next[0].should == inserted_nodes[1]
end
end

describe "the second inserted node" do
Expand All @@ -137,6 +141,12 @@
it "has a height of the second expected node height" do
inserted_node.height.should == expected_node_heights[1]
end

it "has nil next pointers" do
inserted_node.next.each do |next_pointer|
next_pointer.should be_nil
end
end
end
end

Expand Down

0 comments on commit 0618ced

Please sign in to comment.