Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
4.5KB

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace Svelto.DataStructures
  5. {
  6. // Serves as simple circular buffer dictionary, first in, first out
  7. // Main drawback: it is the oldest in the list that is removed and the fact that we might re access a key
  8. // isn't taken into account (we would have to do a shift in both arrays)
  9. // Could be added as an option?
  10. public class CircularBufferIndexer<TKey, TVal> : IDictionary<TKey, TVal>
  11. {
  12. public ICollection<TKey> Keys
  13. {
  14. get { return _keys; }
  15. }
  16. public ICollection<TVal> Values
  17. {
  18. get { return _values; }
  19. }
  20. public int Count
  21. {
  22. get { throw new NotImplementedException(); }
  23. }
  24. public bool IsReadOnly
  25. {
  26. get { throw new NotImplementedException(); }
  27. }
  28. public CircularBufferIndexer(int size)
  29. {
  30. _keys = new TKey[size];
  31. _values = new TVal[size];
  32. _length = _startIndex = _nextIndex = 0;
  33. }
  34. public TVal this[TKey key]
  35. {
  36. get
  37. {
  38. int index = _startIndex;
  39. for (int i = 0; i < _length; ++i)
  40. {
  41. if (_keys[index].Equals(key))
  42. {
  43. return _values[index];
  44. }
  45. index = NextPosition(index);
  46. }
  47. throw new KeyNotFoundException();
  48. }
  49. set
  50. {
  51. int index = _startIndex;
  52. for (int i = 0; i < _length; ++i)
  53. {
  54. if (_keys[index].Equals(key))
  55. {
  56. _values[index] = value;
  57. return;
  58. }
  59. index = NextPosition(index);
  60. }
  61. throw new KeyNotFoundException();
  62. }
  63. }
  64. public void Add(TKey key, TVal value)
  65. {
  66. if (ContainsKey(key))
  67. {
  68. this[key] = value;
  69. return;
  70. }
  71. _keys[_nextIndex] = key;
  72. _values[_nextIndex] = value;
  73. _nextIndex = NextPosition(_nextIndex);
  74. if (IsFull())
  75. {
  76. _startIndex = NextPosition(_startIndex);
  77. }
  78. else
  79. {
  80. ++_length;
  81. }
  82. }
  83. public bool ContainsKey(TKey key)
  84. {
  85. int index = _startIndex;
  86. for (int i = 0; i < _length; ++i)
  87. {
  88. if (_keys[index].Equals(key))
  89. {
  90. return true;
  91. }
  92. index = NextPosition(index);
  93. }
  94. return false;
  95. }
  96. public bool Remove(TKey key)
  97. {
  98. throw new NotImplementedException();
  99. }
  100. public bool TryGetValue(TKey key, out TVal value)
  101. {
  102. value = default(TVal);
  103. int index = _startIndex;
  104. for (int i = 0; i < _length; ++i)
  105. {
  106. if (_keys[index].Equals(key))
  107. {
  108. value = _values[index];
  109. return true;
  110. }
  111. index = NextPosition(index);
  112. }
  113. return false;
  114. }
  115. public IEnumerator GetEnumerator()
  116. {
  117. throw new NotImplementedException();
  118. }
  119. public void Add(KeyValuePair<TKey, TVal> item)
  120. {
  121. Add(item.Key, item.Value);
  122. }
  123. public void Clear()
  124. {
  125. throw new NotImplementedException();
  126. }
  127. public bool Contains(KeyValuePair<TKey, TVal> item)
  128. {
  129. throw new NotImplementedException();
  130. }
  131. public void CopyTo(KeyValuePair<TKey, TVal>[] array, int arrayIndex)
  132. {
  133. throw new NotImplementedException();
  134. }
  135. public bool Remove(KeyValuePair<TKey, TVal> item)
  136. {
  137. throw new NotImplementedException();
  138. }
  139. IEnumerator<KeyValuePair<TKey, TVal>> IEnumerable<KeyValuePair<TKey, TVal>>.GetEnumerator()
  140. {
  141. throw new NotImplementedException();
  142. }
  143. int NextPosition(int position)
  144. {
  145. return (position + 1) % _keys.Length;
  146. }
  147. bool IsFull()
  148. {
  149. return _length == _values.Length;
  150. }
  151. TKey[] _keys;
  152. TVal[] _values;
  153. int _startIndex;
  154. int _nextIndex;
  155. int _length;
  156. }
  157. }