2
0

ServiceModelDictionary.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel
  5. {
  6. using System.Xml;
  7. using System.Collections.Generic;
  8. class ServiceModelDictionary : IXmlDictionary
  9. {
  10. static public readonly ServiceModelDictionary Version1 = new ServiceModelDictionary(new ServiceModelStringsVersion1());
  11. ServiceModelStrings strings;
  12. int count;
  13. XmlDictionaryString[] dictionaryStrings1;
  14. XmlDictionaryString[] dictionaryStrings2;
  15. Dictionary<string, int> dictionary;
  16. XmlDictionaryString[] versionedDictionaryStrings;
  17. public ServiceModelDictionary(ServiceModelStrings strings)
  18. {
  19. this.strings = strings;
  20. this.count = strings.Count;
  21. }
  22. static public ServiceModelDictionary CurrentVersion
  23. {
  24. get
  25. {
  26. return Version1;
  27. }
  28. }
  29. public XmlDictionaryString CreateString(string value, int key)
  30. {
  31. return new XmlDictionaryString(this, value, key);
  32. }
  33. public bool TryLookup(string key, out XmlDictionaryString value)
  34. {
  35. if (key == null)
  36. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("key"));
  37. if (this.dictionary == null)
  38. {
  39. Dictionary<string, int> dictionary = new Dictionary<string, int>(count);
  40. for (int i = 0; i < count; i++)
  41. dictionary.Add(strings[i], i);
  42. this.dictionary = dictionary;
  43. }
  44. int id;
  45. if (this.dictionary.TryGetValue(key, out id))
  46. return TryLookup(id, out value);
  47. value = null;
  48. return false;
  49. }
  50. public bool TryLookup(int key, out XmlDictionaryString value)
  51. {
  52. const int keyThreshold = 32;
  53. if (key < 0 || key >= count)
  54. {
  55. value = null;
  56. return false;
  57. }
  58. XmlDictionaryString s;
  59. if (key < keyThreshold)
  60. {
  61. if (dictionaryStrings1 == null)
  62. dictionaryStrings1 = new XmlDictionaryString[keyThreshold];
  63. s = dictionaryStrings1[key];
  64. if (s == null)
  65. {
  66. s = CreateString(strings[key], key);
  67. dictionaryStrings1[key] = s;
  68. }
  69. }
  70. else
  71. {
  72. if (dictionaryStrings2 == null)
  73. dictionaryStrings2 = new XmlDictionaryString[count - keyThreshold];
  74. s = dictionaryStrings2[key - keyThreshold];
  75. if (s == null)
  76. {
  77. s = CreateString(strings[key], key);
  78. dictionaryStrings2[key - keyThreshold] = s;
  79. }
  80. }
  81. value = s;
  82. return true;
  83. }
  84. public bool TryLookup(XmlDictionaryString key, out XmlDictionaryString value)
  85. {
  86. if (key == null)
  87. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("key"));
  88. if (key.Dictionary == this)
  89. {
  90. value = key;
  91. return true;
  92. }
  93. if (key.Dictionary == CurrentVersion)
  94. {
  95. if (versionedDictionaryStrings == null)
  96. versionedDictionaryStrings = new XmlDictionaryString[CurrentVersion.count];
  97. XmlDictionaryString s = versionedDictionaryStrings[key.Key];
  98. if (s == null)
  99. {
  100. if (!TryLookup(key.Value, out s))
  101. {
  102. value = null;
  103. return false;
  104. }
  105. versionedDictionaryStrings[key.Key] = s;
  106. }
  107. value = s;
  108. return true;
  109. }
  110. value = null;
  111. return false;
  112. }
  113. }
  114. }