I have a List<KeyValuePair<double, double>>, the list is sorted by KeyValuePair.Key, so it’s amendable to binary search. And I have a double object. Now, my task is to find the index of the double object. Here are the conditions that apply:
- If that
doubleobject matches one of theKeyValuePair.Keywithin a specified tolerance, then the correspondingKeyValuePair.Valueshould be returned. - If the
doubleobject falls outside the max and min range ofKeyValuePair.Key, then a 0 should be returned. - If the
doubleobject falls within the max min of theKeyValuePair.Key, but not matched any of theKeyValuePair.Keywithin a specified tolerance, then the get the average of the nearest upper and nearest lowerKeyValuePair.Value( as measured byKeyValuePair.Key).
I know that a binary search implementation is available in C#, but it is not exactly suited to my needs. I would like to ask is there any implementation out there that already meets my needs? I don’t want to spend a few hours writing and debugging the code that other people has already written, debugged and perfected.
This can be done fairly easily with a comparer and a small wrapper around
List<T>.BinarySearch: