[关闭]
@leviyuan 2017-08-11T08:29:15.000000Z 字数 1641 阅读 312

supermobs.lua.utils

supermobs lua utils


time 时间工具

event 事件工具

string

table

orderdict 自动排序的字典

字典中的key是要排序的数据
value是排序的依据,类型为table数组,每项值为number类型

  1. -- 创建一个排序器,排序依据的table有最多4个值
  2. -- 依次按照升序、升序、降序、降序的方式排列
  3. local compare = utils.table.compare.makearr(true, true, false, false)
  4. -- 使用这个排序器创建一个orderdict
  5. local dict = utils.table.createorderdict(compare)
  6. -- 添加一些数据
  7. dict["chiuan"] = {28, 15, 236, 0}
  8. dict["levi"] = {27, 0, 0, 0}
  9. dict["test"] = {28, 15, 0, 0}
  10. -- 调用接口测试一下
  11. -- 数据的数量
  12. print(dict:getlen()) -- 3
  13. -- 获取排序在第2位的数据
  14. print(dict:getat(2)) -- test
  15. -- 完整遍历一下
  16. for k,v in dict:ipairs() do
  17. print(k, v) -- 1,levi 2,test 3,chiuan
  18. end
  19. -- 删除一项
  20. dict["test"] = nil
  21. -- 下面这样会导致error orderdict do not support same order value
  22. dict["test_error"] = {27, 0, 0, 0}

sortdict 有序的字典

与原生table基本一样,唯一差别就是保证pairs的遍历顺序永远与添加进去的顺序一致

  1. -- 创建一个有序字段
  2. local dict = utils.table.createsortdict()
  3. -- 添加一些数据
  4. dict["levi"] = 27
  5. dict["chiuan"] = {}
  6. dict["test"] = true
  7. -- 使用接口测试一下
  8. print(dict:getfirst()) -- 27
  9. print(dict.count) -- 3
  10. -- 完整遍历
  11. for k,v in dict:pairs() do
  12. print(k, v) -- levi,27 chiuan,table0x0000 test,true
  13. end
  14. -- 删除或更新会让这一项移动到最末位
  15. dict["levi"] = 1
  16. print(dict:getfirst()) -- table0x0000
  17. dict["chiuan"] = {}
  18. print(dict:getfirst()) -- true
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注