| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- //
- // HighWaterStack.cs
- //
- // Authors:
- // Ben Maurer ([email protected])
- //
- // (C) 2003 Ben Maurer
- //
- namespace System.Xml {
- // This class represents a stack that minimizes object
- // allocation by using the "high water" method by allowing
- // objects to be reused over time. It would be much easier to do
- // with generics.
- //
- // To push an object one must use the following method:
- // Scope current = (Scope)scopes.Push ();
- // if (current == null) {
- // current = new Scope ();
- // scopes.AddToTop (current);
- // }
- // // set props of current
-
- internal class HighWaterStack : ICloneable {
- public HighWaterStack (int GrowthRate) : this (GrowthRate, int.MaxValue) {}
-
- public HighWaterStack (int GrowthRate, int limit)
- {
- growthrate = GrowthRate;
- used = 0;
- stack = new object [GrowthRate];
- size = GrowthRate;
- limit = limit;
- }
-
- public object Push ()
- {
- if (used == size) {
- if (limit <= used)
- throw new XmlException ("Xml Stack overflow!");
-
- size += growthrate;
- object [] newstack = new object [size];
-
- if (used > 0)
- Array.Copy (stack, 0, newstack, 0, used);
-
- stack = newstack;
-
- }
- return stack [used++];
- }
-
- public object Pop ()
- {
- if (used > 0)
- return stack [--used];
-
- return null;
- }
-
- public object Peek ()
- {
- return used > 0 ? stack[used - 1] : null;
- }
-
- public void AddToTop (object o)
- {
- if (used > 0)
- stack[used - 1] = o;
- }
-
- public object this [int index] {
- get {
- if (index >= 0 && index < used)
- return stack [index];
-
- throw new IndexOutOfRangeException ("index");
- }
- set {
- if (index >= 0 && index < used) stack [index] = value;
-
- throw new IndexOutOfRangeException ("index");
- }
- }
-
- public int Length {
- get { return used;}
- }
-
- public object Clone()
- {
- HighWaterStack ret = (HighWaterStack)this.MemberwiseClone ();
- ret.stack = (object [])this.stack.Clone ();
- return ret;
- }
-
- object [] stack;
- int growthrate;
- int used;
- int size;
- int limit;
- }
- }
|