XmlDictionary.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.Xml
  5. {
  6. using System;
  7. using System.IO;
  8. using System.Xml;
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. using System.Diagnostics;
  12. using System.Text;
  13. using System.Runtime.Serialization;
  14. public class XmlDictionary : IXmlDictionary
  15. {
  16. static IXmlDictionary empty;
  17. Dictionary<string, XmlDictionaryString> lookup;
  18. XmlDictionaryString[] strings;
  19. int nextId;
  20. static public IXmlDictionary Empty
  21. {
  22. get
  23. {
  24. if (empty == null)
  25. empty = new EmptyDictionary();
  26. return empty;
  27. }
  28. }
  29. public XmlDictionary()
  30. {
  31. this.lookup = new Dictionary<string, XmlDictionaryString>();
  32. this.strings = null;
  33. this.nextId = 0;
  34. }
  35. public XmlDictionary(int capacity)
  36. {
  37. this.lookup = new Dictionary<string, XmlDictionaryString>(capacity);
  38. this.strings = new XmlDictionaryString[capacity];
  39. this.nextId = 0;
  40. }
  41. public virtual XmlDictionaryString Add(string value)
  42. {
  43. XmlDictionaryString str;
  44. if (!this.lookup.TryGetValue(value, out str))
  45. {
  46. if (this.strings == null)
  47. {
  48. this.strings = new XmlDictionaryString[4];
  49. }
  50. else if (this.nextId == this.strings.Length)
  51. {
  52. int newSize = this.nextId * 2;
  53. if (newSize == 0)
  54. newSize = 4;
  55. Array.Resize(ref this.strings, newSize);
  56. }
  57. str = new XmlDictionaryString(this, value, this.nextId);
  58. this.strings[this.nextId] = str;
  59. this.lookup.Add(value, str);
  60. this.nextId++;
  61. }
  62. return str;
  63. }
  64. public virtual bool TryLookup(string value, out XmlDictionaryString result)
  65. {
  66. return this.lookup.TryGetValue(value, out result);
  67. }
  68. public virtual bool TryLookup(int key, out XmlDictionaryString result)
  69. {
  70. if (key < 0 || key >= this.nextId)
  71. {
  72. result = null;
  73. return false;
  74. }
  75. result = this.strings[key];
  76. return true;
  77. }
  78. public virtual bool TryLookup(XmlDictionaryString value, out XmlDictionaryString result)
  79. {
  80. if (value == null)
  81. throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("value"));
  82. if (value.Dictionary != this)
  83. {
  84. result = null;
  85. return false;
  86. }
  87. result = value;
  88. return true;
  89. }
  90. class EmptyDictionary : IXmlDictionary
  91. {
  92. public bool TryLookup(string value, out XmlDictionaryString result)
  93. {
  94. result = null;
  95. return false;
  96. }
  97. public bool TryLookup(int key, out XmlDictionaryString result)
  98. {
  99. result = null;
  100. return false;
  101. }
  102. public bool TryLookup(XmlDictionaryString value, out XmlDictionaryString result)
  103. {
  104. result = null;
  105. return false;
  106. }
  107. }
  108. }
  109. }