rubytree 0.9.7 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,9 +3,9 @@
3
3
  # tree_merge_handler.rb
4
4
  #
5
5
  # Author: Anupam Sengupta
6
- # Time-stamp: <2015-05-30 16:06:18 anupam>
6
+ # Time-stamp: <2022-06-20 22:17:12 anupam>
7
7
  #
8
- # Copyright (C) 2013, 2015 Anupam Sengupta (anupamsg@gmail.com)
8
+ # Copyright (C) 2013, 2015, 2022 Anupam Sengupta (anupamsg@gmail.com)
9
9
  #
10
10
  # All rights reserved.
11
11
  #
@@ -34,96 +34,95 @@
34
34
  # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
35
  # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
36
  #
37
+ # frozen_string_literal: true
37
38
 
38
39
  # Provides utility methods to merge two {Tree::TreeNode} based trees.
39
40
  # @since 0.9.0
40
- module Tree::Utils::TreeMergeHandler
41
+ module Tree
42
+ module Utils
43
+ # Handles merging of two trees.
44
+ module TreeMergeHandler
45
+ # @!group Merging Trees
41
46
 
42
- # @!group Merging Trees
47
+ # Merge two trees that share the same root node and returns <em>a new
48
+ # tree</em>.
49
+ #
50
+ # The new tree contains the contents of the merge between _other_tree_ and
51
+ # self. Duplicate nodes (coming from _other_tree_) will *NOT* be overwritten
52
+ # in self.
53
+ #
54
+ # @author Darren Oakley (https://github.com/dazoakley)
55
+ #
56
+ # @param [Tree::TreeNode] other_tree The other tree to merge with.
57
+ # @return [Tree::TreeNode] the resulting tree following the merge.
58
+ #
59
+ # @raise [TypeError] This exception is raised if _other_tree_ is not a
60
+ # {Tree::TreeNode}.
61
+ #
62
+ # @raise [ArgumentError] This exception is raised if _other_tree_ does not
63
+ # have the same root node as self.
64
+ def merge(other_tree)
65
+ check_merge_prerequisites(other_tree)
66
+ merge_trees(root.dup, other_tree.root)
67
+ end
43
68
 
44
- # Merge two trees that share the same root node and returns <em>a new
45
- # tree</em>.
46
- #
47
- # The new tree contains the contents of the merge between _other_tree_ and
48
- # self. Duplicate nodes (coming from _other_tree_) will *NOT* be overwritten
49
- # in self.
50
- #
51
- # @author Darren Oakley (https://github.com/dazoakley)
52
- #
53
- # @param [Tree::TreeNode] other_tree The other tree to merge with.
54
- # @return [Tree::TreeNode] the resulting tree following the merge.
55
- #
56
- # @raise [TypeError] This exception is raised if _other_tree_ is not a
57
- # {Tree::TreeNode}.
58
- #
59
- # @raise [ArgumentError] This exception is raised if _other_tree_ does not
60
- # have the same root node as self.
61
- def merge(other_tree)
62
- check_merge_prerequisites(other_tree)
63
- new_tree = merge_trees(self.root.dup, other_tree.root)
64
- return new_tree
65
- end
69
+ # Merge in another tree (that shares the same root node) into +this+ tree.
70
+ # Duplicate nodes (coming from _other_tree_) will NOT be overwritten in
71
+ # self.
72
+ #
73
+ # @author Darren Oakley (https://github.com/dazoakley)
74
+ #
75
+ # @param [Tree::TreeNode] other_tree The other tree to merge with.
76
+ #
77
+ # @raise [TypeError] This exception is raised if _other_tree_ is not a
78
+ # {Tree::TreeNode}.
79
+ #
80
+ # @raise [ArgumentError] This exception is raised if _other_tree_ does not
81
+ # have the same root node as self.
82
+ def merge!(other_tree)
83
+ check_merge_prerequisites(other_tree)
84
+ merge_trees(root, other_tree.root)
85
+ end
66
86
 
67
- # Merge in another tree (that shares the same root node) into +this+ tree.
68
- # Duplicate nodes (coming from _other_tree_) will NOT be overwritten in
69
- # self.
70
- #
71
- # @author Darren Oakley (https://github.com/dazoakley)
72
- #
73
- # @param [Tree::TreeNode] other_tree The other tree to merge with.
74
- #
75
- # @raise [TypeError] This exception is raised if _other_tree_ is not a
76
- # {Tree::TreeNode}.
77
- #
78
- # @raise [ArgumentError] This exception is raised if _other_tree_ does not
79
- # have the same root node as self.
80
- def merge!(other_tree)
81
- check_merge_prerequisites( other_tree )
82
- merge_trees( self.root, other_tree.root )
83
- end
87
+ private
84
88
 
85
- private
89
+ # Utility function to check that the conditions for a tree merge are met.
90
+ #
91
+ # @author Darren Oakley (https://github.com/dazoakley)
92
+ #
93
+ # @see #merge
94
+ # @see #merge!
95
+ def check_merge_prerequisites(other_tree)
96
+ raise TypeError, 'You can only merge in another instance of Tree::TreeNode' \
97
+ unless other_tree.is_a?(Tree::TreeNode)
86
98
 
