Ruby implementation of multiset. Unlike ordinary set(see Ruby documentation for "set" library), multiset can store two or more same items.
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>
Multisets are typically used for counting elements and their numbers of appearances in collections.
# Counting the appearances of characters in a string # (Ruby 1.8.7 or later) Multiset.new("abracadabra".each_char) # => #<Multiset:#5 "a", #2 "b", #2 "r", #1 "c", #1 "d"> # The same m = Multiset.new "abracadabra".each_char do |c| # replace with 'each_byte' in Ruby 1.8.6 or before m << c end p m # => #<Multiset:#5 "a", #2 "b", #2 "r", #1 "c", #1 "d"> # The same but works with Ruby 1.8.6 or before Multiset.new("abracadabra".split(//)) # => #<Multiset:#5 "a", #2 "b", #2 "r", #1 "c", #1 "d">
Multiset#classify
and Multiset#classify_with
returns an instance of Multimap.
Multiset#map
, Multiset#map_with
, and so on are added. Be careful that new Multiset#map
is NOT equal to conventional Multiset#map
(=Enumerable#map
)!!
The library is available via Rubygems. Use the command
gem install multiset
.gem file is available at http://rubygems.org/gems/multiset.
Source codes are available at http://github.com/maraigue/multiset. You can fork or propose fixes on the library via Git.
[Old versions]
*This program is distributed under MIT License (0.3.* or after) / BSD license (0.2.* or before).
Ver. 0.5.* (almost the same for version 0.4.*) Ver. 0.2.* Ver. 0.132
[Reverse reference] (Added on 2008/3/1)
email to main(at-mark)hhiro.net .