it.unipi.di.util
Class Basics

java.lang.Object
  extended by it.unipi.di.util.Basics

public class Basics
extends Object

This class offers various basic procedures to operate on byte arrays, ByteBuffers and BitSets at bit level.

Author:
Claudio Corsi, Paolo Ferragina, Alessandro Barilari

Method Summary
static byte[] BitSet2ByteArray(BitSet X)
          Wrap a BitSet into a byte[]
static ByteBuffer BitSet2ByteBuffer(BitSet X)
          Wrap a BitSet into a ByteBuffer
static BitSet byteArray2BitSet(byte[] B)
          Wrap a byte[] into a BitSet
static BitSet ByteBuffer2BitSet(ByteBuffer B)
          Wrap a ByteBuffer into a BitSet
static byte countBitInByte(byte b, byte off, byte end, boolean fast)
          Returns the number of bits set to 1 in a portion of byte.
static int countBitInByteArray(byte[] b, int start, int end)
          Returns the number of bits set to 1 in a portion of byte[].
static int countBitInByteBuffer(ByteBuffer b, int start, int end)
          Returns the number of bits set to 1 in a portion of ByteBuffer.
static void fillCounts()
           
static boolean getBitFromByte(byte x, int i)
          Returns the value of the selected bit of a byte.
static boolean getBitFromByteArray(byte[] A, int i)
          Returns the value of the selected bit of a byte[].
static boolean getBitFromByteBuffer(ByteBuffer A, int i)
          Returns the value of the selected bit of a ByteBuffer.
static boolean isEmptyByteArray(byte[] b, int start, int end)
          Checks is a portion of a byte[] contains bits set to 1
static long loadFromBitset(BitSet P, int i, int b)
          Loads a block of bit from a BitSet.
static int loadFromByteArray(byte[] A, int i, int b)
          Loads a block of bit from a byte[].
static int loadFromByteBuffer(ByteBuffer A, int i, int b)
          Loads a block of bit from a ByteBuffer.
static int loadFromFile(RandomAccessFile F, long i, int b)
          Loads a block of bit from a file.
static int log2(long x)
          Returns the log2 of a number.
static int nextSetBitInByte(byte A, int i)
          Returns the position of the first bit set to 1 in a byte after the specified position.
static int nextSetBitInByteArray(byte[] A, int i)
          Returns the position of the first bit set to 1 in a byte[] after the specified position.
static int nextSetBitInByteBuffer(ByteBuffer A, int i)
          Returns the position of the first bit set to 1 in a ByteBuffer after the specified position.
static byte setBitInByte(byte x, int i, boolean b)
          Set the i-th bit of a byte to the specified value.
static void setBitInByteArray(byte[] A, int i, boolean b)
          Set the i-th bit of a byte array to the specified value.
static void setBitInByteBuffer(ByteBuffer A, int i, boolean b)
          Set the i-th bit of a ByteBuffer to the specified value.
static void storeInBitset(BitSet P, int i, long x, int b)
          Store the b lower bits of x in the BitSet P[i,i+b-1].
static void storeInByteArray(byte[] A, int i, int x, int b)
          Store the b lower bits of x starting from the i-th bit of byte array A.
static void storeInByteBuffer(ByteBuffer A, int i, int x, int b)
          Store the b lower bits of x starting from the i-th bit of ByteBuffer A.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

fillCounts

public static void fillCounts()
                       throws IOException
Throws:
IOException

log2

public static int log2(long x)
                throws IOException
Returns the log2 of a number.

Parameters:
x - is a positive long
Returns:
the log2 of x.
Throws:
IOException

byteArray2BitSet

public static BitSet byteArray2BitSet(byte[] B)
                               throws IOException
Wrap a byte[] into a BitSet

Parameters:
B - is the byte array to be converted
Returns:
the BitSet representation of the (bits in the) byte array B
Throws:
IOException

ByteBuffer2BitSet

public static BitSet ByteBuffer2BitSet(ByteBuffer B)
                                throws IOException
Wrap a ByteBuffer into a BitSet

Parameters:
B - is the byte array to be converted
Returns:
the BitSet representation of the (bits in the) byteBuffer B
Throws:
IOException

BitSet2ByteArray

public static byte[] BitSet2ByteArray(BitSet X)
                               throws IOException
