hamster 0.1.2 → 0.1.5

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.
Files changed (39) hide show
  1. data/History.rdoc +13 -1
  2. data/README.rdoc +72 -25
  3. data/TODO +1 -0
  4. data/lib/hamster.rb +4 -1
  5. data/lib/hamster/hash.rb +66 -0
  6. data/lib/hamster/list.rb +105 -0
  7. data/lib/hamster/set.rb +65 -0
  8. data/lib/hamster/stack.rb +53 -0
  9. data/lib/hamster/trie.rb +45 -28
  10. data/lib/hamster/version.rb +1 -1
  11. data/spec/hamster/hash/copying_spec.rb +25 -0
  12. data/spec/hamster/hash/each_spec.rb +47 -0
  13. data/spec/hamster/hash/empty_spec.rb +17 -0
  14. data/spec/hamster/hash/eql_spec.rb +20 -0
  15. data/spec/hamster/hash/get_spec.rb +22 -0
  16. data/spec/hamster/hash/has_key_spec.rb +21 -0
  17. data/spec/hamster/hash/put_spec.rb +93 -0
  18. data/spec/hamster/hash/remove_spec.rb +113 -0
  19. data/spec/hamster/list/accessor_spec.rb +26 -0
  20. data/spec/hamster/list/car_spec.rb +17 -0
  21. data/spec/hamster/list/copying_spec.rb +25 -0
  22. data/spec/hamster/list/each_spec.rb +45 -0
  23. data/spec/hamster/list/empty_spec.rb +18 -0
  24. data/spec/hamster/list/eql_spec.rb +22 -0
  25. data/spec/hamster/list/map_spec.rb +43 -0
  26. data/spec/hamster/list/reduce_spec.rb +31 -0
  27. data/spec/hamster/stack/copying_spec.rb +25 -0
  28. data/spec/hamster/stack/empty_spec.rb +17 -0
  29. data/spec/hamster/stack/eql_spec.rb +20 -0
  30. data/spec/hamster/stack/push_spec.rb +42 -0
  31. metadata +28 -12
  32. data/lib/hamster/entry.rb +0 -18
  33. data/spec/hamster/trie/each_spec.rb +0 -51
  34. data/spec/hamster/trie/empty_spec.rb +0 -22
  35. data/spec/hamster/trie/enumerable_spec.rb +0 -13
  36. data/spec/hamster/trie/get_spec.rb +0 -26
  37. data/spec/hamster/trie/has_key_spec.rb +0 -25
  38. data/spec/hamster/trie/put_spec.rb +0 -97
  39. data/spec/hamster/trie/remove_spec.rb +0 -115
