Skip to content

Commit

Permalink
fix multiset super and subset
Browse files Browse the repository at this point in the history
  • Loading branch information
josh committed May 26, 2009
1 parent ec1941a commit 9f007ca
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 28 deletions.
57 changes: 29 additions & 28 deletions lib/multiset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ class Multiset < Set
attr_reader :hash
protected :hash

def initialize(*args, &block) #:nodoc:
@hash = Hash.new(0)
super
end

# Returns the number of times an element belongs to the multiset.
def multiplicity(e)
@hash[e]
Expand All @@ -24,37 +29,33 @@ def to_a
inject([]) { |ary, (key, _)| ary << key }
end

#--
# def superset?(set)
# set.is_a?(Set) or raise ArgumentError, "value must be a set"
# return false if size < set.size
# set.all? { |o| include?(o) }
# end
#++
# Returns true if the set is a superset of the given set.
def superset?(set)
set.is_a?(self.class) or raise ArgumentError, "value must be a set"
return false if cardinality < set.cardinality
set.all? { |o| set.multiplicity(o) <= multiplicity(o) }
end

#--
# def proper_superset?(set)
# set.is_a?(Set) or raise ArgumentError, "value must be a set"
# return false if size <= set.size
# set.all? { |o| include?(o) }
# end
#++
# Returns true if the set is a proper superset of the given set.
def proper_superset?(set)
set.is_a?(self.class) or raise ArgumentError, "value must be a set"
return false if cardinality <= set.cardinality
set.all? { |o| set.multiplicity(o) <= multiplicity(o) }
end

#--
# def subset?(set)
# set.is_a?(Set) or raise ArgumentError, "value must be a set"
# return false if set.size < size
# all? { |o| set.include?(o) }
# end
#++
# Returns true if the set is a subset of the given set.
def subset?(set)
set.is_a?(self.class) or raise ArgumentError, "value must be a set"
return false if set.cardinality < cardinality
all? { |o| multiplicity(o) <= set.multiplicity(o) }
end

#--
# def proper_subset?(set)
# set.is_a?(Set) or raise ArgumentError, "value must be a set"
# return false if set.size <= size
# all? { |o| set.include?(o) }
# end
#++
# Returns true if the set is a proper subset of the given set.
def proper_subset?(set)
set.is_a?(self.class) or raise ArgumentError, "value must be a set"
return false if set.cardinality <= cardinality
all? { |o| multiplicity(o) <= set.multiplicity(o) }
end

# Calls the given block once for each element in the set, passing
# the element as parameter. Returns an enumerator if no block is
Expand Down
47 changes: 47 additions & 0 deletions spec/multiset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,53 @@
set.should equal(ret)
set.should == Multiset[:a, :a, :b, :b, :b, :c]
end

it "should return true if the set is a superset of the given set" do
set = Multiset[1, 2, 2, 3]

set.superset?(Multiset[]).should be_true
set.superset?(Multiset[1, 2]).should be_true
set.superset?(Multiset[1, 2, 3]).should be_true
set.superset?(Multiset[1, 2, 2, 3]).should be_true
set.superset?(Multiset[1, 2, 2, 2]).should be_false
set.superset?(Multiset[1, 2, 3, 4]).should be_false
set.superset?(Multiset[1, 4]).should be_false
end

it "should return true if the set is a proper superset of the given set" do
set = Multiset[1, 2, 2, 3, 3]

set.proper_superset?(Multiset[]).should be_true
set.proper_superset?(Multiset[1, 2]).should be_true
set.proper_superset?(Multiset[1, 2, 3]).should be_true
set.proper_superset?(Multiset[1, 2, 2, 3, 3]).should be_false
set.proper_superset?(Multiset[1, 2, 2, 2]).should be_false
set.proper_superset?(Multiset[1, 2, 3, 4]).should be_false
set.proper_superset?(Multiset[1, 4]).should be_false
end

it "should return true if the set is a subset of the given set" do
set = Multiset[1, 2, 2, 3]

set.subset?(Multiset[1, 2, 2, 3, 4]).should be_true
set.subset?(Multiset[1, 2, 2, 3, 3]).should be_true
set.subset?(Multiset[1, 2, 2, 3]).should be_true
set.subset?(Multiset[1, 2, 3]).should be_false
set.subset?(Multiset[1, 2, 2]).should be_false
set.subset?(Multiset[1, 2, 3]).should be_false
set.subset?(Multiset[]).should be_false
end

it "should return true if the set is a proper subset of the given set" do
set = Multiset[1, 2, 2, 3, 3]

set.proper_subset?(Multiset[1, 2, 2, 3, 3, 4]).should be_true
set.proper_subset?(Multiset[1, 2, 2, 3, 3]).should be_false
set.proper_subset?(Multiset[1, 2, 3]).should be_false
set.proper_subset?(Multiset[1, 2, 2]).should be_false
set.proper_subset?(Multiset[1, 2, 3]).should be_false
set.proper_subset?(Multiset[]).should be_false
end
end

describe Multiset, "with inital values" do
Expand Down

0 comments on commit 9f007ca

Please sign in to comment.