Skip to content

Commit 8f583e9

Browse files
RyanGuildRyanGuild-us-ignite
authored andcommitted
Removed priority queue in favor of FlatQueue (Azgaar#1157)
* removed priority queue in favor of simple array extension as it will be easier to migrate to esm * patch: bump version * spacing * moved references to globalThis * demonstrate module interop * added version to priority-queue and moved to utils to follow dom loading pattern * removed PriorityQueue in favor of FlatQueue * update index.html * never mind that force push I don't know how to amend commits right * missing capitalization * priority set to 0 on 541 --------- Co-authored-by: RyanGuild <ryan.guild@us-ignite.org>
1 parent ef2eb0a commit 8f583e9

File tree

8 files changed

+40
-38
lines changed

8 files changed

+40
-38
lines changed

index.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8326,7 +8326,6 @@
83268326
<script src="libs/jquery-ui.min.js"></script>
83278327
<script src="versioning.js"></script>
83288328
<script src="libs/d3.min.js"></script>
8329-
<script src="libs/priority-queue.min.js"></script>
83308329
<script src="libs/flatqueue.js"></script>
83318330
<script src="libs/delaunator.min.js"></script>
83328331
<script src="libs/indexedDB.js?v=1.99.00"></script>
@@ -8357,17 +8356,18 @@
83578356
<script src="modules/lakes.js?v=1.99.00"></script>
83588357
<script src="modules/biomes.js?v=1.99.00"></script>
83598358
<script src="modules/names-generator.js?v=1.105.11"></script>
8360-
<script src="modules/cultures-generator.js?v=1.105.13"></script>
83618359
<script src="modules/resources-generator.js"></script>
83628360
<script src="modules/renderers/state-labels.js?v=1.96.04"></script>
8363-
<script src="modules/burgs-and-states.js?v=1.99.06"></script>
83648361
<script src="modules/production-generator.js"></script>
83658362
<script src="modules/trade-generator.js"></script>
8363+
<script src="modules/cultures-generator.js?v=1.105.21"></script>
8364+
<script src="modules/burgs-and-states.js?v=1.105.21"></script>
8365+
<script src="modules/provinces-generator.js?v=1.105.21"></script>
83668366
<script src="modules/routes-generator.js?v=1.104.10"></script>
8367-
<script src="modules/religions-generator.js?v=1.99.05"></script>
8367+
<script src="modules/religions-generator.js?v=1.105.21"></script>
83688368
<script src="modules/military-generator.js?v=1.104.0"></script>
83698369
<script src="modules/markers-generator.js?v=1.104.0"></script>
8370-
<script src="modules/zones-generator.js?v=1.104.0"></script>
8370+
<script src="modules/zones-generator.js?v=1.105.21"></script>
83718371
<script src="modules/coa-generator.js?v=1.99.00"></script>
83728372
<script src="modules/ui/resources-editor.js"></script>
83738373
<script src="modules/submap.js?v=1.104.0"></script>

libs/priority-queue.min.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

modules/burgs-and-states.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,8 @@ window.BurgsAndStates = (() => {
286286
const {cells, states, cultures, burgs} = pack;
287287

288288
cells.state = cells.state || new Uint16Array(cells.i.length);
289-
const queue = new PriorityQueue({comparator: (a, b) => a.p - b.p});
289+
290+
const queue = new FlatQueue();
290291
const cost = [];
291292

292293
const globalGrowthRate = byId("growthRate").valueAsNumber || 1;
@@ -307,12 +308,13 @@ window.BurgsAndStates = (() => {
307308
cells.state[capitalCell] = state.i;
308309
const cultureCenter = cultures[state.culture].center;
309310
const b = cells.biome[cultureCenter]; // state native biome
310-
queue.queue({e: state.center, p: 0, s: state.i, b});
311+
queue.push({e: state.center, p: 0, s: state.i, b}, 0);
311312
cost[state.center] = 1;
312313
}
313314

314315
while (queue.length) {
315-
const next = queue.dequeue();
316+
const next = queue.pop();
317+
316318
const {e, p, s, b} = next;
317319
const {type, culture} = states[s];
318320

@@ -335,7 +337,7 @@ window.BurgsAndStates = (() => {
335337
if (!cost[e] || totalCost < cost[e]) {
336338
if (cells.h[e] >= 20) cells.state[e] = s; // assign state to cell
337339
cost[e] = totalCost;
338-
queue.queue({e, p: totalCost, s, b});
340+
queue.push({e, p: totalCost, s, b}, totalCost);
339341
}
340342
});
341343
}

modules/cultures-generator.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ window.Cultures = (function () {
518518
TIME && console.time("expandCultures");
519519
const {cells, cultures} = pack;
520520

521-
const queue = new PriorityQueue({comparator: (a, b) => a.priority - b.priority});
521+
const queue = new FlatQueue();
522522
const cost = [];
523523

524524
const neutralRate = byId("neutralRate")?.valueAsNumber || 1;
@@ -538,11 +538,11 @@ window.Cultures = (function () {
538538

539539
for (const culture of cultures) {
540540
if (!culture.i || culture.removed || culture.lock) continue;
541-
queue.queue({cellId: culture.center, cultureId: culture.i, priority: 0});
541+
queue.push({cellId: culture.center, cultureId: culture.i, priority: 0}, 0);
542542
}
543543

544544
while (queue.length) {
545-
const {cellId, priority, cultureId} = queue.dequeue();
545+
const {cellId, priority, cultureId} = queue.pop();
546546
const {type, expansionism} = cultures[cultureId];
547547

548548
cells.c[cellId].forEach(neibCellId => {
@@ -566,7 +566,7 @@ window.Cultures = (function () {
566566
if (!cost[neibCellId] || totalCost < cost[neibCellId]) {
567567
if (cells.pop[neibCellId] > 0) cells.culture[neibCellId] = cultureId; // assign culture to populated cell
568568
cost[neibCellId] = totalCost;
569-
queue.queue({cellId: neibCellId, cultureId, priority: totalCost});
569+
queue.push({cellId: neibCellId, cultureId, priority: totalCost}, totalCost);
570570
}
571571
});
572572
}

modules/provinces-generator.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,18 @@ window.Provinces = (function () {
7777
});
7878

7979
// expand generated provinces
80-
const queue = new PriorityQueue({comparator: (a, b) => a.p - b.p});
80+
const queue = new FlatQueue();
8181
const cost = [];
8282

8383
provinces.forEach(p => {
8484
if (!p.i || p.removed || isProvinceLocked(p)) return;
8585
provinceIds[p.center] = p.i;
86-
queue.queue({e: p.center, p: 0, province: p.i, state: p.state});
86+
queue.push({e: p.center, province: p.i, state: p.state, p: 0}, 0);
8787
cost[p.center] = 1;
8888
});
8989

9090
while (queue.length) {
91-
const {e, p, province, state} = queue.dequeue();
91+
const {e, p, province, state} = queue.pop();
9292

9393
cells.c[e].forEach(e => {
9494
if (isProvinceCellLocked(e)) return; // do not overwrite cell of locked provinces
@@ -103,7 +103,7 @@ window.Provinces = (function () {
103103
if (!cost[e] || totalCost < cost[e]) {
104104
if (land) provinceIds[e] = province; // assign province to a cell
105105
cost[e] = totalCost;
106-
queue.queue({e, p: totalCost, province, state});
106+
queue.push({e, province, state, p: totalCost}, totalCost);
107107
}
108108
});
109109
}
@@ -158,9 +158,9 @@ window.Provinces = (function () {
158158
// expand province
159159
const cost = [];
160160
cost[center] = 1;
161-
queue.queue({e: center, p: 0});
161+
queue.push({e: center, p: 0}, 0);
162162
while (queue.length) {
163-
const {e, p} = queue.dequeue();
163+
const {e, p} = queue.pop();
164164

165165
cells.c[e].forEach(nextCellId => {
166166
if (provinceIds[nextCellId]) return;
@@ -173,7 +173,7 @@ window.Provinces = (function () {
173173
if (!cost[nextCellId] || totalCost < cost[nextCellId]) {
174174
if (land && cells.state[nextCellId] === s.i) provinceIds[nextCellId] = provinceId; // assign province to a cell
175175
cost[nextCellId] = totalCost;
176-
queue.queue({e: nextCellId, p: totalCost});
176+
queue.push({e: nextCellId, p: totalCost}, totalCost);
177177
}
178178
});
179179
}
@@ -216,15 +216,15 @@ window.Provinces = (function () {
216216
// check if there is a land way within the same state between two cells
217217
function isPassable(from, to) {
218218
if (cells.f[from] !== cells.f[to]) return false; // on different islands
219-
const queue = [from],
219+
const passableQueue = [from],
220220
used = new Uint8Array(cells.i.length),
221221
state = cells.state[from];
222-
while (queue.length) {
223-
const current = queue.pop();
222+
while (passableQueue.length) {
223+
const current = passableQueue.pop();
224224
if (current === to) return true; // way is found
225225
cells.c[current].forEach(c => {
226226
if (used[c] || cells.h[c] < 20 || cells.state[c] !== state) return;
227-
queue.push(c);
227+
passableQueue.push(c);
228228
used[c] = 1;
229229
});
230230
}

modules/religions-generator.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ window.Religions = (function () {
695695
const {cells, routes} = pack;
696696
const religionIds = spreadFolkReligions(religions);
697697

698-
const queue = new PriorityQueue({comparator: (a, b) => a.p - b.p});
698+
const queue = new FlatQueue();
699699
const cost = [];
700700

701701
// limit cost for organized religions growth
@@ -705,14 +705,14 @@ window.Religions = (function () {
705705
.filter(r => r.i && !r.lock && r.type !== "Folk" && !r.removed)
706706
.forEach(r => {
707707
religionIds[r.center] = r.i;
708-
queue.queue({e: r.center, p: 0, r: r.i, s: cells.state[r.center]});
708+
queue.push({e: r.center, p: 0, r: r.i, s: cells.state[r.center]}, 0);
709709
cost[r.center] = 1;
710710
});
711711

712712
const religionsMap = new Map(religions.map(r => [r.i, r]));
713713

714714
while (queue.length) {
715-
const {e: cellId, p, r, s: state} = queue.dequeue();
715+
const {e: cellId, p, r, s: state} = queue.pop();
716716
const {culture, expansion, expansionism} = religionsMap.get(r);
717717

718718
cells.c[cellId].forEach(nextCell => {
@@ -732,7 +732,7 @@ window.Religions = (function () {
732732
if (cells.culture[nextCell]) religionIds[nextCell] = r; // assign religion to cell
733733
cost[nextCell] = totalCost;
734734

735-
queue.queue({e: nextCell, p: totalCost, r, s: state});
735+
queue.push({e: nextCell, p: totalCost, r, s: state}, totalCost);
736736
}
737737
});
738738
}

modules/zones-generator.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,11 @@ window.Zones = (function () {
209209
const cost = [];
210210
const maxCells = rand(20, 40);
211211

212-
const queue = new PriorityQueue({comparator: (a, b) => a.p - b.p});
213-
queue.queue({e: burg.cell, p: 0});
212+
const queue = new FlatQueue();
213+
queue.push({e: burg.cell, p: 0}, 0);
214214

215215
while (queue.length) {
216-
const next = queue.dequeue();
216+
const next = queue.pop();
217217
if (cells.burg[next.e] || cells.pop[next.e]) cellsArray.push(next.e);
218218
usedCells[next.e] = 1;
219219

@@ -224,7 +224,7 @@ window.Zones = (function () {
224224

225225
if (!cost[nextCellId] || p < cost[nextCellId]) {
226226
cost[nextCellId] = p;
227-
queue.queue({e: nextCellId, p});
227+
queue.push({e: nextCellId, p}, p);
228228
}
229229
});
230230
}
@@ -251,11 +251,11 @@ window.Zones = (function () {
251251
const cost = [];
252252
const maxCells = rand(5, 25);
253253

254-
const queue = new PriorityQueue({comparator: (a, b) => a.p - b.p});
255-
queue.queue({e: burg.cell, p: 0});
254+
const queue = new FlatQueue();
255+
queue.push({e: burg.cell, p: 0}, 0);
256256

257257
while (queue.length) {
258-
const next = queue.dequeue();
258+
const next = queue.pop();
259259
if (cells.burg[next.e] || cells.pop[next.e]) cellsArray.push(next.e);
260260
usedCells[next.e] = 1;
261261

@@ -266,7 +266,7 @@ window.Zones = (function () {
266266

267267
if (!cost[e] || p < cost[e]) {
268268
cost[e] = p;
269-
queue.queue({e, p});
269+
queue.push({e, p}), p;
270270
}
271271
});
272272
}

versioning.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
*
1313
* Example: 1.102.2 -> Major version 1, Minor version 102, Patch version 2
1414
*/
15-
const VERSION = "1.105.20";
15+
16+
const VERSION = "1.105.21";
1617
if (parseMapVersion(VERSION) !== VERSION) alert("versioning.js: Invalid format or parsing function");
1718

1819
{

0 commit comments

Comments
 (0)