The Labs \ Source Viewer \ SSCLI \ System.Runtime.Serialization.Formatters.Soap.Xml \ HWStack

  1. // ==++==
  2. //
  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. //
  14. // ==--==
  15. /*
  16. *
  17. */
  18. using System;
  19. namespace System.Runtime.Serialization.Formatters.Soap.Xml
  20. {
  21.    
  22.     // This stack is designed to minimize object creation for the
  23.     // objects being stored in the stack by allowing them to be
  24.     // re-used over time. It basically pushes the objects creating
  25.     // a high water mark then as Pop() is called they are not removed
  26.     // so that next time Push() is called it simply returns the last
  27.     // object that was already on the stack.
  28.    
  29.     internal class HWStack
  30.     {
  31.         public HWStack(int GrowthRate)
  32.         {
  33.             _GrowthRate = GrowthRate;
  34.             _Used = 0;
  35.             _Stack = new object[GrowthRate];
  36.             _Size = GrowthRate;
  37.         }
  38.        
  39.         public object Push()
  40.         {
  41.             if (_Used == _Size) {
  42.                 object[] newstack = new object[_Size + _GrowthRate];
  43.                 if (_Used > 0) {
  44.                     System.Array.Copy(_Stack, 0, newstack, 0, _Used);
  45.                 }
  46.                 _Stack = newstack;
  47.                 _Size += _GrowthRate;
  48.             }
  49.             return _Stack[_Used++];
  50.         }
  51.        
  52.         public object Pop()
  53.         {
  54.             if (0 < _Used) {
  55.                 _Used--;
  56.                 object result = _Stack[_Used];
  57.                 return result;
  58.             }
  59.             return null;
  60.         }
  61.        
  62.         public object this[int index]
  63.         {
  64.             get {
  65.                 if (index >= 0 && index < _Used) {
  66.                     object result = _Stack[index];
  67.                     return result;
  68.                 }
  69.                 else
  70.                     throw new IndexOutOfRangeException();
  71.             }
  72.             set {
  73.                 if (index >= 0 && index < _Used)
  74.                     _Stack[index] = value;
  75.                 else
  76.                     throw new IndexOutOfRangeException();
  77.             }
  78.         }
  79.        
  80.         public int Length {
  81.             get { return _Used; }
  82.         }
  83.        
  84.         private object[] _Stack;
  85.         private int _GrowthRate;
  86.         private int _Used;
  87.         private int _Size;
  88.     }
  89.    
  90. }

Developer Fusion