PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/DICK.B1/IronPython/Runtime/DictionaryStorage.cs

https://bitbucket.org/williamybs/uidipythontool
C# | 130 lines | 67 code | 21 blank | 42 comment | 3 complexity | 71cbb181b14cc9efd9a74f412a9b22dd 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;
  17. using System.Collections.Generic;
  18. using System.Diagnostics;
  19. using Microsoft.Scripting;
  20. namespace IronPython.Runtime {
  21. /// <summary>
  22. /// Abstract base class for all PythonDictionary storage.
  23. ///
  24. /// Defined as a class instead of an interface for performance reasons. Also not
  25. /// using IDictionary* for keeping a simple interface.
  26. ///
  27. /// Full locking is defined as being on the DictionaryStorage object it's self,
  28. /// not an internal member. This enables subclasses to provide their own locking
  29. /// aruond large operations and call lock free functions.
  30. /// </summary>
  31. [Serializable]
  32. internal abstract class DictionaryStorage {
  33. public abstract void Add(object key, object value);
  34. public virtual void AddNoLock(object key, object value) {
  35. Add(key, value);
  36. }
  37. public abstract bool Contains(object key);
  38. public abstract bool Remove(object key);
  39. public abstract bool TryGetValue(object key, out object value);
  40. public virtual bool TryRemoveValue(object key, out object value) {
  41. if (TryGetValue(key, out value)) {
  42. return Remove(key);
  43. }
  44. return false;
  45. }
  46. public abstract int Count { get; }
  47. public abstract void Clear();
  48. public virtual bool HasNonStringAttributes() {
  49. foreach (KeyValuePair<object, object> o in GetItems()) {
  50. if (!(o.Key is string)) {
  51. return true;
  52. }
  53. }
  54. return false;
  55. }
  56. public abstract List<KeyValuePair<object, object>> GetItems();
  57. public virtual IEnumerable<object>/*!*/ GetKeys() {
  58. foreach (var o in GetItems()) {
  59. yield return o.Key;
  60. }
  61. }
  62. public virtual DictionaryStorage Clone() {
  63. CommonDictionaryStorage storage = new CommonDictionaryStorage();
  64. foreach (KeyValuePair<object, object> kvp in GetItems()) {
  65. storage.Add(kvp.Key, kvp.Value);
  66. }
  67. return storage;
  68. }
  69. /// <summary>
  70. /// Adds items from this dictionary into the other dictionary
  71. /// </summary>
  72. public virtual void CopyTo(DictionaryStorage/*!*/ into) {
  73. Debug.Assert(into != null);
  74. foreach (KeyValuePair<object, object> kvp in GetItems()) {
  75. into.Add(kvp.Key, kvp.Value);
  76. }
  77. }
  78. /// <summary>
  79. /// Provides fast access to the __path__ attribute if the dictionary storage supports caching it.
  80. /// </summary>
  81. public virtual bool TryGetPath(out object value) {
  82. return TryGetValue("__path__", out value);
  83. }
  84. /// <summary>
  85. /// Provides fast access to the __package__ attribute if the dictionary storage supports caching it.
  86. /// </summary>
  87. public virtual bool TryGetPackage(out object value) {
  88. return TryGetValue("__package__", out value);
  89. }
  90. /// <summary>
  91. /// Provides fast access to the __builtins__ attribute if the dictionary storage supports caching it.
  92. /// </summary>
  93. public virtual bool TryGetBuiltins(out object value) {
  94. return TryGetValue("__builtins__", out value);
  95. }
  96. /// <summary>
  97. /// Provides fast access to the __name__ attribute if the dictionary storage supports caching it.
  98. /// </summary>
  99. public virtual bool TryGetName(out object value) {
  100. return TryGetValue("__name__", out value);
  101. }
  102. /// <summary>
  103. /// Provides fast access to the __import__ attribute if the dictionary storage supports caching it.
  104. /// </summary>
  105. public virtual bool TryGetImport(out object value) {
  106. return TryGetValue("__import__", out value);
  107. }
  108. }
  109. }