algorithms 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +23 -0
- data/Manifest +23 -0
- data/README.txt +58 -0
- data/Rakefile +17 -0
- data/algorithms.gemspec +105 -0
- data/benchmark.rb +49 -0
- data/ext/containers/priority_queue/extconf.rb +4 -0
- data/ext/containers/priority_queue/priority_queue.c +948 -0
- data/ext/containers/tree_map/Rakefile +4 -0
- data/ext/containers/tree_map/extconf.rb +4 -0
- data/ext/containers/tree_map/rbtree.c +395 -0
- data/lib/algorithms.rb +7 -0
- data/lib/containers/hash.rb +0 -0
- data/lib/containers/heap.rb +146 -0
- data/lib/containers/priority_queue.rb +514 -0
- data/lib/containers/queue.rb +29 -0
- data/lib/containers/stack.rb +29 -0
- data/lib/containers/tree_map.rb +265 -0
- data/spec/heap_spec.rb +74 -0
- data/spec/priority_queue_spec.rb +31 -0
- data/spec/priority_queue_test.rb +371 -0
- data/spec/queue_spec.rb +53 -0
- data/spec/stack_spec.rb +53 -0
- data/spec/tree_map_spec.rb +99 -0
- metadata +104 -0
data/spec/queue_spec.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'lib/algorithms'
|
2
|
+
|
3
|
+
describe Containers::Queue do
|
4
|
+
before(:each) do
|
5
|
+
@queue = Containers::Queue.new
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "(empty)" do
|
9
|
+
it "should return nil when sent #get" do
|
10
|
+
@queue.get.should eql(nil)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should return a size of 1 when sent #put" do
|
14
|
+
@queue.put(1).size.should eql(1)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should return nil when peeked" do
|
18
|
+
@queue.peek.should eql(nil)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should return empty?" do
|
22
|
+
@queue.empty?.should eql(true)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "(non-empty)" do
|
27
|
+
before(:each) do
|
28
|
+
@queue.put(10)
|
29
|
+
@queue.put("10")
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should return first put object" do
|
33
|
+
@queue.get.should eql(10)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should return the size" do
|
37
|
+
@queue.size.should eql(2)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should not return empty?" do
|
41
|
+
@queue.empty?.should eql(false)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should return nil after all gets" do
|
45
|
+
@queue.get
|
46
|
+
@queue.get
|
47
|
+
@queue.get.should eql(nil)
|
48
|
+
@queue.peek.should eql(nil)
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
data/spec/stack_spec.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'lib/algorithms'
|
2
|
+
|
3
|
+
describe Containers::Stack do
|
4
|
+
before(:each) do
|
5
|
+
@stack = Containers::Stack.new
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "(empty)" do
|
9
|
+
it "should return nil when sent #pop" do
|
10
|
+
@stack.pop.should eql(nil)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should return a size of 1 when sent #push" do
|
14
|
+
@stack.push(1).size.should eql(1)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should return nil when peeked" do
|
18
|
+
@stack.peek.should eql(nil)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should return empty?" do
|
22
|
+
@stack.empty?.should eql(true)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "(non-empty)" do
|
27
|
+
before(:each) do
|
28
|
+
@stack.push(10)
|
29
|
+
@stack.push("10")
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should return last pushed object" do
|
33
|
+
@stack.pop.should eql("10")
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should return the size" do
|
37
|
+
@stack.size.should eql(2)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should not return empty?" do
|
41
|
+
@stack.empty?.should eql(false)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should return nil after all pops" do
|
45
|
+
@stack.pop
|
46
|
+
@stack.pop
|
47
|
+
@stack.pop.should eql(nil)
|
48
|
+
@stack.peek.should eql(nil)
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require 'lib/algorithms'
|
2
|
+
|
3
|
+
describe "(empty)", :shared => true do
|
4
|
+
it "should let you put stuff in" do
|
5
|
+
100.times { |x| @tree[x] = x }
|
6
|
+
@tree.size.should eql(100)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should return 0 for height" do
|
10
|
+
@tree.height.should eql(0)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should return 0 for size" do
|
14
|
+
@tree.size.should eql(0)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should return nil for #min_key and #max_key" do
|
18
|
+
@tree.min_key.should eql(nil)
|
19
|
+
@tree.max_key.should eql(nil)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should not delete" do
|
23
|
+
@tree.delete(:non_existing).should eql(nil)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "(non-empty)", :shared => true do
|
28
|
+
before(:each) do
|
29
|
+
@num_items = 100
|
30
|
+
@random_array = []
|
31
|
+
@num_items.times { @random_array << rand(@num_items) }
|
32
|
+
@random_array.each { |x| @tree[x] = x }
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should return correct size (uniqify items first)" do
|
36
|
+
@tree.size.should eql(@random_array.uniq.size)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should return correct max and min keys" do
|
40
|
+
@tree.min_key.should eql(@random_array.min)
|
41
|
+
@tree.max_key.should eql(@random_array.max)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should not #contain? keys it doesn't have" do
|
45
|
+
@tree.contains?(10000).should eql(false)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should #contain? keys it does have" do
|
49
|
+
@tree.contains?(@random_array[0]).should eql(true)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should remove any key" do
|
53
|
+
random_key = @random_array[rand(@num_items)]
|
54
|
+
@tree.contains?(random_key).should eql(true)
|
55
|
+
@tree.delete(random_key).should eql(random_key)
|
56
|
+
@tree.contains?(random_key).should eql(false)
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should let you iterate with #each" do
|
60
|
+
counter = 0
|
61
|
+
sorted_array = @random_array.uniq.sort
|
62
|
+
@tree.each do |key, val|
|
63
|
+
key.should eql(sorted_array[counter])
|
64
|
+
counter += 1
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe Containers::CTreeMap do
|
70
|
+
describe "empty" do
|
71
|
+
before(:each) do
|
72
|
+
@tree = Containers::CTreeMap.new
|
73
|
+
end
|
74
|
+
it_should_behave_like "(empty)"
|
75
|
+
end
|
76
|
+
|
77
|
+
describe "full" do
|
78
|
+
before(:each) do
|
79
|
+
@tree = Containers::CTreeMap.new
|
80
|
+
end
|
81
|
+
it_should_behave_like "(non-empty)"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe Containers::RubyTreeMap do
|
86
|
+
describe "empty" do
|
87
|
+
before(:each) do
|
88
|
+
@tree = Containers::RubyTreeMap.new
|
89
|
+
end
|
90
|
+
it_should_behave_like "(empty)"
|
91
|
+
end
|
92
|
+
|
93
|
+
describe "full" do
|
94
|
+
before(:each) do
|
95
|
+
@tree = Containers::RubyTreeMap.new
|
96
|
+
end
|
97
|
+
it_should_behave_like "(non-empty)"
|
98
|
+
end
|
99
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: algorithms
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kanwei Li
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-06-25 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: echoe
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
description: A library of algorithms and containers.
|
26
|
+
email: kanwei@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions:
|
30
|
+
- ext/containers/priority_queue/extconf.rb
|
31
|
+
- ext/containers/tree_map/extconf.rb
|
32
|
+
extra_rdoc_files:
|
33
|
+
- ext/containers/priority_queue/extconf.rb
|
34
|
+
- ext/containers/priority_queue/priority_queue.c
|
35
|
+
- ext/containers/tree_map/extconf.rb
|
36
|
+
- ext/containers/tree_map/Rakefile
|
37
|
+
- ext/containers/tree_map/rbtree.c
|
38
|
+
- lib/algorithms.rb
|
39
|
+
- lib/containers/hash.rb
|
40
|
+
- lib/containers/heap.rb
|
41
|
+
- lib/containers/priority_queue.rb
|
42
|
+
- lib/containers/queue.rb
|
43
|
+
- lib/containers/stack.rb
|
44
|
+
- lib/containers/tree_map.rb
|
45
|
+
- README.txt
|
46
|
+
files:
|
47
|
+
- benchmark.rb
|
48
|
+
- ext/containers/priority_queue/extconf.rb
|
49
|
+
- ext/containers/priority_queue/priority_queue.c
|
50
|
+
- ext/containers/tree_map/extconf.rb
|
51
|
+
- ext/containers/tree_map/Rakefile
|
52
|
+
- ext/containers/tree_map/rbtree.c
|
53
|
+
- History.txt
|
54
|
+
- lib/algorithms.rb
|
55
|
+
- lib/containers/hash.rb
|
56
|
+
- lib/containers/heap.rb
|
57
|
+
- lib/containers/priority_queue.rb
|
58
|
+
- lib/containers/queue.rb
|
59
|
+
- lib/containers/stack.rb
|
60
|
+
- lib/containers/tree_map.rb
|
61
|
+
- Manifest
|
62
|
+
- Rakefile
|
63
|
+
- README.txt
|
64
|
+
- spec/heap_spec.rb
|
65
|
+
- spec/priority_queue_spec.rb
|
66
|
+
- spec/priority_queue_test.rb
|
67
|
+
- spec/queue_spec.rb
|
68
|
+
- spec/stack_spec.rb
|
69
|
+
- spec/tree_map_spec.rb
|
70
|
+
- algorithms.gemspec
|
71
|
+
has_rdoc: true
|
72
|
+
homepage: http://rubyforge.org/projects/algorithms/
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options:
|
75
|
+
- --line-numbers
|
76
|
+
- --inline-source
|
77
|
+
- --title
|
78
|
+
- Algorithms
|
79
|
+
- --main
|
80
|
+
- README.txt
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
- ext
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: "0"
|
89
|
+
version:
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - "="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: "1.2"
|
95
|
+
version:
|
96
|
+
requirements: []
|
97
|
+
|
98
|
+
rubyforge_project: algorithms
|
99
|
+
rubygems_version: 1.2.0
|
100
|
+
signing_key:
|
101
|
+
specification_version: 2
|
102
|
+
summary: A library of algorithms and containers.
|
103
|
+
test_files: []
|
104
|
+
|