Class: Stupidedi::Zipper::MemoizedCursor

Inherits:
AbstractCursor show all
Defined in:
lib/stupidedi/zipper/memoized_cursor.rb

Instance Attribute Summary collapse

Querying the Tree Location collapse

Traversing the Tree collapse

Editing the Tree collapse

Instance Method Summary collapse

Methods inherited from AbstractCursor

#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.



16
17
18
19
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 16

def initialize(node, path, parent)
  @node, @path, @parent =
    node, path, parent
end

Instance Attribute Details

#node#leaf?, ... (readonly)

Returns:



7
8
9
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 7

def node
  @node
end

#parentAbstractCursor (readonly)

Returns:



14
15
16
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 14

def parent
  @parent
end

#pathHole (readonly)

Returns:



10
11
12
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 10

def path
  @path
end

Instance Method Details

#append(node) ⇒ EditedCursor

Returns:



86
87
88
89
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 86

def append(node)
  EditedCursor.new(node,
    Hole.new(@node.cons(@path.left), @path.parent, @path.right), @parent)
end

#deleteEditedCursor

Returns:



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 103

def delete
  if not last?
    # Move to `next`
    head, *tail = @path.right

    EditedCursor.new(head,
      Hole.new(@path.left, @path.parent, tail), @parent)
  elsif not first?
    # Move to `prev`
    head, *tail = @path.left

    EditedCursor.new(head,
      Hole.new(tail, @path.parent, @path.right), @parent)
  else
    # Deleting the only child
    parent =
      @parent.node.copy(:children =>
        @path.left.reverse.concat(@path.right))

    EditedCursor.new(parent, @path.parent, @parent.parent).dangle
  end
end

#firstMemoizedCursor

Returns:



71
72
73
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 71

def first
  @parent.down
end

#lastMemoizedCursor

Returns:



76
77
78
79
80
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 76

def last
  current = self
  current = current.next until current.last?
  current
end

#leaf?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 24

def leaf?
  @node.leaf? or @node.children.empty?
end

#nextMemoizedCursor

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 41

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

#prependEditedCursor

Returns:



92
93
94
95
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 92

def prepend
  EditedCursor.new(node,
    Hole.new(@path.left, @path.parent, @node.cons(@path.right)), @parent)
end

#prevMemoizedCursor

Returns:



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 56

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

#replace(node) ⇒ EditedCursor

Returns:



98
99
100
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 98

def replace(node)
  EditedCursor.new(node, @path, @parent)
end

#root?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 28

def root?
  false
end

#upAbstractCursor

Returns:



36
37
38
# File 'lib/stupidedi/zipper/memoized_cursor.rb', line 36

def up
  @parent
end