PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/IronPython_Main/Tools/IronStudio/IronPythonToolsCore/PyAnalysis/Interpreter/SingleDict.cs

#
C# | 167 lines | 124 code | 27 blank | 16 comment | 30 complexity | bddf5b6699f1161b3859cc3bbb77e6a2 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0, CC-BY-SA-3.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1, Apache-2.0
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Apache License, Version 2.0. 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 Apache License, Version 2.0, please send an email to
  8. * ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. * ***************************************************************************/
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Collections;
  19. namespace Microsoft.PyAnalysis.Interpreter {
  20. /// <summary>
  21. /// A simple dictionary like object which has efficient storage when there's only a single item in the dictionary.
  22. /// </summary>
  23. struct SingleDict<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>> {
  24. private object _data; // Dictionary<TKey, TValue> or SingleEntry<TKey, TValue>
  25. sealed class SingleDependency {
  26. public readonly TKey Key;
  27. public TValue Value;
  28. public SingleDependency(TKey key, TValue value) {
  29. Key = key;
  30. Value = value;
  31. }
  32. }
  33. internal bool TryGetValue(TKey fromModule, out TValue deps) {
  34. SingleDependency single = _data as SingleDependency;
  35. if (single != null) {
  36. if (single.Key.Equals(fromModule)) {
  37. deps = single.Value;
  38. return true;
  39. }
  40. deps = default(TValue);
  41. return false;
  42. }
  43. Dictionary<TKey, TValue> dict = _data as Dictionary<TKey, TValue>;
  44. if (_data != null) {
  45. return dict.TryGetValue(fromModule, out deps);
  46. }
  47. deps = default(TValue);
  48. return false;
  49. }
  50. public TValue this[TKey key] {
  51. get {
  52. TValue res;
  53. if (TryGetValue(key, out res)) {
  54. return res;
  55. }
  56. throw new KeyNotFoundException();
  57. }
  58. set {
  59. if (_data == null) {
  60. _data = new SingleDependency(key, value);
  61. return;
  62. }
  63. SingleDependency single = _data as SingleDependency;
  64. if (single != null) {
  65. if (single.Key.Equals(key)) {
  66. single.Value = value;
  67. return;
  68. }
  69. var data = new Dictionary<TKey, TValue>();
  70. data[single.Key] = single.Value;
  71. _data = data;
  72. }
  73. Dictionary<TKey, TValue> dict = _data as Dictionary<TKey, TValue>;
  74. if (dict == null) {
  75. _data = dict = new Dictionary<TKey, TValue>();
  76. }
  77. dict[key] = value;
  78. }
  79. }
  80. internal void Remove(TKey fromModule) {
  81. SingleDependency single = _data as SingleDependency;
  82. if (single != null) {
  83. if (single.Key.Equals(fromModule)) {
  84. single = null;
  85. }
  86. return;
  87. }
  88. Dictionary<TKey, TValue> dict = _data as Dictionary<TKey, TValue>;
  89. if (_data != null) {
  90. dict.Remove(fromModule);
  91. }
  92. }
  93. public IEnumerable<TValue> Values {
  94. get {
  95. SingleDependency single = _data as SingleDependency;
  96. if (single != null) {
  97. yield return single.Value;
  98. }
  99. Dictionary<TKey, TValue> dict = _data as Dictionary<TKey, TValue>;
  100. if (dict != null) {
  101. foreach (var value in dict.Values) {
  102. yield return value;
  103. }
  104. }
  105. }
  106. }
  107. public int Count {
  108. get {
  109. SingleDependency single = _data as SingleDependency;
  110. if (single != null) {
  111. return 1;
  112. }
  113. Dictionary<TKey, TValue> dict = _data as Dictionary<TKey, TValue>;
  114. if (dict != null) {
  115. return dict.Count;
  116. }
  117. return 0;
  118. }
  119. }
  120. #region IEnumerable<KeyValuePair<TKey,TValue>> Members
  121. public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() {
  122. SingleDependency single = _data as SingleDependency;
  123. if (single != null) {
  124. yield return new KeyValuePair<TKey, TValue>(single.Key, single.Value);
  125. }
  126. Dictionary<TKey, TValue> dict = _data as Dictionary<TKey, TValue>;
  127. if (dict != null) {
  128. foreach (var keyValue in dict) {
  129. yield return keyValue;
  130. }
  131. }
  132. }
  133. #endregion
  134. #region IEnumerable Members
  135. IEnumerator IEnumerable.GetEnumerator() {
  136. throw new NotImplementedException();
  137. }
  138. #endregion
  139. }
  140. }