The solution, unfortunately, requires you to write custom extensions. So, I already did this and uploaded it as an entity: SortedDictionaryExtensions.cs .
It uses the List<T>.BinarySearch by converting a collection of dictionary keys into a list. Then, using the answer here , we determine whether the key exists, and if not, we get the floor and ceiling values โโas a bitwise complement, and then choose which one we need for the method.
Please note that I did not check the effectiveness of this algorithm, but, at first glance, it seems quite good.
You can check it as follows:
SortedDictionary<float, string> neededMap = new SortedDictionary<float, string>(); neededMap.Add(1.0f, "first!"); neededMap.Add(3.0f, "second!"); Console.WriteLine("see how useful this is? (looking up indices that aren't in my map)"); Console.WriteLine(neededMap.FloorEntry(2.0f)); Console.WriteLine(neededMap.CeilingEntry(2.0f));
source share