bundler-for-redmine.patch

Alexey Palazhchenko, 2010-06-02 23:39

Download (120 KB)

View differences:

Gemfile
1
source :rubygems
2

  
3
gem "bundler", "0.9.25"
4
gem "rails", "2.3.5"
5
gem "rack", "1.0.1"
6

  
7
gem "rubytree", "0.5.2", :require => "tree"
8
gem "RedCloth", "~>4.2.3", :require => "redcloth" # for CodeRay
9

  
10
# Please uncomment lines for your databases.
11
# Alternatively you may want to add these lines to specific groups below.
12
# gem "sqlite3-ruby", :require => "sqlite3"  # for SQLite 3
13
# gem "mysql"                                #     MySQL
14
# gem "pg"                                   #     PostgreSQL
15

  
16
group :development do
17
end
18

  
19
group :production do
20
end
21

  
22
group :test do
23
  gem "shoulda"
24
  gem "mocha", :require => nil # ":require => nil" fixes obscure bugs - remote and run all tests
25
  gem "edavis10-object_daddy", :require => "object_daddy"
26
end
27

  
28
# Load plugins' Gemfiles
29
Dir.glob(File.join(File.dirname(__FILE__), %w(vendor plugins * Gemfile))) do |file|
30
  puts "Loading #{file} ..."
31
  instance_eval File.read(file)
32
end
config/boot.rb
106 106
  end
107 107
end
108 108

  
109
# Bundler
110
class Rails::Boot
111
  def run
112
    load_initializer
113

  
114
    Rails::Initializer.class_eval do
115
      def load_gems
116
        @bundler_loaded ||= Bundler.require :default, Rails.env.to_sym
117
      end
118
    end
119

  
120
    Rails::Initializer.run(:set_load_path)
121
  end
122
end
123

  
109 124
# All that for this:
110 125
Rails.boot!
config/environment.rb
50 50
  # It will automatically turn deliveries on
51 51
  config.action_mailer.perform_deliveries = false
52 52

  
53
  config.gem 'rubytree', :lib => 'tree'
53
  # No 'config.gem' there, please use Gemfile instead.
54 54
  
55 55
  # Load any local configuration that is kept out of source control
56 56
  # (e.g. gems, patches).
config/environments/test.rb
24 24
# Skip protect_from_forgery in requests http://m.onkey.org/2007/9/28/csrf-protection-for-your-existing-rails-application
25 25
config.action_controller.allow_forgery_protection  = false
26 26

  
27
config.gem "shoulda"
28
config.gem "edavis10-object_daddy", :lib => "object_daddy"
29
config.gem "mocha"
27
# No 'config.gem' there, please use Gemfile instead.
config/preinitializer.rb
1
# Bundler
2
begin
3
  # Require the preresolved locked set of gems.
4
  require File.expand_path('../../.bundle/environment', __FILE__)
5
rescue LoadError
6
  # Fallback on doing the resolve at runtime.
7
  require "rubygems"
8
  require "bundler"
9
  ENV["BUNDLE_GEMFILE"] = File.expand_path("../../Gemfile", __FILE__)
10
  Bundler.setup
11
end
vendor/gems/rubytree-0.5.2/.specification
1
--- !ruby/object:Gem::Specification 
2
name: rubytree
3
version: !ruby/object:Gem::Version 
4
  version: 0.5.2
5
platform: ruby
6
authors: 
7
- Anupam Sengupta
8
autorequire: tree
9
bindir: bin
10
cert_chain: []
11

  
12
date: 2007-12-20 00:00:00 -08:00
13
default_executable: 
14
dependencies: 
15
- !ruby/object:Gem::Dependency 
16
  name: hoe
17
  type: :runtime
18
  version_requirement: 
19
  version_requirements: !ruby/object:Gem::Requirement 
20
    requirements: 
21
    - - ">="
22
      - !ruby/object:Gem::Version 
23
        version: 1.3.0
24
    version: 
25
description: "Provides a generic tree data-structure with ability to store keyed node-elements in the tree. The implementation mixes in the Enumerable module.  Website:  http://rubytree.rubyforge.org/"
26
email: anupamsg@gmail.com
27
executables: []
28

  
29
extensions: []
30

  
31
extra_rdoc_files: 
32
- README
33
- COPYING
34
- ChangeLog
35
- History.txt
36
files: 
37
- COPYING
38
- ChangeLog
39
- History.txt
40
- Manifest.txt
41
- README
42
- Rakefile
43
- TODO
44
- lib/tree.rb
45
- lib/tree/binarytree.rb
46
- setup.rb
47
- test/test_binarytree.rb
48
- test/test_tree.rb
49
has_rdoc: true
50
homepage: http://rubytree.rubyforge.org/
51
licenses: []
52

  
53
post_install_message: 
54
rdoc_options: 
55
- --main
56
- README
57
require_paths: 
58
- lib
59
required_ruby_version: !ruby/object:Gem::Requirement 
60
  requirements: 
61
  - - ">="
62
    - !ruby/object:Gem::Version 
63
      version: "0"
64
  version: 
65
required_rubygems_version: !ruby/object:Gem::Requirement 
66
  requirements: 
67
  - - ">="
68
    - !ruby/object:Gem::Version 
69
      version: "0"
70
  version: 
71
requirements: []
72

  
73
rubyforge_project: rubytree
74
rubygems_version: 1.3.5
75
signing_key: 
76
specification_version: 2
77
summary: Ruby implementation of the Tree data structure.
78
test_files: 
79
- test/test_binarytree.rb
80
- test/test_tree.rb
vendor/gems/rubytree-0.5.2/COPYING
1
RUBYTREE - http://rubytree.rubyforge.org
2
========================================
3

  
4
Copyright (c) 2006, 2007 Anupam Sengupta
5

  
6
All rights reserved.
7

  
8
Redistribution and use in source and binary forms, with or without modification,
9
are permitted provided that the following conditions are met:
10

  
11
- Redistributions of source code must retain the above copyright notice, this
12
  list of conditions and the following disclaimer.
