DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
Queue.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 Queue : public Object {
22 private:
23 List<T> m_lstItems;
24
25 public:
27 Queue() = default;
28
31 int GetCount() const { return m_lstItems.GetCount(); }
32
35 void Enqueue(const T& item) {
36 m_lstItems.Add(item);
37 }
38
42 T Dequeue() {
43 if (m_lstItems.GetCount() == 0) {
44 throw System::InvalidOperationException("Queue is empty.");
45 }
46 T item = m_lstItems[0];
47 m_lstItems.RemoveAt(0);
48 return item;
49 }
50
54 T Peek() const {
55 if (m_lstItems.GetCount() == 0) {
56 throw System::InvalidOperationException("Queue is empty.");
57 }
58 return m_lstItems[0];
59 }
60
64 bool TryDequeue(T& result) {
65 if (m_lstItems.GetCount() == 0) {
66 return false;
67 }
68 result = m_lstItems[0];
69 m_lstItems.RemoveAt(0);
70 return true;
71 }
72
76 bool TryPeek(T& result) const {
77 if (m_lstItems.GetCount() == 0) {
78 return false;
79 }
80 result = m_lstItems[0];
81 return true;
82 }
83
85 void Clear() {
86 m_lstItems.Clear();
87 }
88
92 bool Contains(const T& item) const {
93 return m_lstItems.Contains(item);
94 }
95
98 Array<T> ToArray() const {
99 return m_lstItems.ToArray();
100 }
101 };
102
103 }
104 }
105 }
106}
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 TryDequeue(T &result)
Removes the object at the beginning of the Queue, and copies it to the result parameter.
Definition Queue.h:64
T Peek() const
Returns the object at the beginning of the Queue without removing it.
Definition Queue.h:54
T Dequeue()
Removes and returns the object at the beginning of the Queue.
Definition Queue.h:42
void Enqueue(const T &item)
Adds an object to the end of the Queue.
Definition Queue.h:35
Queue()=default
Initializes a new instance of the Queue class that is empty.
void Clear()
Removes all objects from the Queue.
Definition Queue.h:85
int GetCount() const
Gets the number of elements contained in the Queue.
Definition Queue.h:31
bool TryPeek(T &result) const
Returns a value that indicates whether there is an object at the beginning of the Queue,...
Definition Queue.h:76
Array< T > ToArray() const
Copies the Queue elements to a new array.
Definition Queue.h:98
bool Contains(const T &item) const
Determines whether an element is in the Queue.
Definition Queue.h:92
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