Package smile.util
Class SparseArray
java.lang.Object
smile.util.SparseArray
- All Implemented Interfaces:
Serializable
,Iterable<SparseArray.Entry>
Sparse array of double values.
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionclass
The entry in a sparse array of double values. -
Constructor Summary
ConstructorDescriptionConstructor.SparseArray
(int initialCapacity) Constructor.SparseArray
(List<SparseArray.Entry> entries) Constructor.SparseArray
(Stream<SparseArray.Entry> stream) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
append
(int i, double x) Append an entry to the array, optimizing for the case where the index is greater than all existing indices in the array.double
get
(int i) Returns the value of i-th entry.boolean
isEmpty()
Returns true if the array is empty.iterator()
void
remove
(int i) Removes an entry.boolean
set
(int i, double x) Sets or adds an entry.int
size()
Returns the number of nonzero entries.void
sort()
Sorts the array elements such that the indices are in ascending order.stream()
Returns the stream of nonzero entries.toString()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
SparseArray
public SparseArray()Constructor. -
SparseArray
public SparseArray(int initialCapacity) Constructor.- Parameters:
initialCapacity
- the initial capacity.
-
SparseArray
Constructor.- Parameters:
entries
- the nonzero entries.
-
SparseArray
Constructor.- Parameters:
stream
- the stream of nonzero entries.
-
-
Method Details
-
toString
-
size
public int size()Returns the number of nonzero entries.- Returns:
- the number of nonzero entries
-
isEmpty
public boolean isEmpty()Returns true if the array is empty.- Returns:
- true if the array is empty.
-
iterator
- Specified by:
iterator
in interfaceIterable<SparseArray.Entry>
-
stream
Returns the stream of nonzero entries.- Returns:
- the stream of nonzero entries.
-
sort
public void sort()Sorts the array elements such that the indices are in ascending order. -
get
public double get(int i) Returns the value of i-th entry.- Parameters:
i
- the index of entry.- Returns:
- the value of entry, 0.0 if the index doesn't exist in the array.
-
set
public boolean set(int i, double x) Sets or adds an entry.- Parameters:
i
- the index of entry.x
- the value of entry.- Returns:
- true if a new entry added, false if an existing entry updated.
-
append
public void append(int i, double x) Append an entry to the array, optimizing for the case where the index is greater than all existing indices in the array.- Parameters:
i
- the index of entry.x
- the value of entry.
-
remove
public void remove(int i) Removes an entry.- Parameters:
i
- the index of entry.
-