SparseArray:
Filter:
Classes | Collections > Ordered

SparseArray : Order : SequenceableCollection : Collection : Object

Array that stores duplicated values more efficiently

Description

A sparse array is a data structure that acts in exactly the same manner as an Array. However, data is represented differently in memory, in a way that makes it much more efficient to store very large arrays in which many values are the same.

Take for example an array consisting of a million zeroes, with a 1 appended to the end; SparseArray would compress this array by storing zero as a default value, and only explicitly storing the single value that differs, therefore offering a much more economical use of memory.

The benefits of using SparseArray typically arise when creating collections containing many millions of slots.

Class Methods

SparseArray.newClear(size, default)

Create a new SparseArray of the specified size, with each slot's value being default.

Arguments:

size

Number of slots in the desired array. Note that slots are not explicitly created, so the speed of creation is not related to the array size.

default

The default value, i.e. the value that all slots should take at first.

SparseArray.reduceArray(array, default)

Create a new SparseArray holding the same data as array.

Arguments:

array

Any ArrayedCollection.

default

The default value, i.e. the value that all slots should take at first. For best memory efficiency, you should supply the most common value found in the collection.

Inherited class methods

Instance Methods

.put(index, obj)

From superclass: Order

Put a value at the desired index. This works just like all ArrayedCollection types. Behind the scenes the class will ensure the compact representation (deciding whether to store the value explicitly or implicitly).

.at(index)

Retrieve the value at index.

.asArray

Convert to an ordinary Array.

Inherited instance methods

Undocumented instance methods

++(coll)

.atSeries(first, second, last)

.clear(size)

.collect(function)

.compress

.copy

.default

.default = value

.defaultSize

.defaultSize = value

.do(function)

.firstGap(from: 0, to)

.indexOf(item)

.lastIndex

.maxItem(function)

.minItem(function)

.pop

.pos

.putIfNotDefault(i, item)

.putSeries(first, second, last, value)

.reject(function)

.removeAt(index)

.select(function)

.size

.sparseAddAll(coll)

.sparseCollect(function)

.sparseReject(function)

.sparseRemove(item)

.sparseRemoveAt(index)

.sparseSelect(function)

.sparseSum(function)

.sum(function)

Examples

Here we compare speed of Array vs SparseArray.