Class RawGrowableArray
The memory format contains a header is as follows:
Byte Meaning -------------------------------------------- 0..3 Pointer to the growable block. Null if the number of records <= inlineRecordCount 4..7 Record [0] 8..11 Record [1] ... k...k+4 Record [inlineRecordCount-1]As shown above, the first few records are stored inline with the array. inlineRecordCount is a tunable parameter which may be 0. If there are fewer than inlineRecordCount records, there is no growable block and all records are stored in the header. Storing the first few records in the header is intended as an optimization for very small arrays in the case where small arrays are expected to be a common case. If there are fewer than inlineRecordCount records stored in the array, the size of the array is not stored explicitly. It is computed on demand by searching for the first null entry among the inline records.
The memory format for a growable block is as follows:
Byte Meaning
--------------------------------------------
0..3 Size of the array, including all inline records. This is also the index at which the next entry will
be inserted
4..7 Capacity of this growable block.
8..11 Record [n]
12..15 Record [n+1]
...
k...k+4 Record [blockSize-1]
The growable block itself begins with a 4-byte int holding the size of the array, followed by a 4-byte int holding
the capacity of the growable block. In the event that the array is larger than
RawGrowableArray.GrowableBlockHeader.MAX_GROWABLE_SIZE enough to be using a metablock, there will be multiple growable blocks
in use. In this case, the size and capacity stored in the metablock is used for the array and the size and capacity
stored in each growable block will be filled in with 0s.
If capacity <= MAX_BLOCK_SIZE then this is a normal block containing a flat array of record pointers starting from the element numbered inlineRecordCount. If capacity > MAX_BLOCK_SIZE then then it is a metablock which holds record pointers to separate growable blocks, each of which holds exactly MAX_BLOCK_SIZE elements.
Every time an element is inserted in the array, the add method returns the element's index. Indices can be used to remove elements in constant time, but will be reassigned by the RawGrowableArray during removes by swapping the removed element with the last element in the array. If the owner of the array is keeping track of indices, it should update the relevant indices on remove.
The array itself is tightly packed. When an element is removed, the last element in the array is swapped into its location. Anyone keeping track of indices may rely on the fact that they are consecutive integers.
These arrays preserve insertion order until the first call to "remove". If element order matters, you should not remove individual elements but should instead destroy and rebuild the entire array.
Element additions and removals run in constant amortized time.
There are a lot of ints and longs used in the implementation of this class. In order to help clarify their function, they get the following suffixes:
- index - holds an index into the array
- size - holds a count of the number of indices
- value - holds one of the pointer values inserted into the array
- address - holds a pointer into the database (refers to a full 8-byte long, not the compressed 4-byte version)
- bytes - holds the size (in bytes) of something in the database
- block - holds a block number (in the case where a metablock is in use, the growable blocks are identified by block numbers).
- blockCount - holds a number of blocks
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintAdds the given value to the array.voidvoidensureCapacity(Nd nd, long address, int desiredSize)Ensures that the array contains at least enough space allocated to fit the given number of new elements.longReturns the element at the given index (nonzero).intgetCapacity(Nd nd, long address)static intintReturns the record size for a RawGrowableSize with the given number of inline recordsbooleanReturns true iff the size of the array is 0longRemoves an entry from the array, given an element index.intReturns the size of the array.
-
Constructor Details
-
RawGrowableArray
public RawGrowableArray(int inlineRecords)
-
-
Method Details
-
getMaxGrowableBlockSize
public static int getMaxGrowableBlockSize() -
size
Returns the size of the array.- Parameters:
address- address of the array- Returns:
- the array size, in number elements
-
add
Adds the given value to the array. Returns an index which can be later passed into remove in order to remove the element at a later time. -
get
Returns the element at the given index (nonzero). The given index must be < size(). -
ensureCapacity
Ensures that the array contains at least enough space allocated to fit the given number of new elements. -
remove
Removes an entry from the array, given an element index. If the given index is not the last element in the list, the last element will have its index swapped with the removed element. If another element was swapped into the position of the removed element, this returns the value of that element. Otherwise, it returns 0. -
getRecordSize
public int getRecordSize()Returns the record size for a RawGrowableSize with the given number of inline records -
destruct
-
isEmpty
Returns true iff the size of the array is 0- Parameters:
address- address of the array- Returns:
- the array size, in number elements
-
getCapacity
-