hamster 1.0.1.pre.rc.1 → 1.0.1.pre.rc2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/hamster/groupable.rb +12 -0
- data/lib/hamster/hash.rb +32 -12
- data/lib/hamster/list.rb +68 -22
- data/lib/hamster/queue.rb +5 -6
- data/lib/hamster/set.rb +50 -14
- data/lib/hamster/stack.rb +2 -0
- data/lib/hamster/trie.rb +34 -14
- data/lib/hamster/tuple.rb +9 -24
- data/lib/hamster/vector.rb +29 -7
- data/lib/hamster/version.rb +1 -1
- data/spec/hamster/core_ext/array_spec.rb +4 -10
- data/spec/hamster/core_ext/enumerable_spec.rb +1 -0
- data/spec/hamster/experimental/mutable_set/add_qm_spec.rb +14 -22
- data/spec/hamster/experimental/mutable_set/add_spec.rb +20 -34
- data/spec/hamster/experimental/mutable_set/delete_qm_spec.rb +15 -24
- data/spec/hamster/experimental/mutable_set/delete_spec.rb +15 -25
- data/spec/hamster/experimental/mutable_stack/pop_spec.rb +21 -27
- data/spec/hamster/experimental/mutable_stack/push_spec.rb +11 -31
- data/spec/hamster/hash/all_spec.rb +21 -34
- data/spec/hamster/hash/any_spec.rb +0 -15
- data/spec/hamster/hash/clear_spec.rb +1 -8
- data/spec/hamster/hash/construction_spec.rb +3 -11
- data/spec/hamster/hash/copying_spec.rb +1 -8
- data/spec/hamster/hash/delete_spec.rb +1 -10
- data/spec/hamster/hash/each_spec.rb +1 -12
- data/spec/hamster/hash/empty_spec.rb +8 -8
- data/spec/hamster/hash/eql_spec.rb +30 -22
- data/spec/hamster/hash/except_spec.rb +1 -10
- data/spec/hamster/hash/fetch_spec.rb +1 -24
- data/spec/hamster/hash/filter_spec.rb +1 -16
- data/spec/hamster/hash/find_spec.rb +1 -14
- data/spec/hamster/hash/has_key_spec.rb +1 -10
- data/spec/hamster/hash/hash_spec.rb +1 -16
- data/spec/hamster/hash/immutable_spec.rb +0 -3
- data/spec/hamster/hash/inspect_spec.rb +1 -9
- data/spec/hamster/hash/keys_spec.rb +4 -5
- data/spec/hamster/hash/map_spec.rb +1 -16
- data/spec/hamster/hash/merge_spec.rb +5 -11
- data/spec/hamster/hash/new_spec.rb +21 -0
- data/spec/hamster/hash/put_spec.rb +13 -11
- data/spec/hamster/hash/reduce_spec.rb +7 -15
- data/spec/hamster/hash/remove_spec.rb +1 -16
- data/spec/hamster/hash/uniq_spec.rb +1 -8
- data/spec/hamster/hash/values_spec.rb +1 -6
- data/spec/hamster/immutable/copying_spec.rb +1 -8
- data/spec/hamster/immutable/immutable_spec.rb +1 -14
- data/spec/hamster/immutable/memoize_spec.rb +0 -1
- data/spec/hamster/list/add_spec.rb +1 -6
- data/spec/hamster/list/all_spec.rb +1 -28
- data/spec/hamster/list/any_spec.rb +1 -20
- data/spec/hamster/list/append_spec.rb +1 -11
- data/spec/hamster/list/at_spec.rb +1 -13
- data/spec/hamster/list/break_spec.rb +1 -13
- data/spec/hamster/list/cadr_spec.rb +1 -9
- data/spec/hamster/list/chunk_spec.rb +1 -9
- data/spec/hamster/list/clear_spec.rb +1 -9
- data/spec/hamster/list/combinations_spec.rb +1 -11
- data/spec/hamster/list/compact_spec.rb +1 -9
- data/spec/hamster/list/cons_spec.rb +1 -10
- data/spec/hamster/list/construction_spec.rb +1 -30
- data/spec/hamster/list/copying_spec.rb +1 -9
- data/spec/hamster/list/count_spec.rb +1 -15
- data/spec/hamster/list/cycle_spec.rb +1 -10
- data/spec/hamster/list/drop_spec.rb +1 -9
- data/spec/hamster/list/drop_while_spec.rb +1 -13
- data/spec/hamster/list/each_slice_spec.rb +1 -17
- data/spec/hamster/list/each_spec.rb +1 -17
- data/spec/hamster/list/each_with_index_spec.rb +1 -10
- data/spec/hamster/list/elem_index_spec.rb +1 -13
- data/spec/hamster/list/elem_indices_spec.rb +1 -11
- data/spec/hamster/list/empty_spec.rb +1 -13
- data/spec/hamster/list/eql_spec.rb +52 -62
- data/spec/hamster/list/find_index_spec.rb +1 -13
- data/spec/hamster/list/find_indices_spec.rb +1 -11
- data/spec/hamster/list/find_spec.rb +1 -17
- data/spec/hamster/list/flatten_spec.rb +1 -9
- data/spec/hamster/list/grep_spec.rb +1 -16
- data/spec/hamster/list/group_by_spec.rb +1 -20
- data/spec/hamster/list/head_spec.rb +1 -11
- data/spec/hamster/list/include_spec.rb +1 -13
- data/spec/hamster/list/init_spec.rb +1 -8
- data/spec/hamster/list/inits_spec.rb +1 -9
- data/spec/hamster/list/inspect_spec.rb +1 -11
- data/spec/hamster/list/intersperse_spec.rb +1 -9
- data/spec/hamster/list/join_spec.rb +1 -18
- data/spec/hamster/list/last_spec.rb +1 -11
- data/spec/hamster/list/map_spec.rb +1 -15
- data/spec/hamster/list/maximum_spec.rb +1 -20
- data/spec/hamster/list/merge_by_spec.rb +1 -19
- data/spec/hamster/list/merge_spec.rb +1 -20
- data/spec/hamster/list/minimum_spec.rb +1 -20
- data/spec/hamster/list/none_spec.rb +1 -18
- data/spec/hamster/list/one_spec.rb +1 -16
- data/spec/hamster/list/partition_spec.rb +1 -13
- data/spec/hamster/list/product_spec.rb +1 -11
- data/spec/hamster/list/reduce_spec.rb +0 -1
- data/spec/hamster/list/remove_spec.rb +1 -15
- data/spec/hamster/list/reverse_spec.rb +1 -11
- data/spec/hamster/list/size_spec.rb +1 -13
- data/spec/hamster/list/slice_spec.rb +1 -11
- data/spec/hamster/list/sorting_spec.rb +1 -14
- data/spec/hamster/list/span_spec.rb +1 -12
- data/spec/hamster/list/split_at_spec.rb +1 -9
- data/spec/hamster/list/sum_spec.rb +1 -11
- data/spec/hamster/list/tail_spec.rb +1 -11
- data/spec/hamster/list/tails_spec.rb +1 -9
- data/spec/hamster/list/take_spec.rb +1 -9
- data/spec/hamster/list/take_while_spec.rb +0 -1
- data/spec/hamster/list/to_a_spec.rb +1 -13
- data/spec/hamster/list/to_ary_spec.rb +0 -1
- data/spec/hamster/list/to_list_spec.rb +1 -9
- data/spec/hamster/list/to_set_spec.rb +2 -8
- data/spec/hamster/list/union_spec.rb +1 -11
- data/spec/hamster/list/uniq_spec.rb +1 -11
- data/spec/hamster/queue/clear_spec.rb +1 -9
- data/spec/hamster/queue/construction_spec.rb +1 -10
- data/spec/hamster/queue/dequeue_spec.rb +1 -11
- data/spec/hamster/queue/empty_spec.rb +1 -13
- data/spec/hamster/queue/enqueue_spec.rb +1 -11
- data/spec/hamster/queue/head_spec.rb +1 -11
- data/spec/hamster/queue/inspect_spec.rb +1 -9
- data/spec/hamster/queue/size_spec.rb +1 -11
- data/spec/hamster/queue/to_a_spec.rb +1 -11
- data/spec/hamster/queue/to_list_spec.rb +1 -11
- data/spec/hamster/set/add_spec.rb +1 -12
- data/spec/hamster/set/all_spec.rb +1 -18
- data/spec/hamster/set/any_spec.rb +1 -17
- data/spec/hamster/set/clear_spec.rb +9 -8
- data/spec/hamster/set/compact_spec.rb +1 -9
- data/spec/hamster/set/construction_spec.rb +1 -9
- data/spec/hamster/set/copying_spec.rb +1 -8
- data/spec/hamster/set/count_spec.rb +1 -13
- data/spec/hamster/set/delete_spec.rb +1 -10
- data/spec/hamster/set/difference_spec.rb +1 -11
- data/spec/hamster/set/empty_spec.rb +1 -11
- data/spec/hamster/set/exclusion_spec.rb +1 -11
- data/spec/hamster/set/filter_spec.rb +10 -17
- data/spec/hamster/set/find_spec.rb +1 -14
- data/spec/hamster/set/flatten_spec.rb +8 -10
- data/spec/hamster/set/group_by_spec.rb +8 -17
- data/spec/hamster/set/head_spec.rb +1 -12
- data/spec/hamster/set/immutable_spec.rb +1 -4
- data/spec/hamster/set/include_spec.rb +1 -9
- data/spec/hamster/set/inspect_spec.rb +1 -9
- data/spec/hamster/set/intersection_spec.rb +1 -11
- data/spec/hamster/set/join_spec.rb +1 -16
- data/spec/hamster/set/map_spec.rb +1 -16
- data/spec/hamster/set/marshal_spec.rb +1 -6
- data/spec/hamster/set/maximum_spec.rb +1 -18
- data/spec/hamster/set/minimum_spec.rb +1 -17
- data/spec/hamster/set/new_spec.rb +21 -0
- data/spec/hamster/set/none_spec.rb +1 -16
- data/spec/hamster/set/one_spec.rb +1 -14
- data/spec/hamster/set/partition_spec.rb +1 -13
- data/spec/hamster/set/product_spec.rb +1 -9
- data/spec/hamster/set/reduce_spec.rb +1 -26
- data/spec/hamster/set/remove_spec.rb +1 -16
- data/spec/hamster/set/size_spec.rb +1 -9
- data/spec/hamster/set/sorting_spec.rb +1 -3
- data/spec/hamster/set/subset_spec.rb +1 -9
- data/spec/hamster/set/sum_spec.rb +1 -9
- data/spec/hamster/set/superset_spec.rb +1 -9
- data/spec/hamster/set/to_a_spec.rb +1 -11
- data/spec/hamster/set/to_list_spec.rb +1 -11
- data/spec/hamster/set/to_set_spec.rb +1 -9
- data/spec/hamster/set/union_spec.rb +1 -11
- data/spec/hamster/set/uniq_spec.rb +1 -7
- data/spec/hamster/sorter/immutable_spec.rb +1 -4
- data/spec/hamster/stack/clear_spec.rb +1 -9
- data/spec/hamster/stack/construction_spec.rb +1 -10
- data/spec/hamster/stack/copying_spec.rb +1 -9
- data/spec/hamster/stack/empty_spec.rb +1 -9
- data/spec/hamster/stack/eql_spec.rb +1 -13
- data/spec/hamster/stack/immutable_spec.rb +1 -4
- data/spec/hamster/stack/inspect_spec.rb +1 -9
- data/spec/hamster/stack/peek_spec.rb +1 -11
- data/spec/hamster/stack/pop_spec.rb +1 -11
- data/spec/hamster/stack/push_spec.rb +1 -11
- data/spec/hamster/stack/size_spec.rb +1 -11
- data/spec/hamster/stack/to_a_spec.rb +1 -11
- data/spec/hamster/stack/to_list_spec.rb +1 -9
- data/spec/hamster/tuple/construction_spec.rb +30 -0
- data/spec/hamster/tuple/copying_spec.rb +1 -8
- data/spec/hamster/tuple/eql_spec.rb +57 -40
- data/spec/hamster/tuple/first_spec.rb +1 -6
- data/spec/hamster/tuple/immutable_spec.rb +1 -4
- data/spec/hamster/tuple/inspect_spec.rb +1 -6
- data/spec/hamster/tuple/last_spec.rb +1 -6
- data/spec/hamster/tuple/to_a_spec.rb +1 -9
- data/spec/hamster/tuple/to_ary_spec.rb +0 -1
- data/spec/hamster/undefined/erase_spec.rb +1 -12
- data/spec/hamster/vector/add_spec.rb +8 -0
- data/spec/hamster/vector/clear_spec.rb +1 -8
- data/spec/hamster/vector/copying_spec.rb +1 -8
- data/spec/hamster/vector/each_spec.rb +1 -11
- data/spec/hamster/vector/each_with_index_spec.rb +1 -9
- data/spec/hamster/vector/empty_spec.rb +7 -9
- data/spec/hamster/vector/eql_spec.rb +1 -12
- data/spec/hamster/vector/filter_spec.rb +8 -12
- data/spec/hamster/vector/first_spec.rb +1 -11
- data/spec/hamster/vector/get_spec.rb +1 -23
- data/spec/hamster/vector/include_spec.rb +1 -10
- data/spec/hamster/vector/map_spec.rb +8 -14
- data/spec/hamster/vector/new_spec.rb +48 -0
- data/spec/hamster/vector/reduce_spec.rb +1 -26
- data/spec/hamster/vector/set_spec.rb +8 -0
- data/spec/spec_helper.rb +0 -5
- metadata +35 -68
data/lib/hamster/stack.rb
CHANGED
data/lib/hamster/trie.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
|
-
require "forwardable"
|
2
|
-
|
3
1
|
module Hamster
|
4
2
|
class Trie
|
5
|
-
|
3
|
+
def self.[](pairs)
|
4
|
+
result = self.new(0)
|
5
|
+
pairs.each { |key, val| result.send(:put!, key, val) }
|
6
|
+
result
|
7
|
+
end
|
6
8
|
|
7
9
|
# Returns the number of key-value pairs in the trie.
|
8
10
|
attr_reader :size
|
@@ -23,7 +25,6 @@ module Hamster
|
|
23
25
|
def key?(key)
|
24
26
|
!!get(key)
|
25
27
|
end
|
26
|
-
def_delegator :self, :key?, :has_key?
|
27
28
|
|
28
29
|
# Calls <tt>block</tt> once for each entry in the trie, passing the key-value pair as parameters.
|
29
30
|
def each
|
@@ -50,12 +51,14 @@ module Hamster
|
|
50
51
|
|
51
52
|
if !entry
|
52
53
|
entries = @entries.dup
|
54
|
+
key = key.dup.freeze if key.is_a?(String)
|
53
55
|
entries[index] = Entry.new(key, value)
|
54
|
-
|
56
|
+
Trie.new(@significant_bits, @size + 1, entries, @children)
|
55
57
|
elsif entry.key.eql?(key)
|
56
58
|
entries = @entries.dup
|
59
|
+
key = key.dup.freeze if key.is_a?(String)
|
57
60
|
entries[index] = Entry.new(key, value)
|
58
|
-
|
61
|
+
Trie.new(@significant_bits, @size, entries, @children)
|
59
62
|
else
|
60
63
|
children = @children.dup
|
61
64
|
child = children[index]
|
@@ -63,11 +66,11 @@ module Hamster
|
|
63
66
|
if child
|
64
67
|
children[index] = child.put(key, value)
|
65
68
|
else
|
66
|
-
children[index] =
|
69
|
+
children[index] = Trie.new(@significant_bits + 5).put!(key, value)
|
67
70
|
end
|
68
71
|
new_child_size = children[index].size
|
69
72
|
new_self_size = @size + (new_child_size - child_size)
|
70
|
-
|
73
|
+
Trie.new(@significant_bits, new_self_size, @entries, children)
|
71
74
|
end
|
72
75
|
end
|
73
76
|
|
@@ -85,7 +88,7 @@ module Hamster
|
|
85
88
|
|
86
89
|
# Returns a copy of <tt>self</tt> with the given key (and associated value) deleted. If not found, returns <tt>self</tt>.
|
87
90
|
def delete(key)
|
88
|
-
find_and_delete(key) ||
|
91
|
+
find_and_delete(key) || Trie.new(@significant_bits)
|
89
92
|
end
|
90
93
|
|
91
94
|
def include?(key, value)
|
@@ -102,15 +105,32 @@ module Hamster
|
|
102
105
|
end
|
103
106
|
true
|
104
107
|
end
|
105
|
-
|
108
|
+
alias :== :eql?
|
106
109
|
|
107
110
|
protected
|
108
111
|
|
109
112
|
# Returns <tt>self</tt> after overwriting the element associated with the specified key.
|
110
113
|
def put!(key, value)
|
111
114
|
index = index_for(key)
|
112
|
-
|
113
|
-
|
115
|
+
entry = @entries[index]
|
116
|
+
if !entry
|
117
|
+
@size += 1
|
118
|
+
key = key.dup.freeze if key.is_a?(String)
|
119
|
+
@entries[index] = Entry.new(key, value)
|
120
|
+
elsif entry.key.eql?(key)
|
121
|
+
key = key.dup.freeze if key.is_a?(String)
|
122
|
+
@entries[index] = Entry.new(key, value)
|
123
|
+
else
|
124
|
+
child = @children[index]
|
125
|
+
if child
|
126
|
+
old_child_size = child.size
|
127
|
+
@children[index] = child.put!(key, value)
|
128
|
+
@size += child.size - old_child_size
|
129
|
+
else
|
130
|
+
@children[index] = Trie.new(@significant_bits + 5).put!(key, value)
|
131
|
+
@size += 1
|
132
|
+
end
|
133
|
+
end
|
114
134
|
self
|
115
135
|
end
|
116
136
|
|
@@ -130,7 +150,7 @@ module Hamster
|
|
130
150
|
children = @children.dup
|
131
151
|
children[index] = copy
|
132
152
|
new_size = @size - (child.size - copy_size(copy))
|
133
|
-
return
|
153
|
+
return Trie.new(@significant_bits, new_size, @entries, children)
|
134
154
|
end
|
135
155
|
end
|
136
156
|
end
|
@@ -151,7 +171,7 @@ module Hamster
|
|
151
171
|
else
|
152
172
|
entries[index] = nil
|
153
173
|
end
|
154
|
-
|
174
|
+
Trie.new(@significant_bits, @size - 1, entries, children || @children)
|
155
175
|
end
|
156
176
|
end
|
157
177
|
|
data/lib/hamster/tuple.rb
CHANGED
@@ -1,39 +1,24 @@
|
|
1
|
-
require "forwardable"
|
2
1
|
require "hamster/immutable"
|
3
2
|
|
4
3
|
module Hamster
|
5
|
-
|
6
|
-
|
4
|
+
def self.tuple(*items)
|
5
|
+
Hamster::Tuple.new(*items)
|
6
|
+
end
|
7
|
+
class Tuple < Array
|
7
8
|
include Immutable
|
8
9
|
|
9
10
|
def initialize(*items)
|
10
|
-
|
11
|
-
end
|
12
|
-
|
13
|
-
def first
|
14
|
-
@items.first
|
15
|
-
end
|
16
|
-
|
17
|
-
def last
|
18
|
-
@items.last
|
11
|
+
super(items)
|
19
12
|
end
|
20
13
|
|
21
14
|
def eql?(other)
|
22
|
-
|
23
|
-
instance_of?(other.class) && @items.eql?(other.instance_variable_get(:@items))
|
24
|
-
end
|
25
|
-
def_delegator :self, :eql?, :==
|
26
|
-
|
27
|
-
def to_ary
|
28
|
-
@items
|
29
|
-
end
|
30
|
-
|
31
|
-
def to_a
|
32
|
-
@items.dup
|
15
|
+
other.is_a?(Tuple) && super
|
33
16
|
end
|
34
17
|
|
35
18
|
def inspect
|
36
|
-
"(#{
|
19
|
+
"(#{super[1..-2]})"
|
37
20
|
end
|
21
|
+
|
22
|
+
undef :<<, :[]=, :collect!, :compact!, :delete, :delete_at, :delete_if, :flatten!, :map!, :reject!, :reverse!, :rotate!, :select!, :shuffle!, :slice!, :sort!, :sort_by!, :uniq!
|
38
23
|
end
|
39
24
|
end
|
data/lib/hamster/vector.rb
CHANGED
@@ -5,7 +5,7 @@ require "hamster/enumerable"
|
|
5
5
|
|
6
6
|
module Hamster
|
7
7
|
def self.vector(*items)
|
8
|
-
|
8
|
+
Vector.new(items)
|
9
9
|
end
|
10
10
|
|
11
11
|
class Vector
|
@@ -20,10 +20,30 @@ module Hamster
|
|
20
20
|
attr_reader :size
|
21
21
|
def_delegator :self, :size, :length
|
22
22
|
|
23
|
-
|
23
|
+
class << self
|
24
|
+
alias :alloc :new
|
25
|
+
|
26
|
+
def new(items=[])
|
27
|
+
items.empty? ? empty : alloc(items)
|
28
|
+
end
|
29
|
+
|
30
|
+
def [](*items)
|
31
|
+
new(items)
|
32
|
+
end
|
33
|
+
|
34
|
+
def empty
|
35
|
+
@empty ||= self.alloc
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def initialize(items=[])
|
40
|
+
@root = items
|
41
|
+
@size = items.size
|
24
42
|
@levels = 0
|
25
|
-
@root
|
26
|
-
|
43
|
+
while @root.size > 32
|
44
|
+
@root = @root.each_slice(32).to_a
|
45
|
+
@levels += 1
|
46
|
+
end
|
27
47
|
end
|
28
48
|
|
29
49
|
def empty?
|
@@ -48,6 +68,8 @@ module Hamster
|
|
48
68
|
end
|
49
69
|
def_delegator :self, :add, :<<
|
50
70
|
def_delegator :self, :add, :cons
|
71
|
+
def_delegator :self, :add, :conj
|
72
|
+
def_delegator :self, :add, :conjoin
|
51
73
|
|
52
74
|
# def delete(index)
|
53
75
|
# end
|
@@ -79,13 +101,13 @@ module Hamster
|
|
79
101
|
|
80
102
|
def map(&block)
|
81
103
|
return self unless block_given?
|
82
|
-
reduce(
|
104
|
+
reduce(self.class.empty) { |vector, item| vector.add(yield(item)) }
|
83
105
|
end
|
84
106
|
def_delegator :self, :map, :collect
|
85
107
|
|
86
108
|
def filter
|
87
109
|
return self unless block_given?
|
88
|
-
reduce(
|
110
|
+
reduce(self.class.empty) { |vector, item| yield(item) ? vector.add(item) : vector }
|
89
111
|
end
|
90
112
|
|
91
113
|
def clear
|
@@ -143,7 +165,7 @@ module Hamster
|
|
143
165
|
end
|
144
166
|
|
145
167
|
def full?
|
146
|
-
(@size >> root_index_bits) >
|
168
|
+
(@size >> root_index_bits) > 31
|
147
169
|
end
|
148
170
|
|
149
171
|
def root_index_bits
|
data/lib/hamster/version.rb
CHANGED
@@ -1,20 +1,14 @@
|
|
1
1
|
require "spec_helper"
|
2
|
-
|
3
2
|
require "hamster/core_ext/enumerable"
|
4
3
|
|
5
4
|
describe Array do
|
5
|
+
let(:array) { %w[A B C] }
|
6
6
|
|
7
7
|
describe "#to_list" do
|
8
|
+
let(:to_list) { array.to_list }
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
@list = array.to_list
|
12
|
-
end
|
13
|
-
|
14
|
-
it "returns an equivalent list" do
|
15
|
-
@list.should == Hamster.list("A", "B", "C")
|
10
|
+
it "should be an equivalent hamster list" do
|
11
|
+
expect(to_list).to eq(Hamster.list("A", "B", "C"))
|
16
12
|
end
|
17
|
-
|
18
13
|
end
|
19
|
-
|
20
14
|
end
|
@@ -3,45 +3,37 @@ require "spec_helper"
|
|
3
3
|
require "hamster/experimental/mutable_set"
|
4
4
|
|
5
5
|
describe Hamster::MutableSet do
|
6
|
+
let(:mutable) { Hamster.mutable_set(*values) }
|
6
7
|
|
7
8
|
describe "#add?" do
|
9
|
+
let(:values) { %w[A B C] }
|
10
|
+
let(:add?) { mutable.add?(value) }
|
8
11
|
|
9
|
-
|
10
|
-
|
11
|
-
end
|
12
|
-
|
13
|
-
describe "with a unique value" do
|
14
|
-
|
15
|
-
before do
|
16
|
-
@result = @set.add?("D")
|
17
|
-
end
|
12
|
+
context "with a unique value" do
|
13
|
+
let(:value) { "D" }
|
18
14
|
|
19
15
|
it "returns true" do
|
20
|
-
|
16
|
+
expect(add?).to be true
|
21
17
|
end
|
22
18
|
|
23
19
|
it "modifies the set to include the new value" do
|
24
|
-
|
20
|
+
add?
|
21
|
+
expect(mutable).to eq(Hamster.mutable_set("A", "B", "C", "D"))
|
25
22
|
end
|
26
23
|
|
27
24
|
end
|
28
25
|
|
29
|
-
|
26
|
+
context "with a duplicate value" do
|
27
|
+
let(:value) { "C" }
|
30
28
|
|
31
|
-
|
32
|
-
|
29
|
+
it "returns false" do
|
30
|
+
expect(add?).to be(false)
|
33
31
|
end
|
34
32
|
|
35
33
|
it "preserves the original values" do
|
36
|
-
|
34
|
+
add?
|
35
|
+
expect(mutable).to eq(Hamster.mutable_set("A", "B", "C"))
|
37
36
|
end
|
38
|
-
|
39
|
-
it "returns false" do
|
40
|
-
@result.should == false
|
41
|
-
end
|
42
|
-
|
43
37
|
end
|
44
|
-
|
45
38
|
end
|
46
|
-
|
47
39
|
end
|
@@ -1,51 +1,37 @@
|
|
1
1
|
require "spec_helper"
|
2
|
-
|
3
2
|
require "hamster/experimental/mutable_set"
|
4
3
|
|
5
4
|
describe Hamster::MutableSet do
|
5
|
+
let(:mutable) { Hamster.mutable_set(*values) }
|
6
6
|
|
7
|
-
|
7
|
+
describe "#add" do
|
8
|
+
let(:values) { %w[A B C] }
|
9
|
+
let(:add) { mutable.add(value) }
|
8
10
|
|
9
|
-
|
11
|
+
context "with a unique value" do
|
12
|
+
let(:value) { "D" }
|
10
13
|
|
11
|
-
|
12
|
-
|
14
|
+
it "returns self" do
|
15
|
+
expect(add).to eq(mutable)
|
13
16
|
end
|
14
17
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
@result = @set.send(method, "D")
|
19
|
-
end
|
20
|
-
|
21
|
-
it "returns self" do
|
22
|
-
@result.should equal(@set)
|
23
|
-
end
|
24
|
-
|
25
|
-
it "modifies the set to include the new value" do
|
26
|
-
@set.should == Hamster.mutable_set("A", "B", "C", "D")
|
27
|
-
end
|
28
|
-
|
18
|
+
it "modifies the original set to include new value" do
|
19
|
+
add
|
20
|
+
expect(mutable).to eq(Hamster.mutable_set("A", "B", "C", "D"))
|
29
21
|
end
|
22
|
+
end
|
30
23
|
|
31
|
-
|
32
|
-
|
33
|
-
before do
|
34
|
-
@result = @set.send(method, "C")
|
35
|
-
end
|
36
|
-
|
37
|
-
it "preserves the original values" do
|
38
|
-
@set.should == Hamster.mutable_set("A", "B", "C")
|
39
|
-
end
|
40
|
-
|
41
|
-
it "returns self" do
|
42
|
-
@result.should equal(@set)
|
43
|
-
end
|
24
|
+
context "with a duplicate value" do
|
25
|
+
let(:value) { "C" }
|
44
26
|
|
27
|
+
it "returns self" do
|
28
|
+
expect(add).to eq(mutable)
|
45
29
|
end
|
46
30
|
|
31
|
+
it "preserves the original values" do
|
32
|
+
add
|
33
|
+
expect(mutable).to eq(Hamster.mutable_set("A", "B", "C"))
|
34
|
+
end
|
47
35
|
end
|
48
|
-
|
49
36
|
end
|
50
|
-
|
51
37
|
end
|
@@ -1,47 +1,38 @@
|
|
1
1
|
require "spec_helper"
|
2
|
-
|
3
2
|
require "hamster/experimental/mutable_set"
|
4
3
|
|
5
|
-
describe Hamster::
|
4
|
+
describe Hamster::MutableSet do
|
5
|
+
let(:mutable) { Hamster.mutable_set(*values) }
|
6
6
|
|
7
7
|
describe "#delete?" do
|
8
|
+
let(:values) { %w[A B C] }
|
9
|
+
let(:delete?) { mutable.delete?(value) }
|
8
10
|
|
9
|
-
before do
|
10
|
-
@set = Hamster.mutable_set("A", "B", "C")
|
11
|
-
end
|
12
|
-
|
13
|
-
describe "with an existing value" do
|
14
11
|
|
15
|
-
|
16
|
-
|
17
|
-
end
|
12
|
+
context "with an existing value" do
|
13
|
+
let(:value) { "B" }
|
18
14
|
|
19
15
|
it "returns true" do
|
20
|
-
|
16
|
+
expect(delete?).to be(true)
|
21
17
|
end
|
22
18
|
|
23
19
|
it "modifies the set to remove the value" do
|
24
|
-
|
20
|
+
delete?
|
21
|
+
expect(mutable).to eq(Hamster.mutable_set("A", "C"))
|
25
22
|
end
|
26
|
-
|
27
23
|
end
|
28
24
|
|
29
|
-
|
25
|
+
context "with a non-existing value" do
|
26
|
+
let(:value) { "D" }
|
30
27
|
|
31
|
-
|
32
|
-
|
28
|
+
it "returns false" do
|
29
|
+
expect(delete?).to be(false)
|
33
30
|
end
|
34
31
|
|
35
32
|
it "preserves the original values" do
|
36
|
-
|
33
|
+
delete?
|
34
|
+
expect(mutable).to eq(Hamster.mutable_set("A", "B", "C"))
|
37
35
|
end
|
38
|
-
|
39
|
-
it "returns false" do
|
40
|
-
@result.should == false
|
41
|
-
end
|
42
|
-
|
43
36
|
end
|
44
|
-
|
45
37
|
end
|
46
|
-
|
47
38
|
end
|