Showing posts with label Lua. Show all posts
Showing posts with label Lua. Show all posts

September 23, 2006

我设计的Lua数据结构List的接口

代码还没有测试完,先把接口并注释发一下,将来和着代码一起发看起来很不方便。
设计参考了Python的相关函数,但比Python更强。不过python中的数组分片功能不知道能否真的实现,如果不行我只好用其它函数对分片功能的支持来代替。

--域,没有多余的域
List = {n = 0}

--静态方法接口
function List.new (...) end --构造函数,将整个参数列表转为List
local function List.cons (table) end --内部构造函数,将表转为List

--成员方法接口
function List:insert (item, n) end --在位置n之前插入item,默认插在List头部
function List:append (item) end --在list尾部插入item,等价于insert(item, list.n)
function List:remove (item) end --弹出第一个等于item的项并返回
function List:pop (n) end --弹出下标为n的项并返回,默认弹出末项,pop(0)等价于pop(1)
function List:index (item) end --返回第一个值为item的项的下标
function List:qindex (item) end --对于已排序list使用二分法查找
function List:count (item) end --返回list中该item的出现次数
function List:sub (from, to) end --返回从from到to的子list,下标为from的项不包括在内;from默认为0,to默认为list.n
function List:subl (from, l) end --等价于sub(from, from+l)
function List:cut (from, to) end --从list删除下标从from到to的子list并返回...
function List:cutl (from, l) end --等价于cut(from, from+l)
function List:extend (list) end --直接在self后添加list子列表
function List:reverse () end --倒排这个list
--function List:reverse (from, to) end #如果sub函数不能实现,这个功能就十分必要了
function List:sort (func) end --接受一个函数,它比较两个参数arg1
function List:qsort (func) end --sort()的快速排序版本
function List:bsort (func) end --sort()的基数排序版本,要求所有项都为正整数
--下面3个函数是python中的同名函数,充作迭代器
local function List:map(func)
local function UNM () end --每项取负访问器
local function EXP2 () end --每项取平方访问器
local function EXP3 () end --每项取立方访问器
end
local function List:filter(func)
local function DEL (item) end --返回并删除所有等于item的项的过滤器
local function EQUAL (item) end --返回所有等于item的项的过滤器
local function MORE (item) end--返回所有大于item的项的过滤器
local function LESS (item) end--返回所有小于item的项的过滤器
end
local function List:reduce(func)
local function MAX (pre, next) end --求最大递归访问器
local function MIX (pre, next) end --求最小递归访问器
local function MUL (pre, next) end --求list各项之积递归访问器
local function SUM (pre, next) end --求list各项之和递归访问器
local function SUM_MAX (pre, next) end --求最大子序列递归访问器
end

赶快写~~赶快写~~
×补充:我已经有办法解决数组分片问题了——给list加两个域:_stat和_end,再使用函数代理访问下标,玩“虚拟下标”即可!

September 20, 2006

参考AllStartFromGame写了一个排列组合算法

--昨天想了半个小时才想到的一个不错的方法。
--Lua果然号称“只用表的Scheme”,库基本没有。
--代码写得很零乱,还有问题——该死的Lua默认变量为全局,搞得递归有问题。
--不过想法还是很强的,把书本上的知识扩展了一下。
--下次我来扩充一下库,顺便把这个东西改写一下,至少让它能跑起来。

function turn_left (list) --数组左移函数
table.insert(list, list[1])
table.remove(list, 1)
end
function table.extend (t1, t2) --Python中的同名函数
for i = 1, table.getn(t2) do
table.insert(t1, t2[i])
end
end
function list_all (...) --启动函数,接受n个参数作为待排列的项
h_result = list_half(arg)
table.extend(h_result, reverse(h_result))
return h_result
end
function reverse (list) --数组假反序(返回一个新数组)
temp = {}
for i = 0, table.getn(list)-1 do
table.insert(temp, list[table.getn(list)-i])
end
return temp
end
function list_half (list, n) --列表半个排列组合
result = {} or result --保存结果的二维数组
tmp = {list[1], list[2]} or tmp
if n == nil then --处理起始情况
list_half(tmp, table.getn(list)-1)
end
if n == 1 then --处理基准情况——最终排序
for i = 1, table.getn(tmp) do
turn_left(tmp)
table.insert(result, tmp)
end
return result
else --递归列出最一般的排序
table.insert(tmp, list[table.getn(list)-n])
for i = 1, n do
turn_left(tmp)
list_half(tmp, n-1)
end
return result
end
end