multiroot_tree 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.8.7"
4
+ - "1.9.2"
5
+ - "1.9.3"
6
+ - jruby-18mode # JRuby in 1.8 mode
7
+ - jruby-19mode # JRuby in 1.9 mode
8
+ - rbx-18mode
9
+ - rbx-19mode
10
+ - ruby-head
11
+ - jruby-head
12
+ - ree
13
+ # uncomment this line if your project needs to run something other than `rake`:
14
+ # script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.8.0"
10
+ #gem "rdoc", "~> 3.12"
11
+ gem "bundler"
12
+ gem "jeweler", "~> 1.8.4"
13
+ #gem "rcov", ">= 0"
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,30 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ git (1.2.5)
6
+ jeweler (1.8.4)
7
+ bundler (~> 1.0)
8
+ git (>= 1.2.5)
9
+ rake
10
+ rdoc
11
+ json (1.7.5)
12
+ rake (10.0.2)
13
+ rdoc (3.12)
14
+ json (~> 1.4)
15
+ rspec (2.8.0)
16
+ rspec-core (~> 2.8.0)
17
+ rspec-expectations (~> 2.8.0)
18
+ rspec-mocks (~> 2.8.0)
19
+ rspec-core (2.8.0)
20
+ rspec-expectations (2.8.0)
21
+ diff-lcs (~> 1.1.2)
22
+ rspec-mocks (2.8.0)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ bundler
29
+ jeweler (~> 1.8.4)
30
+ rspec (~> 2.8.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 dp
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,4 @@
1
+ multiroot_tree
2
+ ==========
3
+
4
+ Multiroot_tree is a tree data structure that allows for an arbitrary number of roots/parents and an arbitrary number of children.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = multiroot_tree
2
+
3
+ Multiroot_tree is tree data structure that can have an arbitrary number of roots and arbitrary number of children.
4
+
5
+ == Contributing to multiroot_tree
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 dp. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "multiroot_tree"
18
+ gem.homepage = "http://github.com/phamdt/multiroot_tree"
19
+ gem.license = "MIT"
20
+ gem.summary = "Tree data structure with arbitrary number of roots"
21
+ gem.description = "Multiroot_tree has some of the functions of a typical tree data structure. It can be used to model process, dependencies, and prerequisites."
22
+ gem.authors = ["dp"]
23
+ # dependencies defined in Gemfile
24
+ end
25
+ Jeweler::RubygemsDotOrgTasks.new
26
+
27
+ require 'rspec/core'
28
+ require 'rspec/core/rake_task'
29
+ RSpec::Core::RakeTask.new(:spec) do |spec|
30
+ spec.pattern = FileList['spec/**/*_spec.rb']
31
+ end
32
+
33
+ # RSpec::Core::RakeTask.new(:rcov) do |spec|
34
+ # spec.pattern = 'spec/**/*_spec.rb'
35
+ # spec.rcov = true
36
+ # end
37
+
38
+ task :default => :spec
39
+
40
+ require 'rdoc/task'
41
+ Rake::RDocTask.new do |rdoc|
42
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
43
+
44
+ rdoc.rdoc_dir = 'rdoc'
45
+ rdoc.title = "multiroot_tree #{version}"
46
+ rdoc.rdoc_files.include('README*')
47
+ rdoc.rdoc_files.include('lib/**/*.rb')
48
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,101 @@
1
+ ##
2
+ # Beginning implementation of a tree data structure with multiple
3
+ # and arbitrary number of roots or N roots. Structure can be used
4
+ # to model business logic, sequence of events, or ingredients.
5
+ ##
6
+
7
+ ##
8
+ # Danny Pham
9
+ # 12/10/12
10
+ ##
11
+
12
+ class MultirootTree
13
+ include Enumerable
14
+ attr_reader :content, :children, :parents
15
+
16
+ def initialize(content)
17
+ @content = content
18
+ @children = []
19
+ @parents = []
20
+ end
21
+
22
+ #add a child to the children array, instance variable
23
+ def add_child(child)
24
+ if child.instance_of? MultirootTree
25
+ @children << child unless @children.include? child
26
+ child.add_parent(self) unless child.parents.include?(self)
27
+ end
28
+ end
29
+
30
+ #adds a list of MultirootTrees with each successive
31
+ #MultirootTree being the child of the previous
32
+ def add_descendant_line(*descendants)
33
+ max = descendants.size
34
+ num = 0
35
+ first = self
36
+ second = descendants[num]
37
+
38
+ while (num < descendants.size)
39
+ first.add_child(second)
40
+ first = second
41
+ second = descendants[num + 1]
42
+ num += 1
43
+ end
44
+ end
45
+
46
+ #severs tree connections going down descendant line
47
+ def disowns(child)
48
+ if child.is_a? MultirootTree
49
+ @children -= [child]
50
+ child.emancipates_from(self) if child.parents.include?(self)
51
+ end
52
+ end
53
+
54
+ #severs tree connections going up the ancestry
55
+ def emancipates_from(parent)
56
+ @parents -= [parent]
57
+ parent.disowns(self) if parent.children.include?(self)
58
+ end
59
+
60
+ #adding parents is particularly necessary because any one
61
+ #node can have N parents, not just one
62
+ def add_parent(parent)
63
+ @parents << parent unless @parents.include? parent
64
+ parent.add_child(self) unless parent.children.include?(self)
65
+ end
66
+
67
+ #adds immediate parents, not an ancestor line
68
+ def add_parents(*parents)
69
+ parents.each do |parent|
70
+ self.add_parent(parent)
71
+ parent.add_child(self) unless parent.children.include?(self)
72
+ end
73
+ end
74
+
75
+ #returns roots/greatest parents
76
+ def roots
77
+ roots ||= []
78
+ if @parents.flatten.size == 0
79
+ roots << self
80
+ else
81
+ @parents.each {|parent| roots << parent.roots}
82
+ end
83
+ roots.flatten
84
+ end
85
+
86
+ #returns "youngest" children
87
+ def floor
88
+ floor = []
89
+ if @children.flatten.size == 0
90
+ floor << MultirootTree.new(self.content)
91
+ else
92
+ @children.each {|child| floor << child.floor}
93
+ end
94
+ floor.flatten
95
+ end
96
+
97
+ #returns value of node
98
+ def inspect
99
+ @content.inspect
100
+ end
101
+ end
@@ -0,0 +1,59 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "multiroot_tree"
8
+ s.version = "0.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["dp"]
12
+ s.date = "2012-12-11"
13
+ s.description = "Multiroot_tree has some of the functions of a typical tree data structure. It can be used to model process, dependencies, and prerequisites."
14
+ s.extra_rdoc_files = [
15
+ "LICENSE.txt",
16
+ "README.md",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ ".travis.yml",
23
+ "Gemfile",
24
+ "Gemfile.lock",
25
+ "LICENSE.txt",
26
+ "README.md",
27
+ "README.rdoc",
28
+ "Rakefile",
29
+ "VERSION",
30
+ "lib/multiroot_tree.rb",
31
+ "multiroot_tree.gemspec",
32
+ "spec/multiroot_tree_spec.rb",
33
+ "spec/spec_helper.rb"
34
+ ]
35
+ s.homepage = "http://github.com/phamdt/multiroot_tree"
36
+ s.licenses = ["MIT"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = "1.8.24"
39
+ s.summary = "Tree data structure with arbitrary number of roots"
40
+
41
+ if s.respond_to? :specification_version then
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
+ s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
46
+ s.add_development_dependency(%q<bundler>, [">= 0"])
47
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
48
+ else
49
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
50
+ s.add_dependency(%q<bundler>, [">= 0"])
51
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
52
+ end
53
+ else
54
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
55
+ s.add_dependency(%q<bundler>, [">= 0"])
56
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
57
+ end
58
+ end
59
+
@@ -0,0 +1,151 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ ##
3
+ # This spec uses college courses and their prerequisites as
4
+ # the content to test the MultirootTree structure
5
+ ##
6
+
7
+ ##
8
+ # Danny Pham
9
+ # 12/10/12
10
+ ##
11
+
12
+ describe MultirootTree do
13
+ it "adds a child" do
14
+ e0 = MultirootTree.new("ENGL 073")
15
+ e1 = MultirootTree.new("ENGL 092")
16
+
17
+ e0.add_child(e1)
18
+
19
+ e0.children.should include(e1)
20
+ end
21
+
22
+ it "adds a child then the child adds its own child" do
23
+ f0 = MultirootTree.new("FRCH 121")
24
+ f1 = MultirootTree.new("FRCH 122")
25
+ f2 = MultirootTree.new("FRCH 123")
26
+
27
+ f0.add_child(f1)
28
+ f1.add_child(f2)
29
+
30
+ (f0.children.should include(f1)) && (f1.children.should include(f2))
31
+ end
32
+
33
+ it "adds a child and the child has it as a parent" do
34
+ f0 = MultirootTree.new("FRCH 121")
35
+ f1 = MultirootTree.new("FRCH 122")
36
+
37
+ f0.add_child(f1)
38
+
39
+ f1.parents.should include(f0)
40
+ end
41
+
42
+ it "adds line of descendants" do
43
+ p0 = MultirootTree.new("PROG 110")
44
+ p1 = MultirootTree.new("PROG 118")
45
+ p2 = MultirootTree.new("PROG 120")
46
+ p3 = MultirootTree.new("PROG 140")
47
+ p4 = MultirootTree.new("PROG 210")
48
+ p5 = MultirootTree.new("PROG 225")
49
+
50
+ p0.add_descendant_line(p1, p2, p3, p4, p5)
51
+
52
+ ((p0.children.should include(p1)) && \
53
+ (p1.children.should include(p2)) && \
54
+ (p2.children.should include(p3)) && \
55
+ (p3.children.should include(p4)) && \
56
+ (p4.children.should include(p5))) == true
57
+ end
58
+
59
+ it "adds a parent" do
60
+ d1 = MultirootTree.new("BUSIT 103")
61
+ d2 = MultirootTree.new("DBA 130")
62
+
63
+ d2.add_parent(d1)
64
+
65
+ d2.parents.should include(d1)
66
+ end
67
+
68
+ it "adds multiple parents" do
69
+ p3 = MultirootTree.new("PROG 140")
70
+
71
+ m3 = MultirootTree.new("MATH 099")
72
+ e1 = MultirootTree.new("ENGL 101")
73
+ d2 = MultirootTree.new("DBA 130")
74
+
75
+ p3.add_parents(m3, e1, d2)
76
+ p3.parents.should include(m3, e1, d2)
77
+ end
78
+
79
+ it "adds a parent and the parent adds it as a child" do
80
+ d1 = MultirootTree.new("BUSIT 103")
81
+ d2 = MultirootTree.new("DBA 130")
82
+
83
+ d2.add_parent(d1)
84
+
85
+ d1.children.should include(d2)
86
+ end
87
+
88
+ it "adds children then returns itself when finding roots (greatest parents)" do
89
+ m0 = MultirootTree.new("MATH 075")
90
+ m1 = MultirootTree.new("MATH 097")
91
+ m2 = MultirootTree.new("MATH 098")
92
+ m3 = MultirootTree.new("MATH 099")
93
+
94
+ m0.add_child(m1)
95
+ m1.add_child(m2)
96
+ m2.add_child(m3)
97
+
98
+ (m3.roots.inspect == m0.inspect && m0.roots.inspect == m0.inspect)
99
+ end
100
+
101
+ it "adds parents then returns the last parent as the root" do
102
+ m0 = MultirootTree.new("MATH 075")
103
+ m1 = MultirootTree.new("MATH 097")
104
+ m2 = MultirootTree.new("MATH 098")
105
+ m3 = MultirootTree.new("MATH 099")
106
+
107
+ m3.add_parent(m2)
108
+ m2.add_parent(m1)
109
+ m1.add_parent(m0)
110
+
111
+ (m3.roots.inspect == m0.inspect && m0.roots.inspect == m0.inspect)
112
+ end
113
+
114
+ it "finds floor (youngest children)" do
115
+ m0 = MultirootTree.new("MATH 075")
116
+ m1 = MultirootTree.new("MATH 097")
117
+ m2 = MultirootTree.new("MATH 098")
118
+ m3 = MultirootTree.new("MATH 099")
119
+
120
+ m3.add_parent(m2)
121
+ m2.add_parent(m1)
122
+ m1.add_parent(m0)
123
+
124
+ m0.floor.inspect == m3.inspect
125
+ end
126
+
127
+ it "disowns a child" do
128
+ m0 = MultirootTree.new("MATH 075")
129
+ m1 = MultirootTree.new("MATH 097")
130
+
131
+ m0.add_child(m1)
132
+
133
+ m0.disowns(m1)
134
+
135
+ (m1.parents.size == 0 && m0.children.size == 0)
136
+ end
137
+
138
+ it "emancipates from a parent" do
139
+ m0 = MultirootTree.new("MATH 075")
140
+ m1 = MultirootTree.new("MATH 097")
141
+
142
+ m0.add_child(m1)
143
+
144
+ m1.emancipates_from(m0)
145
+
146
+ (m1.parents.size == 0 && m0.children.size == 0)
147
+ end
148
+ end
149
+
150
+
151
+
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'multiroot_tree'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: multiroot_tree
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.0
6
+ platform: ruby
7
+ authors:
8
+ - dp
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-12-11 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 2.8.0
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: bundler
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: jeweler
39
+ requirement: &id003 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 1.8.4
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *id003
48
+ description: Multiroot_tree has some of the functions of a typical tree data structure. It can be used to model process, dependencies, and prerequisites.
49
+ email:
50
+ executables: []
51
+
52
+ extensions: []
53
+
54
+ extra_rdoc_files:
55
+ - LICENSE.txt
56
+ - README.md
57
+ - README.rdoc
58
+ files:
59
+ - .document
60
+ - .rspec
61
+ - .travis.yml
62
+ - Gemfile
63
+ - Gemfile.lock
64
+ - LICENSE.txt
65
+ - README.md
66
+ - README.rdoc
67
+ - Rakefile
68
+ - VERSION
69
+ - lib/multiroot_tree.rb
70
+ - multiroot_tree.gemspec
71
+ - spec/multiroot_tree_spec.rb
72
+ - spec/spec_helper.rb
73
+ homepage: http://github.com/phamdt/multiroot_tree
74
+ licenses:
75
+ - MIT
76
+ post_install_message:
77
+ rdoc_options: []
78
+
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ hash: 523468993
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: "0"
96
+ requirements: []
97
+
98
+ rubyforge_project:
99
+ rubygems_version: 1.8.24
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: Tree data structure with arbitrary number of roots
103
+ test_files: []
104
+