Class GenericBitSet<E>
- Type Parameters:
E
- type of elements
- All Implemented Interfaces:
Serializable
,Iterable<E>
,Collection<E>
,Set<E>
,SetEx<E>
,Copyable<SetEx<E>>
- Direct Known Subclasses:
IndexerBitSet
Set
.
To store objects in bit set, the subclasses need to take care of
the mappings between objects and indexes by implementing getIndex(E)
and getElement(int)
. The objects stored in the same bit set s
should preserve the invariant:
e.equals(s.getElement(s.getIndex(e)))
.
Note: objects in different contexts may be mapped to the same index, and
it may cause unexpected behaviors if they are stored in the same bit set.
To avoid this, for each bit set, we require the subclasses to provide
a context object which indicates the context of the objects represented
by the bits in the set. We consider set operations (e.g., addAll(java.util.Collection<? extends E>)
and containsAll(java.util.Collection<?>)
) meaningful only when the two bit sets have
the equivalent context object.
TODO: add mod count
- See Also:
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
boolean
addAll
(Collection<? extends E> c) addAllDiff
(Collection<? extends E> c) Adds all elements in collectionc
, and returns the difference set betweenc
and this set (before the call).void
clear()
boolean
boolean
containsAll
(Collection<?> c) copy()
Creates and returns a copy of this object.protected abstract Object
protected abstract E
getElement
(int index) Maps an index to the corresponding object.protected abstract int
Maps an object to the corresponding index.int
hashCode()
boolean
hasOverlapWith
(Set<E> other) boolean
isEmpty()
iterator()
protected abstract GenericBitSet<E>
newSet()
Creates and returns a new set.boolean
boolean
removeAll
(Collection<?> c) boolean
retainAll
(Collection<?> c) void
setTo
(Collection<E> c) Sets the content of this bit set to the same as given collection.int
size()
Methods inherited from class java.util.AbstractSet
equals
Methods inherited from class java.util.AbstractCollection
toArray, toArray, toString
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
Methods inherited from interface java.util.Set
equals, spliterator, toArray, toArray
-
Field Details
-
bitSet
-
-
Constructor Details
-
GenericBitSet
protected GenericBitSet(boolean isSparse)
-
-
Method Details
-
contains
- Specified by:
contains
in interfaceCollection<E>
- Specified by:
contains
in interfaceSet<E>
- Overrides:
contains
in classAbstractCollection<E>
-
add
- Specified by:
add
in interfaceCollection<E>
- Specified by:
add
in interfaceSet<E>
- Overrides:
add
in classAbstractCollection<E>
-
remove
- Specified by:
remove
in interfaceCollection<E>
- Specified by:
remove
in interfaceSet<E>
- Overrides:
remove
in classAbstractCollection<E>
-
containsAll
- Specified by:
containsAll
in interfaceCollection<E>
- Specified by:
containsAll
in interfaceSet<E>
- Overrides:
containsAll
in classAbstractCollection<E>
-
addAll
- Specified by:
addAll
in interfaceCollection<E>
- Specified by:
addAll
in interfaceSet<E>
- Overrides:
addAll
in classAbstractCollection<E>
-
removeAll
- Specified by:
removeAll
in interfaceCollection<E>
- Specified by:
removeAll
in interfaceSet<E>
- Overrides:
removeAll
in classAbstractSet<E>
-
retainAll
- Specified by:
retainAll
in interfaceCollection<E>
- Specified by:
retainAll
in interfaceSet<E>
- Overrides:
retainAll
in classAbstractCollection<E>
-
setTo
Sets the content of this bit set to the same as given collection. -
isEmpty
public boolean isEmpty()- Specified by:
isEmpty
in interfaceCollection<E>
- Specified by:
isEmpty
in interfaceSet<E>
- Overrides:
isEmpty
in classAbstractCollection<E>
-
clear
public void clear()- Specified by:
clear
in interfaceCollection<E>
- Specified by:
clear
in interfaceSet<E>
- Overrides:
clear
in classAbstractCollection<E>
-
size
public int size()- Specified by:
size
in interfaceCollection<E>
- Specified by:
size
in interfaceSet<E>
- Specified by:
size
in classAbstractCollection<E>
-
iterator
-
hashCode
public int hashCode()- Specified by:
hashCode
in interfaceCollection<E>
- Specified by:
hashCode
in interfaceSet<E>
- Overrides:
hashCode
in classAbstractSet<E>
-
copy
Description copied from interface:Copyable
Creates and returns a copy of this object. -
addAllDiff
Description copied from interface:SetEx
Adds all elements in collectionc
, and returns the difference set betweenc
and this set (before the call).- Specified by:
addAllDiff
in interfaceSetEx<E>
- Overrides:
addAllDiff
in classAbstractSetEx<E>
- Returns:
- a set of elements that are contained in
c
but not in this set before the call.
-
newSet
Description copied from class:AbstractSetEx
Creates and returns a new set. The type of the new set should be the corresponding subclass. This method is provided to ease the implementation ofAbstractSetEx.copy()
andAbstractSetEx.addAllDiff(Collection)
. If a subclass overwrites above two methods, it does not need to re-implement this method.- Overrides:
newSet
in classAbstractSetEx<E>
-
hasOverlapWith
- Specified by:
hasOverlapWith
in interfaceSetEx<E>
- Overrides:
hasOverlapWith
in classAbstractSetEx<E>
- Returns:
true
if this set has at least one element contained in the given set.
-
getContext
- Returns:
- the context for the objects represented by the bits in this set.
-
getIndex
Maps an object to the corresponding index. -
getElement
Maps an index to the corresponding object.
-