-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPriorityQueue.hpp
69 lines (54 loc) · 1.71 KB
/
PriorityQueue.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#pragma once
#include "BinaryHeap.hpp"
#include "IPriorityQueue.hpp"
template<class T>
class PriorityQueue : IPriorityQueue<T>
{
private:
BinaryHeap<T>* heap;
public:
PriorityQueue(T* items, size_t len) : heap(new BinaryHeap<T>(items, len)) {};
PriorityQueue() : heap(new BinaryHeap<T>()) {};
~PriorityQueue() { delete this->heap; }
public:
bool IsEmpty() { return heap->IsEmpty(); }
int GetLength() { return heap->GetLength(); }
virtual void Enqueue(T item) override { heap->Insert(item); }
virtual T Dequeue() override { return heap->Extract(); }
virtual T Peek(const size_t i) const override { return heap->Peek(i); }
virtual T PeekFirst() const override { return heap->Peek(0); }
virtual T PeekLast() const override { return heap->Peek(heap->GetLength() - 1); }
PriorityQueue<T>* Map(T (*f) (T))
{
BinaryHeap<T>* mapped = heap->Map(f);
int len = mapped->GetLength();
T* arr = new T[len];
for(int i = 0; i < len; i++)
arr[i] = mapped->Extract();
PriorityQueue<T>* pQ = new PriorityQueue<T>(arr, len);
delete mapped;
delete [] arr;
return pQ;
}
PriorityQueue<T>* Where(bool (*f) (T))
{
BinaryHeap<T>* whered = heap->Where(f);
int len = whered->GetLength();
T* arr = new T[len];
for(int i = 0; i < len; i++)
arr[i] = whered->Extract();
PriorityQueue<T>* pQ = new PriorityQueue<T>(arr, len);
delete whered;
delete [] arr;
return pQ;
}
T Reduce(T (*f) (T, T), T c)
{
T reduced = heap->Reduce(f, c);
return reduced;
}
void Print()
{
heap->Print();
}
};