class Multiset

Basic information 概要

A Ruby implementation of multiset.
Unlike ordinary set (see Ruby documentation for "set" library),
multiset can contain two or more same items.

Methods' names are basically consistent with those of Set class.

* <code>Set[:a,:b,:c,:b,:b,:c] => #<Set: {:b, :c, :a}></code>
* <code>Multiset[:a,:b,:c,:b,:b,:c] => #<Multiset:<tt>#</tt>3 :b, <tt>#</tt>2 :c, <tt>#</tt>1 :a></code>

Rubyによる多重集合(マルチセット)の実装です。
通常の集合(Rubyでは"set"ライブラリ)と異なり、多重集合は
同一の要素を複数格納することができます。

メソッド名は基本的にSetクラスに合わせてあります。

Constants

VERSION

Public Class Methods

[](*list) click to toggle source

Generates a multiset from items in list. Unlike using Multiset.new, each argument is one item in generated multiset.

This method is mainly used when you generate a multiset from literals.

listに含まれる要素からなる多重集合を生成します。 Multiset.newを用いる場合と異なり、引数の1つ1つが多重集合の要素になります。

主に、リテラルから多重集合を生成するのに用います。

# File lib/multiset.rb, line 66
def Multiset.[](*list)
  Multiset.new(list)
end
from_lines(str) click to toggle source

Generates a Multiset from string, separated by lines.

文字列を行単位で区切ってMultisetにします。

# File lib/multiset.rb, line 118
def Multiset.from_lines(str)
  Multiset.new(str.enum_for(:each_line))
end
new(list = nil) click to toggle source

Generates a multiset from items in list. If list is omitted, returns empty multiset.

list must be an object including Enumerable. Otherwise, ArgumentError is raised.

listに含まれる要素からなる多重集合を生成します。 listを省略した場合、空の多重集合を生成します。

listにはEnumerableであるオブジェクトのみ 指定できます。そうでない場合、例外ArgumentErrorが 発生します。

# File lib/multiset.rb, line 48
def initialize(list = nil)
  @entries = {}
  if list.kind_of?(Enumerable)
    list.each{ |item| add item }
  elsif list != nil
    raise ArgumentError, "Item list must be an instance including 'Enumerable' module"
  end
end
parse(object) click to toggle source

Generates a multiset by converting object.

  • If object is an instance of Multiset, returns duplicated object.

  • If object is not an instance of Multiset and has the method each_pair, for each pair of two arguments from each_pair, the first argument becomes the item in multiset and the second argument becomes its number in the multiset. See also Hash#to_multiset .

  • If object does not have the method each_pair and object includes Enumerable, this method works the same as Multiset#new .

  • Otherwise, ArgumentError is raised.

objectを多重集合に変換し生成します。

  • objectがMultisetのインスタンスである場合、 その複製を返します。

  • objectがMultisetのインスタンスでなく、 かつeach_pairメソッドを持っている場合、 each_pairから渡される2つの引数について、前者を要素、 後者をその個数とした多重集合を生成します。Hash#to_multisetも ご覧下さい。

  • objecteach_pairメソッドを持っておらず、 かつEnumerableである場合は、Multiset#newと同じ結果です。

  • それ以外の場合は、例外ArgumentErrorが発生します。

# File lib/multiset.rb, line 95
def Multiset.parse(object)
  if object.kind_of?(String)
    raise ArgumentError, "Multiset.parse can not parse strings. If you would like to store string lines to a multiset, use Multiset.from_lines(string)."
  end
  
  if object.instance_of?(Multiset)
    ret = object.dup
  else
    ret = Multiset.new
    if defined? object.each_pair
      object.each_pair{ |item, count| ret.add item, count }
    elsif object.kind_of?(Enumerable)
      object.each{ |item| ret.add item }
    else
      raise ArgumentError, "Source of Multiset must have 'each_pair' method or include 'Enumerable' module"
    end
  end
  ret
end
parse_force(object) click to toggle source

If a string is given, it works as Multiset.from_lines, otherwise as Multiset.parse.

文字列が渡された場合は、Multiset.from_linesと同じ挙動。 それ以外の場合は、Multiset.parseと同じ挙動。

# File lib/multiset.rb, line 127
def Multiset.parse_force(object)
  if object.kind_of?(String)
    Multiset.from_lines(object)
  else
    Multiset.parse(object)
  end
end

Public Instance Methods

&(other) click to toggle source

Returns the intersection of self and other, that is, for each item both in self and other, the multiset includes it in the smaller number of the two.

selfotherの積集合からなる多重集合を返します。 すなわち、selfotherの両方に存在する要素について、 少ないほうの個数を持った多重集合を返します。

