Updates referencesource to .NET 4.7
[mono-project.git] / mcs / class / referencesource / System.Xml / System / Xml / HWStack.cs
blob2f076f8c835ffa2891cabe871bb4afacbab3b1fd
1 //------------------------------------------------------------------------------
2 // <copyright file="HWStack.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 // </copyright>
5 // <owner current="true" primary="true">Microsoft</owner>
6 //------------------------------------------------------------------------------
8 using System;
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;
25 this.used = 0;
26 this.stack = new Object[GrowthRate];
27 this.size = GrowthRate;
28 this.limit = limit;
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];
37 if (this.used > 0) {
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() {
47 if (0 < this.used) {
48 this.used--;
49 Object result = this.stack[this.used];
50 return result;
52 return null;
55 internal object Peek() {
56 return this.used > 0 ? this.stack[this.used - 1] : null;
59 internal void AddToTop(object o) {
60 if (this.used > 0) {
61 this.stack[this.used - 1] = o;
65 internal Object this[int index] {
66 get {
67 if (index >= 0 && index < this.used) {
68 Object result = this.stack[index];
69 return result;
71 else {
72 throw new IndexOutOfRangeException();
75 set {
76 if (index >= 0 && index < this.used) {
77 this.stack[index] = value;
79 else {
80 throw new IndexOutOfRangeException();
85 internal int Length {
86 get { return this.used;}
90 // ICloneable
93 private HWStack(object[] stack, int growthRate, int used, int size) {
94 this.stack = stack;
95 this.growthRate = growthRate;
96 this.used = used;
97 this.size = size;
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;
106 private int used;
107 private int size;
108 private int limit;