13

  
14
- Redistributions in binary form must reproduce the above copyright notice, this
15
  list of conditions and the following disclaimer in the documentation and/or
16
  other materials provided with the distribution.
17

  
18
- Neither the name of the organization nor the names of its contributors may
19
  be used to endorse or promote products derived from this software without
20
  specific prior written permission.
21

  
22
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
26
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
vendor/gems/rubytree-0.5.2/ChangeLog
1
2007-12-21  Anupam Sengupta  <anupamsg@gmail.com>
2

  
3
	* Rakefile: Added the rcov option to exclude rcov itself from
4
	coverage reports.
5

  
6
	* lib/tree.rb: Minor comment changes.
7

  
8
	* test/test_tree.rb: Added the TestTree enclosing module, and
9
	renamed tests to meet ZenTest requirements. Also added a few
10
	missing test cases.
11

  
12
	* test/test_binarytree.rb: Added the TestTree enclosing Module,
13
	and renamed the tests to meet ZenTest requirements.
14

  
15
2007-12-19  Anupam Sengupta  <anupamsg@gmail.com>
16

  
17
	* README (Module): Modified the install instructions from source.
18

  
19
	* lib/tree.rb (Tree::TreeNode::initialize): Removed the
20
	unnecessary self_initialize method.
21
	(Tree::TreeNode): Removed the spurious self_initialize from the
22
	protected list.
23
	(Module): Updated the minor version number.
24

  
25
	* Rakefile: Fixed a problem with reading the Tree::VERSION for the
26
	gem packaging, if any prior version of the gem is already installed.
27

  
28
2007-12-18  Anupam Sengupta  <anupamsg@gmail.com>
29

  
30
	* lib/tree.rb: Updated the marshalling logic to correctly handle
31
	non-string content.
32
	(Tree::TreeNode::createDumpRep): Minor code change to use symbols
33
	instead of string key names.
34
	(Tree): Version number change to 0.5.0
35
	(Tree::TreeNode::hasContent): Minor fix to the comments.
36

  
37
	* test/test_tree.rb (TC_TreeTest::test_breadth_each): Updated test
38
	cases for the marshalling logic.
39

  
40
2007-11-12  Anupam Sengupta  <anupamsg@gmail.com>
41

  
42
	* test/test_binarytree.rb: Minor documentation correction.
43

  
44
	* lib/tree/binarytree.rb (Tree::BinaryTreeNode::isRightChild):
45
	Minor documentation change.
46

  
47
2007-10-10  Anupam Sengupta  <anupamsg@gmail.com>
48

  
49
	* README: Restructured the format.
50

  
51
	* Rakefile: Added Hoe related logic. If not present, the Rakefile
52
	will default to old behavior.
53

  
54
2007-10-09  Anupam Sengupta  <anupamsg@gmail.com>
55

  
56
	* Rakefile: Added setup.rb related tasks. Also added the setup.rb in the PKG_FILES list.
57

  
58
2007-10-01  Anupam Sengupta  <anupamsg@gmail.com>
59

  
60
	* Rakefile: Added an optional task for rcov code coverage.
61
	  Added a dependency for rake in the Gem Specification.
62

  
63
	* test/test_binarytree.rb: Removed the unnecessary dependency on "Person" class.
64

  
65
	* test/test_tree.rb: Removed dependency on the redundant "Person" class.
66
	(TC_TreeTest::test_comparator): Added a new test for the spaceship operator.
67
	(TC_TreeTest::test_hasContent): Added tests for hasContent? and length methods.
68

  
69
2007-08-30  Anupam Sengupta  <anupamsg@gmail.com>
70

  
71
	* test/test_tree.rb (TC_TreeTest::test_preordered_each, TC_TreeTest::test_breadth_each, TC_TreeTest::test_detached_copy):
72
	Added new tests for the new functions added to tree.rb.
73

  
74
	* lib/tree.rb (Tree::TreeNode::detached_copy, Tree::TreeNode::preordered_each, Tree::TreeNode::breadth_each):
75
	Added new functions for returning a detached copy of the node and
76
	for performing breadth first traversal. Also added the pre-ordered
77
	traversal function which is an alias of the existing 'each' method.
78

  
79
	* test/test_binarytree.rb (TC_BinaryTreeTest::test_swap_children):
80
	Added a test case for the children swap function.
81

  
82
	* lib/tree/binarytree.rb (Tree::BinaryTreeNode::swap_children):
83
	Added new function to swap the children. Other minor changes in
84
	comments and code.
85

  
86
2007-07-18  Anupam Sengupta  <anupamsg@gmail.com>
87

  
88
	* lib/tree/binarytree.rb (Tree::BinaryTreeNode::leftChild /
89
	rightChild): Minor cosmetic change on the parameter name.
90

  
91
	* test/testbinarytree.rb (TC_BinaryTreeTest::test_isLeftChild):
92
	Minor syntax correction.
93

  
94
	* lib/tree.rb (Tree::TreeNode::depth): Added a tree depth
95
	computation method.
96
	(Tree::TreeNode::breadth): Added a tree breadth method.
97

  
98
	* test/testtree.rb (TC_TreeTest::test_depth/test_breadth): Added a
99
	test for the depth and breadth method.
100

  
101
	* lib/tree/binarytree.rb (Tree::BinaryTreeNode::is*Child):
102
	Added tests for determining whether a node is a left or right
103
	child.
104

  
105
	* test/testbinarytree.rb: Added the test cases for the binary tree
