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

/DICK.B1/IronPython/Runtime/StringDictionaryStorage.cs

https://bitbucket.org/williamybs/uidipythontool
C# | 197 lines | 147 code | 35 blank | 15 comment | 44 complexity | 594becf9dc73d1bf1b4f87a31f891d43 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. using System;
  16. using System.Collections.Generic;
  17. using System.Diagnostics;
  18. using Microsoft.Scripting;
  19. using System.Threading;
  20. namespace IronPython.Runtime {
  21. [Serializable]
  22. internal class StringDictionaryStorage : DictionaryStorage {
  23. private Dictionary<string, object> _data;
  24. public StringDictionaryStorage() {
  25. }
  26. public StringDictionaryStorage(int count) {
  27. _data = new Dictionary<string, object>(count, StringComparer.Ordinal);
  28. }
  29. public override void Add(object key, object value) {
  30. lock (this) {
  31. AddNoLock(key, value);
  32. }
  33. }
  34. public override void AddNoLock(object key, object value) {
  35. EnsureData();
  36. string strKey = key as string;
  37. if (strKey != null) {
  38. _data[strKey] = value;
  39. } else {
  40. GetObjectDictionary()[key] = value;
  41. }
  42. }
  43. public override bool Contains(object key) {
  44. if (_data == null) return false;
  45. lock (this) {
  46. string strKey = key as string;
  47. if (strKey != null) {
  48. return _data.ContainsKey(strKey);
  49. } else {
  50. Dictionary<object, object> dict = TryGetObjectDictionary();
  51. if (dict != null) {
  52. return dict.ContainsKey(key);
  53. }
  54. return false;
  55. }
  56. }
  57. }
  58. public override bool Remove(object key) {
  59. if (_data == null) return false;
  60. lock (this) {
  61. string strKey = key as string;
  62. if (strKey != null) {
  63. return _data.Remove(strKey);
  64. } else {
  65. Dictionary<object, object> dict = TryGetObjectDictionary();
  66. if (dict != null) {
  67. return dict.Remove(key);
  68. }
  69. return false;
  70. }
  71. }
  72. }
  73. public override bool TryGetValue(object key, out object value) {
  74. if (_data != null) {
  75. lock (this) {
  76. string strKey = key as string;
  77. if (strKey != null) {
  78. return _data.TryGetValue(strKey, out value);
  79. }
  80. Dictionary<object, object> dict = TryGetObjectDictionary();
  81. if (dict != null) {
  82. return dict.TryGetValue(key, out value);
  83. }
  84. }
  85. }
  86. value = null;
  87. return false;
  88. }
  89. public override int Count {
  90. get {
  91. if (_data == null) return 0;
  92. lock (this) {
  93. if (_data == null) return 0;
  94. int count = _data.Count;
  95. Dictionary<object, object> dict = TryGetObjectDictionary();
  96. if (dict != null) {
  97. // plus the object keys, minus the object dictionary key
  98. count += dict.Count - 1;
  99. }
  100. return count;
  101. }
  102. }
  103. }
  104. public override void Clear() {
  105. _data = null;
  106. }
  107. public override List<KeyValuePair<object, object>> GetItems() {
  108. List<KeyValuePair<object, object>> res = new List<KeyValuePair<object, object>>();
  109. if (_data != null) {
  110. lock (this) {
  111. foreach (KeyValuePair<string, object> kvp in _data) {
  112. if (String.IsNullOrEmpty(kvp.Key)) continue;
  113. res.Add(new KeyValuePair<object, object>(kvp.Key, kvp.Value));
  114. }
  115. Dictionary<object, object> dataDict = TryGetObjectDictionary();
  116. if (dataDict != null) {
  117. foreach (KeyValuePair<object, object> kvp in GetObjectDictionary()) {
  118. res.Add(kvp);
  119. }
  120. }
  121. }
  122. }
  123. return res;
  124. }
  125. public override bool HasNonStringAttributes() {
  126. if (_data != null) {
  127. lock (this) {
  128. if (TryGetObjectDictionary() != null) {
  129. return true;
  130. }
  131. }
  132. }
  133. return false;
  134. }
  135. private Dictionary<object, object> TryGetObjectDictionary() {
  136. if (_data != null) {
  137. object dict;
  138. if (_data.TryGetValue(string.Empty, out dict)) {
  139. return (Dictionary<object, object>)dict;
  140. }
  141. }
  142. return null;
  143. }
  144. private Dictionary<object, object> GetObjectDictionary() {
  145. lock (this) {
  146. EnsureData();
  147. object dict;
  148. if (_data.TryGetValue(string.Empty, out dict)) {
  149. return (Dictionary<object, object>)dict;
  150. }
  151. Dictionary<object, object> res = new Dictionary<object, object>();
  152. _data[string.Empty] = res;
  153. return res;
  154. }
  155. }
  156. private void EnsureData() {
  157. if (_data == null) {
  158. _data = new Dictionary<string, object>();
  159. }
  160. }
  161. }
  162. }