basic_tree 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/basic_tree.gemspec +4 -4
- data/lib/basic_tree.rb +10 -55
- data/spec/basic_tree_spec.rb +81 -1
- metadata +11 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
data/basic_tree.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{basic_tree}
|
8
|
-
s.version = "0.0
|
8
|
+
s.version = "0.1.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Austin Schneider"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-07-01}
|
13
13
|
s.description = %q{A basic tree structure}
|
14
14
|
s.email = %q{soccer022483@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -32,7 +32,7 @@ Gem::Specification.new do |s|
|
|
32
32
|
s.homepage = %q{http://github.com/soccer022483/basic_tree}
|
33
33
|
s.rdoc_options = ["--charset=UTF-8"]
|
34
34
|
s.require_paths = ["lib"]
|
35
|
-
s.rubygems_version = %q{1.3.
|
35
|
+
s.rubygems_version = %q{1.3.7}
|
36
36
|
s.summary = %q{A basic tree structure}
|
37
37
|
s.test_files = [
|
38
38
|
"spec/basic_tree_spec.rb",
|
@@ -43,7 +43,7 @@ Gem::Specification.new do |s|
|
|
43
43
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
44
44
|
s.specification_version = 3
|
45
45
|
|
46
|
-
if Gem::Version.new(Gem::
|
46
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
47
47
|
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
48
48
|
else
|
49
49
|
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
data/lib/basic_tree.rb
CHANGED
@@ -1,25 +1,4 @@
|
|
1
1
|
class BasicTree
|
2
|
-
class Children
|
3
|
-
include Enumerable
|
4
|
-
|
5
|
-
def each(&block)
|
6
|
-
trees.each(&block)
|
7
|
-
end
|
8
|
-
|
9
|
-
def empty?
|
10
|
-
trees.empty?
|
11
|
-
end
|
12
|
-
|
13
|
-
private
|
14
|
-
|
15
|
-
def trees
|
16
|
-
@trees ||= []
|
17
|
-
end
|
18
|
-
|
19
|
-
def <<(tree)
|
20
|
-
trees << tree
|
21
|
-
end
|
22
|
-
end
|
23
2
|
|
24
3
|
include Enumerable
|
25
4
|
|
@@ -28,21 +7,16 @@ class BasicTree
|
|
28
7
|
yield self if block_given?
|
29
8
|
end
|
30
9
|
|
31
|
-
attr_accessor :object
|
32
|
-
attr_reader :parent
|
10
|
+
attr_accessor :object, :parent
|
33
11
|
|
34
12
|
def add(object, &block)
|
35
13
|
self.class.new(object) do |child|
|
36
|
-
children
|
37
|
-
child.
|
14
|
+
children << child
|
15
|
+
child.parent = self
|
38
16
|
yield child if block_given?
|
39
17
|
end
|
40
18
|
end
|
41
19
|
|
42
|
-
def each(&block)
|
43
|
-
children.each(&block)
|
44
|
-
end
|
45
|
-
|
46
20
|
def path
|
47
21
|
ancestors << self
|
48
22
|
end
|
@@ -51,27 +25,20 @@ class BasicTree
|
|
51
25
|
root? ? [] : (parent.ancestors << parent)
|
52
26
|
end
|
53
27
|
|
54
|
-
def descendants
|
55
|
-
|
56
|
-
if depth != 0
|
57
|
-
children.each do |child|
|
58
|
-
trees << child
|
59
|
-
trees += child.descendants(depth - 1)
|
60
|
-
end
|
61
|
-
end
|
62
|
-
trees
|
28
|
+
def descendants
|
29
|
+
children.map { |c| [c] + c.descendants }.flatten
|
63
30
|
end
|
64
31
|
|
65
|
-
def subtree
|
66
|
-
[self] + descendants
|
32
|
+
def subtree
|
33
|
+
[self] + descendants
|
67
34
|
end
|
68
35
|
|
69
36
|
def siblings
|
70
|
-
parent.children.select { |child| child != self }
|
37
|
+
root? ? [] : parent.children.select { |child| child != self }
|
71
38
|
end
|
72
39
|
|
73
40
|
def root
|
74
|
-
|
41
|
+
path.first
|
75
42
|
end
|
76
43
|
|
77
44
|
def level
|
@@ -86,20 +53,8 @@ class BasicTree
|
|
86
53
|
children.empty?
|
87
54
|
end
|
88
55
|
|
89
|
-
def leaves
|
90
|
-
trees = []
|
91
|
-
children.each do |child|
|
92
|
-
child.leaf? ? (trees << child) : (trees += child.leaves)
|
93
|
-
end
|
94
|
-
trees
|
95
|
-
end
|
96
|
-
|
97
|
-
private
|
98
|
-
|
99
|
-
attr_writer :parent
|
100
|
-
|
101
56
|
def children
|
102
|
-
@children ||=
|
57
|
+
@children ||= []
|
103
58
|
end
|
104
59
|
|
105
60
|
end
|
data/spec/basic_tree_spec.rb
CHANGED
@@ -1,5 +1,85 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe BasicTree do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@a = BasicTree.new "a" do |a|
|
7
|
+
@a1 = a.add "a1" do |a1|
|
8
|
+
@a11 = a1.add "a11"
|
9
|
+
@a12 = a1.add "a12"
|
10
|
+
end
|
11
|
+
@a2 = a.add "a2"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
it "path" do
|
16
|
+
@a.path.should == [@a]
|
17
|
+
@a1.path.should == [@a, @a1]
|
18
|
+
@a11.path.should == [@a, @a1, @a11]
|
19
|
+
@a12.path.should == [@a, @a1, @a12]
|
20
|
+
@a2.path.should == [@a, @a2]
|
21
|
+
end
|
22
|
+
|
23
|
+
it "ancestors" do
|
24
|
+
@a.ancestors.should == []
|
25
|
+
@a1.ancestors.should == [@a]
|
26
|
+
@a11.ancestors.should == [@a, @a1]
|
27
|
+
@a12.ancestors.should == [@a, @a1]
|
28
|
+
@a2.ancestors.should == [@a]
|
29
|
+
end
|
30
|
+
|
31
|
+
it "descendants" do
|
32
|
+
@a.descendants.should == [@a1, @a11, @a12, @a2]
|
33
|
+
@a1.descendants.should == [@a11, @a12]
|
34
|
+
@a11.descendants.should == []
|
35
|
+
@a12.descendants.should == []
|
36
|
+
@a2.descendants.should == []
|
37
|
+
end
|
38
|
+
|
39
|
+
it "subtree" do
|
40
|
+
@a.subtree.should == [@a, @a1, @a11, @a12, @a2]
|
41
|
+
@a1.subtree.should == [@a1, @a11, @a12]
|
42
|
+
@a11.subtree.should == [@a11]
|
43
|
+
@a12.subtree.should == [@a12]
|
44
|
+
@a2.subtree.should == [@a2]
|
45
|
+
end
|
46
|
+
|
47
|
+
it "siblings" do
|
48
|
+
@a.siblings.should == []
|
49
|
+
@a1.siblings.should == [@a2]
|
50
|
+
@a11.siblings.should == [@a12]
|
51
|
+
@a12.siblings.should == [@a11]
|
52
|
+
@a2.siblings.should == [@a1]
|
53
|
+
end
|
54
|
+
|
55
|
+
it "root" do
|
56
|
+
[@a, @a1, @a11, @a12, @a2].each do |m|
|
57
|
+
m.root.should == @a
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
it "level" do
|
62
|
+
@a.level.should == 1
|
63
|
+
@a1.level.should == 2
|
64
|
+
@a11.level.should == 3
|
65
|
+
@a12.level.should == 3
|
66
|
+
@a2.level.should == 2
|
67
|
+
end
|
68
|
+
|
69
|
+
it "root?" do
|
70
|
+
@a.root?.should be_true
|
71
|
+
@a1.root?.should be_false
|
72
|
+
@a11.root?.should be_false
|
73
|
+
@a12.root?.should be_false
|
74
|
+
@a2.root?.should be_false
|
75
|
+
end
|
76
|
+
|
77
|
+
it "leaf?" do
|
78
|
+
@a.leaf?.should be_false
|
79
|
+
@a1.leaf?.should be_false
|
80
|
+
@a11.leaf?.should be_true
|
81
|
+
@a12.leaf?.should be_true
|
82
|
+
@a2.leaf?.should be_true
|
83
|
+
end
|
4
84
|
|
5
85
|
end
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: basic_tree
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 0
|
7
|
-
- 0
|
8
8
|
- 1
|
9
|
-
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Austin Schneider
|
@@ -14,16 +15,18 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date: 2010-
|
18
|
+
date: 2010-07-01 00:00:00 -04:00
|
18
19
|
default_executable:
|
19
20
|
dependencies:
|
20
21
|
- !ruby/object:Gem::Dependency
|
21
22
|
name: rspec
|
22
23
|
prerelease: false
|
23
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
24
26
|
requirements:
|
25
27
|
- - ">="
|
26
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 13
|
27
30
|
segments:
|
28
31
|
- 1
|
29
32
|
- 2
|
@@ -62,23 +65,27 @@ rdoc_options:
|
|
62
65
|
require_paths:
|
63
66
|
- lib
|
64
67
|
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
65
69
|
requirements:
|
66
70
|
- - ">="
|
67
71
|
- !ruby/object:Gem::Version
|
72
|
+
hash: 3
|
68
73
|
segments:
|
69
74
|
- 0
|
70
75
|
version: "0"
|
71
76
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
72
78
|
requirements:
|
73
79
|
- - ">="
|
74
80
|
- !ruby/object:Gem::Version
|
81
|
+
hash: 3
|
75
82
|
segments:
|
76
83
|
- 0
|
77
84
|
version: "0"
|
78
85
|
requirements: []
|
79
86
|
|
80
87
|
rubyforge_project:
|
81
|
-
rubygems_version: 1.3.
|
88
|
+
rubygems_version: 1.3.7
|
82
89
|
signing_key:
|
83
90
|
specification_version: 3
|
84
91
|
summary: A basic tree structure
|