Class: IntervalSkipList

Inherits:
Object
  • Object
show all
Defined in:
lib/treetop/runtime/interval_skip_list/node.rb,
lib/treetop/runtime/interval_skip_list/head_node.rb,
lib/treetop/runtime/interval_skip_list/interval_skip_list.rb

Defined Under Namespace

Classes: HeadNode, Node

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIntervalSkipList


4
5
6
7
8
# File 'lib/treetop/runtime/interval_skip_list/interval_skip_list.rb', line 4

def initialize
  @head = HeadNode.new(max_height)
  @ranges = {}
  @probability = 0.5
end

Instance Attribute Details

#probabilityObject (readonly)

Returns the value of attribute probability


2
3
4
# File 'lib/treetop/runtime/interval_skip_list/interval_skip_list.rb', line 2

def probability
  @probability
end

Instance Method Details

#containing(n) ⇒ Object


36
37
38
# File 'lib/treetop/runtime/interval_skip_list/interval_skip_list.rb', line 36

def containing(n)
  containing_with_node(n).first
end

#delete(marker) ⇒ Object


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/treetop/runtime/interval_skip_list/interval_skip_list.rb', line 64

def delete(marker)
  range = ranges[marker]
  path_to_first_node = make_path
  first_node = find(range.first, path_to_first_node)

  cur_node = first_node
  cur_level = first_node.top_level
  while next_node_at_level_inside_range?(cur_node, cur_level, range)
    while can_ascend_from?(cur_node, cur_level) && next_node_at_level_inside_range?(cur_node, cur_level + 1, range)
      cur_level += 1
    end
    cur_node = unmark_forward_path_at_level(cur_node, cur_level, marker)
  end

  while node_inside_range?(cur_node, range)
    while can_descend_from?(cur_level) && next_node_at_level_outside_range?(cur_node, cur_level, range)
      cur_level -= 1
    end
    cur_node = unmark_forward_path_at_level(cur_node, cur_level, marker)
  end
  last_node = cur_node

  first_node.endpoint_of.delete(marker)
  if first_node.endpoint_of.empty?
    first_node.delete(path_to_first_node)
  end

  last_node.endpoint_of.delete(marker)
  if last_node.endpoint_of.empty?
    path_to_last_node = make_path
    find(range.last, path_to_last_node)
    last_node.delete(path_to_last_node)
  end
end

#empty?Boolean


14
15
16
# File 'lib/treetop/runtime/interval_skip_list/interval_skip_list.rb', line 14

def empty?
  head.forward[0].nil?
end

#expire(range, length_change) ⇒ Object


18
19
20
21
22
# File 'lib/treetop/runtime/interval_skip_list/interval_skip_list.rb', line 18

def expire(range, length_change)
  expired_markers, first_node_after_range = overlapping(range)
  expired_markers.each { |marker| delete(marker) }
  first_node_after_range.propagate_length_change(length_change)    
end

#insert(range, marker) ⇒ Object


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/treetop/runtime/interval_skip_list/interval_skip_list.rb', line 40

def insert(range, marker)
  ranges[marker] = range
  first_node = insert_node(range.first)
  first_node.endpoint_of.push(marker)
  last_node = insert_node(range.last)
  last_node.endpoint_of.push(marker)

  cur_node = first_node
  cur_level = first_node.top_level
  while next_node_at_level_inside_range?(cur_node, cur_level, range)
    while can_ascend_from?(cur_node, cur_level) && next_node_at_level_inside_range?(cur_node, cur_level + 1, range)
      cur_level += 1
    end
    cur_node = mark_forward_path_at_level(cur_node, cur_level, marker)
  end

  while node_inside_range?(cur_node, range)
    while can_descend_from?(cur_level) && next_node_at_level_outside_range?(cur_node, cur_level, range)
      cur_level -= 1 
    end
    cur_node = mark_forward_path_at_level(cur_node, cur_level, marker)
  end
end

#max_heightObject


10
11
12
# File 'lib/treetop/runtime/interval_skip_list/interval_skip_list.rb', line 10

def max_height
  3
end

#overlapping(range) ⇒ Object


24
25
26
27
28
29
30
31
32
33
34
# File 'lib/treetop/runtime/interval_skip_list/interval_skip_list.rb', line 24

def overlapping(range)
  markers, first_node = containing_with_node(range.first)

  cur_node = first_node
  begin
    markers.concat(cur_node.forward_markers.flatten)
    cur_node = cur_node.forward[0]
  end while cur_node.key < range.last

  return markers.uniq, cur_node
end