multiset 0.5.1 → 0.5.3

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.
@@ -3,70 +3,84 @@
3
3
 
4
4
  require "enumerator"
5
5
  require "multimap"
6
- VERSION = "0.5.1"
7
6
 
8
- #==概要(Basic information)
7
+ #==Basic information 概要
9
8
  #
10
- # Rubyによる多重集合(マルチセット)の実装です。
11
- # 通常の集合(Rubyでは"set"ライブラリ)と異なり、多重集合は
12
- # 同一の要素を複数格納することができます。
13
- #
14
- # メソッド名は基本的にSetクラスに合わせてあります。またSetクラスが持つ
15
- # メソッドの大部分を実装していますが、いくつか未実装なものもあります。
16
- #
17
- # Ruby implementation of multiset.
18
- # Unlike ordinary set(see Ruby documentation for "set" library),
9
+ # A Ruby implementation of multiset.
10
+ # Unlike ordinary set (see Ruby documentation for "set" library),
19
11
  # multiset can contain two or more same items.
20
12
  #
21
- # Most methods’ names are same as those of Set class, and all other than
22
- # a few methods in Set class is implemented on Multiset class.
13
+ # Methods' names are basically consistent with those of Set class.
23
14
  #
24
15
  # * <code>Set[:a,:b,:c,:b,:b,:c] => #<Set: {:b, :c, :a}></code>
25
16
  # * <code>Multiset[:a,:b,:c,:b,:b,:c] => #<Multiset:<tt>#</tt>3 :b, <tt>#</tt>2 :c, <tt>#</tt>1 :a></code>
17
+ #
18
+ # Rubyによる多重集合(マルチセット)の実装です。
19
+ # 通常の集合(Rubyでは"set"ライブラリ)と異なり、多重集合は
20
+ # 同一の要素を複数格納することができます。
21
+ #
22
+ # メソッド名は基本的にSetクラスに合わせてあります。
26
23
 
27
24
  class Multiset
25
+ VERSION = "0.5.3"
26
+
28
27
  include Enumerable
29
28
 
30
29
  #--
31
30
  # ============================================================
31
+ # Constructor
32
32
  # コンストラクタ
33
33
  # ============================================================
34
34
  #++
35
35
 
36
+ # Generates a multiset from items in <code>list</code>.
37
+ # If <code>list</code> is omitted, returns empty multiset.
38
+ #
39
+ # <code>list</code> must be an object including <code>Enumerable</code>.
40
+ # Otherwise, <code>ArgumentError</code> is raised.
41
+ #
36
42
  # <code>list</code>に含まれる要素からなる多重集合を生成します。
37
43
  # <code>list</code>を省略した場合、空の多重集合を生成します。
38
44
  #
39
45
  # <code>list</code>には<code>Enumerable</code>であるオブジェクトのみ
40
46
  # 指定できます。そうでない場合、例外<code>ArgumentError</code>が
41
47
  # 発生します。
42
- #
43
- # Generates a multiset from items in <code>list</code>.
44
- # If <code>list</code> is omitted, returns empty multiset.
45
- #
46
- # <code>list</code> must be <code>Enumerable</code>. If not,
47
- # <code>ArgumentError</code> is raised.
48
48
  def initialize(list = nil)
49
49
  @entries = {}
50
50
  if list.kind_of?(Enumerable)
51
51
  list.each{ |item| add item }
52
52
  elsif list != nil
53
- raise ArgumentError, "Item list must include 'Enumerable' module"
53
+ raise ArgumentError, "Item list must be an instance including 'Enumerable' module"
54
54
  end
55
55
  end
56
56
 
57
- # <code>list</code>に含まれる要素からなる多重集合を生成します。
58
- # <code>new</code>を用いる場合と異なり、引数の1つ1つが多重集合の要素になります。
57
+ # Generates a multiset from items in <code>list</code>.
58
+ # Unlike using <code>Multiset.new</code>, each argument is one item in generated multiset.
59
59
  #
60
- # 主に多重集合のリテラルを生成するのに用います。
60
+ # This method is mainly used when you generate a multiset from literals.
61
61
  #
62
- # Generates a multiset from items in <code>list</code>.
63
- # Unlike using <code>new</code>, each argument is one item in generated multiset.
62
+ # <code>list</code>に含まれる要素からなる多重集合を生成します。
63
+ # <code>Multiset.new</code>を用いる場合と異なり、引数の1つ1つが多重集合の要素になります。
64
64
  #
65
- # This method is mainly used when you generate literal of multiset.
65
+ # 主に、リテラルから多重集合を生成するのに用います。
66
66
  def Multiset.[](*list)
67
67
  Multiset.new(list)
68
68
  end
69
69
 
70
+ # Generates a multiset by converting <code>object</code>.
71
+ # * If <code>object</code> is an instance of Multiset, returns
72
+ # duplicated <code>object</code>.
73
+ # * If <code>object</code> is not an instance of Multiset and has
74
+ # the method <code>each_pair</code>,
75
+ # for each pair of two arguments from <code>each_pair</code>,
76
+ # the first argument becomes the item in multiset and
77
+ # the second argument becomes its number in the multiset.
78
+ # See also Hash#to_multiset .
79
+ # * If <code>object</code> does not have the method <code>each_pair</code>
80
+ # and <code>object</code> includes <code>Enumerable</code>, this method
81
+ # works the same as Multiset#new .
82
+ # * Otherwise, <code>ArgumentError</code> is raised.
83
+ #
70
84
  # <code>object</code>を多重集合に変換し生成します。
71
85
  # * <code>object</code>がMultisetのインスタンスである場合、
72
86
  # その複製を返します。
@@ -78,19 +92,6 @@ class Multiset
78
92
  # * <code>object</code>が<code>each_pair</code>メソッドを持っておらず、
79
93
  # かつ<code>Enumerable</code>である場合は、Multiset#newと同じ結果です。
80
94
  # * それ以外の場合は、例外<code>ArgumentError</code>が発生します。
81
- #
82
- # Generates a multiset converting <code>object</code>.
83
- # * If <code>object</code> is an instance of Multiset, returns
84
- # duplicated <code>object</code>.
85
- # * If <code>object</code> is not an instance of Multiset and has
86
- # the method <code>each_pair</code>,
87
- # for each pair of two arguments from <code>each_pair</code>,
88
- # first argument becomes item in multiset and second argument
89
- # becomes its number. See also Hash#to_multiset .
90
- # * If <code>object</code> does not have the method <code>each_pair</code>
91
- # and <code>object</code> includes <code>Enumerable</code>, this method
92
- # results equal to Multiset#new .
93
- # * Otherwise, <code>ArgumentError</code> is raised.
94
95
  def Multiset.parse(object)
95
96
  if object.kind_of?(String)
96
97
  raise ArgumentError, "Multiset.parse can not parse strings. If you would like to store string lines to a multiset, use Multiset.from_lines(string)."
@@ -111,18 +112,18 @@ class Multiset
111
112
  ret
112
113
  end
113
114
 
114
- # 文字列を行単位で区切ってMultisetにします。
115
- #
116
115
  # Generates a Multiset from string, separated by lines.
116
+ #
117
+ # 文字列を行単位で区切ってMultisetにします。
117
118
  def Multiset.from_lines(str)
118
119
  Multiset.new(str.enum_for(:each_line))
119
120
  end
120
121
 
121
- # 文字列が渡された場合は、Multiset.from_linesと同じ挙動。
122
- # それ以外の場合は、Multiset.parseと同じ挙動。
123
- #
124
122
  # If a string is given, it works as Multiset.from_lines,
125
123
  # otherwise as Multiset.parse.
124
+ #
125
+ # 文字列が渡された場合は、Multiset.from_linesと同じ挙動。
126
+ # それ以外の場合は、Multiset.parseと同じ挙動。
126
127
  def Multiset.parse_force(object)
127
128
  if object.kind_of?(String)
128
129
  Multiset.from_lines(object)
@@ -131,50 +132,52 @@ class Multiset
131
132
  end
