Class: Fr::TZipper::MemoizedCursor

Inherits:
AbstractCursor show all
Defined in:
lib/fr/tzipper/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, up) ⇒ MemoizedCursor

Returns a new instance of MemoizedCursor.



15
16
17
18
# File 'lib/fr/tzipper/memoized_cursor.rb', line 15

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

Instance Attribute Details

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

Returns:



7
8
9
# File 'lib/fr/tzipper/memoized_cursor.rb', line 7

def node
  @node
end

#pathHole (readonly)

Returns:



10
11
12
# File 'lib/fr/tzipper/memoized_cursor.rb', line 10

def path
  @path
end

#upAbstractCursor (readonly)

Returns:



13
14
15
# File 'lib/fr/tzipper/memoized_cursor.rb', line 13

def up
  @up
end

Instance Method Details

#append(node) ⇒ EditedCursor

Returns:



80
81
82
83
# File 'lib/fr/tzipper/memoized_cursor.rb', line 80

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

#deleteEditedCursor

Returns:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/fr/tzipper/memoized_cursor.rb', line 97

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

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

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

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

#firstMemoizedCursor

Returns:



65
66
67
# File 'lib/fr/tzipper/memoized_cursor.rb', line 65

def first
  @up.down
end

#inspectObject



123
124
125
# File 'lib/fr/tzipper/memoized_cursor.rb', line 123

def inspect
  "Cursor(..., #{@node.inspect})"
end

#lastMemoizedCursor

Returns:



70
71
72
73
74
# File 'lib/fr/tzipper/memoized_cursor.rb', line 70

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

#leaf?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/fr/tzipper/memoized_cursor.rb', line 23

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

#nextMemoizedCursor

Returns:



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fr/tzipper/memoized_cursor.rb', line 35

def next
  @__next ||= begin
    if last?
      raise Errors::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), @up)
  end
end

#prepend(node) ⇒ EditedCursor

Returns:



86
87
88
89
# File 'lib/fr/tzipper/memoized_cursor.rb', line 86

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

#prevMemoizedCursor

Returns:



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fr/tzipper/memoized_cursor.rb', line 50

def prev
  @__prev ||= begin
    if first?
      raise Errors::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)), @up)
  end
end

#replace(node) ⇒ EditedCursor

Returns:



92
93
94
# File 'lib/fr/tzipper/memoized_cursor.rb', line 92

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

#root?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/fr/tzipper/memoized_cursor.rb', line 27

def root?
  false
end