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
3
votes
1 answer

Performing lookups on a sparse Javascript arrays

I have an interesting Javascript task (performed in Node.js, FWIW): I need to take the "weighted median" of a dataset for which I have values (income, in this case) and a weight for each one. For example: income #people 0 5 16000 3 20000 …
Chris Wilson
  • 6,599
  • 8
  • 35
  • 71
3
votes
4 answers

Save SparseBooleanArray to SharedPreferences

For my app, I need to save a simple SparseBooleanArray to memory and read it later. Is there any way to save it using SharedPreferences? I considered using an SQLite database but it seemed overkill for something as simple as this. Some other answers…
Pkmmte
  • 2,822
  • 1
  • 31
  • 41
3
votes
2 answers

Can't add a value at the end of a sparse array in android

I have been playing very well with sparseArray until today. Now it looks sparseArray will not repay my love :( I have to maintain the order of my objects I am storing in a sparseArray. There is only one method setValueAt that don't allow to set the…
ahmadalibaloch
  • 5,851
  • 2
  • 50
  • 59
3
votes
2 answers

How To Create a Linked List in Ascending Order

I am given a sparse-array called "head", which is two-dimensional having an index, and a value. So something like : (3, 100) (6,200) (8,100) I have to insert a node (value, index) into this sparse array in ascending order. So if I am given (2,100),…
2
votes
1 answer

Efficient Int32/Uint32 Sorted Map / Sparse Array

I'm looking for a specialized (and fast) Int32/UInt32 sorted map (that preferably is faster then System.Collections.Generic.SortedDictionary where K is either Int32 or UInt32). It's going to be used as a sparse array, are there any implementations…
thr
  • 19,160
  • 23
  • 93
  • 130
2
votes
1 answer

Declare sparse array in coffeescript

In Javascript I can declare a sparse array like: a = [, 1] But that gives an error in Coffeescript. So how can I create sparse arrays in Coffeescript? I want to do it in a single assignment and not like: a = [] a[1] = 1
priomsrb
  • 2,602
  • 3
  • 26
  • 34
2
votes
1 answer

Stream compaction with Thrust; best practices and fastest way?

I am interested in porting some existing code to use thrust to see if I can speed it up on the GPU with relative ease. What I'm looking to accomplish is a stream compaction operation, where only nonzero elements will be kept. I have this mostly…
Andrew P.
  • 31
  • 4
2
votes
2 answers

Unsorted SparseArray

I have a SparseArray Key value Pair. The Key will be my ID and I'm sorting the value with separately maintained sort order numbers. But when I put data into SparseArray the key is sorted and sort order are changed according to Key. But i need the…
Kavin Prabhu
  • 2,307
  • 2
  • 17
  • 36
2
votes
2 answers

Sparse arrays and reduce in JavaScript

One would think that in JavaScript: var array = [1,2,undefined,4]; is the same as: var array = [1,2]; array.length = 3; array.push(4); but it's not. This code shows it: var array1 = [1,2]; array1.length = 3; array1.push(4); var array2 =…
Cristobal
  • 35
  • 4
2
votes
3 answers

Local maxima in a point cloud

I have a point cloud C, where each point has an associated value. Lets say the points are in 2-d space, so each point can be represented with the triplet (x, y, v). I'd like to find the subset of points which are local maxima. That is, for some…
Peter
  • 12,274
  • 9
  • 71
  • 86
2
votes
1 answer

How to wrap Eigen::SparseMatrix over preexistant 3-standard compress row/colum arrays

NOTE: I allready asked this question, but it was closed because of "too broad" without much explanation. I can't see how this question could be more specific (it deals with a specific class of a specific library for a specific usage...), so I assume…
janou195
  • 1,175
  • 2
  • 10
  • 25
2
votes
1 answer

MATLAB tensor: Append row and fill in NaN instead of 0 for empty elements

I have a tensor T T=ones(2,2,2) T(:,:,1) = 1 1 1 1 T(:,:,2) = 1 1 1 1 Now I want to add an element by doing T(3,3,3)=100 and I get the following result T(:,:,1) = 1 1 0 1 1 0 0 0 0 T(:,:,2) = 1 …
SeanKw
  • 63
  • 1
  • 7
2
votes
1 answer

Draw into custom buffer class [Java]

Short Version: Is it possible to draw (via Graphics2D) into a custom buffer class ('sparse' raster-image)? Longer Version: I'd like to convert a polygon (given by a closed path) into a raster image. But since the polygons can potentially be really…
TomKeegasi
  • 200
  • 8
2
votes
2 answers

Why isn't SparseArray in Android JFC compatible?

So, I'm supposed to use SparseArray instead of HashMap for the sake of performance: However, SparseArray isn't a part of JCF and does not implement Collection nor List nor Map. HashMap, on the other hand, implements Map and provides values() that I…
mawcsco
  • 624
  • 6
  • 18
2
votes
3 answers

What does "SparseArray - there can be gaps in the indices" mean?

Developing a program which uses hash map with integer as keys and objects as values. I keep on getting Lint warning informing SparseArray is more efficient and when I read about the same it was given in this Link that, there can be gaps in the…
Suman
  • 4,221
  • 7
  • 44
  • 64