132
133
  end
133
134
 
134
- # <code>self</code>の複製を生成して返します。
135
- #
136
135
  # Returns duplicated <code>self</code>.
136
+ #
137
+ # <code>self</code>の複製を生成して返します。
137
138
  def dup
138
139
  @entries.to_multiset
139
140
  end
140
141
 
141
- # <code>self</code>を<code>Hash</code>に変換して返します。
142
- # 生成されるハッシュの構造については、Hash#to_multisetをご覧下さい。
143
- #
144
142
  # Converts <code>self</code> to a <code>Hash</code>.
145
143
  # See Hash#to_multiset about format of generated hash.
144
+ #
145
+ # <code>self</code>を<code>Hash</code>に変換して返します。
146
+ # 生成されるハッシュの構造については、Hash#to_multisetをご覧下さい。
146
147
  def to_hash
147
148
  @entries.dup
148
149
  end
149
150
 
150
151
  #--
151
152
  # ============================================================
153
+ # Basic functions such as converting into other types
152
154
  # 別の型への変換、基本的な関数など
153
155
  # ============================================================
154
156
  #++
155
157
 
156
- # <code>self</code>を通常の集合(Ruby標準添付の<code>Set</code>)に
157
- # 変換したものを返します。
158
- #
159
- # このメソッドを呼び出すと、<code>require "set"</code>が行われます。
160
- #
161
- # なおSetをMultisetに変換するには、<code>Multiset.new(instance_of_set)</code>で
162
- # 可能です。
163
- #
164
158
  # Converts <code>self</code> to ordinary set
165
159
  # (The <code>Set</code> class attached to Ruby by default).
166
160
  #
167
161
  # <code>require "set"</code> is performed when this method is called.
168
162
  #
169
- # To convert Set to Multiset, use <code>Multiset.new(instance_of_set)</code>.
163
+ # Note: To convert an instance of Set to Multiset, use
164
+ # <code>Multiset.new(instance_of_set)</code>.
165
+ #
166
+ # <code>self</code>を通常の集合(Ruby標準添付の<code>Set</code>)に
167
+ # 変換したものを返します。
168
+ #
169
+ # このメソッドを呼び出すと、<code>require "set"</code>が行われます。
170
+ #
171
+ # 注:逆に、SetのインスタンスをMultisetに変換するには、
172
+ # <code>Multiset.new(instance_of_set)</code>で可能です。
170
173
  def to_set
171
174
  require "set"
172
175
  Set.new(@entries.keys)
173
176
  end
174
177
 
175
- # <code>self</code>を配列に変換して返します。
176
- #
177
178
  # Converts <code>self</code> to an array.
179
+ #
180
+ # <code>self</code>を配列に変換して返します。
178
181
  def to_a
179
182
  ret = []
180
183
  @entries.each_pair do |item, count|
@@ -201,15 +204,16 @@ class Multiset
201
204
 
202
205
  #--
203
206
  # ============================================================
207
+ # Basic operations such as ones required for other methods
204
208
  # 基本操作(他のメソッドを定義するのに頻出するメソッドなど)
205
209
  # ============================================================
206
210
  #++
207
211
 
208
- # <code>self</code>の内容を<code>other</code>のものに置き換えます。
209
- # <code>self</code>を返します。
210
- #
211
212
  # Replaces <code>self</code> by <code>other</code>.
212
213
  # Returns <code>self</code>.
214
+ #
215
+ # <code>self</code>の内容を<code>other</code>のものに置き換えます。
216
+ # <code>self</code>を返します。
213
217
  def replace(other)
214
218
  @entries.clear
215
219
  other.each_pair do |item, count|
@@ -218,54 +222,54 @@ class Multiset
218
222
  self
219
223
  end
220
224
 
221
- # <code>self</code>に含まれている要素数を返します。
222
- #
223
225
  # Returns number of all items in <code>self</code>.
226
+ #
227
+ # <code>self</code>に含まれている要素数を返します。
224
228
  def size
225
229
  @entries.inject(0){ |sum, item| sum += item[1] }
226
230
  end
227
231
  alias length size
228
232
 
229
- # <code>self</code>に要素がないかどうかを返します。
230
- #
231
233
  # Returns whether <code>self</code> has no item.
234
+ #
235
+ # <code>self</code>に要素がないかどうかを返します。
232
236
  def empty?
233
237
  @entries.empty?
234
238
  end
235
239
 
236
- # <code>self</code>に含まれている要素(重複は除く)からなる配列を返します。
237
- #
238
240
  # Returns an array with all items in <code>self</code>, without duplication.
241
+ #
242
+ # <code>self</code>に含まれている要素(重複は除く)からなる配列を返します。
239
243
  def items
240
244
  @entries.keys
241
245
  end
242
246
 
243
- # <code>self</code>の要素をすべて削除します。
244
- # <code>self</code>を返します。
245
- #
246
247
  # Deletes all items in <code>self</code>.
247
248
  # Returns <code>self</code>.
249
+ #
250
+ # <code>self</code>の要素をすべて削除します。
251
+ # <code>self</code>を返します。
248
252
  def clear
249
253
  @entries.clear
250
254
  self
251
255
  end
252
256
 
253
- # <code>item</code>が<code>self</code>中に含まれているかを返します。
254
- #
255
257
  # Returns whether <code>self</code> has <code>item</code>.
258
+ #
259
+ # <code>item</code>が<code>self</code>中に含まれているかを返します。
256
260
  def include?(item)
257
261
  @entries.has_key?(item)
258
262
  end
259
263
  alias member? include?
260
264
 
261
- # <code>self</code>の全要素を(重複を許して)並べた文字列を返します。
262
- # 要素間の区切りは<code>delim</code>の値を用い、
263
- # 各要素の表示形式は与えられたブロックの返り値(なければObject#inspect)を用います。
264
- #
265
265
  # Lists all items with duplication in <code>self</code>.
266
266
  # Items are deliminated with <code>delim</code>, and items are
267
267
  # converted to string in the given block.
268
268
  # If block is omitted, Object#inspect is used.
269
+ #
270
+ # <code>self</code>の全要素を(重複を許して)並べた文字列を返します。
271
+ # 要素間の区切りは<code>delim</code>の値を用い、
272
+ # 各要素の表示形式は与えられたブロックの返り値(なければObject#inspect)を用います。
269
273
  def listing(delim = "\n")
270
274
  buf = ''
271
275
  init = true
@@ -280,14 +284,14 @@ class Multiset
280
284
  buf
281
285
  end
282
286
 
283
- # <code>self</code>の要素と要素数の組を並べた文字列を返します。
284
- # 要素間の区切りは<code>delim</code>の値を用い、
285
- # 各要素の表示形式は与えられたブロックの返り値(なければObject#inspect)を用います。
286
- #
287
287
  # Lists all items without duplication and its number in <code>self</code>.
288
288
  # Items are deliminated with <code>delim</code>, and items are
289
289
  # converted to string in the given block.
290
290
  # If block is omitted, Object#inspect is used.
291
+ #
292
+ # <code>self</code>の要素と要素数の組を並べた文字列を返します。
293
+ # 要素間の区切りは<code>delim</code>の値を用い、
294
+ # 各要素の表示形式は与えられたブロックの返り値(なければObject#inspect)を用います。
291
295
  def to_s(delim = "\n")
292
296
  buf = ''
293
297
  init = true
@@ -312,21 +316,22 @@ class Multiset
312
316
 
313
317
  #--
314
318
  # ============================================================
315
- # 要素数の更新
319
+ # The number of elements in a multiset
320
+ # マルチセットの要素数
316
321
  # ============================================================
317
322
  #++
318
323
 
319
- # <code>self</code>中に含まれる<code>item</code>の個数を返します。
320
- # 引数を指定しない場合は、Multiset#sizeと同じです。
321
- # ブロックを指定することもでき、その場合は(重複しない)各要素をブロックに与え、
322
- # 条件を満たした(結果が真であった)要素がMultiset内にいくつ入っているかを数えます。
323
- #
324
324
  # Returns number of <code>item</code>s in <code>self</code>.
