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
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "glk/archetype/heap_sort.h"
#include "glk/archetype/archetype.h"
#include "glk/archetype/misc.h"
namespace Glk {
namespace Archetype {
const char *const CANT_PEEK = "Internal error: cannot peek into heap";
const char *const CANT_POKE = "Internal error: cannot poke into heap";
HeapType H;
void heap_sort_init() {
new_xarray(H);
}
static bool lighter(const Element one, const Element two) {
return *static_cast<StringPtr>(one) < *static_cast<StringPtr>(two);
}
static void heapup() {
int L, parent;
Element Lp, parentp = nullptr;
Element temp;
L = H.size();
while (L > 1) {
if ((L % 2) == 0)
parent = L / 2;
else
parent = (L - 1) / 2;
if (!(access_xarray(H, L, Lp, PEEK_ACCESS) && access_xarray(H, parent, parentp, PEEK_ACCESS)))
g_vm->writeln(CANT_PEEK);
if (lighter(Lp, parentp)) {
temp = parentp;
if (!(access_xarray(H, parent, Lp, POKE_ACCESS) && access_xarray(H, L, temp, POKE_ACCESS)))
g_vm->writeln(CANT_POKE);
L = parent;
} else {
L = 0;
}
}
}
static void heapdown() {
uint L, compare, lc, rc;
Element lp;
Element lcp, rcp;
Element comparep;
Element temp;
L = 1;
while (L < H.size()) {
lc = L * 2;
if (lc > H.size()) {
L = lc;
} else {
rc = lc + 1;
if (!access_xarray(H, lc, lcp, PEEK_ACCESS))
g_vm->writeln(CANT_PEEK);
if (rc > H.size()) {
compare = lc;
comparep = lcp;
} else {
if (!access_xarray(H, rc, rcp, PEEK_ACCESS))
g_vm->writeln(CANT_PEEK);
if (lighter(lcp, rcp)) {
compare = lc;
comparep = lcp;
} else {
compare = rc;
comparep = rcp;
}
}
if (!access_xarray(H, L, lp, PEEK_ACCESS))
g_vm->writeln(CANT_PEEK);
if (!lighter(comparep, lp)) {
temp = comparep;
if (!(access_xarray(H, compare, lp, POKE_ACCESS) && access_xarray(H, L, temp, POKE_ACCESS)))
g_vm->writeln(CANT_POKE);
L = compare;
} else {
L = H.size() + 1;
}
}
}
}
bool pop_heap(Element &e) {
Element temp;
if (H.empty()) {
return false;
} else {
if (!(access_xarray(H, 0, e, PEEK_ACCESS) && access_xarray(H, H.size() - 1, temp, PEEK_ACCESS)
&& access_xarray(H, 0, temp, POKE_ACCESS)))
g_vm->writeln(CANT_PEEK);
shrink_xarray(H);
heapdown();
return true;
}
}
void drop_on_heap(Element e) {
append_to_xarray(H, e);
heapup();
}
void drop_str_on_heap(const String &s) {
StringPtr sp = NewDynStr(s);
void *p = (void *)sp;
drop_on_heap(p);
}
void reinit_heap() {
dispose_xarray(H);
new_xarray(H);
}
} // End of namespace Archetype
} // End of namespace Glk
|