Class: Stupidedi::Zipper::EditedCursor

Inherits:
AbstractCursor show all
Defined in:
lib/stupidedi/zipper/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.



19
20
21
22
# File 'lib/stupidedi/zipper/edited_cursor.rb', line 19

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

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



11
12
13
# File 'lib/stupidedi/zipper/edited_cursor.rb', line 11

def node
  @node
end

#parentAbstractCursor (readonly)

Returns:



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

def parent
  @parent
end

#pathHole (readonly)

Returns:



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

def path
  @path
end

Instance Method Details

#append(node) ⇒ AbstractCursor, EditedCursor

Navigate to the last (rightmost) sibling node



113
114
115
116
# File 'lib/stupidedi/zipper/edited_cursor.rb', line 113

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

#deleteAbstractCursor, EditedCursor

Navigate to the last (rightmost) sibling node



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/stupidedi/zipper/edited_cursor.rb', line 133

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

#firstAbstractCursor, EditedCursor

Navigate to the first (leftmost) sibling node



84
85
86
87
88
89
90
91
92
93
# File 'lib/stupidedi/zipper/edited_cursor.rb', line 84

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

#lastAbstractCursor, EditedCursor

Navigate to the last (rightmost) sibling node



97
98
99
100
101
102
103
104
105
106
# File 'lib/stupidedi/zipper/edited_cursor.rb', line 97

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)


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

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

#nextAbstractCursor, EditedCursor

Navigate to the next (rightward) sibling node



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

def next
  if last?
    raise Exceptions::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) ⇒ AbstractCursor, EditedCursor

Navigate to the last (rightmost) sibling node



120
121
122
123
# File 'lib/stupidedi/zipper/edited_cursor.rb', line 120

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

#prevAbstractCursor, EditedCursor

Navigate to the previous (leftward) sibling node



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/stupidedi/zipper/edited_cursor.rb', line 70

def prev
  if first?
    raise Exceptions::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) ⇒ AbstractCursor, EditedCursor

Navigate to the last (rightmost) sibling node



127
128
129
# File 'lib/stupidedi/zipper/edited_cursor.rb', line 127

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

#root?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/stupidedi/zipper/edited_cursor.rb', line 33

def root?
  false
end

#upAbstractCursor

Navigate to the parent node



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

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