87
- # Utility function to check that the conditions for a tree merge are met.
88
- #
89
- # @author Darren Oakley (https://github.com/dazoakley)
90
- #
91
- # @see #merge
92
- # @see #merge!
93
- def check_merge_prerequisites(other_tree)
94
- unless other_tree.is_a?(Tree::TreeNode)
95
- raise TypeError,
96
- 'You can only merge in another instance of Tree::TreeNode'
97
- end
99
+ raise ArgumentError, 'Unable to merge trees as they do not share the same root' \
100
+ unless root.name == other_tree.root.name
101
+ end
98
102
 
99
- unless self.root.name == other_tree.root.name
100
- raise ArgumentError,
101
- 'Unable to merge trees as they do not share the same root'
102
- end
103
- end
103
+ # Utility function to recursively merge two subtrees.
104
+ #
105
+ # @author Darren Oakley (https://github.com/dazoakley)
106
+ #
107
+ # @param [Tree::TreeNode] tree1 The target tree to merge into.
108
+ # @param [Tree::TreeNode] tree2 The donor tree (that will be merged
109
+ # into target).
110
+ # @raise [Tree::TreeNode] The merged tree.
111
+ def merge_trees(tree1, tree2)
112
+ names1 = tree1.children? ? tree1.children.map(&:name) : []
113
+ names2 = tree2.children? ? tree2.children.map(&:name) : []
104
114
 
105
- # Utility function to recursivley merge two subtrees.
106
- #
107
- # @author Darren Oakley (https://github.com/dazoakley)
108
- #
109
- # @param [Tree::TreeNode] tree1 The target tree to merge into.
110
- # @param [Tree::TreeNode] tree2 The donor tree (that will be merged
111
- # into target).
112
- # @raise [Tree::TreeNode] The merged tree.
113
- def merge_trees(tree1, tree2)
114
- names1 = tree1.has_children? ? tree1.children.map { |child| child.name } : []
115
- names2 = tree2.has_children? ? tree2.children.map { |child| child.name } : []
115
+ names_to_merge = names2 - names1
116
+ names_to_merge.each do |name|
117
+ tree1 << tree2[name].detached_subtree_copy
118
+ end
116
119
 
117
- names_to_merge = names2 - names1
118
- names_to_merge.each do |name|
119
- tree1 << tree2[name].detached_subtree_copy
120
- end
120
+ tree1.children.each do |child|
121
+ merge_trees(child, tree2[child.name]) unless tree2[child.name].nil?
122
+ end
121
123
 
122
- tree1.children.each do |child|
123
- merge_trees( child, tree2[child.name] ) unless tree2[child.name].nil?
124
+ tree1
125
+ end
124
126
  end
125
-
126
- return tree1
127
127
  end
128
-
129
128
  end
@@ -4,11 +4,9 @@
4
4
  #
5
5
  # Author:: Anupam Sengupta (anupamsg@gmail.com)
6
6
  #
7
- # Time-stamp: <2015-05-30 14:25:57 anupam>
7
+ # Time-stamp: <2022-06-20 01:21:38 anupam>
8
8
  #
9
- # Copyright (C) 2012, 2015 Anupam Sengupta <anupamsg@gmail.com>
10
- #
11
- # All rights reserved.
9
+ # Copyright (C) 2012-2022 Anupam Sengupta. All rights reserved.
12
10
  #
13
11
  # Redistribution and use in source and binary forms, with or without
14
12
  # modification, are permitted provided that the following conditions are met:
@@ -34,8 +32,13 @@
34
32
  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35
33
  # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36
34
  # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
+ #
36
+ # frozen_string_literal: true
37
37
 
38
38
  # Provides utilities and mixin modules for RubyTree.
39
- module Tree::Utils
40
- # Empty module. Being used as a namespace.
39
+ module Tree
40
+ # Provides various utilities for the TreeNode class.
41
+ module Utils
42
+ # Empty module. Being used as a namespace.
43
+ end
41
44
  end
data/lib/tree/version.rb CHANGED
@@ -4,9 +4,7 @@
4
4
  #
5
5
  # Author:: Anupam Sengupta (anupamsg@gmail.com)
6
6
  #
7
- # Copyright (c) 2012, 2013, 2014, 2015 Anupam Sengupta
8
- #
9
- # All rights reserved.
7
+ # Copyright (c) 2012-2022 Anupam Sengupta. All rights reserved.
10
8
  #
11
9
  # Redistribution and use in source and binary forms, with or without
12
10
  # modification, are permitted provided that the following conditions are met:
@@ -33,9 +31,9 @@
33
31
  # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34
32
  # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
33
  #
34
+ # frozen_string_literal: true
36
35
 
37
- #
38
36
  module Tree
39
37
  # Rubytree Package Version
40
- VERSION = '0.9.7'
38
+ VERSION = '2.0.0'
41
39
  end