T
- A type that all elements in the domain share.public interface DomainBitSet<T> extends Iterable<T>, Cloneable, Serializable
The methods that return a DomainBitSet<T> are expected to create a new object and not
change the state of this object. The implementation could be mutable or immutable. Mutable
implementations should not implement Set
<T>, because the specifications of
equals(Object)
are different.
Methods such as union(Iterable)
, toSet()
, and complement()
return a
new and independent set. This allows a functional style of programming.
However, this set could be mutable. This allows the classic imperative style of programming.
Note that the implementation is not necessarily a bit set. It could be any data structure that
allows to add and remove elements of the domain. Therefore, all elements of the domain should
implement hashCode()
and equals(Object)
for correct behavior and better
performance. The iterator
can return the elements in any order.
Modifier and Type | Method and Description |
---|---|
static <T> DomainBitSet<T> |
allOf(List<T> elements)
Creates a set with the given domain, that contains all elements.
|
static <T> DomainBitSet<T> |
allOf(T... elements)
Creates a set with the given domain, that contains all elements.
|
default DomainBitSet<T> |
clone()
Default implementation of a
cloning-method using
union(BigInteger) . |
default DomainBitSet<T> |
complement()
Creates a new set with the same domain, initially containing all the elements of the domain
that are not contained in this set.
|
default boolean |
contains(Object o)
Returns true if this set contains the specified element.
|
default boolean |
containsAll(Collection<?> c)
Returns true if this set contains all of the elements in the specified collection.
|
static DomainBitSet<Enum<?>> |
createMultiEnumBitSet(Class<? extends Enum<?>>... enumTypes)
Creates a general bit set with a domain that consists of all elements of all given enum types.
|
default <Y> Set<Pair<?,T,Y>> |
cross(DomainBitSet<Y> set)
Returns the Cartesian Product.
|
default <Y> void |
cross(DomainBitSet<Y> set,
BiConsumer<T,Y> consumer)
Creates the Cartesian Product and applies a given function to all coordinates.
|
default boolean |
domainContains(T object)
Searches an object in the domain of this set.
|
boolean |
equals(Object other)
Compares the specified object with this domain bit set for equality.
|
boolean |
getBit(int bitIndex)
Returns the value of the bit with the specified index.
|
Domain<T> |
getDomain()
Returns a distinct list, containing all elements of the domain.
|
default Optional<T> |
getElement(int index)
Returns an Optional that might contain the element at the specified position.
|
int |
hashCode()
Hash code of domain and elements.
|
DomainBitSet<T> |
intersect(BigInteger mask)
The intersection of this and a given mask.
|
DomainBitSet<T> |
intersect(BitSet set)
The intersection of this and a given bit set.
|
DomainBitSet<T> |
intersect(Iterable<T> set)
Intersection of this and the given set.
|
DomainBitSet<T> |
intersect(long mask)
Intersection of this and the given set.
|
default DomainBitSet<T> |
intersectVarArgs(T... set)
The intersection of this set and a set represented by an array (varargs).
|
default boolean |
isEmpty()
Returns true if this set contains no elements.
|
Iterator<T> |
iterator()
Returns an iterator over elements of type T.
|
default <S> DomainBitSet<S> |
map(Domain<S> domain)
Returns a new set with elements of a given domain, containing all mapped elements.
|
default <S> DomainBitSet<S> |
map(Domain<S> domain,
Function<T,S> mapper)
Returns a new set with elements of a given domain, containing all mapped elements.
|
DomainBitSet<T> |
minus(BigInteger mask)
The relative complement of this set and a set represented by a
BigInteger . |
DomainBitSet<T> |
minus(BitSet set)
The relative complement of this set and a set represented by a
BitSet . |
DomainBitSet<T> |
minus(Iterable<T> set)
The relative complement of this set and another set.
|
DomainBitSet<T> |
minus(long mask)
The relative complement of this set and a set represented by a bit mask.
|
default DomainBitSet<T> |
minusVarArgs(T... set)
The relative complement of this set and a set represented by an array (varargs).
|
static <T> DomainBitSet<T> |
noneOf(List<T> elements)
Creates a set with the given domain, that contains none of the elements.
|
static <T> DomainBitSet<T> |
noneOf(T... elements)
Creates a set with the given domain, that contains none of the elements.
|
default boolean |
ofEqualDomain(DomainBitSet<T> set)
Compares the domains.
|
default boolean |
ofEqualElements(DomainBitSet<T> set)
Compares the elements, ignoring the domains.
|
default Stream<T> |
parallelStream()
Returns a possibly parallel
Stream with this set as its source. |
default Iterable<? extends DomainBitSet<T>> |
powerset()
The powerset, which is the set of all subsets.
|
default void |
powerset(Consumer<DomainBitSet<T>> consumer,
boolean blocking)
Pass all subsets to a given consumer.
|
default <S> DomainBitSet<T> |
semijoin(DomainBitSet<S> set,
BiPredicate<T,S> predicate)
Returns a new set with all elements in this set, that have a matching element in the other set.
|
int |
size()
The number of elements in this set.
|
default Spliterator<T> |
spliterator()
Creates a
Spliterator over the elements in this collection. |
default Stream<T> |
stream()
Returns a sequential
Stream with this collection as its source. |
BigInteger |
toBigInteger()
A representation of the elements in this set as a
BigInteger . |
BitSet |
toBitSet()
A representation of the elements in this set as a
BitSet . |
long |
toLong()
A representation of the elements in this set as a
long . |
Set<T> |
toSet()
A regular set, with no defined domain.
|
DomainBitSet<T> |
union(BigInteger mask)
The union of this set and a set represented by a
BitSet . |
DomainBitSet<T> |
union(BitSet set)
The union of this set and a set represented by a
BitSet . |
DomainBitSet<T> |
union(Iterable<T> set)
The union of this set and a set represented by an
iterable collection. |
DomainBitSet<T> |
union(long mask)
The union of this set and a set represented by a bit mask.
|
default DomainBitSet<T> |
unionVarArgs(T... set)
The union of this set and a set represented by an array (varargs).
|
default Stream<Pair<Object,Integer,T>> |
zipWithPosition()
Returns a sequential stream with pairs of all elements of this set and their position in the
domain.
|
static <T> DomainBitSet<T> allOf(List<T> elements)
T
- The type of the elements.elements
- The elements of the domain and the set.@SafeVarargs static <T> DomainBitSet<T> allOf(T... elements)
T
- The type of the elements.elements
- The elements of the domain and the set.@SafeVarargs static DomainBitSet<Enum<?>> createMultiEnumBitSet(Class<? extends Enum<?>>... enumTypes)
enumTypes
- All enum types that define the domain. The ordering is relevant.static <T> DomainBitSet<T> noneOf(List<T> elements)
T
- The type of the elements.elements
- The elements of the domain.@SafeVarargs static <T> DomainBitSet<T> noneOf(T... elements)
T
- The type of the elements.elements
- The elements of the domain.default DomainBitSet<T> clone()
cloning-method
using
union(BigInteger)
.this.union(BigInteger.ZERO)
default DomainBitSet<T> complement()
default boolean contains(Object o)
o
- element whose presence in this collection is to be testedClassCastException
- if the type of the specified element is incompatible with this collection (optional)NullPointerException
- if the specified element is null.Collection.contains(Object)
default boolean containsAll(Collection<?> c)
c
- collection to be checked for containment in this collectionClassCastException
- if the types of one or more elements in the specified collection are incompatible
with this collectionNullPointerException
- if the specified collection contains one or more null elements, or if the specified
collection is null.Collection.containsAll(Collection)
,
contains(Object)
default <Y> Set<Pair<?,T,Y>> cross(DomainBitSet<Y> set)
The returned set has a size of this.size() * set.size()
.
Y
- The type of the elements in the given set.set
- Another set.cross(DomainBitSet, BiConsumer)
,
semijoin(DomainBitSet, BiPredicate)
,
BitSetUtilities.cross(DomainBitSet, DomainBitSet, Class)
default <Y> void cross(DomainBitSet<Y> set, BiConsumer<T,Y> consumer)
Cartesian product of A and B, denoted A × B
, is the set whose members are all
possible ordered pairs (a,b)
where a is a member of A and b is a member of B.
The Cartesian product of {1, 2}
and {red, white}
is {(1, red), (1,
white), (2, red), (2, white)}.
The consumer will be invoked exactly (this.size() × set.size())
times.
A BiFunction can be used by passing ::apply
as consumer.
Example: Pair.curry(mySet::add)::apply
.
Y
- The type of the elements in the given set.set
- Another set.consumer
- A function to consume two elements.cross(DomainBitSet)
,
BitSetUtilities.cross(DomainBitSet, DomainBitSet, Class)
,
semijoin(DomainBitSet, BiPredicate)
default boolean domainContains(T object)
object
- The object to be searched.NullPointerException
- if the object is null.boolean equals(Object other)
DomainBitSet
, the two sets have the same domain, and every
member of the given set is contained in this set.
Comparison of elements only:
this.ofEqualElements(other)
Which is equivalent to:
this.toSet().equals(other.toSet())
Comparison of domain only:
this.ofEqualDomain(other)
equals
in class Object
DomainBitSet
, with the same domain and elements.ofEqualElements(DomainBitSet)
boolean getBit(int bitIndex) throws IndexOutOfBoundsException
true
if the bit
with the index bitIndex
is currently set in this BitSet
; otherwise, the result
is false
.
Note that not all DomainBitSets are implemented as a bit set. In that case this method emulates the behavior of an actual bit set.
bitIndex
- the bit indexIndexOutOfBoundsException
- if the specified index is negative or out of bounds.contains(Object)
,
BitSet.get(int)
,
BigInteger.testBit(int)
Domain<T> getDomain()
All elements are ordered as they are defined in the domain.
Note that the returned set is immutable.
Domain
of this set.default Optional<T> getElement(int index)
The inverse has to be done in the domain:
mySet.getDomain().indexOf(element);
index
- index of an element in the domain.IndexOutOfBoundsException
- if the index is out of rangezipWithPosition()
,
getBit(int)
,
List.indexOf(Object)
DomainBitSet<T> intersect(BigInteger mask)
mask
- The mask of the other set.DomainBitSet<T> intersect(BitSet set)
set
- The bit set representation of the other set.DomainBitSet<T> intersect(Iterable<T> set) throws IllegalArgumentException
set
- An Iterable
collection of elements from the domain.IllegalArgumentException
- If any of the elements are not in the domain.DomainBitSet<T> intersect(long mask) throws MoreThan64ElementsException
mask
- The bit mask of another set.MoreThan64ElementsException
- If the domain contains more than 64 elements, then long can't be used.IllegalArgumentException
- If the domain contains less than 64 elements then some long values are illegal.default DomainBitSet<T> intersectVarArgs(T... set)
set
- A set as an array. Duplicates are ignored. Must not be nor contain null
.intersect(Iterable)
default boolean isEmpty()
Collection.isEmpty()
Iterator<T> iterator()
The order is not defined as this could be backed by a set. Iteration in the same order as the
domain can be done like this:
domainBitSet.getDomain().stream().filter(domainBitSet::contains).forEach(...)
default <S> DomainBitSet<S> map(Domain<S> domain)
If the given domain is the same as the domain of this set then the returned value is equal to
this.clone()
.
S
- Type of given domain. It has to be the same size or larger than the domain of this
set.domain
- The new domainIllegalArgumentException
- if the given domain contains less elements.zipWithPosition()
,
map(Domain, Function)
default <S> DomainBitSet<S> map(Domain<S> domain, Function<T,S> mapper)
This is a convenience method. The same can be done with: this.stream().map(mapper)
S
- Type of given domain.domain
- The new domainmapper
- function to map from T to S.IllegalArgumentException
- if the mapper returns illegal elements.Stream.map(Function)
,
zipWithPosition()
,
map(Domain)
DomainBitSet<T> minus(BigInteger mask)
BigInteger
.mask
- The other set as a bit mask.IllegalArgumentException
- If the parameter is negative.DomainBitSet<T> minus(BitSet set)
BitSet
.set
- The other set.DomainBitSet<T> minus(Iterable<T> set)
set
- The other set.DomainBitSet<T> minus(long mask) throws MoreThan64ElementsException
mask
- The mask representing the other set.MoreThan64ElementsException
- If the domain contains more than 64 elements, then long can't be used.IllegalArgumentException
- If the domain contains less than 64 elements then some long values are illegal.default DomainBitSet<T> minusVarArgs(T... set)
set
- A set as an array. Duplicates are ignored. Must not be nor contain null
.minus(Iterable)
default boolean ofEqualDomain(DomainBitSet<T> set)
This is equal to, but could be a bit faster than
this.getDomain().equals(set.getDomain())
.
set
- The other set.true
if both are of equal domains.default boolean ofEqualElements(DomainBitSet<T> set)
This is equal to, but could be a bit faster than this.toSet().equals(set.toSet())
.
set
- The other set.true
if both contain the same elements.Set.equals(Object)
default Stream<T> parallelStream()
Stream
with this set as its source. It is allowable for
this method to return a sequential stream.Stream
over the elements in this setCollection.parallelStream()
default Iterable<? extends DomainBitSet<T>> powerset() throws MoreThan64ElementsException
Note: Complexity is O(2n)
. For sets with more than 64 elements this
would be insanely large. Therefore this is limited to sets with up to 64 elements. However, the
size of the domain does not matter.
This is not thread safe and has to be processed sequentially.
MoreThan64ElementsException
- if this set contains more than 64 elements. This would result in more than 18E18
subsets.powerset(Consumer, boolean)
default void powerset(Consumer<DomainBitSet<T>> consumer, boolean blocking) throws MoreThan64ElementsException
The consumer must be thread-safe. This will process all possible subsets concurrently, using an
ExecutorService
. For better performance this uses SmallDomainBitSet only.
consumer
- to process each subsetblocking
- if true this will block until all are processedMoreThan64ElementsException
- if this set contains more than 64 elements. This would result in more than 18E18
subsets.powerset()
default <S> DomainBitSet<T> semijoin(DomainBitSet<S> set, BiPredicate<T,S> predicate)
This is basically the same as cross(DomainBitSet)
, but filtered by a predicate. All
this.size() × set.size()
combinations are tested! The term "semijoin" is
used in relational algebra, where the predicate compares the primary attributes of two tuples
(natural join).
set
- The other setpredicate
- Predicate to match the tuplesthis ⋉ set = { t | t ∈ this, s ∈ set : predicate(t, s) }
.cross(DomainBitSet)
,
cross(DomainBitSet, BiConsumer)
,
map(Domain)
,
map(Domain, Function)
int size()
Collection.size()
default Spliterator<T> spliterator()
Spliterator
over the elements in this collection.spliterator
in interface Iterable<T>
Spliterator
over the elements in this collectionCollection.spliterator()
default Stream<T> stream()
Stream
with this collection as its source.Stream
over the elements in this collectionCollection.stream()
BigInteger toBigInteger()
BigInteger
.BigInteger
.BitSet toBitSet()
BitSet
.BitSet
.long toLong() throws MoreThan64ElementsException
long
.long
.MoreThan64ElementsException
- If the domain contains more than 64 elements.Set<T> toSet()
false
for other sets without a domain.DomainBitSet<T> union(BigInteger mask)
BitSet
.
Note: A fast version for BigInteger.ZERO should exist for each implementation!
mask
- A BitSet representing another set.DomainBitSet<T> union(BitSet set)
BitSet
.set
- A BitSet representing another set.DomainBitSet<T> union(Iterable<T> set)
iterable
collection.set
- An Iterable representing another set.DomainBitSet<T> union(long mask) throws MoreThan64ElementsException
mask
- A bit mask representing another set.MoreThan64ElementsException
- If the domain contains more than 64 elements, then long can't be used.IllegalArgumentException
- If the domain contains less than 64 elements then some long values are illegal.default DomainBitSet<T> unionVarArgs(T... set)
set
- A set as an array. Duplicates are ignored. Must not be nor contain null
.union(Iterable)
default Stream<Pair<Object,Integer,T>> zipWithPosition()
BitSetUtilities.toTreeMap()
,
getElement(int)