Wrap a BitSet into a byte[]

Parameters:
X - is the BitSet to be converted
Returns:
the byte array representation of BitSet X, with one bit per element of X.
Throws:
IOException

BitSet2ByteBuffer

public static ByteBuffer BitSet2ByteBuffer(BitSet X)
                                    throws IOException
Wrap a BitSet into a ByteBuffer

Parameters:
X - is the BitSet to be converted
Returns:
the ByteBuffer representation of BitSet X, with one bit per element of X.
Throws:
IOException

setBitInByte

public static byte setBitInByte(byte x,
                                int i,
                                boolean b)
                         throws IOException
Set the i-th bit of a byte to the specified value.

Parameters:
x - is a byte
i - is the offset in the byte of the bit to be set, 0 <= i <= 7
b - is the value to set, either true (1) or false (0)
Returns:
the byte x changed by setting the i-th bit to b
Throws:
IOException

setBitInByteArray

public static void setBitInByteArray(byte[] A,
                                     int i,
                                     boolean b)
                              throws IOException
Set the i-th bit of a byte array to the specified value. Offset i is counted from the left and starts with 0.

Parameters:
A - is the byte array whose bit has to be set
i - is the offset of the bit to be set, i is counted from 0
b - is the value to set, either true (1) or false (0)
Throws:
IOException

setBitInByteBuffer

public static void setBitInByteBuffer(ByteBuffer A,
                                      int i,
                                      boolean b)
                               throws IOException
Set the i-th bit of a ByteBuffer to the specified value. Offset i is counted from the left and starts with 0.

Parameters:
A - is the ByteBuffer whose bit has to be set
i - is the offset of the bit to be set, i is counted from 0
b - is the value to set, either true (1) or false (0)
Throws:
BasicException
IOException

getBitFromByte

public static boolean getBitFromByte(byte x,
                                     int i)
                              throws IOException
Returns the value of the selected bit of a byte.

Parameters:
x - is the byte from which the bit is read
i - is the offset of the bit to be read, i is counted from 0
Returns:
the i-th bit of byte x, as a boolean value
Throws:
IOException

getBitFromByteArray

public static boolean getBitFromByteArray(byte[] A,
                                          int i)
                                   throws IOException
Returns the value of the selected bit of a byte[].

Parameters:
A - is the byte array from which the bit is read
i - is the offset of the bit to be read, i is counted from 0
Returns:
the i-th bit of byte array A, as a boolean value
Throws:
IOException

getBitFromByteBuffer

public static boolean getBitFromByteBuffer(ByteBuffer A,
                                           int i)
                                    throws IOException
Returns the value of the selected bit of a ByteBuffer.

Parameters:
A - is the ByteBuffer from which the bit is read
i - is the offset of the bit to be read, i is counted from 0
Returns:
the i-th bit of ByteBuffer A, as a boolean value
Throws:
IOException

nextSetBitInByte

public static int nextSetBitInByte(byte A,
                                   int i)
                            throws IOException
Returns the position of the first bit set to 1 in a byte after the specified position.

Parameters:
A - is a byte
i - is the offset from which the next set bit is searched
Returns:
the position of the next set bit in A after i; -1 if there isn't any set bit.
Throws:
IOException

nextSetBitInByteArray

public static int nextSetBitInByteArray(byte[] A,
                                        int i)
                                 throws IOException
Returns the position of the first bit set to 1 in a byte[] after the specified position.

Parameters:
A - is a byte array
i - is the offset from which the 1 bit is searched
Returns:
the position of the next set bit in A[i,...]; -1 if there isn't any set bit.
Throws:
IOException

nextSetBitInByteBuffer

public static int nextSetBitInByteBuffer(ByteBuffer A,
                                         int i)
                                  throws IOException
Returns the position of the first bit set to 1 in a ByteBuffer after the specified position.

Parameters:
A - is a ByteBuffer
i - is the offset from which the next set bit is searched
Returns:
the position of the next set bit in A[i,...]; -1 if there isn't any set bit.
Throws:
IOException

storeInBitset

public static void storeInBitset(BitSet P,
                                 int i,
                                 long x,
                                 int b)
                          throws IOException
Store the b lower bits of x in the BitSet P[i,i+b-1].

