From: Thomas Chust Date: 2009-07-28T04:10:53+09:00 Subject: Re: Set class and repetetive elements 2009/7/27 Jos Backus : > On Tue, Jul 28, 2009 at 03:21:38AM +0900, Marcin Grski wrote: >> It would be nice to have for example {5, 1, 1, 1} in Set class object. >> Is it possible? Which method should I use to add element in that way? > > Sounds like you want a Bag, not a Set. I don't know of aany implementation but > it's probably not hard to create one - all you have to do is keep track of the > count of each element. What about using a Hash mapping objects to counts? That way you can still do efficient lookups whether something is contained in the Bag. A simple implementation could look like this: class Bag < Hash def initialize(*contents) super 0 contents.each {|item| self[item] += 1 } end def each return enum_for(__method__) unless block_given? each_pair {|item, count| count.times { yield item } } end def to_a each.to_a end end cu, Thomas -- When C++ is your hammer, every problem looks like your thumb.