Code:
/ DotNET / DotNET / 8.0 / untmp / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / 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.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- OleDbException.cs
- PasswordBoxAutomationPeer.cs
- FontStretches.cs
- FullTextState.cs
- PasswordRecoveryAutoFormat.cs
- SkinBuilder.cs
- WebHttpBinding.cs
- GridToolTip.cs
- ResourcesBuildProvider.cs
- DispatcherTimer.cs
- DataColumnMappingCollection.cs
- InlineCollection.cs
- TimeEnumHelper.cs
- SqlColumnizer.cs
- CompilationLock.cs
- oledbmetadatacolumnnames.cs
- RegistrySecurity.cs
- EventLogPermissionEntryCollection.cs
- SqlDataSourceCustomCommandPanel.cs
- SourceChangedEventArgs.cs
- ExpandableObjectConverter.cs
- SaveWorkflowCommand.cs
- SecurityRuntime.cs
- NamedPermissionSet.cs
- ControlPropertyNameConverter.cs
- WsdlParser.cs
- FlowLayoutSettings.cs
- TypeConverterValueSerializer.cs
- SizeFConverter.cs
- Matrix3DConverter.cs
- ThreadInterruptedException.cs
- CultureInfoConverter.cs
- arc.cs
- IconBitmapDecoder.cs
- MaskedTextProvider.cs
- tabpagecollectioneditor.cs
- ChannelDispatcherBase.cs
- SQLBoolean.cs
- HwndHostAutomationPeer.cs
- ShellProvider.cs
- AssociationSetEnd.cs
- ResourceSet.cs
- Memoizer.cs
- AddressHeaderCollection.cs
- XPathNavigatorKeyComparer.cs
- Vector3D.cs
- StateMachine.cs
- WindowsTokenRoleProvider.cs
- CollectionDataContractAttribute.cs
- SqlResolver.cs
- InstalledVoice.cs
- TypeSystemHelpers.cs
- DecimalConstantAttribute.cs
- DbMetaDataCollectionNames.cs
- SqlBooleanMismatchVisitor.cs
- LockCookie.cs
- ComplexObject.cs
- QueryInterceptorAttribute.cs
- TextTreeTextNode.cs
- BigInt.cs
- ApplicationActivator.cs
- UIElement.cs
- CodeTryCatchFinallyStatement.cs
- IDQuery.cs
- CustomUserNameSecurityTokenAuthenticator.cs
- Semaphore.cs
- RenderDataDrawingContext.cs
- SiteMapNodeItem.cs
- SortQuery.cs
- WsdlContractConversionContext.cs
- WindowsPrincipal.cs
- PolyLineSegment.cs
- AsyncSerializedWorker.cs
- AuthenticationManager.cs
- DbConnectionClosed.cs
- QilLoop.cs
- ComponentResourceManager.cs
- WebPartCancelEventArgs.cs
- NavigatingCancelEventArgs.cs
- FlowDocumentReaderAutomationPeer.cs
- HttpServerVarsCollection.cs
- ClientConvert.cs
- DesignerFrame.cs
- SystemIPGlobalStatistics.cs
- VideoDrawing.cs
- PingReply.cs
- KeyValueConfigurationCollection.cs
- LocalizationComments.cs
- VirtualizedCellInfoCollection.cs
- StaticContext.cs
- DataListCommandEventArgs.cs
- HandlerFactoryWrapper.cs
- LoadRetryConstantStrategy.cs
- ConnectionStringsExpressionBuilder.cs
- XmlTextReader.cs
- BuilderInfo.cs
- LocatorManager.cs
- StrokeRenderer.cs
- BypassElement.cs
- RightsManagementEncryptionTransform.cs