DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
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"
13#include <functional>
14
15namespace 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
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}
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.
Represents a strongly typed list of objects accessible by index.
Definition List.h:29
int GetCount() const
Gets the number of elements contained in the List.
Definition List.h:100
PriorityQueue()=default
Initializes a new instance of the PriorityQueue class.
int GetCount() const
Gets the number of elements contained in the PriorityQueue.
bool TryDequeue(TElement &element, TPriority &priority)
Removes the minimal element and copies it and its priority to the specified out parameters.
TElement Dequeue()
Removes and returns the minimal element from the PriorityQueue.
void Clear()
Removes all items from the PriorityQueue.
TElement Peek() const
Returns the minimal element from the PriorityQueue without removing it.
void Enqueue(const TElement &element, const TPriority &priority)
Adds the specified element with associated priority to the PriorityQueue.
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
bool operator==(const Object &obj) const
Determines reference equality between two objects.
Definition Object.cpp:6