PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Microsoft.Scripting/Utils/CacheDict.cs

https://bitbucket.org/stefanrusek/xronos
C# | 117 lines | 66 code | 11 blank | 40 comment | 8 complexity | b895f1da32e439c6c6021cc64893b91a 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. namespace Microsoft.Scripting.Utils {
  24. /// <summary>
  25. /// Provides a dictionary-like object used for caches which holds onto a maximum
  26. /// number of elements specified at construction time.
  27. ///
  28. /// This class is not thread safe.
  29. /// </summary>
  30. public class CacheDict<TKey, TValue> {
  31. private readonly Dictionary<TKey, KeyInfo> _dict = new Dictionary<TKey, KeyInfo>();
  32. private readonly LinkedList<TKey> _list = new LinkedList<TKey>();
  33. private readonly int _maxSize;
  34. /// <summary>
  35. /// Creates a dictionary-like object used for caches.
  36. /// </summary>
  37. /// <param name="maxSize">The maximum number of elements to store.</param>
  38. public CacheDict(int maxSize) {
  39. _maxSize = maxSize;
  40. }
  41. /// <summary>
  42. /// Tries to get the value associated with 'key', returning true if it's found and
  43. /// false if it's not present.
  44. /// </summary>
  45. public bool TryGetValue(TKey key, out TValue value) {
  46. KeyInfo storedValue;
  47. if (_dict.TryGetValue(key, out storedValue)) {
  48. LinkedListNode<TKey> node = storedValue.List;
  49. if (node.Previous != null) {
  50. // move us to the head of the list...
  51. _list.Remove(node);
  52. _list.AddFirst(node);
  53. }
  54. value = storedValue.Value;
  55. return true;
  56. }
  57. value = default(TValue);
  58. return false;
  59. }
  60. /// <summary>
  61. /// Adds a new element to the cache, replacing and moving it to the front if the
  62. /// element is already present.
  63. /// </summary>
  64. public void Add(TKey key, TValue value) {
  65. KeyInfo keyInfo;
  66. if (_dict.TryGetValue(key, out keyInfo)) {
  67. // remove original entry from the linked list
  68. _list.Remove(keyInfo.List);
  69. } else if (_list.Count == _maxSize) {
  70. // we've reached capacity, remove the last used element...
  71. LinkedListNode<TKey> node = _list.Last;
  72. _list.RemoveLast();
  73. bool res = _dict.Remove(node.Value);
  74. Debug.Assert(res);
  75. }
  76. // add the new entry to the head of the list and into the dictionary
  77. LinkedListNode<TKey> listNode = new LinkedListNode<TKey>(key);
  78. _list.AddFirst(listNode);
  79. _dict[key] = new CacheDict<TKey, TValue>.KeyInfo(value, listNode);
  80. }
  81. /// <summary>
  82. /// Returns the value associated with the given key, or throws KeyNotFoundException
  83. /// if the key is not present.
  84. /// </summary>
  85. public TValue this[TKey key] {
  86. get {
  87. TValue res;
  88. if (TryGetValue(key, out res)) {
  89. return res;
  90. }
  91. throw new KeyNotFoundException();
  92. }
  93. set {
  94. Add(key, value);
  95. }
  96. }
  97. private struct KeyInfo {
  98. internal readonly TValue Value;
  99. internal readonly LinkedListNode<TKey> List;
  100. internal KeyInfo(TValue value, LinkedListNode<TKey> list) {
  101. Value = value;
  102. List = list;
  103. }
  104. }
  105. }
  106. }