Class: Stupidedi::Zipper::MemoizedCursor
Instance Attribute Summary collapse
Querying the Tree Location
collapse
Traversing the Tree
collapse
Editing the Tree
collapse
Instance Method Summary
collapse
#append_child, #between, #child, #children, #dangle, #depth, #descendant, #down, #first?, #flatten, #insert_left, #insert_right, #last?, #prepend_child, #root
Constructor Details
#initialize(node, path, parent) ⇒ MemoizedCursor
Returns a new instance of MemoizedCursor.
20
21
22
23
|
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 20
def initialize(node, path, parent)
@node, @path, @parent =
node, path, parent
end
|
Instance Attribute Details
#node ⇒ #leaf?, ...
11
12
13
|
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 11
def node
@node
end
|
18
19
20
|
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 18
def parent
@parent
end
|
14
15
16
|
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 14
def path
@path
end
|
Instance Method Details
90
91
92
93
|
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 90
def append(node)
EditedCursor.new(node,
Hole.new(@node.cons(@path.left), @path.parent, @path.right), @parent)
end
|
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 107
def delete
if not last?
head, *tail = @path.right
EditedCursor.new(head,
Hole.new(@path.left, @path.parent, tail), @parent)
elsif not first?
head, *tail = @path.left
EditedCursor.new(head,
Hole.new(tail, @path.parent, @path.right), @parent)
else
parent =
@parent.node.copy(:children =>
@path.left.reverse.concat(@path.right))
EditedCursor.new(parent, @path.parent, @parent.parent).dangle
end
end
|
75
76
77
|
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 75
def first
@parent.down
end
|
80
81
82
83
84
|
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 80
def last
current = self
current = current.next until current.last?
current
end
|
#leaf? ⇒ Boolean
28
29
30
|
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 28
def leaf?
@node.leaf? or @node.children.empty?
end
|
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 45
def next
@__next ||= begin
if last?
raise Exceptions::ZipperError,
"cannot move to next after last node"
end
head, *tail = @path.right
MemoizedCursor.new(head,
Hole.new(@node.cons(@path.left), @path.parent, tail), @parent)
end
end
|
96
97
98
99
|
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 96
def prepend
EditedCursor.new(node,
Hole.new(@path.left, @path.parent, @node.cons(@path.right)), @parent)
end
|
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 60
def prev
@__prev ||= begin
if first?
raise Exceptions::ZipperError,
"cannot move to prev before first node"
end
head, *tail = @path.left
MemoizedCursor.new(head,
Hole.new(tail, @path.parent, @node.cons(@path.right)), @parent)
end
end
|
102
103
104
|
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 102
def replace(node)
EditedCursor.new(node, @path, @parent)
end
|
#root? ⇒ Boolean
32
33
34
|
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 32
def root?
false
end
|
40
41
42
|
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 40
def up
@parent
end
|