-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdump_postings.rb
More file actions
executable file
·71 lines (62 loc) · 1.44 KB
/
dump_postings.rb
File metadata and controls
executable file
·71 lines (62 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env ruby
class PostingsListNode
attr_accessor :label, :order, :next, :jump
def initialize(label, nxxt=nil, jump=nil)
self.label = label
self.order = -1
self.next = nxxt
self.jump = jump
end
def to_string
memo = "[#{label}:#{order}]→ "
if self.next
memo += self.next.to_string
else
memo += 'x'
end
memo
end
def self.set_jump_order_recursive(node, order=0)
if node and node.order == -1
node.order = order +=1
set_jump_order_recursive node.jump, order
set_jump_order_recursive node.next, order
end
end
def self.set_jump_order_iterative(head)
stack = []
order = 0
stack.push head
while stack.length > 0
node = stack.last
stack.pop
if node and node.order == -1
node.order = order +=1
stack.push node.next
stack.push node.jump
end
end
end
end
# ↱-------↘
# [a]→ [b]→ [c]→ [d]→ x
# ↳-------↗ | ↑↓
# ↖___↙
def example_list
_d = PostingsListNode.new('d')
_c = PostingsListNode.new('c', _d)
_b = PostingsListNode.new('b', _c, _d)
_a = PostingsListNode.new('a', _b, _c)
_d.jump = _d
_c.jump = _b
_a
end
e1 = example_list
PostingsListNode.set_jump_order_recursive e1
puts e1.to_string
e2 = example_list
PostingsListNode.set_jump_order_iterative e2
puts e2.to_string
__END__
[a:1]→ [b:3]→ [c:2]→ [d:4]→ x
[a:1]→ [b:3]→ [c:2]→ [d:4]→ x