106
	implementation.
107
	(TC_BinaryTreeTest::test_is*Child): Added tests for right or left
108
	childs.
109

  
110
	* lib/tree/binarytree.rb: Added the binary tree implementation.
111

  
112
2007-07-17  Anupam Sengupta  <anupamsg@gmail.com>
113

  
114
	* lib/tree.rb (Tree::TreeNode::parentage): Renamed 'ancestors'
115
	method to 'parentage' to avoid clobbering Module.ancestors
116

  
117
2007-07-16  Anupam Sengupta  <anupamsg@gmail.com>
118

  
119
	* Rakefile: Added an optional rtags task to generate TAGS file for
120
	Emacs.
121

  
122
	* lib/tree.rb (Tree::TreeNode): Added navigation methods for
123
	siblings and children. Also added some convenience methods.
124

  
125
2007-07-08  Anupam Sengupta  <anupamsg@gmail.com>
126

  
127
	* Rakefile: Added a developer target for generating rdoc for the
128
	website.
129

  
130
2007-06-24  Anupam Sengupta  <anupamsg@gmail.com>
131

  
132
	* test/testtree.rb, lib/tree.rb: Added the each_leaf traversal method.
133

  
134
	* README: Replaced all occurrances of LICENSE with COPYING
135
	and lowercased all instances of the word 'RubyTree'.
136

  
137
	* Rakefile: Replaced all occurrances of LICENSE with COPYING
138

  
139
2007-06-23  Anupam Sengupta  <anupamsg@gmail.com>
140

  
141
	* lib/tree.rb (Tree::TreeNode::isLeaf): Added a isLeaf? method.
142

  
143
	* test/testtree.rb (TC_TreeTest::test_removeFromParent): Added
144
	test for isLeaf? method
145

  
146
	* Rakefile: Added the LICENSE and ChangeLog to the extra RDoc files.
147

  
148
	* lib/tree.rb: Minor updates to the comments.
149

  
150
	* test/testtree.rb: Added the Copyright and License header.
151

  
152
	* test/person.rb: Added the Copyright and License header.
153

  
154
	* lib/tree.rb: Added the Copyright and License header.
155

  
156
	* Rakefile: Added the LICENSE and Changelog to be part of the RDoc task.
157

  
158
	* README: Added documentation in the README, including install
159
	instructions and an example.
160

  
161
	* LICENSE: Added the BSD LICENSE file.
162

  
163
	* Changelog: Added the Changelog file.
vendor/gems/rubytree-0.5.2/History.txt
1
= 0.5.2 / 2007-12-21
2

  
3
* Added more test cases and enabled ZenTest compatibility for the test case
4
  names.
5

  
6
= 0.5.1 / 2007-12-20
7

  
8
* Minor code refactoring.
9

  
10
= 0.5.0 / 2007-12-18
11

  
12
* Fixed the marshalling code to correctly handle non-string content.
13

  
14
= 0.4.3 / 2007-10-09
15

  
16
* Changes to the build mechanism (now uses Hoe).
17

  
18
= 0.4.2 / 2007-10-01
19

  
20
* Minor code refactoring. Changes in the Rakefile.
vendor/gems/rubytree-0.5.2/Manifest.txt
1
COPYING
2
ChangeLog
3
History.txt
4
Manifest.txt
5
README
6
Rakefile
7
TODO
8
lib/tree.rb
9
lib/tree/binarytree.rb
10
setup.rb
11
test/test_binarytree.rb
12
test/test_tree.rb
vendor/gems/rubytree-0.5.2/README
1

  
2
   __       _           _
3
   /__\_   _| |__  _   _| |_ _ __ ___  ___
4
  / \// | | | '_ \| | | | __| '__/ _ \/ _ \
5
 / _  \ |_| | |_) | |_| | |_| | |  __/  __/
6
 \/ \_/\__,_|_.__/ \__, |\__|_|  \___|\___|
7
                  |___/
8

  
9
  (c) 2006, 2007 Anupam Sengupta
10
  http://rubytree.rubyforge.org
11

  
12
Rubytree is a simple implementation of the generic Tree data structure.  This
13
implementation is node-centric, where the individual nodes on the tree are the
14
primary objects and drive the structure.
15

  
16
== INSTALL:
17

  
18
Rubytree is an open source project and is hosted at:
19

  
20
   http://rubytree.rubyforge.org
21

  
22
Rubytree can be downloaded as a Rubygem or as a tar/zip file from:
23

  
24
   http://rubyforge.org/frs/?group_id=1215&release_id=8817
25

  
26
The file-name is one of:
27

  
28
    rubytree-<VERSION>.gem - The Rubygem
29
    rubytree-<VERSION>.tgz - GZipped source files
30
    rubytree-<VERSION>.zip - Zipped  source files
31

  
32
Download the appropriate file-type for your system.
33

  
34
It is recommended to install Rubytree as a Ruby Gem, as this is an easy way to
35
keep the version updated, and keep multiple versions of the library available on
36
your system.
37

  
38
=== Installing the Gem
39

  
40
To Install the Gem, from a Terminal/CLI command prompt, issue the command:
41

  
42
   gem install rubytree
43

  
44
This should install the gem file for Rubytree. Note that you may need to be a
45
super-user (root) to successfully install the gem.
46

  
47
=== Installing from the tgz/zip file
48

  
49
Extract the archive file (tgz or zip) and run the following command from the
50
top-level source directory:
51

  
52
    ruby ./setup.rb
53

  
54
You may need administrator/super-user privileges to complete the setup using
55
this method.
56

  
57
== DOCUMENTATION:
58

  
59
The primary class for this implementation is Tree::TreeNode. See the
60
class documentation for an usage example.
61

  
62
From a command line/terminal prompt, you can issue the following command to view
63
the text mode ri documentation:
64

  
65
    ri Tree::TreeNode
