CyclicalList.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace MonoGame.Extended.Triangulation
  5. {
  6. /// <summary>
  7. /// Implements a List structure as a cyclical list where indices are wrapped.
  8. /// </summary>
  9. /// MIT Licensed: https://github.com/nickgravelyn/Triangulator
  10. /// <typeparam name="T">The Type to hold in the list.</typeparam>
  11. class CyclicalList<T> : List<T>
  12. {
  13. public new T this[int index]
  14. {
  15. get
  16. {
  17. //perform the index wrapping
  18. while (index < 0)
  19. index = Count + index;
  20. if (index >= Count)
  21. index %= Count;
  22. return base[index];
  23. }
  24. set
  25. {
  26. //perform the index wrapping
  27. while (index < 0)
  28. index = Count + index;
  29. if (index >= Count)
  30. index %= Count;
  31. base[index] = value;
  32. }
  33. }
  34. public CyclicalList() { }
  35. public CyclicalList(IEnumerable<T> collection)
  36. : base(collection)
  37. {
  38. }
  39. public new void RemoveAt(int index)
  40. {
  41. Remove(this[index]);
  42. }
  43. }
  44. }