DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
Stack.h
Go to the documentation of this file.
1#pragma once
2
3#include "Common.h"
4#include "System/Object.h"
5#include "System/Array.h"
8
9namespace DotNetDupe {
10 namespace System {
11 namespace Collections {
12 namespace Generic {
13
20 template <typename T>
21 class Stack : public Object {
22 private:
23 List<T> m_lstItems;
24
25 public:
27 Stack() = default;
28
31 int GetCount() const { return m_lstItems.GetCount(); }
32
35 void Push(const T& item) {
36 m_lstItems.Add(item);
37 }
38
42 T Pop() {
43 if (m_lstItems.GetCount() == 0) {
44 throw System::InvalidOperationException("Stack is empty.");
45 }
46 int lastIndex = m_lstItems.GetCount() - 1;
47 T item = m_lstItems[lastIndex];
48 m_lstItems.RemoveAt(lastIndex);
49 return item;
50 }
51
55 T Peek() const {
56 if (m_lstItems.GetCount() == 0) {
57 throw System::InvalidOperationException("Stack is empty.");
58 }
59 return m_lstItems[m_lstItems.GetCount() - 1];
60 }
61
65 bool TryPop(T& result) {
66 if (m_lstItems.GetCount() == 0) {
67 return false;
68 }
69 int lastIndex = m_lstItems.GetCount() - 1;
70 result = m_lstItems[lastIndex];
71 m_lstItems.RemoveAt(lastIndex);
72 return true;
73 }
74
78 bool TryPeek(T& result) const {
79 if (m_lstItems.GetCount() == 0) {
80 return false;
81 }
82 result = m_lstItems[m_lstItems.GetCount() - 1];
83 return true;
84 }
85
87 void Clear() {
88 m_lstItems.Clear();
89 }
90
94 bool Contains(const T& item) const {
95 return m_lstItems.Contains(item);
96 }
97
101 int count = m_lstItems.GetCount();
102 Array<T> arrResult(count);
103 for (int i = 0; i < count; ++i) {
104 arrResult[i] = m_lstItems[count - 1 - i];
105 }
106 return arrResult;
107 }
108 };
109
110 }
111 }
112 }
113}
Provides methods for creating, manipulating, searching, and sorting arrays.
Defines common cross-platform macros, export decorators, and fundamental types.
Defines the exception thrown when a method call is invalid for the object's current state.
Represents a strongly typed list of objects that can be accessed by index mirroring ....
Base object class for DotNetDupe mirroring .NET System.Object.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition Array.h:29
Represents a strongly typed list of objects accessible by index.
Definition List.h:29
bool TryPeek(T &result) const
Returns a value that indicates whether there is an object at the top of the Stack,...
Definition Stack.h:78
Array< T > ToArray() const
Copies the Stack to a new array in LIFO order.
Definition Stack.h:100
bool TryPop(T &result)
Removes the object at the top of the Stack and copies it to the result parameter.
Definition Stack.h:65
void Push(const T &item)
Inserts an object at the top of the Stack.
Definition Stack.h:35
void Clear()
Removes all objects from the Stack.
Definition Stack.h:87
int GetCount() const
Gets the number of elements contained in the Stack.
Definition Stack.h:31
T Peek() const
Returns the object at the top of the Stack without removing it.
Definition Stack.h:55
bool Contains(const T &item) const
Determines whether an element is in the Stack.
Definition Stack.h:94
Stack()=default
Initializes a new instance of the Stack class that is empty.
T Pop()
Removes and returns the object at the top of the Stack.
Definition Stack.h:42
The exception that is thrown when a method call is invalid for the object's current state.
Supports all classes in the DotNetDupe class hierarchy.
Definition Object.h:18