66

  
67
Documentation on the web is available at:
68

  
69
http://rubytree.rubyforge.org/rdoc
70

  
71
== EXAMPLE:
72

  
73
The following code-snippet implements this tree structure:
74

  
75
                 +------------+
76
                 |    ROOT    |
77
                 +-----+------+
78
         +-------------+------------+
79
         |                          |
80
 +-------+-------+          +-------+-------+
81
 |  CHILD 1      |          |  CHILD 2      |
82
 +-------+-------+          +---------------+
83
         |
84
         |
85
 +-------+-------+
86
 | GRANDCHILD 1  |
87
 +---------------+
88

  
89
 require 'tree'
90

  
91
 myTreeRoot = Tree::TreeNode.new("ROOT", "Root Content")
92

  
93
 myTreeRoot << Tree::TreeNode.new("CHILD1", "Child1 Content") << Tree::TreeNode.new("GRANDCHILD1", "GrandChild1 Content")
94

  
95
 myTreeRoot << Tree::TreeNode.new("CHILD2", "Child2 Content")
96

  
97
 myTreeRoot.printTree
98

  
99
 child1 = myTreeRoot["CHILD1"]
100

  
101
 grandChild1 = myTreeRoot["CHILD1"]["GRANDCHILD1"]
102

  
103
 siblingsOfChild1Array = child1.siblings
104

  
105
 immediateChildrenArray = myTreeRoot.children
106

  
107
 # Process all nodes
108

  
109
 myTreeRoot.each { |node| node.content.reverse }
110

  
111
 myTreeRoot.remove!(child1) # Remove the child
112

  
113
== LICENSE:
114

  
115
Rubytree is licensed under BSD license.
116

  
117
Copyright (c) 2006, 2007 Anupam Sengupta
118

  
119
All rights reserved.
120

  
121
Redistribution and use in source and binary forms, with or without modification,
122
are permitted provided that the following conditions are met:
123

  
124
- Redistributions of source code must retain the above copyright notice, this
125
  list of conditions and the following disclaimer.
126

  
127
- Redistributions in binary form must reproduce the above copyright notice, this
128
  list of conditions and the following disclaimer in the documentation and/or
129
  other materials provided with the distribution.
130

  
131
- Neither the name of the organization nor the names of its contributors may
132
  be used to endorse or promote products derived from this software without
133
  specific prior written permission.
134

  
135
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
136
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
137
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
138
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
139
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
140
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
141
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
142
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
144
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145

  
146

  
147
(Document Revision: $Revision: 1.16 $ by $Author: anupamsg $)
vendor/gems/rubytree-0.5.2/Rakefile
1
# Rakefile
2
#
3
# $Revision: 1.27 $ by $Author: anupamsg $
4
# $Name:  $
5
#
6
# Copyright (c) 2006, 2007 Anupam Sengupta
7
#
8
# All rights reserved.
9
#
10
# Redistribution and use in source and binary forms, with or without modification,
11
# are permitted provided that the following conditions are met:
12
#
13
# - Redistributions of source code must retain the above copyright notice, this
14
#   list of conditions and the following disclaimer.
15
#
16
# - Redistributions in binary form must reproduce the above copyright notice, this
17
#   list of conditions and the following disclaimer in the documentation and/or
18
#   other materials provided with the distribution.
19
#
20
# - Neither the name of the organization nor the names of its contributors may
21
#   be used to endorse or promote products derived from this software without
22
#   specific prior written permission.
23
#
24
#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
28
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
31
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
#
35

  
36
require 'rubygems'
37
require 'rake/gempackagetask'
38

  
39
require 'rake/clean'
40
require 'rake/packagetask'
41
require 'rake/testtask'
42
require 'rake/rdoctask'
43

  
44
require 'fileutils'
45

  
46
# General Stuff ####################################################
47

  
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
62

  
63
PKG_FILES = FileList[
64
                     '[A-Z]*',
65
                     '*.rb',
66
                     'lib/**/*.rb',
67
                     'test/**/*.rb'
68
                    ]
69

  
70
# Default is to create a rubygem.
71
desc "Default Task"
72
task :default => :gem
73

  
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
99

  
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
120

  
121
  desc "Prepares for installation"
122
  task :prepare do
123
    ruby "setup.rb config"
124
    ruby "setup.rb setup"
125
  end
126

  
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
148

  
149
# Hoe's rdoc task is ugly.
150
Rake::RDocTask.new(:docs) do |rd|
151
  rd.rdoc_files.include("README", "COPYING", "ChangeLog", "lib/**/*.rb")
152
  rd.rdoc_dir = 'doc'
153
  rd.title = "#{PKG_FULLNAME} Documentation"
154

  
155
  # Use the template only if it is present, otherwise, the standard template is
156
  # used.
157
  template = "../allison/allison.rb"
158
  rd.template = template if File.file?(template)
159

  
160
  rd.options << '--line-numbers' << '--inline-source'
161
end
162

  
163
# Optional TAGS Task.
164
# Needs https://rubyforge.org/projects/rtagstask/
165
begin
166
  require 'rtagstask'
167
  RTagsTask.new do |rd|
168
    rd.vi = false
169
  end
170
rescue LoadError
171
end
172

  
173
# Optional RCOV Task
174
# Needs http://eigenclass.org/hiki/rcov
175
begin
176
  require 'rcov/rcovtask'
177
  Rcov::RcovTask.new do |t|
178
    t.test_files = FileList['test/test*.rb']
179
    t.rcov_opts << "--exclude 'rcov.rb'" # rcov itself should not be profiled
180
    # t.verbose = true     # uncomment to see the executed commands
181
  end
