Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve values from next and previous elements in Sorted List C#

Tags:

c#

I have a sorted list that will pass in two elements and compare the two. Is there a function in the SortedList class in C# that will do a next and previous? I got some help with a .Skip, but since the keys would be variable, how would that work? All I need to do is take in the first element and second element, then skip to the third and fourth, fifth and sixth, etc. I wish it were as simple as LinkedList's ".next.next."

  double velocity = positionList.Values.Skip(1);

Edit: The positionList is type

   <double, HandCoordinate>
   HandCoordinate = {double, double, double}

Does that help?

Thanks!

like image 777
m00nbeam360 Avatar asked Mar 11 '13 09:03

m00nbeam360


1 Answers

The class SortedList inherites IEnumerator, so you can use it:

SortedList list = ...
var listEnumerator = ((IEnumerable)list).GetEnumerator();
Pair<MyType> pair = null
do
{
    pair = Pair.Next<MyType>(listEnumerator);
    ...
}
while(pair != null)

...

class Pair<T>
{
    public T First {get; set;}
    public T Second {get; set;}

    public static Pair<T> Next<T>(IEnumerator enumerator)
    {
        var first = enumerator.Current;
        if(enumerator.MoveNext())
        {
           return new Pair<T>
               {
                   First = (T)first,
                   Second = (T)enumerator.Current,
               }
        }
        return null;
    }
}
like image 144
Warlock Avatar answered Nov 15 '22 01:11

Warlock