com.alexkasko.unsafe.offheaplong
public class OffHeapLongArrayList extends Object implements OffHeapLongAddressable, OffHeapDisposable, Iterable<Long>
Implementation of array-list of long using OffHeapMemory
.
Memory area will be allocated another time and copied on elements adding. This class doesn't support elements removing.
get(long)
and set(long, long)
access operations indexes are checked using assert
keyword
(indexes between size and capacity will be rejected).
Default implementation uses sun.misc.Unsafe
, with all operations guarded with assert
keyword.
With assertions enabled in runtime (-ea
java switch) AssertionError
will be thrown on illegal index access. Without assertions illegal index will crash JVM.
Allocated memory may be freed manually using free()
(thread-safe
and may be called multiple times) or it will be freed after OffHeapLongArray
will be garbage collected.
Note: while class implements Iterable, iterator will create new autoboxed Long object
on every next()
call, this behaviour is inevitable with iterators in java 6/7.
Modifier and Type | Field and Description |
---|---|
private long |
capacity |
private static int |
ELEMENT_LENGTH |
private static int |
MIN_CAPACITY_INCREMENT |
private OffHeapMemory |
ohm |
private long |
size |
Constructor and Description |
---|
OffHeapLongArrayList()
Constructor,
12 is used as initial capacity |
OffHeapLongArrayList(long capacity)
Constructor
|
Modifier and Type | Method and Description |
---|---|
void |
add(long value)
Adds element to the end of this list.
|
long |
capacity()
Returns number of elements list may contain without additional memory allocation
|
void |
free()
Frees allocated memory, may be called multiple times from any thread
|
long |
get(long index)
Gets the element at position
index from 0 to size-1 |
boolean |
isUnsafe()
Whether unsafe implementation of
OffHeapMemory is used |
OffHeapDisposableIterator<Long> |
iterator() |
void |
reset()
Resets the collection setting size to 0.
|
void |
set(long index,
long value)
Sets the element at position
index (from 0 to size-1 ) to the given value |
long |
size()
Returns number of elements in list
|
String |
toString() |
private static final int MIN_CAPACITY_INCREMENT
private static final int ELEMENT_LENGTH
private OffHeapMemory ohm
private long size
private long capacity
public OffHeapLongArrayList()
12
is used as initial capacitypublic OffHeapLongArrayList(long capacity)
capacity
- initial capacitypublic void add(long value)
value
- value to addpublic boolean isUnsafe()
OffHeapMemory
is usedOffHeapMemory
is usedpublic long get(long index)
index
from 0
to size-1
get
in interface OffHeapAddressable
index
- list indexpublic void set(long index, long value)
index
(from 0
to size-1
) to the given valueset
in interface OffHeapLongAddressable
index
- list indexvalue
- long valuepublic long size()
size
in interface OffHeapAddressable
public long capacity()
public void free()
free
in interface OffHeapDisposable
public OffHeapDisposableIterator<Long> iterator()
public void reset()
Copyright © 2014. All Rights Reserved.