Package org.basex.util
Class Num
java.lang.Object
org.basex.util.Num
This class provides operations to compress and decompress 4-byte integer values in byte arrays in order to save memory.
The first two bits of a Num array indicate the range of the
compressed number:
00: the number (0x00-0x3F) is encoded in the remaining 6 bits of the current byte01: the number (0x40-0x3FFF) is encoded in 14 bits of the current and the following byte10: the number (0x4000-0x3FFFFFFF) is encoded in 30 bits of the current and the following three bytes11: the number (0x40000000-0xFFFFFFFF) is encoded in 32 bits of the following four bytes
- Author:
- BaseX Team 2005-21, BSD License, Christian Gruen
-
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]add(byte[] array, int value) Compresses and adds a value to the specified array and returns the resulting array.static intget(byte[] array, int pos) Decompresses and returns a value from the specified byte array.static intlength(byte[] array, int pos) Returns the compressed length of the value at the specified position.static intlength(int v) Returns the compressed length of the specified value.static byte[]newNum(int value) Creates a new number array, in which the first four bytes contain the number of occupied bytes.static byte[]num(int value) Creates a compressed representation of the specified value.static intset(byte[] array, int value) Compresses and stores an integer value at the beginning of the byte array.static intsize(byte[] array) Returns the length value of the specified array, stored in the first four bytes.static voidsize(byte[] array, int length) Stores the specified length value in the first bytes of the specified array.static StringtoString(byte[] num) Returns a string representation of the specified number array.
-
Method Details
-
newNum
public static byte[] newNum(int value) Creates a new number array, in which the first four bytes contain the number of occupied bytes.- Parameters:
value- initial value to be compressed and stored- Returns:
- new number array
-
num
public static byte[] num(int value) Creates a compressed representation of the specified value.- Parameters:
value- value to be compressed- Returns:
- new number array
-
add
public static byte[] add(byte[] array, int value) Compresses and adds a value to the specified array and returns the resulting array.- Parameters:
array- input arrayvalue- value to be added- Returns:
- resulting array (may be the same as input array)
-
get
public static int get(byte[] array, int pos) Decompresses and returns a value from the specified byte array.- Parameters:
array- arraypos- position where the value is found- Returns:
- decompressed value
-
set
public static int set(byte[] array, int value) Compresses and stores an integer value at the beginning of the byte array.- Parameters:
array- arrayvalue- value to be stored- Returns:
- length
-
size
public static int size(byte[] array) Returns the length value of the specified array, stored in the first four bytes.- Parameters:
array- input array- Returns:
- array length
-
size
public static void size(byte[] array, int length) Stores the specified length value in the first bytes of the specified array.- Parameters:
array- input arraylength- length to be stored
-
length
public static int length(byte[] array, int pos) Returns the compressed length of the value at the specified position.- Parameters:
array- arraypos- position where the value is found- Returns:
- value length
-
length
public static int length(int v) Returns the compressed length of the specified value.- Parameters:
v- integer value- Returns:
- value length
-
toString
Returns a string representation of the specified number array.- Parameters:
num- numbers- Returns:
- string
-