182
rescue LoadError
183
end
184

  
185
#Rakefile,v $
186
# Revision 1.21  2007/07/21 05:14:43  anupamsg
187
# Added a VERSION constant to the Tree module,
188
# and using the same in the Rakefile.
189
#
190
# Revision 1.20  2007/07/21 03:24:25  anupamsg
191
# Minor edits to parameter names. User visible functionality does not change.
192
#
193
# Revision 1.19  2007/07/19 02:16:01  anupamsg
194
# Release 0.4.0 (and minor fix in Rakefile).
195
#
196
# Revision 1.18  2007/07/18 20:15:06  anupamsg
197
# Added two predicate methods in BinaryTreeNode to determine whether a node
198
# is a left or a right node.
199
#
200
# Revision 1.17  2007/07/18 07:17:34  anupamsg
201
# Fixed a  issue where TreeNode.ancestors was shadowing Module.ancestors. This method
202
# has been renamed to TreeNode.parentage.
203
#
204
# Revision 1.16  2007/07/17 05:34:03  anupamsg
205
# Added an optional tags Rake-task for generating the TAGS file for Emacs.
206
#
207
# Revision 1.15  2007/07/17 04:42:45  anupamsg
208
# Minor fixes to the Rakefile.
209
#
210
# Revision 1.14  2007/07/17 03:39:28  anupamsg
211
# Moved the CVS Log keyword to end of the files.
212
#
vendor/gems/rubytree-0.5.2/TODO
1
# -*- mode: outline; coding: utf-8-unix; -*-
2

  
3
* Add logic in Rakefile to read the file list from Manifest.txt file.
4

  
5
* Add a YAML export method to the TreeNode class.
6

  
7

  
vendor/gems/rubytree-0.5.2/lib/tree.rb
1
# tree.rb
2
#
3
# $Revision: 1.29 $ by $Author: anupamsg $
4
# $Name:  $
5
#
6
# = tree.rb - Generic Multi-way Tree implementation
7
#
8
# Provides a generic tree data structure with ability to
9
# store keyed node elements in the tree. The implementation
10
# mixes in the Enumerable module.
11
#
12
# Author:: Anupam Sengupta (anupamsg@gmail.com)
13
#
14

  
15
# Copyright (c) 2006, 2007 Anupam Sengupta
16
#
17
# All rights reserved.
18
#
19
# Redistribution and use in source and binary forms, with or without modification,
20
# are permitted provided that the following conditions are met:
21
#
22
# - Redistributions of source code must retain the above copyright notice, this
23
#   list of conditions and the following disclaimer.
24
#
25
# - Redistributions in binary form must reproduce the above copyright notice, this
26
#   list of conditions and the following disclaimer in the documentation and/or
27
#   other materials provided with the distribution.
28
#
29
# - Neither the name of the organization nor the names of its contributors may
30
#   be used to endorse or promote products derived from this software without
31
#   specific prior written permission.
32
#
33
#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
34
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
37
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
40
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43
#
44

  
45
# This module provides a TreeNode class which is the primary class for all
46
# nodes represented in the Tree.
47
# This module mixes in the Enumerable module.
48
module Tree
49

  
50
  # Rubytree Package Version
51
  VERSION = '0.5.2'
52

  
53
  # == TreeNode Class Description
54
  #
55
  # The node class for the tree representation. the nodes are named and have a
56
  # place-holder for the node data (i.e., the `content' of the node). The node
57
  # names are expected to be unique.  In addition, the node provides navigation
58
  # methods to traverse the tree.
59
  #
60
  # The nodes can have any number of child nodes attached to it. Note that while
61
  # this implementation does not support directed graphs, the class itself makes
62
  # no restrictions on associating a node's CONTENT with multiple parent nodes.
63
  #
64
  #
65
  # == Example
66
  #
67
  #  The following code-snippet implements this tree structure:
68
  #
69
  #                    +------------+
70
  #                    |    ROOT    |
71
  #                    +-----+------+
72
  #            +-------------+------------+
73
  #            |                          |
74
  #    +-------+-------+          +-------+-------+
75
  #    |  CHILD 1      |          |  CHILD 2      |
76
  #    +-------+-------+          +---------------+
77
  #            |
78
  #            |
79
  #    +-------+-------+
80
  #    | GRANDCHILD 1  |
81
  #    +---------------+
82
  #
83
  # require 'tree'
84
  #
85
  # myTreeRoot = Tree::TreeNode.new("ROOT", "Root Content")
86
  #
87
  # myTreeRoot << Tree::TreeNode.new("CHILD1", "Child1 Content") << Tree::TreeNode.new("GRANDCHILD1", "GrandChild1 Content")
88
  #
89
  # myTreeRoot << Tree::TreeNode.new("CHILD2", "Child2 Content")
90
  #
91
  # myTreeRoot.printTree
92
  #
93
  # child1 = myTreeRoot["CHILD1"]
94
  #
95
  # grandChild1 = myTreeRoot["CHILD1"]["GRANDCHILD1"]
96
  #
97
  # siblingsOfChild1Array = child1.siblings
98
  #
99
  # immediateChildrenArray = myTreeRoot.children
100
  #
101
  # # Process all nodes
102
  #
103
  # myTreeRoot.each { |node| node.content.reverse }
104
  #
105
  # myTreeRoot.remove!(child1) # Remove the child
106
  class TreeNode
107
    include Enumerable
108

  
109
    attr_reader :content, :name, :parent
110
    attr_writer :content
111

  
112
    # Constructor which expects the name of the node
113
    #
114
    # Name of the node is expected to be unique across the
115
    # tree.
116
    #
117
    # The content can be of any type, and is defaulted to _nil_.
118
    def initialize(name, content = nil)
