using System;
using System.Collections.Generic;
using System.Text;
namespace MonoGame.Extended.Triangulation
{
///
/// Implements a LinkedList that is both indexable as well as cyclical. Thus
/// indexing into the list with an out-of-bounds index will automatically cycle
/// around the list to find a valid node.
///
/// MIT Licensed: https://github.com/nickgravelyn/Triangulator
class IndexableCyclicalLinkedList : LinkedList
{
///
/// Gets the LinkedListNode at a particular index.
///
/// The index of the node to retrieve.
/// The LinkedListNode found at the index given.
public LinkedListNode this[int index]
{
get
{
//perform the index wrapping
while (index < 0)
index = Count + index;
if (index >= Count)
index %= Count;
//find the proper node
LinkedListNode node = First;
for (int i = 0; i < index; i++)
node = node.Next;
return node;
}
}
///
/// Removes the node at a given index.
///
/// The index of the node to remove.
public void RemoveAt(int index)
{
Remove(this[index]);
}
///
/// Finds the index of a given item.
///
/// The item to find.
/// The index of the item if found; -1 if the item is not found.
public int IndexOf(T item)
{
for (int i = 0; i < Count; i++)
if (this[i].Value.Equals(item))
return i;
return -1;
}
}
}