Reverse reference - Multiset Library for Ruby

This is reverse reference of Multiset Library for Ruby.


To create a multiset

To create a multiset with fixed items

 irb(main):???:0> Multiset[:a, :a, :b, :c, :b]
 => #<Multiset:#2 :b, #1 :c, #2 :a>

To create a multiset from an array

You can specify objecy not only an array but also all otherEnumerable objects.

 irb(main):???:0> arr = [:a, :a, :b, :c, :b]
 => [:a, :a, :b, :c, :b]
 irb(main):???:0> Multiset.new(arr)
 => #<Multiset:#2 :b, #1 :c, #2 :a>

To create a multiset from items and their numbers

 irb(main):???:0> hash = { :a => 2, :b => 2, :c => 1 }
 => {:b=>2, :c=>1, :a=>2}
 irb(main):???:0> hash.to_multiset
 => #<Multiset:#2 :b, #1 :c, #2 :a>

To create a copy of multiset of another one

 irb(main):???:0> ms = Multiset[:a, :a, :b, :c, :b]
 => #<Multiset:#2 :a, #2 :b, #1 :c>
 irb(main):???:0> ms2 = ms.dup
 => #<Multiset:#2 :a, #2 :b, #1 :c>
 irb(main):???:0> ms == ms2
 => true
 irb(main):???:0> ms.object_id == ms2.object_id
 => false

To make a multiset be another multiset's copy

 irb(main):???:0> ms = Multiset[:a, :a, :b, :c, :b]
 => #<Multiset:#2 :a, #2 :b, #1 :c>
 irb(main):???:0> ms.replace Multiset[:x, :x, :y]
 => #<Multiset:#2 :x, #1 :y>

To edit a multiset

To add items to a multiset

 irb(main):???:0> ms = Multiset[:a, :a, :b, :c, :b]
 => #<Multiset:#2 :b, #1 :c, #2 :a>
 irb(main):???:0> ms.add(:a)
 => #<Multiset:#2 :b, #1 :c, #3 :a>
 irb(main):???:0> ms << :b
 => #<Multiset:#3 :b, #1 :c, #3 :a>

To add two or more same items,set second parameter of add method.

 irb(main):???:0> ms = Multiset[:a, :a, :b, :c, :b]
 => #<Multiset:#2 :b, #1 :c, #2 :a>
 irb(main):???:0> ms.add(:c, 6)
 => #<Multiset:#2 :b, #7 :c, #2 :a>

To delete specific items from a multiset

Same as add method, you can specify second parameterfor delete method. delete_all methoddeletes all items in a multiset.

 irb(main):???:0> ms = Multiset[:a, :a, :b, :c, :b]
 => #<Multiset:#2 :b, #1 :c, #2 :a>
 irb(main):???:0> ms.delete(:a)
 => #<Multiset:#2 :b, #1 :c, #1 :a>
 irb(main):???:0> ms.delete(:b, 2)
 => #<Multiset:#1 :c, #1 :a>
 irb(main):???:0> ms.delete_all(:c)
 => #<Multiset:#1 :a>

To delete items with specified condition

 irb(main):???:0> ms = Multiset[:a, :a, :b, :c, :b]
 => #<Multiset:#2 :b, #1 :c, #2 :a>
 irb(main):???:0> ms.delete_if{ |item| item.to_s < "c" }
 => #<Multiset:#1 :c>
 irb(main):???:0> ms = Multiset[:a, :a, :b, :c, :b]
 => #<Multiset:#2 :b, #1 :c, #2 :a>
 irb(main):???:0> ms.delete_with{ |item, count| count < 2 }
 => #<Multiset:#2 :b, #2 :a>

To change an item's number in a multiset

 irb(main):???:0> ms = Multiset[:a, :a, :b, :c, :b]
 => #<Multiset:#2 :b, #1 :c, #2 :a>
 irb(main):???:0> ms.renew_count(:c, 10)
 => #<Multiset:#2 :b, #10 :c, #2 :a>

To merge two multisets

 irb(main):???:0> Multiset[:a, :a, :b, :c, :b] + Multiset[:a, :b, :x, :y]
 => #<Multiset:#3 :a, #3 :b, #1 :x, #1 :c, #1 :y>

To get information about a multiset

To get number of items in a multiset

 irb(main):???:0> Multiset[:a, :a, :b, :c, :b].size
 => 5
 irb(main):???:0> Multiset[:a, :a, :b, :c, :b].count(:a)
 => 2

