The Labs \ Source Viewer \ SSCLI \ System.Xml \ HWStack

  1. //------------------------------------------------------------------------------
  2. // <copyright file="HWStack.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. namespace System.Xml
  17. {
  18.    
  19.     // This stack is designed to minimize object creation for the
  20.     // objects being stored in the stack by allowing them to be
  21.     // re-used over time. It basically pushes the objects creating
  22.     // a high water mark then as Pop() is called they are not removed
  23.     // so that next time Push() is called it simply returns the last
  24.     // object that was already on the stack.
  25.    
  26.     internal class HWStack : ICloneable
  27.     {
  28.         internal HWStack(int GrowthRate) : this(GrowthRate, int.MaxValue)
  29.         {
  30.         }
  31.        
  32.         internal HWStack(int GrowthRate, int limit)
  33.         {
  34.             this.growthRate = GrowthRate;
  35.             this.used = 0;
  36.             this.stack = new object[GrowthRate];
  37.             this.size = GrowthRate;
  38.             this.limit = limit;
  39.         }
  40.        
  41.         internal object Push()
  42.         {
  43.             if (this.used == this.size) {
  44.                 if (this.limit <= this.used) {
  45.                     throw new XmlException(Res.Xml_StackOverflow, string.Empty);
  46.                 }
  47.                 object[] newstack = new object[this.size + this.growthRate];
  48.                 if (this.used > 0) {
  49.                     System.Array.Copy(this.stack, 0, newstack, 0, this.used);
  50.                 }
  51.                 this.stack = newstack;
  52.                 this.size += this.growthRate;
  53.             }
  54.             return this.stack[this.used++];
  55.         }
  56.        
  57.         internal object Pop()
  58.         {
  59.             if (0 < this.used) {
  60.                 this.used--;
  61.                 object result = this.stack[this.used];
  62.                 return result;
  63.             }
  64.             return null;
  65.         }
  66.        
  67.         internal object Peek()
  68.         {
  69.             return this.used > 0 ? this.stack[this.used - 1] : null;
  70.         }
  71.        
  72.         internal void AddToTop(object o)
  73.         {
  74.             if (this.used > 0) {
  75.                 this.stack[this.used - 1] = o;
  76.             }
  77.         }
  78.        
  79.         internal object this[int index]
  80.         {
  81.             get {
  82.                 if (index >= 0 && index < this.used) {
  83.                     object result = this.stack[index];
  84.                     return result;
  85.                 }
  86.                 else {
  87.                     throw new IndexOutOfRangeException();
  88.                 }
  89.             }
  90.             set {
  91.                 if (index >= 0 && index < this.used) {
  92.                     this.stack[index] = value;
  93.                 }
  94.                 else {
  95.                     throw new IndexOutOfRangeException();
  96.                 }
  97.             }
  98.         }
  99.        
  100.         internal int Length {
  101.             get { return this.used; }
  102.         }
  103.        
  104.         //
  105.         // ICloneable
  106.         //
  107.        
  108.         private HWStack(object[] stack, int growthRate, int used, int size)
  109.         {
  110.             this.stack = stack;
  111.             this.growthRate = growthRate;
  112.             this.used = used;
  113.             this.size = size;
  114.         }
  115.        
  116.         public object Clone()
  117.         {
  118.             return new HWStack((object[])this.stack.Clone(), this.growthRate, this.used, this.size);
  119.         }
  120.        
  121.         private object[] stack;
  122.         private int growthRate;
  123.         private int used;
  124.         private int size;
  125.         private int limit;
  126.     }
  127.    
  128. }

Developer Fusion