325
325
  # If the <code>item</code> is omitted, the value is same as Multiset#size.
326
326
  # If a block is given, each element (without duplication) is given to
327
327
  # the block, and returns the number of elements (including duplication)
328
328
  # that returns true in the block.
329
- #
329
+ #
330
+ # <code>self</code>中に含まれる<code>item</code>の個数を返します。
331
+ # 引数を指定しない場合は、Multiset#sizeと同じです。
332
+ # ブロックを指定することもでき、その場合は(重複しない)各要素をブロックに与え、
333
+ # 条件を満たした(結果が真であった)要素がMultiset内にいくつ入っているかを数えます。
334
+ #
330
335
  # :call-seq:
331
336
  # count(item)
332
337
  # count{ |item| ... }
@@ -353,13 +358,13 @@ class Multiset
353
358
  end
354
359
  end
355
360
 
361
+ # Sets the number of <code>item</code> in <code>self</code> as <code>number</code>.
362
+ # If <code>number</code> is negative, it is considered as <code>number = 0</code>.
363
+ # Returns <code>self</code> if succeeded, <code>nil</code> otherwise.
364
+ #
356
365
  # <code>self</code>に含まれる<code>item</code>の個数を<code>number</code>個にします。
357
366
  # <code>number</code>が負の数であった場合は、<code>number = 0</code>とみなします。
358
367
  # 成功した場合は<code>self</code>を、失敗した場合は<code>nil</code>を返します。
359
- #
360
- # Sets number of <code>item</code> to <code>number</code> in <code>self</code>.
361
- # If <code>number</code> is negative, treats as <code>number = 0</code>.
362
- # Returns <code>self</code> if succeeded, <code>nil</code> otherwise.
363
368
  def renew_count(item, number)
364
369
  return nil if number == nil
365
370
  n = number.to_i
@@ -371,11 +376,11 @@ class Multiset
371
376
  self
372
377
  end
373
378
 
374
- # <code>self</code>に、<code>addcount</code>個の<code>item</code>を追加します。
375
- # 成功した場合は<code>self</code>を、失敗した場合は<code>nil</code>を返します。
376
- #
377
379
  # Adds <code>addcount</code> number of <code>item</code>s to <code>self</code>.
378
380
  # Returns <code>self</code> if succeeded, or <code>nil</code> if failed.
381
+ #
382
+ # <code>self</code>に、<code>addcount</code>個の<code>item</code>を追加します。
383
+ # 成功した場合は<code>self</code>を、失敗した場合は<code>nil</code>を返します。
379
384
  def add(item, addcount = 1)
380
385
  return nil if addcount == nil
381
386
  a = addcount.to_i
@@ -384,12 +389,12 @@ class Multiset
384
389
  end
385
390
  alias << add
386
391
 
387
- # <code>self</code>から、<code>delcount</code>個の<code>item</code>を削除します。
388
- # 成功した場合は<code>self</code>を、失敗した場合は<code>nil</code>を返します。
389
- #
390
392
  # Deletes <code>delcount</code> number of <code>item</code>s
391
393
  # from <code>self</code>.
392
394
  # Returns <code>self</code> if succeeded, <code>nil</code> otherwise.
395
+ #
396
+ # <code>self</code>から、<code>delcount</code>個の<code>item</code>を削除します。
397
+ # 成功した場合は<code>self</code>を、失敗した場合は<code>nil</code>を返します。
393
398
  def delete(item, delcount = 1)
394
399
  return nil if delcount == nil || !self.include?(item)
395
400
  d = delcount.to_i
@@ -397,11 +402,11 @@ class Multiset
397
402
  self.renew_count(item, self.count(item) - d)
398
403
  end
399
404
 
400
- # <code>self</code>に含まれる<code>item</code>をすべて削除します。
401
- # <code>self</code>を返します。
402
- #
403
405
  # Deletes all <code>item</code>s in <code>self</code>.
404
406
  # Returns <code>self</code>.
407
+ #
408
+ # <code>self</code>に含まれる<code>item</code>をすべて削除します。
409
+ # <code>self</code>を返します。
405
410
  def delete_all(item)
406
411
  @entries.delete(item)
407
412
  self
@@ -409,22 +414,23 @@ class Multiset
409
414
 
410
415
  #--
411
416
  # ============================================================
417
+ # Relationships about inclusions
412
418
  # 包含関係の比較
413
419
  # ============================================================
414
420
  #++
415
421
 
416
- # <code>self</code>と<code>other</code>が持つすべての要素(重複なし)について
417
- # 繰り返し、ブロックの返り値が偽であるものが存在すればその時点でfalseを返します。
418
- # すべての要素について真であればtrueを返します。
419
- #
420
- # このメソッドはsuperset?、subset?、== のために定義されています。
421
- #
422
422
  # Iterates for each item in <code>self</code> and <code>other</code>,
423
423
  # without duplication. If the given block returns false, then iteration
424
424
  # immediately ends and returns false.
425
425
  # Returns true if the given block returns true for all of iteration.
426
426
  #
427
427
  # This method is defined for methods superset?, subset?, ==.
428
+ #
429
+ # <code>self</code>と<code>other</code>が持つすべての要素(重複なし)について
430
+ # 繰り返し、ブロックの返り値が偽であるものが存在すればその時点でfalseを返します。
431
+ # すべての要素について真であればtrueを返します。
432
+ #
433
+ # このメソッドはsuperset?、subset?、== のために定義されています。
428
434
  def compare_set_with(other) # :nodoc: :yields: number_in_self, number_in_other
429
435
  (self.items | other.items).each do |item|
430
436
  return false unless yield(self.count(item), other.count(item))
@@ -432,9 +438,13 @@ class Multiset
432
438
  true
433
439
  end
434
440
 
435
- # <code>self</code>が<code>other</code>を含んでいるかどうかを返します。
441
+ # Returns whether <code>self</code> is a superset of <code>other</code>,
442
+ # that is, for any item, the number of it in <code>self</code> is
443
+ # equal to or larger than that in <code>other</code>.
436
444
  #
437
- # Returns whether <code>self</code> is a superset of <code>other</code>.
445
+ # <code>self</code>が<code>other</code>を含んでいるかどうかを返します。
446
+ # すなわち、いかなる要素についても、それが<code>self</code>に含まれている
447
+ # 個数が<code>other</code>に含まれている数以上であるかを返します。
438
448
  def superset?(other)
439
449
  unless other.instance_of?(Multiset)
440
450
  raise ArgumentError, "Argument must be a Multiset"
@@ -442,20 +452,26 @@ class Multiset
442
452
  compare_set_with(other){ |s, o| s >= o }
443
453
  end
444
454
 
455
+ # Returns whether <code>self</code> is a proper superset of <code>other</code>,
456
+ # that is, it returns true if superset? is satisfied and
457
+ # <code>self</code> is not equal to <code>other</code>.
458
+ #
445
459
  # <code>self</code>が<code>other</code>を真に含んでいるかどうかを返します。
446
- # 「真に」とは、両者が一致する場合は含めないことを示します。
447
- #
448
- # Returns whether <code>self</code> is a proper superset of <code>other</code>.
460
+ # すなわち、 superset? の条件に加えて両者が一致しなければ真となります。
449
461
  def proper_superset?(other)
450
462
  unless other.instance_of?(Multiset)
451
463
  raise ArgumentError, "Argument must be a Multiset"
452
464
  end
453
465
  self.superset?(other) && self != other
454
466
  end
455
-
456
- # <code>self</code>が<code>other</code>に含まれているかどうかを返します。
467
+
468
+ # Returns whether <code>self</code> is a subset of <code>other</code>,
469
+ # that is, for any item, the number of it in <code>self</code> is
470
+ # equal to or smaller than that in <code>other</code>.
457
471
  #