# File lib/multiset.rb, line 573
def &(other)
  ret = Multiset.new
  (self.items & other.items).each do |item|
    ret.renew_count(item, [self.count(item), other.count(item)].min)
  end
  ret
end
+(other)
Alias for: merge
-(other)
Alias for: subtract
<<(item, addcount = 1)
Alias for: add
==(other) click to toggle source

Returns whether self is equal to other.

selfotherと等しいかどうかを返します。

# File lib/multiset.rb, line 498
def ==(other)
  return false unless other.instance_of?(Multiset)
  compare_set_with(other){ |s, o| s == o }
end
add(item, addcount = 1) click to toggle source

Adds addcount number of items to self. Returns self if succeeded, or nil if failed.

selfに、addcount個のitemを追加します。 成功した場合はselfを、失敗した場合はnilを返します。

# File lib/multiset.rb, line 384
def add(item, addcount = 1)
  return nil if addcount == nil
  a = addcount.to_i
  return nil if a <= 0
  self.renew_count(item, self.count(item) + a)
end
Also aliased as: <<
classify()
Alias for: group_by
classify_with()
Alias for: group_by_with
clear() click to toggle source

Deletes all items in self. Returns self.

selfの要素をすべて削除します。 selfを返します。

# File lib/multiset.rb, line 252
def clear
  @entries.clear
  self
end
collect()
Alias for: map
collect!()
Alias for: map!
collect_with()
Alias for: map_with
collect_with!()
Alias for: map_with!
count(item) click to toggle source
count{ |item| ... }

Returns number of items in self. If the item is omitted, the value is same as Multiset#size. If a block is given, each element (without duplication) is given to the block, and returns the number of elements (including duplication) that returns true in the block.

self中に含まれるitemの個数を返します。 引数を指定しない場合は、Multiset#sizeと同じです。 ブロックを指定することもでき、その場合は(重複しない)各要素をブロックに与え、 条件を満たした(結果が真であった)要素がMultiset内にいくつ入っているかを数えます。

# File lib/multiset.rb, line 338
def count(*item_list)
  if block_given?
    unless item_list.empty?
      raise ArgumentError, "Both item and block cannot be given"
    end
    
    result = 0
    @entries.each_pair do |i, c|
      result += c if yield(i)
    end
    result
  else
    case item_list.size
    when 0
      self.size
    when 1
      @entries.has_key?(item_list.first) ? @entries[item_list.first] : 0
    else
      raise ArgumentError, "Only one item can be given"
    end
  end
end
delete(item, delcount = 1) click to toggle source

Deletes delcount number of items from self. Returns self if succeeded, nil otherwise.

selfから、delcount個のitemを削除します。 成功した場合はselfを、失敗した場合はnilを返します。

# File lib/multiset.rb, line 398
def delete(item, delcount = 1)
  return nil if delcount == nil || !self.include?(item)
  d = delcount.to_i
  return nil if d <= 0
  self.renew_count(item, self.count(item) - d)
end
delete_all(item) click to toggle source

Deletes all items in self. Returns self.

selfに含まれるitemをすべて削除します。 selfを返します。

# File lib/multiset.rb, line 410
def delete_all(item)
  @entries.delete(item)
  self
end
delete_if() { |item| ... } click to toggle source

Gives all items in self (without duplication) to given block, and deletes the items such that the block returns true. Returns self.

ブロックにselfの要素(重複なし)を順次与え、 結果が真であった要素をすべて削除します。 selfを返します。

# File lib/multiset.rb, line 822
def delete_if
  @entries.each_pair do |item, count|
    self.delete_all(item) if yield(item)
  end
  self
end
delete_with() { |item, count| ... } click to toggle source

Gives each pair of (non-duplicated) item and its number to given block, and deletes those items such that the block returns true. Returns self.

selfに含まれるすべての要素(重複なし)とその個数について、 その組をブロックに与え、結果が真であった要素をすべて削除します。 selfを返します。

# File lib/multiset.rb, line 836
def delete_with
  @entries.each_pair do |item, count|
    @entries.delete(item) if yield(item, count)
  end
  self
end
detect(ifnone = nil)
Alias for: find
detect_with(ifnone = nil)
Alias for: find_with
dup() click to toggle source

Returns duplicated self.

selfの複製を生成して返します。

# File lib/multiset.rb, line 138
def dup
  @entries.to_multiset
end
each() { |item| ... } click to toggle source

Iterates for each item in self. Returns self. An Enumerator will be returned if no block is given.

