rubytree 0.4.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog CHANGED
@@ -1,3 +1,38 @@
1
+ 2007-12-19 Anupam Sengupta <anupamsg@gmail.com>
2
+
3
+ * Rakefile: Fixed a problem with reading the Tree::VERSION for the
4
+ gem packaging, if any prior version of the gem is already installed.
5
+
6
+ 2007-12-18 Anupam Sengupta <anupamsg@gmail.com>
7
+
8
+ * lib/tree.rb: Updated the marshalling logic to correctly handle
9
+ non-string content.
10
+ (Tree::TreeNode::createDumpRep): Minor code change to use symbols
11
+ instead of string key names.
12
+ (Tree): Version number change to 0.5.0
13
+ (Tree::TreeNode::hasContent): Minor fix to the comments.
14
+
15
+ * test/test_tree.rb (TC_TreeTest::test_breadth_each): Updated test
16
+ cases for the marshalling logic.
17
+
18
+ 2007-11-12 Anupam Sengupta <anupamsg@gmail.com>
19
+
20
+ * test/test_binarytree.rb: Minor documentation correction.
21
+
22
+ * lib/tree/binarytree.rb (Tree::BinaryTreeNode::isRightChild):
23
+ Minor documentation change.
24
+
25
+ 2007-10-10 Anupam Sengupta <anupamsg@gmail.com>
26
+
27
+ * README: Restructured the format.
28
+
29
+ * Rakefile: Added Hoe related logic. If not present, the Rakefile
30
+ will default to old behavior.
31
+
32
+ 2007-10-09 Anupam Sengupta <anupamsg@gmail.com>
33
+
34
+ * Rakefile: Added setup.rb related tasks. Also added the setup.rb in the PKG_FILES list.
35
+
1
36
  2007-10-01 Anupam Sengupta <anupamsg@gmail.com>
2
37
 
3
38
  * Rakefile: Added an optional task for rcov code coverage.
@@ -0,0 +1,11 @@
1
+ = 0.5.0 / 2007-12-18
2
+
3
+ * Fixed the marshalling code to correctly handle non-string content.
4
+
5
+ = 0.4.3 / 2007-10-09
6
+
7
+ * Changes to the build mechanism (now uses Hoe).
8
+
9
+ = 0.4.2 / 2007-10-01
10
+
11
+ * Minor code refactoring. Changes in the Rakefile.
@@ -0,0 +1,12 @@
1
+ COPYING
2
+ ChangeLog
3
+ History.txt
4
+ Manifest.txt
5
+ README
6
+ TODO
7
+ Rakefile
8
+ lib/tree.rb
9
+ lib/tree/binarytree.rb
10
+ setup.rb
11
+ test/test_binarytree.rb
12
+ test/test_tree.rb
data/README CHANGED
@@ -1,27 +1,18 @@
1
- = Rubytree 0.4.2
1
+ = Rubytree
2
2
 
3
- (c) 2006, 2007 Anupam Sengupta
3
+ (c) 2006, 2007 Anupam Sengupta
4
4
  http://rubytree.rubyforge.org
5
5
 
6
- Document Revision: $Revision: 1.11 $ by $Author: anupamsg $
7
-
8
- == License
9
-
10
- Rubytree has been released under the BSD License. See the file COPYING for
11
- details.
12
-
13
- == Introduction
6
+ == DESCRIPTION:
14
7
 
15
8
  Rubytree is a simple implementation of the generic Tree data structure. This
16
9
  implementation is node-centric, where the individual nodes on the tree are the
17
10
  primary objects and drive the structure.
18
11
 
19
- == Getting Rubytree
12
+ == INSTALL:
20
13
 
21
14
  Rubytree is an open source project and is hosted at
22
- http://rubyforge.org/projects/rubytree
23
-
24
- The project home page is http://rubytree.rubyforge.org
15
+ http://rubytree.rubyforge.org
25
16
 
26
17
  Rubytree can be downloaded as a Rubygem or as a tar/zip file from:
27
18
 
