DotNetDupe
4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Toggle main menu visibility
Loading...
Searching...
No Matches
PriorityQueue.h
Go to the documentation of this file.
1
5
6
#pragma once
7
8
#include "
Common.h
"
9
#include "
System/Object.h
"
10
#include "
System/Array.h
"
11
#include "
System/InvalidOperationException.h
"
12
#include "
System/Collections/Generic/List.h
"
13
#include <functional>
14
15
namespace
DotNetDupe
{
16
namespace
System
{
17
namespace
Collections
{
18
namespace
Generic
{
19
28
template
<
typename
TElement,
typename
TPriority>
29
class
PriorityQueue
:
public
Object
{
30
private
:
31
struct
ElementPriorityPair {
32
TElement Element;
33
TPriority Priority;
34
35
bool
operator>(
const
ElementPriorityPair& other)
const
{
36
return
Priority > other.Priority;
37
}
38
39
bool
operator==
(
const
ElementPriorityPair& other)
const
{
40
return
false
;
// required for List<T> Contains
41
}
42
};
43
44
List<ElementPriorityPair>
m_lstItems;
45
49
void
SiftUp(
int
index) {
50
while
(index > 0) {
51
int
parent = (index - 1) / 2;
52
if
(m_lstItems[parent] > m_lstItems[index]) {
53
std::swap(m_lstItems[parent], m_lstItems[index]);
54
index = parent;
55
}
else
{
56
break
;
57
}
58
}
59
}
60
64
void
SiftDown(
int
index) {
65
int
count = m_lstItems.
GetCount
();
66
while
(index * 2 + 1 < count) {
67
int
smallest = index * 2 + 1;
68
int
right = index * 2 + 2;
69
if
(right < count && m_lstItems[smallest] > m_lstItems[right]) {
70
smallest = right;
71
}
72
if
(m_lstItems[index] > m_lstItems[smallest]) {
73
std::swap(m_lstItems[index], m_lstItems[smallest]);
74
index = smallest;
75
}
else
{
76
break
;
77
}
78
}
79
}
80
81
public
:
83
PriorityQueue
() =
default
;
84
87
int
GetCount
()
const
{
return
m_lstItems.GetCount(); }
88
92
void
Enqueue
(
const
TElement& element,
const
TPriority& priority) {
93
m_lstItems.Add(ElementPriorityPair{ element, priority });
94
SiftUp(m_lstItems.GetCount() - 1);
95
}
96
100
TElement
Dequeue
() {
101
if
(m_lstItems.GetCount() == 0) {
102
throw
System::InvalidOperationException
(
"PriorityQueue is empty."
);
103
}
104
TElement item = std::move(m_lstItems[0].Element);
105
std::swap(m_lstItems[0], m_lstItems[m_lstItems.GetCount() - 1]);
106
m_lstItems.RemoveAt(m_lstItems.GetCount() - 1);
107
SiftDown(0);
108
return
item;
109
}
110
114
TElement
Peek
()
const
{
115
if
(m_lstItems.GetCount() == 0) {
116
throw
System::InvalidOperationException
(
"PriorityQueue is empty."
);
117
}
118
return
m_lstItems[0].Element;
119
}
120
125
bool
TryDequeue
(TElement& element, TPriority& priority) {
126
if
(m_lstItems.GetCount() == 0) {
127
return
false
;
128
}
129
element = std::move(m_lstItems[0].Element);
130
priority = std::move(m_lstItems[0].Priority);
131
std::swap(m_lstItems[0], m_lstItems[m_lstItems.GetCount() - 1]);
132
m_lstItems.RemoveAt(m_lstItems.GetCount() - 1);
133
SiftDown(0);
134
return
true
;
135
}
136
138
void
Clear
() {
139
m_lstItems.Clear();
140
}
141
};
142
143
}
144
}
145
}
146
}
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::Collections::Generic::List
Represents a strongly typed list of objects accessible by index.
Definition
List.h:29
DotNetDupe::System::Collections::Generic::List::GetCount
int GetCount() const
Gets the number of elements contained in the List.
Definition
List.h:100
DotNetDupe::System::Collections::Generic::PriorityQueue::PriorityQueue
PriorityQueue()=default
Initializes a new instance of the PriorityQueue class.
DotNetDupe::System::Collections::Generic::PriorityQueue::GetCount
int GetCount() const
Gets the number of elements contained in the PriorityQueue.
Definition
PriorityQueue.h:87
DotNetDupe::System::Collections::Generic::PriorityQueue::TryDequeue
bool TryDequeue(TElement &element, TPriority &priority)
Removes the minimal element and copies it and its priority to the specified out parameters.
Definition
PriorityQueue.h:125
DotNetDupe::System::Collections::Generic::PriorityQueue::Dequeue
TElement Dequeue()
Removes and returns the minimal element from the PriorityQueue.
Definition
PriorityQueue.h:100
DotNetDupe::System::Collections::Generic::PriorityQueue::Clear
void Clear()
Removes all items from the PriorityQueue.
Definition
PriorityQueue.h:138
DotNetDupe::System::Collections::Generic::PriorityQueue::Peek
TElement Peek() const
Returns the minimal element from the PriorityQueue without removing it.
Definition
PriorityQueue.h:114
DotNetDupe::System::Collections::Generic::PriorityQueue::Enqueue
void Enqueue(const TElement &element, const TPriority &priority)
Adds the specified element with associated priority to the PriorityQueue.
Definition
PriorityQueue.h:92
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::Object::operator==
bool operator==(const Object &obj) const
Determines reference equality between two objects.
Definition
Object.cpp:6
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
PriorityQueue.h
Generated by
1.18.0