sparse-array

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

a 夏天 提交于 2019-12-01 16:32:51
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 article : There is no guarantee that for...in will return the indexes in any particular order But stackoverflow questions like this suggest that whilst object property orders are not guaranteed, array orders are: properties order in objects are not guaranted in JavaScript, you need to use an Array. I tested this in the latest versions of Chrome,

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

痞子三分冷 提交于 2019-12-01 15:31:22
问题 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 article: There is no guarantee that for...in will return the indexes in any particular order But stackoverflow questions like this suggest that whilst object property orders are not guaranteed, array orders are: properties order in objects are not

How to represent a sparse array in JSON?

痞子三分冷 提交于 2019-12-01 08:56:47
I've got a sparse array that I want to represent in JSON. For example: -10 => 100 -1 => 102 3 => 44 12 => -87 12345 => 0 How can I do this? Can I do this? You can represent it as a simple object: { "-10" : 100, "-1" : 102, "3" : 44, "12" : -87, "12345" : 0 } Since it will be a simple object, you cannot iterate it the same way as an array, but you can use the for...in statement: for (var key in obj) { if (obj.hasOwnProperty(key)) { var value = obj[key]; } } And if you want to access an specific element by key, you can use also here the square bracket property accessor : obj['-10']; // 100 Note

How to represent a sparse array in JSON?

好久不见. 提交于 2019-12-01 07:19:46
问题 I've got a sparse array that I want to represent in JSON. For example: -10 => 100 -1 => 102 3 => 44 12 => -87 12345 => 0 How can I do this? Can I do this? 回答1: You can represent it as a simple object: { "-10" : 100, "-1" : 102, "3" : 44, "12" : -87, "12345" : 0 } Since it will be a simple object, you cannot iterate it the same way as an array, but you can use the for...in statement: for (var key in obj) { if (obj.hasOwnProperty(key)) { var value = obj[key]; } } And if you want to access an

Does null occupy memory in javascript?

霸气de小男生 提交于 2019-11-30 04:41:45
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 one, but not every element of large requires an associated element in small and so I set it to null . However, I do still need to keep the indices the same so I can do things like large[16].id + ': ' + small[16].description . Does the fact that I've got an array that's mostly null in value result in increased memory usage? My question is whether or not

Sparse array compression using SIMD (AVX2)

痴心易碎 提交于 2019-11-29 07:15:10
I have a sparse array a (mostly zeroes): unsigned char a[1000000]; and I would like to create an array b of indexes to non-zero elements of a using SIMD instructions on Intel x64 architecture with AVX2. I'm looking for tips how to do it efficiently. Specifically, are there SIMD instruction(s) to get positions of consecutive non-zero elements in SIMD register, arranged contiguously? wim Five methods to compute the indices of the nonzeros are: Semi vectorized loop: Load a SIMD vector with chars, compare with zero and apply a movemask. Use a small scalar loop if any of the chars is nonzero (also

Best way to store SparseBooleanArray in Bundle?

半腔热情 提交于 2019-11-29 02:33:57
问题 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 SparseBooleanArray in the outState . Also, I'm a bit confused, as ListView has the method: getListView().getCheckedItemPositions(); What's this good for? 回答1: In my case, I ended up doing it by implementing a Parcelable wrapper around the

Does null occupy memory in javascript?

夙愿已清 提交于 2019-11-29 01:55:39
问题 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 one, but not every element of large requires an associated element in small and so I set it to null . However, I do still need to keep the indices the same so I can do things like large[16].id + ': ' + small[16].description . Does the fact that I've

Condensing a sparse array in Javascript?

隐身守侯 提交于 2019-11-28 13:55:41
I have an array of elements where the entries are sparse. How can I easily condense the sparse array into a dense array so that I don't have to keep checking for null and undefined values every time I loop through the data? Here is some example data: var sparse = []; sparse[1] = undefined; sparse[5] = 3; sparse[10] = null; var dense = sparseToDenseArray(sparse); // dense should be [3] In vanilla JS, works on all browsers: function filt(a) { var b = []; for(var i = 0;i < a.length;i++) { if (a[i] !== undefined && a[i] !== null) { b.push(a[i]); } } return b; } > filt([1,undefined,3]) [1, 3] You

Memory-efficient sparse array in Java

时光毁灭记忆、已成空白 提交于 2019-11-27 02:05:55
(There are some questions about time-efficient sparse arrays but I am looking for memory efficiency.) I need the equivalent of a List<T> or Map<Integer,T> which Can grow on demand just by setting a key larger than any encountered before. (Can assume keys are nonnegative.) Is about as memory-efficient as an ArrayList<T> in the case that most of the indices are not null , i.e. when the actual data is not very sparse. When the indices are sparse, consumes space proportional to the number of non- null indices. Uses less memory than HashMap<Integer,T> (as this autoboxes the keys and probably does