summaryrefslogtreecommitdiff
path: root/src/inc/stack.h
diff options
context:
space:
mode:
authorJiyoung Yun <jy910.yun@samsung.com>2016-11-23 19:09:09 +0900
committerJiyoung Yun <jy910.yun@samsung.com>2016-11-23 19:09:09 +0900
commit4b4aad7217d3292650e77eec2cf4c198ea9c3b4b (patch)
tree98110734c91668dfdbb126fcc0e15ddbd93738ca /src/inc/stack.h
parentfa45f57ed55137c75ac870356a1b8f76c84b229c (diff)
downloadcoreclr-4b4aad7217d3292650e77eec2cf4c198ea9c3b4b.tar.gz
coreclr-4b4aad7217d3292650e77eec2cf4c198ea9c3b4b.tar.bz2
coreclr-4b4aad7217d3292650e77eec2cf4c198ea9c3b4b.zip
Imported Upstream version 1.1.0upstream/1.1.0
Diffstat (limited to 'src/inc/stack.h')
-rw-r--r--src/inc/stack.h87
1 files changed, 87 insertions, 0 deletions
diff --git a/src/inc/stack.h b/src/inc/stack.h
new file mode 100644
index 0000000000..589f8f7766
--- /dev/null
+++ b/src/inc/stack.h
@@ -0,0 +1,87 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+//-----------------------------------------------------------------------------
+
+//-----------------------------------------------------------------------------
+// This is a generic growable stack of T.
+
+#ifndef GENERIC_STACK_H
+#define GENERIC_STACK_H 1
+
+#include <math.h>
+
+template<typename T>
+class Stack
+{
+ T* m_elems;
+ unsigned m_elemsSize;
+ unsigned m_elemsCount;
+
+ static const unsigned InitSize = 8;
+
+ void GrowForPush()
+ {
+ if (m_elemsCount == m_elemsSize)
+ {
+ m_elemsSize = max(InitSize, 2*m_elemsSize);
+ T* newElems = new T[m_elemsSize];
+ if (m_elemsCount != 0)
+ {
+ _ASSERTE(m_elems != NULL);
+ for (unsigned k = 0; k < m_elemsCount; k++) newElems[k] = m_elems[k];
+ delete[] m_elems;
+ }
+ m_elems = newElems;
+ }
+ }
+
+ public:
+ Stack(unsigned sz = 0) : m_elems(NULL), m_elemsSize(sz), m_elemsCount(0)
+ {
+ if (sz > 0)
+ {
+ m_elems = new T[sz];
+ }
+ }
+
+ ~Stack()
+ {
+ if (m_elems != NULL) delete[] m_elems;
+ }
+
+ void Push(T t)
+ {
+ GrowForPush();
+ m_elems[m_elemsCount] = t;
+ m_elemsCount++;
+ }
+
+ bool IsEmpty()
+ {
+ return m_elemsCount == 0;
+ }
+
+ T Pop()
+ {
+ _ASSERTE(m_elemsCount > 0);
+ m_elemsCount--;
+ return m_elems[m_elemsCount];
+ }
+
+ T Peek()
+ {
+ _ASSERTE(m_elemsCount > 0);
+ return m_elems[m_elemsCount-1];
+ }
+
+ // Caller should take care to only side-effect the return reference if he/she is *sure*
+ // that the stack will not be popped in the interim!
+ T& PeekRef()
+ {
+ _ASSERTE(m_elemsCount > 0);
+ return m_elems[m_elemsCount-1];
+ }
+};
+
+#endif // GENERIC_STACK_H