rooted_tree 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e3f5ccd99747a243d238569e0701d73798e94c0d
4
- data.tar.gz: 9fe0b64c62ae48112271a9c757112f683f3bdecc
3
+ metadata.gz: 2f48a3504fb307daf09d507cd556d7b622d6e17d
4
+ data.tar.gz: 363432cb0d5a2b69f0f4f0980de16ec03451619b
5
5
  SHA512:
6
- metadata.gz: 281c52f594074e7ed2c2b8d9b85fbd405615f8d35b71f999aebdf55a9cd35cc51b7658c3f3491fe52a762e712dcde4b8955de99fe85962b6f22850aa87d8add6
7
- data.tar.gz: 7e1a800d3a03349fe3364f722a6826afbbad0bafb205ec6dca44a19f8828c987daa9d822b33b2d81f565ab8e5e5648b50baa5899aa6fd1bcdf0ca603b92d9de9
6
+ metadata.gz: 08d262553a452128cceedda189e57d7118184f253c5106696a9a5b98751392091717dbd8551670cb81c6743e08ecd9876442c3592f4ff1e6ea9b67e9a581d5ee
7
+ data.tar.gz: f1de5a0b40607102934aaea8ecbb94d98de13aed5a5805bfed6e2ff34d41121a4cb600cc8a1f05dbf88b4c6001432c54e75621dde560ac4e79937cdd5f2a55f4
@@ -31,10 +31,22 @@ module RootedTree
31
31
 
32
32
  alias arity degree
33
33
 
34
+ # Creates a new node with the given object as its value, unless a Node is
35
+ # passed, in which case it will be returned.
36
+ #
37
+ # value - the object to be used as value for a new Node, or a Node object.
38
+ #
39
+ # Returns a Node object.
40
+
34
41
  def self.[](value = nil)
35
42
  return value if value.is_a? self
36
43
  new value
37
44
  end
45
+
46
+ # Create a new, unconnected Node object with an optional value. The value is
47
+ # owned by the Node instance and will be duped along with it.
48
+ #
49
+ # value - arbitrary object that is owned by the Node instance.
38
50
 
39
51
  def initialize(value = nil)
40
52
  @parent = nil
@@ -45,15 +57,33 @@ module RootedTree
45
57
  @degree = 0
46
58
  @value = value
47
59
  end
60
+
61
+ # When copying a node the child nodes are copied as well, along with the
62
+ # value.
48
63
 
49
- def initialize_copy(*)
50
- duped_children = children.map { |v| v.dup.tap { |w| w.parent = self } }
64
+ def initialize_dup(source)
65
+ # Dup each child and link them to the new parent
66
+ duped_children = source.children.map do |child|
67
+ child.dup.tap { |n| n.parent = self }
68
+ end
69
+
70
+ # Connect each child to its adjecent siblings
51
71
  duped_children.each_cons(2) { |a, b| a.next, b.prev = b, a }
52
72
 
53
73
  @parent = nil
54
74
  @first_child = duped_children.first
55
75
  @last_child = duped_children.last
76
+ @value = begin
77
+ source.value.dup
78
+ rescue TypeError
79
+ source.value
80
+ end
81
+
82
+ super
56
83
  end
84
+
85
+ # Freezes the value as well as each of the children, outside of the normal
86
+ # behaviour.
57
87
 
58
88
  def freeze
59
89
  @value.freeze
@@ -135,12 +165,21 @@ module RootedTree
135
165
  # Access the next sibling. Raises a StopIteration if this node is the last
136
166
  # one.
137
167
  #
138
- # Returns the previous sibling node.
168
+ # Returns the next sibling node.
139
169
 
140
170
  def next
141
171
  raise StopIteration if last?
142
172
  @next
143
173
  end
174
+
175
+ # Dangerous accessor of the next sibling. Unlike the regular #next this
176
+ # method will return nil if this node is the last one.
177
+ #
178
+ # Returns the next sibling node or nil if this node is last.
179
+
180
+ def next!
181
+ @next
182
+ end
144
183
 
145
184
  # Access the previous sibling. Raises a StopIteration if this node is the
146
185
  # first one.
@@ -153,6 +192,15 @@ module RootedTree
153
192
  end
154
193
 
155
194
  alias previous prev
195
+
196
+ # Dangerous accessor of the previous sibling. Unlike the regular #prev this
197
+ # method will return nil if this node is the first one.
198
+
199
+ def prev!
200
+ @prev
201
+ end
202
+
203
+ alias previous! prev!
156
204
 
157
205
  # Access the parent node. Raises a StopIteration if this node is the
158
206
  # root.
@@ -270,9 +318,9 @@ module RootedTree
270
318
  # Returns an array of the children to the deleted node, now made roots.
271
319
 
272
320
  def delete
273
- extract.children.map do |child|
321
+ extract.children.to_a.each do |child|
274
322
  child.parent = nil
275
- child
323
+ child.next = child.prev = nil
276
324
  end
277
325
  end
278
326
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RootedTree
4
- VERSION = '0.3.2'
4
+ VERSION = '0.3.3'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rooted_tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastian Lindberg
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-01 00:00:00.000000000 Z
11
+ date: 2016-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler