it.unimi.dsi.bits
Class AbstractBitVector

java.lang.Object
  extended by it.unimi.dsi.fastutil.booleans.AbstractBooleanCollection
      extended by it.unimi.dsi.fastutil.booleans.AbstractBooleanList
          extended by it.unimi.dsi.bits.AbstractBitVector
All Implemented Interfaces:
BitVector, BooleanCollection, BooleanIterable, BooleanList, BooleanStack, Stack<java.lang.Boolean>, java.lang.Comparable<java.util.List<? extends java.lang.Boolean>>, java.lang.Iterable<java.lang.Boolean>, java.util.Collection<java.lang.Boolean>, java.util.List<java.lang.Boolean>, java.util.RandomAccess
Direct Known Subclasses:
AbstractBitVector.SubBitVector, BooleanListBitVector, LongArrayBitVector

public abstract class AbstractBitVector
extends AbstractBooleanList
implements BitVector

An abstract implementation of a BitVector.

This abstract implementation provides almost all methods: you have to provide just BooleanList.getBoolean(int) and List.size(). No attributes are defined.

Note that the integer-set view provided by asLongSet() is not cached: if you want to cache the result of the first call, you must do your own caching.

Warning: this class has several optimised methods that assume that getLong(long, long) is implemented efficiently when its arguments are multiples of Long.SIZE (see, e.g., the implementation of compareTo(BitVector) and longestCommonPrefixLength(BitVector)). If you want speed up the processing of your own BitVector implementations, just implement getLong(long, long) so that it is fast under the above conditions.


Nested Class Summary
static class AbstractBitVector.LongBigListView
          A list-of-integers view of a bit vector.
static class AbstractBitVector.LongSetView
          An integer sorted set view of a bit vector.
static class AbstractBitVector.SubBitVector
          A subvector of a given bit vector, specified by an initial and a final bit.
 
Nested classes/interfaces inherited from class it.unimi.dsi.fastutil.booleans.AbstractBooleanList
AbstractBooleanList.BooleanSubList
 
Constructor Summary
AbstractBitVector()
           
 
Method Summary
 boolean add(boolean value)
           
 void add(int value)
          Adds a bit with specified value at the end of this bit vector.
 void add(int index, boolean value)
           
 void add(long index, boolean value)
          Adds a bit with specified value at the specified index (optional operation).
 void add(long index, int value)
          Adds a bit with specified integer value at the specified index (optional operation).
 BitVector and(BitVector v)
          Performs a logical and between this bit vector and another one, leaving the result in this vector.
 BitVector append(BitVector bv)
          Appends another bit vector to this bit vector.
 BitVector append(long value, int k)
          Appends the less significant bits of a long integer to this bit vector.
 LongBigList asLongBigList(int width)
          Returns a view of this bit vector as a list of nonnegative integers of specified width.
 LongSortedSet asLongSet()
          Returns a view of this bit vector as a sorted set of long integers.
 long[] bits()
          Returns the bits in this bit vector as an array of longs, not to be modified.
 void clear()
           
 void clear(int index)
           
 void clear(long index)
          Clears a bit in this bit vector (optional operation).
 int compareTo(BitVector v)
           
 int compareTo(java.util.List<? extends java.lang.Boolean> list)
           
 BitVector copy()
          Returns a copy of this bit vector.
 BitVector copy(long from, long to)
          Returns a copy of a part of this bit vector.
 long count()
          Counts the number of bits set to true in this bit vector.
protected  void ensureIndex(long index)
           
protected  void ensureRestrictedIndex(long index)
           
 boolean equals(java.lang.Object o)
           
 BitVector fast()
          Returns an instance of LongArrayBitVector containing a copy of this bit vector.
 void fill(boolean value)
          Sets all bits this bit vector to the given boolean value (optional operation).
 void fill(int value)
          Sets all bits this bit vector to the given integer value (optional operation).
 void fill(long from, long to, boolean value)
          Fills a range of bits in this bit vector (optional operation).
 void fill(long from, long to, int value)
          Clears a range of bits in this bit vector (optional operation).
 long firstOne()
          Returns the position of the first bit set in this vector.
 long firstZero()
          Returns the position of the first bit unset in this vector.
 void flip()
          Flips all bits in this bit vector (optional operation).
 void flip(int index)
           
 void flip(long index)
          Flips a bit in this bit vector (optional operation).
 void flip(long from, long to)
          Flips a range of bits in this bit vector (optional operation).
 boolean getBoolean(int index)
           
 int getInt(long index)
          Returns the value of the specified bit as an integer.
 long getLong(long from, long to)
          Returns the specified bit range as a long.
 int hashCode()
           
 long lastOne()
          Returns the position of the last bit set in this vector.
 long lastZero()
          Returns the position of the last bit unset in this vector.
 BitVector length(long newLength)
          Sets the number of bits in this bit vector.
 long longestCommonPrefixLength(BitVector v)
          Returns the length of the greatest common prefix between this and the specified vector.
 long nextOne(long index)
          Returns the position of the first bit set after the given position.
 long nextZero(long index)
          Returns the position of the first bit unset after the given position.
 BitVector or(BitVector v)
          Performs a logical or between this bit vector and another one, leaving the result in this vector.
 long previousOne(long index)
          Returns the position of the first bit set before or at the given position.
 long previousZero(long index)
          Returns the position of the first bit unset before or at the given position.
 boolean removeBoolean(int index)
           
 boolean removeBoolean(long index)
          Removes a bit with specified index (optional operation).
 BitVector replace(BitVector bv)
          Replaces the content of this bit vector with another bit vector.
 void set(int index)
           
 boolean set(int index, boolean value)
           
 void set(long index)
          Sets a bit in this bit vector (optional operation).
 boolean set(long index, boolean value)
          Sets the value of the specified bit (optional operation).
 void set(long index, int value)
          Sets the value of the specified bit as an integer (optional operation).
 int size()
           
 void size(int newSize)
           
 AbstractBitVector.SubBitVector subList(int from, int to)
           
 BitVector subVector(long from)
          Returns a subvector view specified by initial index and running up to the end of this vector.
 BitVector subVector(long from, long to)
          Returns a subvector view specified by initial and final index.
 java.lang.String toString()
          Returns a string representation of this vector.
 BitVector xor(BitVector v)
          Performs a logical xor between this bit vector and another one, leaving the result in this vector.
 
Methods inherited from class it.unimi.dsi.fastutil.booleans.AbstractBooleanList
add, addAll, addAll, addAll, addAll, addAll, addAll, addElements, addElements, booleanListIterator, booleanListIterator, booleanSubList, contains, ensureIndex, ensureRestrictedIndex, get, getElements, indexOf, indexOf, iterator, lastIndexOf, lastIndexOf, listIterator, listIterator, peek, peekBoolean, pop, popBoolean, push, push, rem, remove, remove, removeElements, set, top, topBoolean
 
Methods inherited from class it.unimi.dsi.fastutil.booleans.AbstractBooleanCollection
add, booleanIterator, contains, containsAll, containsAll, isEmpty, rem, removeAll, removeAll, retainAll, retainAll, toArray, toArray, toArray, toBooleanArray, toBooleanArray
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface it.unimi.dsi.bits.BitVector
getBoolean, length
 
Methods inherited from interface it.unimi.dsi.fastutil.booleans.BooleanList
addAll, addAll, addAll, addElements, addElements, booleanListIterator, booleanListIterator, booleanSubList, getElements, indexOf, iterator, lastIndexOf, listIterator, listIterator, removeElements
 
Methods inherited from interface java.util.List
add, add, addAll, addAll, contains, containsAll, get, indexOf, isEmpty, lastIndexOf, remove, remove, removeAll, retainAll, set, toArray, toArray
 
Methods inherited from interface it.unimi.dsi.fastutil.booleans.BooleanCollection
addAll, booleanIterator, contains, containsAll, rem, removeAll, retainAll, toArray, toArray, toBooleanArray, toBooleanArray
 
Methods inherited from interface it.unimi.dsi.fastutil.Stack
isEmpty
 

Constructor Detail

AbstractBitVector

public AbstractBitVector()
Method Detail

ensureRestrictedIndex

protected void ensureRestrictedIndex(long index)

ensureIndex

protected void ensureIndex(long index)

set

public void set(int index)

clear

public void clear(int index)

flip

public void flip(int index)

set

public void set(long index)
Description copied from interface: BitVector
Sets a bit in this bit vector (optional operation).

Specified by:
set in interface BitVector
Parameters:
index - the index of a bit.

clear

public void clear(long index)
Description copied from interface: BitVector
Clears a bit in this bit vector (optional operation).

Specified by:
clear in interface BitVector
Parameters:
index - the index of a bit.

flip

public void flip(long index)
Description copied from interface: BitVector
Flips a bit in this bit vector (optional operation).

Specified by:
flip in interface BitVector
Parameters:
index - the index of a bit.

