forked from MUME/MMapper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemapping.cpp
More file actions
351 lines (302 loc) · 9.63 KB
/
Copy pathRemapping.cpp
File metadata and controls
351 lines (302 loc) · 9.63 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2024 The MMapper Authors
#include "Remapping.h"
#include "../global/AnsiOstream.h"
#include "../global/ConfigConsts.h"
#include "../global/Timer.h"
#include "../global/logging.h"
#include "../global/progresscounter.h"
#include "InvalidMapOperation.h"
#include "RawExit.h"
#include "RawRoom.h"
#include <set>
#include <vector>
template<typename Result, typename Input, typename Convert>
NODISCARD static Result convertExit(const Input &input, Convert &&convert)
{
Result result;
result.fields = input.fields;
result.outgoing = convert(input.outgoing);
result.incoming = convert(input.incoming);
return result;
}
template<typename Result, typename Input, typename Convert>
NODISCARD static Result convertRoom(const Input &input, Convert &&convert)
{
Result result;
result.fields = input.fields;
for (const ExitDirEnum dir : ALL_EXITS7) {
result.exits[dir] = convert(input.exits[dir]);
}
result.position = input.position;
result.id = convert(input.id);
result.server_id = input.server_id;
result.status = input.status;
return result;
}
NODISCARD RoomId Remapping::convertToInternal(const ExternalRoomId ext) const
{
if (const auto it = m_extToInt.find(ext)) {
return *it;
}
return INVALID_ROOMID;
}
TinyRoomIdSet Remapping::convertToInternal(const TinyExternalRoomIdSet &set) const
{
TinyRoomIdSet replacement;
for (const ExternalRoomId id : set) {
replacement.insert(convertToInternal(id));
}
return replacement;
}
RawExit Remapping::convertToInternal(const ExternalRawExit &input) const
{
return convertExit<RawExit, ExternalRawExit>(input, [this](const auto &x) {
return convertToInternal(x);
});
}
RawRoom Remapping::convertToInternal(const ExternalRawRoom &input) const
{
return convertRoom<RawRoom, ExternalRawRoom>(input, [this](const auto &x) {
return convertToInternal(x);
});
}
std::vector<RawRoom> Remapping::convertToInternal(const std::vector<ExternalRawRoom> &input) const
{
DECL_TIMER(t, "convertToInternal");
std::vector<RawRoom> result;
result.reserve(input.size());
for (const auto &room : input) {
result.emplace_back(convertToInternal(room));
}
return result;
}
NODISCARD ExternalRoomId Remapping::convertToExternal(const RoomId id) const
{
const uint32_t pos = id.asUint32();
if (pos < m_intToExt.size()) {
return m_intToExt.at(id);
}
return INVALID_EXTERNAL_ROOMID;
}
TinyExternalRoomIdSet Remapping::convertToExternal(const TinyRoomIdSet &set) const
{
TinyExternalRoomIdSet replacement;
for (const RoomId id : set) {
replacement.insert(convertToExternal(id));
}
return replacement;
}
ExternalRawExit Remapping::convertToExternal(const RawExit &input) const
{
return convertExit<ExternalRawExit, RawExit>(input, [this](const auto &x) {
return convertToExternal(x);
});
}
ExternalRawRoom Remapping::convertToExternal(const RawRoom &input) const
{
return convertRoom<ExternalRawRoom, RawRoom>(input, [this](const auto &x) {
return convertToExternal(x);
});
}
std::vector<ExternalRawRoom> Remapping::convertToExternal(const std::vector<RawRoom> &input) const
{
DECL_TIMER(t, "convertToExternal");
std::vector<ExternalRawRoom> result;
result.reserve(input.size());
for (const auto &room : input) {
result.emplace_back(convertToExternal(room));
}
return result;
}
Remapping Remapping::computeFrom(const std::vector<ExternalRawRoom> &input)
{
if (input.empty()) {
return Remapping{};
}
DECL_TIMER(t, "building RoomId mapping");
Remapping remapping;
std::set<ExternalRoomId> seen;
for (const auto &r : input) {
seen.insert(r.id);
for (const auto &e : r.exits) {
for (const ExternalRoomId to : e.outgoing) {
seen.insert(to);
}
for (const ExternalRoomId from : e.incoming) {
seen.insert(from);
}
}
}
if ((false)) {
MMLOG() << "# of unique roomids = " << seen.size();
MMLOG() << "lowest room id = " << seen.begin()->value();
MMLOG() << "highest room id = " << seen.rbegin()->value();
}
// remapping.m_intToExt.reserve(seen.size());
std::vector<ExternalRoomId> intToExt;
RoomId next{0};
for (const auto &r : seen) {
assert(next.value() == intToExt.size());
const ExternalRoomId ext{r};
remapping.m_extToInt.set(ext, next);
intToExt.push_back(ext);
next = next.next();
}
remapping.m_intToExt.init(intToExt.data(), intToExt.size());
if constexpr (IS_DEBUG_BUILD) {
assert(next.asUint32() == seen.size());
assert(next.asUint32() == remapping.m_intToExt.size());
assert(next.asUint32() == remapping.m_extToInt.size());
remapping.m_extToInt.for_each([&remapping](const auto &kv) {
assert(remapping.m_intToExt.at(kv.second) == kv.first);
});
}
return remapping;
}
ExternalRoomId Remapping::getNextExternal() const
{
if (m_extToInt.empty()) {
return ExternalRoomId{0};
}
const auto any = m_extToInt.begin()->first;
ExternalRoomId highest = any;
m_intToExt.for_each([&highest](const ExternalRoomId x) {
if (x != INVALID_EXTERNAL_ROOMID) {
if (x > highest) {
highest = x;
}
}
});
return highest.next();
}
bool Remapping::contains(const RoomId id) const
{
if (id == INVALID_ROOMID) {
return false;
}
const auto pos = id.asUint32();
if (pos >= m_intToExt.size()) {
return false;
}
return m_intToExt.at(id) != INVALID_EXTERNAL_ROOMID;
}
void Remapping::grow_to_include(const RoomId id)
{
if (id == INVALID_ROOMID) {
assert(id != INVALID_ROOMID);
return;
}
if (const auto size = id.asUint32() + 1; m_intToExt.size() < size) {
m_intToExt.grow_to_size(size);
}
assert(id.asUint32() < m_intToExt.size());
}
void Remapping::addNew(const RoomId id)
{
assert(id != INVALID_ROOMID);
assert(!contains(id));
const auto nextExternal = getNextExternal();
const auto pos = id.asUint32();
assert(pos >= m_intToExt.size() || m_intToExt.at(id) == INVALID_EXTERNAL_ROOMID);
assert(m_extToInt.find(nextExternal) == nullptr);
grow_to_include(id);
m_intToExt.set(id, nextExternal);
m_extToInt.set(nextExternal, id);
assert(contains(id));
}
void Remapping::undelete(const RoomId id, const ExternalRoomId extid)
{
assert(id != INVALID_ROOMID);
assert(!contains(id));
assert(m_extToInt.find(extid) == nullptr);
const auto pos = id.asUint32();
assert(pos >= m_intToExt.size() || m_intToExt.at(id) == INVALID_EXTERNAL_ROOMID);
assert(m_extToInt.find(extid) == nullptr);
grow_to_include(id);
m_intToExt.set(id, extid);
m_extToInt.set(extid, id);
assert(contains(id));
}
void Remapping::removeAt(const RoomId id)
{
if (id == INVALID_ROOMID) {
throw InvalidMapOperation();
}
const ExternalRoomId ext = m_intToExt.at(id);
m_intToExt.set(id, INVALID_EXTERNAL_ROOMID);
if (ext != INVALID_EXTERNAL_ROOMID) {
m_extToInt.erase(ext);
}
assert(!contains(id));
}
void Remapping::compact(ProgressCounter &pc, const ExternalRoomId firstId)
{
if (firstId == INVALID_EXTERNAL_ROOMID) {
throw InvalidMapOperation();
}
ExternalRoomId next = firstId;
// REVISIT: this may perform poorly with immer
ImmUnorderedMap<ExternalRoomId, RoomId> newExtToInt;
pc.increaseTotalStepsBy(m_extToInt.size());
m_extToInt.for_each([this, &newExtToInt, &next, &pc](const auto &kv) {
m_intToExt.set(kv.second, next);
newExtToInt.set(next, kv.second);
next = next.next();
pc.step();
});
m_extToInt = newExtToInt;
}
void Remapping::printStats(ProgressCounter & /*pc*/, AnsiOstream &os) const
{
RoomId lo = INVALID_ROOMID;
RoomId hi = INVALID_ROOMID;
ExternalRoomId extLo = INVALID_EXTERNAL_ROOMID;
ExternalRoomId extHi = INVALID_EXTERNAL_ROOMID;
if (!m_extToInt.empty()) {
lo = RoomId{0};
hi = RoomId{static_cast<uint32_t>(m_intToExt.size() - 1u)};
for (; lo <= hi; lo = lo.next()) {
if (m_intToExt[lo] != INVALID_EXTERNAL_ROOMID) {
break;
}
}
for (; lo < hi; hi = RoomId{hi.value() - 1}) {
if (m_intToExt[hi] != INVALID_EXTERNAL_ROOMID) {
break;
}
}
const auto any = m_extToInt.begin()->first;
extLo = any;
extHi = any;
for (auto it = lo; it <= hi; it = it.next()) {
auto ext = m_intToExt.at(it);
if (ext < extLo) {
extLo = ext;
}
if (ext > extHi) {
extHi = ext;
}
}
}
static constexpr auto green = getRawAnsi(AnsiColor16Enum::green);
auto print = [&os](std::string_view prefix, size_t size, uint32_t loval, uint32_t hival) {
os << prefix;
os.writeWithColor(green, size);
if (loval != UINT_MAX && hival != UINT_MAX) {
os << " (";
os.writeWithColor(green, loval);
os << " to ";
os.writeWithColor(green, hival);
os << ")";
}
os << ".\n";
};
print("Allocated internal IDs: ", m_intToExt.size(), lo.value(), hi.value());
print("Allocated external IDs: ", m_extToInt.size(), extLo.value(), extHi.value());
}
bool Remapping::operator==(const Remapping &rhs) const
{
return m_extToInt == rhs.m_extToInt //
&& m_intToExt == rhs.m_intToExt; //
}