Class Multiset
In: multiset/libmultiset.rb
Parent: Object

概要(Basic information)

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

メソッド名は基本的にSetクラスに合わせてあります。またSetクラスが持つ メソッドの大部分を実装していますが、いくつか未実装なものもあります。

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

Most methods’ names are same as those of Set class, and all other than a few methods in Set class is implemented on Multiset class.

  • Set[:a,:b,:c,:b,:b,:c] => #<Set: {:b, :c, :a}>
  • Multiset[:a,:b,:c,:b,:b,:c] => #<Multiset:#3 :b, #2 :c, #1 :a>

Methods

&   +   -   <<   ==   []   add   classify   classify_with   clear   collect   collect!   collect_with   collect_with!   count   delete   delete_all   delete_if   delete_with   dup   each   each_item   each_pair   empty?   flatten   flatten!   include?   items   length   listing   map   map!   map_with   map_with!   member?   merge   merge!   new   parse   parse_force   parse_string   parse_string?   proper_subset?   proper_superset?   rand   reject!   renew_count   replace   size   subset?   subtract   subtract!   superset?   to_a   to_hash   to_s   to_set   |  

Included Modules

Enumerable

Public Class methods

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

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

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

This method is mainly used when you generate literal of multiset.

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

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

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

list must be Enumerable. If not, ArgumentError is raised.

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

  • objectMultisetのインスタンスである場合、 その複製を返します。
  • objectMultisetのインスタンスでなく、 かつeach_pairメソッドを持っている場合、 each_pairから渡される2つの引数について、前者を要素、 後者をその個数とした多重集合を生成します。Hash#to_multisetも ご覧下さい。
  • objecteach_pairメソッドを持っておらず、 かつEnumerableである場合は、Multiset#newと同じ結果です。
  • それ以外の場合は、例外ArgumentErrorが発生します。

Generates a multiset 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, first argument becomes item in multiset and second argument becomes its number. See also Hash#to_multiset .
  • If object does not have the method each_pair and object includes Enumerable, this method results equal to Multiset#new .
  • Otherwise, ArgumentError is raised.

Multiset.parseと同様ですが、挙動がMultiset.parse_stringの設定に 影響されません(Multiset.parse_string(true)の場合と 同様とみなします)。

Same as Multiset.parse, but this method is not affected by the setting of Multiset.parse_string (treats Multiset.parse_string(true)) .

Multiset.parseの引数にStringを渡すことを許すか指定します。

StringはEnumerableなので、Multiset.parseでは引数として文字列を 渡すこともできます。しかしこれはバグの危険性があるため、 このメソッドで禁止することができます。

booleanにはtrueおよびfalseが指定できます。 falseを設定した場合、Multiset.parseMultimap#[]=などで 文字列を直接指定した場合にArgumentErrorが発生します。 デフォルト値はtrueです。

booleanを省略した場合は、現在のこの値がtrueか falseかを返します。

この値はクラス変数で管理されているので、一度変更するとすべての インスタンスに影響が及びます。

Allows or prohibits Strings from being arguments of Multiset.parse .

String includes module Enumerable, therefore you can give a string as an argument of Multiset.parse . But this may cause bugs, so you can prohibit this.

boolean can be true or false. If this value is false, ArgumentError is raised whenever you specify a string as an argument of Multiset.parse , Multimap#[]= , and so on. This value is true by default.

If boolean is not specified, returns current value (true or false).

This value is stored in class variable, therefore this value has effect for all instances of Multiset.

Public Instance methods

selfotherの積集合からなる多重集合を返します。

Returns intersection of self and other.

+(other)

Alias for merge

-(other)

Alias for subtract

<<(item, addcount = 1)

Alias for add

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

Returns whether self is equal to other.

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

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

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

Classify items in self by returned value from block. Returns a Multimap whose values are associated with keys. Keys’ are defined by returned value from given block.

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

Same as classify, but the pair of item and its number is given to block.

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

Deletes all items in self. Returns self.

collect()

Alias for map

collect!()

Alias for map!

collect_with()

Alias for map_with

collect_with!()

Alias for map_with!

self中に含まれるitemの個数を返します。

Returns number of items in self.

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

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

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

Deletes all items in self. Returns self.

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

Gives all items in self to given block, and deletes that item if the block returns true. Returns self.

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

Gives each pair of item and its number to given block, and deletes those items if the block returns true. Returns self.

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

Returns duplicated self.

selfに含まれるすべての要素について繰り返します。 selfを返します。

Iterates for each item in self. Returns self.

selfに含まれるすべての要素について、重複を許さずに繰り返します。 selfを返します。

Iterates for each item in self, without duplication. Returns self.

selfに含まれるすべての要素とその個数について繰り返します。 selfを返します。

Iterates for each pair of item and its number in self. Returns self.

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

Returns whether self has no item.

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

Generates a multiset such that multisets in self are flattened.

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

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

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

Returns whether self has item.

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

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

length()

Alias for size

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

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の各要素をブロックに与え、返り値を集めたものからなる 多重集合を生成します。

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

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

Same as Multiset#map, but replaces self by resulting multiset. Returns self.

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

Gives all pairs of 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.

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

Same as Multiset#map_with, but replaces self by resulting multiset. Returns self.

member?(item)

Alias for include?

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

Returns merged multiset of self and other.

selfotherの要素を追加します。 selfを返します。

Merges other to self. Returns self.

selfotherに真に含まれているかどうかを返します。 「真に」とは、両者が一致する場合は含めないことを示します。

Returns whether self is a proper subset of other.

selfotherを真に含んでいるかどうかを返します。 「真に」とは、両者が一致する場合は含めないことを示します。

Returns whether self is a proper superset of other.

selfの要素を無作為に1つ選んで返します。 すべての要素は等確率で選ばれます。

Returns one item in self randomly. All items are selected with same probability.

delete_ifと同じですが、要素が1つも削除されなければnilを返します。

Same as delete_if, but returns nil if no item is deleted.

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

Sets number of item to number in self. If number is negative, treats as number = 0. Returns self if succeeded, nil otherwise.

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

Replaces self by other. Returns self.

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

Returns number of all items in self.

selfotherに含まれているかどうかを返します。

Returns whether self is a subset of other.

selfからotherの要素を取り除いた多重集合を返します。

Returns multiset such that items in other are removed from self.

selfからotherの要素を削除します。 selfを返します。

Removes items in other from self. Returns self.

selfotherを含んでいるかどうかを返します。

Returns whether self is a superset of other.

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

Converts self to an array.

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

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

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

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を通常の集合(Ruby標準添付のSet)に 変換したものを返します。

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

なおSetをMultisetに変換するには、Multiset.new(instance_of_set)で 可能です。

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

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

To convert Set to Multiset, use Multiset.new(instance_of_set).

selfotherの和集合からなる多重集合を返します。

Returns union of self and other.

[Validate]