Package org.snpsift.annotate.mem.arrays
Class StringArray
java.lang.Object
org.snpsift.annotate.mem.arrays.StringArrayBase
org.snpsift.annotate.mem.arrays.StringArray
- All Implemented Interfaces:
Serializable,Iterable<String>
The StringArray class implements a memory-efficient array of strings.
It stores strings as bytes in a byte array, using UTF-8 encoding, and uses a single byte to mark the end of a string ('\0').
This class provides methods to calculate the size of strings in memory, create StringArray instances from arrays or collections of strings,
and retrieve or set strings at specific indices. The internal data structure uses a byte array to store the string data and an integer array
to map array indices to offsets in the byte array.
Limitations:
- The class does not support dynamic resizing of the internal byte array, so the initial size must be sufficient to store all strings.
- Once a string is set at a specific index, it cannot be changed or removed.
- The class is not thread-safe and should be used with caution in concurrent environments.
- Adding strings beyond the allocated memory will result in a runtime exception.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected byte[]protected int[]static final intprotected intFields inherited from class org.snpsift.annotate.mem.arrays.StringArrayBase
currentIndex -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionget(int i) Get the string at array index 'i'intstatic StringArrayCreate a StringArray from an array of stringsstatic StringArrayof(Collection<String> strings) Create a StringArray from a collection of stringsintAdd a string to the array WARNING: Typically you use either 'add' or 'set', but not bothintsize()Size of the data array (capacity of number of bytes)longMemory size of this object (approximate size in bytes)static intCalculate the size of a string in memorystatic intstatic intsizeOf(Collection<String> strings) toString()Methods inherited from class org.snpsift.annotate.mem.arrays.StringArrayBase
add, iterator, lengthMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
MAX_NUM_STRING_TO_SHOW
public static final int MAX_NUM_STRING_TO_SHOW- See Also:
-
data
protected byte[] data -
index2offset
protected int[] index2offset -
offset
protected int offset
-
-
Constructor Details
-
StringArray
public StringArray(int numElements, int size) Constructor- Parameters:
numElements- : Number of elements in the arraysize- : Initial size of the array
-
-
Method Details
-
sizeOf
Calculate the size of a string in memory -
sizeOf
-
sizeOf
-
of
Create a StringArray from an array of strings -
of
Create a StringArray from a collection of strings -
get
Get the string at array index 'i'- Specified by:
getin classStringArrayBase
-
getOffset
public int getOffset() -
set
Add a string to the array WARNING: Typically you use either 'add' or 'set', but not both- Specified by:
setin classStringArrayBase
-
size
public int size()Size of the data array (capacity of number of bytes)- Specified by:
sizein classStringArrayBase
-
sizeBytes
public long sizeBytes()Memory size of this object (approximate size in bytes)- Specified by:
sizeBytesin classStringArrayBase
-
toString
-