Implementing a sparse array in C# / fastest way to map integer to a specific bucket/range number

后端 未结 2 1752
我寻月下人不归
我寻月下人不归 2020-12-16 17:42

My initial problem is that I need to implement a very fast, sparse array in C#. Original idea was to use a normal Dictionary and wrap it in

相关标签:
2条回答
  • 2020-12-16 17:56

    I, too, noticed that Dictionary<K,V> is slow when the key is an integer. I don’t know exactly why this is the case, but I wrote a faster hash-table implementation for uint and ulong keys:

    • Efficient32bitHashTable and Efficient64bitHashTable

    Caveats/downsides:

    • The 64-bit one (key is ulong) is generic, but the other one (key is uint) assumes int values because that’s all I needed at the time; I’m sure you can make this generic easily.

    • Currently the capacity determines the size of the hashtable forever (i.e. it doesn’t grow).

    0 讨论(0)
  • 2020-12-16 18:00

    There are a 101 different ways to implement sparse arrays depending on factors like:

    • How many items will be in the array
    • How are the items clustered together
    • Space / speed trade of
    • etc

    Most textbooks have a section on sparse array, just doing a Google comes up with lots of hits. You will then have to translate the code into C#, or just use the code someone else has written, I have found two without much effort (I don't know how good these are)

    • Use Specialty Arrays to Accelerate Your Code
    • SparseArray for C#
    0 讨论(0)
提交回复
热议问题