This method is ineffective since the same element in the Multiset can be given to the block for many times, so that it behaves the same as Enumerable#each. Please consider using Multiset#each_item or Multiset#each_pair: for example, a Multiset with 100 times “a” will call the given block for 100 times for Multiset#each, while only once for Multiset#each_pair.

selfに含まれるすべての要素について繰り返します。 selfを返します。 ブロックが与えられていない場合、Enumeratorを返します。

このメソッドは Enumerable#each の挙動に合わせ、同じ要素を何度もブロックに 渡すため、効率が悪いです。Multiset#each_item, Multiset#each_pairの利用もご検討下さい。 例えば「“a”が100個入ったMultiset」をeachで繰り返すと100回の処理が行われますが、 each_pairなら1回で済みます。

# File lib/multiset.rb, line 622
def each
  if block_given?
    @entries.each_pair do |item, count|
      count.times{ yield item }
    end
    self
  else
    Enumerator.new(self, :each)
  end
end
each_item() { |item| ... } click to toggle source

Iterates for each item in self, without duplication. Returns self. An Enumerator will be returned if no block is given.

selfに含まれるすべての要素について、重複を許さずに繰り返します。 selfを返します。 ブロックが与えられていない場合、Enumeratorを返します。

# File lib/multiset.rb, line 640
def each_item(&block) # :yields: item
  if block
    @entries.each_key(&block)
    self
  else
    @entries.each_key
  end
end
each_pair()
Alias for: each_with_count
each_with_count() { |item, count| ... } click to toggle source

Iterates for each pair of (non-duplicated) item and its number in self. Returns self. An Enumerator will be returned if no block is given.

selfに含まれるすべての要素(重複なし)とその個数について繰り返します。 selfを返します。 ブロックが与えられていない場合、Enumeratorを返します。

# File lib/multiset.rb, line 656
def each_with_count(&block) # :yields: item, count
  if block
    @entries.each_pair(&block)
    self
  else
    @entries.each_pair
  end
end
Also aliased as: each_pair
empty?() click to toggle source

Returns whether self has no item.

selfに要素がないかどうかを返します。

# File lib/multiset.rb, line 236
def empty?
  @entries.empty?
end
find(ifnone = nil) { |item| ... } click to toggle source

Gives all items in self (without duplication) to given block, and returns the first item that makes true the result of the block. If none of the items make it true, ifnone.call is executed if ifnone is specified, otherwise nil is returned. If no block is given, corresponding Enumerator is returned.

ブロックにselfの要素(重複なし)を順次与え、 最初に結果が真であった要素を返します。 見つからなかった場合は、ifnoneが指定されている場合は ifnone.call し、 そうでなければnilを返します。 ブロックを与えなかった場合、そのためのEnumeratorを返します。

# File lib/multiset.rb, line 882
def find(ifnone = nil, &block) # :yields: item
  if block
    find_(ifnone, &block)
  else
    self.to_enum(:find_, ifnone)
  end
end
Also aliased as: detect
find_all() { |item| ... } click to toggle source

Gives all items in self (without duplication) to given block, and returns the Multiset by items that makes true the result of the block. If no block is given, corresponding Enumerator is returned.

ブロックにselfの要素(重複なし)を順次与え、 結果が真であった要素を集めた多重集合を返します。 ブロックを与えなかった場合、そのためのEnumeratorを返します。

# File lib/multiset.rb, line 927
def find_all(&block) # :yields: item
  if block
    find_all_(&block)
  else
    self.to_enum(:find_all_, ifnone)
  end
end
Also aliased as: select
find_all_with() { |item, count| ... } click to toggle source

The same as Multiset#find_all except that the pairs of (non-duplicated) items and their counts are given to the block.

Multiset#find_allと似ますが、ブロックにはselfの要素とその個数の組が与えられます。

# File lib/multiset.rb, line 949
def find_all_with(&block) # :yields: item, count
  if block
    find_all_with_(&block)
  else
    self.to_enum(:find_all_with_, ifnone)
  end
end
Also aliased as: select_with
find_with(ifnone = nil) { |item, count| ... } click to toggle source

The same as Multiset#find except that the pairs of (non-duplicated) items and their counts are given to the block.

Multiset#findと似ますが、ブロックにはselfの要素とその個数の組が与えられます。

# File lib/multiset.rb, line 903
def find_with(ifnone = nil, &block) # :yields: item, count
  if block
    find_with_(ifnone, &block)
  else
    self.to_enum(:find_with_, ifnone)
  end
end
Also aliased as: detect_with
flatten() click to toggle source

Generates a multiset such that multisets in self are flattened.

self中に含まれる多重集合を平滑化したものを返します。

