Class ArrayHelper
java.lang.Object
org.javimmutable.collections.common.ArrayHelper
-
Nested Class Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> T[]
allocate
(int size) static <T> ArrayHelper.Allocator
<T> Creates an Allocator for arrays of the given class.static <T> T[]
append
(ArrayHelper.Allocator<T> allocator, T[] orig, T value) Creates a copy of orig with one extra value added at the end.static <T> T[]
assign
(T[] orig, int index, T value) Creates a copy of orig with the value at index replaced by value.static <T> T[]
assignAppend
(ArrayHelper.Allocator<T> allocator, T[] orig, T assignValue, T appendValue) Replace the last value in orig (which cannot be empty) with assignValue and also append appendValue to the end.static <T> T[]
assignDelete
(ArrayHelper.Allocator<T> allocator, T[] orig, int index, T newNode) Deletes the node at index and sets the value of the resulting array at index to newNode.static <T> T[]
assignInsert
(ArrayHelper.Allocator<T> allocator, T[] orig, int index, T assignValue, T insertValue) Replace the value at index with assignValue and insert insertValue immediately at index + 1.static <T> T[]
assignTwo
(T[] orig, int index, T first, T second) Replaces the node at index with first and the node at index + 1 with second.static <T> void
checkBounds
(T[] values, int index) static <T> T[]
concat
(ArrayHelper.Allocator<T> allocator, T[] a, T[] b) Creates a new array containing all the values of a followed by all the values of b.static <T> T[]
delete
(ArrayHelper.Allocator<T> allocator, T[] orig, int index) Creates a copy of orig with one value deleted at index.static <T> T[]
insert
(ArrayHelper.Allocator<T> allocator, T[] orig, int index, T value) Creates a copy of orig with one extra value inserted at index.static <T> T[]
newArray
(T a) static <T> T[]
newArray
(T a, T b) static <T> T[]
prefix
(ArrayHelper.Allocator<T> allocator, T[] orig, int limit) Returns a copy of orig containing of length limit containing all values from [0,limit).static <T> T[]
prefixInsert
(ArrayHelper.Allocator<T> allocator, T[] orig, int limit, int index, T value) Returns a copy of orig containing of length limit+1 containing all values from [0,limit) but with value inserted at index and values after that shifted to the right.static <T> T[]
reverse
(ArrayHelper.Allocator<T> allocator, T[] orig) static <T> T[]
subArray
(ArrayHelper.Allocator<T> allocator, T[] orig, int offset, int limit) Allocate a new array containing the subarray of orig starting at offset and ending before limit.static <T> T[]
subArray
(ArrayHelper.Allocator<T> allocator, T[] a, T[] b, int offset, int limit) Allocate an array containing values from a logical array formed by concatenating the two input arrays.static <T> T[]
suffix
(ArrayHelper.Allocator<T> allocator, T[] orig, int offset) Returns a copy of orig containing of length orig.length-offset containing all values from [offset,orig.length).static <T> T[]
suffixInsert
(ArrayHelper.Allocator<T> allocator, T[] orig, int offset, int index, T value) Returns a copy of orig containing of length orig.length-offset containing all values from [offset,orig.length) but with value inserted at index relative to orig (index-offset relative to resulting array) and all values past that point shifted to the right.
-
Constructor Details
-
ArrayHelper
public ArrayHelper()
-
-
Method Details
-
subArray
@Nonnull public static <T> T[] subArray(@Nonnull ArrayHelper.Allocator<T> allocator, @Nonnull T[] orig, int offset, int limit) Allocate a new array containing the subarray of orig starting at offset and ending before limit. To copy the first value offset would be 0, limit would be 1.- Parameters:
allocator
- used to allocate the resulting arrayorig
- array from which to copyoffset
- 0 based offset into arraylimit
- 0 based offset of first value NOT copied (length of copy is limit - offset)
-
subArray
@Nonnull public static <T> T[] subArray(@Nonnull ArrayHelper.Allocator<T> allocator, @Nonnull T[] a, @Nonnull T[] b, int offset, int limit) Allocate an array containing values from a logical array formed by concatenating the two input arrays. Functionally equivalent to calling append(a,b) and then calling subArray() with the result- Parameters:
allocator
- used to allocate the resulting arraya
- first input arrayb
- second input arrayoffset
- 0 based offset into logical concatenated arraylimit
- 0 based offset of first value NOT copied (length of copy is limit - offset)
-
assign
Creates a copy of orig with the value at index replaced by value.- Parameters:
orig
- array to copyindex
- index of value to changevalue
- value to assign at index
-
append
@Nonnull public static <T> T[] append(@Nonnull ArrayHelper.Allocator<T> allocator, @Nonnull T[] orig, T value) Creates a copy of orig with one extra value added at the end. Length of result is orig.length + 1.- Parameters:
allocator
- used to allocate the resulting arrayorig
- array to copyvalue
- value to assign at end of new array
-
insert
@Nonnull public static <T> T[] insert(@Nonnull ArrayHelper.Allocator<T> allocator, @Nonnull T[] orig, int index, T value) Creates a copy of orig with one extra value inserted at index. All values from index->length are shifted to the right by 1.- Parameters:
allocator
- used to allocate the resulting arrayorig
- array to copyindex
- index where new value should be insertedvalue
- value to assign at end of new array
-
delete
@Nonnull public static <T> T[] delete(@Nonnull ArrayHelper.Allocator<T> allocator, @Nonnull T[] orig, int index) Creates a copy of orig with one value deleted at index. All values from index->length are shifted to the left by 1.- Parameters:
allocator
- used to allocate the resulting arrayorig
- array to copyindex
- index where new value should be inserted
-
concat
@Nonnull public static <T> T[] concat(@Nonnull ArrayHelper.Allocator<T> allocator, @Nonnull T[] a, @Nonnull T[] b) Creates a new array containing all the values of a followed by all the values of b.- Parameters:
allocator
- used to allocate the resulting arraya
- first input arrayb
- second input array
-
assignAppend
@Nonnull public static <T> T[] assignAppend(@Nonnull ArrayHelper.Allocator<T> allocator, @Nonnull T[] orig, T assignValue, T appendValue) Replace the last value in orig (which cannot be empty) with assignValue and also append appendValue to the end. Always returns a new array of length orig.length + 1. -
assignTwo
Replaces the node at index with first and the node at index + 1 with second. Always returns a new array. Array must have size >= index + 2. -
assignInsert
@Nonnull public static <T> T[] assignInsert(@Nonnull ArrayHelper.Allocator<T> allocator, @Nonnull T[] orig, int index, T assignValue, T insertValue) Replace the value at index with assignValue and insert insertValue immediately at index + 1. Always returns a new array. Array size must be at least 1. -
assignDelete
@Nonnull public static <T> T[] assignDelete(@Nonnull ArrayHelper.Allocator<T> allocator, @Nonnull T[] orig, int index, T newNode) Deletes the node at index and sets the value of the resulting array at index to newNode. Always returns a new array. -
prefix
@Nonnull public static <T> T[] prefix(@Nonnull ArrayHelper.Allocator<T> allocator, @Nonnull T[] orig, int limit) Returns a copy of orig containing of length limit containing all values from [0,limit). -
prefixInsert
@Nonnull public static <T> T[] prefixInsert(@Nonnull ArrayHelper.Allocator<T> allocator, @Nonnull T[] orig, int limit, int index, T value) Returns a copy of orig containing of length limit+1 containing all values from [0,limit) but with value inserted at index and values after that shifted to the right. -
suffix
@Nonnull public static <T> T[] suffix(@Nonnull ArrayHelper.Allocator<T> allocator, @Nonnull T[] orig, int offset) Returns a copy of orig containing of length orig.length-offset containing all values from [offset,orig.length). -
suffixInsert
@Nonnull public static <T> T[] suffixInsert(@Nonnull ArrayHelper.Allocator<T> allocator, @Nonnull T[] orig, int offset, int index, T value) Returns a copy of orig containing of length orig.length-offset containing all values from [offset,orig.length) but with value inserted at index relative to orig (index-offset relative to resulting array) and all values past that point shifted to the right. -
reverse
@Nonnull public static <T> T[] reverse(@Nonnull ArrayHelper.Allocator<T> allocator, @Nonnull T[] orig) -
allocator
Creates an Allocator for arrays of the given class. -
checkBounds
public static <T> void checkBounds(T[] values, int index) -
allocate
public static <T> T[] allocate(int size) -
newArray
public static <T> T[] newArray(T a) -
newArray
public static <T> T[] newArray(T a, T b)
-