mscorlib(4.0.0.0) API with additions
NameTable.cs
1 namespace System.Xml
2 {
4  [global::__DynamicallyInvokable]
5  public class NameTable : XmlNameTable
6  {
7  private class Entry
8  {
9  internal string str;
10 
11  internal int hashCode;
12 
13  internal Entry next;
14 
15  internal Entry(string str, int hashCode, Entry next)
16  {
17  this.str = str;
18  this.hashCode = hashCode;
19  this.next = next;
20  }
21  }
22 
23  private Entry[] entries;
24 
25  private int count;
26 
27  private int mask;
28 
29  private int hashCodeRandomizer;
30 
32  [global::__DynamicallyInvokable]
33  public NameTable()
34  {
35  mask = 31;
36  entries = new Entry[mask + 1];
37  hashCodeRandomizer = Environment.TickCount;
38  }
39 
45  [global::__DynamicallyInvokable]
46  public override string Add(string key)
47  {
48  if (key == null)
49  {
50  throw new ArgumentNullException("key");
51  }
52  int length = key.Length;
53  if (length == 0)
54  {
55  return string.Empty;
56  }
57  int num = length + hashCodeRandomizer;
58  for (int i = 0; i < key.Length; i++)
59  {
60  num += ((num << 7) ^ key[i]);
61  }
62  num -= num >> 17;
63  num -= num >> 11;
64  num -= num >> 5;
65  for (Entry entry = entries[num & mask]; entry != null; entry = entry.next)
66  {
67  if (entry.hashCode == num && entry.str.Equals(key))
68  {
69  return entry.str;
70  }
71  }
72  return AddEntry(key, num);
73  }
74 
85  [global::__DynamicallyInvokable]
86  public override string Add(char[] key, int start, int len)
87  {
88  if (len == 0)
89  {
90  return string.Empty;
91  }
92  int num = len + hashCodeRandomizer;
93  num += ((num << 7) ^ key[start]);
94  int num2 = start + len;
95  for (int i = start + 1; i < num2; i++)
96  {
97  num += ((num << 7) ^ key[i]);
98  }
99  num -= num >> 17;
100  num -= num >> 11;
101  num -= num >> 5;
102  for (Entry entry = entries[num & mask]; entry != null; entry = entry.next)
103  {
104  if (entry.hashCode == num && TextEquals(entry.str, key, start, len))
105  {
106  return entry.str;
107  }
108  }
109  return AddEntry(new string(key, start, len), num);
110  }
111 
117  [global::__DynamicallyInvokable]
118  public override string Get(string value)
119  {
120  if (value == null)
121  {
122  throw new ArgumentNullException("value");
123  }
124  if (value.Length == 0)
125  {
126  return string.Empty;
127  }
128  int num = value.Length + hashCodeRandomizer;
129  int num2 = num;
130  for (int i = 0; i < value.Length; i++)
131  {
132  num2 += ((num2 << 7) ^ value[i]);
133  }
134  num2 -= num2 >> 17;
135  num2 -= num2 >> 11;
136  num2 -= num2 >> 5;
137  for (Entry entry = entries[num2 & mask]; entry != null; entry = entry.next)
138  {
139  if (entry.hashCode == num2 && entry.str.Equals(value))
140  {
141  return entry.str;
142  }
143  }
144  return null;
145  }
146 
157  [global::__DynamicallyInvokable]
158  public override string Get(char[] key, int start, int len)
159  {
160  if (len == 0)
161  {
162  return string.Empty;
163  }
164  int num = len + hashCodeRandomizer;
165  num += ((num << 7) ^ key[start]);
166  int num2 = start + len;
167  for (int i = start + 1; i < num2; i++)
168  {
169  num += ((num << 7) ^ key[i]);
170  }
171  num -= num >> 17;
172  num -= num >> 11;
173  num -= num >> 5;
174  for (Entry entry = entries[num & mask]; entry != null; entry = entry.next)
175  {
176  if (entry.hashCode == num && TextEquals(entry.str, key, start, len))
177  {
178  return entry.str;
179  }
180  }
181  return null;
182  }
183 
184  private string AddEntry(string str, int hashCode)
185  {
186  int num = hashCode & mask;
187  Entry entry = new Entry(str, hashCode, entries[num]);
188  entries[num] = entry;
189  if (count++ == mask)
190  {
191  Grow();
192  }
193  return entry.str;
194  }
195 
196  private void Grow()
197  {
198  int num = mask * 2 + 1;
199  Entry[] array = entries;
200  Entry[] array2 = new Entry[num + 1];
201  for (int i = 0; i < array.Length; i++)
202  {
203  Entry next;
204  for (Entry entry = array[i]; entry != null; entry = next)
205  {
206  int num2 = entry.hashCode & num;
207  next = entry.next;
208  entry.next = array2[num2];
209  array2[num2] = entry;
210  }
211  }
212  entries = array2;
213  mask = num;
214  }
215 
216  private static bool TextEquals(string str1, char[] str2, int str2Start, int str2Length)
217  {
218  if (str1.Length != str2Length)
219  {
220  return false;
221  }
222  for (int i = 0; i < str1.Length; i++)
223  {
224  if (str1[i] != str2[str2Start + i])
225  {
226  return false;
227  }
228  }
229  return true;
230  }
231  }
232 }
NameTable()
Initializes a new instance of the NameTable class.
Definition: NameTable.cs:33
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
override string Get(char[] key, int start, int len)
Gets the atomized string containing the same characters as the specified range of characters in the g...
Definition: NameTable.cs:158
override string Add(string key)
Atomizes the specified string and adds it to the NameTable.
Definition: NameTable.cs:46
override string Add(char[] key, int start, int len)
Atomizes the specified string and adds it to the NameTable.
Definition: NameTable.cs:86
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
Implements a single-threaded T:System.Xml.XmlNameTable.
Definition: NameTable.cs:5
static int TickCount
Gets the number of milliseconds elapsed since the system started.
Definition: Environment.cs:306
Table of atomized string objects.
Definition: XmlNameTable.cs:5
override string Get(string value)
Gets the atomized string with the specified value.
Definition: NameTable.cs:118