@@ -1,13 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
-
3
- module Hamster
4
-
5
- describe Trie do
6
-
7
- it "is Enumerable" do
8
- Trie.ancestors.should include(Enumerable)
9
- end
10
-
11
- end
12
-
13
- end
@@ -1,26 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
-
3
- module Hamster
4
-
5
- describe Trie do
6
-
7
- describe "#get" do
8
-
9
- before do
10
- @trie = Trie.new
11
- @trie = @trie.put("A", "aye")
12
- end
13
-
14
- it "returns the value for an existing key" do
15
- @trie.get("A").should == "aye"
16
- end
17
-
18
- it "returns nil for a non-existing key" do
19
- @trie.get("B").should be_nil
20
- end
21
-
22
- end
23
-
24
- end
25
-
26
- end
@@ -1,25 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
-
3
- module Hamster
4
-
5
- describe Trie do
6
-
7
- describe "#has_key?" do
8
-
9
- before do
10
- @trie = Trie.new.put("A", "aye")
11
- end
12
-
13
- it "returns true for an existing key" do
14
- @trie.has_key?("A").should be_true
15
- end
16
-
17
- it "returns false for a non-existing key" do
18
- @trie.has_key?("B").should be_false
19
- end
20
-
21
- end
22
-
23
- end
24
-
25
- end
@@ -1,97 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
-
3
- module Hamster
4
-
5
- describe Trie do
6
-
7
- describe "#put" do
8
-
9
- describe "with a key/value pair that already exists" do
10
-
11
- before do
12
- @original = Trie.new.put("A", "aye").put("B", "bee")
13
- @copy = @original.put("A", "yes")
14
- end
15
-
16
- it "returns a modified copy" do
17
- @copy.should_not === @original
18
- end
19
-
20
- describe "the original" do
21
-
22
- it "still has the original key/value pairs" do
23
- @original.get("A").should == "aye"
24
- @original.get("B").should == "bee"
25
- end
26
-
27
- it "still has the original size" do
28
- @original.size.should == 2
29
- end
30
-
31
- end
32
-
33
- describe "the modified copy" do
34
-
35
- it "has the new key/value pairs" do
36
- @copy.get("A").should == "yes"
37
- @copy.get("B").should == "bee"
38
- end
39
-
40
- it "has the original size" do
41
- @copy.size == 2
42
- end
43
-
44
- end
45
-
46
- end
47
-
48
- describe "with a key/value pair that doesn't exist" do
49
-
50
- before do
51
- @original = Trie.new.put("A", "aye")
52
- @copy = @original.put("B", "bee")
53
- end
54
-
55
- it "returns a modified copy" do
56
- @copy.should_not === @original
57
- end
58
-
59
- describe "the original" do
60
-
61
- it "still has the original key/value pairs" do
62
- @original.get("A").should == "aye"
63
- end
64
-
65
- it "doesn't contain the new key/value pair" do
66
- @original.has_key?("B").should be_false
67
- end
68
-
69
- it "still has the original size" do
70
- @original.size.should == 1
71
- end
72
-
73
- end
74
-
75
- describe "the modified copy" do
76
-
77
- it "has the original key/value pairs" do
78
- @copy.get("A").should == "aye"
79
- end
80
-
81
- it "has the new key/value pair" do
82
- @copy.get("B").should == "bee"
83
- end
84
-
85
- it "size is increased by one" do
86
- @copy.size.should == 2
87
- end
88
-
89
- end
90
-
91
- end
92
-
93
- end
94
-
95
- end
96
-
97
- end
@@ -1,115 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
-
3
- module Hamster
4
-
5
- describe Trie do
6
-
7
- describe "#remove" do
8
-
9
- describe "with an existing key" do
10
-
11
- before do
12
- @original = Trie.new.put("A", "aye").put("B", "bee")
13
- @copy = @original.remove("A")
14
- end
15
-
16
- it "returns a modified copy" do
17
- @copy.should_not === @original
18
- end
19
-
20
- describe "the original" do
21
-
22
- it "still has the original key/value pairs" do
23
- @original.get("A").should == "aye"
24
- @original.get("B").should == "bee"
25
- end
26
-
27
- it "still has the original size" do
28
- @original.size.should == 2
29
- end
30
-
31
- end
32
-
33
- describe "the modified copy" do
34
-
35
- it "has all but the removed original key/value pairs" do
36
- @copy.get("B").should == "bee"
37
- end
38
-
39
- it "doesn't have the removed key" do
40
- @copy.has_key?("A").should be_false
41
- end
42
-
43
- it "has a size one less than the original" do
44
- @copy.size.should == 1
45
- end
46
-
47
- end
48
-
49
- end
50
-
51
- describe "with non-existing keys" do
52
-
53
- before do
54
- @original = Trie.new.put("A", "aye")
55
- @copy = @original.remove("missing")
56
- end
57
-
58
- it "returns self" do
59
- @copy.should === @original
60
- end
61
-
62
- describe "the original" do
63
-
64
- it "still has the original key/value pairs" do
65
- @original.get("A").should == "aye"
66
- end
67
-
68
- it "still has the original size" do
69
- @original.size.should == 1
70
- end
71
-
72
- end
73
-
74
- end
75
-
76
- describe "with keys of the same hash value" do
77
-
78
- class Key
79
- def hash; 1; end
80
- end
81
-
82
- def number_of_tries
83
- ObjectSpace.garbage_collect
84
- ObjectSpace.each_object(Trie) {}
85
- end
86
-
87
- before do
88
- @a = Key.new
89
- @b = Key.new
90
- @original = Trie.new.put(@a, "aye").put(@b, "bee")
91
- end
92
-
93
- it "no longer provides access to the removed key" do
94
- copy = @original.remove(@b)
95
- copy.has_key?(@b).should be_false
96
- end
97
-
98
- it "provides access to the remaining keys" do
99
- copy = @original.remove(@a)
100
- copy.get(@b).should == "bee"
101
- end
102
-
103
- it "cleans up empty tries" do
104
- number_of_tries_before = number_of_tries
105
- copy = @original.remove(@b)
106
- number_of_tries.should == number_of_tries_before + 1
107
- end
108
-
109
- end
110
-
111
- end
112
-
113
- end
114
-
115
- end