What .NET dictionary supports a “find nearest key” operation?

后端 未结 8 696
[愿得一人]
[愿得一人] 2020-12-18 23:08

I\'m converting some C++ code to C# and it calls std::map::lower_bound(k) to find an entry in the map whose key is equal to or greater than k. However, I don\'t see any way

8条回答
  •  北海茫月
    2020-12-18 23:40

    I created several data structures that provide this functionality for any data type: BList (a sorted list), BDictionary (a dictionary whose items are sorted by key), and BMultiMap (a dictionary in which more than one value can be associated with a key). See this article for details. Each of these data structures provide FindLowerBound() and FindUpperBound() methods that work like C++'s lower_bound and upper_bound. Internally, these collections are similar to B+ trees, so they have good performance and low memory usage; BDictionary<,> typically uses about 44% less memory than a standard SortedDictionary<,> (which in turn uses, on average, slightly less memory than Dictionary<,>), assuming 64-bit keys and 64-bit values.

    I also made a "sparse" collection, SparseAList, which is similar to BDictionary except that you can insert and remove "empty space" anywhere in the collection (empty space does not consume any memory). See this article for details.

    All of these collections are in the Loyc.Collections NuGet package.

提交回复
热议问题