Class: Multiset
Overview
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.
-
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>
Rubyによる多重集合(マルチセット)の実装です。通常の集合(Rubyでは“set”ライブラリ)と異なり、多重集合は同一の要素を複数格納することができます。
メソッド名は基本的にSetクラスに合わせてあります。
Constant Summary collapse
- VERSION =
"0.5.3"
Class Method Summary collapse
-
.[](*list) ⇒ Object
Generates a multiset from items in
list. -
.from_lines(str) ⇒ Object
Generates a Multiset from string, separated by lines.
-
.parse(object) ⇒ Object
Generates a multiset by converting
object. -
.parse_force(object) ⇒ Object
If a string is given, it works as Multiset.from_lines, otherwise as Multiset.parse.
Instance Method Summary collapse
-
#&(other) ⇒ Object
Returns the intersection of
selfandother, that is, for each item both inselfandother, the multiset includes it in the smaller number of the two. -
#==(other) ⇒ Object
Returns whether
selfis equal toother. -
#add(item, addcount = 1) ⇒ Object
(also: #<<)
Adds
addcountnumber ofitems toself. -
#clear ⇒ Object
Deletes all items in
self. -
#compare_set_with(other) ⇒ Object
Iterates for each item in
selfandother, without duplication. -
#count(*item_list) ⇒ Object
Returns number of
items inself. -
#delete(item, delcount = 1) ⇒ Object
Deletes
delcountnumber ofitems fromself. -
#delete_all(item) ⇒ Object
Deletes all
items inself. -
#delete_if ⇒ Object
Gives all items in
self(without duplication) to given block, and deletes the items such that the block returns true. -
#delete_with ⇒ Object
Gives each pair of (non-duplicated) item and its number to given block, and deletes those items such that the block returns true.
-
#dup ⇒ Object
Returns duplicated
self. -
#each ⇒ Object
Iterates for each item in
self. -
#each_item(&block) ⇒ Object
Iterates for each item in
self, without duplication. -
#each_with_count(&block) ⇒ Object
(also: #each_pair)
Iterates for each pair of (non-duplicated) item and its number in
self. -
#empty? ⇒ Boolean
Returns whether
selfhas no item. -
#eql?(other) ⇒ Boolean
:nodoc:.
-
#find(ifnone = nil, &block) ⇒ Object
(also: #detect)
Gives all items in
self(without duplication) to given block, and returns the first item that makes true the result of the block. -
#find_all(&block) ⇒ Object
(also: #select)
Gives all items in
self(without duplication) to given block, and returns the Multiset by items that makes true the result of the block. -
#find_all_with(&block) ⇒ Object
(also: #select_with)
The same as Multiset#find_all except that the pairs of (non-duplicated) items and their counts are given to the block.
-
#find_with(ifnone = nil, &block) ⇒ Object
(also: #detect_with)
The same as Multiset#find except that the pairs of (non-duplicated) items and their counts are given to the block.
-
#flatten ⇒ Object
Generates a multiset such that multisets in
selfare flattened. -
#flatten! ⇒ Object
Flattens multisets in
self. -
#grep(pattern) ⇒ Object
Collects items in
selfsatisfyingpattern(pattern=== item). -
#group_by ⇒ Object
(also: #classify)
Classify items in
selfby returned value from block. -
#group_by_with ⇒ Object
(also: #classify_with)
Same as Multiset#group_by except that the pairs of (non-duplicated) items and their counts are given to block.
-
#hash ⇒ Object
:nodoc:.
-
#include?(item) ⇒ Boolean
(also: #member?)
Returns whether
selfhasitem. -
#initialize(list = nil) ⇒ Multiset
constructor
Generates a multiset from items in
list. -
#inject_with(init) ⇒ Object
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 inself. -
#inspect ⇒ Object
:nodoc:.
-
#items ⇒ Object
Returns an array with all items in
self, without duplication. -
#listing(delim = "\n") ⇒ Object
Lists all items with duplication in
self. -
#map ⇒ Object
(also: #collect)
Gives all items in
self(without duplication) to given block, and generates a new multiset whose values are returned value from the block. -
#map!(&block) ⇒ Object
(also: #collect!)
Same as Multiset#map, except that
selfis replaced by the resulted multiset. -
#map_with ⇒ Object
(also: #collect_with)
Gives all pairs of (non-duplicated) items and their numbers in
selfto given block. -
#map_with! ⇒ Object
(also: #collect_with!)
Same as Multiset#map_with, except that
selfby the resulted multiset. -
#max(&block) ⇒ Object
Returns the largest item in
self, ornilif no item is stored inself. -
#max_by(&block) ⇒ Object
Returns the largest item by comparing the items in
selfby the results of the block. -
#max_by_with(&block) ⇒ Object
Same as Multiset#max_by except that pairs of (non-duplicated) items and their counts are given to the block.
-
#max_with ⇒ Object
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.
-
#merge(other) ⇒ Object
(also: #+)
Returns a multiset merging
selfandother. -
#merge!(other) ⇒ Object
Merges
othertoself. -
#min(&block) ⇒ Object
Returns the smallest item in
self, ornilif no item is stored inself. -
#min_by(&block) ⇒ Object
Returns the largest item by comparing the items in
selfby the results of the block. -
#min_by_with(&block) ⇒ Object
Same as Multiset#min_by except that pairs of (non-duplicated) items and their counts are given to the block.
-
#min_with ⇒ Object
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.
-
#minmax(&block) ⇒ Object
Returns the pair consisting of the smallest and the largest item in
self, ornilif no item is stored inself. -
#minmax_by(&block) ⇒ Object
Returns the pair consisting of the smallest and the largest items in
selfby comparing the items by the results of the block. -
#minmax_by_with(&block) ⇒ Object
Same as Multiset#minmax_by except that pairs of (non-duplicated) items and their counts are given to the block.
-
#minmax_with ⇒ Object
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.
-
#proper_subset?(other) ⇒ Boolean
Returns whether
selfis a proper subset ofother, that is, it returns true if subset? is satisfied andselfis not equal toother. -
#proper_superset?(other) ⇒ Boolean
Returns whether
selfis a proper superset ofother, that is, it returns true if superset? is satisfied andselfis not equal toother. -
#reject ⇒ Object
Gives all items in
self(without duplication) to given block, and returns a multiset collecting the items such that the block returns false. -
#reject! ⇒ Object
Same as Multiset#delete_if except that this returns
nilif no item is deleted. -
#reject_with ⇒ Object
Gives all pairs of (non-duplicated) items and counts in
selfto given block, and returns a multiset collecting the items such that the block returns false. -
#renew_count(item, number) ⇒ Object
Sets the number of
iteminselfasnumber. -
#replace(other) ⇒ Object
Replaces
selfbyother. -
#sample ⇒ Object
(also: #rand)
Returns one item in
selfat random in the same probability. -
#size ⇒ Object
(also: #length)
Returns number of all items in
self. -
#sort(&block) ⇒ Object
Generates an array by sorting the items in
self. -
#sort_by(&block) ⇒ Object
The same as Multiset#sort except that, after giving the items to the block, the items are sorted by the values from the block.
-
#sort_by_with ⇒ Object
Same as Multiset#sort_by except that the pairs of (non-duplicated) items and their counts are given to the block.
-
#sort_with ⇒ Object
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.
-
#subset?(other) ⇒ Boolean
Returns whether
selfis a subset ofother, that is, for any item, the number of it inselfis equal to or smaller than that inother. -
#subtract(other) ⇒ Object
(also: #-)
Returns a multiset such that items in
otherare removed fromself, where ‘removed’ means that, for each item inother, the items of the number inotherare removed fromself. -
#subtract!(other) ⇒ Object
Removes items in
otherfromself. -
#superset?(other) ⇒ Boolean
Returns whether
selfis a superset ofother, that is, for any item, the number of it inselfis equal to or larger than that inother. -
#to_a ⇒ Object
Converts
selfto an array. -
#to_hash ⇒ Object
Converts
selfto aHash. -
#to_s(delim = "\n") ⇒ Object
Lists all items without duplication and its number in
self. -
#to_set ⇒ Object
Converts
selfto ordinary set (TheSetclass attached to Ruby by default). -
#|(other) ⇒ Object
Returns the union of
selfandother, that is, for each item either or both inselfandother, the multiset includes it in the larger number of the two.
Constructor Details
#initialize(list = nil) ⇒ Multiset
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が発生します。
48 49 50 51 52 53 54 55 |
# 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 |
Class Method Details
.[](*list) ⇒ Object
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つが多重集合の要素になります。
主に、リテラルから多重集合を生成するのに用います。
66 67 68 |
# File 'lib/multiset.rb', line 66 def Multiset.[](*list) Multiset.new(list) end |
.from_lines(str) ⇒ Object
Generates a Multiset from string, separated by lines.
文字列を行単位で区切ってMultisetにします。
118 119 120 |
# File 'lib/multiset.rb', line 118 def Multiset.from_lines(str) Multiset.new(str.enum_for(:each_line)) end |
.parse(object) ⇒ Object
Generates a multiset by converting object.
-
If
objectis an instance of Multiset, returns duplicatedobject. -
If
objectis not an instance of Multiset and has the methodeach_pair, for each pair of two arguments fromeach_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
objectdoes not have the methodeach_pairandobjectincludesEnumerable, this method works the same as Multiset#new . -
Otherwise,
ArgumentErroris raised.
objectを多重集合に変換し生成します。
-
objectがMultisetのインスタンスである場合、その複製を返します。 -
objectがMultisetのインスタンスでなく、かつeach_pairメソッドを持っている場合、each_pairから渡される2つの引数について、前者を要素、後者をその個数とした多重集合を生成します。Hash#to_multisetもご覧下さい。 -
objectがeach_pairメソッドを持っておらず、かつEnumerableである場合は、Multiset#newと同じ結果です。 -
それ以外の場合は、例外
ArgumentErrorが発生します。
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# 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) ⇒ Object
If a string is given, it works as Multiset.from_lines, otherwise as Multiset.parse.
文字列が渡された場合は、Multiset.from_linesと同じ挙動。それ以外の場合は、Multiset.parseと同じ挙動。
127 128 129 130 131 132 133 |
# 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 |
Instance Method Details
#&(other) ⇒ Object
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.
selfとotherの積集合からなる多重集合を返します。すなわち、selfとotherの両方に存在する要素について、少ないほうの個数を持った多重集合を返します。
573 574 575 576 577 578 579 |
# 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) ⇒ Object
Returns whether self is equal to other.
selfがotherと等しいかどうかを返します。
498 499 500 501 |
# 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) ⇒ Object Also known as: <<
Adds addcount number of items to self. Returns self if succeeded, or nil if failed.
selfに、addcount個のitemを追加します。成功した場合はselfを、失敗した場合はnilを返します。
384 385 386 387 388 389 |
# 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 |
#clear ⇒ Object
Deletes all items in self. Returns self.
selfの要素をすべて削除します。selfを返します。
252 253 254 255 |
# File 'lib/multiset.rb', line 252 def clear @entries.clear self end |
#compare_set_with(other) ⇒ Object
Iterates for each item in self and other, without duplication. If the given block returns false, then iteration immediately ends and returns false. Returns true if the given block returns true for all of iteration.
This method is defined for methods superset?, subset?, ==.
selfとotherが持つすべての要素(重複なし)について繰り返し、ブロックの返り値が偽であるものが存在すればその時点でfalseを返します。すべての要素について真であればtrueを返します。
このメソッドはsuperset?、subset?、== のために定義されています。
434 435 436 437 438 439 |
# File 'lib/multiset.rb', line 434 def compare_set_with(other) # :nodoc: :yields: number_in_self, number_in_other (self.items | other.items).each do |item| return false unless yield(self.count(item), other.count(item)) end true end |
#count(*item_list) ⇒ Object
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内にいくつ入っているかを数えます。
:call-seq:
count(item)
count{ |item| ... }
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# 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) ⇒ Object
Deletes delcount number of items from self. Returns self if succeeded, nil otherwise.
selfから、delcount個のitemを削除します。成功した場合はselfを、失敗した場合はnilを返します。
398 399 400 401 402 403 |
# 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) ⇒ Object
Deletes all items in self. Returns self.
selfに含まれるitemをすべて削除します。selfを返します。
410 411 412 413 |
# File 'lib/multiset.rb', line 410 def delete_all(item) @entries.delete(item) self end |
#delete_if ⇒ Object
Gives all items in self (without duplication) to given block, and deletes the items such that the block returns true. Returns self.
ブロックにselfの要素(重複なし)を順次与え、結果が真であった要素をすべて削除します。selfを返します。
822 823 824 825 826 827 |
# 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 ⇒ Object
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を返します。
836 837 838 839 840 841 |
# 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 |
#dup ⇒ Object
Returns duplicated self.
selfの複製を生成して返します。
138 139 140 |
# File 'lib/multiset.rb', line 138 def dup @entries.to_multiset end |
#each ⇒ Object
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回で済みます。
622 623 624 625 626 627 628 629 630 631 |
# 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(&block) ⇒ Object
Iterates for each item in self, without duplication. Returns self. An Enumerator will be returned if no block is given.
selfに含まれるすべての要素について、重複を許さずに繰り返します。selfを返します。ブロックが与えられていない場合、Enumeratorを返します。
640 641 642 643 644 645 646 647 |
# 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_with_count(&block) ⇒ Object Also known as: each_pair
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を返します。
656 657 658 659 660 661 662 663 |
# 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 |
#empty? ⇒ Boolean
Returns whether self has no item.
selfに要素がないかどうかを返します。
236 237 238 |
# File 'lib/multiset.rb', line 236 def empty? @entries.empty? end |
#eql?(other) ⇒ Boolean
:nodoc:
197 198 199 200 201 202 203 |
# File 'lib/multiset.rb', line 197 def eql?(other) # :nodoc: if self.hash == other.hash self == other else false end end |
#find(ifnone = nil, &block) ⇒ Object Also known as: detect
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を返します。
882 883 884 885 886 887 888 |
# 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 |
#find_all(&block) ⇒ Object Also known as: select
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を返します。
927 928 929 930 931 932 933 |
# 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 |
#find_all_with(&block) ⇒ Object Also known as: select_with
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の要素とその個数の組が与えられます。
949 950 951 952 953 954 955 |
# 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 |
#find_with(ifnone = nil, &block) ⇒ Object Also known as: detect_with
The same as Multiset#find except that the pairs of (non-duplicated) items and their counts are given to the block.
Multiset#findと似ますが、ブロックにはselfの要素とその個数の組が与えられます。
903 904 905 906 907 908 909 |
# 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 |
#flatten ⇒ Object
Generates a multiset such that multisets in self are flattened.
self中に含まれる多重集合を平滑化したものを返します。
744 745 746 747 748 749 750 751 752 753 754 |
# 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! ⇒ Object
Flattens multisets in self. Returns self if any item is flattened, nil otherwise.
self中に含まれる多重集合を平滑化します。平滑化した多重集合が1つでもあればselfを、そうでなければnilを返します。
763 764 765 766 767 768 769 770 771 772 773 |
# 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) ⇒ Object
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)要素のみを集めた多重集合を返します。ブロックが与えられている場合は、さらにその結果を適用した結果を返します。
972 973 974 975 976 977 978 979 980 |
# 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 ⇒ Object Also known as: classify
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を返します。
849 850 851 852 853 854 855 |
# 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 |
#group_by_with ⇒ Object Also known as: classify_with
Same as Multiset#group_by except that the pairs of (non-duplicated) items and their counts are given to block.
Multiset#group_byと同様ですが、ブロックには要素とその個数の組が与えられます。
862 863 864 865 866 867 868 |
# 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 |
#hash ⇒ Object
:nodoc:
189 190 191 192 193 194 195 |
# File 'lib/multiset.rb', line 189 def hash # :nodoc: val = 0 @entries.each_pair do |item, count| val += item.hash * count end val end |
#include?(item) ⇒ Boolean Also known as: member?
Returns whether self has item.
itemがself中に含まれているかを返します。
260 261 262 |
# File 'lib/multiset.rb', line 260 def include?(item) @entries.has_key?(item) end |
#inject_with(init) ⇒ Object
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を与えることもできません。
996 997 998 999 1000 1001 |
# File 'lib/multiset.rb', line 996 def inject_with(init) @entries.each_pair do |item, count| init = yield(init, item, count) end init end |
#inspect ⇒ Object
:nodoc:
310 311 312 313 314 315 |
# File 'lib/multiset.rb', line 310 def inspect # :nodoc: buf = "#<Multiset:" buf += self.to_s(', ') buf += '>' buf end |
#items ⇒ Object
Returns an array with all items in self, without duplication.
selfに含まれている要素(重複は除く)からなる配列を返します。
243 244 245 |
# File 'lib/multiset.rb', line 243 def items @entries.keys end |
#listing(delim = "\n") ⇒ Object
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)を用います。
273 274 275 276 277 278 279 280 281 282 283 284 285 |
# 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 ⇒ Object Also known as: collect
Gives all items in self (without duplication) to given block, and generates a new multiset whose values are returned value from the block.
selfの各要素(重複なし)をブロックに与え、返り値を集めたものからなる多重集合を生成します。
671 672 673 674 675 676 677 |
# 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 |
#map!(&block) ⇒ Object Also known as: collect!
Same as Multiset#map, except that self is replaced by the resulted multiset. Returns self.
Multiset#mapと同様の処理を行いますが、結果として生成される多重集合でselfが置き換えられます。selfを返します。
685 686 687 688 |
# File 'lib/multiset.rb', line 685 def map!(&block) # :yields: item self.replace(self.map(&block)) self end |
#map_with ⇒ Object Also known as: collect_with
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要素の配列を受け取り、前者を要素、後者をその個数とした多重集合を生成します。
699 700 701 702 703 704 705 706 |
# 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 |
#map_with! ⇒ Object Also known as: collect_with!
Same as Multiset#map_with, except that self by the resulted multiset. Returns self.
Multiset#map_withと同様ですが、結果として生成される多重集合でselfが置き換えられます。selfを返します。
714 715 716 717 718 719 720 721 |
# 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 |
#max(&block) ⇒ Object
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つの要素を与えることで行います。
1010 1011 1012 |
# File 'lib/multiset.rb', line 1010 def max(&block) # :yields: a, b @entries.keys.max(&block) end |
#max_by(&block) ⇒ Object
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を返します。
1041 1042 1043 |
# File 'lib/multiset.rb', line 1041 def max_by(&block) # :yields: item @entries.keys.max_by(&block) end |
#max_by_with(&block) ⇒ Object
Same as Multiset#max_by except that pairs of (non-duplicated) items and their counts are given to the block.
Multiset#max_by と同様ですが、ブロックには要素(重複なし)とその出現数の組が与えられます。
1099 1100 1101 1102 |
# 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 ⇒ Object
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引数が与えられます。
1070 1071 1072 1073 |
# 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 |
#merge(other) ⇒ Object Also known as: +
Returns a multiset merging self and other.
selfとotherの要素を合わせた多重集合を返します。
513 514 515 516 517 518 519 |
# 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 |
#merge!(other) ⇒ Object
Merges other to self. See also Multiset#merge . Returns self.
selfにotherの要素を追加します。Multiset#merge も参照してください。selfを返します。
529 530 531 532 533 534 |
# File 'lib/multiset.rb', line 529 def merge!(other) other.each_pair do |item, count| self.add(item, count) end self end |
#min(&block) ⇒ Object
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つの要素を与えることで行います。
1021 1022 1023 |
# File 'lib/multiset.rb', line 1021 def min(&block) # :yields: a, b @entries.keys.min(&block) end |
#min_by(&block) ⇒ Object
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を返します。
1051 1052 1053 |
# File 'lib/multiset.rb', line 1051 def min_by(&block) # :yields: item @entries.keys.min_by(&block) end |
#min_by_with(&block) ⇒ Object
Same as Multiset#min_by except that pairs of (non-duplicated) items and their counts are given to the block.
Multiset#max_by と同様ですが、ブロックには要素(重複なし)とその出現数の組が与えられます。
1108 1109 1110 1111 |
# 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 ⇒ Object
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引数が与えられます。
1080 1081 1082 1083 |
# 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(&block) ⇒ Object
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つの要素を与えることで行います。
1031 1032 1033 |
# File 'lib/multiset.rb', line 1031 def minmax(&block) # :yields: a, b @entries.keys.minmax(&block) end |
#minmax_by(&block) ⇒ Object
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を返します。
1061 1062 1063 |
# File 'lib/multiset.rb', line 1061 def minmax_by(&block) # :yields: item @entries.keys.minmax_by(&block) end |
#minmax_by_with(&block) ⇒ Object
Same as Multiset#minmax_by except that pairs of (non-duplicated) items and their counts are given to the block.
Multiset#minmax_by と同様ですが、ブロックには要素(重複なし)とその出現数の組が与えられます。
1117 1118 1119 1120 |
# 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 ⇒ Object
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引数が与えられます。
1090 1091 1092 1093 |
# 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) ⇒ Boolean
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.
selfがotherに真に含まれているかどうかを返します。すなわち、 subset? の条件に加えて両者が一致しなければ真となります。
488 489 490 491 492 493 |
# 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) ⇒ Boolean
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.
selfがotherを真に含んでいるかどうかを返します。すなわち、 superset? の条件に加えて両者が一致しなければ真となります。
461 462 463 464 465 466 |
# 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 |
#reject ⇒ Object
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を返します。
780 781 782 783 784 785 786 |
# 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! ⇒ Object
Same as Multiset#delete_if except that this returns nil if no item is deleted.
Multiset#delete_ifと似ますが、要素が1つも削除されなければnilを返します。
804 805 806 807 808 809 810 811 812 813 |
# 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 ⇒ Object
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を返します。
793 794 795 796 797 798 799 |
# 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) ⇒ Object
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を返します。
368 369 370 371 372 373 374 375 376 377 |
# 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) ⇒ Object
Replaces self by other. Returns self.
selfの内容をotherのものに置き換えます。selfを返します。
217 218 219 220 221 222 223 |
# 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 ⇒ Object Also known as: rand
Returns one item in self at random in the same probability. Returns nil in case the multiset is empty.
selfの要素を無作為に1つ選んで返します。すべての要素は等確率で選ばれます。空のMultisetに対して呼び出した場合はnilを返します。
731 732 733 734 735 736 737 738 |
# 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 |
#size ⇒ Object Also known as: length
Returns number of all items in self.
selfに含まれている要素数を返します。
228 229 230 |
# File 'lib/multiset.rb', line 228 def size @entries.inject(0){ |sum, item| sum += item[1] } end |
#sort(&block) ⇒ Object
Generates an array by sorting the items in self.
selfの要素を並び替えた配列を生成します。
1125 1126 1127 1128 1129 1130 1131 |
# 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(&block) ⇒ Object
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つの要素が与えられ、その値が小さいものから順に並びます。
1137 1138 1139 1140 1141 1142 1143 |
# 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 ⇒ Object
Same as Multiset#sort_by except that the pairs of (non-duplicated) items and their counts are given to the block.
Multiset#sort_by と同様ですが、ブロックには要素(重複なし)とその出現数の組が与えられます。
1162 1163 1164 1165 1166 1167 1168 |
# 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 ⇒ Object
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引数が与えられます。
1150 1151 1152 1153 1154 1155 1156 |
# 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) ⇒ Boolean
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.
selfがotherを含んでいるかどうかを返します。すなわち、いかなる要素についても、それがselfに含まれている個数がotherに含まれている数以下であるかを返します。
475 476 477 478 479 480 |
# 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) ⇒ Object Also known as: -
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から取り除くことをいいます。
543 544 545 546 547 548 549 |
# 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 |
#subtract!(other) ⇒ Object
Removes items in other from self. See also Multiset#subtract . Returns self.
selfからotherの要素を削除します。Multiset#subtract も参照してください。selfを返します。
559 560 561 562 563 564 |
# File 'lib/multiset.rb', line 559 def subtract!(other) other.each_pair do |item, count| self.delete(item, count) end self end |
#superset?(other) ⇒ Boolean
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.
selfがotherを含んでいるかどうかを返します。すなわち、いかなる要素についても、それがselfに含まれている個数がotherに含まれている数以上であるかを返します。
448 449 450 451 452 453 |
# 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 ⇒ Object
Converts self to an array.
selfを配列に変換して返します。
181 182 183 184 185 186 187 |
# 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 ⇒ Object
Converts self to a Hash. See Hash#to_multiset about format of generated hash.
selfをHashに変換して返します。生成されるハッシュの構造については、Hash#to_multisetをご覧下さい。
147 148 149 |
# File 'lib/multiset.rb', line 147 def to_hash @entries.dup end |
#to_s(delim = "\n") ⇒ Object
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)を用います。
295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# 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 ⇒ Object
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)で可能です。
173 174 175 176 |
# File 'lib/multiset.rb', line 173 def to_set require "set" Set.new(@entries.keys) end |
#|(other) ⇒ Object
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.
selfとotherの和集合からなる多重集合を返します。すなわち、selfとotherの少なくとも一方に存在する要素について、多いほうの個数を持った多重集合を返します。
588 589 590 591 592 593 594 |
# 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 |