Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / CompMod / System / Collections / Generic / BitHelper.cs / 1305376 / BitHelper.cs
using System;
using System.Collections;
using System.Text;
namespace System.Collections.Generic {
///
/// ABOUT:
/// Helps with operations that rely on bit marking to indicate whether an item in the
/// collection should be added, removed, visited already, etc.
///
/// BitHelper doesn't allocate the array; you must pass in an array or ints allocated on the
/// stack or heap. ToIntArrayLength() tells you the int array size you must allocate.
///
/// USAGE:
/// Suppose you need to represent a bit array of length (i.e. logical bit array length)
/// BIT_ARRAY_LENGTH. Then this is the suggested way to instantiate BitHelper:
/// ****************************************************************************
/// int intArrayLength = BitHelper.ToIntArrayLength(BIT_ARRAY_LENGTH);
/// BitHelper bitHelper;
/// if (intArrayLength less than stack alloc threshold)
/// int* m_arrayPtr = stackalloc int[intArrayLength];
/// bitHelper = new BitHelper(m_arrayPtr, intArrayLength);
/// else
/// int[] m_arrayPtr = new int[intArrayLength];
/// bitHelper = new BitHelper(m_arrayPtr, intArrayLength);
/// ***************************************************************************
///
/// IMPORTANT:
/// The second ctor args, length, should be specified as the length of the int array, not
/// the logical bit array. Because length is used for bounds checking into the int array,
/// it's especially important to get this correct for the stackalloc version. See the code
/// samples above; this is the value gotten from ToIntArrayLength().
///
/// The length ctor argument is the only exception; for other methods -- MarkBit and
/// IsMarked -- pass in values as indices into the logical bit array, and it will be mapped
/// to the position within the array of ints.
///
///
unsafe internal class BitHelper { // should not be serialized
private const byte MarkedBitFlag = 1;
private const byte IntSize = 32;
// m_length of underlying int array (not logical bit array)
private int m_length;
// ptr to stack alloc'd array of ints
private int* m_arrayPtr;
// array of ints
private int[] m_array;
// whether to operate on stack alloc'd or heap alloc'd array
private bool useStackAlloc;
///
/// Instantiates a BitHelper with a heap alloc'd array of ints
///
/// int array to hold bits
/// length of int array
//
//
//
//
[System.Security.SecurityCritical]
internal BitHelper(int* bitArrayPtr, int length) {
this.m_arrayPtr = bitArrayPtr;
this.m_length = length;
useStackAlloc = true;
}
///
/// Instantiates a BitHelper with a heap alloc'd array of ints
///
/// int array to hold bits
/// length of int array
internal BitHelper(int[] bitArray, int length) {
this.m_array = bitArray;
this.m_length = length;
}
///
/// Mark bit at specified position
///
///
//
//
//
[System.Security.SecurityCritical]
internal unsafe void MarkBit(int bitPosition) {
if (useStackAlloc) {
int bitArrayIndex = bitPosition / IntSize;
if (bitArrayIndex < m_length && bitArrayIndex >= 0) {
m_arrayPtr[bitArrayIndex] |= (MarkedBitFlag << (bitPosition % IntSize));
}
}
else {
int bitArrayIndex = bitPosition / IntSize;
if (bitArrayIndex < m_length && bitArrayIndex >= 0) {
m_array[bitArrayIndex] |= (MarkedBitFlag << (bitPosition % IntSize));
}
}
}
///
/// Is bit at specified position marked?
///
///
///
//
//
//
[System.Security.SecurityCritical]
internal unsafe bool IsMarked(int bitPosition) {
if (useStackAlloc) {
int bitArrayIndex = bitPosition / IntSize;
if (bitArrayIndex < m_length && bitArrayIndex >= 0) {
return ((m_arrayPtr[bitArrayIndex] & (MarkedBitFlag << (bitPosition % IntSize))) != 0);
}
return false;
}
else {
int bitArrayIndex = bitPosition / IntSize;
if (bitArrayIndex < m_length && bitArrayIndex >= 0) {
return ((m_array[bitArrayIndex] & (MarkedBitFlag << (bitPosition % IntSize))) != 0);
}
return false;
}
}
///
/// How many ints must be allocated to represent n bits. Returns (n+31)/32, but
/// avoids overflow
///
///
///
internal static int ToIntArrayLength(int n) {
return n > 0 ? ((n - 1) / IntSize + 1) : 0;
}
}
}
// 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.Text;
namespace System.Collections.Generic {
///
/// ABOUT:
/// Helps with operations that rely on bit marking to indicate whether an item in the
/// collection should be added, removed, visited already, etc.
///
/// BitHelper doesn't allocate the array; you must pass in an array or ints allocated on the
/// stack or heap. ToIntArrayLength() tells you the int array size you must allocate.
///
/// USAGE:
/// Suppose you need to represent a bit array of length (i.e. logical bit array length)
/// BIT_ARRAY_LENGTH. Then this is the suggested way to instantiate BitHelper:
/// ****************************************************************************
/// int intArrayLength = BitHelper.ToIntArrayLength(BIT_ARRAY_LENGTH);
/// BitHelper bitHelper;
/// if (intArrayLength less than stack alloc threshold)
/// int* m_arrayPtr = stackalloc int[intArrayLength];
/// bitHelper = new BitHelper(m_arrayPtr, intArrayLength);
/// else
/// int[] m_arrayPtr = new int[intArrayLength];
/// bitHelper = new BitHelper(m_arrayPtr, intArrayLength);
/// ***************************************************************************
///
/// IMPORTANT:
/// The second ctor args, length, should be specified as the length of the int array, not
/// the logical bit array. Because length is used for bounds checking into the int array,
/// it's especially important to get this correct for the stackalloc version. See the code
/// samples above; this is the value gotten from ToIntArrayLength().
///
/// The length ctor argument is the only exception; for other methods -- MarkBit and
/// IsMarked -- pass in values as indices into the logical bit array, and it will be mapped
/// to the position within the array of ints.
///
///
unsafe internal class BitHelper { // should not be serialized
private const byte MarkedBitFlag = 1;
private const byte IntSize = 32;
// m_length of underlying int array (not logical bit array)
private int m_length;
// ptr to stack alloc'd array of ints
private int* m_arrayPtr;
// array of ints
private int[] m_array;
// whether to operate on stack alloc'd or heap alloc'd array
private bool useStackAlloc;
///
/// Instantiates a BitHelper with a heap alloc'd array of ints
///
/// int array to hold bits
/// length of int array
//
//
//
//
[System.Security.SecurityCritical]
internal BitHelper(int* bitArrayPtr, int length) {
this.m_arrayPtr = bitArrayPtr;
this.m_length = length;
useStackAlloc = true;
}
///
/// Instantiates a BitHelper with a heap alloc'd array of ints
///
/// int array to hold bits
/// length of int array
internal BitHelper(int[] bitArray, int length) {
this.m_array = bitArray;
this.m_length = length;
}
///
/// Mark bit at specified position
///
///
//
//
//
[System.Security.SecurityCritical]
internal unsafe void MarkBit(int bitPosition) {
if (useStackAlloc) {
int bitArrayIndex = bitPosition / IntSize;
if (bitArrayIndex < m_length && bitArrayIndex >= 0) {
m_arrayPtr[bitArrayIndex] |= (MarkedBitFlag << (bitPosition % IntSize));
}
}
else {
int bitArrayIndex = bitPosition / IntSize;
if (bitArrayIndex < m_length && bitArrayIndex >= 0) {
m_array[bitArrayIndex] |= (MarkedBitFlag << (bitPosition % IntSize));
}
}
}
///
/// Is bit at specified position marked?
///
///
///
//
//
//
[System.Security.SecurityCritical]
internal unsafe bool IsMarked(int bitPosition) {
if (useStackAlloc) {
int bitArrayIndex = bitPosition / IntSize;
if (bitArrayIndex < m_length && bitArrayIndex >= 0) {
return ((m_arrayPtr[bitArrayIndex] & (MarkedBitFlag << (bitPosition % IntSize))) != 0);
}
return false;
}
else {
int bitArrayIndex = bitPosition / IntSize;
if (bitArrayIndex < m_length && bitArrayIndex >= 0) {
return ((m_array[bitArrayIndex] & (MarkedBitFlag << (bitPosition % IntSize))) != 0);
}
return false;
}
}
///
/// How many ints must be allocated to represent n bits. Returns (n+31)/32, but
/// avoids overflow
///
///
///
internal static int ToIntArrayLength(int n) {
return n > 0 ? ((n - 1) / IntSize + 1) : 0;
}
}
}
// 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
- ComplexPropertyEntry.cs
- Receive.cs
- safesecurityhelperavalon.cs
- StickyNoteAnnotations.cs
- DeviceSpecificChoiceCollection.cs
- IUnknownConstantAttribute.cs
- ApplicationManager.cs
- CustomLineCap.cs
- VisualBrush.cs
- SerializationUtility.cs
- ManipulationPivot.cs
- CustomAttributeSerializer.cs
- AutomationPropertyInfo.cs
- SeparatorAutomationPeer.cs
- ContentPosition.cs
- ReachPrintTicketSerializer.cs
- ForeignKeyConstraint.cs
- ColorPalette.cs
- DPCustomTypeDescriptor.cs
- QuaternionIndependentAnimationStorage.cs
- ZipFileInfo.cs
- XsltSettings.cs
- LazyInitializer.cs
- PackagingUtilities.cs
- PersonalizationStateQuery.cs
- SecureConversationServiceCredential.cs
- CodeAttributeArgument.cs
- TypeForwardedToAttribute.cs
- OdbcConnectionPoolProviderInfo.cs
- CommandLibraryHelper.cs
- PerformanceCounterPermissionAttribute.cs
- DocumentEventArgs.cs
- ProfileManager.cs
- LocationUpdates.cs
- Screen.cs
- UnsafeNativeMethods.cs
- WorkBatch.cs
- ExclusiveTcpListener.cs
- ListSortDescriptionCollection.cs
- initElementDictionary.cs
- AesCryptoServiceProvider.cs
- HostedNamedPipeTransportManager.cs
- BezierSegment.cs
- TextHintingModeValidation.cs
- CodeVariableDeclarationStatement.cs
- WorkItem.cs
- RIPEMD160.cs
- FileDataSourceCache.cs
- CultureTableRecord.cs
- IdnElement.cs
- SkipQueryOptionExpression.cs
- ControlBindingsCollection.cs
- CodePrimitiveExpression.cs
- FontStretchConverter.cs
- TileBrush.cs
- DataServiceClientException.cs
- NullRuntimeConfig.cs
- EventEntry.cs
- FileClassifier.cs
- ContainsSearchOperator.cs
- ApplicationProxyInternal.cs
- EdmFunctions.cs
- MergeFilterQuery.cs
- XmlTextWriter.cs
- AxParameterData.cs
- ElementsClipboardData.cs
- HitTestResult.cs
- Resources.Designer.cs
- CodeCompileUnit.cs
- CqlQuery.cs
- CngAlgorithmGroup.cs
- WebServiceParameterData.cs
- ExcludePathInfo.cs
- SQLMoneyStorage.cs
- FieldMetadata.cs
- WebBrowserNavigatedEventHandler.cs
- PartialCachingControl.cs
- WindowsSlider.cs
- XPathNavigator.cs
- MsmqUri.cs
- RelatedView.cs
- DropTarget.cs
- WinFormsComponentEditor.cs
- HuffCodec.cs
- DataAdapter.cs
- DragCompletedEventArgs.cs
- MultiDataTrigger.cs
- MailDefinition.cs
- AutoResizedEvent.cs
- TimeoutValidationAttribute.cs
- ConcurrentDictionary.cs
- SiteMapDesignerDataSourceView.cs
- TextParagraphView.cs
- IdnMapping.cs
- ClientProxyGenerator.cs
- AuthenticateEventArgs.cs
- CfgParser.cs
- ToolStripPanelCell.cs
- FtpWebResponse.cs
- Properties.cs