using System;
using System.Collections;
using System.Collections.Generic;
namespace Unity.Cloud
{
///
/// Represents a cyclical list.
///
/// The type of the items in the list.
public class CyclicalList : IList
{
#region Nested Types
///
/// Represents an enumerator for a cyclical list.
///
private struct Enumerator : IEnumerator
{
#region Constructors
///
/// Creates a new instance of the class.
///
/// The list.
public Enumerator(CyclicalList list)
{
this.list = list;
this.currentIndex = -1;
}
#endregion
#region Fields
private int currentIndex;
private CyclicalList list;
#endregion
#region Properties
///
/// Gets the current item.
///
public T Current
{
get
{
if (this.currentIndex < 0 || this.currentIndex >= this.list.Count)
{
return default(T);
}
return this.list[this.currentIndex];
}
}
///
/// Gets the current item.
///
object IEnumerator.Current
{
get { return this.Current; }
}
#endregion
#region Methods
///
/// Disposes of the enumerator.
///
public void Dispose()
{
// Empty
}
///
/// Moves to the next item.
///
/// A value indicating whether the move was successful.
public bool MoveNext()
{
this.currentIndex++;
return this.currentIndex < this.list.count;
}
///
/// Resets the enumerator.
///
public void Reset()
{
this.currentIndex = 0;
}
#endregion
}
#endregion
#region Constructors
///
/// Creates a new instance of the class.
///
/// The capacity.
public CyclicalList(int capacity)
{
this.items = new T[capacity];
}
#endregion
#region Fields
private int count;
private T[] items;
private int nextPointer;
#endregion
#region Properties
///
/// Gets the capacity.
///
public int Capacity
{
get { return this.items.Length; }
}
///
/// Gets the count.
///
public int Count
{
get { return this.count; }
}
///
/// Gets a value indicating whether the cyclical list is read only.
///
public bool IsReadOnly
{
get { return false; }
}
///
/// Indexes into the cyclical list.
///
/// The index.
/// The item at the specified index.
public T this[int index]
{
get
{
if (index < 0 || index >= this.count)
{
throw new IndexOutOfRangeException();
}
return this.items[this.GetPointer(index)];
}
set
{
if (index < 0 || index >= this.count)
{
throw new IndexOutOfRangeException();
}
this.items[this.GetPointer(index)] = value;
}
}
#endregion
#region Methods
///
/// Adds an item to the cyclical list.
///
/// The item.
public void Add(T item)
{
this.items[this.nextPointer] = item;
this.count++;
if (this.count > this.items.Length)
{
this.count = this.items.Length;
}
this.nextPointer++;
if (this.nextPointer >= this.items.Length)
{
this.nextPointer = 0;
}
}
///
/// Clears the cyclical list.
///
public void Clear()
{
this.count = 0;
this.nextPointer = 0;
}
///
/// Determines whether the cyclical list contains the specified item.
///
/// The item.
/// A value indicating whether the cyclical list contains the specified item.
public bool Contains(T item)
{
foreach (T listItem in this)
{
if (listItem.Equals(item))
{
return true;
}
}
return false;
}
///
/// Copies the cyclical list to an array.
///
/// The array.
/// The array index.
public void CopyTo(T[] array, int arrayIndex)
{
int i = 0;
foreach (T listItem in this)
{
int currentArrayIndex = arrayIndex + i;
if (currentArrayIndex >= array.Length)
{
break;
}
array[currentArrayIndex] = listItem;
i++;
}
}
///
/// Gets the enumerator.
///
/// The enumerator.
public IEnumerator GetEnumerator()
{
return new Enumerator(this);
}
///
/// Gets the enumerator.
///
/// The enumerator.
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
///
/// Gets the next eviction.
///
/// The next eviction.
public T GetNextEviction()
{
return this.items[this.nextPointer];
}
///
/// Gets a pointer.
///
/// The index.
/// The pointer.
private int GetPointer(int index)
{
if (index < 0 || index >= this.count)
{
throw new IndexOutOfRangeException();
}
if (this.count < this.items.Length)
{
return index;
}
return (this.nextPointer + index) % this.count;
}
///
/// Gets the index of the specified item.
///
/// The item.
/// The index of the specified item.
public int IndexOf(T item)
{
int i = 0;
foreach (T listItem in this)
{
if (listItem.Equals(item))
{
return i;
}
i++;
}
return -1;
}
///
/// Inserts an item into the cyclical list. This is a no-op on a cyclical list.
///
/// The index.
/// The item.
public void Insert(int index, T item)
{
if (index < 0 || index >= this.count)
{
throw new IndexOutOfRangeException();
}
}
///
/// Removes an item from the cyclical list.
///
/// The item.
/// A value indicating whether the removal was successful. This is a no-op on a cyclical list.
public bool Remove(T item)
{
return false;
}
///
/// Removes an item from the cyclical list at the specified index. This is a no-op on a cyclical list.
///
/// The index.
public void RemoveAt(int index)
{
if (index < 0 || index >= this.count)
{
throw new IndexOutOfRangeException();
}
}
#endregion
}
}