Rubyによる多重集合(マルチセット)の実装です。 通常の集合(Rubyでは"set"ライブラリ)と異なり、多重集合は同一の要素を複数格納することができます。
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>
多重集合(マルチセット)の典型的な使い方として、コレクションの中に含まれる要素とその数を数える、というものがあります。
# 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およびMultiset#classify_withの返り値がMultimapとなりました。
Multiset#map・Multiset#map_withなどいくつかのメソッドを追加しました。以前のMultiset#map(=Enumerable#map)とは挙動が変更になっていますのでご注意下さい。
Rubygemsによるインストールに対応しています。以下のコマンドでインストールできます。
gem install multiset
http://rubygems.org/gems/multisetから、.gemファイルを入手することもできます。
ソースコードはhttps://github.com/maraigue/multisetからご覧頂けます。Git経由でプロジェクトをフォークしたり、修正の提案をしていただくことも可能です。
[Old versions]
*このプログラムはMITライセンス(0.3以降)およびBSDライセンス(0.2まで)にて公開しています。
Ver. 0.5.* (almost the same for version 0.4.*) Ver. 0.2.* Ver. 0.132
[逆引きリファレンス] (Added on 2008/3/1)
メールアドレス main(あっとまーく)hhiro.net