# File lib/multiset.rb, line 744
def flatten
  ret = Multiset.new
  self.each do |item|
    if item.kind_of?(Multiset)
      ret += item.flatten
    else
      ret << item
    end
  end
  ret
end
flatten!() click to toggle source

Flattens multisets in self. Returns self if any item is flattened, nil otherwise.

self中に含まれる多重集合を平滑化します。 平滑化した多重集合が1つでもあればselfを、 そうでなければnilを返します。

# File lib/multiset.rb, line 763
def flatten!
  ret = nil
  self.to_a.each do |item|
    if item.kind_of?(Multiset)
      self.delete(item)
      self.merge!(item.flatten)
      ret = self
    end
  end
  ret
end
grep(pattern) { |item| ... } click to toggle source

Collects items in self satisfying pattern (pattern === item). If a block is given, the items are converted by the result of the block.

patternの条件を満たした(pattern === item)要素のみを集めた多重集合を返します。 ブロックが与えられている場合は、さらにその結果を適用した結果を返します。

# File lib/multiset.rb, line 972
def grep(pattern)
  ret = Multiset.new
  @entries.each_pair do |item, count|
    if pattern === item
      ret.add((block_given? ? yield(item) : item), count)
    end
  end
  ret
end
group_by() { |item| ... } click to toggle source

Classify items in self by returned value from block. Returns a Multimap whose values are associated with keys, where the keys are the returned value from given block.

selfの要素を、与えられたブロックからの返り値によって分類します。 ブロックからの返り値をキーとして値を対応付けたMultimapを返します。

# File lib/multiset.rb, line 849
def group_by
  ret = Multimap.new
  @entries.each_pair do |item, count|
    ret[yield(item)].add(item, count)
  end
  ret
end
Also aliased as: classify
group_by_with() { |item, count| ... } click to toggle source

Same as Multiset#group_by except that the pairs of (non-duplicated) items and their counts are given to block.

Multiset#group_byと同様ですが、ブロックには要素とその個数の組が与えられます。

# File lib/multiset.rb, line 862
def group_by_with
  ret = Multimap.new
  @entries.each_pair do |item, count|
    ret[yield(item, count)].add(item, count)
  end
  ret
end
Also aliased as: classify_with
include?(item) click to toggle source

Returns whether self has item.

itemself中に含まれているかを返します。

# File lib/multiset.rb, line 260
def include?(item)
  @entries.has_key?(item)
end
Also aliased as: member?
inject_with(init) { |init, item, count| ... } click to toggle source

Three elements are given to the block for each (non-duplicated) items in self: the last result of the block, the item, and its number in self. As for the first block call, the first argument is init. The result of the last block call is returned.

Different from Enumerable#inject, init cannot be omitted. In addition, Symbol cannot be given instead of a block.

ブロックに「1回前のブロック呼び出しの返り値」「selfの要素」「その個数」の 3つ組を順次与え、最後にブロックを呼んだ結果を返します。ただし「1回前のブロック呼び出しの返り値」は、 1回目のブロック呼び出しの際については、代わりにinitの値が与えられます。

Enumerable#injectと異なり、initは省略できません。 またブロックの代わりにSymbolを与えることもできません。

# File lib/multiset.rb, line 996
def inject_with(init)
  @entries.each_pair do |item, count|
    init = yield(init, item, count)
  end
  init
end
items() click to toggle source

Returns an array with all items in self, without duplication.

selfに含まれている要素(重複は除く)からなる配列を返します。

# File lib/multiset.rb, line 243
def items
  @entries.keys
end
length()
Alias for: size
listing(delim = "\n") { |item| ... } click to toggle source

Lists all items with duplication in self. Items are deliminated with delim, and items are converted to string in the given block. If block is omitted, Object#inspect is used.