458
- # Returns whether <code>self</code> is a subset of <code>other</code>.
472
+ # <code>self</code>が<code>other</code>を含んでいるかどうかを返します。
473
+ # すなわち、いかなる要素についても、それが<code>self</code>に含まれている
474
+ # 個数が<code>other</code>に含まれている数以下であるかを返します。
459
475
  def subset?(other)
460
476
  unless other.instance_of?(Multiset)
461
477
  raise ArgumentError, "Argument must be a Multiset"
@@ -463,10 +479,12 @@ class Multiset
463
479
  compare_set_with(other){ |s, o| s <= o }
464
480
  end
465
481
 
482
+ # Returns whether <code>self</code> is a proper subset of <code>other</code>,
483
+ # that is, it returns true if subset? is satisfied and
484
+ # <code>self</code> is not equal to <code>other</code>.
485
+ #
466
486
  # <code>self</code>が<code>other</code>に真に含まれているかどうかを返します。
467
- # 「真に」とは、両者が一致する場合は含めないことを示します。
468
- #
469
- # Returns whether <code>self</code> is a proper subset of <code>other</code>.
487
+ # すなわち、 subset? の条件に加えて両者が一致しなければ真となります。
470
488
  def proper_subset?(other)
471
489
  unless other.instance_of?(Multiset)
472
490
  raise ArgumentError, "Argument must be a Multiset"
@@ -474,9 +492,9 @@ class Multiset
474
492
  self.subset?(other) && self != other
475
493
  end
476
494
 
477
- # <code>self</code>が<code>other</code>と等しいかどうかを返します。
478
- #
479
495
  # Returns whether <code>self</code> is equal to <code>other</code>.
496
+ #
497
+ # <code>self</code>が<code>other</code>と等しいかどうかを返します。
480
498
  def ==(other)
481
499
  return false unless other.instance_of?(Multiset)
482
500
  compare_set_with(other){ |s, o| s == o }
@@ -484,13 +502,14 @@ class Multiset
484
502
 
485
503
  #--
486
504
  # ============================================================
505
+ # Other operations for two multisets
487
506
  # その他、2つのMultisetについての処理
488
507
  # ============================================================
489
508
  #++
490
509
 
491
- # <code>self</code>と<code>other</code>の要素を合わせた多重集合を返します。
510
+ # Returns a multiset merging <code>self</code> and <code>other</code>.
492
511
  #
493
- # Returns merged multiset of <code>self</code> and <code>other</code>.
512
+ # <code>self</code>と<code>other</code>の要素を合わせた多重集合を返します。
494
513
  def merge(other)
495
514
  ret = self.dup
496
515
  other.each_pair do |item, count|
@@ -500,11 +519,13 @@ class Multiset
500
519
  end
501
520
  alias + merge
502
521
 
503
- # <code>self</code>に<code>other</code>の要素を追加します。
504
- # <code>self</code>を返します。
505
- #
506
522
  # Merges <code>other</code> to <code>self</code>.
523
+ # See also Multiset#merge .
507
524
  # Returns <code>self</code>.
525
+ #
526
+ # <code>self</code>に<code>other</code>の要素を追加します。
527
+ # Multiset#merge も参照してください。
528
+ # <code>self</code>を返します。
508
529
  def merge!(other)
509
530
  other.each_pair do |item, count|
510
531
  self.add(item, count)
@@ -512,9 +533,13 @@ class Multiset
512
533
  self
513
534
  end
514
535
 
515
- # <code>self</code>から<code>other</code>の要素を取り除いた多重集合を返します。
536
+ # Returns a multiset such that items in <code>other</code> are removed from <code>self</code>,
537
+ # where 'removed' means that, for each item in <code>other</code>,
538
+ # the items of the number in <code>other</code> are removed from <code>self</code>.
516
539
  #
517
- # Returns multiset such that items in <code>other</code> are removed from <code>self</code>.
540
+ # <code>self</code>から<code>other</code>の要素を取り除いた多重集合を返します。
541
+ # ここで「取り除く」ことは、<code>other</code>の各要素について、
542
+ # それを<code>other</code>にある個数分<code>self</code>から取り除くことをいいます。
518
543
  def subtract(other)
519
544
  ret = self.dup
520
545
  other.each_pair do |item, count|
@@ -524,11 +549,13 @@ class Multiset
524
549
  end
525
550
  alias - subtract
526
551
 
527
- # <code>self</code>から<code>other</code>の要素を削除します。
528
- # <code>self</code>を返します。
529
- #
530
552
  # Removes items in <code>other</code> from <code>self</code>.
553
+ # See also Multiset#subtract .
531
554
  # Returns <code>self</code>.
555
+ #
556
+ # <code>self</code>から<code>other</code>の要素を削除します。
557
+ # Multiset#subtract も参照してください。
558
+ # <code>self</code>を返します。
532
559
  def subtract!(other)
533
560
  other.each_pair do |item, count|
534
561
  self.delete(item, count)
@@ -536,9 +563,13 @@ class Multiset
536
563
  self
537
564
  end
538
565
 
539
- # <code>self</code>と<code>other</code>の積集合からなる多重集合を返します。
566
+ # Returns the intersection of <code>self</code> and <code>other</code>,
567
+ # that is, for each item both in <code>self</code> and <code>other</code>,
568
+ # the multiset includes it in the smaller number of the two.
540
569
  #
541
- # Returns intersection of <code>self</code> and <code>other</code>.
570
+ # <code>self</code>と<code>other</code>の積集合からなる多重集合を返します。
571
+ # すなわち、<code>self</code>と<code>other</code>の両方に存在する要素について、
572
+ # 少ないほうの個数を持った多重集合を返します。
542
573
  def &(other)
543
574
  ret = Multiset.new
544
575
  (self.items & other.items).each do |item|
@@ -547,9 +578,13 @@ class Multiset
547
578
  ret
548
579
  end
549
580
 
550
- # <code>self</code>と<code>other</code>の和集合からなる多重集合を返します。
581
+ # Returns the union of <code>self</code> and <code>other</code>,
582
+ # that is, for each item either or both in <code>self</code> and <code>other</code>,
583
+ # the multiset includes it in the larger number of the two.
551
584
  #
552
- # Returns union of <code>self</code> and <code>other</code>.
585
+ # <code>self</code>と<code>other</code>の和集合からなる多重集合を返します。
586
+ # すなわち、<code>self</code>と<code>other</code>の少なくとも一方に存在する要素について、
587
+ # 多いほうの個数を持った多重集合を返します。
553
588
  def |(other)
554
589
  ret = self.dup
555
590
  other.each_pair do |item, count|
@@ -560,28 +595,30 @@ class Multiset
560
595
 
561
596
  #--
562
597
  # ============================================================
598
+ # Processes for single multiset
563
599
  # 1つのMultisetの各要素についての処理
564
600
  # ============================================================
565
601
  #++
566
602
 
567
- # <code>self</code>に含まれるすべての要素について繰り返します。
568
- # <code>self</code>を返します。
569
- # ブロックが与えられていない場合、Enumeratorを返します。
570
- #
571
- # このメソッドは Enumerable#each の挙動に合わせ、同じ要素を何度もブロックに
572
- # 渡すため、効率が悪いです。Multiset#each_item, Multiset#each_pairの利用もご検討下さい。
573
- # 例えば「"a"が100個入ったMultiset」をeachで繰り返すと100回の処理が行われますが、
574
- # each_pairなら1回で済みます。
575
- #
576
603
  # Iterates for each item in <code>self</code>.
577
604
  # Returns <code>self</code>.
578
605
  # An Enumerator will be returned if no block is given.
579
606
  #
580
607
  # This method is ineffective since the same element in the Multiset
581
- # can be given to the block for many times, same as the behavior of Enumerable#each.
608
+ # can be given to the block for many times,
609
+ # so that it behaves the same as Enumerable#each.
582
610
  # Please consider using Multiset#each_item or Multiset#each_pair: for example,
583
611
  # a Multiset with 100 times "a" will call the given block for 100 times for Multiset#each,
584
612
  # while only once for Multiset#each_pair.
