MruPropertyCache2.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System.Collections.Generic;
  2. using System.Runtime.CompilerServices;
  3. namespace Jint.Runtime
  4. {
  5. internal class MruPropertyCache2<TValue> where TValue : class
  6. {
  7. private Dictionary<string, TValue> _dictionary;
  8. private bool _set;
  9. private string _key;
  10. private TValue _value;
  11. public TValue this[string key]
  12. {
  13. get
  14. {
  15. if (_set && _key == key)
  16. {
  17. return _value;
  18. }
  19. return _dictionary?[key];
  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(string key, TValue value)
  46. {
  47. EnsureInitialized(key);
  48. _set = true;
  49. _key = key;
  50. _value = value;
  51. _dictionary?.Add(key, value);
  52. }
  53. public void Clear()
  54. {
  55. _set = false;
  56. _key = default(string);
  57. _value = null;
  58. _dictionary?.Clear();
  59. }
  60. public bool ContainsKey(string key)
  61. {
  62. if (_set && key == _key)
  63. {
  64. return true;
  65. }
  66. return _dictionary?.ContainsKey(key) == true;
  67. }
  68. public IEnumerable<KeyValuePair<string, TValue>> GetEnumerator()
  69. {
  70. if (_dictionary == null)
  71. {
  72. if (_set)
  73. {
  74. yield return new KeyValuePair<string, TValue>(_key, _value);
  75. }
  76. yield break;
  77. }
  78. foreach (var pair in _dictionary)
  79. {
  80. yield return pair;
  81. }
  82. }
  83. public void Remove(string key)
  84. {
  85. if (_set && key == _key)
  86. {
  87. _set = false;
  88. _key = null;
  89. _value = null;
  90. }
  91. _dictionary?.Remove(key);
  92. }
  93. public bool TryGetValue(string key, out TValue value)
  94. {
  95. value = null;
  96. if (_set && _key == key)
  97. {
  98. value = _value;
  99. return true;
  100. }
  101. return _dictionary?.TryGetValue(key, out value) == true;
  102. }
  103. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  104. private void EnsureInitialized(string key)
  105. {
  106. if (_set && _key != key)
  107. {
  108. if (_dictionary == null)
  109. {
  110. _dictionary = new Dictionary<string, TValue>();
  111. }
  112. _dictionary[_key] = _value;
  113. }
  114. }
  115. public string[] GetKeys()
  116. {
  117. int size = _set ? 1 : 0;
  118. if (_dictionary != null)
  119. {
  120. size += _dictionary.Count;
  121. }
  122. var keys = new string[size];
  123. int n = 0;
  124. if (_set)
  125. {
  126. keys[n++] = _key;
  127. }
  128. if (_dictionary != null)
  129. {
  130. foreach (var key in _dictionary.Keys)
  131. {
  132. keys[n++] = key;
  133. }
  134. }
  135. return keys;
  136. }
  137. }
  138. }