Set
Multiset implements a collection of unordered values and allows duplicates.
require 'multiset'
s1 = Multiset.new [1, 2] # -> #<Multiset: {1, 2}>
s1.add(2) # -> #<Multiset: {1, 2, 2}>
s1.merge([2, 6]) # -> #<Multiset: {1, 2, 2, 2, 3}>
s1.multiplicity(2) # -> 3
s1.multiplicity(3) # -> 1
Returns a new set containing elements common to the set and the given enumerable object.
# File lib/rack/mount/vendor/multimap/multiset.rb, line 129
129: def &(enum)
130: s = dup
131: n = self.class.new
132: enum.each { |o|
133: if s.include?(o)
134: s.delete(o, 1)
135: n.add(o)
136: end
137: }
138: n
139: end
Returns a new set containing elements exclusive between the set and the given enumerable object. (set ^ enum) is equivalent to ((set | enum) - (set & enum)).
# File lib/rack/mount/vendor/multimap/multiset.rb, line 145
145: def ^(enum)
146: n = self.class.new(enum)
147: each { |o| n.include?(o) ? n.delete(o, 1) : n.add(o) }
148: n
149: end
Adds the given object to the set and returns self. Use merge to add many elements at once.
# File lib/rack/mount/vendor/multimap/multiset.rb, line 80
80: def add(o)
81: @hash[o] ||= 0
82: @hash[o] += 1
83: self
84: end
Returns the total number of elements in a multiset, including repeated memberships
# File lib/rack/mount/vendor/multimap/multiset.rb, line 27
27: def cardinality
28: @hash.inject(0) { |s, (e, m)| s += m }
29: end
Deletes all the identical object from the set and returns self. If n is given, it will remove that amount of identical objects from the set. Use subtract to delete many different items at once.
# File lib/rack/mount/vendor/multimap/multiset.rb, line 93
93: def delete(o, n = nil)
94: if n
95: @hash[o] ||= 0
96: @hash[o] -= n if @hash[o] > 0
97: @hash.delete(o) if @hash[o] == 0
98: else
99: @hash.delete(o)
100: end
101: self
102: end
Deletes every element of the set for which block evaluates to true, and returns self.
# File lib/rack/mount/vendor/multimap/multiset.rb, line 108
108: def delete_if
109: each { |o| delete(o) if yield(o) }
110: self
111: end
Calls the given block once for each element in the set, passing the element as parameter. Returns an enumerator if no block is given.
# File lib/rack/mount/vendor/multimap/multiset.rb, line 69
69: def each
70: @hash.each_pair do |key, multiplicity|
71: multiplicity.times do
72: yield(key)
73: end
74: end
75: self
76: end
Returns true if two sets are equal. Two multisets are equal if they have the same cardinalities and each element has the same multiplicity in both sets. The equality of each element inside the multiset is defined according to Object#eql?.
# File lib/rack/mount/vendor/multimap/multiset.rb, line 155
155: def eql?(set)
156: return true if equal?(set)
157: set = self.class.new(set) unless set.is_a?(self.class)
158: return false unless cardinality == set.cardinality
159: superset?(set) && subset?(set)
160: end
Merges the elements of the given enumerable object to the set and returns self.
# File lib/rack/mount/vendor/multimap/multiset.rb, line 115
115: def merge(enum)
116: enum.each { |o| add(o) }
117: self
118: end
Returns the number of times an element belongs to the multiset.
# File lib/rack/mount/vendor/multimap/multiset.rb, line 21
21: def multiplicity(e)
22: @hash[e]
23: end
Returns true if the set is a proper subset of the given set.
# File lib/rack/mount/vendor/multimap/multiset.rb, line 60
60: def proper_subset?(set)
61: set.is_a?(self.class) or raise ArgumentError, "value must be a set"
62: return false if set.cardinality <= cardinality
63: all? { |o| multiplicity(o) <= set.multiplicity(o) }
64: end
Returns true if the set is a proper superset of the given set.
# File lib/rack/mount/vendor/multimap/multiset.rb, line 46
46: def proper_superset?(set)
47: set.is_a?(self.class) or raise ArgumentError, "value must be a set"
48: return false if cardinality <= set.cardinality
49: set.all? { |o| set.multiplicity(o) <= multiplicity(o) }
50: end
Returns true if the set is a subset of the given set.
# File lib/rack/mount/vendor/multimap/multiset.rb, line 53
53: def subset?(set)
54: set.is_a?(self.class) or raise ArgumentError, "value must be a set"
55: return false if set.cardinality < cardinality
56: all? { |o| multiplicity(o) <= set.multiplicity(o) }
57: end
Deletes every element that appears in the given enumerable object and returns self.
# File lib/rack/mount/vendor/multimap/multiset.rb, line 122
122: def subtract(enum)
123: enum.each { |o| delete(o, 1) }
124: self
125: end
Returns true if the set is a superset of the given set.
# File lib/rack/mount/vendor/multimap/multiset.rb, line 39
39: def superset?(set)
40: set.is_a?(self.class) or raise ArgumentError, "value must be a set"
41: return false if cardinality < set.cardinality
42: set.all? { |o| set.multiplicity(o) <= multiplicity(o) }
43: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.