@@ -1284,6 +1284,52 @@ def Setting.keys(s: State) -> List<&2, String>:
12841284 case State{_, _, _, _, _, sg, _, _, _, _, _}:
12851285 Map.keys(&2, String, sg)
12861286
1287+ # The keys of a String map that start with pre, without walking the others.
1288+ # Base's Map is a crit-bit trie (Map.bit: char ci of a key takes bits
1289+ # 33·ci .. 33·ci+32), so every key under a node at bit p agrees on its first
1290+ # p/33 chars: once p ≥ 33·|pre|, any one of them says whether all start
1291+ # with pre. Order: as Map.keys.
1292+ def Pfx.first(m: Map<&2, String>) -> String:
1293+ match m:
1294+ case MTip{}:
1295+ ""
1296+ case MLeaf{k, _}:
1297+ k
1298+ case MNode{_, MTip{}, hi}:
1299+ Pfx.first(hi)
1300+ case MNode{_, lo, _}:
1301+ Pfx.first(lo)
1302+
1303+ # whether some key under m may start with pre (bits: 33·|pre|)
1304+ def Pfx.may(+m: Map<&2, String>, +pre: String, +bits: Nat) -> Bool:
1305+ match m:
1306+ case MTip{}:
1307+ False{}
1308+ case MLeaf{k, _}:
1309+ String.starts_with(k, pre)
1310+ case MNode{p, _, _}:
1311+ Bool.or(Nat.is_lt(p, bits), String.starts_with(Pfx.first(m), pre))
1312+
1313+ def Pfx.go(m: Map<&2, String>, may: Bool, +pre: String, +bits: Nat, acc: List<&2, String>) -> List<&2, String>:
1314+ match m may:
1315+ case _ False{}:
1316+ acc
1317+ case MTip{} True{}:
1318+ acc
1319+ case MLeaf{k, _} True{}:
1320+ k <> acc
1321+ case MNode{_, +lo, +hi} True{}:
1322+ Pfx.go(lo, Pfx.may(lo, pre, bits), pre, bits, Pfx.go(hi, Pfx.may(hi, pre, bits), pre, bits, acc))
1323+
1324+ def Pfx.keys(+m: Map<&2, String>, +pre: String) -> List<&2, String>:
1325+ Pfx.go(m, Pfx.may(m, pre, Nat.mul(33n, String.length(pre))), pre, Nat.mul(33n, String.length(pre)), Nil{})
1326+
1327+ # the settings' keys that start with pre
1328+ def Setting.keys.pre(s: State, +pre: String) -> List<&2, String>:
1329+ match s:
1330+ case State{_, _, _, _, _, sg, _, _, _, _, _}:
1331+ Pfx.keys(sg, pre)
1332+
12871333# The "restart.continue" setting: after a restart, a turn the restart cut
12881334# short goes on by itself (on unless "off"; hub.bend's Carry)
12891335def Carry.on(+s: State) -> Bool:
@@ -1652,6 +1698,20 @@ def Locks.names(s: State) -> List<&2, String>:
16521698def Locks.get(s: State, +name: String) -> LK.NamedLock:
16531699 LK.Lock.read(Maybe.default(&2, J.Json, J.Json.parse(Setting.get(s, "lock." ++ name, "")), J.Null{}))
16541700
1701+ # the lock is free with no one waiting (never held, or released): what
1702+ # Locks.get reads as LK.Lock.empty(), told from its setting's text without
1703+ # parsing it. A real log keeps every lock name ever used (72), nearly all
1704+ # idle, and every step and every sidebar row read each one (laws
1705+ # locks_idle_*).
1706+ def Locks.idle.text() -> String:
1707+ J.Json.show(LK.Lock.json(LK.Lock.empty()))
1708+
1709+ def Locks.idle.k(+t: String) -> Bool:
1710+ Bool.or(String.is_empty(t), String.eq(t, Locks.idle.text()))
1711+
1712+ def Locks.idle(s: State, +name: String) -> Bool:
1713+ Locks.idle.k(Setting.get(s, "lock." ++ name, ""))
1714+
16551715# deploy_main's state, one JSON line (core/ship.bend)
16561716def Ship.fold.asked(+s: State, +by: String, +cover: String) -> State:
16571717 State.setting(s, "deploy.ship", SH.Ship.show(SH.Ship.asked(SH.Ship.read(Setting.get(s, "deploy.ship", "")), by, cover)))
@@ -3131,9 +3191,24 @@ def Work.rows.go(ks: List<&2, String>, +s: State, m: Map<&2, Nat>) -> Map<&2, Na
31313191 case +k <> t:
31323192 Work.rows.go(t, s, Work.rows.put(s, Work.rows.id(k, "bg."), Work.rows.put(s, Work.rows.id(k, "watch."), m)))
31333193
3134- def Work.rows.map(+s: State) -> Map<&2, Nat>:
3194+ # every thread's rows by walking every setting (the specification of
3195+ # Work.rows.map; test/work_rows_test.bend holds them equal)
3196+ def Work.rows.map.all(+s: State) -> Map<&2, Nat>:
31353197 Work.rows.go(Setting.keys(s), s, Map.new(&2, Nat))
31363198
3199+ def Work.rows.ids(ks: List<&2, String>, +n: Nat, +s: State, m: Map<&2, Nat>) -> Map<&2, Nat>:
3200+ match ks:
3201+ case Nil{}:
3202+ m
3203+ case +k <> t:
3204+ Work.rows.ids(t, n, s, Work.rows.put(s, String.drop(k, n), m))
3205+
3206+ # Work.rows.map.all reading only the settings named "bg.*" and "watch.*"
3207+ # (Pfx.keys): the sidebar asks it every frame, and walking every setting
3208+ # (10k+ on a real log, each key sliced twice) grew each frame with history
3209+ def Work.rows.map(+s: State) -> Map<&2, Nat>:
3210+ Work.rows.ids(Setting.keys.pre(s, "watch."), 6n, s, Work.rows.ids(Setting.keys.pre(s, "bg."), 3n, s, Map.new(&2, Nat)))
3211+
31373212def Work.kids.at.k(r: Map<&2, Nat> & Nat) -> Nat:
31383213 (_, n) = r
31393214 n
0 commit comments