Class: Fr::TZipper::EditedCursor

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

Returns a new instance of EditedCursor.



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

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

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



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

def node
  @node
end

#parentAbstractCursor (readonly)

Returns:



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

def parent
  @parent
end

#pathHole (readonly)

Returns:



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

def path
  @path
end

Instance Method Details

#append(node) ⇒ EditedCursor

Returns:



109
110
111
112
# File 'lib/fr/tzipper/edited_cursor.rb', line 109

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

#deleteEditedCursor

Returns:



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/fr/tzipper/edited_cursor.rb', line 129

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

#firstEditedCursor

Returns:



80
81
82
83
84
85
86
87
88
89
# File 'lib/fr/tzipper/edited_cursor.rb', line 80

def first
  if first?
    return self
  end

  right = @path.left.init.reverse.concat(@node.cons(@path.right))

  EditedCursor.new(@path.left.last,
    Hole.new([], @path.parent, right), @parent)
end

#inspectObject



155
156
157
# File 'lib/fr/tzipper/edited_cursor.rb', line 155

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

#lastEditedCursor

Returns:



93
94
95
96
97
98
99
100
101
102
# File 'lib/fr/tzipper/edited_cursor.rb', line 93

def last
  if last?
    return self
  end

  left = @node.cons(@path.right.init.reverse).concat(@path.left)

  EditedCursor.new(@path.right.last,
    Hole.new(left, @path.parent, []), @parent)
end

#leaf?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/fr/tzipper/edited_cursor.rb', line 24

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

#nextEditedCursor

Returns:



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

def next
  if last?
    raise Errors::ZipperError,
      "cannot move to next after last node"
  end

  head, *tail = @path.right

  EditedCursor.new(head,
    Hole.new(@node.cons(@path.left), @path.parent, tail), @parent)
end

#prepend(node) ⇒ EditedCursor

Returns:



116
117
118
119
# File 'lib/fr/tzipper/edited_cursor.rb', line 116

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

#prevEditedCursor

Returns:



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/fr/tzipper/edited_cursor.rb', line 66

def prev
  if first?
    raise Errors::ZipperError,
      "cannot move to prev before first node"
  end

  head, *tail = @path.left

  EditedCursor.new(head,
    Hole.new(tail, @path.parent, @node.cons(@path.right)), @parent)
end

#replace(node) ⇒ EditedCursor

Returns:



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

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

#root?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/fr/tzipper/edited_cursor.rb', line 29

def root?
  false
end

#upAbstractCursor

Returns:



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fr/tzipper/edited_cursor.rb', line 38

def up
  node =
    @parent.node.copy(children:
      @path.left.reverse.concat(@node.cons(@path.right)))

  if @parent.root?
    RootCursor.new(node)
  else
    EditedCursor.new(node, @path.parent, @parent.parent)
  end
end