MruPropertyCache.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. namespace Jint.Runtime
  4. {
  5. public class MruPropertyCache<TKey, TValue> : IDictionary<TKey, TValue>
  6. {
  7. private IDictionary<TKey, TValue> _dictionary = new Dictionary<TKey, TValue>();
  8. private LinkedList<KeyValuePair<TKey, TValue>> _list;
  9. private uint _length;
  10. public MruPropertyCache(uint length) {
  11. _length = length;
  12. _list = new LinkedList<KeyValuePair<TKey, TValue>>();
  13. for(int i=0; i<length; i++) {
  14. _list.AddLast(new KeyValuePair<TKey, TValue>(default(TKey), default(TValue)));
  15. }
  16. }
  17. private bool Find(TKey key, out LinkedListNode<KeyValuePair<TKey, TValue>> result) {
  18. result = _list.First;
  19. while(result != null) {
  20. if(key.Equals(result.Value.Key)) {
  21. return true;
  22. }
  23. result = result.Next;
  24. }
  25. return false;
  26. }
  27. public TValue this[TKey key] {
  28. get {
  29. LinkedListNode<KeyValuePair<TKey, TValue>> node;
  30. if(Find(key, out node)) {
  31. return node.Value.Value;
  32. }
  33. return _dictionary[key];
  34. }
  35. set {
  36. LinkedListNode<KeyValuePair<TKey, TValue>> node;
  37. if (!Find(key, out node)) {
  38. _list.AddFirst(new KeyValuePair<TKey, TValue>(key, value));
  39. _list.RemoveLast();
  40. }
  41. else
  42. {
  43. node.Value = new KeyValuePair<TKey, TValue>(key, value);
  44. }
  45. _dictionary[key] = value;
  46. }
  47. }
  48. public int Count {
  49. get {
  50. return _dictionary.Count;
  51. }
  52. }
  53. public bool IsReadOnly {
  54. get {
  55. return _dictionary.IsReadOnly;
  56. }
  57. }
  58. public ICollection<TKey> Keys {
  59. get {
  60. return _dictionary.Keys;
  61. }
  62. }
  63. public ICollection<TValue> Values {
  64. get {
  65. return _dictionary.Values;
  66. }
  67. }
  68. public void Add(KeyValuePair<TKey, TValue> item) {
  69. LinkedListNode<KeyValuePair<TKey, TValue>> node;
  70. if (!Find(item.Key, out node)) {
  71. _list.AddFirst(item);
  72. _list.RemoveLast();
  73. }
  74. _dictionary.Add(item);
  75. }
  76. public void Add(TKey key, TValue value) {
  77. LinkedListNode<KeyValuePair<TKey, TValue>> node;
  78. if (!Find(key, out node)) {
  79. _list.AddFirst(new KeyValuePair<TKey, TValue>(key, value));
  80. _list.RemoveLast();
  81. }
  82. _dictionary.Add(key, value);
  83. }
  84. public void Clear() {
  85. _list.Clear();
  86. _dictionary.Clear();
  87. }
  88. public bool Contains(KeyValuePair<TKey, TValue> item) {
  89. LinkedListNode<KeyValuePair<TKey, TValue>> node;
  90. if (Find(item.Key, out node)) {
  91. return true;
  92. }
  93. return _dictionary.Contains(item);
  94. }
  95. public bool ContainsKey(TKey key) {
  96. LinkedListNode<KeyValuePair<TKey, TValue>> node;
  97. if (Find(key, out node)) {
  98. return true;
  99. }
  100. return _dictionary.ContainsKey(key);
  101. }
  102. public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) {
  103. _dictionary.CopyTo(array, arrayIndex);
  104. }
  105. public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() {
  106. return _dictionary.GetEnumerator();
  107. }
  108. public bool Remove(KeyValuePair<TKey, TValue> item) {
  109. LinkedListNode<KeyValuePair<TKey, TValue>> node;
  110. if (Find(item.Key, out node))
  111. {
  112. _list.Remove(node);
  113. }
  114. return _dictionary.Remove(item);
  115. }
  116. public bool Remove(TKey key) {
  117. LinkedListNode<KeyValuePair<TKey, TValue>> node;
  118. if (Find(key, out node))
  119. {
  120. _list.Remove(node);
  121. }
  122. return _dictionary.Remove(key);
  123. }
  124. public bool TryGetValue(TKey key, out TValue value) {
  125. LinkedListNode<KeyValuePair<TKey, TValue>> node;
  126. if (Find(key, out node)) {
  127. value = node.Value.Value;
  128. return true;
  129. }
  130. return _dictionary.TryGetValue(key, out value);
  131. }
  132. IEnumerator IEnumerable.GetEnumerator() {
  133. return _dictionary.GetEnumerator();
  134. }
  135. }
  136. }