Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Xml / System / Xml / BitStack.cs / 1305376 / BitStack.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// [....]
//-----------------------------------------------------------------------------
namespace System.Xml {
using System;
using System.Diagnostics;
///
/// Manages a stack of bits. Exposes push, pop, and peek operations.
///
internal class BitStack {
private uint[] bitStack;
private int stackPos;
private uint curr;
///
/// Initialize stack.
///
public BitStack() {
// Set sentinel bit in 1st position. As bits are shifted onto this.curr, this sentinel
// bit shifts to the left. When it's about to overflow, this.curr will be pushed
// onto an unsigned int stack and the sentinel bit will be reset to 0x1.
this.curr = 0x1;
}
///
/// Push a 0 or 1 bit onto the stack.
///
public void PushBit(bool bit) {
if ((this.curr & 0x80000000) != 0) {
// If sentinel bit has reached the last position, push this.curr
PushCurr();
}
// Shift new bit onto this.curr (which must have at least one open position)
this.curr = (this.curr << 1) | (bit ? 1u : 0u);
}
///
/// Pop the top bit from the stack and return it.
///
public bool PopBit() {
bool bit;
Debug.Assert(this.curr != 0x1, "Stack empty");
// Shift rightmost bit from this.curr
bit = (this.curr & 0x1) != 0;
this.curr >>= 1;
if (this.curr == 0x1) {
// If sentinel bit has reached the rightmost position, pop this.curr
PopCurr();
}
return bit;
}
///
/// Return the top bit on the stack without pushing or popping.
///
public bool PeekBit() {
Debug.Assert(this.curr != 0x1, "Stack empty");
return (this.curr & 0x1) != 0;
}
#if !SILVERLIGHT // This property is not used in Silverlight
///
/// Return true if there are currently no bits on the stack.
///
public bool IsEmpty {
get { return this.curr == 0x1; }
}
#endif
///
/// this.curr has enough space for 31 bits (minus 1 for sentinel bit). Once this space is
/// exhausted, a uint stack is created to handle the overflow.
///
private void PushCurr() {
int len;
if (this.bitStack == null) {
this.bitStack = new uint[16];
}
// Push current unsigned int (which has been filled) onto a stack
// and initialize this.curr to be used for future pushes.
this.bitStack[this.stackPos++] = this.curr;
this.curr = 0x1;
// Resize stack if necessary
len = this.bitStack.Length;
if (this.stackPos >= len) {
uint[] bitStackNew = new uint[2 * len];
Array.Copy(this.bitStack, bitStackNew, len);
this.bitStack = bitStackNew;
}
}
///
/// If all bits have been popped from this.curr, then pop the previous uint value from the stack in
/// order to provide another 31 bits.
///
private void PopCurr() {
if (this.stackPos > 0)
this.curr = this.bitStack[--this.stackPos];
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// [....]
//-----------------------------------------------------------------------------
namespace System.Xml {
using System;
using System.Diagnostics;
///
/// Manages a stack of bits. Exposes push, pop, and peek operations.
///
internal class BitStack {
private uint[] bitStack;
private int stackPos;
private uint curr;
///
/// Initialize stack.
///
public BitStack() {
// Set sentinel bit in 1st position. As bits are shifted onto this.curr, this sentinel
// bit shifts to the left. When it's about to overflow, this.curr will be pushed
// onto an unsigned int stack and the sentinel bit will be reset to 0x1.
this.curr = 0x1;
}
///
/// Push a 0 or 1 bit onto the stack.
///
public void PushBit(bool bit) {
if ((this.curr & 0x80000000) != 0) {
// If sentinel bit has reached the last position, push this.curr
PushCurr();
}
// Shift new bit onto this.curr (which must have at least one open position)
this.curr = (this.curr << 1) | (bit ? 1u : 0u);
}
///
/// Pop the top bit from the stack and return it.
///
public bool PopBit() {
bool bit;
Debug.Assert(this.curr != 0x1, "Stack empty");
// Shift rightmost bit from this.curr
bit = (this.curr & 0x1) != 0;
this.curr >>= 1;
if (this.curr == 0x1) {
// If sentinel bit has reached the rightmost position, pop this.curr
PopCurr();
}
return bit;
}
///
/// Return the top bit on the stack without pushing or popping.
///
public bool PeekBit() {
Debug.Assert(this.curr != 0x1, "Stack empty");
return (this.curr & 0x1) != 0;
}
#if !SILVERLIGHT // This property is not used in Silverlight
///
/// Return true if there are currently no bits on the stack.
///
public bool IsEmpty {
get { return this.curr == 0x1; }
}
#endif
///
/// this.curr has enough space for 31 bits (minus 1 for sentinel bit). Once this space is
/// exhausted, a uint stack is created to handle the overflow.
///
private void PushCurr() {
int len;
if (this.bitStack == null) {
this.bitStack = new uint[16];
}
// Push current unsigned int (which has been filled) onto a stack
// and initialize this.curr to be used for future pushes.
this.bitStack[this.stackPos++] = this.curr;
this.curr = 0x1;
// Resize stack if necessary
len = this.bitStack.Length;
if (this.stackPos >= len) {
uint[] bitStackNew = new uint[2 * len];
Array.Copy(this.bitStack, bitStackNew, len);
this.bitStack = bitStackNew;
}
}
///
/// If all bits have been popped from this.curr, then pop the previous uint value from the stack in
/// order to provide another 31 bits.
///
private void PopCurr() {
if (this.stackPos > 0)
this.curr = this.bitStack[--this.stackPos];
}
}
}
// 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
- SQLBinaryStorage.cs
- ButtonChrome.cs
- ToolStripDropDown.cs
- ObjectStateEntry.cs
- GridViewPageEventArgs.cs
- StringArrayConverter.cs
- ZipArchive.cs
- DataServices.cs
- XamlFilter.cs
- ForceCopyBuildProvider.cs
- DesignerImageAdapter.cs
- ObservableDictionary.cs
- ProfileSettingsCollection.cs
- SortKey.cs
- MulticastIPAddressInformationCollection.cs
- FileSystemWatcher.cs
- assemblycache.cs
- InfocardChannelParameter.cs
- StringDictionaryEditor.cs
- NavigationCommands.cs
- VisualCollection.cs
- AuthorizationContext.cs
- WindowInteropHelper.cs
- OracleConnectionFactory.cs
- VisualStyleRenderer.cs
- ProcessHostFactoryHelper.cs
- WrappedIUnknown.cs
- GridViewItemAutomationPeer.cs
- TemplateLookupAction.cs
- SettingsAttributeDictionary.cs
- PageTheme.cs
- StreamedFramingRequestChannel.cs
- SchemaMapping.cs
- NullableLongMinMaxAggregationOperator.cs
- WpfKnownType.cs
- tibetanshape.cs
- HasCopySemanticsAttribute.cs
- RegisteredDisposeScript.cs
- XPathEmptyIterator.cs
- CompilerTypeWithParams.cs
- BamlReader.cs
- MenuItem.cs
- NameValuePair.cs
- RectValueSerializer.cs
- NetworkStream.cs
- SmtpAuthenticationManager.cs
- PageParser.cs
- DatePicker.cs
- KeyEventArgs.cs
- WmlTextViewAdapter.cs
- ThreadStateException.cs
- ConfigUtil.cs
- BigInt.cs
- SimpleTypeResolver.cs
- SecurityTokenSpecification.cs
- CorePropertiesFilter.cs
- IntSecurity.cs
- AccessKeyManager.cs
- StringValidator.cs
- Drawing.cs
- InstanceLockedException.cs
- ContextMenuService.cs
- RegexRunner.cs
- FreezableDefaultValueFactory.cs
- ThemeableAttribute.cs
- TypeRefElement.cs
- DeclaredTypeValidatorAttribute.cs
- MediaContextNotificationWindow.cs
- XamlPathDataSerializer.cs
- HttpStaticObjectsCollectionWrapper.cs
- XmlException.cs
- IgnoreFileBuildProvider.cs
- StrokeCollection2.cs
- SafeCryptoHandles.cs
- _OverlappedAsyncResult.cs
- Automation.cs
- UrlPath.cs
- LoginView.cs
- DataGridLength.cs
- ParameterRetriever.cs
- AspNetPartialTrustHelpers.cs
- AspCompat.cs
- CommonGetThemePartSize.cs
- WsatConfiguration.cs
- PeerValidationBehavior.cs
- Transform3D.cs
- HttpCookiesSection.cs
- BoundPropertyEntry.cs
- AppDomainManager.cs
- SQLDoubleStorage.cs
- NTAccount.cs
- ArglessEventHandlerProxy.cs
- PageSetupDialog.cs
- AsyncPostBackErrorEventArgs.cs
- GridView.cs
- HybridDictionary.cs
- TextChange.cs
- PlatformNotSupportedException.cs
- PointAnimationUsingKeyFrames.cs
- GenericEnumerator.cs