Class: Set
- Inherits:
-
Object
- Object
- Set
- Defined in:
- mrbgems/mruby-set/src/set.c,
mrbgems/mruby-set/mrblib/set.rb
Instance Method Summary collapse
-
#^(enum) ⇒ Object
call-seq: set ^ enum -> new_set.
-
#__do_with_enum(enum, &block) ⇒ Object
internal method.
-
#classify ⇒ Object
call-seq: set.classify { |obj| block } -> hash set.classify -> enumerator.
-
#collect! ⇒ Object
(also: #map!)
call-seq: set.collect! { |obj| block } -> set set.map! { |obj| block } -> set set.collect! -> enumerator set.map! -> enumerator.
-
#delete_if ⇒ Object
call-seq: set.delete_if { |obj| block } -> set set.delete_if -> enumerator.
-
#difference(enum) ⇒ Object
(also: #-)
call-seq: set.difference(enum) -> new_set set - enum -> new_set.
-
#divide(&func) ⇒ Object
call-seq: set.divide { |obj1, obj2| block } -> set set.divide -> enumerator.
-
#each(&block) ⇒ Object
call-seq: set.each { |obj| block } -> set set.each -> enumerator.
-
#initialize(enum = nil, &block) ⇒ Set
constructor
call-seq: Set.new(enum = nil) -> set Set.new(enum = nil) { |obj| block } -> set.
-
#intersection(enum) ⇒ Object
(also: #&)
call-seq: set.intersection(enum) -> new_set set & enum -> new_set.
-
#keep_if ⇒ Object
call-seq: set.keep_if { |obj| block } -> set set.keep_if -> enumerator.
-
#merge(enum) ⇒ Object
call-seq: set.merge(enum) -> self.
-
#reject!(&block) ⇒ Object
call-seq: set.reject! { |obj| block } -> set or nil set.reject! -> enumerator.
-
#replace(enum) ⇒ Object
call-seq: set.replace(enum) -> self.
-
#select!(&block) ⇒ Object
(also: #filter!)
call-seq: set.select! { |obj| block } -> set or nil set.filter! { |obj| block } -> set or nil set.select! -> enumerator set.filter! -> enumerator.
-
#subtract(enum) ⇒ Object
call-seq: set.subtract(enum) -> self.
-
#union(enum) ⇒ Object
(also: #|, #+)
call-seq: set.union(enum) -> new_set set | enum -> new_set set + enum -> new_set.
Constructor Details
#initialize(enum = nil, &block) ⇒ Set
call-seq:
Set.new(enum = nil) -> set
Set.new(enum = nil) { |obj| block } -> set
Creates a new set containing the elements of the given enumerable object. If a block is given, the elements are preprocessed by the given block.
Set.new([1, 2, 3]) #=> #<Set: {1, 2, 3}>
Set.new([1, 2, 2, 3]) #=> #<Set: {1, 2, 3}>
Set.new([1, 2, 3]) { |x| x * 2 } #=> #<Set: {2, 4, 6}>
14 15 16 17 18 19 20 21 22 23 24 |
# File 'mrbgems/mruby-set/mrblib/set.rb', line 14 def initialize(enum = nil, &block) __init return self if enum.nil? if block __do_with_enum(enum) { add(block.call(_1)) } else merge(enum) end self end |
Instance Method Details
#^(enum) ⇒ Object
call-seq:
set ^ enum -> new_set
Returns a new set containing elements exclusive between the set and the given enumerable object. (set ^ enum) is equivalent to ((set | enum) - (set & enum)).
Set.new([1, 2, 3]) ^ [2, 3, 4] #=> #<Set: {1, 4}>
Set.new([1, 2]) ^ [2, 3] #=> #<Set: {1, 3}>
157 158 159 160 161 162 |
# File 'mrbgems/mruby-set/mrblib/set.rb', line 157 def ^(enum) __xor(enum) || begin s2 = Set.new(enum) (self | s2) - (self & s2) end end |
#__do_with_enum(enum, &block) ⇒ Object
internal method
27 28 29 30 31 32 33 |
# File 'mrbgems/mruby-set/mrblib/set.rb', line 27 def __do_with_enum(enum, &block) if enum.respond_to?(:each) enum.each(&block) else raise ArgumentError, "value must be enumerable" end end |
#classify ⇒ Object
call-seq:
set.classify { |obj| block } -> hash
set.classify -> enumerator
Classifies the set by the return value of the given block and returns a hash of => set of elements pairs. Returns an enumerator if no block is given.
set = Set.new([1, 2, 3, 4, 5, 6])
set.classify { |x| x % 3 }
#=> {1=>#<Set: {1, 4}>, 2=>#<Set: {2, 5}>, 0=>#<Set: {3, 6}>}
294 295 296 297 298 299 300 301 302 |
# File 'mrbgems/mruby-set/mrblib/set.rb', line 294 def classify return to_enum(:classify) unless block_given? h = {} each {|i| x = yield(i) (h[x] ||= self.class.new).add(i) } h end |
#collect! ⇒ Object Also known as: map!
call-seq:
set.collect! { |obj| block } -> set
set.map! { |obj| block } -> set
set.collect! -> enumerator
set.map! -> enumerator
Replaces the elements with ones returned by collect(). Returns an enumerator if no block is given.
set = Set.new([1, 2, 3])
set.collect! { |x| x * 2 } #=> #<Set: {2, 4, 6}>
set #=> #<Set: {2, 4, 6}>
233 234 235 236 237 238 |
# File 'mrbgems/mruby-set/mrblib/set.rb', line 233 def collect! return to_enum(:collect!) unless block_given? set = self.class.new each { set << yield(_1) } replace(set) end |
#delete_if ⇒ Object
call-seq:
set.delete_if { |obj| block } -> set
set.delete_if -> enumerator
Deletes every element of the set for which block evaluates to true, and returns self. Returns an enumerator if no block is given.
set = Set.new([1, 2, 3, 4, 5])
set.delete_if { |x| x.even? } #=> #<Set: {1, 3, 5}>
set #=> #<Set: {1, 3, 5}>
195 196 197 198 199 |
# File 'mrbgems/mruby-set/mrblib/set.rb', line 195 def delete_if return to_enum(:delete_if) unless block_given? select { yield _1 }.each { delete(_1) } self end |
#difference(enum) ⇒ Object Also known as: -
call-seq:
set.difference(enum) -> new_set
set - enum -> new_set
Returns a new set built by duplicating the set, removing every element that appears in the given enumerable object.
Set.new([1, 2, 3, 4]).difference([2, 4]) #=> #<Set: {1, 3}>
Set.new([1, 2, 3, 4]) - [2, 4] #=> #<Set: {1, 3}>
136 137 138 139 140 141 142 |
# File 'mrbgems/mruby-set/mrblib/set.rb', line 136 def difference(enum) __difference(enum) || begin result = dup __do_with_enum(enum) {|o| result.delete(o) } result end end |
#divide(&func) ⇒ Object
call-seq:
set.divide { |obj1, obj2| block } -> set
set.divide -> enumerator
Divides the set into a set of subsets according to the commonality defined by the given block. Returns an enumerator if no block is given.
set = Set.new([1, 2, 3, 4, 5, 6])
set.divide { |x, y| (x % 3) == (y % 3) }
#=> #<Set: {#<Set: {1, 4}>, #<Set: {2, 5}>, #<Set: {3, 6}>}>
316 317 318 319 320 321 322 323 324 |
# File 'mrbgems/mruby-set/mrblib/set.rb', line 316 def divide(&func) return to_enum(:divide) unless block_given? if func.arity == 2 raise NotImplementedError, "Set#divide with 2 arity block is not implemented." end Set.new(classify(&func).values) end |
#each(&block) ⇒ Object
call-seq:
set.each { |obj| block } -> set
set.each -> enumerator
Calls the given block once for each element in the set, passing the element as parameter. Returns an enumerator if no block is given.
Set.new([1, 2, 3]).each { |x| puts x }
# prints: 1, 2, 3
#=> #<Set: {1, 2, 3}>
176 177 178 179 180 181 |
# File 'mrbgems/mruby-set/mrblib/set.rb', line 176 def each(&block) return to_enum(:each) unless block_given? # Use C implementation's to_a method and iterate to_a.each(&block) self end |
#intersection(enum) ⇒ Object Also known as: &
call-seq:
set.intersection(enum) -> new_set
set & enum -> new_set
Returns a new set containing elements common to the set and the given enumerable object.
Set.new([1, 2, 3]).intersection([2, 3, 4]) #=> #<Set: {2, 3}>
Set.new([1, 2, 3]) & [2, 3, 4] #=> #<Set: {2, 3}>
93 94 95 96 97 98 99 |
# File 'mrbgems/mruby-set/mrblib/set.rb', line 93 def intersection(enum) __intersection(enum) || begin n = Set.new __do_with_enum(enum) {|o| n.add(o) if include?(o) } n end end |
#keep_if ⇒ Object
call-seq:
set.keep_if { |obj| block } -> set
set.keep_if -> enumerator
Deletes every element of the set for which block evaluates to false, and returns self. Returns an enumerator if no block is given.
set = Set.new([1, 2, 3, 4, 5])
set.keep_if { |x| x.even? } #=> #<Set: {2, 4}>
set #=> #<Set: {2, 4}>
213 214 215 216 217 |
# File 'mrbgems/mruby-set/mrblib/set.rb', line 213 def keep_if return to_enum(:keep_if) unless block_given? reject { yield _1 }.each { delete(_1) } self end |
#merge(enum) ⇒ Object
call-seq:
set.merge(enum) -> self
Merges the elements of the given enumerable object to the set and returns self.
set = Set.new([1, 2])
set.merge([2, 3, 4]) #=> #<Set: {1, 2, 3, 4}>
set #=> #<Set: {1, 2, 3, 4}>
45 46 47 48 |
# File 'mrbgems/mruby-set/mrblib/set.rb', line 45 def merge(enum) __merge(enum) || __do_with_enum(enum) {|o| add(o) } self end |
#reject!(&block) ⇒ Object
call-seq:
set.reject! { |obj| block } -> set or nil
set.reject! -> enumerator
Equivalent to Set#delete_if, but returns nil if no changes were made. Returns an enumerator if no block is given.
set = Set.new([1, 2, 3, 4, 5])
set.reject! { |x| x.even? } #=> #<Set: {1, 3, 5}>
set.reject! { |x| x > 10 } #=> nil
253 254 255 256 257 258 |
# File 'mrbgems/mruby-set/mrblib/set.rb', line 253 def reject!(&block) return to_enum(:reject!) unless block_given? n = size delete_if(&block) size == n ? nil : self end |
#replace(enum) ⇒ Object
call-seq:
set.replace(enum) -> self
Replaces the contents of the set with the contents of the given enumerable object and returns self.
set = Set.new([1, 2, 3])
set.replace([4, 5, 6]) #=> #<Set: {4, 5, 6}>
set #=> #<Set: {4, 5, 6}>
61 62 63 64 |
# File 'mrbgems/mruby-set/mrblib/set.rb', line 61 def replace(enum) clear merge(enum) end |
#select!(&block) ⇒ Object Also known as: filter!
call-seq:
set.select! { |obj| block } -> set or nil
set.filter! { |obj| block } -> set or nil
set.select! -> enumerator
set.filter! -> enumerator
Equivalent to Set#keep_if, but returns nil if no changes were made. Returns an enumerator if no block is given.
set = Set.new([1, 2, 3, 4, 5])
set.select! { |x| x.even? } #=> #<Set: {2, 4}>
set.select! { |x| x.even? } #=> nil
274 275 276 277 278 279 |
# File 'mrbgems/mruby-set/mrblib/set.rb', line 274 def select!(&block) return to_enum(:select!) unless block_given? n = size keep_if(&block) size == n ? nil : self end |
#subtract(enum) ⇒ Object
call-seq:
set.subtract(enum) -> self
Deletes every element that appears in the given enumerable object and returns self.
set = Set.new([1, 2, 3, 4])
set.subtract([2, 4]) #=> #<Set: {1, 3}>
set #=> #<Set: {1, 3}>
77 78 79 80 |
# File 'mrbgems/mruby-set/mrblib/set.rb', line 77 def subtract(enum) __subtract(enum) || __do_with_enum(enum) {|o| delete(o) } self end |
#union(enum) ⇒ Object Also known as: |, +
call-seq:
set.union(enum) -> new_set
set | enum -> new_set
set + enum -> new_set
Returns a new set built by merging the set and the elements of the given enumerable object.
Set.new([1, 2]).union([2, 3, 4]) #=> #<Set: {1, 2, 3, 4}>
Set.new([1, 2]) | [2, 3, 4] #=> #<Set: {1, 2, 3, 4}>
Set.new([1, 2]) + [2, 3, 4] #=> #<Set: {1, 2, 3, 4}>
117 118 119 |
# File 'mrbgems/mruby-set/mrblib/set.rb', line 117 def union(enum) __union(enum) || dup.merge(enum) end |