NameTable.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // System.Xml.NameTable.cs
  3. //
  4. // Authors:
  5. // Duncan Mak ([email protected])
  6. // Ben Maurer ([email protected])
  7. //
  8. // (C) Ximian, Inc.
  9. // (C) 2003 Ben Maurer
  10. //
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. namespace System.Xml {
  34. //
  35. // This class implements the name table as a simple
  36. // hashtable, using buckets and a linked list.
  37. //
  38. public class NameTable : XmlNameTable {
  39. const int INITIAL_BUCKETS = 2 << 6; // 64
  40. int count = INITIAL_BUCKETS;
  41. Entry [] buckets = new Entry [INITIAL_BUCKETS];
  42. int size;
  43. public NameTable () {}
  44. class Entry {
  45. public string str;
  46. public int hash, len;
  47. public Entry next;
  48. public Entry (string str, int hash, Entry next)
  49. {
  50. this.str = str;
  51. this.len = str.Length;
  52. this.hash = hash;
  53. this.next = next;
  54. }
  55. }
  56. public override string Add (char [] key, int start, int len)
  57. {
  58. if (((0 > start) && (start >= key.Length))
  59. || ((0 > len) && (len >= key.Length - len)))
  60. throw new IndexOutOfRangeException ("The Index is out of range.");
  61. if (len == 0) return String.Empty;
  62. int h = 0;
  63. // This is from the String.Gethash () icall
  64. int end = start + len;
  65. for (int i = start; i < end; i++)
  66. h = (h << 5) - h + key [i];
  67. // h must be be >= 0
  68. h &= 0x7FFFFFFF;
  69. for (Entry e = buckets [h % count]; e != null; e = e.next) {
  70. if (e.hash == h && e.len == len && StrEqArray (e.str, key, start))
  71. return e.str;
  72. }
  73. return AddEntry (new string (key, start, len), h);
  74. }
  75. public override string Add (string key)
  76. {
  77. if (key == null) throw new ArgumentNullException ("key");
  78. int keyLen = key.Length;
  79. if (keyLen == 0) return String.Empty;
  80. int h = 0;
  81. // This is from the String.Gethash () icall
  82. for (int i = 0; i < keyLen; i++)
  83. h = (h << 5) - h + key [i];
  84. // h must be be >= 0
  85. h &= 0x7FFFFFFF;
  86. for (Entry e = buckets [h % count]; e != null; e = e.next) {
  87. if (e.hash == h && e.len == key.Length && e.str == key)
  88. return e.str;
  89. }
  90. return AddEntry (key, h);
  91. }
  92. public override string Get (char [] key, int start, int len)
  93. {
  94. if (((0 > start) && (start >= key.Length))
  95. || ((0 > len) && (len >= key.Length - len)))
  96. throw new IndexOutOfRangeException ("The Index is out of range.");
  97. if (len == 0) return String.Empty;
  98. int h = 0;
  99. // This is from the String.Gethash () icall
  100. int end = start + len;
  101. for (int i = start; i < end; i++)
  102. h = (h << 5) - h + key [i];
  103. // h must be be >= 0
  104. h &= 0x7FFFFFFF;
  105. for (Entry e = buckets [h % count]; e != null; e = e.next) {
  106. if (e.hash == h && e.len == len && StrEqArray (e.str, key, start))
  107. return e.str;
  108. }
  109. return null;
  110. }
  111. public override string Get (string value) {
  112. if (value == null) throw new ArgumentNullException ("value");
  113. int valLen = value.Length;
  114. if (valLen == 0) return String.Empty;
  115. int h = 0;
  116. // This is from the String.Gethash () icall
  117. for (int i = 0; i < valLen; i++)
  118. h = (h << 5) - h + value [i];
  119. // h must be be >= 0
  120. h &= 0x7FFFFFFF;
  121. for (Entry e = buckets [h % count]; e != null; e = e.next) {
  122. if (e.hash == h && e.len == value.Length && e.str == value)
  123. return e.str;
  124. }
  125. return null;
  126. }
  127. string AddEntry (string str, int hash)
  128. {
  129. int bucket = hash % count;
  130. buckets [bucket] = new Entry (str, hash, buckets [bucket]);
  131. // Grow whenever we double in size
  132. if (size++ == count) {
  133. count <<= 1;
  134. int csub1 = count - 1;
  135. Entry [] newBuckets = new Entry [count];
  136. for (int i = 0; i < buckets.Length; i++) {
  137. Entry root = buckets [i];
  138. Entry e = root;
  139. while (e != null) {
  140. int newLoc = e.hash & csub1;
  141. Entry n = e.next;
  142. e.next = newBuckets [newLoc];
  143. newBuckets [newLoc] = e;
  144. e = n;
  145. }
  146. }
  147. buckets = newBuckets;
  148. }
  149. return str;
  150. }
  151. static bool StrEqArray (string str, char [] str2, int start)
  152. {
  153. for (int i = 0; i < str.Length; i++) {
  154. if (str [i] != str2 [start + i])
  155. return false;
  156. }
  157. return true;
  158. }
  159. }
  160. }