Lua lists are limited when things need to be added and removed frequently from random indices. This PR adds an API for a faster list which is backed by a C array. Allocations to the list are O(n), and removals are O(1).
The Slot List API is based on a ring-buffer, where a head scans the array for the next free "slot" when an item is added to the list. If no slots are free then pushing to the list is an error. This also means that the iteration order does not respect the order items were pushed.
The Slot API is:
new(n) creates a new Slot List with capacity = n
push(v, st) sets the first free slot to v, and sets that slot's state from 0 (SLOT_FREE) to st
setState(i, st) sets the state of slot index i to st. if st == 0 (SLOT_FREE) then the slot is freed and we release the Lua reference to the value
each(fn) iterates over slots and calls fn for each slot where the state is > 0 (SLOT_FREE). fn has 3 arguments: (value, current_state, index)
eachState(st, fn) like each() but only calls fn for each slot with state == st
This PR also cleans up how core libraries are initialized. This is the first step in what will eventually be a reshuffle of names, but for now changes in ps2init.lua preserves compatibility.
Resolves #48
Lua lists are limited when things need to be added and removed frequently from random indices. This PR adds an API for a faster list which is backed by a C array. Allocations to the list are O(n), and removals are O(1).
The Slot List API is based on a ring-buffer, where a head scans the array for the next free "slot" when an item is added to the list. If no slots are free then pushing to the list is an error. This also means that the iteration order does not respect the order items were pushed.
The Slot API is:
(value, current_state, index)
each()
but only calls fn for each slot with state == stThis PR also cleans up how core libraries are initialized. This is the first step in what will eventually be a reshuffle of names, but for now changes in
ps2init.lua
preserves compatibility.TODO