dc-util 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Dorren Chen
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.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = dc-util
2
+
3
+ A library for data structures. Currently only has Node and BinaryNode class.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Dorren Chen. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "dc-util"
8
+ gem.summary = %Q{An utility gem that contains useful data structures.}
9
+ gem.description = %Q{An utility gem that contains useful data structures, like tree node, etc.}
10
+ gem.email = "dorrenchen@gmail.com"
11
+ gem.homepage = "http://github.com/dorren/dc-util"
12
+ gem.authors = ["Dorren Chen"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+
22
+ require 'rake/rdoctask'
23
+ Rake::RDocTask.new do |rdoc|
24
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
25
+
26
+ rdoc.rdoc_dir = 'rdoc'
27
+ rdoc.title = "dc-util #{version}"
28
+ rdoc.rdoc_files.include('README*')
29
+ rdoc.rdoc_files.include('lib/**/*.rb')
30
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.2
data/dc-util.gemspec ADDED
@@ -0,0 +1,58 @@
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 = %q{dc-util}
8
+ s.version = "0.0.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Dorren Chen"]
12
+ s.date = %q{2011-02-01}
13
+ s.description = %q{An utility gem that contains useful data structures, like tree node, etc.}
14
+ s.email = %q{dorrenchen@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "dc-util.gemspec",
26
+ "lib/dc-util.rb",
27
+ "lib/dc_util.rb",
28
+ "lib/dc_util/binary_node.rb",
29
+ "lib/dc_util/node.rb",
30
+ "spec/binary_node_spec.rb",
31
+ "spec/node_spec.rb",
32
+ "spec/spec.opts",
33
+ "spec/spec_helper.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/dorren/dc-util}
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.7}
38
+ s.summary = %q{An utility gem that contains useful data structures.}
39
+ s.test_files = [
40
+ "spec/binary_node_spec.rb",
41
+ "spec/node_spec.rb",
42
+ "spec/spec_helper.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
50
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
51
+ else
52
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
53
+ end
54
+ else
55
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
56
+ end
57
+ end
58
+
data/lib/dc-util.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/dc_util'
data/lib/dc_util.rb ADDED
@@ -0,0 +1,2 @@
1
+ require File.dirname(__FILE__) + '/dc_util/node'
2
+ require File.dirname(__FILE__) + '/dc_util/binary_node'
@@ -0,0 +1,27 @@
1
+ module DcUtil
2
+ class BinaryNode < Node
3
+ # returns left child
4
+ def left
5
+ children[0]
6
+ end
7
+
8
+ # returns right child
9
+ def right
10
+ children[1]
11
+ end
12
+
13
+ # set left child
14
+ def left=(node)
15
+ raise ArgumentError.new("not a valid node class") unless node.kind_of? self.class
16
+ children[0] = node
17
+ node.parent = self
18
+ end
19
+
20
+ # set right child
21
+ def right=(node)
22
+ raise ArgumentError.new("not a valid node class") unless node.kind_of? self.class
23
+ children[1] = node
24
+ node.parent = self
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,86 @@
1
+ module DcUtil
2
+ # a data structure used in tree-like operations.
3
+ class Node
4
+ attr_accessor :children, :data, :parent
5
+
6
+ def initialize(options={})
7
+ self.data = options[:data]
8
+ self.children = []
9
+ end
10
+
11
+ class << self
12
+ # breath first search
13
+ def bfs(node, queue, exclude_root, &block)
14
+ return if node.nil?
15
+ block.call(node) unless exclude_root
16
+
17
+ node.children.each{|x| queue.push(x)}
18
+ bfs(queue.shift, queue, false, &block)
19
+ end
20
+
21
+ # breath first search, non recursive style to prevent stack overflow
22
+ def bfs_non_recursive(node, queue, exclude_root, &block)
23
+ return if node.nil?
24
+ queue.push(node)
25
+
26
+ while not queue.empty?
27
+ node = queue.shift
28
+ block.call(node) unless exclude_root
29
+ node.children.each{|x| queue.push(x)}
30
+ exclude_root = false
31
+ end
32
+ end
33
+
34
+ # depth first search
35
+ def dfs(node, exclude_root, &block)
36
+ block.call(node) unless exclude_root
37
+ node.children.each{|x| dfs(x, false, &block)}
38
+ end
39
+ end
40
+
41
+ # add a child node.
42
+ def add(node)
43
+ raise ArgumentError.new("not a valid node class") unless node.kind_of? self.class
44
+ children << node
45
+ node.parent = self
46
+ end
47
+
48
+ def parents
49
+ parent ? [parent] + parent.parents : []
50
+ end
51
+
52
+ # breath first search
53
+ # root.bfs{|node| puts node.inspect}
54
+ #
55
+ # root.bfs(true){|node| puts node.inspect} # skip first node
56
+ def bfs(exclude_root=false, &block)
57
+ # self.class.bfs(self, [], exclude_root, &block)
58
+ self.class.bfs_non_recursive(self, [], exclude_root, &block)
59
+ end
60
+
61
+ # depth first search
62
+ # root.dfs{|node| puts node.inspect}
63
+ #
64
+ # root.dfs(true){|node| puts node.inspect} # skip first node
65
+ def dfs(exclude_root=false, &block)
66
+ self.class.dfs(self, exclude_root, &block)
67
+ end
68
+
69
+ # return all nodes that has no children
70
+ def leaves
71
+ arr = []
72
+ self.dfs do |node|
73
+ arr << node if node.children.empty?
74
+ end
75
+ arr
76
+ end
77
+
78
+ # return all nodes count
79
+ def tree_size
80
+ n = 0
81
+ dfs{|x| n += 1}
82
+ n
83
+ end
84
+ end
85
+ end
86
+
@@ -0,0 +1,33 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ include DcUtil
3
+
4
+ describe DcUtil::BinaryNode do
5
+ it "should initialize" do
6
+ node = BinaryNode.new
7
+ node.left.should be_nil
8
+ end
9
+
10
+ it "should add" do
11
+ node = BinaryNode.new
12
+ child = BinaryNode.new
13
+
14
+ node.left = child
15
+ node.left.should == child
16
+
17
+ lambda {node.left = "hello"}.should raise_error
18
+ end
19
+
20
+ def to_tree(node)
21
+ node.dfs{|x|
22
+ indent = " " * x.parents.size
23
+ puts "#{indent}#{x.object_id}"
24
+ }
25
+ end
26
+
27
+ it "should do root leaves" do
28
+ root = BinaryNode.new
29
+ root.left = node1 = BinaryNode.new
30
+ root.right = node2 = BinaryNode.new
31
+ root.leaves.should == [node1, node2]
32
+ end
33
+ end
data/spec/node_spec.rb ADDED
@@ -0,0 +1,130 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ include DcUtil
3
+
4
+ describe DcUtil::Node do
5
+ describe "#new" do
6
+ it "should initialize" do
7
+ node = Node.new(:data => 1)
8
+ node.data.should == 1
9
+ end
10
+ end
11
+
12
+ describe "#add" do
13
+ it "should only add node class object as children" do
14
+ node = Node.new(1)
15
+ lambda{
16
+ node.add("something")
17
+ }.should raise_error(ArgumentError)
18
+ end
19
+
20
+ it "should work with subclass node as well" do
21
+ class CoolNode < DcUtil::Node; end
22
+ class SuperCoolNode < CoolNode; end
23
+
24
+ lambda{ # only same or subclass nodes are allowed
25
+ CoolNode.new(:data => 1).add(Node.new(:data => 2))
26
+ }.should raise_error(ArgumentError)
27
+
28
+ lambda{
29
+ CoolNode.new(:data => 1).add(CoolNode.new(:data => 2))
30
+ }.should_not raise_error
31
+
32
+ lambda{
33
+ CoolNode.new(:data => 1).add(SuperCoolNode.new(:data => 2))
34
+ }.should_not raise_error
35
+ end
36
+
37
+
38
+ it "should add children" do
39
+ node = Node.new(:data => 1)
40
+ n2 = Node.new(:data => 2)
41
+ node.add(n2)
42
+ node.children == [n2]
43
+ n2.parent.should == node
44
+ end
45
+ end
46
+
47
+ describe "#parent" do
48
+ it "should have parent" do
49
+ node = Node.new(:data => 1)
50
+ n2 = Node.new(:data => 2)
51
+ node.add(n2)
52
+ n2.parent.should == node
53
+ end
54
+
55
+ it "should have parents" do
56
+ node = Node.new(:data => 1)
57
+ n2 = Node.new(:data => 2)
58
+ n3 = Node.new(:data => 3)
59
+ node.add(n2)
60
+ n2.add(n3)
61
+ node.parents.should == []
62
+ n2.parents.should == [node]
63
+ n3.parents.should == [n2, node]
64
+ end
65
+ end
66
+
67
+ describe "traversal" do
68
+ before(:each) do
69
+ init_tree
70
+ end
71
+
72
+ def init_tree
73
+ # 1
74
+ # / \
75
+ # 2 3
76
+ # / \ / \
77
+ # 4 5 6 7
78
+ # /\ /
79
+ # 8 9 10
80
+ @tree = Node.new(:data => 1)
81
+ n2 = Node.new(:data => 2)
82
+ n3 = Node.new(:data => 3)
83
+ n4 = Node.new(:data => 4)
84
+ n5 = Node.new(:data => 5)
85
+ n6 = Node.new(:data => 6)
86
+ n7 = Node.new(:data => 7)
87
+ n8 = Node.new(:data => 8)
88
+ n9 = Node.new(:data => 9)
89
+ n10 = Node.new(:data => 10)
90
+
91
+ @tree.add n2
92
+ @tree.add n3
93
+ n2.add n4
94
+ n2.add n5
95
+ n4.add n8
96
+ n4.add n9
97
+ n5.add n10
98
+ n3.add n6
99
+ n3.add n7
100
+ end
101
+
102
+ it "should do bfs" do
103
+ arr = []
104
+ @tree.bfs{|node| arr << node.data}
105
+ arr.should == [1,2,3,4,5,6,7,8,9,10]
106
+ end
107
+
108
+ it "should do bfs exclude root" do
109
+ arr = []
110
+ @tree.bfs(true){|node| arr << node.data}
111
+ arr.should == [2,3,4,5,6,7,8,9,10]
112
+ end
113
+
114
+ it "should do dfs" do
115
+ arr = []
116
+ @tree.dfs{|node| arr << node.data}
117
+ arr.should == [1,2,4,8,9,5,10,3,6,7]
118
+ end
119
+
120
+ it "should do dfs exclude root" do
121
+ arr = []
122
+ @tree.dfs(true){|node| arr << node.data}
123
+ arr.should == [2,4,8,9,5,10,3,6,7]
124
+ end
125
+
126
+ it "should count tree size" do
127
+ @tree.tree_size.should == 10
128
+ end
129
+ end
130
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,8 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rspec'
4
+ require 'dc-util'
5
+
6
+ RSpec.configure do |config|
7
+
8
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dc-util
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Dorren Chen
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-01 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ description: An utility gem that contains useful data structures, like tree node, etc.
38
+ email: dorrenchen@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - LICENSE
45
+ - README.rdoc
46
+ files:
47
+ - .document
48
+ - LICENSE
49
+ - README.rdoc
50
+ - Rakefile
51
+ - VERSION
52
+ - dc-util.gemspec
53
+ - lib/dc-util.rb
54
+ - lib/dc_util.rb
55
+ - lib/dc_util/binary_node.rb
56
+ - lib/dc_util/node.rb
57
+ - spec/binary_node_spec.rb
58
+ - spec/node_spec.rb
59
+ - spec/spec.opts
60
+ - spec/spec_helper.rb
61
+ has_rdoc: true
62
+ homepage: http://github.com/dorren/dc-util
63
+ licenses: []
64
+
65
+ post_install_message:
66
+ rdoc_options: []
67
+
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ requirements: []
89
+
90
+ rubyforge_project:
91
+ rubygems_version: 1.3.7
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: An utility gem that contains useful data structures.
95
+ test_files:
96
+ - spec/binary_node_spec.rb
97
+ - spec/node_spec.rb
98
+ - spec/spec_helper.rb