This repository has been archived by the owner on Aug 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpqueue.cpp
185 lines (164 loc) · 4.85 KB
/
pqueue.cpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//
// Created by hartings on 04.05.18.
//
#include <string>
#include <iostream>
#include "pqueue.h"
#define ERSTESELEMENT 1
#define LETZTESELEMENT 2
#define UNBEKANNTESELEMENT 3
using namespace std;
MyException::MyException(string error) {
_error = error;
}
string MyException::msg() {
return _error;
}
PriorityQueueEntry::PriorityQueueEntry(float getPriority, string getValue) {
priority = getPriority;
value = getValue;
}
PriorityQueue::PriorityQueue(int size) {
_size = size;
_last = -1;
_entrys = new PriorityQueueEntry*[size];
}
PriorityQueue::~PriorityQueue() {
for (int i = 0; i < _last ; i++) {
delete _entrys[i];
}
delete[] _entrys;
}
void PriorityQueue::changeArray(int richtung) {
if (richtung == 1) {
if (isFull() || isAlmostFull()) {
PriorityQueueEntry **newArray = new PriorityQueueEntry*[_size * 2];
for (int arraysize = 0; arraysize <= _last ; arraysize++) {
newArray[arraysize] = _entrys[arraysize];
}
delete[](_entrys);
_size *= 2;
_entrys = newArray;
}
} else if (richtung == -1) {
if ((_last+1) * 4 < _size) {
PriorityQueueEntry **newArray = new PriorityQueueEntry*[_size / 2];
for (int arraysize = 0; arraysize <= _last ; arraysize++) {
newArray[arraysize] = _entrys[arraysize];
}
delete[](_entrys);
_size /= 2;
_entrys = newArray;
}
} else {
throw MyException("Fehler 123");
}
}
bool PriorityQueue::isFull() {
return _last == (_size - 1);
}
bool PriorityQueue::isAlmostFull() {
return _last > (_size - 2);
}
bool PriorityQueue::isEmpty() {
return _last == -1;
}
void PriorityQueue::insert(string value, float priority) {
if (_last == -1) { // ARRAY ist leer
_last += 1;
_entrys[_last] = new PriorityQueueEntry(priority,value);
} else {
int i = 1;
changeArray(1);
if (priority < _entrys[0]->priority) {
for (int arraysize = _last; arraysize >= 0; arraysize--) {
_entrys[arraysize + 1] = _entrys[arraysize];
}
_entrys[0] = new PriorityQueueEntry(priority, value);
_last += 1;
} else if (priority > _entrys[_last]->priority) {
_last += 1;
_entrys[_last] = new PriorityQueueEntry(priority, value);
} else {
while (_entrys[i]->priority < priority) {
i++;
}
if (priority > _entrys[_last]->priority) {
throw MyException("Fehler");
}
for (int arraysize = _last; arraysize >= i; arraysize--) {
_entrys[arraysize + 1] = _entrys[arraysize];
}
_entrys[i] = new PriorityQueueEntry(priority, value);
_last += 1;
}
}
}
string PriorityQueue::extractMin() {
string ret;
if(_last == -1) {
throw MyException("Bitte erst das Array mit Inhalt befüllen");
}
ret = _entrys[0]->value;
privateRemove(ERSTESELEMENT, "");
return ret;
}
void PriorityQueue::privateRemove(int toggle, string value) {
if (_last == -1) {
throw MyException("Das Element wurde nicht gefunden.");
}
int i = 0;
switch(toggle) {
case ERSTESELEMENT:
delete _entrys[0];
for (int arraysize = 0; arraysize < _last; arraysize++ ) {
_entrys[arraysize] = _entrys[arraysize + 1];
}
break;
case LETZTESELEMENT:
delete _entrys[_last];
break;
case UNBEKANNTESELEMENT:
i = 0;
while(_entrys[i]->value != value && i < _last) {
i++;
}
if (_entrys[i]->value != value) {
throw MyException("Das Element wurde nicht gefunden.");
}
delete _entrys[i];
for (int arraysize = i; arraysize < _last; arraysize++) {
_entrys[arraysize] = _entrys[arraysize+1];
}
break;
default:
break;
}
_last -= 1;
changeArray(-1);
}
void PriorityQueue::remove(string value) {
if (_last == -1) {
throw MyException("Fehler.");
}
if (_entrys[0]->value == value) {
privateRemove(ERSTESELEMENT, "");
} else if(_entrys[_last]->value == value) {
privateRemove(LETZTESELEMENT, "");
} else {
privateRemove(UNBEKANNTESELEMENT, value);
}
}
void PriorityQueue::decreaseKey(string value, float priority) {
remove(value);
insert(value,priority);
}
void PriorityQueue::print() {
for(int i = 0; i < _size;i++) {
if (i <= _last) {
cout << _entrys[i]->value << " " << _entrys[i]->priority << " " <<_entrys[i] << endl;
} else {
cout << _entrys[i] << endl;
}
}
}