Class: Multimap
Overview
概要(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クラスに合わせてあります。
Instance Method Summary collapse
-
#==(other) ⇒ Object
Returns whether
selfis equal toother. -
#clear ⇒ Object
Removes all elements stored in
self. -
#delete(key) ⇒ Object
Deletes all values associated with
key, and returns those values as a Multiset. -
#delete_if(&block) ⇒ Object
Gives all pairs of a key and single value in
selfto given block, and deletes that element if the block returns true. -
#delete_with(&block) ⇒ Object
Same as Multimap#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).
-
#dup ⇒ Object
Returns duplicated
self. -
#each_key(&block) ⇒ Object
Iterates for each key in
self. -
#each_pair ⇒ Object
(also: #each)
Iterates for each pair of a key and a value in
self. -
#each_pair_list(&block) ⇒ Object
Iterates for each pair of a key and all values associated with the key (list of values is given as Multiset) in
self. -
#each_pair_with ⇒ Object
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). -
#each_value(&block) ⇒ Object
Iterates for each value in
self. -
#empty? ⇒ Boolean
Returns whether
selfhas no element. -
#fetch(key) ⇒ Object
(also: #[])
Returns values associated with
key, which may exist two or more, by the format of Multiset. -
#has_key?(key) ⇒ Boolean
(also: #key?, #include?, #member?)
Returns whether
selfhas a keykey. -
#has_value?(value) ⇒ Boolean
(also: #value?)
Returns whether
selfhas a valuevalue. -
#initialize ⇒ Multimap
constructor
Generates a new multimap.
-
#inspect ⇒ Object
:nodoc:.
-
#invert ⇒ Object
Returns a Multimap whose keys are values in
self, and values are keys inself. -
#key(value) ⇒ Object
(also: #index)
Search a pair of key and value from
selfsuch that the value is equal to the argumentvalue. -
#keys ⇒ Object
Returns an array in which keys in
selfare stored. -
#merge(other) ⇒ Object
(also: #+)
Returns merged multiset of
selfandother. -
#merge!(other) ⇒ Object
Add elements in
othertoself. -
#reject(&block) ⇒ Object
Same as Multimap#delete_if except that, rather than deleting key-value pairs in
self, this generates a new Multimap with specified key-value pairs are deleted. -
#reject!(&block) ⇒ Object
Same as Multimap#delete_if except that
nilis returned if no key-value pair is deleted. -
#reject_with(&block) ⇒ Object
Same as Multimap#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).
-
#replace(other) ⇒ Object
Replaces
selfbyother. -
#size ⇒ Object
(also: #length)
Returns number of all elements in
self. -
#store(key, value_list) ⇒ Object
(also: #[]=)
Sets values associated with
keytovalue_list. -
#to_hash ⇒ Object
Converts
selfto aHashwhose values in the Hash are all multimaps. -
#to_s(delim = "\n") ⇒ Object
:nodoc:.
-
#values ⇒ Object
Returns a Multiset in which values in
selfare stored. -
#values_at(*key_list) ⇒ Object
(also: #indexes, #indices)
Retrieves values (instances of Multiset) of
selfassociated withkey_list, and returns those values as an array.
Constructor Details
Instance Method Details
#==(other) ⇒ Object
Returns whether self is equal to other.
selfがotherと等しいかどうかを返します。
85 86 87 88 |
# File 'lib/multimap.rb', line 85 def ==(other) return false unless other.instance_of?(Multimap) @assoc == other.to_hash end |
#clear ⇒ Object
Removes all elements stored in self. Returns self.
selfに格納された要素をすべて削除します。selfを返します。
104 105 106 |
# File 'lib/multimap.rb', line 104 def clear @assoc.clear end |
#delete(key) ⇒ Object
Deletes all values associated with key, and returns those values as a Multiset.
keyに割り当てられた全ての値を削除し、その値をMultisetとして返します。
133 134 135 136 137 |
# File 'lib/multimap.rb', line 133 def delete(key) ret = @assoc[key] @assoc.delete(key) ret end |
#delete_if(&block) ⇒ Object
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を返します。
159 160 161 162 163 164 165 166 167 |
# File 'lib/multimap.rb', line 159 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 |
#delete_with(&block) ⇒ Object
Same as Multimap#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).
Multimap#delete_ifと同じですが、ブロックへの引数が(キー、キーに割り当てられた値、その値がキーに割り当てられている個数)の3つの組で与えられます。
203 204 205 206 207 208 209 210 211 |
# File 'lib/multimap.rb', line 203 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 |
#dup ⇒ Object
Returns duplicated self.
selfの複製を生成して返します。
111 112 113 |
# File 'lib/multimap.rb', line 111 def dup @assoc.to_multimap end |
#each_key(&block) ⇒ Object
Iterates for each key in self. Returns self.
selfのすべてのキーについて繰り返します。selfを返します。
272 273 274 275 276 277 278 279 |
# File 'lib/multimap.rb', line 272 def each_key(&block) # :yields: key if block_given? cleanup @assoc.each_key &block else Enumerator.new(self, :each_key) end end |
#each_pair ⇒ Object Also known as: each
Iterates for each pair of a key and a value in self. Returns self.
selfのすべてのキーと値の組について繰り返します。selfを返します。
218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/multimap.rb', line 218 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 |
#each_pair_list(&block) ⇒ Object
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を返します。
262 263 264 265 |
# File 'lib/multimap.rb', line 262 def each_pair_list(&block) # :yields: key, value_list cleanup @assoc.each_pair &block end |
#each_pair_with ⇒ Object
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を返します。
241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/multimap.rb', line 241 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 |
#each_value(&block) ⇒ Object
Iterates for each value in self. Returns self.
selfのすべての値について繰り返します。selfを返します。
285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/multimap.rb', line 285 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 |
#empty? ⇒ Boolean
Returns whether self has no element.
selfに要素がないかどうかを返します。
321 322 323 324 |
# File 'lib/multimap.rb', line 321 def empty? cleanup @assoc.empty? end |
#fetch(key) ⇒ Object Also known as: []
Returns values associated with key, which may exist two or more, by the format of Multiset. If key has not associated with any value, Multimap#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の場合と異なり、キーに対応する値が存在しない場合の扱いを指定することはできません。
57 58 59 |
# File 'lib/multimap.rb', line 57 def fetch(key) @assoc[key] end |
#has_key?(key) ⇒ Boolean Also known as: key?, include?, member?
Returns whether self has a key key.
selfにキーkeyかあるかどうかを返します。
329 330 331 332 |
# File 'lib/multimap.rb', line 329 def has_key?(key) cleanup @assoc.has_key?(key) end |
#has_value?(value) ⇒ Boolean Also known as: value?
Returns whether self has a value value.
selfに値valueかあるかどうかを返します。
340 341 342 |
# File 'lib/multimap.rb', line 340 def has_value?(value) self.values.items.include?(value) end |
#inspect ⇒ Object
:nodoc:
450 451 452 453 454 455 |
# File 'lib/multimap.rb', line 450 def inspect # :nodoc: buf = "#<Multimap:" buf += self.to_s(', ') buf += '>' buf end |
#invert ⇒ Object
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です。
384 385 386 387 388 389 390 |
# File 'lib/multimap.rb', line 384 def invert ret = Multimap.new self.each_pair_with do |key, a_value, count| ret[a_value].add key, count end ret end |
#key(value) ⇒ Object Also known as: index
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を返します。
354 355 356 357 358 359 |
# File 'lib/multimap.rb', line 354 def key(value) self.each_pair_with do |key, a_value, count| return key if value == a_value end nil end |
#keys ⇒ Object
Returns an array in which keys in self are stored.
selfのすべてのキーを、配列として返します。
300 301 302 303 |
# File 'lib/multimap.rb', line 300 def keys cleanup @assoc.keys end |
#merge(other) ⇒ Object Also known as: +
Returns merged multiset of self and other.
selfとotherの要素を合わせた多重集合を返します。
417 418 419 420 |
# File 'lib/multimap.rb', line 417 def merge(other) ret = self.dup ret.merge! other end |
#merge!(other) ⇒ Object
Add elements in other to self. Returns self.
selfにotherの要素を追加します。selfを返します。
407 408 409 410 411 412 |
# File 'lib/multimap.rb', line 407 def merge!(other) other.each_pair_with do |key, a_value, count| self[key].add a_value, count end self end |
#reject(&block) ⇒ Object
Same as Multimap#delete_if except that, rather than deleting key-value pairs in self, this generates a new Multimap with specified key-value pairs are deleted.
Multimap#delete_ifと似ますが、self自身からはキーと値の組を削除せず、要素が削除された結果の多重連想配列を新たに生成して返します。
146 147 148 149 150 |
# File 'lib/multimap.rb', line 146 def reject(&block) # :yields: key, single_value ret = self.dup ret.delete_if &block ret end |
#reject!(&block) ⇒ Object
Same as Multimap#delete_if except that nil is returned if no key-value pair is deleted.
Multimap#delete_ifと似ますが、キーと値の組が1つも削除されなければnilを返します。
174 175 176 177 178 179 180 181 182 183 |
# File 'lib/multimap.rb', line 174 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 |
#reject_with(&block) ⇒ Object
Same as Multimap#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).
Multimap#rejectと似ますが、ブロックへの引数が(キー、キーに割り当てられた値、その値がキーに割り当てられている個数)の3つの組で与えられます。
191 192 193 194 195 |
# File 'lib/multimap.rb', line 191 def reject_with(&block) # :yields: key, a_value, count ret = self.dup ret.delete_with &block ret end |
#replace(other) ⇒ Object
Replaces self by other. Returns self.
selfの内容をotherのものに置き換えます。selfを返します。
120 121 122 123 124 125 126 |
# File 'lib/multimap.rb', line 120 def replace(other) @assoc.clear other.each_pair_with do |key, a_value, count| @assoc[key].add a_value, count end self end |
#size ⇒ Object Also known as: length
Returns number of all elements in self.
selfに含まれている要素数を返します。
395 396 397 398 399 |
# File 'lib/multimap.rb', line 395 def size ret = 0 self.each_pair_with{ |key, a_value, count| ret += count } ret end |
#store(key, value_list) ⇒ Object Also known as: []=
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を返します。
72 73 74 75 76 77 78 79 |
# File 'lib/multimap.rb', line 72 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 |
#to_hash ⇒ Object
Converts self to a Hash whose values in the Hash are all multimaps.
selfをHashに変換して返します。返されるHash中において、値はすべてMultimap型となります。
95 96 97 |
# File 'lib/multimap.rb', line 95 def to_hash @assoc.dup end |
#to_s(delim = "\n") ⇒ Object
:nodoc:
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 |
# File 'lib/multimap.rb', line 423 def to_s(delim = "\n") # :nodoc: cleanup buf = '' init = true @assoc.each_pair do |key, value_list| if init init = false else buf += delim end buf += "#{key.inspect}=>{" init_val = true value_list.each_pair do |a_value, count| if init_val init_val = false else buf += ", " end buf += "\##{count} #{a_value.inspect}" end buf += "}" end buf end |
#values ⇒ Object
Returns a Multiset in which values in self are stored.
selfのすべての値を、Multisetとして返します。
309 310 311 312 313 314 315 316 |
# File 'lib/multimap.rb', line 309 def values cleanup ret = Multiset.new @assoc.each_value do |value_list| ret.merge! value_list end ret end |
#values_at(*key_list) ⇒ Object Also known as: indexes, indices
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を要素とする配列を返します。
369 370 371 |
# File 'lib/multimap.rb', line 369 def values_at(*key_list) key_list.map{ |key| self[key] } end |