Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SortedDictionary - getting next value

I am using SortedDictionary to store values sorted by integer.

I need to get next value after specific existing integer. I would prefer working with enumerator but there is no GetEnumerator(Key k) or similar function.

SortedDictionary<int, MyClass> _dict;


void GetNextValue(int start, out MyClass ret)
{
}
like image 580
Captain Comic Avatar asked Feb 11 '11 11:02

Captain Comic


People also ask

How does SortedDictionary work c#?

In C#, SortedDictionary is a generic collection which is used to store the key/value pairs in the sorted form and the sorting is done on the key. SortedDictionary is defined under System. Collection. Generic namespace.

What is SortedList?

A SortedList object internally maintains two arrays to store the elements of the list; that is, one array for the keys and another array for the associated values. Each element is a key/value pair that can be accessed as a DictionaryEntry object. A key cannot be null , but a value can be.

Which collection type represents a collection of key and value pairs that are Sorted by keys and are accessible by keys and values?

A SortedList represents a collection of objects stored as key-value pairs that are sorted by the keys.

How to get value of SortedDictionary in c#?

C# | SortedDictionary. This method is used to check whether the SortedDictionary<TKey, TValue> contains an element with the specified value or not. Syntax: public bool ContainsValue (TValue value); Here, the value is the Value to locate in the SortedDictionary.


1 Answers

With reference to the link that Ani added, in this case it would be something like:

ret = source.SkipWhile(pair => pair.Key <= start).First().Value;

or maybe (to allow a Try-style usage)

using(var iter = source.GetEnumerator()) {
    while(iter.MoveNext()) {
        if(iter.Current.Key > start) {
             ret = iter.Current.Value;
             return true;
        }
    }
    ret = null;
    return false;
}
like image 93
Marc Gravell Avatar answered Oct 14 '22 20:10

Marc Gravell