PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Main/src/DynamicDataDisplay/Common/RingArray.cs

#
C# | 140 lines | 110 code | 30 blank | 0 comment | 4 complexity | a100dc6a9198d5f952fdf2a3fb8e7527 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections.ObjectModel;
  6. using System.Collections;
  7. using System.Collections.Specialized;
  8. namespace Microsoft.Research.DynamicDataDisplay.Common
  9. {
  10. public class RingArray<T> : INotifyCollectionChanged, IList<T>
  11. {
  12. public RingArray(int capacity)
  13. {
  14. this.capacity = capacity;
  15. array = new T[capacity];
  16. }
  17. public void Add(T item)
  18. {
  19. int index = (startIndex + count) % capacity;
  20. if (startIndex + count >= capacity)
  21. {
  22. startIndex++;
  23. }
  24. else
  25. {
  26. count++;
  27. }
  28. array[index] = item;
  29. CollectionChanged.Raise(this);
  30. }
  31. public T this[int index]
  32. {
  33. get { return array[(startIndex + index) % capacity]; }
  34. set
  35. {
  36. array[(startIndex + index) % capacity] = value;
  37. CollectionChanged.Raise(this);
  38. }
  39. }
  40. public void Clear()
  41. {
  42. count = 0;
  43. startIndex = 0;
  44. array = new T[capacity];
  45. }
  46. public IEnumerator<T> GetEnumerator()
  47. {
  48. for (int i = 0; i < count; i++)
  49. {
  50. yield return this[i];
  51. }
  52. }
  53. private int count;
  54. public int Count
  55. {
  56. get { return count; }
  57. }
  58. private T[] array;
  59. private int capacity;
  60. public int Capacity
  61. {
  62. get { return capacity; }
  63. }
  64. private int startIndex = 0;
  65. #region INotifyCollectionChanged Members
  66. public event NotifyCollectionChangedEventHandler CollectionChanged;
  67. #endregion
  68. #region IList<T> Members
  69. public int IndexOf(T item)
  70. {
  71. int index = Array.IndexOf(array, item);
  72. if (index == -1)
  73. return -1;
  74. return (index - startIndex + count) % capacity;
  75. }
  76. public void Insert(int index, T item)
  77. {
  78. throw new NotImplementedException();
  79. }
  80. public void RemoveAt(int index)
  81. {
  82. throw new NotImplementedException();
  83. }
  84. #endregion
  85. #region ICollection<T> Members
  86. public bool Contains(T item)
  87. {
  88. return Array.IndexOf(array, item) > -1;
  89. }
  90. public void CopyTo(T[] array, int arrayIndex)
  91. {
  92. throw new NotImplementedException();
  93. }
  94. public bool IsReadOnly
  95. {
  96. get { throw new NotImplementedException(); }
  97. }
  98. public bool Remove(T item)
  99. {
  100. throw new NotImplementedException();
  101. }
  102. #endregion
  103. #region IEnumerable Members
  104. IEnumerator IEnumerable.GetEnumerator()
  105. {
  106. return GetEnumerator();
  107. }
  108. #endregion
  109. }
  110. }