public final class LargeIntArray
extends java.lang.Object
implements java.lang.Cloneable, java.lang.Iterable<java.lang.Integer>
This class enables arrays of ints up to 64-bits in size (for the exact
maximum size, please see MAX_SIZE
). As a single array is limited to
32-bits in Java, this is done by having an array of arrays. We use a
long
to index this array of arrays, and use bitwise operators
to extract the two indices for the array of arrays. The largest index is
extracted using getSegment(long)
while the smallest index is extracted
using getOffset(long)
, where the actual value is located in
values[segment][offset]
.
In addition, this class enables a dynamic array, and can be dynamically
enlarged or shrunken. The total capacity is indicated by capacity()
,
while the actual number of elements is indicated by size()
. When
constructing new arrays, typically the size is set equal to the capacity,
with the restriction that we always at least reserve a minimum capacity
(indicated by MINIMUM_INITIAL_CAPACITY
). This dynamic array then
also supports push(int)
and pop()
operations.
This class also contains a number of convenience functions to easily add(long, int)
, subtract(long, int)
, multiply(long, int)
and divide(long, int)
elements.
This is more efficient than calling both set
and
get
because this requires to retrieve indices twice.
To facilitate easy iteration over this array, a few convenience iterables are
provided, enabling you to use construct such as
for (int x : array)
and
for (int x : array.fromto(10, 50)
Modifier and Type | Class and Description |
---|---|
class |
LargeIntArray.FromIterable
Iterable starting from a certain element.
|
class |
LargeIntArray.FromToIterable
Iterable starting from a certain element to a certain element.
|
class |
LargeIntArray.Iterator
Iterator capable of iterating over a specified range.
|
Modifier and Type | Field and Description |
---|---|
static byte |
ARRAY_BIT_SIZE
The number of bits to use for each individual array.
|
static long |
MAX_SIZE
The maximum size of the array in total.
|
static int |
MAX_SIZE_ARRAY
The maximum size of each individual array.
|
static long |
MINIMUM_INITIAL_CAPACITY
Minimum initial capacity of the array.
|
static double |
RELATIVE_CAPACITY_INCREASE
The relative increase in the capacity when increasing the capacity.
|
Constructor and Description |
---|
LargeIntArray(int[] values)
Constructs a new array copying the values of the supplied array.
|
LargeIntArray(long size)
Constructs a new empty array of specified size.
|
LargeIntArray(long size,
int constant)
Constructs a new array of indicated size with all elements set to a
constant value.
|
Modifier and Type | Method and Description |
---|---|
void |
add(long index,
int addition)
Adds addition to existing element.
|
void |
append(int value)
Appends a specified value to the end of the array.
|
long |
binarySearch(int value)
Searches for the specified value using binary search.
|
long |
binarySearch(long from,
long to,
int value)
Searches for the specified value using binary search in the indicated
range.
|
double |
calcAverage()
Calculates the average of all elements.
|
int |
calcMaximum()
Calculates the maximum of all elements.
|
int |
calcMinimum()
Calculates the minimum of all elements.
|
int |
calcSum()
Calculates the sum of all elements.
|
int |
calcSum(long from,
long to)
Calculates the sum of all elements in the indicated range.
|
long |
capacity()
Gets capacity of array.
|
void |
clear()
Removes all elements from the array.
|
LargeIntArray |
clone()
Clones the array.
|
LargeIntArray |
copyOfRange(long from,
long to)
Copies the specified range to a new array.
|
void |
divide(long index,
int divisor)
Divides existing element by divisor.
|
void |
ensureCapacity(long minCapacity)
Increases the capacity to ensure that the array has at least the minimum
capacity.
|
void |
fill(int constant)
Fills entire array with constant.
|
void |
fill(long from,
long to,
int constant)
Fills indicated range with constant.
|
LargeIntArray.FromIterable |
from(long from)
Iterates over all elements, starting from the indicated element.
|
LargeIntArray.FromToIterable |
fromTo(long from,
long to)
Iterates over elements in indicated range.
|
int |
get(int segment,
int offset)
Gets value element based on segment and offset.
|
int |
get(long index)
Gets value element based on long index.
|
static int |
getOffset(long index)
Gets the offset within a particular segment of the index.
|
static int |
getSegment(long index)
Gets the segment of the index.
|
LargeIntArray.Iterator |
iterator()
Iterates over all elements
|
int |
length(int segment)
Gets the length of the indicated segment.
|
void |
mergeSort()
Sorts elements in ascending order using merge sort.
|
void |
multiply(long index,
int multiplier)
Multiplies existing element by multiplier.
|
int |
nSegments()
Gets the number of segments.
|
int |
pop()
Pops last value off the end of the array.
|
void |
push(int value)
Appends a specified value to the end of the array.
|
void |
quickSort()
Sorts elements in ascending order using quick sort.
|
void |
resize(long size)
Resizes array.
|
void |
set(int segment,
int offset,
int value)
Sets element to value for indicated segment and offset.
|
void |
set(long index,
int value)
Sets element to value for indicated index.
|
void |
shrink()
Shrinks capacity to fit actual size.
|
long |
size()
Gets size of array.
|
void |
sort()
Sorts elements in ascending order using merge sort.
|
void |
sort(it.unimi.dsi.fastutil.longs.LongComparator comparator)
Sorts elements in a specified order using merge sort.
|
void |
subtract(long index,
int subtraction)
Subtracts subtraction from existing element.
|
void |
swap(long indexA,
long indexB)
Swaps two elements.
|
int[] |
toArray()
Copies the values to an ordinary array.
|
int[] |
toArray(long from,
long to)
Copies indicated range to an ordinary array.
|
void |
updateFrom(LargeIntArray array)
Updates this array from the provided array.
|
void |
updateFrom(LargeIntArray array,
long from,
long to,
long insertionPoint)
Updates this array from the provided array.
|
public static final byte ARRAY_BIT_SIZE
public static final int MAX_SIZE_ARRAY
public static final long MINIMUM_INITIAL_CAPACITY
public static final double RELATIVE_CAPACITY_INCREASE
public static final long MAX_SIZE
public LargeIntArray(long size)
Both the capacity and the size is being set to size
, with
all elements initialised to their default value. If instead, you prefer
an empty array, but reserve capacity, pass 0
size
here and use ensureCapacity(long)
.
size
- Size of the arraypublic LargeIntArray(long size, int constant)
size
- Size of the arrayconstant
- Constant value to setpublic LargeIntArray(int[] values)
values
- Array to copy frompublic static int getSegment(long index)
index
- Indexpublic static int getOffset(long index)
index
- Indexpublic int nSegments()
public int length(int segment)
segment
- Segmentpublic int get(long index)
index
- Index of elementpublic int get(int segment, int offset)
segment
- Segment of elementoffset
- Offset of elementpublic void set(long index, int value)
index
- Index of elementvalue
- Valuepublic void set(int segment, int offset, int value)
segment
- Segment of elementoffset
- Offset of elementvalue
- Valuepublic void fill(int constant)
constant
- Constantpublic void fill(long from, long to, int constant)
from
- From index, inclusiveto
- To index, exclusiveconstant
- Constantpublic void append(int value)
public void push(int value)
value
- Valueappend(int)
,
pop()
public int pop()
public void clear()
public void ensureCapacity(long minCapacity)
The capacity is increased with a percentage indicated by RELATIVE_CAPACITY_INCREASE
, unless the indicated minimum capacity
exceeds this.
minCapacity
- Minimum capacitypublic void resize(long size)
size
- New sizepublic void shrink()
public long size()
This is always less than or equal to the capacity.
public long capacity()
This is always greater than or equal to the size.
public void add(long index, int addition)
index
- Index of elementaddition
- Value to addpublic void subtract(long index, int subtraction)
index
- Index of elementsubtraction
- Value to subtractpublic void multiply(long index, int multiplier)
index
- Index of elementmultiplier
- Multiplierpublic void divide(long index, int divisor)
index
- Index of elementdivisor
- Divisorpublic int calcSum()
public int calcSum(long from, long to)
from
- From index, inclusiveto
- To index, exclusivepublic double calcAverage()
public int calcMaximum()
public int calcMinimum()
public void swap(long indexA, long indexB)
indexA
- First element to swapindexB
- Second element to swappublic void mergeSort()
public void quickSort()
public void sort()
public void sort(it.unimi.dsi.fastutil.longs.LongComparator comparator)
comparator
- Comparator indicating the desired orderingpublic long binarySearch(int value)
The array must be sorted in ascending order prior to making this call. If it is not sorted, the results are undefined. If the array contains multiple elements with the specified value, there is no guarantee which one will be found.
value
- The value to search forpublic long binarySearch(long from, long to, int value)
The (range of the) array must be sorted in ascending order prior to making this call. If it is not sorted, the results are undefined. If the array contains multiple elements with the specified value, there is no guarantee which one will be found.
from
- From index, inclusiveto
- To index, exclusivevalue
- The value to search forpublic LargeIntArray.Iterator iterator()
iterator
in interface java.lang.Iterable<java.lang.Integer>
public LargeIntArray.FromIterable from(long from)
from
- From index, inclusivepublic LargeIntArray.FromToIterable fromTo(long from, long to)
from
- From index, inclusiveto
- To index, exclusivepublic void updateFrom(LargeIntArray array)
array
- Array to update frompublic void updateFrom(LargeIntArray array, long from, long to, long insertionPoint)
Values from other array starting at from
until
to
(exclusive) will be copied to this array, starting
from the insertionPoint
onwards.
array
- Array to update fromfrom
- Index in array
from where to update,
inclusiveto
- Index in array
until where to update,
exclusiveinsertionPoint
- Starting index in this array to updatepublic LargeIntArray copyOfRange(long from, long to)
from
- From index, inclusiveto
- To index, exclusivepublic int[] toArray()
Not all elements of this array may fit in an ordinary array.
public int[] toArray(long from, long to)
from
- From index, inclusiveto
- To index, exclusivepublic LargeIntArray clone()
clone
in class java.lang.Object