fill

public void fill(boolean value)
Description copied from interface: BitVector
Sets all bits this bit vector to the given boolean value (optional operation).

Specified by:
fill in interface BitVector

fill

public void fill(int value)
Description copied from interface: BitVector
Sets all bits this bit vector to the given integer value (optional operation).

Specified by:
fill in interface BitVector

flip

public void flip()
Description copied from interface: BitVector
Flips all bits in this bit vector (optional operation).

Specified by:
flip in interface BitVector

fill

public void fill(long from,
                 long to,
                 boolean value)
Description copied from interface: BitVector
Fills a range of bits in this bit vector (optional operation).

Specified by:
fill in interface BitVector
Parameters:
from - the first index (inclusive).
to - the last index (not inclusive).
value - the value (true or false).

fill

public void fill(long from,
                 long to,
                 int value)
Description copied from interface: BitVector
Clears a range of bits in this bit vector (optional operation).

Specified by:
fill in interface BitVector
Parameters:
from - the first index (inclusive).
to - the last index (not inclusive).
value - the value (zero or nonzero).

flip

public void flip(long from,
                 long to)
Description copied from interface: BitVector
Flips a range of bits in this bit vector (optional operation).

Specified by:
flip in interface BitVector
Parameters:
from - the first index (inclusive).
to - the last index (not inclusive).

getInt

public int getInt(long index)
Description copied from interface: BitVector
Returns the value of the specified bit as an integer.

This method is a useful synonym for BitVector.getBoolean(long).

Specified by:
getInt in interface BitVector
Parameters:
index - the index of a bit.
Returns:
the value of the specified bit as an integer (0 or 1).

getLong

public long getLong(long from,
                    long to)
Description copied from interface: BitVector
Returns the specified bit range as a long.

Note that bit 0 of the returned long will be bit from of this bit vector.

Implementations are invited to provide high-speed implementations for the case in which from is a multiple of Long.SIZE and to is from + Long.SIZE (or less, in case the vector length is exceeded). This behaviour make it possible to implement high-speed hashing, copies, etc.

Specified by:
getLong in interface BitVector
Parameters:
from - the starting bit (inclusive).
to - the ending bit (exclusive).
Returns:
the long value contained in the specified bits.

getBoolean

public boolean getBoolean(int index)
Specified by:
getBoolean in interface BooleanList

removeBoolean

public boolean removeBoolean(int index)
Specified by:
removeBoolean in interface BooleanList
Overrides:
removeBoolean in class AbstractBooleanList

set

public boolean set(int index,
                   boolean value)
Specified by:
set in interface BooleanList
Overrides:
set in class AbstractBooleanList

add

public void add(int index,
                boolean value)
Specified by:
add in interface BooleanList
Overrides:
add in class AbstractBooleanList

removeBoolean

public boolean removeBoolean(long index)
Description copied from interface: BitVector
Removes a bit with specified index (optional operation).

This method is semantically equivalent to BooleanList.removeBoolean(int), but it gives access to long indices.

Specified by:
removeBoolean in interface BitVector
Parameters:
index - the index of a bit.
Returns:
the previous value of the bit.

set

public boolean set(long index,
                   boolean value)
Description copied from interface: BitVector
Sets the value of the specified bit (optional operation).

This method is semantically equivalent to BooleanList.set(int,boolean), but it gives access to long indices.

Specified by:
set in interface BitVector
Parameters:
index - the index of a bit.
value - the new value.

add

public void add(long index,
                boolean value)
Description copied from interface: BitVector
Adds a bit with specified value at the specified index (optional operation).

This method is semantically equivalent to BooleanList.add(int,boolean), but it gives access to long indices.

Specified by:
add in interface BitVector
Parameters:
index - the index of a bit.
value - the value that will be inserted at position index.

set

public void set(long index,
                int value)
Description copied from interface: BitVector
Sets the value of the specified bit as an integer (optional operation).

This method is a useful synonym for BitVector.set(long, boolean).

Specified by:
set in interface BitVector
Parameters:
index - the index of a bit.
value - the new value (any nonzero integer for setting the bit, zero for clearing the bit).

add

public void add(long index,
                int value)
Description copied from interface: BitVector
Adds a bit with specified integer value at the specified index (optional operation).

This method is a useful synonym for BitVector.add(long, boolean).

Specified by:
add in interface BitVector
Parameters:
index - the index of a bit.
value - the value that will be inserted at position index (any nonzero integer for a true bit, zero for a false bit).

add

