ludic/packages/ludic.base/queue.ludic
Orkuncakilkaya 19fcf60599 wip(0.S3): packages and examples migrated again from their pre-0.S sources in one run
ludic migrate state packages <every example program> packages/ludic.lab/example/plate.ludic
  1804 vars into 126 states, 64 into lets; 23498 edits in 460 files

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-25 15:31:34 +03:00

61 lines
1.8 KiB
Text

# ludic.base/queue.ludic - a mechanic reports what HAPPENED as facts in a queue it owns, and the
# game drains it in a later phase. First in, first out; nothing here rolls dice or reorders.
export property Queue<T> {
items: []T = null
id: int = -1
}
# every named queue's pending count, so a frame's end can say which facts nobody read
export state BaseState {
qb_names: []string = null
qb_pending: []int = null
cs_list: []System = null
}
function qb_init(base_st: mut BaseState) -> void {
if base_st.qb_names != null { return }
base_st.qb_names = new []string
base_st.qb_pending = new []int
}
# a queue; the name is only for core_undrained
export function queue_new<T>(base_st: mut BaseState, name: string) -> Queue<T> {
qb_init(base_st)
let q = new Queue<T>
q.items = new []T
q.id = len(base_st.qb_names)
push(base_st.qb_names, name)
push(base_st.qb_pending, 0)
return q
}
export function q_push<T>(base_st: mut BaseState, q: Queue<T>, v: T) -> void {
push(q.items, v)
base_st.qb_pending[q.id] = len(q.items)
}
# the facts in the order they were pushed; the queue is empty afterwards
export function q_drain<T>(base_st: mut BaseState, q: Queue<T>) -> []T {
let out = q.items
q.items = new []T
base_st.qb_pending[q.id] = 0
return out
}
export function q_len<T>(q: Queue<T>) -> int { return len(q.items) }
export function q_clear<T>(base_st: mut BaseState, q: Queue<T>) -> void {
q.items = new []T
base_st.qb_pending[q.id] = 0
}
# the names of the queues still holding facts; call it at the end of a frame, where a fact
# left behind is a route nobody wrote
export function core_undrained(base_st: mut BaseState) -> []string {
qb_init(base_st)
let out = new []string
for i in 0 .. len(base_st.qb_names) {
if base_st.qb_pending[i] > 0 { push(out, base_st.qb_names[i]) }
}
return out
}