multiset 0.4.0 → 0.4.1
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.
- checksums.yaml +15 -0
- data/History.txt +10 -0
- data/README.txt +2 -2
- data/lib/multimap.rb +27 -14
- data/lib/multiset.rb +21 -9
- data/spec/multiset_spec.rb +54 -1
- metadata +9 -15
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZDBiNjk1YzU0M2U4ZTEzYzdlZjQwZTdhOWY3OGQ5MWI5MzJiNGY1OA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MDMwNjFmMTkzZjhmYjY0OTE3MzJjZDE2YTliZGE4NTdkZjVkMjM4MQ==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
YjdlOWFjMDBhY2E5YzllYzgzNjNmNjM0YWVjOTcyNGYyOWU2ZmZhNmFkYjdh
|
10
|
+
MDkyYmIzMzg5NzVjNGRlODU1OGE5ZGUzZDhkOTUzNWY0ZGJmMjY4ZjI1Njcz
|
11
|
+
NDNkOGU0OTEwZTYwNGE3ZjhlNmNjZTFjMzZiNTJiNDQ2MzdjODc=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NWRkNDVmMDg4Y2Q3ODMyMzkxMjliNGIwM2ZhNDBiNTdlZDVkMTc4M2U1MjMy
|
14
|
+
ZDJmZTlmODRjOTNhMjMzOTlmY2MxOTc2MjNlMDg0MzBjOGMyZWQ3YmM1NTBh
|
15
|
+
ZGU2OTUwOWU5ODMzYTZhOTFmNTQ0ZjIxZThkNzYwZjQ2ZTIwYzQ=
|
data/History.txt
CHANGED
@@ -204,3 +204,13 @@
|
|
204
204
|
* Multiset#minmax_by
|
205
205
|
* Multiset#sort
|
206
206
|
* Multiset#sort_by
|
207
|
+
|
208
|
+
=== 0.4.1 / 2013-12-02
|
209
|
+
|
210
|
+
(ja)
|
211
|
+
* Multiset#each_**** および Multimap#each_**** が、ブロックなしで呼び出された場合にEnumeratorを返すようにした。
|
212
|
+
(ドキュメントに書いたのに実装を忘れてました…)
|
213
|
+
|
214
|
+
(en)
|
215
|
+
* Multiset#each_**** and Multimap#each_**** returns an Enumerator if called without a block.
|
216
|
+
(Forgot implementation although documented...)
|
data/README.txt
CHANGED
@@ -25,7 +25,7 @@ Multisets are typically used for counting elements and their appearances in coll
|
|
25
25
|
|
26
26
|
# Counting the appearances of characters in a string
|
27
27
|
m = Multiset.new
|
28
|
-
"abracadabra".each_char do |c| # replace with 'each_byte' in Ruby 1.8.6 or
|
28
|
+
"abracadabra".each_char do |c| # replace with 'each_byte' in Ruby 1.8.6 or earlier
|
29
29
|
m << c
|
30
30
|
end
|
31
31
|
p m
|
@@ -35,7 +35,7 @@ Multisets are typically used for counting elements and their appearances in coll
|
|
35
35
|
Multiset.new("abracadabra".split(//))
|
36
36
|
# => #<Multiset:#5 "a", #2 "b", #2 "r", #1 "c", #1 "d">
|
37
37
|
|
38
|
-
# The same, but available with Ruby 1.8.7 or
|
38
|
+
# The same, but available with Ruby 1.8.7 or later
|
39
39
|
Multiset.new("abracadabra".each_char)
|
40
40
|
# => #<Multiset:#5 "a", #2 "b", #2 "r", #1 "c", #1 "d">
|
41
41
|
|
data/lib/multimap.rb
CHANGED
@@ -17,7 +17,7 @@ require "multiset"
|
|
17
17
|
#(see Ruby documentation for "Hash" class),
|
18
18
|
#multimap can contain two or more items for a key.
|
19
19
|
#
|
20
|
-
#Most methods
|
20
|
+
#Most methods’ names are same as those of Hash class, and all other than
|
21
21
|
#a few methods in Hash class is implemented on Multimap class.
|
22
22
|
|
23
23
|
class Multimap
|
@@ -217,13 +217,17 @@ class Multimap
|
|
217
217
|
# Iterates for each pair of a key and a value in <code>self</code>.
|
218
218
|
# Returns <code>self</code>.
|
219
219
|
def each_pair
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
220
|
+
if block_given?
|
221
|
+
cleanup
|
222
|
+
@assoc.each_pair do |key, value_list|
|
223
|
+
value_list.each do |single_value|
|
224
|
+
yield key, single_value
|
225
|
+
end
|
224
226
|
end
|
227
|
+
self
|
228
|
+
else
|
229
|
+
Enumerator.new(self, :each_pair)
|
225
230
|
end
|
226
|
-
self
|
227
231
|
end
|
228
232
|
alias :each :each_pair
|
229
233
|
|
@@ -236,13 +240,17 @@ class Multimap
|
|
236
240
|
# (key, one value associated with the key, numbers of that value
|
237
241
|
# associated with the key). Returns <code>self</code>.
|
238
242
|
def each_pair_with
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
+
if block_given?
|
244
|
+
cleanup
|
245
|
+
@assoc.each_pair do |key, value_list|
|
246
|
+
value_list.each_pair do |a_value, count|
|
247
|
+
yield key, a_value, count
|
248
|
+
end
|
243
249
|
end
|
250
|
+
self
|
251
|
+
else
|
252
|
+
Enumerator.new(self, :each_pair_with)
|
244
253
|
end
|
245
|
-
self
|
246
254
|
end
|
247
255
|
|
248
256
|
# <code>self</code>のすべてのキーと、そのキーに割り当てられた
|
@@ -272,9 +280,14 @@ class Multimap
|
|
272
280
|
#
|
273
281
|
# Iterates for each value in <code>self</code>. Returns <code>self</code>.
|
274
282
|
def each_value(&block) # :yields: single_value
|
275
|
-
|
276
|
-
|
277
|
-
|
283
|
+
if block_given?
|
284
|
+
cleanup
|
285
|
+
@assoc.each_value do |value_list|
|
286
|
+
value_list.each &block
|
287
|
+
end
|
288
|
+
self
|
289
|
+
else
|
290
|
+
Enumerator.new(self, :each_value)
|
278
291
|
end
|
279
292
|
end
|
280
293
|
|
data/lib/multiset.rb
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
|
4
4
|
require "enumerator"
|
5
5
|
require "multimap"
|
6
|
-
VERSION = "0.4.
|
6
|
+
VERSION = "0.4.1"
|
7
7
|
|
8
8
|
#==概要(Basic information)
|
9
9
|
#
|
@@ -18,7 +18,7 @@ VERSION = "0.4.0"
|
|
18
18
|
# Unlike ordinary set(see Ruby documentation for "set" library),
|
19
19
|
# multiset can contain two or more same items.
|
20
20
|
#
|
21
|
-
# Most methods
|
21
|
+
# Most methods’ names are same as those of Set class, and all other than
|
22
22
|
# a few methods in Set class is implemented on Multiset class.
|
23
23
|
#
|
24
24
|
# * <code>Set[:a,:b,:c,:b,:b,:c] => #<Set: {:b, :c, :a}></code>
|
@@ -583,10 +583,14 @@ class Multiset
|
|
583
583
|
# a Multiset with 100 times "a" will call the given block for 100 times for Multiset#each,
|
584
584
|
# while only once for Multiset#each_pair.
|
585
585
|
def each
|
586
|
-
|
587
|
-
|
586
|
+
if block_given?
|
587
|
+
@entries.each_pair do |item, count|
|
588
|
+
count.times{ yield item }
|
589
|
+
end
|
590
|
+
self
|
591
|
+
else
|
592
|
+
Enumerator.new(self, :each)
|
588
593
|
end
|
589
|
-
self
|
590
594
|
end
|
591
595
|
|
592
596
|
# <code>self</code>に含まれるすべての要素について、重複を許さずに繰り返します。
|
@@ -597,8 +601,12 @@ class Multiset
|
|
597
601
|
# Returns <code>self</code>.
|
598
602
|
# An Enumerator will be returned if no block is given.
|
599
603
|
def each_item(&block) # :yields: item
|
600
|
-
|
601
|
-
|
604
|
+
if block
|
605
|
+
@entries.each_key(&block)
|
606
|
+
self
|
607
|
+
else
|
608
|
+
@entries.each_key
|
609
|
+
end
|
602
610
|
end
|
603
611
|
|
604
612
|
# <code>self</code>に含まれるすべての要素(重複なし)とその個数について繰り返します。
|
@@ -609,8 +617,12 @@ class Multiset
|
|
609
617
|
# Returns <code>self</code>.
|
610
618
|
# An Enumerator will be returned if no block is given.
|
611
619
|
def each_with_count(&block) # :yields: item, count
|
612
|
-
|
613
|
-
|
620
|
+
if block
|
621
|
+
@entries.each_pair(&block)
|
622
|
+
self
|
623
|
+
else
|
624
|
+
@entries.each_pair
|
625
|
+
end
|
614
626
|
end
|
615
627
|
alias :each_pair :each_with_count
|
616
628
|
|
data/spec/multiset_spec.rb
CHANGED
@@ -201,6 +201,28 @@ describe Multiset do
|
|
201
201
|
result[:count_sum].should == 10
|
202
202
|
end
|
203
203
|
|
204
|
+
it "should return an Enumerator with Multiset#each_****" do
|
205
|
+
@ms.each.should be_kind_of(Enumerator)
|
206
|
+
@ms.each_item.should be_kind_of(Enumerator)
|
207
|
+
@ms.each_pair.should be_kind_of(Enumerator)
|
208
|
+
@ms.each_with_count.should be_kind_of(Enumerator)
|
209
|
+
end
|
210
|
+
|
211
|
+
it "should return an Enumerator with Multiset#each_**** whose behavior is the same as original method" do
|
212
|
+
e = @ms.each
|
213
|
+
e.to_a.should == @ms.to_a
|
214
|
+
|
215
|
+
e = @ms.each_item
|
216
|
+
a = []; @ms.each_item{ |x| a << x }
|
217
|
+
b = []; e.each{ |x| b << x }
|
218
|
+
b.should == a
|
219
|
+
|
220
|
+
e = @ms.each_with_count
|
221
|
+
a = []; @ms.each_with_count{ |x, i| a << [x, i] }
|
222
|
+
b = []; e.each{ |x, i| b << [x, i] }
|
223
|
+
b.should == a
|
224
|
+
end
|
225
|
+
|
204
226
|
it "should return a new Multiset by the specified condition with Multiset#map / Multiset#collect" do
|
205
227
|
@ms.map{ |item| item * 2 }.should == Multiset.new(%w'aa aa bb bb bb bb cc dd dd dd')
|
206
228
|
end
|
@@ -298,7 +320,7 @@ describe Multiset do
|
|
298
320
|
tmp.should == Multiset.new(%w'b b b c c d')
|
299
321
|
|
300
322
|
tmp = @ms1.dup
|
301
|
-
tmp.delete_all("e") # nothing deleted because
|
323
|
+
tmp.delete_all("e") # nothing deleted because ``tmp'' does not contain "e"
|
302
324
|
tmp.should == @ms1
|
303
325
|
end
|
304
326
|
end
|
@@ -391,6 +413,37 @@ describe Multimap do
|
|
391
413
|
@mm[:b] = ["foo", "bar", "hoge", "hoge", "bar"]
|
392
414
|
end
|
393
415
|
|
416
|
+
it "should return an Enumerator with Multimap#each_****" do
|
417
|
+
@mm.each.should be_kind_of(Enumerator)
|
418
|
+
@mm.each_pair.should be_kind_of(Enumerator)
|
419
|
+
@mm.each_key.should be_kind_of(Enumerator)
|
420
|
+
@mm.each_value.should be_kind_of(Enumerator)
|
421
|
+
@mm.each_pair_list.should be_kind_of(Enumerator)
|
422
|
+
@mm.each_pair_with.should be_kind_of(Enumerator)
|
423
|
+
end
|
424
|
+
|
425
|
+
it "should return an Enumerator with Multimap#each_**** whose behavior is the same as original method" do
|
426
|
+
e = @mm.each_key
|
427
|
+
a = []; @mm.each_key{ |k| a << k }
|
428
|
+
b = []; e.each{ |k| b << k }
|
429
|
+
b.should == a
|
430
|
+
|
431
|
+
e = @mm.each_value
|
432
|
+
a = []; @mm.each_value{ |v| a << v }
|
433
|
+
b = []; e.each{ |v| b << v }
|
434
|
+
b.should == a
|
435
|
+
|
436
|
+
e = @mm.each_pair_list
|
437
|
+
a = []; @mm.each_pair_list{ |k, v| a << [k, v] }
|
438
|
+
b = []; e.each{ |k, v| b << [k, v] }
|
439
|
+
b.should == a
|
440
|
+
|
441
|
+
e = @mm.each_pair_with
|
442
|
+
a = []; @mm.each_pair_with{ |k, v, c| a << [k, v, c] }
|
443
|
+
b = []; e.each{ |k, v, c| b << [k, v, c] }
|
444
|
+
b.should == a
|
445
|
+
end
|
446
|
+
|
394
447
|
it "should be correctly iterated by 'each_pair'" do
|
395
448
|
tmp_a = []
|
396
449
|
tmp_b = []
|
metadata
CHANGED
@@ -1,48 +1,43 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: multiset
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
5
|
-
prerelease:
|
4
|
+
version: 0.4.1
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- H.Hiro(Maraigue)
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2013-12-01 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: rdoc
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
17
|
- - ~>
|
20
18
|
- !ruby/object:Gem::Version
|
21
|
-
version: '
|
19
|
+
version: '4.0'
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
24
|
- - ~>
|
28
25
|
- !ruby/object:Gem::Version
|
29
|
-
version: '
|
26
|
+
version: '4.0'
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: hoe
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
31
|
- - ~>
|
36
32
|
- !ruby/object:Gem::Version
|
37
|
-
version: '3.
|
33
|
+
version: '3.6'
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
38
|
- - ~>
|
44
39
|
- !ruby/object:Gem::Version
|
45
|
-
version: '3.
|
40
|
+
version: '3.6'
|
46
41
|
description: ! "Unlike ordinary set(see Ruby documentation for \"set\" library), multiset
|
47
42
|
can contain two or more same items.\n\n Set[:a,:b,:c,:b,:b,:c] # => #<Set: {:b,
|
48
43
|
:c, :a}>\n Multiset[:a,:b,:c,:b,:b,:c] # => #<Multiset:#3 :b, #2 :c, #1 :a>\n\nMultisets
|
@@ -66,6 +61,7 @@ files:
|
|
66
61
|
- .gemtest
|
67
62
|
homepage: http://maraigue.hhiro.net/multiset/
|
68
63
|
licenses: []
|
64
|
+
metadata: {}
|
69
65
|
post_install_message:
|
70
66
|
rdoc_options:
|
71
67
|
- --main
|
@@ -73,22 +69,20 @@ rdoc_options:
|
|
73
69
|
require_paths:
|
74
70
|
- lib
|
75
71
|
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
-
none: false
|
77
72
|
requirements:
|
78
73
|
- - ! '>='
|
79
74
|
- !ruby/object:Gem::Version
|
80
75
|
version: '0'
|
81
76
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
|
-
none: false
|
83
77
|
requirements:
|
84
78
|
- - ! '>='
|
85
79
|
- !ruby/object:Gem::Version
|
86
80
|
version: '0'
|
87
81
|
requirements: []
|
88
82
|
rubyforge_project: multiset
|
89
|
-
rubygems_version:
|
83
|
+
rubygems_version: 2.0.3
|
90
84
|
signing_key:
|
91
|
-
specification_version:
|
85
|
+
specification_version: 4
|
92
86
|
summary: Unlike ordinary set(see Ruby documentation for "set" library), multiset can
|
93
87
|
contain two or more same items
|
94
88
|
test_files: []
|