Class BitSetModule.AbstractBitSet<T>
- All Implemented Interfaces:
BitSet<T>, Foldable<T>, Ordered<T>, Set<T>, SortedSet<T>, Traversable<T>, Function1<T,Boolean>, Value<T>, Serializable, Iterable<T>, Function<T, Boolean>
- Direct Known Subclasses:
BitSetModule.BitSet1, BitSetModule.BitSet2, BitSetModule.BitSetN
- Enclosing interface:
BitSetModule
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface BitSet
BitSet.Builder<T> -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionAdds all of the given elements to this set, if not already contained.addElement(int element) Returns the comparator which defines the order of the elements contained in this collection.booleanShortcut forexists(e -> Objects.equals(e, element)), tests if the givenelementis contained.(package private) abstract long[]copyExpand(int wordsNum) createFromAll(Iterable<? extends T> values) distinctBy(Comparator<? super T> comparator) Returns a new version of this which contains no duplicates.distinctBy(Function<? super T, ? extends U> keyExtractor) Returns a new version of this which contains no duplicates.drop(int n) Drops the first n elements of this or all elements, if this length < n.dropRight(int n) Drops the last n elements of this or all elements, if this length < n.Drops elements while the predicate holds for the current element.booleanIn Vavr there are four basic classes of collections: Seq (sequential elements) Set (distinct elements) Map (indexed elements) Multimap (indexed collections) Two collection instances of these classes are equal if and only if both collections belong to the same basic collection class (Seq, Set, Map or Multimap) contain the same elements have the same element order, if the collections are of type Seq Two Map/Multimap elements, resp.Returns a new traversable consisting of all elements which satisfy the given predicate.fromBitMaskNoCopy(long[] elements) (package private) abstract longgetWord(int index) (package private) abstract intGroups this elements by classifying the elements.inthashCode()Returns the hash code of this collection.init()Dual of Traversable.tail(), returning all elements except the last.Computes the intersection between this set and another set.iterator()An iterator by means of head() and tail().Returns thisBitSetif it is nonempty, otherwiseBitSetcreated from iterable, using existing bitset properties.Returns thisBitSetif it is nonempty, otherwiseBitSetcreated from result of evaluating supplier, using existing bitset properties.Creates a partition of thisTraversableby splitting this elements in two in distinct traversables according to a predicate.Returns a new traversable consisting of all elements which do not satisfy the given predicate.Removes a specific element from this set, if present.Removes all of the given elements from this set, if present.Computes a prefix scan of the elements of the collection.private voidsetElement(long[] words, int element) (package private) long[]shrink(long[] elements) Slides a non-overlapping window of a variable size over thisTraversable.sliding(int size, int step) Slides a window of a specificsizeandstepsize over thisTraversable.Returns a tuple where the first element is the longest prefix of elements that satisfy the givenpredicateand the second element is the remainder.take(int n) Takes the first n elements of this or all elements, if this length < n.takeRight(int n) Takes the last n elements of this or all elements, if this length < n.Takes elements while the predicate holds for the current element.toString()Clarifies that values have a proper toString() method implemented.private voidunsetElement(long[] words, int element) Methods inherited from interface BitSet
add, collect, diff, distinct, dropUntil, flatMap, flatMap, foldRight, grouped, hasDefiniteSize, initOption, isAsync, isLazy, isTraversableAgain, last, map, map, peek, replace, replaceAll, retainAll, scanLeft, scanRight, sliding, stringPrefix, tail, tailOption, takeUntil, toJavaSet, union, unzip, unzip3, zip, zipAll, zipWith, zipWithIndex, zipWithIndexMethods inherited from interface Foldable
fold, reduce, reduceOptionMethods inherited from interface Function1
andThen, arity, compose, curried, isMemoized, memoized, partial, reversed, tupledMethods inherited from interface Set
apply, isDistinct, lengthMethods inherited from interface Traversable
arrangeBy, average, containsAll, count, existsUnique, find, findLast, foldLeft, forEachWithIndex, get, head, headOption, isEmpty, isSequential, isSingleValued, lastOption, max, maxBy, maxBy, min, minBy, minBy, mkCharSeq, mkCharSeq, mkCharSeq, mkString, mkString, mkString, nonEmpty, product, reduceLeft, reduceLeftOption, reduceRight, reduceRightOption, single, singleOption, size, spliterator, sumMethods inherited from interface Value
collect, collect, corresponds, eq, exists, forAll, forEach, getOrElse, getOrElse, getOrElseThrow, getOrElseTry, getOrNull, out, out, stderr, stdout, toArray, toCharSeq, toCompletableFuture, toEither, toEither, toInvalid, toInvalid, toJavaArray, toJavaArray, toJavaArray, toJavaCollection, toJavaList, toJavaList, toJavaMap, toJavaMap, toJavaMap, toJavaOptional, toJavaParallelStream, toJavaSet, toJavaStream, toLeft, toLeft, toLinkedMap, toLinkedMap, toLinkedSet, toList, toMap, toMap, toOption, toPriorityQueue, toPriorityQueue, toQueue, toRight, toRight, toSet, toSortedMap, toSortedMap, toSortedMap, toSortedMap, toSortedSet, toSortedSet, toStream, toTree, toTree, toTry, toTry, toValid, toValid, toValidation, toValidation, toVector
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
fromInt
-
toInt
-
-
Constructor Details
-
AbstractBitSet
-
-
Method Details
-
getWordsNum
abstract int getWordsNum() -
copyExpand
abstract long[] copyExpand(int wordsNum) -
getWord
abstract long getWord(int index) -
createEmpty
-
createFromAll
-
fromBitMaskNoCopy
-
setElement
private void setElement(long[] words, int element) -
unsetElement
private void unsetElement(long[] words, int element) -
shrink
long[] shrink(long[] elements) -
addElement
-
distinctBy
Description copied from interface:TraversableReturns a new version of this which contains no duplicates. Elements are compared using the givencomparator.- Specified by:
distinctByin interfaceBitSet<T>- Specified by:
distinctByin interfaceSet<T>- Specified by:
distinctByin interfaceSortedSet<T>- Specified by:
distinctByin interfaceTraversable<T>- Parameters:
comparator- A comparator- Returns:
- a new
Traversablecontaining this elements without duplicates
-
distinctBy
Description copied from interface:TraversableReturns a new version of this which contains no duplicates. Elements mapped to keys which are compared usingequals.The elements of the result are determined in the order of their occurrence - first match wins.
- Specified by:
distinctByin interfaceBitSet<T>- Specified by:
distinctByin interfaceSet<T>- Specified by:
distinctByin interfaceSortedSet<T>- Specified by:
distinctByin interfaceTraversable<T>- Type Parameters:
U- key type- Parameters:
keyExtractor- A key extractor- Returns:
- a new
Traversablecontaining this elements without duplicates
-
drop
Description copied from interface:TraversableDrops the first n elements of this or all elements, if this length < n.- Specified by:
dropin interfaceBitSet<T>- Specified by:
dropin interfaceSet<T>- Specified by:
dropin interfaceSortedSet<T>- Specified by:
dropin interfaceTraversable<T>- Parameters:
n- The number of elements to drop.- Returns:
- a new instance consisting of all elements of this except the first n ones, or else the empty instance, if this has less than n elements.
-
dropRight
Description copied from interface:TraversableDrops the last n elements of this or all elements, if this length < n.- Specified by:
dropRightin interfaceBitSet<T>- Specified by:
dropRightin interfaceSet<T>- Specified by:
dropRightin interfaceSortedSet<T>- Specified by:
dropRightin interfaceTraversable<T>- Parameters:
n- The number of elements to drop.- Returns:
- a new instance consisting of all elements of this except the last n ones, or else the empty instance, if this has less than n elements.
-
dropWhile
Description copied from interface:TraversableDrops elements while the predicate holds for the current element.Note: This is essentially the same as
dropUntil(predicate.negate()). It is intended to be used with method references, which cannot be negated directly.- Specified by:
dropWhilein interfaceBitSet<T>- Specified by:
dropWhilein interfaceSet<T>- Specified by:
dropWhilein interfaceSortedSet<T>- Specified by:
dropWhilein interfaceTraversable<T>- Parameters:
predicate- A condition tested subsequently for this elements.- Returns:
- a new instance consisting of all elements starting from the first one which does not satisfy the given predicate.
-
intersect
Description copied from interface:SetComputes the intersection between this set and another set.See also
Set.retainAll(Iterable). -
orElse
Returns thisBitSetif it is nonempty, otherwiseBitSetcreated from iterable, using existing bitset properties. -
orElse
Returns thisBitSetif it is nonempty, otherwiseBitSetcreated from result of evaluating supplier, using existing bitset properties.- Specified by:
orElsein interfaceSet<T>- Specified by:
orElsein interfaceSortedSet<T>- Specified by:
orElsein interfaceTraversable<T>- Parameters:
supplier- An alternativeTraversable- Returns:
- this
BitSetif it is nonempty, otherwiseBitSetcreated from result of evaluating supplier, using existing bitset properties.
-
slideBy
Description copied from interface:TraversableSlides a non-overlapping window of a variable size over thisTraversable.Each window contains elements with the same class, as determined by
classifier. Two consecutive values in thisTraversablewill be in the same window only ifclassifierreturns equal values for them. Otherwise, the values will constitute the last element of the previous window and the first element of the next window.Examples:
[].slideBy(Function.identity()) = [] [1,2,3,4,4,5].slideBy(Function.identity()) = [[1],[2],[3],[4,4],[5]] [1,2,3,10,12,5,7,20,29].slideBy(x -> x/10) = [[1,2,3],[10,12],[5,7],[20,29]]- Specified by:
slideByin interfaceBitSet<T>- Specified by:
slideByin interfaceSet<T>- Specified by:
slideByin interfaceSortedSet<T>- Specified by:
slideByin interfaceTraversable<T>- Parameters:
classifier- A function which classifies elements into classes- Returns:
- A new Iterator of windows of the grouped elements
-
sliding
Description copied from interface:TraversableSlides a window of a specificsizeandstepsize over thisTraversable.Examples:
[].sliding(1,1) = [] [1,2,3,4,5].sliding(2,3) = [[1,2],[4,5]] [1,2,3,4,5].sliding(2,4) = [[1,2],[5]] [1,2,3,4,5].sliding(2,5) = [[1,2]] [1,2,3,4].sliding(5,3) = [[1,2,3,4],[4]]- Specified by:
slidingin interfaceBitSet<T>- Specified by:
slidingin interfaceSet<T>- Specified by:
slidingin interfaceSortedSet<T>- Specified by:
slidingin interfaceTraversable<T>- Parameters:
size- a positive window sizestep- a positive step size- Returns:
- a new Iterator of windows of a specific size using a specific step size
-
span
Description copied from interface:TraversableReturns a tuple where the first element is the longest prefix of elements that satisfy the givenpredicateand the second element is the remainder.- Specified by:
spanin interfaceBitSet<T>- Specified by:
spanin interfaceSet<T>- Specified by:
spanin interfaceSortedSet<T>- Specified by:
spanin interfaceTraversable<T>- Parameters:
predicate- A predicate.- Returns:
- a
Tuplecontaining the longest prefix of elements that satisfy p and the remainder.
-
scan
Description copied from interface:TraversableComputes a prefix scan of the elements of the collection. Note: The neutral element z may be applied more than once.- Specified by:
scanin interfaceBitSet<T>- Specified by:
scanin interfaceSet<T>- Specified by:
scanin interfaceSortedSet<T>- Specified by:
scanin interfaceTraversable<T>- Parameters:
zero- neutral element for the operator opoperation- the associative operator for the scan- Returns:
- a new traversable collection containing the prefix scan of the elements in this traversable collection
-
partition
Description copied from interface:TraversableCreates a partition of thisTraversableby splitting this elements in two in distinct traversables according to a predicate.- Specified by:
partitionin interfaceBitSet<T>- Specified by:
partitionin interfaceSet<T>- Specified by:
partitionin interfaceSortedSet<T>- Specified by:
partitionin interfaceTraversable<T>- Parameters:
predicate- A predicate which classifies an element if it is in the first or the second traversable.- Returns:
- A disjoint union of two traversables. The first
Traversablecontains all elements that satisfy the givenpredicate, the secondTraversablecontains all elements that don't. The original order of elements is preserved.
-
filter
Description copied from interface:TraversableReturns a new traversable consisting of all elements which satisfy the given predicate. -
reject
Description copied from interface:TraversableReturns a new traversable consisting of all elements which do not satisfy the given predicate.The default implementation is equivalent to
filter(predicate.negate() -
groupBy
Description copied from interface:TraversableGroups this elements by classifying the elements.- Specified by:
groupByin interfaceBitSet<T>- Specified by:
groupByin interfaceSet<T>- Specified by:
groupByin interfaceSortedSet<T>- Specified by:
groupByin interfaceTraversable<T>- Type Parameters:
C- classified class type- Parameters:
classifier- A function which classifies elements into classes- Returns:
- A Map containing the grouped elements
- See Also:
-
comparator
Description copied from interface:OrderedReturns the comparator which defines the order of the elements contained in this collection.- Specified by:
comparatorin interfaceOrdered<T>- Returns:
- The comparator that defines the order of this collection's elements.
-
takeWhile
Description copied from interface:TraversableTakes elements while the predicate holds for the current element.- Specified by:
takeWhilein interfaceBitSet<T>- Specified by:
takeWhilein interfaceSet<T>- Specified by:
takeWhilein interfaceSortedSet<T>- Specified by:
takeWhilein interfaceTraversable<T>- Parameters:
predicate- A condition tested subsequently for the contained elements.- Returns:
- a new instance consisting of all elements before the first one which does not satisfy the given predicate.
-
addAll
Description copied from interface:SetAdds all of the given elements to this set, if not already contained. -
contains
Description copied from interface:ValueShortcut forexists(e -> Objects.equals(e, element)), tests if the givenelementis contained. -
init
Description copied from interface:TraversableDual of Traversable.tail(), returning all elements except the last. -
iterator
Description copied from interface:TraversableAn iterator by means of head() and tail(). Subclasses may want to override this method. -
take
Description copied from interface:TraversableTakes the first n elements of this or all elements, if this length < n.The result is equivalent to
sublist(0, max(0, min(length(), n)))but does not throw ifn < 0orn > length().In the case of
n < 0the empty instance is returned, in the case ofn > length()this is returned.- Specified by:
takein interfaceBitSet<T>- Specified by:
takein interfaceSet<T>- Specified by:
takein interfaceSortedSet<T>- Specified by:
takein interfaceTraversable<T>- Parameters:
n- The number of elements to take.- Returns:
- A new instance consisting of the first n elements of this or all elements, if this has less than n elements.
-
takeRight
Description copied from interface:TraversableTakes the last n elements of this or all elements, if this length < n.The result is equivalent to
sublist(max(0, min(length(), length() - n)), n), i.e. takeRight will not throw ifn < 0orn > length().In the case of
n < 0the empty instance is returned, in the case ofn > length()this is returned.- Specified by:
takeRightin interfaceBitSet<T>- Specified by:
takeRightin interfaceSet<T>- Specified by:
takeRightin interfaceSortedSet<T>- Specified by:
takeRightin interfaceTraversable<T>- Parameters:
n- The number of elements to take.- Returns:
- A new instance consisting of the last n elements of this or all elements, if this has less than n elements.
-
remove
Description copied from interface:SetRemoves a specific element from this set, if present. -
removeAll
Description copied from interface:SetRemoves all of the given elements from this set, if present. -
toString
-
equals
Description copied from interface:TraversableIn Vavr there are four basic classes of collections:- Seq (sequential elements)
- Set (distinct elements)
- Map (indexed elements)
- Multimap (indexed collections)
- belong to the same basic collection class (Seq, Set, Map or Multimap)
- contain the same elements
- have the same element order, if the collections are of type Seq
Notes:
- No collection instance equals null, e.g. Queue(1) not equals null.
- Nulls are allowed and handled as expected, e.g. List(null, 1) equals Stream(null, 1) and HashMap((null, 1)) equals LinkedHashMap((null, 1)).
- The element order is taken into account for Seq only. E.g. List(null, 1) not equals Stream(1, null) and HashMap((null, 1), ("a", null)) equals LinkedHashMap(("a", null), (null, 1)). The reason is, that we do not know which implementations we compare when having two instances of type Map, Multimap or Set (see Liskov Substitution Principle).
- Other collection classes are equal if their types are equal and their elements are equal (in iteration order).
- Iterator equality is defined to be object reference equality.
-
hashCode
public int hashCode()Description copied from interface:TraversableReturns the hash code of this collection.
We distinguish between two types of hashes, those for collections with predictable iteration order (like Seq) and those with arbitrary iteration order (like Set, Map and Multimap).
In all cases the hash of an empty collection is defined to be 1.
Collections with predictable iteration order are hashed as follows:
Collections with arbitrary iteration order are hashed in a way such that the hash of a fixed number of elements is independent of their iteration order.int hash = 1; for (T t : this) { hash = hash * 31 + Objects.hashCode(t); }
Please note that the particular hashing algorithms may change in a future version of Vavr.int hash = 1; for (T t : this) { hash += Objects.hashCode(t); }
Generally, hash codes of collections aren't cached in Vavr (opposed to the size/length). Storing hash codes in order to reduce the time complexity would increase the memory footprint. Persistent collections are built upon tree structures, it allows us to implement efficient memory sharing. A drawback of tree structures is that they make it necessary to store collection attributes at each tree node (read: element).
The computation of the hash code is linear in time, i.e. O(n). If the hash code of a collection is re-calculated often, e.g. when using a List as HashMap key, we might want to cache the hash code. This can be achieved by simply using a wrapper class, which is not included in Vavr but could be implemented like this:public final class Hashed<K> { private final K key; private final Lazy<Integer> hashCode; public Hashed(K key) { this.key = key; this.hashCode = Lazy.of(() -> Objects.hashCode(key)); } public K key() { return key; } @Override public boolean equals(Object o) { if (o == key) { return true; } else if (key != null && o instanceof Hashed) { final Hashed that = (Hashed) o; return key.equals(that.key); } else { return false; } } @Override public int hashCode() { return hashCode.get(); } @Override public String toString() { return "Hashed(" + (key == null ? "null" : key.toString()) + ")"; } }
-