Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there sparse array implementation in .NET library?

Is there already implemented data structure in .NET library acting like sparse array (where most indexes are empty) with O(1) access by index and O(1) access to next (and previous) element?

like image 593
zduny Avatar asked Jul 01 '12 18:07

zduny


1 Answers

A couple of years ago I implemented a sparse collection based on my "AList" concept. It's called SparseAList, and it's probably better than any "simple" solution that you might roll yourself. For example, @NathanPhilips' solution has Insert and RemoveAt methods that call ToDictionary. Enumerable.ToDictionary is an O(N) method - it regenerates the whole dictionary "from scratch" - so it's not efficient.

SparseAList, by contrast, is based on the B+ tree, so it has efficient O(log N) insertions, lookups and removals, and it also uses memory efficiently. It is included in Loyc.Collections.dll in LoycCore, available on NuGet (search for Loyc.Collections).

like image 185
Qwertie Avatar answered Sep 21 '22 17:09

Qwertie