613
+ #
614
+ # <code>self</code>に含まれるすべての要素について繰り返します。
615
+ # <code>self</code>を返します。
616
+ # ブロックが与えられていない場合、Enumeratorを返します。
617
+ #
618
+ # このメソッドは Enumerable#each の挙動に合わせ、同じ要素を何度もブロックに
619
+ # 渡すため、効率が悪いです。Multiset#each_item, Multiset#each_pairの利用もご検討下さい。
620
+ # 例えば「"a"が100個入ったMultiset」をeachで繰り返すと100回の処理が行われますが、
621
+ # each_pairなら1回で済みます。
585
622
  def each
586
623
  if block_given?
587
624
  @entries.each_pair do |item, count|
@@ -593,13 +630,13 @@ class Multiset
593
630
  end
594
631
  end
595
632
 
596
- # <code>self</code>に含まれるすべての要素について、重複を許さずに繰り返します。
597
- # <code>self</code>を返します。
598
- # ブロックが与えられていない場合、Enumeratorを返します。
599
- #
600
633
  # Iterates for each item in <code>self</code>, without duplication.
601
634
  # Returns <code>self</code>.
602
635
  # An Enumerator will be returned if no block is given.
636
+ #
637
+ # <code>self</code>に含まれるすべての要素について、重複を許さずに繰り返します。
638
+ # <code>self</code>を返します。
639
+ # ブロックが与えられていない場合、Enumeratorを返します。
603
640
  def each_item(&block) # :yields: item
604
641
  if block
605
642
  @entries.each_key(&block)
@@ -609,13 +646,13 @@ class Multiset
609
646
  end
610
647
  end
611
648
 
612
- # <code>self</code>に含まれるすべての要素(重複なし)とその個数について繰り返します。
613
- # <code>self</code>を返します。
614
- # ブロックが与えられていない場合、Enumeratorを返します。
615
- #
616
649
  # Iterates for each pair of (non-duplicated) item and its number in <code>self</code>.
617
650
  # Returns <code>self</code>.
618
651
  # An Enumerator will be returned if no block is given.
652
+ #
653
+ # <code>self</code>に含まれるすべての要素(重複なし)とその個数について繰り返します。
654
+ # <code>self</code>を返します。
655
+ # ブロックが与えられていない場合、Enumeratorを返します。
619
656
  def each_with_count(&block) # :yields: item, count
620
657
  if block
621
658
  @entries.each_pair(&block)
@@ -626,11 +663,11 @@ class Multiset
626
663
  end
627
664
  alias :each_pair :each_with_count
628
665
 
629
- # <code>self</code>の各要素(重複なし)をブロックに与え、返り値を集めたものからなる
630
- # 多重集合を生成します。
631
- #
632
666
  # Gives all items in <code>self</code> (without duplication) to given block,
633
667
  # and generates a new multiset whose values are returned value from the block.
668
+ #
669
+ # <code>self</code>の各要素(重複なし)をブロックに与え、返り値を集めたものからなる
670
+ # 多重集合を生成します。
634
671
  def map # :yields: item
635
672
  ret = Multiset.new
636
673
  @entries.each_pair do |item, count|
@@ -640,25 +677,25 @@ class Multiset
640
677
  end
641
678
  alias collect map
642
679
 
643
- # Multiset#mapと同様ですが、結果として生成される多重集合で<code>self</code>が
644
- # 置き換えられます。<code>self</code>を返します。
645
- #
646
- # Same as Multiset#map, but replaces <code>self</code> by resulting multiset.
680
+ # Same as Multiset#map, except that <code>self</code> is replaced by the resulted multiset.
647
681
  # Returns <code>self</code>.
682
+ #
683
+ # Multiset#mapと同様の処理を行いますが、結果として生成される多重集合で<code>self</code>が
684
+ # 置き換えられます。<code>self</code>を返します。
648
685
  def map!(&block) # :yields: item
649
686
  self.replace(self.map(&block))
650
687
  self
651
688
  end
652
689
  alias collect! map!
653
690
 
654
- # <code>self</code>の要素(重複なし)とその個数の組をブロックに与えます。
655
- # ブロックから2要素の配列を受け取り、前者を要素、後者をその個数とした
656
- # 多重集合を生成します。
657
- #
658
- # Gives all pairs of (non-duplicate) items and their numbers in <code>self</code> to
691
+ # Gives all pairs of (non-duplicated) items and their numbers in <code>self</code> to
659
692
  # given block. The block must return an array of two items.
660
693
  # Generates a new multiset whose values and numbers are the first and
661
694
  # second item of returned array, respectively.
695
+ #
696
+ # <code>self</code>の要素(重複なし)とその個数の組をブロックに与えます。
697
+ # ブロックから2要素の配列を受け取り、前者を要素、後者をその個数とした
698
+ # 多重集合を生成します。
662
699
  def map_with
663
700
  ret = Multiset.new
664
701
  @entries.each_pair do |item, count|
@@ -669,11 +706,11 @@ class Multiset
669
706
  end
670
707
  alias collect_with map_with
671
708
 
709
+ # Same as Multiset#map_with, except that <code>self</code> by
710
+ # the resulted multiset. Returns <code>self</code>.
711
+ #
672
712
  # Multiset#map_withと同様ですが、結果として生成される多重集合で
673
713
  # <code>self</code>が置き換えられます。<code>self</code>を返します。
674
- #
675
- # Same as Multiset#map_with, but replaces <code>self</code> by
676
- # resulting multiset. Returns <code>self</code>.
677
714
  def map_with!
678
715
  self.to_hash.each_pair do |item, count|
679
716
  self.delete(item, count)
@@ -684,13 +721,13 @@ class Multiset
684
721
  end
685
722
  alias collect_with! map_with!
686
723
 
724
+ # Returns one item in <code>self</code> at random
725
+ # in the same probability.
726
+ # Returns <code>nil</code> in case the multiset is empty.
727
+ #
687
728
  # <code>self</code>の要素を無作為に1つ選んで返します。
688
729
  # すべての要素は等確率で選ばれます。
689
- # 空のmultisetに対して呼び出した場合は<code>nil</code>を返します。
690
- #
691
- # Returns one item in <code>self</code> randomly.
692
- # All items are selected with the same probability.
693
- # Returns <code>nil</code> in case the multiset is empty.
730
+ # 空のMultisetに対して呼び出した場合は<code>nil</code>を返します。
694
731
  def sample
695
732
  return nil if empty?
696
733
  pos = Kernel.rand(self.size)
@@ -701,9 +738,9 @@ class Multiset
701
738
  end
702
739
  alias :rand :sample
703
740
 
704
- # <code>self</code>中に含まれる多重集合を平滑化したものを返します。
705
- #
706
741
  # Generates a multiset such that multisets in <code>self</code> are flattened.
742
+ #
743
+ # <code>self</code>中に含まれる多重集合を平滑化したものを返します。
707
744
  def flatten
708
745
  ret = Multiset.new
709
746
  self.each do |item|
@@ -716,13 +753,13 @@ class Multiset
716
753
  ret
717
754
  end
718
755
 
719
- # <code>self</code>中に含まれる多重集合を平滑化します。
720
- # 平滑化した多重集合が1つでもあれば<code>self</code>を、
721
- # そうでなければ<code>nil</code>を返します。
722
- #
723
756
  # Flattens multisets in <code>self</code>.
724
757
  # Returns <code>self</code> if any item is flattened,
725
758
  # <code>nil</code> otherwise.
759
+ #
760
+ # <code>self</code>中に含まれる多重集合を平滑化します。
761
+ # 平滑化した多重集合が1つでもあれば<code>self</code>を、
762
+ # そうでなければ<code>nil</code>を返します。
726
763
  def flatten!
727
764
  ret = nil
728
765
  self.to_a.each do |item|
@@ -735,11 +772,11 @@ class Multiset
735
772
  ret
736
773
  end
737
774
 
775
+ # Gives all items in <code>self</code> (without duplication) to given block,
776
+ # and returns a multiset collecting the items such that the block returns false.
777
+ #
738
778
  # ブロックに<code>self</code>の要素(重複なし)を順次与え、
