The Labs \ Source Viewer \ SSCLI \ System.Xml.Xsl.Runtime \ XmlNavigatorStack

  1. //------------------------------------------------------------------------------
  2. // <copyright file="XmlNavigatorStack.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. using System;
  16. using System.Xml;
  17. using System.Xml.XPath;
  18. using System.Diagnostics;
  19. namespace System.Xml.Xsl.Runtime
  20. {
  21.    
  22.     /// <summary>
  23.     /// A dynamic stack of IXmlNavigators.
  24.     /// </summary>
  25.     internal struct XmlNavigatorStack
  26.     {
  27.         private XPathNavigator[] stkNav;
  28.         // Stack of XPathNavigators
  29.         private int sp;
  30.         // Stack pointer (size of stack)
  31.         #if DEBUG
  32.         private const int InitialStackSize = 2;
  33.         #else
  34.         private const int InitialStackSize = 8;
  35.         #endif
  36.        
  37.         /// <summary>
  38.         /// Push a navigator onto the stack
  39.         /// </summary>
  40.         public void Push(XPathNavigator nav)
  41.         {
  42.             if (this.stkNav == null) {
  43.                 this.stkNav = new XPathNavigator[InitialStackSize];
  44.             }
  45.             else {
  46.                 if (this.sp >= this.stkNav.Length) {
  47.                     // Resize the stack
  48.                     XPathNavigator[] stkOld = this.stkNav;
  49.                     this.stkNav = new XPathNavigator[2 * this.sp];
  50.                     Array.Copy(stkOld, this.stkNav, this.sp);
  51.                 }
  52.             }
  53.            
  54.             this.stkNav[this.sp++] = nav;
  55.         }
  56.        
  57.         /// <summary>
  58.         /// Pop the topmost navigator and return it
  59.         /// </summary>
  60.         public XPathNavigator Pop()
  61.         {
  62.             Debug.Assert(!IsEmpty);
  63.             return this.stkNav[--this.sp];
  64.         }
  65.        
  66.         /// <summary>
  67.         /// Returns the navigator at the top of the stack without adjusting the stack pointer
  68.         /// </summary>
  69.         public XPathNavigator Peek()
  70.         {
  71.             Debug.Assert(!IsEmpty);
  72.             return this.stkNav[this.sp - 1];
  73.         }
  74.        
  75.         /// <summary>
  76.         /// Remove all navigators from the stack
  77.         /// </summary>
  78.         public void Reset()
  79.         {
  80.             this.sp = 0;
  81.         }
  82.        
  83.         /// <summary>
  84.         /// Returns true if there are no navigators in the stack
  85.         /// </summary>
  86.         public bool IsEmpty {
  87.             get { return this.sp == 0; }
  88.         }
  89.     }
  90. }

Developer Fusion