@@ -35,8 +26,6 @@ The file name is one of:
35
26
 
36
27
  Download the appropriate file-type.
37
28
 
38
- == Installing Rubytree
39
-
40
29
  It is recommended to install Rubytree as a Ruby Gem, as this is an easy way to
41
30
  keep the version updated, and keep multiple versions of the library available on
42
31
  your system.
@@ -54,7 +43,7 @@ super-user (root) to successfully install the gem.
54
43
  Extract the archive file (tgz or zip) and run the 'rake' command from the
55
44
  top-level source directory.
56
45
 
57
- == Documentation
46
+ == DOCUMENTATION:
58
47
 
59
48
  The primary class for this implementation is Tree::TreeNode. See the
60
49
  class documentation for an usage example.
@@ -68,7 +57,7 @@ Documentation on the web is available at:
68
57
 
69
58
  http://rubytree.rubyforge.org/rdoc
70
59
 
71
- == Example
60
+ == EXAMPLE:
72
61
 
73
62
  The following code-snippet implements this tree structure:
74
63
 
@@ -110,7 +99,7 @@ The following code-snippet implements this tree structure:
110
99
 
111
100
  myTreeRoot.remove!(child1) # Remove the child
112
101
 
113
- == LICENSE
102
+ == LICENSE:
114
103
 
115
104
  Rubytree is licensed under BSD license.
116
105
 
@@ -142,3 +131,6 @@ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
142
131
  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143
132
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
144
133
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
134
+
135
+
136
+ (Document Revision: $Revision: 1.12 $ by $Author: anupamsg $)
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
1
  # Rakefile
2
2
  #
3
- # $Revision: 1.23 $ by $Author: anupamsg $
3
+ # $Revision: 1.27 $ by $Author: anupamsg $
4
4
  # $Name: $
5
5
  #
6
6
  # Copyright (c) 2006, 2007 Anupam Sengupta
@@ -34,64 +34,123 @@
34
34
  #
35
35
 
36
36
  require 'rubygems'
37
- require 'rake/clean'
38
37
  require 'rake/gempackagetask'
38
+
39
+ require 'rake/clean'
40
+ require 'rake/packagetask'
39
41
  require 'rake/testtask'
40
42
  require 'rake/rdoctask'
41
43
 
42
- require './lib/tree.rb' # To read the version information.
44
+ require 'fileutils'
43
45
 
44
- desc "Default Task"
45
- task :default => :gem
46
+ # General Stuff ####################################################
46
47
 
47
- PKG_VERSION = Tree::VERSION
48
+ $:.insert 0, File.expand_path( File.join( File.dirname(__FILE__), 'lib' ) )
49
+ require 'tree' # To read the version information.
50
+
51
+ PKG_NAME = "rubytree"
52
+ PKG_VERSION = Tree::VERSION
53
+ PKG_FULLNAME = PKG_NAME + "-" + PKG_VERSION
54
+ PKG_SUMMARY = "Ruby implementation of the Tree data structure."
55
+ PKG_DESCRIPTION = <<-END
56
+ Provides a generic tree data-structure with ability to
57
+ store keyed node-elements in the tree. The implementation
58
+ mixes in the Enumerable module.
59
+
60
+ Website: http://rubytree.rubyforge.org/
61
+ END
48
62
 
49
63
  PKG_FILES = FileList[
50
64
  '[A-Z]*',
65
+ '*.rb',
51
66
  'lib/**/*.rb',
52
67
  'test/**/*.rb'
53
68
  ]
54
69
 
55
- spec = Gem::Specification.new do |s|
56
- s.name = "rubytree"
57
- s.version = PKG_VERSION
58
- s.platform = Gem::Platform::RUBY
59
- s.author = "Anupam Sengupta"
60
- s.email = "anupamsg@gmail.com"
61
- s.homepage = "http://rubytree.rubyforge.org/"
62
- s.rubyforge_project = 'rubytree'
63
- s.summary = "Ruby implementation of the Tree data structure."
64
- s.add_dependency('rake', '>= 0.7.2')
65
-
66
- s.description = <<-END
67
- Provides a generic tree data-structure with ability to
68
- store keyed node-elements in the tree. The implementation
69
- mixes in the Enumerable module.
70
+ # Default is to create a rubygem.
71
+ desc "Default Task"
72
+ task :default => :gem
70
73
 