739
779
  # 結果が偽であった要素のみを集めたMultisetを返します。
740
- #
741
- # Gives all items in <code>self</code> (without duplication) to given block,
742
- # and returns a multiset collecting the items whose results in the block are false.
743
780
  def reject
744
781
  ret = Multiset.new
745
782
  @entries.each_pair do |item, count|
@@ -748,11 +785,11 @@ class Multiset
748
785
  ret
749
786
  end
750
787
 
788
+ # Gives all pairs of (non-duplicated) items and counts in <code>self</code> to given block,
789
+ # and returns a multiset collecting the items such that the block returns false.
790
+ #
751
791
  # ブロックに<code>self</code>の要素(重複なし)と個数の組を順次与え、
752
792
  # 結果が偽であった要素のみを集めたMultisetを返します。
753
- #
754
- # Gives all pairs of (non-duplicate) items and counts in <code>self</code> to given block,
755
- # and returns a multiset collecting the items whose results in the block are false.
756
793
  def reject_with
757
794
  ret = Multiset.new
758
795
  @entries.each_pair do |item, count|
@@ -761,9 +798,9 @@ class Multiset
761
798
  ret
762
799
  end
763
800
 
764
- # Multiset#delete_ifと同じですが、要素が1つも削除されなければ<code>nil</code>を返します。
801
+ # Same as Multiset#delete_if except that this returns <code>nil</code> if no item is deleted.
765
802
  #
766
- # Same as Multiset#delete_if, but returns <code>nil</code> if no item is deleted.
803
+ # Multiset#delete_ifと似ますが、要素が1つも削除されなければ<code>nil</code>を返します。
767
804
  def reject!
768
805
  ret = nil
769
806
  @entries.each_pair do |item, count|
@@ -775,13 +812,13 @@ class Multiset
775
812
  ret
776
813
  end
777
814
 
815
+ # Gives all items in <code>self</code> (without duplication) to given block,
816
+ # and deletes the items such that the block returns true.
817
+ # Returns <code>self</code>.
818
+ #
778
819
  # ブロックに<code>self</code>の要素(重複なし)を順次与え、
779
820
  # 結果が真であった要素をすべて削除します。
780
821
  # <code>self</code>を返します。
781
- #
782
- # Gives all items in <code>self</code> (without duplication) to given block,
783
- # and deletes that item if the block returns true.
784
- # Returns <code>self</code>.
785
822
  def delete_if
786
823
  @entries.each_pair do |item, count|
787
824
  self.delete_all(item) if yield(item)
@@ -789,13 +826,13 @@ class Multiset
789
826
  self
790
827
  end
791
828
 
829
+ # Gives each pair of (non-duplicated) item and its number to given block,
830
+ # and deletes those items such that the block returns true.
831
+ # Returns <code>self</code>.
832
+ #
792
833
  # <code>self</code>に含まれるすべての要素(重複なし)とその個数について、
793
834
  # その組をブロックに与え、結果が真であった要素をすべて削除します。
794
835
  # <code>self</code>を返します。
795
- #
796
- # Gives each pair of (non-duplicate) item and its number to given block,
797
- # and deletes those items if the block returns true.
798
- # Returns <code>self</code>.
799
836
  def delete_with
800
837
  @entries.each_pair do |item, count|
801
838
  @entries.delete(item) if yield(item, count)
@@ -803,12 +840,12 @@ class Multiset
803
840
  self
804
841
  end
805
842
 
843
+ # Classify items in <code>self</code> by returned value from block.
844
+ # Returns a Multimap whose values are associated with keys, where
845
+ # the keys are the returned value from given block.
846
+ #
806
847
  # <code>self</code>の要素を、与えられたブロックからの返り値によって分類します。
807
848
  # ブロックからの返り値をキーとして値を対応付けたMultimapを返します。
808
- #
809
- # Classify items in <code>self</code> by returned value from block.
810
- # Returns a Multimap whose values are associated with keys. Keys
811
- # are defined by returned value from given block.
812
849
  def group_by
813
850
  ret = Multimap.new
814
851
  @entries.each_pair do |item, count|
@@ -818,9 +855,10 @@ class Multiset
818
855
  end
819
856
  alias :classify :group_by
820
857
 
821
- # Multiset#group_byと同様ですが、ブロックには要素とその個数の組が与えられます。
858
+ # Same as Multiset#group_by except that the pairs of (non-duplicated) items and
859
+ # their counts are given to block.
822
860
  #
823
- # Same as Multiset#group_by, but the pairs of (non-duplicate) items and their counts are given to block.
861
+ # Multiset#group_byと同様ですが、ブロックには要素とその個数の組が与えられます。
824
862
  def group_by_with
825
863
  ret = Multimap.new
826
864
  @entries.each_pair do |item, count|
@@ -830,17 +868,17 @@ class Multiset
830
868
  end
831
869
  alias :classify_with :group_by_with
832
870
 
833
- # ブロックに<code>self</code>の要素(重複なし)を順次与え、
834
- # 最初に結果が真であった要素を返します。
835
- # 見つからなかった場合は、ifnoneが指定されている場合は ifnone.call し、
836
- # そうでなければnilを返します。
837
- # ブロックを与えなかった場合、そのためのEnumeratorを返します。
838
- #
839
871
  # Gives all items in <code>self</code> (without duplication) to given block,
840
872
  # and returns the first item that makes true the result of the block.
841
873
  # If none of the items make it true, ifnone.call is executed if ifnone is specified,
842
874
  # otherwise nil is returned.
843
875
  # If no block is given, corresponding Enumerator is returned.
876
+ #
877
+ # ブロックに<code>self</code>の要素(重複なし)を順次与え、
878
+ # 最初に結果が真であった要素を返します。
879
+ # 見つからなかった場合は、ifnoneが指定されている場合は ifnone.call し、
880
+ # そうでなければnilを返します。
881
+ # ブロックを与えなかった場合、そのためのEnumeratorを返します。
844
882
  def find(ifnone = nil, &block) # :yields: item
845
883
  if block
846
884
  find_(ifnone, &block)
@@ -858,9 +896,10 @@ class Multiset
858
896
  end
859
897
  private :find_
860
898
 
861
- # Multiset#findと同じですが、ブロックには<code>self</code>の要素とその個数の組が与えられます。
899
+ # The same as Multiset#find except that the pairs of (non-duplicated) items and
900
+ # their counts are given to the block.
862
901
  #
863
- # The same as Multiset#find, but pairs of (non-duplicate) items and their counts are given to the block.
902
+ # Multiset#findと似ますが、ブロックには<code>self</code>の要素とその個数の組が与えられます。
864
903
  def find_with(ifnone = nil, &block) # :yields: item, count
865
904
  if block
866
905
  find_with_(ifnone, &block)
@@ -878,13 +917,13 @@ class Multiset
878
917
  end
879
918
  private :find_with_
880
919
 
881
- # ブロックに<code>self</code>の要素(重複なし)を順次与え、
882
- # 結果が真であった要素を集めた多重集合を返します。
883
- # ブロックを与えなかった場合、そのためのEnumeratorを返します。
884
- #
885
920
  # Gives all items in <code>self</code> (without duplication) to given block,
886
921
  # and returns the Multiset by items that makes true the result of the block.
887
922
  # If no block is given, corresponding Enumerator is returned.
923
+ #
924
+ # ブロックに<code>self</code>の要素(重複なし)を順次与え、
925
+ # 結果が真であった要素を集めた多重集合を返します。
926
+ # ブロックを与えなかった場合、そのためのEnumeratorを返します。
888
927
  def find_all(&block) # :yields: item
889
928
  if block
890
929
  find_all_(&block)
@@ -903,9 +942,10 @@ class Multiset
903
942
  end
904
943
  private :find_all_
905
944
 
906
- # Multiset#find_allと同じですが、ブロックには<code>self</code>の要素とその個数の組が与えられます。
945
+ # The same as Multiset#find_all except that the pairs of (non-duplicated) items and
946
+ # their counts are given to the block.
907
947
  #