To check whether a multiset is empty, use empty? method.

 irb(main):???:0> Multiset[:a, :a, :b, :c, :b].empty?
 => false
 irb(main):???:0> Multiset[].empty?
 => true

To check whether a multiset has specified item

 irb(main):???:0> Multiset[:a, :a, :b, :c, :b].include?(:a)
 => true
 irb(main):???:0> Multiset[:a, :a, :b, :c, :b].include?(:x)
 => false

Get an item randomly from a multiset

 irb(main):???:0> Multiset[:a, :a, :b, :c, :b].rand
 => :b
 irb(main):???:0> Multiset[:a, :a, :b, :c, :b].rand
 => :a

To convert a multiset to an object of another type

To convert a multiset to an array

 irb(main):???:0> Multiset[:a, :a, :b, :c, :b].to_a
 => [:b, :b, :c, :a, :a]

To exclude duplicated items:

 irb(main):???:0> Multiset[:a, :a, :b, :c, :b].items
 => [:b, :c, :a]

To convert a multiset to a hash

Keys are items, and values are its numbers in the multiset.

 irb(main):???:0> Multiset[:a, :a, :b, :c, :b].to_hash
 => {:b=>2, :c=>1, :a=>2}

To convert a multiset to a set (pre-installed "Set" in Ruby)

 irb(main):???:0> Multiset[:a, :a, :b, :c, :b].to_set
 => #<Set: {:a, :b, :c}>

To do something according to items in a multiset

To process for each item in a multiset

 irb(main):???:0> ms = Multiset[:a, :a, :b, :c, :b]
 => #<Multiset:#2 :b, #1 :c, #2 :a>
 irb(main):???:0> ms.each { |i| p i }
 :b
 :b
 :c
 :a
 :a
 => #<Multiset:#2 :b, #1 :c, #2 :a>

To process for each item in a multiset without duplication

 irb(main):???:0> ms = Multiset[:a, :a, :b, :c, :b]
 => #<Multiset:#2 :b, #1 :c, #2 :a>
 irb(main):???:0> ms.each_item { |i| p i }
 :b
 :c
 :a
 => #<Multiset:#2 :b, #1 :c, #2 :a>

To process for each item and its number in a multiset

 irb(main):???:0> ms = Multiset[:a, :a, :b, :c, :b]
 => #<Multiset:#2 :b, #1 :c, #2 :a>
 irb(main):???:0> ms.each_pair { |i, c| p [i, c] }
 [:b, 2]
 [:c, 1]
 [:a, 2]
 => #<Multiset:#2 :b, #1 :c, #2 :a>

To replace all items with specified process

 irb(main):???:0> ms = Multiset[:a, :a, :b, :c, :b]
 => #<Multiset:#2 :a, #2 :b, #1 :c>
 irb(main):???:0> ms.map{ |i| item.to_s }
 => #<Multiset:#2 "a", #2 "b", #1 "c">
 irb(main):???:0> ms = Multiset[:a, :a, :b, :c, :b]
 => #<Multiset:#2 :a, #2 :b, #1 :c>
 irb(main):???:0> ms.map_with{ |i, c| [item.to_s, count * 2] }
 => #<Multiset:#4 "a", #4 "b", #2 "c">

To compare two multisets

 irb(main):???:0> Multiset[:a, :a, :b, :c, :b] == Multiset[:a, :b, :c, :b, :a]
 => true
 irb(main):???:0> Multiset[:a, :a, :b, :c, :b] == Multiset[:a, :b, :c, :b]
 => false
 irb(main):???:0> Multiset[:a, :a, :b, :c].subset?(Multiset[:a, :b, :c, :b, :a])
 => true
 irb(main):???:0> Multiset[:a, :a, :b, :c, :b].superset?(Multiset[:a, :b, :c, :b])
 => true

You can use more methods such as proper_subset? andproper_superset?.

To divide items in multiset into some groups

 irb(main):???:0> Multiset[:a, :a, :b, :c, :b].classify{ |i| i.to_s < "c" }
 => {false=>#<Multiset:#1 :c>, true=>#<Multiset:#2 :a, #2 :b>}
 irb(main):???:0> Multiset[:a, :a, :b, :c, :b].classify_with{ |i, c| c }
 => {1=>#<Multiset:#1 :c>, 2=>#<Multiset:#2 :a, #2 :b>}