71
- Website: http://rubytree.rubyforge.org/
72
- END
74
+ begin # Try loading hoe
75
+ require 'hoe'
76
+ # If Hoe is found, use it to define tasks
77
+ # =======================================
78
+ Hoe.new(PKG_NAME, PKG_VERSION) do |p|
79
+ p.rubyforge_name = PKG_NAME
80
+ p.author = "Anupam Sengupta"
81
+ p.email = "anupamsg@gmail.com"
82
+ p.summary = PKG_SUMMARY
83
+ p.description = PKG_DESCRIPTION
84
+ p.url = "http://rubytree.rubyforge.org/"
85
+ p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
86
+ p.remote_rdoc_dir = 'rdoc'
87
+ p.need_tar = true
88
+ p.need_zip = true
89
+ p.test_globs = ['test/test_*.rb']
90
+ p.spec_extras = {
91
+ :has_rdoc => true,
92
+ :platform => Gem::Platform::RUBY,
93
+ :has_rdoc => true,
94
+ :extra_rdoc_files => ['README', 'COPYING', 'ChangeLog', 'History.txt'],
95
+ :rdoc_options => ['--main', 'README'],
96
+ :autorequire => 'tree'
97
+ }
98
+ end
73
99
 
74
- s.has_rdoc = true
75
- s.extra_rdoc_files = ['README', 'COPYING', 'ChangeLog']
76
- s.autorequire = "tree"
77
- s.files = PKG_FILES.to_a
78
- s.test_files = Dir.glob('test/test*.rb')
79
- end
100
+ rescue LoadError # If Hoe is not found
101
+ # If Hoe is not found, then use the usual Gemspec based Rake tasks
102
+ # ================================================================
103
+ spec = Gem::Specification.new do |s|
104
+ s.name = PKG_NAME
105
+ s.version = PKG_VERSION
106
+ s.platform = Gem::Platform::RUBY
107
+ s.author = "Anupam Sengupta"
108
+ s.email = "anupamsg@gmail.com"
109
+ s.homepage = "http://rubytree.rubyforge.org/"
110
+ s.rubyforge_project = 'rubytree'
111
+ s.summary = PKG_SUMMARY
112
+ s.add_dependency('rake', '>= 0.7.2')
113
+ s.description = PKG_DESCRIPTION
114
+ s.has_rdoc = true
115
+ s.extra_rdoc_files = ['README', 'COPYING', 'ChangeLog']
116
+ s.autorequire = "tree"
117
+ s.files = PKG_FILES.to_a
118
+ s.test_files = Dir.glob('test/test*.rb')
119
+ end
80
120
 
81
- Rake::GemPackageTask.new(spec) do |pkg|
82
- pkg.need_zip = true
83
- pkg.need_tar = true
84
- end
121
+ desc "Prepares for installation"
122
+ task :prepare do
123
+ ruby "setup.rb config"
124
+ ruby "setup.rb setup"
125
+ end
85
126
 
86
- Rake::TestTask.new do |t|
87
- t.libs << "test"
88
- t.test_files = FileList['test/test*.rb']
89
- t.verbose = true
90
- end
127
+ desc "Installs the package #{PKG_NAME}"
128
+ task :install => [:prepare] do
129
+ ruby "setup.rb install"
130
+ end
131
+
132
+ Rake::GemPackageTask.new(spec) do |pkg|
133
+ pkg.need_zip = true
134
+ pkg.need_tar = true
135
+ end
136
+
137
+ Rake::TestTask.new do |t|
138
+ t.libs << "test"
139
+ t.test_files = FileList['test/test*.rb']
140
+ t.verbose = true
141
+ end
142
+
143
+ end # End loading Hoerc
144
+ # ================================================================
145
+
146
+
147
+ # The following tasks are loaded independently of Hoe
91
148
 
