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.



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

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

Instance Attribute Details

#node (readonly)

Returns the value of attribute node.



8
9
10
# File 'lib/stupidedi/zipper/edited_cursor.rb', line 8

def node
  @node
end

#parentAbstractCursor (readonly)

Returns:



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

def parent
  @parent
end

#pathHole (readonly)

Returns:



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

def path
  @path
end

Instance Method Details

#append(node) ⇒ AbstractCursor, EditedCursor

Navigate to the last (rightmost) sibling node



110
111
112
113
# File 'lib/stupidedi/zipper/edited_cursor.rb', line 110

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



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

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



81
82
83
84
85
86
87
88
89
90
# File 'lib/stupidedi/zipper/edited_cursor.rb', line 81

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



94
95
96
97
98
99
100
101
102
103
# File 'lib/stupidedi/zipper/edited_cursor.rb', line 94

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?

True if the node has no children



25
26
27
# File 'lib/stupidedi/zipper/edited_cursor.rb', line 25

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

#nextAbstractCursor, EditedCursor

Navigate to the next (rightward) sibling node



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/stupidedi/zipper/edited_cursor.rb', line 53

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



117
118
119
120
# File 'lib/stupidedi/zipper/edited_cursor.rb', line 117

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



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/stupidedi/zipper/edited_cursor.rb', line 67

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



124
125
126
# File 'lib/stupidedi/zipper/edited_cursor.rb', line 124

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

#root?

True if the node has no parent



30
31
32
# File 'lib/stupidedi/zipper/edited_cursor.rb', line 30

def root?
  false
end

#upAbstractCursor

Navigate to the parent node



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/stupidedi/zipper/edited_cursor.rb', line 39

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