Questions tagged [sparse-array]

In computer science, a sparse array is an array in which most of the elements have the same value (known as the default value—usually 0 or null).

In computer science, a sparse array is an array in which most of the elements have the same value (known as the default value—usually 0 or null).

The occurrence of zero elements in a large array is inefficient for both computation and storage. An array in which there is a large number of zero elements is referred to as being sparse.

In the case of sparse arrays, one can ask for a value from an "empty" array position. If one does this, then for an array of numbers, a value of zero should be returned, and for an array of objects, a value of null should be returned.

A naive implementation of an array may allocate space for the entire array, but in the case where there are few non-default values, this implementation is inefficient. Typically the algorithm used instead of an ordinary array is determined by other known features (or statistical features) of the array. For instance, if the sparsity is known in advance or if the elements are arranged according to some function (e.g., the elements occur in blocks). A heap memory allocator in a program might choose to store regions of blank space in a linked list rather than storing all of the allocated regions in, say a bit array.

Wikipedia: http://en.wikipedia.org/wiki/Sparse_array

94 questions
71
votes
7 answers

Sparse matrices / arrays in Java

I'm working on a project, written in Java, which requires that I build a very large 2-D sparse array. Very sparse, if that makes a difference. Anyway: the most crucial aspect for this application is efficency in terms of time (assume loads of…
DanM
  • 6,727
  • 11
  • 48
  • 83
41
votes
5 answers

SparseArray, check if key exists

I was implementing a Bitmap cache using a HashMap and received the following warning in Eclipse: Use new SparseArray(...) instead for better performance. I've never heard of that class before, but inspecting it it doesn't seem to…
magritte
  • 6,801
  • 8
  • 52
  • 78
33
votes
4 answers

javascript sort sparse array keep indexes

What is the best method to sort a sparse array and keep the elements on the same indexes? For example: a[0] = 3, a[1] = 2, a[2] = 6, a[7] = 4, a[8] = 5, I would like after the sort to have a[0] = 2, a[1] = 3, a[2] = 4, a[7] = 5, a[8] = 6.
TestersGonnaTest
  • 1,006
  • 2
  • 9
  • 21
24
votes
4 answers

Does null occupy memory in javascript?

I've got the following situation: var large = [a,b,c,d,e,f,g,h,i]; var small = [a2, b2, c2, null, null, null, null, null, null, i2]; where every element of both arrays is an object. The small array contains information related to the the larger…
Radu
  • 7,996
  • 8
  • 47
  • 89
22
votes
1 answer

Fast n-dimensional sparse array in Python / Cython

I have an application that involves large n-dimensional arrays which are very sparse. scipy.sparse has a useful 'vectorized getting and setting' feature, so that Cython can be used to populate a sparse matrix quickly. Of course the scipy package…
Neal Hughes
  • 502
  • 3
  • 13
21
votes
3 answers

Decode sparse json object to php array

I can create a sparse php array (or map) using the command: $myarray = array(10=>'hi','test20'=>'howdy'); I want to serialize/deserialize this as JSON. I can serialize it using the command: $json = json_encode($myarray); which results in the…
Isaac Sutherland
  • 2,780
  • 4
  • 25
  • 36
15
votes
3 answers

Sparse array support in HDF5

I need to store a 512^3 array on disk in some way and I'm currently using HDF5. Since the array is sparse a lot of disk space gets wasted. Does HDF5 provide any support for sparse array ?
andreabedini
  • 1,255
  • 1
  • 12
  • 20
15
votes
8 answers

Best way to store SparseBooleanArray in Bundle?

When a config change happens, my ListView Checkbox states get lost, which I understand why. I try to implement public void onSaveInstanceState(final Bundle outState) in one of my Fragments. So I'm just wondering what's the easiest way to store my…
Kitesurfer
  • 3,263
  • 2
  • 25
  • 41
14
votes
4 answers

Android SparseArray with String key?

I need to use a hashmap to store key / values in my Android app (potentially thousands), but I understand that I should be using SparseArray in order to save memory. However, my key needs to be a String. Is there a way to create a custom…
Mike6679
  • 4,831
  • 15
  • 55
  • 104
12
votes
1 answer

Multidimensional sparse matrices in Julia

Why aren't there any multidimensional sparse matrices/arrays in Julia? Why can we only have 2D sparse matrices and not for example 3D sparse matrices (or arrays)?
12.yakir
  • 273
  • 2
  • 8
11
votes
7 answers

java: sparse bit vector

Are there any well-known libraries in Java for sparse bit vectors? (And are there guidelines for how sparse is useful to use them vs. java.util.BitSet?)
Jason S
  • 171,795
  • 155
  • 551
  • 900
10
votes
3 answers

Load sparse array from npy file

I am trying load a sparse array that I have previously saved. Saving the sparse array was easy enough. Trying to read it though is a pain. scipy.load returns a 0d array around my sparse array. import scipy as sp A = sp.load("my_array");…
iform
  • 213
  • 1
  • 4
  • 9
10
votes
5 answers

Memory-efficient sparse array in Java

(There are some questions about time-efficient sparse arrays but I am looking for memory efficiency.) I need the equivalent of a List or Map which Can grow on demand just by setting a key larger than any encountered before. (Can…
Jesse Glick
  • 22,072
  • 9
  • 77
  • 100
9
votes
3 answers

Android: Is there an idiom for Iterating through a SparseArray

I'm using a list of unique int ids against a list of user names as a fast lookup table and decided to use the sparseArray but I would like to be able print to log the entire list from time to time for debugging purposes. The SparseArray is not…
user245357
  • 121
  • 1
  • 6
9
votes
2 answers

How should I iterate over a sparse array in index order?

I have a sparse array whose contents aren't guaranteed to be inserted in index order but need to be iterated through in index order. To iterate through a sparse array I understand that you need to use a for..in statement. However, according to this…
Chris Simpson
  • 7,411
  • 8
  • 42
  • 66
1
2 3 4 5 6 7