Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / wpf / src / Shared / MS / Utility / ItemMap.cs / 1 / ItemMap.cs
using System; using System.Collections; using System.Diagnostics; namespace MS.Utility { // // ItemStructMap// // Disable warnings about fields never being assigned to // This struct is designed to function with its fields starting at // their default values and without the need of surfacing a constructor // other than the deafult #pragma warning disable 649 internal struct ItemStructMap { public int EnsureEntry(int key) { int index = Search(key); if (index < 0) { // Not found, add Entry // Create initial capacity, if necessary if (Entries == null) { Entries = new Entry[SearchTypeThreshold]; } // Convert not-found index to insertion point index = ~index; Entry[] destEntries = Entries; // Increase capacity, if necessary if ((Count + 1) > Entries.Length) { destEntries = new Entry[Entries.Length * 2]; // Initialize start of new array Array.Copy(Entries, 0, destEntries, 0, index); } // Shift entries to make room for new key at provided insertion point Array.Copy(Entries, index, destEntries, index + 1, Count - index); // Ensure Source and Destination arrays are the same Entries = destEntries; // Clear new entry Entries[index] = EmptyEntry; // Set Entries[index].Key = key; Count++; } return index; } public int Search(int key) { int keyPv = Int32.MaxValue; int iPv = 0; // Use fastest search based on size if (Count > SearchTypeThreshold) { // Binary Search int iLo = 0; int iHi = Count - 1; while (iLo <= iHi) { iPv = (iHi + iLo) / 2; keyPv = Entries[iPv].Key; if (key == keyPv) { return iPv; } if (key < keyPv) { iHi = iPv - 1; } else { iLo = iPv + 1; } } } else { // Linear search for (int i = 0; i < Count; i++) { iPv = i; keyPv = Entries[iPv].Key; if (key == keyPv) { return iPv; } if (key < keyPv) { break; } } } // iPv and keyPv will match and have the last pivot check // Return a negative value whose bitwise compliment // is this index of the first Entry that is greater // than the key passed in (sorted insertion point) if (key > keyPv) { iPv++; } return ~iPv; } private const int SearchTypeThreshold = 4; public Entry[] Entries; public int Count; public struct Entry { public int Key; public T Value; } private static Entry EmptyEntry; } #pragma warning restore 649 } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. using System; using System.Collections; using System.Diagnostics; namespace MS.Utility { // // ItemStructMap // // Disable warnings about fields never being assigned to // This struct is designed to function with its fields starting at // their default values and without the need of surfacing a constructor // other than the deafult #pragma warning disable 649 internal struct ItemStructMap { public int EnsureEntry(int key) { int index = Search(key); if (index < 0) { // Not found, add Entry // Create initial capacity, if necessary if (Entries == null) { Entries = new Entry[SearchTypeThreshold]; } // Convert not-found index to insertion point index = ~index; Entry[] destEntries = Entries; // Increase capacity, if necessary if ((Count + 1) > Entries.Length) { destEntries = new Entry[Entries.Length * 2]; // Initialize start of new array Array.Copy(Entries, 0, destEntries, 0, index); } // Shift entries to make room for new key at provided insertion point Array.Copy(Entries, index, destEntries, index + 1, Count - index); // Ensure Source and Destination arrays are the same Entries = destEntries; // Clear new entry Entries[index] = EmptyEntry; // Set Entries[index].Key = key; Count++; } return index; } public int Search(int key) { int keyPv = Int32.MaxValue; int iPv = 0; // Use fastest search based on size if (Count > SearchTypeThreshold) { // Binary Search int iLo = 0; int iHi = Count - 1; while (iLo <= iHi) { iPv = (iHi + iLo) / 2; keyPv = Entries[iPv].Key; if (key == keyPv) { return iPv; } if (key < keyPv) { iHi = iPv - 1; } else { iLo = iPv + 1; } } } else { // Linear search for (int i = 0; i < Count; i++) { iPv = i; keyPv = Entries[iPv].Key; if (key == keyPv) { return iPv; } if (key < keyPv) { break; } } } // iPv and keyPv will match and have the last pivot check // Return a negative value whose bitwise compliment // is this index of the first Entry that is greater // than the key passed in (sorted insertion point) if (key > keyPv) { iPv++; } return ~iPv; } private const int SearchTypeThreshold = 4; public Entry[] Entries; public int Count; public struct Entry { public int Key; public T Value; } private static Entry EmptyEntry; } #pragma warning restore 649 } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- TdsParserSafeHandles.cs
- BulletedListEventArgs.cs
- SystemIcmpV6Statistics.cs
- LoadedEvent.cs
- SqlUtil.cs
- CompModHelpers.cs
- AssemblyAttributesGoHere.cs
- MissingFieldException.cs
- ProcessModelInfo.cs
- FontFaceLayoutInfo.cs
- SiteMapDataSource.cs
- DataGridViewRowEventArgs.cs
- ProcessInfo.cs
- UshortList2.cs
- NamespaceCollection.cs
- ProxyWebPartConnectionCollection.cs
- FileSystemEnumerable.cs
- AccessibleObject.cs
- WebBrowserProgressChangedEventHandler.cs
- RegexWriter.cs
- ResourceSet.cs
- PenContexts.cs
- ArrayConverter.cs
- InternalConfigSettingsFactory.cs
- ToolStripItemCollection.cs
- DataGridViewTopLeftHeaderCell.cs
- XmlSecureResolver.cs
- FreezableCollection.cs
- ThemeDirectoryCompiler.cs
- StorageEntityTypeMapping.cs
- VirtualDirectoryMappingCollection.cs
- SafeEventLogWriteHandle.cs
- ListViewDataItem.cs
- AssemblyResourceLoader.cs
- SuppressMergeCheckAttribute.cs
- WebPartConnectionsCancelVerb.cs
- TextTreeObjectNode.cs
- HatchBrush.cs
- GacUtil.cs
- GridViewCancelEditEventArgs.cs
- PrintDialog.cs
- Version.cs
- Site.cs
- ListViewGroup.cs
- CrossContextChannel.cs
- BamlCollectionHolder.cs
- NamedPermissionSet.cs
- PointConverter.cs
- DescendantOverDescendantQuery.cs
- TabPage.cs
- SerialPort.cs
- NGCPageContentCollectionSerializerAsync.cs
- ConstraintStruct.cs
- X509CertificateValidator.cs
- ComponentCollection.cs
- SliderAutomationPeer.cs
- Rules.cs
- FactoryRecord.cs
- SizeConverter.cs
- Size3D.cs
- SoapCodeExporter.cs
- SamlEvidence.cs
- EndpointBehaviorElement.cs
- PseudoWebRequest.cs
- RadialGradientBrush.cs
- DataObjectFieldAttribute.cs
- ServiceHttpModule.cs
- BitmapImage.cs
- DrawingVisual.cs
- UriSectionReader.cs
- SessionIDManager.cs
- ByteKeyFrameCollection.cs
- ErasingStroke.cs
- VectorCollectionValueSerializer.cs
- FunctionQuery.cs
- CompleteWizardStep.cs
- SpAudioStreamWrapper.cs
- EmptyStringExpandableObjectConverter.cs
- ContainerAction.cs
- relpropertyhelper.cs
- TraceInternal.cs
- DropShadowEffect.cs
- IItemContainerGenerator.cs
- EpmSyndicationContentSerializer.cs
- PrintPreviewControl.cs
- FixedSOMTextRun.cs
- DataGridViewComponentPropertyGridSite.cs
- EventMap.cs
- MouseOverProperty.cs
- ScriptingWebServicesSectionGroup.cs
- PenContext.cs
- TableLayoutPanelResizeGlyph.cs
- UnsafeNativeMethods.cs
- TrackingParameters.cs
- BaseUriHelper.cs
- CreateUserWizardStep.cs
- MenuItemStyleCollection.cs
- WebServiceData.cs
- ErrorReporting.cs
- FontWeightConverter.cs