119
      raise "Node name HAS to be provided" if name == nil
120
      @name = name
121
      @content = content
122
      self.setAsRoot!
123

  
124
      @childrenHash = Hash.new
125
      @children = []
126
    end
127

  
128
    # Returns a copy of this node, with the parent and children links removed.
129
    def detached_copy
130
      Tree::TreeNode.new(@name, @content ? @content.clone : nil)
131
    end
132

  
133
    # Print the string representation of this node.
134
    def to_s
135
      "Node Name: #{@name}" +
136
        " Content: " + (@content || "<Empty>") +
137
        " Parent: " + (isRoot?()  ? "<None>" : @parent.name) +
138
        " Children: #{@children.length}" +
139
        " Total Nodes: #{size()}"
140
    end
141

  
142
    # Returns an array of ancestors in reversed order (the first element is the
143
    # immediate parent). Returns nil if this is a root node.
144
    def parentage
145
      return nil if isRoot?
146

  
147
      parentageArray = []
148
      prevParent = self.parent
149
      while (prevParent)
150
        parentageArray << prevParent
151
        prevParent = prevParent.parent
152
      end
153

  
154
      parentageArray
155
    end
156

  
157
    # Protected method to set the parent node.
158
    # This method should NOT be invoked by client code.
159
    def parent=(parent)
160
      @parent = parent
161
    end
162

  
163
    # Convenience synonym for TreeNode#add method.  This method allows a convenient
164
    # method to add children hierarchies in the tree.
165
    #
166
    # E.g. root << child << grand_child
167
    def <<(child)
168
      add(child)
169
    end
170

  
171
    # Adds the specified child node to the receiver node.  The child node's
172
    # parent is set to be the receiver.  The child is added as the last child in
173
    # the current list of children for the receiver node.
174
    def add(child)
175
      raise "Child already added" if @childrenHash.has_key?(child.name)
176

  
177
      @childrenHash[child.name]  = child
178
      @children << child
179
      child.parent = self
180
      return child
181

  
182
    end
183

  
184
    # Removes the specified child node from the receiver node.  The removed
185
    # children nodes are orphaned but available if an alternate reference
186
    # exists.
187
    #
188
    # Returns the child node.
189
    def remove!(child)
190
      @childrenHash.delete(child.name)
191
      @children.delete(child)
192
      child.setAsRoot! unless child == nil
193
      return child
194
    end
195

  
196
    # Removes this node from its parent. If this is the root node, then does
197
    # nothing.
198
    def removeFromParent!
199
      @parent.remove!(self) unless isRoot?
200
    end
201

  
202
    # Removes all children from the receiver node.
203
    def removeAll!
204
      for child in @children
205
        child.setAsRoot!
206
      end
207
      @childrenHash.clear
208
      @children.clear
209
      self
210
    end
211

  
212
    # Indicates whether this node has any associated content.
213
    def hasContent?
214
      @content != nil
215
    end
216

  
217
    # Protected method which sets this node as a root node.
218
    def setAsRoot!
219
      @parent = nil
220
    end
221

  
222
    # Indicates whether this node is a root node. Note that
223
    # orphaned children will also be reported as root nodes.
224
    def isRoot?
225
      @parent == nil
226
    end
227

  
228
    # Indicates whether this node has any immediate child nodes.
229
    def hasChildren?
230
      @children.length != 0
231
    end
232

  
233
    # Indicates whether this node is a 'leaf' - i.e., one without
234
    # any children
235
    def isLeaf?
236
      !hasChildren?
237
    end
238

  
239
    # Returns an array of all the immediate children.  If a block is given,
240
    # yields each child node to the block.
241
    def children
242
      if block_given?
243
        @children.each {|child| yield child}
244
      else
245
        @children
246
      end
247
    end
248

  
249
    # Returns the first child of this node. Will return nil if no children are
250
    # present.
251
    def firstChild
252
      children.first
253
    end
254

  
255
    # Returns the last child of this node. Will return nil if no children are
256
    # present.
257
    def lastChild
258
      children.last
259
    end
260

  
261
    # Returns every node (including the receiver node) from the tree to the
262
    # specified block. The traversal is depth first and from left to right in
263
    # pre-ordered sequence.
264
    def each &block
265
      yield self
266
      children { |child| child.each(&block) }
267
    end
268

  
269
    # Traverses the tree in a pre-ordered sequence. This is equivalent to
270
    # TreeNode#each
271
    def preordered_each &block
272
      each(&block)
273
    end
274

  
275
    # Performs breadth first traversal of the tree rooted at this node. The
276
    # traversal in a given level is from left to right.
277
    def breadth_each &block
278
      node_queue = [self]       # Create a queue with self as the initial entry
279

  
280
      # Use a queue to do breadth traversal
281
      until node_queue.empty?
282
        node_to_traverse = node_queue.shift
283
        yield node_to_traverse
284
        # Enqueue the children from left to right.
285
        node_to_traverse.children { |child| node_queue.push child }
286
      end
287
    end
288

  
289
    # Yields all leaf nodes from this node to the specified block. May yield
290
    # this node as well if this is a leaf node.  Leaf traversal depth first and
291
    # left to right.
292
    def each_leaf &block
293
      self.each { |node| yield(node) if node.isLeaf? }
294
    end
295

  
296
    # Returns the requested node from the set of immediate children.
297
    #
298
    # If the parameter is _numeric_, then the in-sequence array of children is
299
    # accessed (see Tree#children).  If the parameter is not _numeric_, then it
300
    # is assumed to be the *name* of the child node to be returned.
301
    def [](name_or_index)
302
      raise "Name_or_index needs to be provided" if name_or_index == nil
303

  
304
      if name_or_index.kind_of?(Integer)
305
        @children[name_or_index]
306
      else
