1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
//------------------------------------------------------------------------------
// <copyright file="NameTable.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------
using System;
namespace System.Xml {
/// <include file='doc\NameTable.uex' path='docs/doc[@for="NameTable"]/*' />
/// <devdoc>
/// <para>
/// XmlNameTable implemented as a simple hash table.
/// </para>
/// </devdoc>
public class NameTable : XmlNameTable {
//
// Private types
//
class Entry {
internal string str;
internal int hashCode;
internal Entry next;
internal Entry( string str, int hashCode, Entry next ) {
this.str = str;
this.hashCode = hashCode;
this.next = next;
}
}
//
// Fields
//
Entry[] entries;
int count;
int mask;
int hashCodeRandomizer;
//
// Constructor
//
/// <include file='doc\NameTable.uex' path='docs/doc[@for="NameTable.NameTable"]/*' />
/// <devdoc>
/// Public constructor.
/// </devdoc>
public NameTable() {
mask = 31;
entries = new Entry[mask+1];
hashCodeRandomizer = Environment.TickCount;
}
//
// XmlNameTable public methods
//
/// <include file='doc\NameTable.uex' path='docs/doc[@for="NameTable.Add"]/*' />
/// <devdoc>
/// Add the given string to the NameTable or return
/// the existing string if it is already in the NameTable.
/// </devdoc>
public override string Add( string key ) {
if ( key == null ) {
throw new ArgumentNullException( "key" );
}
int len = key.Length;
if ( len == 0 ) {
return string.Empty;
}
int hashCode = len + hashCodeRandomizer;
// use key.Length to eliminate the rangecheck
for ( int i = 0; i < key.Length; i++ ) {
hashCode += ( hashCode << 7 ) ^ key[i];
}
// mix it a bit more
hashCode -= hashCode >> 17;
hashCode -= hashCode >> 11;
hashCode -= hashCode >> 5;
for ( Entry e = entries[hashCode & mask]; e != null; e = e.next ) {
if ( e.hashCode == hashCode && e.str.Equals( key ) ) {
return e.str;
}
}
return AddEntry( key, hashCode );
}
/// <include file='doc\NameTable.uex' path='docs/doc[@for="NameTable.Add1"]/*' />
/// <devdoc>
/// Add the given string to the NameTable or return
/// the existing string if it is already in the NameTable.
/// </devdoc>
public override string Add( char[] key, int start, int len ) {
if ( len == 0 ) {
return string.Empty;
}
int hashCode = len + hashCodeRandomizer;
hashCode += ( hashCode << 7 ) ^ key[start]; // this will throw IndexOutOfRangeException in case the start index is invalid
int end = start+len;
for ( int i = start + 1; i < end; i++) {
hashCode += ( hashCode << 7 ) ^ key[i];
}
// mix it a bit more
hashCode -= hashCode >> 17;
hashCode -= hashCode >> 11;
hashCode -= hashCode >> 5;
for ( Entry e = entries[hashCode & mask]; e != null; e = e.next ) {
if ( e.hashCode == hashCode && TextEquals( e.str, key, start, len ) ) {
return e.str;
}
}
return AddEntry( new string( key, start, len ), hashCode );
}
/// <include file='doc\NameTable.uex' path='docs/doc[@for="NameTable.Get"]/*' />
/// <devdoc>
/// Find the matching string in the NameTable.
/// </devdoc>
public override string Get( string value ) {
if ( value == null ) {
throw new ArgumentNullException("value");
}
if ( value.Length == 0 ) {
return string.Empty;
}
int len = value.Length + hashCodeRandomizer;
int hashCode = len;
// use value.Length to eliminate the rangecheck
for ( int i = 0; i < value.Length; i++ ) {
hashCode += ( hashCode << 7 ) ^ value[i];
}
// mix it a bit more
hashCode -= hashCode >> 17;
hashCode -= hashCode >> 11;
hashCode -= hashCode >> 5;
for ( Entry e = entries[hashCode & mask]; e != null; e = e.next ) {
if ( e.hashCode == hashCode && e.str.Equals( value ) ) {
return e.str;
}
}
return null;
}
/// <include file='doc\NameTable.uex' path='docs/doc[@for="NameTable.Get1"]/*' />
/// <devdoc>
/// Find the matching string atom given a range of
/// characters.
/// </devdoc>
public override string Get( char[] key, int start, int len ) {
if ( len == 0 ) {
return string.Empty;
}
int hashCode = len + hashCodeRandomizer;
hashCode += ( hashCode << 7 ) ^ key[start]; // this will throw IndexOutOfRangeException in case the start index is invalid
int end = start+len;
for ( int i = start + 1; i < end; i++) {
hashCode += ( hashCode << 7 ) ^ key[i];
}
// mix it a bit more
hashCode -= hashCode >> 17;
hashCode -= hashCode >> 11;
hashCode -= hashCode >> 5;
for ( Entry e = entries[hashCode & mask]; e != null; e = e.next ) {
if ( e.hashCode == hashCode && TextEquals( e.str, key, start, len ) ) {
return e.str;
}
}
return null;
}
//
// Private methods
//
private string AddEntry( string str, int hashCode ) {
int index = hashCode & mask;
Entry e = new Entry( str, hashCode, entries[index] );
entries[index] = e;
if ( count++ == mask ) {
Grow();
}
return e.str;
}
private void Grow() {
int newMask = mask * 2 + 1;
Entry[] oldEntries = entries;
Entry[] newEntries = new Entry[newMask+1];
// use oldEntries.Length to eliminate the rangecheck
for ( int i = 0; i < oldEntries.Length; i++ ) {
Entry e = oldEntries[i];
while ( e != null ) {
int newIndex = e.hashCode & newMask;
Entry tmp = e.next;
e.next = newEntries[newIndex];
newEntries[newIndex] = e;
e = tmp;
}
}
entries = newEntries;
mask = newMask;
}
private static bool TextEquals( string str1, char[] str2, int str2Start, int str2Length ) {
if ( str1.Length != str2Length ) {
return false;
}
// use array.Length to eliminate the rangecheck
for ( int i = 0; i < str1.Length; i++ ) {
if ( str1[i] != str2[str2Start+i] ) {
return false;
}
}
return true;
}
}
}
|