Parameters:
P - is the BitSet to be updated
i - is the bit-offset (>=0) from which the update of P is executed
x - is the integer whose b lower bits are taken to update P
b - is the number of bits to update, 0 < b <= 64
Throws:
IOException

storeInByteArray

public static void storeInByteArray(byte[] A,
                                    int i,
                                    int x,
                                    int b)
                             throws IOException
Store the b lower bits of x starting from the i-th bit of byte array A.

Parameters:
A - is the byte array to be updated
i - is the bit-offset (>= 0) from which the update of A is executed
x - is the integer whose b lower bits are taken to update A
b - is the number of bits to update, 0 < b <= 32
Throws:
IOException

storeInByteBuffer

public static void storeInByteBuffer(ByteBuffer A,
                                     int i,
                                     int x,
                                     int b)
                              throws IOException
Store the b lower bits of x starting from the i-th bit of ByteBuffer A.

Parameters:
A - is the ByteBuffer to be updated
i - is the bit-offset (>= 0) from which the update of A is executed
x - is the integer whose b lower bits are taken to update A
b - is the number of bits to update, 0 < b <= 32.
Throws:
IOException

loadFromFile

public static int loadFromFile(RandomAccessFile F,
                               long i,
                               int b)
                        throws IOException
Loads a block of bit from a file.

Parameters:
F - is a RandomAccessFile
i - is the bit-offset (>= 0) in A from which the reading is executed
b - is the number of bits to read, 0 < b <= 32.
Returns:
b bits starting from the i-th bit position in the file F
Throws:
IOException

loadFromByteArray

public static int loadFromByteArray(byte[] A,
                                    int i,
                                    int b)
                             throws IOException
Loads a block of bit from a byte[].

Parameters:
A - is the byte array to be read
i - is the bit-offset (>= 0) in A from which the reading is executed
b - is the number of bits to read, 0 < b <= 32
Returns:
b bits starting from the i-th bit position of A
Throws:
IOException

loadFromByteBuffer

public static int loadFromByteBuffer(ByteBuffer A,
                                     int i,
                                     int b)
                              throws IOException
Loads a block of bit from a ByteBuffer.

Parameters:
A - is the ByteBuffer to be read
i - is the bit-offset (>= 0) in A from which the reading is executed
b - is the number of bits to read, 0 < b <= 32
Returns:
b bits starting from the i-th bit position of A
Throws:
IOException

loadFromBitset

public static long loadFromBitset(BitSet P,
                                  int i,
                                  int b)
                           throws IOException
Loads a block of bit from a BitSet.

Parameters:
P - is the BitSet to be read
i - is the bit-offset (>= 0) in P from which the reading is executed
b - is the number of bits to read, 0 < b <= 64. b bits in the BitSet P[i,i+b-1]
Throws:
IOException

countBitInByte

public static byte countBitInByte(byte b,
                                  byte off,
                                  byte end,
                                  boolean fast)
                           throws IOException
Returns the number of bits set to 1 in a portion of byte.

Parameters:
b - the byte to scan
off - the bit to start from
end - the last bit to count
fast - if true use fast count options (to use this option you will need to call the fillCounts method before)
Returns:
the number of set bits in the Byte b, starting from the off-th bit
Throws:
IOException

countBitInByteArray

public static int countBitInByteArray(byte[] b,
                                      int start,
                                      int end)
                               throws IOException
Returns the number of bits set to 1 in a portion of byte[].

Parameters:
b - the byte[] to scan
start - the byte to start from
Returns:
the number of set bits in the byte[] b in range [start, end]
Throws:
IOException

countBitInByteBuffer

public static int countBitInByteBuffer(ByteBuffer b,
                                       int start,
                                       int end)
                                throws IOException
Returns the number of bits set to 1 in a portion of ByteBuffer.

Parameters:
b - the ByteBuffer to scan
start - the byte to start from
end - the byte to stop at
Returns:
the number of set bits in the ByteBuffer b in range [start, end]
Throws:
IOException

isEmptyByteArray

public static boolean isEmptyByteArray(byte[] b,
                                       int start,
                                       int end)
                                throws IOException
Checks is a portion of a byte[] contains bits set to 1

Parameters:
b - the byte[] to scan
start - the byte to start from
end - the byte to stop at
Returns:
true if there aren't bits set to 1, false otherwise
Throws:
IOException