Class: Multiset
- Inherits:
-
Set
- Object
- Set
- Multiset
- Defined in:
- lib/rack/mount/vendor/multimap/multiset.rb
Overview
Multiset implements a collection of unordered values and allows duplicate values.
Instance Method Summary collapse
-
#&(enum) ⇒ Object
(also: #intersection)
Returns a new set containing elements common to the set and the given enumerable object.
-
#^(enum) ⇒ Object
Returns a new set containing elements exclusive between the set and the given enumerable object.
-
#add(o) ⇒ Object
(also: #<<)
Adds the given object to the set and returns self.
-
#cardinality ⇒ Object
(also: #size, #length)
Returns the total number of elements in a multiset, including repeated memberships.
-
#delete(o, n = nil) ⇒ Object
Deletes all the identical object from the set and returns self.
-
#delete_if ⇒ Object
Deletes every element of the set for which block evaluates to true, and returns self.
-
#each ⇒ Object
Calls the given block once for each element in the set, passing the element as parameter.
-
#eql?(set) ⇒ Boolean
(also: #==)
Returns true if two sets are equal.
-
#initialize(*args, &block) ⇒ Multiset
constructor
:nodoc:.
-
#merge(enum) ⇒ Object
Merges the elements of the given enumerable object to the set and returns self.
-
#multiplicity(e) ⇒ Object
Returns the number of times an element belongs to the multiset.
-
#proper_subset?(set) ⇒ Boolean
Returns true if the set is a proper subset of the given set.
-
#proper_superset?(set) ⇒ Boolean
Returns true if the set is a proper superset of the given set.
-
#subset?(set) ⇒ Boolean
Returns true if the set is a subset of the given set.
-
#subtract(enum) ⇒ Object
Deletes every element that appears in the given enumerable object and returns self.
-
#superset?(set) ⇒ Boolean
Returns true if the set is a superset of the given set.
-
#to_a ⇒ Object
Converts the set to an array.
Constructor Details
#initialize(*args, &block) ⇒ Multiset
:nodoc:
6 7 8 9 |
# File 'lib/rack/mount/vendor/multimap/multiset.rb', line 6 def initialize(*args, &block) #:nodoc: @hash = Hash.new(0) super end |
Instance Method Details
#&(enum) ⇒ Object Also known as: intersection
Returns a new set containing elements common to the set and the given enumerable object.
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/rack/mount/vendor/multimap/multiset.rb', line 120 def &(enum) s = dup n = self.class.new enum.each { |o| if s.include?(o) s.delete(o, 1) n.add(o) end } n end |
#^(enum) ⇒ Object
Returns a new set containing elements exclusive between the set and the given enumerable object. (set ^ enum) is equivalent to ((set | enum) - (set & enum)).
136 137 138 139 140 |
# File 'lib/rack/mount/vendor/multimap/multiset.rb', line 136 def ^(enum) n = self.class.new(enum) each { |o| n.include?(o) ? n.delete(o, 1) : n.add(o) } n end |
#add(o) ⇒ Object Also known as: <<
Adds the given object to the set and returns self. Use merge
to add many elements at once.
71 72 73 74 75 |
# File 'lib/rack/mount/vendor/multimap/multiset.rb', line 71 def add(o) @hash[o] ||= 0 @hash[o] += 1 self end |
#cardinality ⇒ Object Also known as: size, length
Returns the total number of elements in a multiset, including repeated memberships
18 19 20 |
# File 'lib/rack/mount/vendor/multimap/multiset.rb', line 18 def cardinality @hash.inject(0) { |s, (e, m)| s += m } end |
#delete(o, n = nil) ⇒ Object
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.
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/rack/mount/vendor/multimap/multiset.rb', line 84 def delete(o, n = nil) if n @hash[o] ||= 0 @hash[o] -= n if @hash[o] > 0 @hash.delete(o) if @hash[o] == 0 else @hash.delete(o) end self end |
#delete_if ⇒ Object
Deletes every element of the set for which block evaluates to true, and returns self.
99 100 101 102 |
# File 'lib/rack/mount/vendor/multimap/multiset.rb', line 99 def delete_if each { |o| delete(o) if yield(o) } self end |
#each ⇒ Object
Calls the given block once for each element in the set, passing the element as parameter. Returns an enumerator if no block is given.
60 61 62 63 64 65 66 67 |
# File 'lib/rack/mount/vendor/multimap/multiset.rb', line 60 def each @hash.each_pair do |key, multiplicity| multiplicity.times do yield(key) end end self end |
#eql?(set) ⇒ Boolean Also known as: ==
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?.
146 147 148 149 150 151 |
# File 'lib/rack/mount/vendor/multimap/multiset.rb', line 146 def eql?(set) return true if equal?(set) set = self.class.new(set) unless set.is_a?(self.class) return false unless cardinality == set.cardinality superset?(set) && subset?(set) end |
#merge(enum) ⇒ Object
Merges the elements of the given enumerable object to the set and returns self.
106 107 108 109 |
# File 'lib/rack/mount/vendor/multimap/multiset.rb', line 106 def merge(enum) enum.each { |o| add(o) } self end |
#multiplicity(e) ⇒ Object
Returns the number of times an element belongs to the multiset.
12 13 14 |
# File 'lib/rack/mount/vendor/multimap/multiset.rb', line 12 def multiplicity(e) @hash[e] end |
#proper_subset?(set) ⇒ Boolean
Returns true if the set is a proper subset of the given set.
51 52 53 54 55 |
# File 'lib/rack/mount/vendor/multimap/multiset.rb', line 51 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 |
#proper_superset?(set) ⇒ Boolean
Returns true if the set is a proper superset of the given set.
37 38 39 40 41 |
# File 'lib/rack/mount/vendor/multimap/multiset.rb', line 37 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 |
#subset?(set) ⇒ Boolean
Returns true if the set is a subset of the given set.
44 45 46 47 48 |
# File 'lib/rack/mount/vendor/multimap/multiset.rb', line 44 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 |
#subtract(enum) ⇒ Object
Deletes every element that appears in the given enumerable object and returns self.
113 114 115 116 |
# File 'lib/rack/mount/vendor/multimap/multiset.rb', line 113 def subtract(enum) enum.each { |o| delete(o, 1) } self end |
#superset?(set) ⇒ Boolean
Returns true if the set is a superset of the given set.
30 31 32 33 34 |
# File 'lib/rack/mount/vendor/multimap/multiset.rb', line 30 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 |
#to_a ⇒ Object
Converts the set to an array. The order of elements is uncertain.
25 26 27 |
# File 'lib/rack/mount/vendor/multimap/multiset.rb', line 25 def to_a inject([]) { |ary, (key, _)| ary << key } end |