- Lua 原有的对 table 的插入和删除 , 但效率较低
- Lua 中队列 的基本操作实现
List = {}
function List.new()
return { first = 0 , last = -1 }
end
function List.pushfirst( liast , value )
local first = list.first - 1
list.first = first
end
function List.pushlast( list , value )
local last = list.last + 1
list.last = last
list.[last] = value
end
function List.popfirst( list )
local first = list.first
if first > list.last then
error("list is empty")
end
local value = list[first]
list[first] = nil
list.first = first + 1
return value
end
function List.poplast( list )
local last = list.last
if list.first > last then
error("list is empty")
end
local value = list[last]
list[last] = nil
list.last = last - 1
return value
end
- 集合与无序组
reserved = { "while" = true , ["end"] = true , ["function"} = true , ["lcoal"] = true }
function Set( list )
local set = {}
for _ , l ipairs( list ) do
set[l] = true
end
return set
end
reserved = Set{ "while" , "end" , "function" , "lcoal" }
- 包
function insert( bag , element )
bag[element] = (bag[element] or 0 ) + 1
end
function remove( bag , element )
local count = bag[element]
bag[element] = ( count and count > 1 ) and count - 1 or nil
end