DotNetDupe
4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Toggle main menu visibility
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
"
6
#include "
System/InvalidOperationException.h
"
7
#include "
System/Collections/Generic/List.h
"
8
9
namespace
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
100
Array<T>
ToArray
()
const
{
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
}
Array.h
Provides methods for creating, manipulating, searching, and sorting arrays.
Common.h
Defines common cross-platform macros, export decorators, and fundamental types.
InvalidOperationException.h
Defines the exception thrown when a method call is invalid for the object's current state.
List.h
Represents a strongly typed list of objects that can be accessed by index mirroring ....
Object.h
Base object class for DotNetDupe mirroring .NET System.Object.
DotNetDupe::System::Array
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition
Array.h:29
DotNetDupe::System::Collections::Generic::List
Represents a strongly typed list of objects accessible by index.
Definition
List.h:29
DotNetDupe::System::Collections::Generic::Stack::TryPeek
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
DotNetDupe::System::Collections::Generic::Stack::ToArray
Array< T > ToArray() const
Copies the Stack to a new array in LIFO order.
Definition
Stack.h:100
DotNetDupe::System::Collections::Generic::Stack::TryPop
bool TryPop(T &result)
Removes the object at the top of the Stack and copies it to the result parameter.
Definition
Stack.h:65
DotNetDupe::System::Collections::Generic::Stack::Push
void Push(const T &item)
Inserts an object at the top of the Stack.
Definition
Stack.h:35
DotNetDupe::System::Collections::Generic::Stack::Clear
void Clear()
Removes all objects from the Stack.
Definition
Stack.h:87
DotNetDupe::System::Collections::Generic::Stack::GetCount
int GetCount() const
Gets the number of elements contained in the Stack.
Definition
Stack.h:31
DotNetDupe::System::Collections::Generic::Stack::Peek
T Peek() const
Returns the object at the top of the Stack without removing it.
Definition
Stack.h:55
DotNetDupe::System::Collections::Generic::Stack::Contains
bool Contains(const T &item) const
Determines whether an element is in the Stack.
Definition
Stack.h:94
DotNetDupe::System::Collections::Generic::Stack::Stack
Stack()=default
Initializes a new instance of the Stack class that is empty.
DotNetDupe::System::Collections::Generic::Stack::Pop
T Pop()
Removes and returns the object at the top of the Stack.
Definition
Stack.h:42
DotNetDupe::System::InvalidOperationException
The exception that is thrown when a method call is invalid for the object's current state.
Definition
InvalidOperationException.h:16
DotNetDupe::System::Object
Supports all classes in the DotNetDupe class hierarchy.
Definition
Object.h:18
DotNetDupe::System::Collections::Generic
Definition
Dictionary.h:15
DotNetDupe::System::Collections
Definition
BlockingCollection.h:15
DotNetDupe::System
Definition
Action.h:11
DotNetDupe
Definition
IServiceCollection.h:7
Include
System
Collections
Generic
Stack.h
Generated by
1.18.0