Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / ndp / fx / src / Xml / System / Xml / NameTable.cs / 1 / NameTable.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// [....]
//-----------------------------------------------------------------------------
using System;
namespace System.Xml {
///
///
///
/// XmlNameTable implemented as a simple hash table.
///
///
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
//
///
///
/// Public constructor.
///
public NameTable() {
mask = 31;
entries = new Entry[mask+1];
hashCodeRandomizer = Environment.TickCount;
}
//
// XmlNameTable public methods
//
///
///
/// Add the given string to the NameTable or return
/// the existing string if it is already in the NameTable.
///
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 );
}
///
///
/// Add the given string to the NameTable or return
/// the existing string if it is already in the NameTable.
///
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 ) ) {
return e.str;
}
}
return AddEntry( new string( key, start, len ), hashCode );
}
///
///
/// Find the matching string in the NameTable.
///
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;
}
///
///
/// Find the matching string atom given a range of
/// characters.
///
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 ) ) {
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 array, char[] text, int start ) {
// use array.Length to eliminate the rangecheck
for ( int i = 0; i < array.Length; i++ ) {
if ( array[i] != text[start+i] ) {
return false;
}
}
return true;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// [....]
//-----------------------------------------------------------------------------
using System;
namespace System.Xml {
///
///
///
/// XmlNameTable implemented as a simple hash table.
///
///
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
//
///
///
/// Public constructor.
///
public NameTable() {
mask = 31;
entries = new Entry[mask+1];
hashCodeRandomizer = Environment.TickCount;
}
//
// XmlNameTable public methods
//
///
///
/// Add the given string to the NameTable or return
/// the existing string if it is already in the NameTable.
///
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 );
}
///
///
/// Add the given string to the NameTable or return
/// the existing string if it is already in the NameTable.
///
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 ) ) {
return e.str;
}
}
return AddEntry( new string( key, start, len ), hashCode );
}
///
///
/// Find the matching string in the NameTable.
///
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;
}
///
///
/// Find the matching string atom given a range of
/// characters.
///
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 ) ) {
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 array, char[] text, int start ) {
// use array.Length to eliminate the rangecheck
for ( int i = 0; i < array.Length; i++ ) {
if ( array[i] != text[start+i] ) {
return false;
}
}
return true;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- SignatureToken.cs
- HtmlForm.cs
- ThrowHelper.cs
- EntityDataSourceMemberPath.cs
- RadioButton.cs
- SizeKeyFrameCollection.cs
- EnterpriseServicesHelper.cs
- CreateParams.cs
- TypeExtensions.cs
- XmlElementCollection.cs
- WorkflowValidationFailedException.cs
- SqlXml.cs
- VisualTreeUtils.cs
- HandlerWithFactory.cs
- InfoCardSymmetricCrypto.cs
- CollectionChangedEventManager.cs
- Nullable.cs
- OrderByQueryOptionExpression.cs
- DataGridViewCellValueEventArgs.cs
- TreeWalkHelper.cs
- ResourceManagerWrapper.cs
- NameScope.cs
- LeafCellTreeNode.cs
- ServiceEndpointCollection.cs
- CodeAttributeDeclarationCollection.cs
- ClientSideQueueItem.cs
- DateTime.cs
- GestureRecognitionResult.cs
- ChangeConflicts.cs
- ProcessStartInfo.cs
- CLSCompliantAttribute.cs
- LineSegment.cs
- DependencyPropertyKind.cs
- ClientApiGenerator.cs
- ValidationErrorCollection.cs
- AmbientValueAttribute.cs
- ExceptionUtil.cs
- NamedPipeTransportSecurity.cs
- Int64.cs
- WebServiceErrorEvent.cs
- ByteAnimationUsingKeyFrames.cs
- CommonDialog.cs
- CommandLibraryHelper.cs
- Path.cs
- XmlDataContract.cs
- ResourceDefaultValueAttribute.cs
- HtmlControl.cs
- HttpException.cs
- ChameleonKey.cs
- OleDbPropertySetGuid.cs
- UIElement3DAutomationPeer.cs
- InlineCollection.cs
- DefaultBinder.cs
- WhitespaceRule.cs
- TypePropertyEditor.cs
- StrokeNodeOperations2.cs
- SchemaTableOptionalColumn.cs
- HtmlTableCell.cs
- AssemblyAttributesGoHere.cs
- ControlBuilderAttribute.cs
- RuntimeHandles.cs
- RawUIStateInputReport.cs
- NetPeerTcpBindingElement.cs
- ViewManager.cs
- BamlCollectionHolder.cs
- Profiler.cs
- IgnoreFileBuildProvider.cs
- AddInSegmentDirectoryNotFoundException.cs
- NeutralResourcesLanguageAttribute.cs
- WebPartHeaderCloseVerb.cs
- RawUIStateInputReport.cs
- UniqueEventHelper.cs
- ExtentKey.cs
- XmlTypeMapping.cs
- WebPartEditorCancelVerb.cs
- ItemDragEvent.cs
- TextViewBase.cs
- CacheOutputQuery.cs
- XmlSchemaChoice.cs
- RelationshipManager.cs
- LinqExpressionNormalizer.cs
- SupportingTokenProviderSpecification.cs
- HealthMonitoringSection.cs
- FormsAuthenticationUserCollection.cs
- KeyedHashAlgorithm.cs
- XmlTextReaderImpl.cs
- ConfigXmlAttribute.cs
- StoreItemCollection.cs
- SafeCryptoHandles.cs
- DispatcherEventArgs.cs
- HttpProtocolImporter.cs
- WindowsFormsSectionHandler.cs
- TreeViewImageGenerator.cs
- ThreadLocal.cs
- WebPartMenuStyle.cs
- FeedUtils.cs
- InternalSafeNativeMethods.cs
- MsmqInputSessionChannelListener.cs
- TypeSystem.cs
- PreProcessInputEventArgs.cs