MruPropertyCache2.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System.Collections.Generic;
  2. using System.Runtime.CompilerServices;
  3. namespace Jint.Runtime
  4. {
  5. internal class MruPropertyCache2<TKey, TValue> where TKey : class where TValue : class
  6. {
  7. private Dictionary<TKey, TValue> _dictionary;
  8. private bool _set;
  9. private TKey _key;
  10. private TValue _value;
  11. public TValue this[TKey key]
  12. {
  13. get
  14. {
  15. if (_set && key.Equals(_key))
  16. {
  17. return _value;
  18. }
  19. return _dictionary != null ? _dictionary[key] : null;
  20. }
  21. set
  22. {
  23. EnsureInitialized(key);
  24. _set = true;
  25. _key = key;
  26. _value = value;
  27. if (_dictionary != null)
  28. {
  29. _dictionary[key] = value;
  30. }
  31. }
  32. }
  33. public int Count
  34. {
  35. get
  36. {
  37. int count = _set ? 1 : 0;
  38. if (_dictionary != null)
  39. {
  40. count += _dictionary.Count;
  41. }
  42. return count;
  43. }
  44. }
  45. public void Add(TKey key, TValue value)
  46. {
  47. EnsureInitialized(key);
  48. _set = true;
  49. _key = key;
  50. _value = value;
  51. if (_dictionary != null)
  52. {
  53. _dictionary.Add(key, value);
  54. }
  55. }
  56. public void Clear()
  57. {
  58. _set = false;
  59. _key = default(TKey);
  60. _value = null;
  61. _dictionary?.Clear();
  62. }
  63. public bool ContainsKey(TKey key)
  64. {
  65. if (_set && key.Equals(_key))
  66. {
  67. return true;
  68. }
  69. return _dictionary != null && _dictionary.ContainsKey(key);
  70. }
  71. public IEnumerable<KeyValuePair<TKey, TValue>> GetEnumerator()
  72. {
  73. if (_dictionary == null)
  74. {
  75. if (_set)
  76. {
  77. yield return new KeyValuePair<TKey, TValue>(_key, _value);
  78. }
  79. yield break;
  80. }
  81. foreach (var pair in _dictionary)
  82. {
  83. yield return pair;
  84. }
  85. }
  86. public bool Remove(TKey key)
  87. {
  88. bool removed = false;
  89. if (_set && key.Equals(_key))
  90. {
  91. _set = false;
  92. _key = default(TKey);
  93. _value = null;
  94. removed = true;
  95. }
  96. _dictionary?.Remove(key);
  97. return removed;
  98. }
  99. public bool TryGetValue(TKey key, out TValue value)
  100. {
  101. if (_set && _key.Equals(key))
  102. {
  103. value = _value;
  104. return true;
  105. }
  106. if (_dictionary == null)
  107. {
  108. value = null;
  109. return false;
  110. }
  111. return _dictionary.TryGetValue(key, out value);
  112. }
  113. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  114. private void EnsureInitialized(TKey key)
  115. {
  116. if (_set && !Equals(_key, key))
  117. {
  118. if (_dictionary == null)
  119. {
  120. _dictionary = new Dictionary<TKey, TValue>();
  121. }
  122. _dictionary[_key] = _value;
  123. }
  124. }
  125. public TKey[] GetKeys()
  126. {
  127. int size = _set ? 1 : 0;
  128. if (_dictionary != null)
  129. {
  130. size += _dictionary.Count;
  131. }
  132. var keys = new TKey[size];
  133. int n = 0;
  134. if (_set)
  135. {
  136. keys[n++] = _key;
  137. }
  138. if (_dictionary != null)
  139. {
  140. foreach (var key in _dictionary.Keys)
  141. {
  142. keys[n++] = key;
  143. }
  144. }
  145. return keys;
  146. }
  147. }
  148. }