908
- # The same as Multiset#find_all, but pairs of (non-duplicate) items and their counts are given to the block.
948
+ # Multiset#find_allと似ますが、ブロックには<code>self</code>の要素とその個数の組が与えられます。
909
949
  def find_all_with(&block) # :yields: item, count
910
950
  if block
911
951
  find_all_with_(&block)
@@ -924,11 +964,11 @@ class Multiset
924
964
  end
925
965
  private :find_all_with_
926
966
 
927
- # <code>pattern</code>の条件を満たした(<code>pattern</code> === item)要素のみを集めた多重集合を返します。
928
- # ブロックが与えられている場合は、さらにその結果を適用した結果を返します。
929
- #
930
967
  # Collects items in <code>self</code> satisfying <code>pattern</code> (<code>pattern</code> === item).
931
968
  # If a block is given, the items are converted by the result of the block.
969
+ #
970
+ # <code>pattern</code>の条件を満たした(<code>pattern</code> === item)要素のみを集めた多重集合を返します。
971
+ # ブロックが与えられている場合は、さらにその結果を適用した結果を返します。
932
972
  def grep(pattern)
933
973
  ret = Multiset.new
934
974
  @entries.each_pair do |item, count|
@@ -939,20 +979,20 @@ class Multiset
939
979
  ret
940
980
  end
941
981
 
982
+ # Three elements are given to the block for each (non-duplicated) items in <code>self</code>:
983
+ # the last result of the block, the item, and its number in <code>self</code>.
984
+ # As for the first block call, the first argument is <code>init</code>.
985
+ # The result of the last block call is returned.
986
+ #
987
+ # Different from Enumerable#inject, <code>init</code> cannot be omitted.
988
+ # In addition, Symbol cannot be given instead of a block.
989
+ #
942
990
  # ブロックに「1回前のブロック呼び出しの返り値」「<code>self</code>の要素」「その個数」の
943
991
  # 3つ組を順次与え、最後にブロックを呼んだ結果を返します。ただし「1回前のブロック呼び出しの返り値」は、
944
992
  # 1回目のブロック呼び出しの際については、代わりに<code>init</code>の値が与えられます。
945
993
  #
946
994
  # Enumerable#injectと異なり、<code>init</code>は省略できません。
947
995
  # またブロックの代わりにSymbolを与えることもできません。
948
- #
949
- # Three elements are given to the block for each (non-duplicate) items:
950
- # the last result of the block, the item and its count.
951
- # As for the first block call, the first argument is <code>init</code>.
952
- # The result of the last block call is returned.
953
- #
954
- # Different from Enumerable#inject, <code>init</code> cannot be omitted.
955
- # In addition, Symbol cannot be given instead of a block.
956
996
  def inject_with(init)
957
997
  @entries.each_pair do |item, count|
958
998
  init = yield(init, item, count)
@@ -960,113 +1000,128 @@ class Multiset
960
1000
  init
961
1001
  end
962
1002
 
1003
+ # Returns the largest item in <code>self</code>,
1004
+ # or <code>nil</code> if no item is stored in <code>self</code>.
1005
+ # If a block is given, they are ordered by giving pairs of items to the block.
1006
+ #
963
1007
  # 最大の要素を返します。
964
1008
  # 要素が存在しない場合はnilを返します。
965
1009
  # ブロックが与えられた場合は、要素間の大小判定を、ブロックに2つの要素を与えることで行います。
966
- #
967
- # Returns the largest item, or <code>nil</code> if no item is stored in <code>self</code>.
968
- # If a block is given, their order is judged by giving two items to the block.
969
1010
  def max(&block) # :yields: a, b
970
1011
  @entries.keys.max(&block)
971
1012
  end
972
1013
 
1014
+ # Returns the smallest item in <code>self</code>,
1015
+ # or <code>nil</code> if no item is stored in <code>self</code>.
1016
+ # If a block is given, they are ordered by giving pairs of items to the block.
1017
+ #
973
1018
  # 最小の要素を返します。
974
1019
  # 要素が存在しない場合はnilを返します。
975
1020
  # ブロックが与えられた場合は、要素間の大小判定を、ブロックに2つの要素を与えることで行います。
976
- #
977
- # Returns the smallest item, or <code>nil</code> if no item is stored in <code>self</code>.
978
- # If a block is given, their order is judged by giving two items to the block.
979
1021
  def min(&block) # :yields: a, b
980
1022
  @entries.keys.min(&block)
981
1023
  end
982
1024
 
1025
+ # Returns the pair consisting of the smallest and the largest item in <code>self</code>,
1026
+ # or <code>nil</code> if no item is stored in <code>self</code>.
1027
+ # If a block is given, they are ordered by giving pairs of items to the block.
1028
+ #
983
1029
  # 最小の要素と最大の要素の組を返します。
984
1030
  # ブロックが与えられた場合は、要素間の大小判定を、ブロックに2つの要素を与えることで行います。
985
- #
986
- # Returns the pair consisting of the smallest and the largest item.
987
- # If a block is given, their order is judged by giving two items to the block.
988
1031
  def minmax(&block) # :yields: a, b
989
1032
  @entries.keys.minmax(&block)
990
1033
  end
991
1034
 
1035
+ # Returns the largest item by comparing the items in <code>self</code>
1036
+ # by the results of the block.
1037
+ # If no item is stored in <code>self</code>, <code>nil</code> is returned.
1038
+ #
992
1039
  # ブロックの値を評価した結果が最大になるような要素を返します。
993
1040
  # 要素が存在しない場合はnilを返します。
994
- #
995
- # Returns the largest item, or <code>nil</code> if no item is stored in <code>self</code>.
996
1041
  def max_by(&block) # :yields: item
997
1042
  @entries.keys.max_by(&block)
998
1043
  end
999
1044
 
1045
+ # Returns the largest item by comparing the items in <code>self</code>
1046
+ # by the results of the block.
1047
+ # If no item is stored in <code>self</code>, <code>nil</code> is returned.
1048
+ #
1000
1049
  # ブロックの値を評価した結果が最小になるような要素を返します。
1001
1050
  # 要素が存在しない場合はnilを返します。
1002
- #
1003
- # Returns the smallest item, or <code>nil</code> if no item is stored in <code>self</code>.
1004
1051
  def min_by(&block) # :yields: item
1005
1052
  @entries.keys.min_by(&block)
1006
1053
  end
1007
1054
 
1055
+ # Returns the pair consisting of the smallest and the largest items in <code>self</code>
1056
+ # by comparing the items by the results of the block.
1057
+ # If no item is stored in <code>self</code>, <code>nil</code> is returned.
1058
+ #
1008
1059
  # ブロックの値を評価した結果が最小になる要素と最大になる要素の組を返します。
1009
1060
  # 要素が存在しない場合はnilを返します。
1010
- #
1011
- # Returns the pair consisting of the smallest and the largest item.
1012
1061
  def minmax_by(&block) # :yields: item
1013
1062
  @entries.keys.minmax_by(&block)
1014
1063
  end
1015
1064
 
1065
+ # Same as Multiset#max except that the following four:
1066
+ # "item 1", "number of item 1", "item 2" and "number of item 2" are given to the block.
1067
+ #
1016
1068
  # Multiset#max と同様ですが、ブロックには「要素1」「要素1の出現数」「要素2」「要素2の出現数」の
1017
1069
  # 4引数が与えられます。
1018
- #
1019
- # Same as Multiset#max, but four arguments: "item 1", "number of item 1", "item 2" and "number of item 2" are given to the block.
1020
1070
  def max_with # :yields: item1, count1, item2, count2
1021
1071
  tmp = @entries.each_pair.max{ |a, b| yield(a[0], a[1], b[0], b[1]) }
1022
1072
  tmp ? tmp[0] : nil
1023
1073
  end
1024
1074
 