92
- Rake::RDocTask.new(:rdoc) do |rd|
149
+ # Hoe's rdoc task is ugly.
150
+ Rake::RDocTask.new(:docs) do |rd|
93
151
  rd.rdoc_files.include("README", "COPYING", "ChangeLog", "lib/**/*.rb")
94
- rd.title = "Rubytree Documentation"
152
+ rd.rdoc_dir = 'doc'
153
+ rd.title = "#{PKG_FULLNAME} Documentation"
95
154
 
96
155
  # Use the template only if it is present, otherwise, the standard template is
97
156
  # used.
@@ -108,7 +167,7 @@ begin
108
167
  RTagsTask.new do |rd|
109
168
  rd.vi = false
110
169
  end
111
- rescue LoadError
170
+ rescue LoadError
112
171
  end
113
172
 
114
173
  # Optional RCOV Task
@@ -117,9 +176,9 @@ begin
117
176
  require 'rcov/rcovtask'
118
177
  Rcov::RcovTask.new do |t|
119
178
  t.test_files = FileList['test/test*.rb']
120
- # t.verbose = true # uncomment to see the executed commands
179
+ # t.verbose = true # uncomment to see the executed commands
121
180
  end
122
- rescue LoadError
181
+ rescue LoadError
123
182
  end
124
183
 
125
184
  #Rakefile,v $
data/TODO ADDED
@@ -0,0 +1 @@
1
+ * Add logic in Rakefile to read the file list from Manifest.txt
@@ -1,6 +1,6 @@
1
1
  # tree.rb
2
2
  #
3
- # $Revision: 1.19 $ by $Author: anupamsg $
3
+ # $Revision: 1.24 $ by $Author: anupamsg $
4
4
  # $Name: $
5
5
  #
6
6
  # = tree.rb - Generic Multi-way Tree implementation
@@ -47,7 +47,7 @@
47
47
  # This module mixes in the Enumerable module.
48
48
  module Tree
49
49
 
50
- VERSION = '0.4.2'
50
+ VERSION = '0.5.0'
51
51
 
52
52
  # == TreeNode Class Description
53
53
  #
@@ -108,9 +108,6 @@ module Tree
108
108
  attr_reader :content, :name, :parent
109
109
  attr_writer :content
110
110
 
111
- @@fieldSep = '|'
112
- @@recordSep = "\n"
113
-
114
111
  # Constructor which expects the name of the node
115
112
  #
116
113
  # Name of the node is expected to be unique across the
@@ -118,12 +115,14 @@ module Tree
118
115
  #
119
116
  # The content can be of any type, and is defaulted to _nil_.
120
117
  def initialize(name, content = nil)
121
-
122
118
  raise "Node name HAS to be provided" if name == nil
119
+ self_initialize name, content
120
+ end
123
121
 
122
+ # A common initialization routine (also used by the marshalling code).
123
+ def self_initialize(name, content)
124
124
  @name = name
125
125
  @content = content
126
-
127
126
  self.setAsRoot!
128
127
 
129
128
  @childrenHash = Hash.new
@@ -221,7 +220,7 @@ module Tree
221
220
  @content != nil
222
221
  end
223
222
 
224
- # Private method which sets this node as a root node.
223
+ # Protected method which sets this node as a root node.
225
224
  def setAsRoot!
226
225
  @parent = nil
227
226
  end
@@ -276,7 +275,7 @@ module Tree
276
275
  # Traverses the tree in a pre-ordered sequence. This is equivalent to
277
276
  # TreeNode#each
278
277
  def preordered_each &block
279
- each &block
278
+ each(&block)
280
279
  end
281
280
 
282
281
  # Performs breadth first traversal of the tree rooted at this node. The
@@ -431,40 +430,35 @@ module Tree
431
430
  each {|node| node.freeze}
432
431
  end
433
432
 
