using System.Collections.Generic;
namespace MoreMountains.Tools
{
///
/// A improved list that lets you parse it and automatically have it loop to the start or end when you reach the end or start
/// To use it : set the CurrentIndex to whatever you want, then use IncrementCurrentIndex / DecrementCurrentIndex to move it, get the current element via Current
///
///
public class MMCircularList : List
{
private int _currentIndex = 0;
///
/// Lets you set the current index, or compute it if you get it
///
public int CurrentIndex
{
get
{
return GetCurrentIndex();
}
set => _currentIndex = value;
}
///
/// Computes the current index
///
///
protected virtual int GetCurrentIndex()
{
if (_currentIndex > Count - 1) { _currentIndex = 0; }
if (_currentIndex < 0) { _currentIndex = Count - 1; }
return _currentIndex;
}
///
/// Returns the current element
///
public T Current => this[CurrentIndex];
///
/// Increments the current index (towards the "right" of the list)
///
public virtual void IncrementCurrentIndex()
{
_currentIndex++;
GetCurrentIndex();
}
///
/// Decrements the current index (towards the "left" of the list)
///
public virtual void DecrementCurrentIndex()
{
_currentIndex--;
GetCurrentIndex();
}
///
/// Returns the previous index in the circular list
///
public virtual int PreviousIndex => (_currentIndex == 0) ? Count - 1 : _currentIndex - 1;
///
/// Returns the next index in the circular list
///
public virtual int NextIndex => (_currentIndex == Count - 1) ? 0 : _currentIndex + 1;
}
}