|
AlgART Home | ||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
public interface MutableArray extends Stack, UpdatableArray
AlgART one-dimensional array of any elements, full access (reading, writing, resizing).
If the elements of this array are primitive values (byte, short, etc.),
the array must implement one of
MutableBitArray, MutableCharArray, MutableByteArray, MutableShortArray,
MutableIntArray, MutableLongArray, MutableFloatArray, MutableDoubleArray
subinterfaces.
In other case, this array must implement MutableObjectArray subinterface.
Resizable arrays, implementing this interface, are not thread-safe, but are thread-compatible and can be synchronized manually if multithread access is necessary. Please see more details in the package description.
AlgART Laboratory 2007-2013
Array,
UpdatableArray,
Matrix| Modifier and Type | Method and Description |
|---|---|
MutableArray |
append(Array appendedArray)
Appends the specified array to the end of this array. |
MutableArray |
asCopyOnNextWrite()
Returns a copy-on-next-write view of this array. |
MutableArray |
copy(Array src)
Copies min(this. length(), src.length()) elements of
src array, starting from index 0,
to this array, starting from index 0. |
MutableArray |
ensureCapacity(long minCapacity)
Increases the capacity of this instance, if necessary, to ensure that it can hold at least the given number of elements. |
MutableArray |
length(long newLength)
Changes the current number of elements in this array. |
MutableArray |
setData(long arrayPos,
java.lang.Object srcArray)
Copies min(this. length() - arrayPos, srcArray.length})
elements from the specified Java array of corresponding type,
starting from 0 index,
into this array, starting from arrayPos index. |
MutableArray |
setData(long arrayPos,
java.lang.Object srcArray,
int srcArrayOffset,
int count)
Copies count elements from the specified Java array of corresponding type, starting from srcArrayOffset index, into this array, starting from arrayPos index. |
MutableArray |
shallowClone()
Returns a "shallow" clone of this object: another array consisting of the same elements, but with independent length, start offset, capacity, copy-on-next-write and possible other information about any array characteristics besides its elements. |
MutableArray |
swap(UpdatableArray another)
Swaps min(this. length(), src.length()) elements of
another array, starting from index 0,
and the same number of elements of this array, starting from index 0. |
MutableArray |
trim()
Trims the capacity of this array to be the array's current length. |
| Methods inherited from interface net.algart.arrays.Stack |
|---|
length, popElement, pushElement |
| Methods inherited from interface net.algart.arrays.UpdatableArray |
|---|
asUnresizable, copy, copy, setElement, setNonNew, subArr, subArray, swap, swap |
| Methods inherited from interface net.algart.arrays.Array |
|---|
asImmutable, asTrustedImmutable, buffer, buffer, buffer, buffer, byteOrder, capacity, checkUnallowedMutation, elementType, equals, flushResources, flushResources, freeResources, freeResources, getData, getData, getElement, hashCode, isCopyOnNextWrite, isImmutable, isLazy, isNew, isNewReadOnlyView, isUnresizable, length, loadResources, mutableClone, mutableType, newJavaArray, toString, type, updatableClone, updatableType |
| Method Detail |
|---|
MutableArray length(long newLength)
Notice: if newLength is less than current number of elements,
then the unused elements (from newLength to (current length)-1)
may be changed (for example, filled by zero) while calling this method.
It is not important usually, because the array doesn't provide access to elements
after (current length)-1.
But if there are some "views" of this array, for examples, created by
UpdatableArray.subArray(long, long) or UpdatableArray.asUnresizable() methods,
you need to remember that reducing length of the source array can lead to changing
last elements of such views.
newLength - the desired new number of elements.TooLargeArrayException - if the specified length is too large for this type of arrays.java.lang.IllegalArgumentException - if the specified length is negative.Arrays.lengthUnsigned(MutableArray, long)MutableArray ensureCapacity(long minCapacity)
minCapacity - the desired minimum capacity.TooLargeArrayException - if the specified capacity is too large for this type of arrays.java.lang.IllegalArgumentException - if the specified capacity is negative.MutableArray trim()
MutableArray append(Array appendedArray)
Array.elementType()) are the same.
IllegalArgumentException will be thrown.
appendedArray - appended array.java.lang.NullPointerException - if appendedArray argument is null.java.lang.IllegalArgumentException - if the source and this element types do not match.TooLargeArrayException - if the resulting array length is too large for this type of arrays.UpdatableArray.asUnresizable()
MutableArray setData(long arrayPos,
java.lang.Object srcArray,
int srcArrayOffset,
int count)
UpdatableArrayFor arrays of non-primitive elements,
if some elements of Java array are null,
this method does not throw NullPointerException,
but may set the corresponding array elements to some default value,
if null elements are not supported
by the memory model (as in a case of CombinedMemoryModel).
Note: if IndexOutOfBoundsException occurs due to attempt to read data outside the passed Java array, this AlgART array can be partially filled. In other words, this method can be non-atomic regarding this failure. All other possible exceptions are checked in the very beginning of this method before any other actions (the standard way for checking exceptions).
setData in interface UpdatableArrayarrayPos - starting position in this AlgART array.srcArray - the source Java array.srcArrayOffset - starting position in the source Java array.count - the number of elements to be copied.DirectAccessible,
Array.getData(long, Object, int, int)
MutableArray setData(long arrayPos,
java.lang.Object srcArray)
UpdatableArraylength() - arrayPos, srcArray.length})
elements from the specified Java array of corresponding type,
starting from 0 index,
into this array, starting from arrayPos index.
For arrays of non-primitive elements,
if some elements of Java array are null,
this method does not throw NullPointerException,
but may set the corresponding array elements to some default value,
if null elements are not supported
by the memory model (as in a case of CombinedMemoryModel).
Note: if IndexOutOfBoundsException occurs due to attempt to read data outside the passed Java array, this AlgART array can be partially filled. In other words, this method can be non-atomic regarding this failure. All other possible exceptions are checked in the very beginning of this method before any other actions (the standard way for checking exceptions).
setData in interface UpdatableArrayarrayPos - starting position in this AlgART array.srcArray - the source Java array.DirectAccessible,
UpdatableArray.setData(long, Object, int, int),
Array.getData(long, Object),
UpdatableBitArray.setBits(long, long[], long, long)MutableArray copy(Array src)
UpdatableArraylength(), src.length()) elements of
src array, starting from index 0,
to this array, starting from index 0.
You may use UpdatableArray.subArray(long, long) or UpdatableArray.subArr(long, long) methods
to copy any portion of one array to any position of another array, for example:
destArray.subArr(destPos, count).copy(srcArray.subArr(srcPos, count));
Some elements may be copied incorrectly if this array and src
are views of the same data and the swapped data areas overlap
(in the example above, Math.abs(destPos - srcPos) < count).
However, for arrays, created by SimpleMemoryModel, this method work correctly always,
even for overlapping areas.
For any arrays, if the copied areas of the underlying data are the same
(for example, if src if some view of this array generated by
Arrays.asFuncArray, but not Arrays.asShifted method),
this method work correctly: any elements will be read before they will be updated.
This method works only if the element types of this and src arrays
(returned by Array.elementType()) are the same, or (for non-primitive elements)
if element type of this array is a superclass of the source element type.
For non-primitive element type (ObjectArray, UpdatableObjectArray,
MutableObjectArray subinterfaces), this method may copy only references to elements,
but also may copy the content of elements: it depends on implementation.
Please note: this method is a good choice for copying little arrays (thousands,
maybe hundreds of thousands elements). If you copy large arrays by this method,
the user, in particular, has no ways to view the progress of copying or to interrupt copying.
In most situations, we recommend more "intellectual" method
Arrays.copy(ArrayContext, UpdatableArray, Array) for copying large arrays.
copy in interface UpdatableArraysrc - the source array.Arrays.copy(ArrayContext, UpdatableArray, Array)MutableArray swap(UpdatableArray another)
UpdatableArraylength(), src.length()) elements of
another array, starting from index 0,
and the same number of elements of this array, starting from index 0.
You may use UpdatableArray.subArray(long, long) or UpdatableArray.subArr(long, long) methods
to swap any portions of two array, for example:
array1.subArr(pos1, count).swap(array2.subArr(pos2, count));
Some elements may be swapped incorrectly if this array and another is the same array, or are views of the same data, and the swapped areas overlap (in the example above, Math.abs(pos1 - pos2) < count).
This method must work always if the element types of this and src arrays
(returned by Array.elementType()) are the same.
For non-primitive element type (ObjectArray, UpdatableObjectArray,
MutableObjectArray subinterfaces), this method may swap only references to elements,
but also may swap the content of elements: it depends on implementation.
Please note: this method is a good choice for swapping little arrays (thousands,
maybe hundreds of thousands elementthousands elements). If you swap large arrays by this method,
the user, in particular, has no ways to view the progress of copying or to interrupt copying.
To swap large arrays, you can split them to little regions and swap the regions in a loop by this method,
with calling ArrayContext methods to allow interruption and showing progress.
swap in interface UpdatableArrayanother - another array.MutableArray asCopyOnNextWrite()
ArrayUpdatableArray interface,
then the returned array also implements it.
If (and only if) this array implements MutableArray interface,
then the returned array also implements it.
Copy-on-next-write array is a array with the following special feature:
the next attempt (but not further!) to modify this array,
or any other access that can lead to its modification (like DirectAccessible.javaArray()
method), will lead to reallocation of the underlying storage, used for array elements,
before performing the operation. In other words,
you have a guarantee: if this array is a view of some another array or data
(for example, a subarray or
a view of Java array,
that there are no ways to change that data
via accessing the returned array. Any changes, it they will occur,
will be performed with the newly allocated storage only.
Moreover, there are no guarantees that the returned array will be a view of this one,
even immediately after creation.
Some implementations of updatable arrays may just return the full (deep) copy of this object,
alike Array.mutableClone(MemoryModel) method, and in this case
TooLargeArrayException is possible.
All implementations from this package, excepting AbstractUpdatableXxxArray classes,
returns a view;
but in AbstractUpdatableXxxArray classes this method is equivalent to
updatableClone(Arrays.SMM).
Please note that copy-on-next-write arrays are not traditional copy-on-write objects like CopyOnWriteArrayList! In particular, copy-on-next-write arrays are not thread-safe.
The main purpose of using copy-on-next-write arrays is more efficient alternative
to cloning and creating quite immutable views,
when we need to be sure that original data will not be corrupted.
Please see the package description
to learn more about possible usage of this technique.
asCopyOnNextWrite in interface ArrayasCopyOnNextWrite in interface UpdatableArrayArray.isCopyOnNextWrite(),
MemoryModel.newLazyCopy(Array),
MemoryModel.newUnresizableLazyCopy(Array)MutableArray shallowClone()
ArrayPlease note: this method never returns a reference to this object, even if this array is immutable! Moreover, the returned object does not store any references to this instance in any internal fields. It can be important while using weak references and reference queues: this object and its shallow copy are deallocated by the garbage collector separately.
There are no guarantees that the returned array will share the elements with this one.
Some implementations may return the full (deep) copy of this object, alike
Array.updatableClone(MemoryModel) or Array.mutableClone(MemoryModel) methods.
All implementations from this package returns a shallow (non-deep) copy.
If modifications of this or returned array characteristics lead to reallocation
of the internal storage, then the returned array ceases to be a view of this array.
The only possible reasons for reallocation are the following:
calling length(long),
ensureCapacity(long) or trim() methods
for this array, or any modification of this or returned array in a case when
this array is copy-on-next-write.
The values of Array.length(), Array.capacity(), DirectAccessible.javaArrayOffset(),
Array.isCopyOnNextWrite() in the result will be the same as in this array.
The returned instance implements the same set of interfaces as this array.
This method is an analog of the standard Java NIO
ByteBuffer.duplicate() method. However, unlike ByteBuffer.duplicate(),
this method is necessary very rarely. Usually, you need another forms
of array views: Array.asImmutable(), UpdatableArray.asUnresizable(), etc.
The most often usage of this method is finalization
via Finalizer class:
see example in comments to Array.checkUnallowedMutation() method. Also this method
can be useful if you need to pass a array with the same content
into some another class, but must be sure that further resizing
of the source array will not affect to correct work of that class.
shallowClone in interface ArrayshallowClone in interface UpdatableArrayArray.length(),
Array.capacity(),
DirectAccessible.javaArrayOffset(),
Array.isCopyOnNextWrite()
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||