434
- # Creates a dump representation and returns the same as a string
433
+ # Creates the marshal-dump represention of the tree rooted at this node.
434
+ def marshal_dump
435
+ self.collect { |node| node.createDumpRep }
436
+ end
437
+
438
+ # Creates a dump representation and returns the same as a hash
435
439
  def createDumpRep
436
- strRep = String.new
437
- strRep << @name << @@fieldSep << (isRoot? ? @name : @parent.name)
438
- strRep << @@fieldSep << Marshal.dump(@content) << @@recordSep
439
- end
440
-
441
- def _dump(depth)
442
- strRep = String.new
443
- each {|node| strRep << node.createDumpRep}
444
- strRep
445
- end
446
-
447
- # Loads a dump representation of the tree from the specified string
448
- def TreeNode.loadDumpRep(str)
449
- nodeHash = Hash.new
450
- rootNode = nil
451
- str.split(@@recordSep).each do |line|
452
- name, parent, contentStr = line.split(@@fieldSep)
453
- content = Marshal.load(contentStr)
454
- currentNode = Tree::TreeNode.new(name, content)
455
- nodeHash[name] = currentNode
456
- if name != parent # Do for a child node
457
- nodeHash[parent].add(currentNode)
458
- else
459
- rootNode = currentNode
460
- end
461
- end
462
- rootNode
440
+ { :name => @name, :parent => (isRoot? ? nil : @parent.name), :content => Marshal.dump(@content)}
463
441
  end
464
442
 
465
- # Loads a dump representation of the tree from the specified string.
466
- def TreeNode._load(str)
467
- loadDumpRep(str)
443
+ # Loads a marshalled dump of the tree and returns the root node of the
444
+ # reconstructed tree. See the Marshal class for additional details.
445
+ def marshal_load(dumped_tree_array)
446
+ nodes = { }
447
+ for node_hash in dumped_tree_array do
448
+ name = node_hash[:name]
449
+ parent_name = node_hash[:parent]
450
+ content = Marshal.load(node_hash[:content])
451
+
452
+ if parent_name then
453
+ nodes[name] = current_node = Tree::TreeNode.new(name, content)
454
+ nodes[parent_name].add current_node
455
+ else
456
+ # This is the root node, hence initialize self.
457
+ self_initialize(name, content)
458
+
459
+ nodes[name] = self # Add self to the
460
+ end
461
+ end
468
462
  end
469
463
 
470
464
  # Returns depth of the tree from this node. A single leaf node has a
@@ -481,13 +475,31 @@ module Tree
481
475
  parent.children.size
482
476
  end
483
477
 
484
- protected :parent=, :setAsRoot!
485
- private_class_method :loadDumpRep
478
+ protected :parent=, :setAsRoot!, :self_initialize, :createDumpRep
486
479
 
487
480
  end
488
481
  end
489
482
 
490
483
  # $Log: tree.rb,v $
484
+ # Revision 1.24 2007/12/19 06:39:17 anupamsg
485
+ # Removed the unnecessary field and record separator constants. Also updated the
486
+ # history.txt file.
487
+ #
488
+ # Revision 1.23 2007/12/19 06:25:00 anupamsg
489
+ # (Tree::TreeNode): Minor fix to the comments. Also fixed the private/protected
490
+ # scope issue with the createDumpRep method.
491
+ #
492
+ # Revision 1.22 2007/12/19 06:22:03 anupamsg
493
+ # Updated the marshalling logic to correctly handle non-string content. This
494
+ # should fix the bug # 15614 ("When dumping with an Object as the content, you get
495
+ # a delimiter collision")
496
+ #
497
+ # Revision 1.21 2007/12/19 02:24:17 anupamsg
498
+ # Updated the marshalling logic to handle non-string contents on the nodes.
499
+ #
500
+ # Revision 1.20 2007/10/10 08:42:57 anupamsg
501
+ # Release 0.4.3
502
+ #
491
503
  # Revision 1.19 2007/08/31 01:16:27 anupamsg
492
504
  # Added breadth and pre-order traversals for the tree. Also added a method
493
505
  # to return the detached copy of a node from the tree.