Package org.ddogleg.struct
Class DogArray<T>
java.lang.Object
org.ddogleg.struct.FastAccess<T>
org.ddogleg.struct.DogArray<T>
- All Implemented Interfaces:
Serializable
Growable array which automatically creates, recycles, and resets its elements. Access to internal variables
is provided for high performant code. If a reset function is provided then when an object is created or recycled
the reset function is called with the objective of giving the object a repeatable initial state.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class org.ddogleg.struct.FastAccess
FastAccess.FunctionEach<T>, FastAccess.FunctionEachIdx<T>, FastAccess.FunctionMatches<T>
-
Field Summary
Fields inherited from class org.ddogleg.struct.FastAccess
data, size, type
-
Constructor Summary
ConstructorDescriptionConstructor which allows new instances to be created using a lambdaConstructor which allows new instances to be created using a lambdaConstructor which allows new instances to be created using a lambda and determines the class by creating a new instance.User provided factory function and reset function.User provided factory function and reset function. -
Method Summary
Modifier and TypeMethodDescription<S> void
copyAll
(List<S> list, DogArray.Set<S, T> setter) Grows the array and adds all the items in list.copyIntoList
(List<T> ret) protected T
Creates a new instance of elements stored in this arrayT[]
getData()
int
getSize()
getType()
grow()
Returns a new element of data.protected void
Data structure initialization is done here so that child classes can declay initialization until they are readyfinal boolean
boolean
Checks to see if the object is in the unused list.remove
(int index) Removes an element from the array and preserves the order of all elements.void
Removes the indexes from the array.boolean
Searches for and removes the 'target' from the list.removeSwap
(int index) Removes the specified index from the array by swapping it with last element.Shrinks the size of the array by one and returns the element stored at the former last element.void
reserve
(int length) Ensures that the internal array has at least `length` elements.reset()
void
resetResize
(int newSize) Deprecated.void
resetResize
(int newSize, DProcessIdx<T> configure) Deprecated.resize
(int newSize) Changes the size to the specified length.Resize with a configuration operator.resize
(int length, DProcessIdx<T> configure) Resize with a configuration operator.void
void
setSize
(int size) void
Randomly shuffles elements in the list.void
Shuffle where it will only shuffle up to the specified number of elements.toList()
Returns a wrapper around FastQueue that allows it to act as a read only list.
-
Constructor Details
-
DogArray
Constructor which allows new instances to be created using a lambda -
DogArray
Constructor which allows new instances to be created using a lambda and determines the class by creating a new instance. -
DogArray
User provided factory function and reset function.- Parameters:
factory
- Creates new instancesreset
- Called whenever an element is recycled and needs to be reset
-
DogArray
User provided factory function and reset function.- Parameters:
factory
- Creates new instancesreset
- Called whenever an element is recycled and needs to be resetinitialize
- Called after a new instance is created
-
DogArray
Constructor which allows new instances to be created using a lambda
-
-
Method Details
-
init
Data structure initialization is done here so that child classes can declay initialization until they are ready -
toList
Returns a wrapper around FastQueue that allows it to act as a read only list. There is little overhead in using this interface. NOTE: The same instead of a list is returned each time. Be careful when writing concurrent code and create a copy.- Specified by:
toList
in classFastAccess<T>
- Returns:
- List wrapper.
-
remove
public void remove(int[] indexes, int fromIndex, int toIndex, @Nullable @Nullable List<T> workSpace) Removes the indexes from the array. This is done by swapping removed elements with the last element. O(N) copies.- Parameters:
indexes
- Index of elements which are to be removed. This will be modifiedfromIndex
- the index of the first element, inclusive, to be sortedtoIndex
- the index of the last element, exclusive, to be sortedworkSpace
- Optional internal workspace. Can be set to null.
-
removeTail
Shrinks the size of the array by one and returns the element stored at the former last element.- Returns:
- The last element in the list that was removed.
-
reset
-
grow
Returns a new element of data. If there are new data elements available then array will automatically grow.- Returns:
- A new instance.
-
copyAll
Grows the array and adds all the items in list. Values are copied using the provided function -
remove
Removes an element from the array and preserves the order of all elements. This is done by shifting elements in the array down one and placing the removed element at the old end of the list. O(N) runtime.- Specified by:
remove
in classFastAccess<T>
- Parameters:
index
- Index of the element being removed- Returns:
- The object removed.
-
remove
Searches for and removes the 'target' from the list. Returns true if the target was found. If false then the target was never found and no change has been made. This is an O(N) operation.- Parameters:
target
- Object to remove from the list- Returns:
- true if the target was found and removed
-
removeSwap
Removes the specified index from the array by swapping it with last element. Does not preserve order but has a runtime of O(1).- Specified by:
removeSwap
in classFastAccess<T>
- Parameters:
index
- The index to be removed.- Returns:
- The removed object
-
reserve
public void reserve(int length) Ensures that the internal array has at least `length` elements. If it does not then a new internal array is created with the specified length and elements from the old are copied into the new. The `size` does not change.- Parameters:
length
- Requested minimum internal array length
-
resize
Resize with a configuration operator. Equivalent to callingreserve(int)
and this.size = N, then applying the 'configure' operator to each new element. NOTE: The 'reset' operator is applied before the 'configure' operator.- Parameters:
length
- The new size of the arrayconfigure
- Operator that the "new" element is passed in to.
-
resize
Resize with a configuration operator. Equivalent to callingreserve(int)
and this.size = N, then applying the 'configure' operator to each new element. NOTE: The 'reset' operator is applied before the 'configure' operator.- Parameters:
length
- The new size of the arrayconfigure
- Operator that the "new" element is passed in to along with the index of the element.
-
resize
Changes the size to the specified length. Equivalent to callingreserve(int)
and this.size = N. All new elements will be passed in toreset
.- Parameters:
newSize
- New array size
-
resetResize
Deprecated.Convenience functions that callsreset
first beforeresize(int, org.ddogleg.struct.DProcess<T>)
.- Parameters:
newSize
- New array size
-
resetResize
Deprecated.Convenience functions that callsreset
first beforeresize(int, org.ddogleg.struct.DProcess<T>)
, then applies the configure function for each element..- Parameters:
newSize
- New array sizeconfigure
- Operator that the "new" element is passed in to along with the index of the element.
-
shuffle
Randomly shuffles elements in the list. O(N) complexity.- Parameters:
rand
- random seed.
-
shuffle
Shuffle where it will only shuffle up to the specified number of elements. This is useful when you want to randomly select up to N elements in the list. When shuffling, The first i < N elements is randomly selected out from an element from i+1 to N-1.- Parameters:
numShuffle
- The maximum number of elements that will be shuffledrand
- random seed.
-
createInstance
Creates a new instance of elements stored in this array -
copyIntoList
-
isUnused
Checks to see if the object is in the unused list. -
getData
-
setData
-
getSize
public int getSize() -
setSize
public void setSize(int size) -
isDeclare
public final boolean isDeclare() -
getType
-