summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTor Andersson <tor@ccxvii.net>2023-09-23 11:49:51 +0200
committerTor Andersson <tor@ccxvii.net>2023-12-10 18:16:55 +0100
commit40cc08c41a5d583f880e582693f1749a95fca0be (patch)
tree015bc7f401d36eb15e1e1ea39794e78c9af3332c
parenta3bb2a2886cfa9da53fe1c90e4013875ee11febc (diff)
downloadplantagenet-40cc08c41a5d583f880e582693f1749a95fca0be.tar.gz
Rejig map_search a bit.
Use the "set" functions instead of array.push and array.includes. Update "seen" when adding entries to search queue to avoid the extra loop that scanned the queue for duplicates. Don't create lots of temporary arrays that create needless work for the garbage collector.
-rw-r--r--rules.js38
1 files changed, 17 insertions, 21 deletions
diff --git a/rules.js b/rules.js
index ccb9378..5cdc14d 100644
--- a/rules.js
+++ b/rules.js
@@ -3534,32 +3534,28 @@ function find_parley_targets(lord, acceptfn, adjacentfn) {
return results
}
-function* map_search(lord, acceptfn, adjacentfn, prune=true) {
- let here = get_lord_locale(lord)
- let locales = [{locale:here, distance: 0}]
-
- let seen = []
-
- while (true) {
- if (locales.length === 0)
- return
+function* map_search(lord, acceptfn, adjacentfn, prune = true) {
+ let start = get_lord_locale(lord)
+ let queue = [ { locale: start, distance: 0 } ]
+ let seen = [ start ]
- let loc = locales.shift()
- seen.push(loc.locale)
+ while (queue.length > 0) {
+ let item = queue.shift()
- if (acceptfn(loc)) {
- yield loc
+ if (acceptfn(item)) {
+ yield item
if (prune)
continue
}
- if (is_friendly_locale(loc.locale)) {
- let distance = loc.distance + 1
- locales = locales.concat(
- adjacentfn(loc.locale, lord)
- .filter(l => !seen.includes(l))
- .filter(l => !locales.some((r) => r.locale === l))
- .map(x => {return {locale: x, distance: distance }})
- )
+
+ if (is_friendly_locale(item.locale)) {
+ let distance = item.distance + 1
+ for (let next of adjacentfn(item.locale, lord)) {
+ if (!set_has(seen, next)) {
+ set_add(seen, next)
+ queue.push({ locale: next, distance })
+ }
+ }
}
}
}