307
        @childrenHash[name_or_index]
308
      end
309
    end
310

  
311
    # Returns the total number of nodes in this tree, rooted at the receiver
312
    # node.
313
    def size
314
      @children.inject(1) {|sum, node| sum + node.size}
315
    end
316

  
317
    # Convenience synonym for Tree#size
318
    def length
319
      size()
320
    end
321

  
322
    # Pretty prints the tree starting with the receiver node.
323
    def printTree(level = 0)
324

  
325
      if isRoot?
326
        print "*"
327
      else
328
        print "|" unless parent.isLastSibling?
329
        print(' ' * (level - 1) * 4)
330
        print(isLastSibling? ? "+" : "|")
331
        print "---"
332
        print(hasChildren? ? "+" : ">")
333
      end
334

  
335
      puts " #{name}"
336

  
337
      children { |child| child.printTree(level + 1)}
338
    end
339

  
340
    # Returns the root for this tree. Root's root is itself.
341
    def root
342
      root = self
343
      root = root.parent while !root.isRoot?
344
      root
345
    end
346

  
347
    # Returns the first sibling for this node. If this is the root node, returns
348
    # itself.
349
    def firstSibling
350
      if isRoot?
351
        self
352
      else
353
        parent.children.first
354
      end
355
    end
356

  
357
    # Returns true if this node is the first sibling.
358
    def isFirstSibling?
359
      firstSibling == self
360
    end
361

  
362
    # Returns the last sibling for this node.  If this node is the root, returns
363
    # itself.
364
    def lastSibling
365
      if isRoot?
366
        self
367
      else
368
        parent.children.last
369
      end
370
    end
371

  
372
    # Returns true if his node is the last sibling
373
    def isLastSibling?
374
      lastSibling == self
375
    end
376

  
377
    # Returns an array of siblings for this node.
378
    # If a block is provided, yields each of the sibling
379
    # nodes to the block. The root always has nil siblings.
380
    def siblings
381
      return nil if isRoot?
382
      if block_given?
383
        for sibling in parent.children
384
          yield sibling if sibling != self
385
        end
386
      else
387
        siblings = []
388
        parent.children {|sibling| siblings << sibling if sibling != self}
389
        siblings
390
      end
391
    end
392

  
393
    # Returns true if this node is the only child of its parent
394
    def isOnlyChild?
395
      parent.children.size == 1
396
    end
397

  
398
    # Returns the next sibling for this node. Will return nil if no subsequent
399
    # node is present.
400
    def nextSibling
401
      if myidx = parent.children.index(self)
402
        parent.children.at(myidx + 1)
403
      end
404
    end
405

  
406
    # Returns the previous sibling for this node. Will return nil if no
407
    # subsequent node is present.
408
    def previousSibling
409
      if myidx = parent.children.index(self)
410
        parent.children.at(myidx - 1) if myidx > 0
411
      end
412
    end
413

  
414
    # Provides a comparision operation for the nodes. Comparision
415
    # is based on the natural character-set ordering for the
416
    # node names.
417
    def <=>(other)
418
      return +1 if other == nil
419
      self.name <=> other.name
420
    end
421

  
422
    # Freezes all nodes in the tree
423
    def freezeTree!
424
      each {|node| node.freeze}
425
    end
426

  
427
    # Creates the marshal-dump represention of the tree rooted at this node.
428
    def marshal_dump
429
      self.collect { |node| node.createDumpRep }
430
    end
431

  
432
    # Creates a dump representation and returns the same as a hash.
433
    def createDumpRep
434
      { :name => @name, :parent => (isRoot? ? nil : @parent.name),  :content => Marshal.dump(@content)}
435
    end
436

  
437
    # Loads a marshalled dump of the tree and returns the root node of the
438
    # reconstructed tree. See the Marshal class for additional details.
439
    def marshal_load(dumped_tree_array)
440
      nodes = { }
441
      for node_hash in dumped_tree_array do
442
        name        = node_hash[:name]
443
        parent_name = node_hash[:parent]
444
        content     = Marshal.load(node_hash[:content])
445

  
446
        if parent_name then
447
          nodes[name] = current_node = Tree::TreeNode.new(name, content)
448
          nodes[parent_name].add current_node
449
        else
450
          # This is the root node, hence initialize self.
451
          initialize(name, content)
452

  
453
          nodes[name] = self    # Add self to the list of nodes
454
         end
455
      end
456
    end
457

  
458
    # Returns depth of the tree from this node. A single leaf node has a
459
    # depth of 1.
460
    def depth
461
      return 1 if isLeaf?
462
      1 + @children.collect { |child| child.depth }.max
463
    end
464

  
465
    # Returns breadth of the tree at this node level. A single node has a
466
    # breadth of 1.
467
    def breadth
468
      return 1 if isRoot?
469
      parent.children.size
470
    end
471

  
472
    protected :parent=, :setAsRoot!, :createDumpRep
473

  
474
  end
