博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Lua的table处理
阅读量:7239 次
发布时间:2019-06-29

本文共 2741 字,大约阅读时间需要 9 分钟。

-copy自云风的blog

lua 的整体效率是很高的,其中,它的 table 实现的很巧妙为这个效率贡献很大。

lua 的 table 充当了数组和映射表的双重功能,所以在实现时就考虑了这些,让 table 在做数组使用时尽量少效率惩罚。

lua 是这样做的。它把一个 table 分成数组段和 hash 段两个部分。数字 key 一般放在数组段中,没有初始化过的 key 值全部设置为 nil 。当数字 key 过于离散的时候,部分较大的数字 key 会被移到 hash段中去。这个分割线是以数组段的利用率不低于 50% 为准。 0 和 负数做 key 时是肯定放在 hash 段中的。

string 和 number 都放在一起做 hash ,分别有各自的算法,但是 hash 的结果都在一个数值段中。hash 段采用闭散列方法,即,所有的值都存在于表中。如果hash 发生碰撞,额外的数据记在空闲槽位里,而不额外分配空间存放。当整个个表放满后,hash 段会扩大,所有段内的数据将被重新 hash ,重新 hash 后,冲突将大大减少。

这种 table 的实现策略,首先保证的是查找效率。对于把 table 当数组使用时将和 C 数组一样高效。对于 hash 段的值,查找几乎就是计算 hash 值的过程(其中string 的 hash 值是事先计算好保存的),只有在碰撞的时候才会有少许的额外查找时间,而空间也不至于过于浪费。在 hash 表比较满时,插入较容易发生碰撞,这个时候,则需要在表中找到空的插槽。lua 在table 的结构中记录了一个指针顺次从一头向另一头循序插入来解决空槽的检索。每个槽点在记录 next 指针保存被碰撞的 key 的关联性。

整个来说,这种解决方法是非常不错的。

---上点例子

t={
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,6}for i=1,20 do t[i]=nil table.insert(t,20)endfor i=1,30 do print(t[i])end得出的结果:20202020nilnilnilnilnilnilnilnilnilnilnilnilnilnilnilnil111162020202020

---再来一段:

for startIndex = 1,10 do        t = {}        for j = startIndex, 15 do                t[j] = j        end        table.insert(t,"InsertedValue")        tableStr = ""        for i = 1,16 do                if t[i] == nil then                        tableStr = tableStr .."nil" .." | "                else                        tableStr = tableStr ..t[i] .." | "                end        end        print("StartIndex = "..startIndex..", The Table = " .. tableStr)end---[/i][/i][i][i]StartIndex = 1, The Table = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | InsertedValue | StartIndex = 2, The Table = nil | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | InsertedValue | StartIndex = 3, The Table = nil | nil | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | InsertedValue | StartIndex = 4, The Table = nil | nil | nil | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | InsertedValue | StartIndex = 5, The Table = nil | nil | nil | nil | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | InsertedValue | StartIndex = 6, The Table = nil | nil | nil | nil | nil | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | InsertedValue | StartIndex = 7, The Table = nil | nil | nil | nil | nil | nil | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | InsertedValue | StartIndex = 8, The Table = InsertedValue | nil | nil | nil | nil | nil | nil | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | nil | StartIndex = 9, The Table = InsertedValue | nil | nil | nil | nil | nil | nil | nil | 9 | 10 | 11 | 12 | 13 | 14 | 15 | nil | StartIndex = 10, The Table = InsertedValue | nil | nil | nil | nil | nil | nil | nil | nil | 10 | 11 | 12 | 13 | 14 | 15 | nil | [/i][/i][i][i]

 

转载于:https://www.cnblogs.com/Fallever/p/6880719.html

你可能感兴趣的文章
js实现阶乘
查看>>
.net 程序集
查看>>
table font size LaTex
查看>>
IOS UI 01 课堂笔记 -label
查看>>
mootools_Number的内容
查看>>
Hibernate 性能优化之懒加载
查看>>
flask_sqlalchemy
查看>>
采用tcp协议和UDP协议实现简单的聊天功能
查看>>
文件下载的ie11兼容性优化
查看>>
python写的分析mysql binlog日志工具
查看>>
MySQL 系列(二) 你不知道的数据库操作
查看>>
适配,
查看>>
缓存小姐 挡拆,网络请求 不同步 惹的祸,
查看>>
JS计算从某年某月某日到某年某月某日的间隔天数差
查看>>
使用PYTHON解析Wireshark的PCAP文件
查看>>
BarCodeToHTML(条形码toHEML)
查看>>
VC++文件监控 ReadDirectoryChangesW
查看>>
.net开发杂记
查看>>
《自动化技术中的进给电气运动》及学科学习笔记二
查看>>
所思所想目录导航
查看>>