|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectutil.PackedArray
public class PackedArray
This is the implementation of a simple packing algorithm,
to reduce the size of an integer array with a fixed number of positive elements and
where the highest value is known.
This implementation allows further extraction and injection of values,
with out unpacking and packing the howl array again.
The implementation is based on three simple bit twiddling operations
on integer values: PackedArray.ld, PackedArray.extract and PackedArray.inject.
The idea of this algorithm is that if the largest integer is known,
we can represent every element by a fixed number of bits
(hopefully smaller then the actual size of the integer).
Assume that the number of bits is three and we have a array of 20
elements, we can store the whole array in an array consisting of only two integer values.
Each such integer value contains 10 sections (each three bits long).
So every element of the original array can be stored in one of the 20 sections.
With that we have reduced the size of a 80 Byte array to 8 Byte.
Because we have sliced each integer in a fixed size of peaces it is easy,
to access each value in constant time, without unpacking the array.
Note: This implementation can is only applicable if the following constraints hold:
Constructor Summary | |
---|---|
PackedArray(int[] array,
int high)
Creates a new PackedArray by packing the given integer array to a from where at least values of the given high can be stored. |
|
PackedArray(PackedArray packedArray)
Creates a new copy of the given packed array. |
Method Summary | |
---|---|
static int |
extract(int source,
int index,
int bits)
This method extracts a section of an integer, by slicing the integer into equal parts of the given size and extracting the part with the given index. |
int |
get(int index)
Returns the element at the specified position in the packed array. |
static int |
inject(int source,
int index,
int bits,
int value)
This method injects the given value into a section of the given integer. |
static int |
ld(int value)
Computes the logarithm to the base 2 for the given integer value. |
int |
length()
Returns the number of elements in this array. |
static void |
main(java.lang.String[] args)
Method to test the implementation of the packed array. |
int[] |
packed()
Returns the internal representation of the integer array. |
void |
set(int index,
int element)
Replaces the element at the specified position in this list with the specified element. |
int[] |
unpack()
Unpacks the internal representation to a full integer array where every value is stored in every element. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public PackedArray(PackedArray packedArray)
packedArray
- to copy frompublic PackedArray(int[] array, int high)
array
- to be represented by a packed arrayhigh
- the highest value of possible elements in of this arrayMethod Detail |
---|
public static int ld(int value)
value
- for the logarithm
public static int extract(int source, int index, int bits)
PackedArray.extract(0x789ABCDE, 3, 4)
return 0x0000009
as a result.
source
- from which a part has to be extractedindex
- of the section holding the result (starting from zero)bits
- the size of each section
public static int inject(int source, int index, int bits, int value)
PackedArray.extract(0x789ABCDE, 3, 4, 0x00000005)
return 0x785ABCDE
as a result.
source
- where the value should be injectedindex
- of the section where the value should be placed (starting from zero)bits
- the size of each sectionvalue
- to inject into the source
java.lang.IllegalArgumentException
- if the value fits not in a sectionpublic int get(int index)
index
- of the element to return
java.lang.ArrayIndexOutOfBoundsException
- if the index is out of range (index < 0 || index >= length())public void set(int index, int element)
index
- index of the element to replaceelement
- to be stored at the specified position
java.lang.ArrayIndexOutOfBoundsException
- if the index is smaller zero or greater then index of the last elementpublic int[] unpack()
public int length()
public static void main(java.lang.String[] args)
args
- no arguments are processed during executionpublic int[] packed()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |