1 //------------------------------------------------------------------------------
2 // <copyright file="HWStack.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">Microsoft</owner>
6 //------------------------------------------------------------------------------
11 namespace System
.Xml
{
13 // This stack is designed to minimize object creation for the
14 // objects being stored in the stack by allowing them to be
15 // re-used over time. It basically pushes the objects creating
16 // a high water mark then as Pop() is called they are not removed
17 // so that next time Push() is called it simply returns the last
18 // object that was already on the stack.
20 internal class HWStack
: ICloneable
{
21 internal HWStack(int GrowthRate
) : this (GrowthRate
, int.MaxValue
) {}
23 internal HWStack(int GrowthRate
, int limit
) {
24 this.growthRate
= GrowthRate
;
26 this.stack
= new Object
[GrowthRate
];
27 this.size
= GrowthRate
;
31 internal Object
Push() {
32 if (this.used
== this.size
) {
33 if (this.limit
<= this.used
) {
34 throw new XmlException(Res
.Xml_StackOverflow
, string.Empty
);
36 Object
[] newstack
= new Object
[this.size
+ this.growthRate
];
38 System
.Array
.Copy(this.stack
, 0, newstack
, 0, this.used
);
40 this.stack
= newstack
;
41 this.size
+= this.growthRate
;
43 return this.stack
[this.used
++];
46 internal Object
Pop() {
49 Object result
= this.stack
[this.used
];
55 internal object Peek() {
56 return this.used
> 0 ? this.stack
[this.used
- 1] : null;
59 internal void AddToTop(object o
) {
61 this.stack
[this.used
- 1] = o
;
65 internal Object
this[int index
] {
67 if (index
>= 0 && index
< this.used
) {
68 Object result
= this.stack
[index
];
72 throw new IndexOutOfRangeException();
76 if (index
>= 0 && index
< this.used
) {
77 this.stack
[index
] = value;
80 throw new IndexOutOfRangeException();
86 get { return this.used;}
93 private HWStack(object[] stack
, int growthRate
, int used
, int size
) {
95 this.growthRate
= growthRate
;
100 public object Clone() {
101 return new HWStack((object[]) this.stack
.Clone(), this.growthRate
, this.used
, this.size
);
104 private Object
[] stack
;
105 private int growthRate
;