475
end
476

  
477
# $Log: tree.rb,v $
478
# Revision 1.29  2007/12/22 00:28:59  anupamsg
479
# Added more test cases, and enabled ZenTest compatibility.
480
#
481
# Revision 1.28  2007/12/20 03:19:33  anupamsg
482
# * README (Module): Modified the install instructions from source.
483
# (Module): Updated the minor version number.
484
#
485
# Revision 1.27  2007/12/20 03:00:03  anupamsg
486
# Minor code changes. Removed self_initialize from the protected methods' list.
487
#
488
# Revision 1.26  2007/12/20 02:50:04  anupamsg
489
# (Tree::TreeNode): Removed the spurious self_initialize from the protected list.
490
#
491
# Revision 1.25  2007/12/19 20:28:05  anupamsg
492
# Removed the unnecesary self_initialize method.
493
#
494
# Revision 1.24  2007/12/19 06:39:17  anupamsg
495
# Removed the unnecessary field and record separator constants.  Also updated the
496
# history.txt file.
497
#
498
# Revision 1.23  2007/12/19 06:25:00  anupamsg
499
# (Tree::TreeNode): Minor fix to the comments.  Also fixed the private/protected
500
# scope issue with the createDumpRep method.
501
#
502
# Revision 1.22  2007/12/19 06:22:03  anupamsg
503
# Updated the marshalling logic to correctly handle non-string content. This
504
# should fix the bug # 15614 ("When dumping with an Object as the content, you get
505
# a delimiter collision")
506
#
507
# Revision 1.21  2007/12/19 02:24:17  anupamsg
508
# Updated the marshalling logic to handle non-string contents on the nodes.
509
#
510
# Revision 1.20  2007/10/10 08:42:57  anupamsg
511
# Release 0.4.3
512
#
513
# Revision 1.19  2007/08/31 01:16:27  anupamsg
514
# Added breadth and pre-order traversals for the tree. Also added a method
515
# to return the detached copy of a node from the tree.
516
#
517
# Revision 1.18  2007/07/21 05:14:44  anupamsg
518
# Added a VERSION constant to the Tree module,
519
# and using the same in the Rakefile.
520
#
521
# Revision 1.17  2007/07/21 03:24:25  anupamsg
522
# Minor edits to parameter names. User visible functionality does not change.
523
#
524
# Revision 1.16  2007/07/18 23:38:55  anupamsg
525
# Minor updates to tree.rb
526
#
527
# Revision 1.15  2007/07/18 22:11:50  anupamsg
528
# Added depth and breadth methods for the TreeNode.
529
#
530
# Revision 1.14  2007/07/18 19:33:27  anupamsg
531
# Added a new binary tree implementation.
532
#
533
# Revision 1.13  2007/07/18 07:17:34  anupamsg
534
# Fixed a  issue where TreeNode.ancestors was shadowing Module.ancestors. This method
535
# has been renamed to TreeNode.parentage.
536
#
537
# Revision 1.12  2007/07/17 03:39:28  anupamsg
538
# Moved the CVS Log keyword to end of the files.
539
#
vendor/gems/rubytree-0.5.2/lib/tree/binarytree.rb
1
# binarytree.rb
2
#
3
# $Revision: 1.5 $ by $Author: anupamsg $
4
# $Name:  $
5
#
6
# = binarytree.rb - Binary Tree implementation
7
#
8
# Provides a generic tree data structure with ability to
9
# store keyed node elements in the tree. The implementation
10
# mixes in the Enumerable module.
11
#
12
# Author:: Anupam Sengupta (anupamsg@gmail.com)
13
#
14

  
15
# Copyright (c) 2007 Anupam Sengupta
16
#
17
# All rights reserved.
18
#
19
# Redistribution and use in source and binary forms, with or without modification,
20
# are permitted provided that the following conditions are met:
21
#
22
# - Redistributions of source code must retain the above copyright notice, this
23
#   list of conditions and the following disclaimer.
24
#
25
# - Redistributions in binary form must reproduce the above copyright notice, this
26
#   list of conditions and the following disclaimer in the documentation and/or
27
#   other materials provided with the distribution.
28
#
29
# - Neither the name of the organization nor the names of its contributors may
30
#   be used to endorse or promote products derived from this software without
31
#   specific prior written permission.
32
#
33
#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
34
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
37
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
40
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43
#
44

  
45
require 'tree'
46

  
47
module Tree
48

  
49
  # Provides a Binary tree implementation. This tree node allows only two child
50
  # nodes (left and right childs). It also provides direct access to the left
51
  # and right children, including assignment to the same.
52
  class BinaryTreeNode < TreeNode
53

  
54
    # Adds the specified child node to the receiver node.  The child node's
55
    # parent is set to be the receiver.  The child nodes are added in the order
56
    # of addition, i.e., the first child added becomes the left node, and the
57
    # second child will be the second node.
58
    # If only one child is present, then this will be the left child.
59
    def add(child)
60
      raise "Already has two child nodes" if @children.size == 2
61

  
62
      super(child)
63
    end
64

  
65
    # Returns the left child node. Note that
66
    # left Child == first Child
67
    def leftChild
68
      children.first
69
    end
70

  
71
    # Returns the right child node. Note that
72
    # right child == last child unless there is only one child.
73
    # Returns nil if the right child does not exist.
74
    def rightChild
75
      children[1]
76
    end
77

  
78
    # Sets the left child. If a previous child existed, it is replaced.
79
    def leftChild=(child)
80
      @children[0] = child
81
      @childrenHash[child.name] = child if child # Assign the name mapping
82
    end
83

  
84
    # Sets the right child. If a previous child existed, it is replaced.
85
    def rightChild=(child)
86
      @children[1] = child
87
      @childrenHash[child.name] = child if child # Assign the name mapping
88
    end
89

  
90
    # Returns true if this is the left child of its parent. Always returns false
91
    # if this is the root node.
92
    def isLeftChild?
93
      return nil if isRoot?
94
      self == parent.leftChild
95
    end
96

  
97
    # Returns true if this is the right child of its parent. Always returns false
98
    # if this is the root node.
99
    def isRightChild?
100
      return nil if isRoot?
101
      self == parent.rightChild
102
    end
103

  
104
    # Swaps the left and right children with each other
105
    def swap_children
106
      tempChild = leftChild
107
      self.leftChild= rightChild
108
      self.rightChild= tempChild
109
    end
110
  end
111

  
112
end
113

  
114
# $Log: binarytree.rb,v $
... This diff was truncated because it exceeds the maximum size that can be displayed.