1075
+ # Same as Multiset#min except that the following four:
1076
+ # "item 1", "number of item 1", "item 2" and "number of item 2" are given to the block.
1077
+ #
1025
1078
  # Multiset#min と同様ですが、ブロックには「要素1」「要素1の出現数」「要素2」「要素2の出現数」の
1026
1079
  # 4引数が与えられます。
1027
- #
1028
- # Same as Multiset#min, but four arguments: "item 1", "number of item 1", "item 2" and "number of item 2" are given to the block.
1029
1080
  def min_with # :yields: item1, count1, item2, count2
1030
1081
  tmp = @entries.each_pair.min{ |a, b| yield(a[0], a[1], b[0], b[1]) }
1031
1082
  tmp ? tmp[0] : nil
1032
1083
  end
1033
1084
 
1085
+ # Same as Multiset#minmax except that the following four:
1086
+ # "item 1", "number of item 1", "item 2" and "number of item 2" are given to the block.
1087
+ #
1034
1088
  # Multiset#minmax と同様ですが、ブロックには「要素1」「要素1の出現数」「要素2」「要素2の出現数」の
1035
1089
  # 4引数が与えられます。
1036
- #
1037
- # Same as Multiset#minmax, but four arguments: "item 1", "number of item 1", "item 2" and "number of item 2" are given to the block.
1038
1090
  def minmax_with # :yields: item1, count1, item2, count2
1039
1091
  tmp = @entries.each_pair.minmax{ |a, b| yield(a[0], a[1], b[0], b[1]) }
1040
1092
  tmp ? [tmp[0][0], tmp[1][0]] : nil
1041
1093
  end
1042
1094
 
1095
+ # Same as Multiset#max_by except that pairs of (non-duplicated) items and their counts
1096
+ # are given to the block.
1097
+ #
1043
1098
  # Multiset#max_by と同様ですが、ブロックには要素(重複なし)とその出現数の組が与えられます。
1044
- #
1045
- # Same as Multiset#min, but pairs of (non-duplicated) items and their counts are given to the block.
1046
1099
  def max_by_with(&block) # :yields: item, count
1047
1100
  tmp = @entries.each_pair.max_by(&block)
1048
1101
  tmp ? tmp[0] : nil # if @entries is not empty, tmp must be a two-element array
1049
1102
  end
1050
1103
 
1051
- # Multiset#min_by と同様ですが、ブロックには要素(重複なし)とその出現数の組が与えられます。
1052
- #
1053
- # Same as Multiset#max, but pairs of (non-duplicated) items and their counts are given to the block.
1104
+ # Same as Multiset#min_by except that pairs of (non-duplicated) items and their counts
1105
+ # are given to the block.
1106
+ #
1107
+ # Multiset#max_by と同様ですが、ブロックには要素(重複なし)とその出現数の組が与えられます。
1054
1108
  def min_by_with(&block) # :yields: item, count
1055
1109
  tmp = @entries.each_pair.min_by(&block)
1056
1110
  tmp ? tmp[0] : nil # if @entries is not empty, tmp must be a two-element array
1057
1111
  end
1058
1112
 
1113
+ # Same as Multiset#minmax_by except that pairs of (non-duplicated) items and their counts
1114
+ # are given to the block.
1115
+ #
1059
1116
  # Multiset#minmax_by と同様ですが、ブロックには要素(重複なし)とその出現数の組が与えられます。
1060
- #
1061
- # Same as Multiset#minmax, but pairs of (non-duplicated) items and their counts are given to the block.
1062
1117
  def minmax_by_with(&block) # :yields: item, count
1063
1118
  tmp = @entries.each_pair.minmax_by(&block)
1064
1119
  tmp[0] ? [tmp[0][0], tmp[1][0]] : nil
1065
1120
  end
1066
1121
 
1067
- # <code>self</code>の要素を並び替えた配列を生成します。
1068
- #
1069
1122
  # Generates an array by sorting the items in <code>self</code>.
1123
+ #
1124
+ # <code>self</code>の要素を並び替えた配列を生成します。
1070
1125
  def sort(&block) # :yields: a, b
1071
1126
  ret = []
1072
1127
  @entries.keys.sort(&block).each do |item|
@@ -1075,9 +1130,10 @@ class Multiset
1075
1130
  ret
1076
1131
  end
1077
1132
 
1078
- # Multiset#sortと同様ですが、ブロックには1つの要素が与えられ、その値が小さいものから順に並びます。
1133
+ # The same as Multiset#sort except that, after giving the items to the block,
1134
+ # the items are sorted by the values from the block.
1079
1135
  #
1080
- # Same as Multiset#sort, but only one item is given to the block.
1136
+ # Multiset#sortと同様ですが、ブロックには1つの要素が与えられ、その値が小さいものから順に並びます。
1081
1137
  def sort_by(&block) # :yields: item
1082
1138
  ret = []
1083
1139
  @entries.keys.sort_by(&block).each do |item|
@@ -1086,10 +1142,11 @@ class Multiset
1086
1142
  ret
1087
1143
  end
1088
1144
 
1145
+ # Same as Multiset#sort except that the following four:
1146
+ # "item 1", "number of item 1", "item 2" and "number of item 2" are given to the block.
1147
+ #
1089
1148
  # Multiset#sort と同様ですが、ブロックには「要素1」「要素1の出現数」「要素2」「要素2の出現数」の
1090
1149
  # 4引数が与えられます。
1091
- #
1092
- # Same as Multiset#sort, but four arguments: "item 1", "number of item 1", "item 2" and "number of item 2" are given to the block.
1093
1150
  def sort_with # :yields: item1, count1, item2, count2
1094
1151
  ret = []
1095
1152
  @entries.each_pair.sort{ |a, b| yield(a[0], a[1], b[0], b[1]) }.each do |item_count|
@@ -1098,9 +1155,10 @@ class Multiset
1098
1155
  ret
1099
1156
  end
1100
1157
 
1101
- # Multiset#sort_by と同様ですが、ブロックには要素(重複なし)とその出現数の組が与えられます。
1158
+ # Same as Multiset#sort_by except that the pairs of (non-duplicated) items
1159
+ # and their counts are given to the block.
1102
1160
  #
1103
- # Same as Multiset#sort_by, but pairs of (non-duplicated) items and their counts are given to the block.
1161
+ # Multiset#sort_by と同様ですが、ブロックには要素(重複なし)とその出現数の組が与えられます。
1104
1162
  def sort_by_with # :yields: item1, count1, item2, count2
1105
1163
  ret = []
1106
1164
  @entries.each_pair.sort_by{ |a| yield(*a) }.each do |item_count|
@@ -1111,16 +1169,16 @@ class Multiset
1111
1169
  end
1112
1170
 
1113
1171
  class Hash
1114
- # <code>self</code>を多重集合に変換し、その結果を返します。
1115
- # キーを要素、キーに対応する値をその要素の要素数とします。
1172
+ # Generates multiset from <code>self</code>.
1173
+ # Keys of the Hash are treated as items in the multiset,
1174
+ # while values of the Hash are number of items.
1116
1175
  #
1117
- # (例)<code>{:a => 4, :b => 2}.to_multiset # :aを4個、:bを2個含む多重集合</code>
1176
+ # (example) <code>{:a => 4, :b => 2}.to_multiset # Multiset with four :a's and two :b's</code>
1118
1177
  #
1119
- # Generates multiset from <code>self</code>.
1120
- # Keys are treated as elements, and values are number of elements
1121
- # in the multiset. For example,
1178
+ # <code>self</code>を多重集合に変換し、その結果を返します。
1179
+ # Hashのキーを要素、Hashの値をその要素の要素数とします。
1122
1180
  #
1123
- # <code>{:a => 4, :b => 2}.to_multiset # Multiset with four :a's and two :b's</code>
1181
+ # (例)<code>{:a => 4, :b => 2}.to_multiset # :aを4個、:bを2個含む多重集合</code>
1124
1182
  def to_multiset
1125
1183
  ret = Multiset.new
1126
1184
  self.each_pair{ |item, count| ret.renew_count(item, count) }