Feature #10726 » implement_set_powerset.patch
NEWS | ||
---|---|---|
=== Stdlib updates (outstanding ones only)
|
||
* lib/set.rb
|
||
* New methods:
|
||
* Set#powerset returns set of all subsets of +self+.
|
||
=== Stdlib compatibility issues (excluding feature bug fixes)
|
||
* lib/webrick/utils.rb
|
lib/set.rb | ||
---|---|---|
end
|
||
end
|
||
# Returns set of all subsets of +self+.
|
||
#
|
||
# e.g.:
|
||
#
|
||
# require 'set'
|
||
# set = Set[1, 2].powerset
|
||
# p set # => #<Set: {#<Set: {}>,
|
||
# # #<Set: {1}>,
|
||
# # #<Set: {2}>,
|
||
# # #<Set: {1, 2}>}>
|
||
def powerset
|
||
(0 .. 2 ** length - 1).map { |n|
|
||
select.with_index { |_, i| (n >> i) & 1 == 1 }.to_set
|
||
}.to_set
|
||
end
|
||
InspectKey = :__inspect_key__ # :nodoc:
|
||
# Returns a string containing a human-readable representation of the
|
test/test_set.rb | ||
---|---|---|
}
|
||
end
|
||
def test_powerset
|
||
assert_equal(Set[1, 2].powerset, Set[Set[], Set[1], Set[2], Set[1, 2]])
|
||
assert_equal(
|
||
Set[1, 2, 3].powerset,
|
||
Set[Set[], Set[1], Set[2], Set[3], Set[1, 2], Set[2, 3], Set[3, 1], Set[1, 2, 3]]
|
||
)
|
||
end
|
||
def test_taintness
|
||
orig = set = Set[1,2,3]
|
||
assert_equal false, set.tainted?
|
||
-
|
- « Previous
- 1
- …
- 3
- 4
- 5
- Next »