Frog Archiver 1.0
Frog Archiver is compression tools for any files you have.
Loading...
Searching...
No Matches
prioqueue.h
1#pragma once
2
3#include <iostream>
4#include <stdlib.h>
5using namespace std;
6
8template <class Type> class Queue {
9public:
10 Queue(int d = 2); // ctor
11 ~Queue(); // dtor
12 Queue(const Queue &); // cctor
13 const Queue &operator=(const Queue &);
14
15 void push(Type *);
16 Type *pop();
17 Type *head();
18
19 bool isEmpty() const;
20 bool isFull() const;
21
22 void reheapUp(int, int); // fix heap upward
23 void reheapDown(int, int); // fix heap downward
24 void swap(Type *&, Type *&);
25
26private:
27 int tail; // last element
28 Type **arr; // dinamic array
29 int size; // current size array
30 static const int incSize = 10;
31 int D;
32};
33
34// constructor to make new Queue
35template <class Type> Queue<Type>::Queue(int d) {
36 if (d < 2) {
37 d = 2;
38 }
39 D = d;
40 tail = 0;
41 size = incSize;
42 arr = new Type *[size];
43}
44
45// destructor to erase array
46template <class Type> Queue<Type>::~Queue(void) { delete[] arr; }
47
48// copy constructor
49template <class Type> Queue<Type>::Queue(const Queue &) {}
50
51template <class Type>
53
54// add new element to array
55template <class Type> void Queue<Type>::push(Type *item) {
56 if (isFull()) { // growing
57 int temp_size = size + incSize;
58 Type **temp_arr = new Type *[temp_size];
59
60 for (int i = 0; i < size; ++i) { // copy old arr to the new one
61 temp_arr[i] = arr[i];
62 }
63
64 delete[] arr; // del old arr
65 arr = temp_arr; // update arr
66 size = temp_size; // update size
67 }
68
69 arr[tail++] = item;
70 reheapUp(0, tail - 1);
71}
72
73// pop first elemnt arr
74template <class Type> Type *Queue<Type>::pop() {
75 if (isEmpty()) {
76 cout << "pop gagal!" << endl;
77 exit(1);
78 }
79
80 Type *rval = arr[0];
81 arr[0] = arr[tail - 1];
82 --tail;
83 reheapDown(0, tail - 1);
84
85 return rval;
86}
87
88template <class Type> Type *Queue<Type>::head() {
89 if (isEmpty()) {
90 cout << "pop gagal!" << endl;
91 exit(1);
92 }
93
94 return arr[0];
95}
96
97template <class Type> bool Queue<Type>::isEmpty() const { return (tail <= 0); }
98
99template <class Type> bool Queue<Type>::isFull() const {
100 return (tail >= size);
101}
102
103// fix heap upward
104template <class Type> void Queue<Type>::reheapUp(int root, int bottom) {
105 int parent; // parent of the bottom
106
107 if (bottom > root) {
108 parent = (bottom - 1) / D;
109
110 if (*arr[parent] > *arr[bottom]) {
111 swap(arr[parent], arr[bottom]);
112 reheapUp(root, parent);
113 }
114 }
115}
116
117// fix heap downward
118template <class Type> void Queue<Type>::reheapDown(int root, int bottom) {
119 int minChild, firstChild, child;
120
121 firstChild = root * D + 1;
122
123 if (firstChild <= bottom) {
124 minChild = firstChild;
125
126 for (int i = 2; i <= D; ++i) {
127 child = root * D + i;
128 if (child <= bottom) {
129 if (*arr[child] < *arr[minChild]) {
130 minChild = child;
131 }
132 }
133 }
134
135 if (*arr[root] > *arr[minChild]) {
136 swap(arr[root], arr[minChild]);
137 reheapDown(minChild, bottom);
138 }
139 }
140}
141
142template <class Type> void Queue<Type>::swap(Type *&a, Type *&b) {
143 Type *c;
144
145 c = a;
146 a = b;
147 b = c;
148}
Definition prioqueue.h:8