|
Class: PowerSet
Object
|
+--Collection
|
+--PowerSet
- Package:
- stx:libbasic2
- Category:
- Collections-Unordered
- Version:
- rev:
1.8
date: 2023/11/15 07:26:24
- user: cg
- file: PowerSet.st directory: libbasic2
- module: stx stc-classLibrary: libbasic2
A PowerSet of a collection is the set of all possible (sub-)collections
built from the original collection's elements.
This includes the empty collection.
The PowerSet of [1 2 3] is therefore:
[]
[1]
[2]
[3]
[1 2]
[1 3]
[2 3]
[1 2 3]
As the powerset can quickly become huge in size (for a baseSet of n elements, we
get a powerSet size of 2^N), the elements of the powerset are created dynamically.
I.e. they are only created on demand.
As this, this is a good example of polymorphism: powerSets represent collections,
but do not really store their elements (see Interval for another such Collection).
copyrightCOPYRIGHT (c) 2006 by eXept Software AG
All Rights Reserved
This software is furnished under a license and may be used
only in accordance with the terms of that license and with the
inclusion of the above copyright notice. This software may not
be provided or otherwise made available to, or used by, any
other person. No title to or ownership of the software is
hereby transferred.
instance creation
-
for: aCollection
-
Create & return a new instance for aCollection.
enumerating
-
do: aBlock
-
evaluate the argument, aBlock for each element
initialization
-
initializeFor: aCollection
-
queries
-
includes: aCollection
-
(comment from inherited method)
return true, if an element equal to the argument, searchedElement is in the collection.
This compares using #= (i.e. it does not look for the object itself,
instead, one that compares equal).
See #includesIdentical: when identity is asked for.
This is a *very* slow fallback - many subclasses redefine this for performance.
-
size
-
^ 2 raisedTo:baseSet size
|s p|
s := #(1 2 3).
p := PowerSet for:s.
p size.
p do:[:eachSubSet |
Transcript showCR:eachSubSet
].
|
A huge powerSet - do not enumerate its elements ;-)
|s p|
s := (1 to:1000).
p := PowerSet for:s.
Transcript show:'p''s size is: '; showCR:p size
|
|s p|
s := (1 to:10).
p := PowerSet for:s.
p size.
p includes:#(1 2 3) asSet.
p includes:#(1 2 4) asSet.
p includes:#(0 1 2) asSet.
|
|