class Multimap
概要(Basic information)¶ ↑
Ruby implementation of multimap. Unlike ordinary map, also known as associative array (see Ruby documentation for "Hash" class), multimap can contain two or more items for a key. Methods' names are basically consistent with those of Hash class. Rubyによる多重連想配列(マルチマップ)の実装です。 通常の連想配列(Rubyでは"Hash"クラス)と異なり、多重連想配列は 1つのキーに対して複数の要素が存在し得ます。 メソッド名は基本的にHashクラスに合わせてあります。
Public Class Methods
Generates a new multimap. Different from Hash#new , you cannot specify default value.
新しい多重連想配列を生成します。Hash#newと異なり、デフォルト値は 設定できません。
# File lib/multimap.rb, line 32 def initialize @assoc = Hash.new{ |hash, key| hash[key] = Multiset.new } end
Public Instance Methods
Returns whether self is equal to other.
selfがotherと等しいかどうかを返します。
# File lib/multimap.rb, line 84 def ==(other) return false unless other.instance_of?(Multimap) @assoc == other.to_hash end
Removes all elements stored in self. Returns
self.
selfに格納された要素をすべて削除します。 selfを返します。
# File lib/multimap.rb, line 103 def clear @assoc.clear end
Deletes all values associated with key, and returns those
values as a Multiset.
keyに割り当てられた全ての値を削除し、その値を Multisetとして返します。
# File lib/multimap.rb, line 132 def delete(key) ret = @assoc[key] @assoc.delete(key) ret end
Gives all pairs of a key and single value in self to given
block, and deletes that element if the block returns true. Returns
self.
ブロックにselfのキーと値の組(値は1つ)を順次与え、 結果が真であった組をすべて削除します。
selfを返します。
# File lib/multimap.rb, line 158 def delete_if(&block) # :yields: key, single_value cleanup @assoc.each_pair do |key, value_list| value_list.delete_if{ |single_value| block.call(key, single_value) } end self end
Same as #delete_if except that arguments given to block is the following three: (key, single value associated with the key, numbers of that value associated with the key).
#delete_ifと同じですが、ブロックへの引数が(キー、キーに割り当てられた値、 その値がキーに割り当てられている個数)の3つの組で与えられます。
# File lib/multimap.rb, line 202 def delete_with(&block) # :yields: key, a_value, count cleanup @assoc.each_pair do |key, value_list| value_list.delete_with{ |a_value, count| block.call(key, a_value, count) } end self end
Returns duplicated self.
selfの複製を生成して返します。
# File lib/multimap.rb, line 110 def dup @assoc.to_multimap end
Iterates for each key in self. Returns self.
selfのすべてのキーについて繰り返します。 selfを返します。
# File lib/multimap.rb, line 271 def each_key(&block) # :yields: key if block_given? cleanup @assoc.each_key &block else Enumerator.new(self, :each_key) end end
Iterates for each pair of a key and a value in self. Returns
self.
selfのすべてのキーと値の組について繰り返します。 selfを返します。
# File lib/multimap.rb, line 217 def each_pair if block_given? cleanup @assoc.each_pair do |key, value_list| value_list.each do |single_value| yield key, single_value end end self else Enumerator.new(self, :each_pair) end end
Iterates for each pair of a key and all values associated with the key
(list of values is given as Multiset) in
self. Returns self.
selfのすべてのキーと、そのキーに割り当てられた すべての値(Multisetで与えられる)の組について繰り返します。
selfを返します。
# File lib/multimap.rb, line 261 def each_pair_list(&block) # :yields: key, value_list cleanup @assoc.each_pair &block end
Iterates for each pair of a key and a value in self, giving
the following three to block: (key, single value associated with the key,
numbers of that value associated with the key). Returns self.
selfのすべてのキーと値の組について、 ブロックに(キー、キーに割り当てられた値、その値が割り当てられた数)
の組を与えながら繰り返します。selfを返します。
# File lib/multimap.rb, line 240 def each_pair_with if block_given? cleanup @assoc.each_pair do |key, value_list| value_list.each_pair do |a_value, count| yield key, a_value, count end end self else Enumerator.new(self, :each_pair_with) end end
Iterates for each value in self. Returns self.
selfのすべての値について繰り返します。 selfを返します。
# File lib/multimap.rb, line 284 def each_value(&block) # :yields: single_value if block_given? cleanup @assoc.each_value do |value_list| value_list.each &block end self else Enumerator.new(self, :each_value) end end
Returns whether self has no element.
selfに要素がないかどうかを返します。
# File lib/multimap.rb, line 320 def empty? cleanup @assoc.empty? end
Returns values associated with key, which may exist two or
more, by the format of Multiset. If
key has not associated with any value, #fetch returns empty Multiset. Different from Hash#fetch, you cannot
specify a value or a process when key has not associated with
any value.
キーkeyに対応する値(複数存在しうる)を、 Multisetとして返します。
キーに対応する値が存在しない場合、空のMultisetが返ります。 Hash#fetchの場合と異なり、キーに対応する値が存在しない場合の扱いを
指定することはできません。
# File lib/multimap.rb, line 56 def fetch(key) @assoc[key] end
Returns whether self has a key key.
selfにキーkeyかあるかどうかを返します。
# File lib/multimap.rb, line 328 def has_key?(key) cleanup @assoc.has_key?(key) end
Returns whether self has a value value.
selfに値valueかあるかどうかを返します。
# File lib/multimap.rb, line 339 def has_value?(value) self.values.items.include?(value) end
Returns a Multimap whose keys are values in
self, and values are keys in self. For example,
If self has a key :a associated with two :x and one :y,
returned multimap has two keys :x and :y, and their values are two :a and
one :a respectively.
selfのキーと値を入れ替えたMultimapを返します。
例えばキー:aに対応する値が2つの:xと1つの:yであれば、変換結果は キー:xに:aが2つ、キー:yに:aが1つ対応するMultimapです。
# File lib/multimap.rb, line 383 def invert ret = Multimap.new self.each_pair_with do |key, a_value, count| ret[a_value].add key, count end ret end
Search a pair of key and value from self such that the value
is equal to the argument value. If two or keys are matched,
returns one of them. If no key is matched, returns nil.
selfから値がvalueであるような要素を
検索し、それに対応するキーを返します。該当するキーが複数存在する場合、 そのうちの1つを返します。該当するキーが存在しなければ
nilを返します。
# File lib/multimap.rb, line 353 def key(value) self.each_pair_with do |key, a_value, count| return key if value == a_value end nil end
Returns an array in which keys in self are stored.
selfのすべてのキーを、配列として返します。
# File lib/multimap.rb, line 299 def keys cleanup @assoc.keys end
Returns merged multiset of self and other.
selfとotherの要素を合わせた多重集合を返します。
# File lib/multimap.rb, line 416 def merge(other) ret = self.dup ret.merge! other end
Add elements in other to self. Returns
self.
selfにotherの要素を追加します。 selfを返します。
# File lib/multimap.rb, line 406 def merge!(other) other.each_pair_with do |key, a_value, count| self[key].add a_value, count end self end
Same as #delete_if except
that, rather than deleting key-value pairs in self, this
generates a new Multimap with specified
key-value pairs are deleted.
#delete_ifと似ますが、self自身からはキーと値の組を
削除せず、要素が削除された結果の多重連想配列を新たに生成して 返します。
# File lib/multimap.rb, line 145 def reject(&block) # :yields: key, single_value ret = self.dup ret.delete_if &block ret end
Same as #delete_if except
that nil is returned if no key-value pair is deleted.
#delete_ifと似ますが、キーと値の組が1つも削除されなければ
nilを返します。
# File lib/multimap.rb, line 173 def reject!(&block) # :yields: key, single_value cleanup ret = nil @assoc.each_pair do |key, value_list| ret = self if value_list.reject!{ |single_value| block.call(key, single_value) } end ret end
Same as #reject except that arguments given to block is the following three: (key, single value associated with the key, numbers of that value associated with the key).
#rejectと似ますが、ブロックへの引数が(キー、キーに割り当てられた値、 その値がキーに割り当てられている個数)の3つの組で与えられます。
# File lib/multimap.rb, line 190 def reject_with(&block) # :yields: key, a_value, count ret = self.dup ret.delete_with &block ret end
Replaces self by other. Returns
self.
selfの内容をotherのものに置き換えます。 selfを返します。
# File lib/multimap.rb, line 119 def replace(other) @assoc.clear other.each_pair_with do |key, a_value, count| @assoc[key].add a_value, count end self end
Returns number of all elements in self.
selfに含まれている要素数を返します。
# File lib/multimap.rb, line 394 def size ret = 0 self.each_pair_with{ |key, a_value, count| ret += count } ret end
Sets values associated with key to value_list.
value_list is converted to a Multiset by Multiset.parse .
Returns value_list.
キーkeyに対応する値(複数存在しうる)を value_listで置き換えます。この際、
value_listはMultiset.parseを用いてMultisetに変換されます。
value_listを返します。
# File lib/multimap.rb, line 71 def store(key, value_list) if value_list.class == Multiset @assoc[key] = value_list.dup else @assoc[key] = Multiset.parse(value_list) end value_list end
Converts self to a Hash whose values in the Hash are all multimaps.
selfをHashに変換して返します。
返されるHash中において、値はすべてMultimap型となります。
# File lib/multimap.rb, line 94 def to_hash @assoc.dup end
Returns a Multiset in which values in
self are stored.
selfのすべての値を、Multisetとして返します。
# File lib/multimap.rb, line 308 def values cleanup ret = Multiset.new @assoc.each_value do |value_list| ret.merge! value_list end ret end
Retrieves values (instances of Multiset) of
self associated with key_list, and returns those
values as an array. i.e. returns an array whose elements are multisets.
selfからkey_listの各キーに対応する値
(Multiset型)を取り出し、それらを配列として返します。 すなわち、Multisetを要素とする配列を返します。
# File lib/multimap.rb, line 368 def values_at(*key_list) key_list.map{ |key| self[key] } end