public boolean add(boolean value)
Specified by:
add in interface BooleanCollection
Overrides:
add in class AbstractBooleanList

add

public void add(int value)
Description copied from interface: BitVector
Adds a bit with specified value at the end of this bit vector.

This method is a useful synonym for BooleanCollection.add(boolean).

Specified by:
add in interface BitVector
Parameters:
value - the new value (any nonzero integer for a true bit, zero for a false bit).

append

public BitVector append(long value,
                        int k)
Description copied from interface: BitVector
Appends the less significant bits of a long integer to this bit vector.

Specified by:
append in interface BitVector
Parameters:
value - a value to be appended
k - the number of less significant bits to be added to this bit vector.
Returns:
this bit vector.

append

public BitVector append(BitVector bv)
Description copied from interface: BitVector
Appends another bit vector to this bit vector.

Specified by:
append in interface BitVector
Parameters:
bv - a bit vector to be appended.
Returns:
this bit vector.

copy

public BitVector copy()
Description copied from interface: BitVector
Returns a copy of this bit vector.

Specified by:
copy in interface BitVector
Returns:
a copy of this bit vector.

copy

public BitVector copy(long from,
                      long to)
Description copied from interface: BitVector
Returns a copy of a part of this bit vector.

Specified by:
copy in interface BitVector
Parameters:
from - the starting bit, inclusive.
to - the ending bit, not inclusive.
Returns:
a copy of the part of this bit vector going from bit from (inclusive) to bit to (not inclusive)

fast

public BitVector fast()
Returns an instance of LongArrayBitVector containing a copy of this bit vector.

Specified by:
fast in interface BitVector
Returns:
an instance of LongArrayBitVector containing a copy of this bit vector.

count

public long count()
Description copied from interface: BitVector
Counts the number of bits set to true in this bit vector.

Specified by:
count in interface BitVector
Returns:
the number of bits set to true in this bit vector.

firstOne

public long firstOne()
Description copied from interface: BitVector
Returns the position of the first bit set in this vector.

Specified by:
firstOne in interface BitVector
Returns:
the first bit set, or -1 for a vector of zeroes.

lastOne

public long lastOne()
Description copied from interface: BitVector
Returns the position of the last bit set in this vector.

Specified by:
lastOne in interface BitVector
Returns:
the last bit set, or -1 for a vector of zeroes.

firstZero

public long firstZero()
Description copied from interface: BitVector
Returns the position of the first bit unset in this vector.

Specified by:
firstZero in interface BitVector
Returns:
the first bit unset, or -1 for a vector of ones.

lastZero

public long lastZero()
Description copied from interface: BitVector
Returns the position of the last bit unset in this vector.

Specified by:
lastZero in interface BitVector
Returns:
the last bit unset, or -1 for a vector of ones.

nextOne

public long nextOne(long index)
Description copied from interface: BitVector
Returns the position of the first bit set after the given position.

Specified by:
nextOne in interface BitVector
Returns:
the first bit set after position index (inclusive), or -1 if no such bit exists.

previousOne

public long previousOne(long index)
Description copied from interface: BitVector
Returns the position of the first bit set before or at the given position.

Specified by:
previousOne in interface BitVector
Returns:
the first bit set before or at the given position, or -1 if no such bit exists.

nextZero

public long nextZero(long index)
Description copied from interface: BitVector
Returns the position of the first bit unset after the given position.

Specified by:
nextZero in interface BitVector
Returns:
the first bit unset after position index (inclusive), or -1 if no such bit exists.

previousZero

public long previousZero(long index)
Description copied from interface: BitVector
Returns the position of the first bit unset before or at the given position.

Specified by:
previousZero in interface BitVector
Returns:
the first bit unset before or at the given position, or -1 if no such bit exists.

longestCommonPrefixLength

public long longestCommonPrefixLength(BitVector v)
Description copied from interface: BitVector
Returns the length of the greatest common prefix between this and the specified vector.

Specified by:
longestCommonPrefixLength in interface BitVector
Parameters:
v - a bit vector.
Returns:
the length of the greatest common prefix.

and

public BitVector and(BitVector v)
Description copied from interface: BitVector
Performs a logical and between this bit vector and another one, leaving the result in this vector.

Specified by:
and in interface BitVector
Parameters:
v - a bit vector.
Returns:
this bit vector.

or

public BitVector or(BitVector v)
Description copied from interface: BitVector
Performs a logical or between this bit vector and another one, leaving the result in this vector.

Specified by:
or in interface BitVector
Parameters:
v - a bit vector.
Returns:
this bit vector.

