hamster 0.1.2 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/History.rdoc +13 -1
- data/README.rdoc +72 -25
- data/TODO +1 -0
- data/lib/hamster.rb +4 -1
- data/lib/hamster/hash.rb +66 -0
- data/lib/hamster/list.rb +105 -0
- data/lib/hamster/set.rb +65 -0
- data/lib/hamster/stack.rb +53 -0
- data/lib/hamster/trie.rb +45 -28
- data/lib/hamster/version.rb +1 -1
- data/spec/hamster/hash/copying_spec.rb +25 -0
- data/spec/hamster/hash/each_spec.rb +47 -0
- data/spec/hamster/hash/empty_spec.rb +17 -0
- data/spec/hamster/hash/eql_spec.rb +20 -0
- data/spec/hamster/hash/get_spec.rb +22 -0
- data/spec/hamster/hash/has_key_spec.rb +21 -0
- data/spec/hamster/hash/put_spec.rb +93 -0
- data/spec/hamster/hash/remove_spec.rb +113 -0
- data/spec/hamster/list/accessor_spec.rb +26 -0
- data/spec/hamster/list/car_spec.rb +17 -0
- data/spec/hamster/list/copying_spec.rb +25 -0
- data/spec/hamster/list/each_spec.rb +45 -0
- data/spec/hamster/list/empty_spec.rb +18 -0
- data/spec/hamster/list/eql_spec.rb +22 -0
- data/spec/hamster/list/map_spec.rb +43 -0
- data/spec/hamster/list/reduce_spec.rb +31 -0
- data/spec/hamster/stack/copying_spec.rb +25 -0
- data/spec/hamster/stack/empty_spec.rb +17 -0
- data/spec/hamster/stack/eql_spec.rb +20 -0
- data/spec/hamster/stack/push_spec.rb +42 -0
- metadata +28 -12
- data/lib/hamster/entry.rb +0 -18
- data/spec/hamster/trie/each_spec.rb +0 -51
- data/spec/hamster/trie/empty_spec.rb +0 -22
- data/spec/hamster/trie/enumerable_spec.rb +0 -13
- data/spec/hamster/trie/get_spec.rb +0 -26
- data/spec/hamster/trie/has_key_spec.rb +0 -25
- data/spec/hamster/trie/put_spec.rb +0 -97
- data/spec/hamster/trie/remove_spec.rb +0 -115
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Hamster::List do
|
4
|
+
|
5
|
+
describe "#each" do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@expected_values = (0..100).to_a
|
9
|
+
@list = Hamster::List.new
|
10
|
+
@expected_values.reverse.each { |value| @list = @list.cons(value) }
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "with a block (internal iteration)" do
|
14
|
+
|
15
|
+
it "returns self" do
|
16
|
+
@list.each {}.should equal(@list)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "yields all key value pairs" do
|
20
|
+
actual_values = []
|
21
|
+
@list.each do |value|
|
22
|
+
actual_values << value
|
23
|
+
end
|
24
|
+
actual_values.should == @expected_values
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "with no block (external iteration)" do
|
30
|
+
|
31
|
+
it "returns an enumerator over all key value pairs" do
|
32
|
+
actual_values = []
|
33
|
+
enum = @list.each
|
34
|
+
loop do
|
35
|
+
value = enum.next
|
36
|
+
actual_values << value
|
37
|
+
end
|
38
|
+
actual_values.should == @expected_values
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Hamster::List do
|
4
|
+
|
5
|
+
describe "#empty?" do
|
6
|
+
|
7
|
+
it "initially returns true" do
|
8
|
+
Hamster::List.new.should be_empty
|
9
|
+
end
|
10
|
+
|
11
|
+
it "returns false once items have been added" do
|
12
|
+
list = Hamster::List.new.cons("A")
|
13
|
+
list.should_not be_empty
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Hamster::List do
|
4
|
+
|
5
|
+
describe "#eql?" do
|
6
|
+
|
7
|
+
it "is true for the same instance" do
|
8
|
+
list = Hamster::List.new
|
9
|
+
pending do
|
10
|
+
list.should eql(list)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
it "is true for two empty instances" do
|
15
|
+
pending do
|
16
|
+
Hamster::List.new.should eql(Hamster::List.new)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Hamster::List do
|
4
|
+
|
5
|
+
describe "#map" do
|
6
|
+
|
7
|
+
it "initially returns self" do
|
8
|
+
list = Hamster::List.new
|
9
|
+
list.map {}.should equal(list)
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "when not empty" do
|
13
|
+
|
14
|
+
before do
|
15
|
+
@original = Hamster::List.new.cons(1).cons(2).cons(3).cons(4)
|
16
|
+
@copy = @original.map { |i| i + 5 }
|
17
|
+
end
|
18
|
+
|
19
|
+
it "returns a modified copy" do
|
20
|
+
@copy.should_not equal(@original)
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "the original" do
|
24
|
+
|
25
|
+
it "has the original values" do
|
26
|
+
@original.to_enum.to_a.should == [4, 3, 2, 1]
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "the modified copy" do
|
32
|
+
|
33
|
+
it "has the mapped values" do
|
34
|
+
@copy.to_enum.to_a.should == [9, 8, 7, 6]
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Hamster::List do
|
4
|
+
|
5
|
+
describe "#reduce" do
|
6
|
+
|
7
|
+
it "initially returns memo" do
|
8
|
+
Hamster::List.new.reduce(0).should == 0
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "with values" do
|
12
|
+
|
13
|
+
before do
|
14
|
+
@list = Hamster::List.new.cons(1).cons(2).cons(3).cons(4)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "works with an initial value" do
|
18
|
+
@list.reduce(0) { |memo, i| memo + i }.should == 10
|
19
|
+
end
|
20
|
+
|
21
|
+
it "defaults to the first item in the list" do
|
22
|
+
pending do
|
23
|
+
@list.reduce { |memo, i| memo + i }.should == 10
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Hamster::Stack do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@stack = Hamster::Stack.new
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "#dup" do
|
10
|
+
|
11
|
+
it "returns self" do
|
12
|
+
@stack.dup.should equal(@stack)
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "#clone" do
|
18
|
+
|
19
|
+
it "returns self" do
|
20
|
+
@stack.clone.should equal(@stack)
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Hamster::Stack do
|
4
|
+
|
5
|
+
describe "#empty?" do
|
6
|
+
|
7
|
+
it "initially returns true" do
|
8
|
+
Hamster::Stack.new.should be_empty
|
9
|
+
end
|
10
|
+
|
11
|
+
it "returns false once items have been added" do
|
12
|
+
Hamster::Stack.new.push("A").should_not be_empty
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Hamster::Stack do
|
4
|
+
|
5
|
+
describe "#eql?" do
|
6
|
+
|
7
|
+
it "is true for the same instance" do
|
8
|
+
stack = Hamster::Stack.new
|
9
|
+
stack.should eql(stack)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "is true for two empty instances" do
|
13
|
+
pending do
|
14
|
+
Hamster::Stack.new.should eql(Hamster::Stack.new)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe Hamster::Stack do
|
4
|
+
|
5
|
+
describe "#push" do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@original = Hamster::Stack.new.push("A")
|
9
|
+
@copy = @original.push("B")
|
10
|
+
end
|
11
|
+
|
12
|
+
it "returns a modified copy" do
|
13
|
+
@copy.should_not equal(@original)
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "the original" do
|
17
|
+
|
18
|
+
it "still has the original top" do
|
19
|
+
@original.top.should == "A"
|
20
|
+
end
|
21
|
+
|
22
|
+
it "still has the original size" do
|
23
|
+
@original.size.should == 1
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "the modified copy" do
|
29
|
+
|
30
|
+
it "has a new top" do
|
31
|
+
@copy.top.should == "B"
|
32
|
+
end
|
33
|
+
|
34
|
+
it "size is increased by one" do
|
35
|
+
@copy.size == 2
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hamster
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Simon Harris
|
@@ -9,11 +9,11 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-11-07 00:00:00 +11:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
16
|
-
description:
|
16
|
+
description: Persistent Data Structures for Ruby
|
17
17
|
email: haruki.zaemon@gmail.com
|
18
18
|
executables: []
|
19
19
|
|
@@ -25,17 +25,33 @@ extra_rdoc_files:
|
|
25
25
|
- TODO
|
26
26
|
- LICENSE
|
27
27
|
files:
|
28
|
-
- lib/hamster/
|
28
|
+
- lib/hamster/hash.rb
|
29
|
+
- lib/hamster/list.rb
|
30
|
+
- lib/hamster/set.rb
|
31
|
+
- lib/hamster/stack.rb
|
29
32
|
- lib/hamster/trie.rb
|
30
33
|
- lib/hamster/version.rb
|
31
34
|
- lib/hamster.rb
|
32
|
-
- spec/hamster/
|
33
|
-
- spec/hamster/
|
34
|
-
- spec/hamster/
|
35
|
-
- spec/hamster/
|
36
|
-
- spec/hamster/
|
37
|
-
- spec/hamster/
|
38
|
-
- spec/hamster/
|
35
|
+
- spec/hamster/hash/copying_spec.rb
|
36
|
+
- spec/hamster/hash/each_spec.rb
|
37
|
+
- spec/hamster/hash/empty_spec.rb
|
38
|
+
- spec/hamster/hash/eql_spec.rb
|
39
|
+
- spec/hamster/hash/get_spec.rb
|
40
|
+
- spec/hamster/hash/has_key_spec.rb
|
41
|
+
- spec/hamster/hash/put_spec.rb
|
42
|
+
- spec/hamster/hash/remove_spec.rb
|
43
|
+
- spec/hamster/list/accessor_spec.rb
|
44
|
+
- spec/hamster/list/car_spec.rb
|
45
|
+
- spec/hamster/list/copying_spec.rb
|
46
|
+
- spec/hamster/list/each_spec.rb
|
47
|
+
- spec/hamster/list/empty_spec.rb
|
48
|
+
- spec/hamster/list/eql_spec.rb
|
49
|
+
- spec/hamster/list/map_spec.rb
|
50
|
+
- spec/hamster/list/reduce_spec.rb
|
51
|
+
- spec/hamster/stack/copying_spec.rb
|
52
|
+
- spec/hamster/stack/empty_spec.rb
|
53
|
+
- spec/hamster/stack/eql_spec.rb
|
54
|
+
- spec/hamster/stack/push_spec.rb
|
39
55
|
- spec/spec.opts
|
40
56
|
- spec/spec_helper.rb
|
41
57
|
- tasks/spec.rb
|
@@ -71,6 +87,6 @@ rubyforge_project:
|
|
71
87
|
rubygems_version: 1.3.5
|
72
88
|
signing_key:
|
73
89
|
specification_version: 3
|
74
|
-
summary:
|
90
|
+
summary: Persistent Data Structures for Ruby
|
75
91
|
test_files: []
|
76
92
|
|
data/lib/hamster/entry.rb
DELETED
@@ -1,51 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
-
|
3
|
-
module Hamster
|
4
|
-
|
5
|
-
describe Trie do
|
6
|
-
|
7
|
-
describe "#each" do
|
8
|
-
|
9
|
-
before do
|
10
|
-
@trie = Trie.new
|
11
|
-
@expected_pairs = { "A" => "aye", "B" => "bee", "C" => "sea" }
|
12
|
-
@expected_pairs.each do |key, value|
|
13
|
-
@trie = @trie.put(key, value)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
describe "with a block (internal iteration)" do
|
18
|
-
|
19
|
-
it "returns self" do
|
20
|
-
@trie.each {}.should == @trie
|
21
|
-
end
|
22
|
-
|
23
|
-
it "yields all key value pairs" do
|
24
|
-
actual_pairs = {}
|
25
|
-
@trie.each do |key, value|
|
26
|
-
actual_pairs[key] = value
|
27
|
-
end
|
28
|
-
actual_pairs.should == @expected_pairs
|
29
|
-
end
|
30
|
-
|
31
|
-
end
|
32
|
-
|
33
|
-
describe "with no block (external iteration)" do
|
34
|
-
|
35
|
-
it "returns an enumerator over all key value pairs" do
|
36
|
-
actual_pairs = {}
|
37
|
-
enum = @trie.each
|
38
|
-
loop do
|
39
|
-
key, value = enum.next
|
40
|
-
actual_pairs[key] = value
|
41
|
-
end
|
42
|
-
actual_pairs.should == @expected_pairs
|
43
|
-
end
|
44
|
-
|
45
|
-
end
|
46
|
-
|
47
|
-
end
|
48
|
-
|
49
|
-
end
|
50
|
-
|
51
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
-
|
3
|
-
module Hamster
|
4
|
-
|
5
|
-
describe Trie do
|
6
|
-
|
7
|
-
describe "#empty?" do
|
8
|
-
|
9
|
-
it "initially returns true" do
|
10
|
-
Trie.new.should be_empty
|
11
|
-
end
|
12
|
-
|
13
|
-
it "returns false once items have been added" do
|
14
|
-
trie = Trie.new.put("A", "aye")
|
15
|
-
trie.should_not be_empty
|
16
|
-
end
|
17
|
-
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
21
|
-
|
22
|
-
end
|