Circular Buffer Implementation on C#


SUBMITTED BY: Guest

DATE: Nov. 11, 2013, 9:08 p.m.

FORMAT: Text only

SIZE: 3.2 kB

HITS: 1105

  1. using System;
  2. /* Circular Buffer Generic Implementation
  3. * By KDERazorback (http://twitter.com/kderazorback)
  4. *
  5. * Fast and Lightweight, Generic Circular Buffer Implementation, supports for bidirectional rotations
  6. * Free to use or modify! Just keep me on the comments!
  7. */
  8. /// <summary>
  9. /// Used to store a Circular Buffer of objects with a particular size, that rotates when an item is added to the collection.
  10. /// </summary>
  11. class CircularBuffer<T>
  12. {
  13. /// <summary>
  14. /// Creates a new Circular Buffer with the specified Capacity
  15. /// </summary>
  16. /// <param name="capacity">Total elements that can be stored inside the buffer before it starts discarding items</param>
  17. public CircularBuffer(int capacity)
  18. {
  19. plainBuffer = new T[capacity];
  20. _startIndex = 0;
  21. }
  22. private T[] plainBuffer;
  23. private int _startIndex; // Stores the start of the Circular Buffer
  24. /// <summary>
  25. /// Stores the current Capacity of this Buffer
  26. /// </summary>
  27. public int Capacity
  28. {
  29. get { return plainBuffer.Length; }
  30. }
  31. /// <summary>
  32. /// Returns the item that is stored on the specified Index inside the Circular Buffer
  33. /// </summary>
  34. /// <param name="index">Index of the Item to be returned</param>
  35. /// <returns>The object value stored on the specified index</returns>
  36. public T ElementAt(int index)
  37. {
  38. if ((index >= plainBuffer.Length) || (index < 0))
  39. throw new IndexOutOfRangeException();
  40. index += _startIndex;
  41. if (index >= plainBuffer.Length)
  42. index -= plainBuffer.Length;
  43. return plainBuffer[index];
  44. }
  45. /// <summary>
  46. /// Returns an array with the full content of the actual Circular Buffer
  47. /// </summary>
  48. /// <returns></returns>
  49. public T[] ToArray()
  50. {
  51. int i;
  52. T[] output = new T[plainBuffer.Length];
  53. for (i = 0; i < plainBuffer.Length; i++)
  54. {
  55. output[i] = ElementAt(i);
  56. }
  57. return output;
  58. }
  59. /// <summary>
  60. /// Inserts a new item inside the Circular Buffer and rotates the entire structure by one step forwards
  61. /// </summary>
  62. /// <param name="newItem">New item to be inserted into the Circular Buffer</param>
  63. public void Insert(T newItem)
  64. {
  65. if (_startIndex == 0)
  66. _startIndex = plainBuffer.Length - 1;
  67. else
  68. _startIndex--;
  69. plainBuffer[_startIndex] = newItem;
  70. }
  71. /// <summary>
  72. /// Inserts a new item inside the Circular Buffer and rotates the entire structure by one step backwards
  73. /// </summary>
  74. /// <param name="newItem">New item to be inserted into the Circular Buffer</param>
  75. public void InsertBackwards(T newItem)
  76. {
  77. plainBuffer[_startIndex] = newItem;
  78. if (_startIndex == plainBuffer.Length - 1)
  79. _startIndex = 0;
  80. else
  81. _startIndex++;
  82. }
  83. }

comments powered by Disqus