xor

public BitVector xor(BitVector v)
Description copied from interface: BitVector
Performs a logical xor between this bit vector and another one, leaving the result in this vector.

Specified by:
xor in interface BitVector
Parameters:
v - a bit vector.
Returns:
this bit vector.

size

public int size()
Specified by:
size in interface java.util.Collection<java.lang.Boolean>
Specified by:
size in interface java.util.List<java.lang.Boolean>

size

public void size(int newSize)
Specified by:
size in interface BooleanList
Overrides:
size in class AbstractBooleanList

clear

public void clear()
Specified by:
clear in interface java.util.Collection<java.lang.Boolean>
Specified by:
clear in interface java.util.List<java.lang.Boolean>
Overrides:
clear in class AbstractBooleanCollection

replace

public BitVector replace(BitVector bv)
Description copied from interface: BitVector
Replaces the content of this bit vector with another bit vector.

Specified by:
replace in interface BitVector
Parameters:
bv - a bit vector.
Returns:
this bit vector.

equals

public boolean equals(java.lang.Object o)
Specified by:
equals in interface java.util.Collection<java.lang.Boolean>
Specified by:
equals in interface java.util.List<java.lang.Boolean>
Overrides:
equals in class AbstractBooleanList

hashCode

public int hashCode()
Specified by:
hashCode in interface java.util.Collection<java.lang.Boolean>
Specified by:
hashCode in interface java.util.List<java.lang.Boolean>
Overrides:
hashCode in class AbstractBooleanList

bits

public long[] bits()
Description copied from interface: BitVector
Returns the bits in this bit vector as an array of longs, not to be modified.

Specified by:
bits in interface BitVector
Returns:
an array of longs whose first BitVector.length() bits contain the bits of this bit vector. The array cannot be modified.

length

public BitVector length(long newLength)
Description copied from interface: BitVector
Sets the number of bits in this bit vector.

It is expected that this method will try to allocate exactly the necessary space.

If the number of bits in this vector is smaller than or equal to Integer.MAX_VALUE, this method is semantically equivalent to BooleanList.size(int).

Specified by:
length in interface BitVector
Returns:
this bit vector.

asLongSet

public LongSortedSet asLongSet()
Description copied from interface: BitVector
Returns a view of this bit vector as a sorted set of long integers.

More formally, this bit vector is infinitely extended to the left with zeros (e.g., all bits beyond BitVector.length(long) are considered zeroes). The resulting infinite string is interpreted as the characteristic function of a set of integers.

Note that, in particular, the resulting string representation is exactly that of a BitSet.

Specified by:
asLongSet in interface BitVector

asLongBigList

public LongBigList asLongBigList(int width)
Description copied from interface: BitVector
Returns a view of this bit vector as a list of nonnegative integers of specified width.

More formally, getLong(p) will return the nonnegative integer defined by the bits starting at p * width (bit 0, inclusive) and ending at (p + 1) * width (bit width − 1, exclusive).

Specified by:
asLongBigList in interface BitVector

subList

public AbstractBitVector.SubBitVector subList(int from,
                                              int to)
Specified by:
subList in interface BooleanList
Specified by:
subList in interface java.util.List<java.lang.Boolean>
Overrides:
subList in class AbstractBooleanList

subVector

public BitVector subVector(long from,
                           long to)
Description copied from interface: BitVector
Returns a subvector view specified by initial and final index.

The object returned by this method is a bit vector representing a view of this bit vector restricted to the given indices. Changes to the subvector will be reflected in the main vector.

Specified by:
subVector in interface BitVector
Parameters:
from - the first index (inclusive).
to - the last index (not inclusive).

subVector

public BitVector subVector(long from)
Description copied from interface: BitVector
Returns a subvector view specified by initial index and running up to the end of this vector.

Specified by:
subVector in interface BitVector
Parameters:
from - the first index (inclusive).
See Also:
BitVector.subVector(long, long)

compareTo

public int compareTo(java.util.List<? extends java.lang.Boolean> list)
Specified by:
compareTo in interface java.lang.Comparable<java.util.List<? extends java.lang.Boolean>>
Overrides:
compareTo in class AbstractBooleanList

compareTo

public int compareTo(BitVector v)

toString

public java.lang.String toString()
Returns a string representation of this vector.

Note that this string representation shows the bit of index 0 at the leftmost position.

Overrides:
toString in class AbstractBooleanList
Returns:
a string representation of this vector, with the bit of index 0 on the left.