PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Microsoft.Scripting.Core/Utils/CacheDict.cs

https://bitbucket.org/stefanrusek/xronos
C# | 121 lines | 70 code | 11 blank | 40 comment | 8 complexity | 114ee933280513d12c650d77e7ef8d8b MD5 | raw file
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Microsoft Public License. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Microsoft Public License, please send an email to
  8. * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Microsoft Public License.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. #if CODEPLEX_40
  16. using System;
  17. #else
  18. using System; using Microsoft;
  19. #endif
  20. using System.Collections.Generic;
  21. using System.Text;
  22. using System.Diagnostics;
  23. #if CODEPLEX_40
  24. namespace System.Dynamic.Utils {
  25. #else
  26. namespace Microsoft.Scripting.Utils {
  27. #endif
  28. /// <summary>
  29. /// Provides a dictionary-like object used for caches which holds onto a maximum
  30. /// number of elements specified at construction time.
  31. ///
  32. /// This class is not thread safe.
  33. /// </summary>
  34. internal class CacheDict<TKey, TValue> {
  35. private readonly Dictionary<TKey, KeyInfo> _dict = new Dictionary<TKey, KeyInfo>();
  36. private readonly LinkedList<TKey> _list = new LinkedList<TKey>();
  37. private readonly int _maxSize;
  38. /// <summary>
  39. /// Creates a dictionary-like object used for caches.
  40. /// </summary>
  41. /// <param name="maxSize">The maximum number of elements to store.</param>
  42. internal CacheDict(int maxSize) {
  43. _maxSize = maxSize;
  44. }
  45. /// <summary>
  46. /// Tries to get the value associated with 'key', returning true if it's found and
  47. /// false if it's not present.
  48. /// </summary>
  49. internal bool TryGetValue(TKey key, out TValue value) {
  50. KeyInfo storedValue;
  51. if (_dict.TryGetValue(key, out storedValue)) {
  52. LinkedListNode<TKey> node = storedValue.List;
  53. if (node.Previous != null) {
  54. // move us to the head of the list...
  55. _list.Remove(node);
  56. _list.AddFirst(node);
  57. }
  58. value = storedValue.Value;
  59. return true;
  60. }
  61. value = default(TValue);
  62. return false;
  63. }
  64. /// <summary>
  65. /// Adds a new element to the cache, replacing and moving it to the front if the
  66. /// element is already present.
  67. /// </summary>
  68. internal void Add(TKey key, TValue value) {
  69. KeyInfo keyInfo;
  70. if (_dict.TryGetValue(key, out keyInfo)) {
  71. // remove original entry from the linked list
  72. _list.Remove(keyInfo.List);
  73. } else if (_list.Count == _maxSize) {
  74. // we've reached capacity, remove the last used element...
  75. LinkedListNode<TKey> node = _list.Last;
  76. _list.RemoveLast();
  77. bool res = _dict.Remove(node.Value);
  78. Debug.Assert(res);
  79. }
  80. // add the new entry to the head of the list and into the dictionary
  81. LinkedListNode<TKey> listNode = new LinkedListNode<TKey>(key);
  82. _list.AddFirst(listNode);
  83. _dict[key] = new CacheDict<TKey, TValue>.KeyInfo(value, listNode);
  84. }
  85. /// <summary>
  86. /// Returns the value associated with the given key, or throws KeyNotFoundException
  87. /// if the key is not present.
  88. /// </summary>
  89. internal TValue this[TKey key] {
  90. get {
  91. TValue res;
  92. if (TryGetValue(key, out res)) {
  93. return res;
  94. }
  95. throw new KeyNotFoundException();
  96. }
  97. set {
  98. Add(key, value);
  99. }
  100. }
  101. private struct KeyInfo {
  102. internal readonly TValue Value;
  103. internal readonly LinkedListNode<TKey> List;
  104. internal KeyInfo(TValue value, LinkedListNode<TKey> list) {
  105. Value = value;
  106. List = list;
  107. }
  108. }
  109. }
  110. }