selfの全要素を(重複を許して)並べた文字列を返します。 要素間の区切りはdelimの値を用い、 各要素の表示形式は与えられたブロックの返り値(なければObject#inspect)を用います。

# File lib/multiset.rb, line 273
def listing(delim = "\n")
  buf = ''
  init = true
  self.each do |item|
    if init
      init = false
    else
      buf += delim
    end
    buf += block_given? ? yield(item).to_s : item.inspect
  end
  buf
end
map() { |item| ... } click to toggle source

Gives all items in self (without duplication) to given block, and generates a new multiset whose values are returned value from the block.

selfの各要素(重複なし)をブロックに与え、返り値を集めたものからなる 多重集合を生成します。

# File lib/multiset.rb, line 671
def map # :yields: item
  ret = Multiset.new
  @entries.each_pair do |item, count|
    ret.add(yield(item), count)
  end
  ret
end
Also aliased as: collect
map!() { |item| ... } click to toggle source

Same as Multiset#map, except that self is replaced by the resulted multiset. Returns self.

Multiset#mapと同様の処理を行いますが、結果として生成される多重集合でselfが 置き換えられます。selfを返します。

# File lib/multiset.rb, line 685
def map!(&block) # :yields: item
  self.replace(self.map(&block))
  self
end
Also aliased as: collect!
map_with() { |item, count| ... } click to toggle source

Gives all pairs of (non-duplicated) items and their numbers in self to given block. The block must return an array of two items. Generates a new multiset whose values and numbers are the first and second item of returned array, respectively.

selfの要素(重複なし)とその個数の組をブロックに与えます。 ブロックから2要素の配列を受け取り、前者を要素、後者をその個数とした 多重集合を生成します。

# File lib/multiset.rb, line 699
def map_with
  ret = Multiset.new
  @entries.each_pair do |item, count|
    val = yield(item, count)
    ret.add(val[0], val[1])
  end
  ret
end
Also aliased as: collect_with
map_with!() { |item, count| ... } click to toggle source

Same as Multiset#map_with, except that self by the resulted multiset. Returns self.

Multiset#map_withと同様ですが、結果として生成される多重集合で selfが置き換えられます。selfを返します。

# File lib/multiset.rb, line 714
def map_with!
  self.to_hash.each_pair do |item, count|
    self.delete(item, count)
    val = yield(item, count)
    self.add(val[0], val[1])
  end
  self
end
Also aliased as: collect_with!
max() { |a, b| ... } click to toggle source

Returns the largest item in self, or nil if no item is stored in self. If a block is given, they are ordered by giving pairs of items to the block.

最大の要素を返します。 要素が存在しない場合はnilを返します。 ブロックが与えられた場合は、要素間の大小判定を、ブロックに2つの要素を与えることで行います。

# File lib/multiset.rb, line 1010
def max(&block) # :yields: a, b
  @entries.keys.max(&block)
end
max_by() { |item| ... } click to toggle source

Returns the largest item by comparing the items in self by the results of the block. If no item is stored in self, nil is returned.

ブロックの値を評価した結果が最大になるような要素を返します。 要素が存在しない場合はnilを返します。

# File lib/multiset.rb, line 1041
def max_by(&block) # :yields: item
  @entries.keys.max_by(&block)
end
max_by_with() { |item, count| ... } click to toggle source

Same as Multiset#max_by except that pairs of (non-duplicated) items and their counts are given to the block.

Multiset#max_by と同様ですが、ブロックには要素(重複なし)とその出現数の組が与えられます。

# File lib/multiset.rb, line 1099
def max_by_with(&block) # :yields: item, count
  tmp = @entries.each_pair.max_by(&block)
  tmp ? tmp[0] : nil # if @entries is not empty, tmp must be a two-element array
end
max_with() { |item1, count1, item2, count2| ... } click to toggle source

Same as Multiset#max except that the following four: “item 1”, “number of item 1”, “item 2” and “number of item 2” are given to the block.

Multiset#max と同様ですが、ブロックには「要素1」「要素1の出現数」「要素2」「要素2の出現数」の 4引数が与えられます。

# File lib/multiset.rb, line 1070
def max_with # :yields: item1, count1, item2, count2
  tmp = @entries.each_pair.max{ |a, b| yield(a[0], a[1], b[0], b[1]) }
  tmp ? tmp[0] : nil
end
member?(item)
Alias for: include?
merge(other) click to toggle source

Returns a multiset merging self and other.

selfotherの要素を合わせた多重集合を返します。

# File lib/multiset.rb, line 513
def merge(other)
  ret = self.dup
  other.each_pair do |item, count|
    ret.add(item, count)
  end
  ret
end
Also aliased as: +
merge!(other) click to toggle source

Merges other to self. See also Multiset#merge . Returns self.

selfotherの要素を追加します。 Multiset#merge も参照してください。 selfを返します。

# File lib/multiset.rb, line 529
def merge!(other)
  other.each_pair do |item, count|
    self.add(item, count)
  end
  self
end
min() { |a, b| ... } click to toggle source

Returns the smallest item in self, or nil if no item is stored in self. If a block is given, they are ordered by giving pairs of items to the block.

最小の要素を返します。 要素が存在しない場合はnilを返します。 ブロックが与えられた場合は、要素間の大小判定を、ブロックに2つの要素を与えることで行います。

# File lib/multiset.rb, line 1021
def min(&block) # :yields: a, b
  @entries.keys.min(&block)
end
min_by() { |item| ... } click to toggle source

Returns the largest item by comparing the items in self by the results of the block. If no item is stored in self, nil is returned.

ブロックの値を評価した結果が最小になるような要素を返します。 要素が存在しない場合はnilを返します。

# File lib/multiset.rb, line 1051
def min_by(&block) # :yields: item
  @entries.keys.min_by(&block)
end
min_by_with() { |item, count| ... } click to toggle source

Same as Multiset#min_by except that pairs of (non-duplicated) items and their counts are given to the block.

Multiset#max_by と同様ですが、ブロックには要素(重複なし)とその出現数の組が与えられます。

# File lib/multiset.rb, line 1108
def min_by_with(&block) # :yields: item, count
  tmp = @entries.each_pair.min_by(&block)
  tmp ? tmp[0] : nil # if @entries is not empty, tmp must be a two-element array
end
min_with() { |item1, count1, item2, count2| ... } click to toggle source

Same as Multiset#min except that the following four: “item 1”, “number of item 1”, “item 2” and “number of item 2” are given to the block.

Multiset#min と同様ですが、ブロックには「要素1」「要素1の出現数」「要素2」「要素2の出現数」の 4引数が与えられます。

# File lib/multiset.rb, line 1080
def min_with # :yields: item1, count1, item2, count2
  tmp = @entries.each_pair.min{ |a, b| yield(a[0], a[1], b[0], b[1]) }
  tmp ? tmp[0] : nil
end
minmax() { |a, b| ... } click to toggle source

Returns the pair consisting of the smallest and the largest item in self, or nil if no item is stored in self. If a block is given, they are ordered by giving pairs of items to the block.

最小の要素と最大の要素の組を返します。 ブロックが与えられた場合は、要素間の大小判定を、ブロックに2つの要素を与えることで行います。

# File lib/multiset.rb, line 1031
def minmax(&block) # :yields: a, b
  @entries.keys.minmax(&block)
end
minmax_by() { |item| ... } click to toggle source

Returns the pair consisting of the smallest and the largest items in self by comparing the items by the results of the block. If no item is stored in self, nil is returned.

ブロックの値を評価した結果が最小になる要素と最大になる要素の組を返します。 要素が存在しない場合はnilを返します。

# File lib/multiset.rb, line 1061
def minmax_by(&block) # :yields: item
  @entries.keys.minmax_by(&block)
end
minmax_by_with() { |item, count| ... } click to toggle source

Same as Multiset#minmax_by except that pairs of (non-duplicated) items and their counts are given to the block.

Multiset#minmax_by と同様ですが、ブロックには要素(重複なし)とその出現数の組が与えられます。

# File lib/multiset.rb, line 1117
def minmax_by_with(&block) # :yields: item, count
  tmp = @entries.each_pair.minmax_by(&block)
  tmp[0] ? [tmp[0][0], tmp[1][0]] : nil
end
minmax_with() { |item1, count1, item2, count2| ... } click to toggle source

Same as Multiset#minmax except that the following four: “item 1”, “number of item 1”, “item 2” and “number of item 2” are given to the block.

Multiset#minmax と同様ですが、ブロックには「要素1」「要素1の出現数」「要素2」「要素2の出現数」の 4引数が与えられます。

# File lib/multiset.rb, line 1090
def minmax_with # :yields: item1, count1, item2, count2
  tmp = @entries.each_pair.minmax{ |a, b| yield(a[0], a[1], b[0], b[1]) }
  tmp ? [tmp[0][0], tmp[1][0]] : nil
end
proper_subset?(other) click to toggle source

Returns whether self is a proper subset of other, that is, it returns true if subset? is satisfied and self is not equal to other.

selfotherに真に含まれているかどうかを返します。 すなわち、 subset? の条件に加えて両者が一致しなければ真となります。

# File lib/multiset.rb, line 488
def proper_subset?(other)
  unless other.instance_of?(Multiset)
    raise ArgumentError, "Argument must be a Multiset"
  end
  self.subset?(other) && self != other
end
proper_superset?(other) click to toggle source

Returns whether self is a proper superset of other, that is, it returns true if superset? is satisfied and self is not equal to other.

selfotherを真に含んでいるかどうかを返します。 すなわち、 superset? の条件に加えて両者が一致しなければ真となります。

# File lib/multiset.rb, line 461
def proper_superset?(other)
  unless other.instance_of?(Multiset)
    raise ArgumentError, "Argument must be a Multiset"
  end
  self.superset?(other) && self != other
end
rand()
Alias for: sample
reject() { |item| ... } click to toggle source

Gives all items in self (without duplication) to given block, and returns a multiset collecting the items such that the block returns false.

ブロックにselfの要素(重複なし)を順次与え、 結果が偽であった要素のみを集めたMultisetを返します。

# File lib/multiset.rb, line 780
def reject
  ret = Multiset.new
  @entries.each_pair do |item, count|
    ret.renew_count(item, count) unless yield(item)
  end
  ret
end
reject!() { |item| ... } click to toggle source

Same as Multiset#delete_if except that this returns nil if no item is deleted.

Multiset#delete_ifと似ますが、要素が1つも削除されなければnilを返します。

# File lib/multiset.rb, line 804
def reject!
  ret = nil
  @entries.each_pair do |item, count|
    if yield(item)
      self.delete_all(item)
      ret = self
    end
  end
  ret
end
reject_with() { |item, count| ... } click to toggle source

Gives all pairs of (non-duplicated) items and counts in self to given block, and returns a multiset collecting the items such that the block returns false.

ブロックにselfの要素(重複なし)と個数の組を順次与え、 結果が偽であった要素のみを集めたMultisetを返します。

# File lib/multiset.rb, line 793
def reject_with
  ret = Multiset.new
  @entries.each_pair do |item, count|
    ret.renew_count(item, count) unless yield(item, count)
  end
  ret
end
renew_count(item, number) click to toggle source

Sets the number of item in self as number. If number is negative, it is considered as number = 0. Returns self if succeeded, nil otherwise.

selfに含まれるitemの個数をnumber個にします。 numberが負の数であった場合は、number = 0とみなします。 成功した場合はselfを、失敗した場合はnilを返します。

# File lib/multiset.rb, line 368
def renew_count(item, number)
  return nil if number == nil
  n = number.to_i
  if n > 0
    @entries[item] = n
  else
    @entries.delete(item)
  end
  self
end
replace(other) click to toggle source

Replaces self by other. Returns self.

selfの内容をotherのものに置き換えます。 selfを返します。

# File lib/multiset.rb, line 217
def replace(other)
  @entries.clear
  other.each_pair do |item, count|
    self.renew_count(item, count)
  end
  self
end
sample() click to toggle source

Returns one item in self at random in the same probability. Returns nil in case the multiset is empty.

selfの要素を無作為に1つ選んで返します。 すべての要素は等確率で選ばれます。 空のMultisetに対して呼び出した場合はnilを返します。

# File lib/multiset.rb, line 731
def sample
  return nil if empty?
  pos = Kernel.rand(self.size)
  @entries.each_pair do |item, count|
    pos -= count
    return item if pos < 0
  end
end
Also aliased as: rand
select()
Alias for: find_all
select_with()
Alias for: find_all_with
size() click to toggle source

Returns number of all items in self.

selfに含まれている要素数を返します。

# File lib/multiset.rb, line 228
def size
  @entries.inject(0){ |sum, item| sum += item[1] }
end
Also aliased as: length
sort() { |a, b| ... } click to toggle source

Generates an array by sorting the items in self.

selfの要素を並び替えた配列を生成します。

# File lib/multiset.rb, line 1125
def sort(&block) # :yields: a, b
  ret = []
  @entries.keys.sort(&block).each do |item|
    ret.fill(item, ret.length, @entries[item])
  end
  ret
end
sort_by() { |item| ... } click to toggle source

The same as Multiset#sort except that, after giving the items to the block, the items are sorted by the values from the block.

Multiset#sortと同様ですが、ブロックには1つの要素が与えられ、その値が小さいものから順に並びます。

# File lib/multiset.rb, line 1137
def sort_by(&block) # :yields: item
  ret = []
  @entries.keys.sort_by(&block).each do |item|
    ret.fill(item, ret.length, @entries[item])
  end
  ret
end
sort_by_with() { |item1, count1, item2, count2| ... } click to toggle source

Same as Multiset#sort_by except that the pairs of (non-duplicated) items and their counts are given to the block.

Multiset#sort_by と同様ですが、ブロックには要素(重複なし)とその出現数の組が与えられます。

# File lib/multiset.rb, line 1162
def sort_by_with # :yields: item1, count1, item2, count2
  ret = []
  @entries.each_pair.sort_by{ |a| yield(*a) }.each do |item_count|
    ret.fill(item_count[0], ret.length, item_count[1])
  end
  ret
end
sort_with() { |item1, count1, item2, count2| ... } click to toggle source

Same as Multiset#sort except that the following four: “item 1”, “number of item 1”, “item 2” and “number of item 2” are given to the block.

Multiset#sort と同様ですが、ブロックには「要素1」「要素1の出現数」「要素2」「要素2の出現数」の 4引数が与えられます。

# File lib/multiset.rb, line 1150
def sort_with # :yields: item1, count1, item2, count2
  ret = []
  @entries.each_pair.sort{ |a, b| yield(a[0], a[1], b[0], b[1]) }.each do |item_count|
    ret.fill(item_count[0], ret.length, item_count[1])
  end
  ret
end
subset?(other) click to toggle source

Returns whether self is a subset of other, that is, for any item, the number of it in self is equal to or smaller than that in other.

selfotherを含んでいるかどうかを返します。 すなわち、いかなる要素についても、それがselfに含まれている 個数がotherに含まれている数以下であるかを返します。

# File lib/multiset.rb, line 475
def subset?(other)
  unless other.instance_of?(Multiset)
    raise ArgumentError, "Argument must be a Multiset"
  end
  compare_set_with(other){ |s, o| s <= o }
end
subtract(other) click to toggle source

Returns a multiset such that items in other are removed from self, where 'removed' means that, for each item in other, the items of the number in other are removed from self.

selfからotherの要素を取り除いた多重集合を返します。 ここで「取り除く」ことは、otherの各要素について、 それをotherにある個数分selfから取り除くことをいいます。

# File lib/multiset.rb, line 543
def subtract(other)
  ret = self.dup
  other.each_pair do |item, count|
    ret.delete(item, count)
  end
  ret
end
Also aliased as: -
subtract!(other) click to toggle source

Removes items in other from self. See also Multiset#subtract . Returns self.

selfからotherの要素を削除します。 Multiset#subtract も参照してください。 selfを返します。

# File lib/multiset.rb, line 559
def subtract!(other)
  other.each_pair do |item, count|
    self.delete(item, count)
  end
  self
end
superset?(other) click to toggle source

Returns whether self is a superset of other, that is, for any item, the number of it in self is equal to or larger than that in other.

selfotherを含んでいるかどうかを返します。 すなわち、いかなる要素についても、それがselfに含まれている 個数がotherに含まれている数以上であるかを返します。

# File lib/multiset.rb, line 448
def superset?(other)
  unless other.instance_of?(Multiset)
    raise ArgumentError, "Argument must be a Multiset"
  end
  compare_set_with(other){ |s, o| s >= o }
end
to_a() click to toggle source

Converts self to an array.

selfを配列に変換して返します。

# File lib/multiset.rb, line 181
def to_a
  ret = []
  @entries.each_pair do |item, count|
    ret.concat Array.new(count, item)
  end
  ret
end
to_hash() click to toggle source

Converts self to a Hash. See Hash#to_multiset about format of generated hash.

selfHashに変換して返します。 生成されるハッシュの構造については、Hash#to_multisetをご覧下さい。

# File lib/multiset.rb, line 147
def to_hash
  @entries.dup
end
to_s(delim = "\n") { |item| ... } click to toggle source

Lists all items without duplication and its number in self. Items are deliminated with delim, and items are converted to string in the given block. If block is omitted, Object#inspect is used.

selfの要素と要素数の組を並べた文字列を返します。 要素間の区切りはdelimの値を用い、 各要素の表示形式は与えられたブロックの返り値(なければObject#inspect)を用います。

# File lib/multiset.rb, line 295
def to_s(delim = "\n")
  buf = ''
  init = true
  @entries.each_pair do |item, count|
    if init
      init = false
    else
      buf += delim
    end
    item_tmp = block_given? ? yield(item) : item.inspect
    buf += "\##{count} #{item_tmp}"
  end
  buf
end
to_set() click to toggle source

Converts self to ordinary set (The Set class attached to Ruby by default).

require "set" is performed when this method is called.

Note: To convert an instance of Set to Multiset, use Multiset.new(instance_of_set).

selfを通常の集合(Ruby標準添付のSet)に 変換したものを返します。

このメソッドを呼び出すと、require "set"が行われます。

注:逆に、SetのインスタンスをMultisetに変換するには、 Multiset.new(instance_of_set)で可能です。

# File lib/multiset.rb, line 173
def to_set
  require "set"
  Set.new(@entries.keys)
end
|(other) click to toggle source

Returns the union of self and other, that is, for each item either or both in self and other, the multiset includes it in the larger number of the two.

selfotherの和集合からなる多重集合を返します。 すなわち、selfotherの少なくとも一方に存在する要素について、 多いほうの個数を持った多重集合を返します。

# File lib/multiset.rb, line 588
def |(other)
  ret = self.dup
  other.each_pair do |item, count|
    ret.renew_count(item, [self.count(item), count].max)
  end
  ret
end