hash_graph 0.1.0

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/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Trampoline Systems Ltd
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,25 @@
1
+ = hash_graph
2
+
3
+ HashGraph::DirectedGraph and HashGraph::UndirectedGraph are simple implementations of
4
+ a directed an undirected graph respectively, based on Hash
5
+
6
+ HashGraph::UndirectedGraph ensures that g[a][b]==g[b][a]
7
+
8
+ HashGraph::DirectedGraph ensures that for every edge g[a][b] that there is always a key
9
+ b in the Hash g, so all nodes can be identified by g.keys
10
+
11
+ HashGraph::DirectedGraph also includes TSort for Tarjan's algorithm
12
+
13
+ == Note on Patches/Pull Requests
14
+
15
+ * Fork the project.
16
+ * Make your feature addition or bug fix.
17
+ * Add tests for it. This is important so I don't break it in a
18
+ future version unintentionally.
19
+ * Commit, do not mess with rakefile, version, or history.
20
+ (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)
21
+ * Send me a pull request. Bonus points for topic branches.
22
+
23
+ == Copyright
24
+
25
+ Copyright (c) 2010 Trampoline Systems Ltd. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "hash_graph"
8
+ gem.summary = %Q{simple Hash based directed and undirected graph implementations for Ruby}
9
+ gem.description = %Q{Hash based directed and undirected graph implementations for Ruby}
10
+ gem.email = "craig@trampolinesystems.com"
11
+ gem.homepage = "http://github.com/trampoline/hash_graph"
12
+ gem.authors = ["mccraig mccraig of the clan mccraig"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.add_development_dependency "yard", ">= 0"
15
+ gem.add_development_dependency "rr", ">= 0.10.5"
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
+ end
22
+
23
+ require 'spec/rake/spectask'
24
+ Spec::Rake::SpecTask.new(:spec) do |spec|
25
+ spec.libs << 'lib' << 'spec'
26
+ spec.spec_files = FileList['spec/**/*_spec.rb']
27
+ end
28
+
29
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
30
+ spec.libs << 'lib' << 'spec'
31
+ spec.pattern = 'spec/**/*_spec.rb'
32
+ spec.rcov = true
33
+ end
34
+
35
+ task :spec => :check_dependencies
36
+
37
+ task :default => :spec
38
+
39
+ begin
40
+ require 'yard'
41
+ YARD::Rake::YardocTask.new
42
+ rescue LoadError
43
+ task :yardoc do
44
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
45
+ end
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/hash_graph.rb ADDED
@@ -0,0 +1,73 @@
1
+ require 'tsort'
2
+
3
+ module HashGraph
4
+ # a simple directed graph representation for TSort,
5
+ # [ TSort implements tarjan's algorithm for finding the
6
+ # strongly connected components of a graph ]
7
+ # DirectedGraph extends Hash, all nodes have a key in the top-level
8
+ # Hash, and a possibly empty child-hash of node to weight
9
+ # mappings, thus the structure is :
10
+ # {from_v*=>{to_v*=>weight}} and all keys used in the child hashes
11
+ # are guaranteed to also be present in the top-level hash
12
+ class DirectedGraph < Hash
13
+ include TSort
14
+
15
+ def self.new_lower_hash(top_hash,from_vertex)
16
+ tos = Hash.new()
17
+ tos.instance_eval do
18
+ ec = class << self ; self ; end
19
+ ec.send(:alias_method, :uhg_store, :store)
20
+ ec.send(:define_method, :store) do |to_vertex,weight|
21
+ top_hash[from_vertex]=tos if !top_hash.key?(from_vertex)
22
+ uhg_store(to_vertex,weight)
23
+ top_hash[to_vertex]=DirectedGraph::new_lower_hash(top_hash,to_vertex) if !top_hash.key?(to_vertex)
24
+ end
25
+ ec.send(:alias_method, :[]=, :store)
26
+ end
27
+ tos
28
+ end
29
+
30
+ # construct a DirectedGraph which ensures that all destination node keys
31
+ # are present in the top level Hash
32
+ def initialize()
33
+ super() do |h,from_vertex|
34
+ DirectedGraph::new_lower_hash(h,from_vertex)
35
+ end
36
+ end
37
+
38
+ alias tsort_each_node each_key
39
+
40
+ def tsort_each_child(node,&block)
41
+ fetch(node).keys.each(&block)
42
+ end
43
+ end
44
+
45
+ # an undirected graph representation based on Hash,
46
+ # which ensures that h[a][b]==h[b][a]
47
+ class UndirectedGraph < Hash
48
+ def self.new_lower_hash(top_hash,a_vertex)
49
+ tos = Hash.new()
50
+ tos.instance_eval do
51
+ ec = class << self ; self ; end
52
+ ec.send(:alias_method, :uhg_store, :store)
53
+ ec.send(:define_method, :store) do |b_vertex,weight|
54
+ top_hash[a_vertex]=tos if !top_hash.key?(a_vertex)
55
+ uhg_store(b_vertex,weight)
56
+ if top_hash.key?(b_vertex)
57
+ top_hash[b_vertex]
58
+ else
59
+ top_hash[b_vertex]=UndirectedGraph::new_lower_hash(top_hash,b_vertex)
60
+ end.uhg_store(a_vertex,weight) # ensure reverse link added too
61
+ end
62
+ ec.send(:alias_method, :[]=, :store)
63
+ end
64
+ tos
65
+ end
66
+
67
+ def initialize()
68
+ super() do |h,a_vertex|
69
+ UndirectedGraph::new_lower_hash(h,a_vertex)
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,53 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe HashGraph do
4
+ module HashGraph
5
+ describe DirectedGraph do
6
+ it "should not create a top-level key on top-level access" do
7
+ h = DirectedGraph.new
8
+ h[:foo]
9
+ h.should == {}
10
+ end
11
+
12
+ it "should not create a top-level key on second-level access" do
13
+ h = DirectedGraph.new
14
+ h[:foo][:bar]
15
+ h.should == {}
16
+ end
17
+
18
+ it "should create a top-level key for new target nodes" do
19
+ h = DirectedGraph.new
20
+ h[:foo][:bar] = 1
21
+ h.should == {:foo=>{:bar=>1}, :bar=>{}}
22
+ end
23
+
24
+ it "should create new edges on existing top-level keys" do
25
+ h = DirectedGraph.new
26
+ h[:foo][:bar] = 1
27
+ h[:bar][:baz] = 5
28
+ h.should == {:foo=>{:bar=>1}, :bar=>{:baz=>5}, :baz=>{}}
29
+ end
30
+ end
31
+
32
+ describe UndirectedGraph do
33
+ it "should not create a top-level key on top-level access" do
34
+ h = UndirectedGraph.new
35
+ h[:foo]
36
+ h.should == {}
37
+ end
38
+
39
+ it "should not create a top-leve key on second level acces" do
40
+ h = UndirectedGraph.new
41
+ h[:foo][:bar]
42
+ h.should == {}
43
+ end
44
+
45
+ it "should create the reverse edge on edge creation" do
46
+ h = UndirectedGraph.new
47
+ h[:foo][:bar]=1
48
+ h.should == {:foo=>{:bar=>1}, :bar=>{:foo=>1}}
49
+ end
50
+ end
51
+
52
+ end
53
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'hash_graph'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+
7
+ Spec::Runner.configure do |config|
8
+ config.mock_with :rr
9
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash_graph
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - mccraig mccraig of the clan mccraig
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-19 00:00:00 +00:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.9
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: yard
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: rr
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 0.10.5
44
+ version:
45
+ description: Hash based directed and undirected graph implementations for Ruby
46
+ email: craig@trampolinesystems.com
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - LICENSE
53
+ - README.rdoc
54
+ files:
55
+ - .document
56
+ - .gitignore
57
+ - LICENSE
58
+ - README.rdoc
59
+ - Rakefile
60
+ - VERSION
61
+ - lib/hash_graph.rb
62
+ - spec/hash_graph_spec.rb
63
+ - spec/spec.opts
64
+ - spec/spec_helper.rb
65
+ has_rdoc: true
66
+ homepage: http://github.com/trampoline/hash_graph
67
+ licenses: []
68
+
69
+ post_install_message:
70
+ rdoc_options:
71
+ - --charset=UTF-8
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ version:
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: "0"
85
+ version:
86
+ requirements: []
87
+
88
+ rubyforge_project:
89
+ rubygems_version: 1.3.5
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: simple Hash based directed and undirected graph implementations for Ruby
93
+ test_files:
94
+ - spec/hash_graph_spec.rb
95
+ - spec/spec_helper.rb