public final class LargeBooleanArray
extends java.lang.Object
implements java.lang.Cloneable
This class enables arrays of booleans 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(boolean)
and pop()
operations.
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 |
---|
LargeBooleanArray(boolean[] values)
Constructs a new array copying the values of the supplied array.
|
LargeBooleanArray(long size)
Constructs a new empty array of specified size.
|
LargeBooleanArray(long size,
boolean constant)
Constructs a new array of indicated size with all elements set to a
constant value.
|
Modifier and Type | Method and Description |
---|---|
void |
append(boolean value)
Appends a specified value to the end of the array.
|
long |
capacity()
Gets capacity of array.
|
void |
clear()
Removes all elements from the array.
|
LargeBooleanArray |
clone()
Clones the array.
|
LargeBooleanArray |
copyOfRange(long from,
long to)
Copies the specified range to a new array.
|
void |
ensureCapacity(long minCapacity)
Increases the capacity to ensure that the array has at least the minimum
capacity.
|
void |
fill(boolean constant)
Fills entire array with constant.
|
void |
fill(long from,
long to,
boolean constant)
Fills indicated range with constant.
|
boolean |
get(int segment,
int offset)
Gets value element based on segment and offset.
|
boolean |
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.
|
int |
length(int segment)
Gets the length of the indicated segment.
|
void |
mergeSort()
Sorts elements in ascending order using merge sort.
|
int |
nSegments()
Gets the number of segments.
|
boolean |
pop()
Pops last value off the end of the array.
|
void |
push(boolean 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,
boolean value)
Sets element to value for indicated segment and offset.
|
void |
set(long index,
boolean 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 |
swap(long indexA,
long indexB)
Swaps two elements.
|
boolean[] |
toArray()
Copies the values to an ordinary array.
|
boolean[] |
toArray(long from,
long to)
Copies indicated range to an ordinary array.
|
void |
updateFrom(LargeBooleanArray array)
Updates this array from the provided array.
|
void |
updateFrom(LargeBooleanArray 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 LargeBooleanArray(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 LargeBooleanArray(long size, boolean constant)
size
- Size of the arrayconstant
- Constant value to setpublic LargeBooleanArray(boolean[] 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 boolean get(long index)
index
- Index of elementpublic boolean get(int segment, int offset)
segment
- Segment of elementoffset
- Offset of elementpublic void set(long index, boolean value)
index
- Index of elementvalue
- Valuepublic void set(int segment, int offset, boolean value)
segment
- Segment of elementoffset
- Offset of elementvalue
- Valuepublic void fill(boolean constant)
constant
- Constantpublic void fill(long from, long to, boolean constant)
from
- From index, inclusiveto
- To index, exclusiveconstant
- Constantpublic void append(boolean value)
value
- Valuepush(boolean)
,
pop()
public void push(boolean value)
value
- Valueappend(boolean)
,
pop()
public boolean 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 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 void updateFrom(LargeBooleanArray array)
array
- Array to update frompublic void updateFrom(LargeBooleanArray 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 LargeBooleanArray copyOfRange(long from, long to)
from
- From index, inclusiveto
- To index, exclusivepublic boolean[] toArray()
Not all elements of this array may fit in an ordinary array.
public boolean[] toArray(long from, long to)
from
- From index, inclusiveto
- To index, exclusivepublic LargeBooleanArray clone()
clone
in class java.lang.Object