-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: implemented the new table.nkeys Lua API.
This Lua builtin returns the number of elements in a Lua table, i.e., the number of non-nil array elements plus the number of non-nil key-value pairs in a single Lua table. It can be used like this: local nkeys = require "table.nkeys" print(nkeys({})) -- 0 print(nkeys({ "a", nil, "b" })) -- 2 print(nkeys({ dog = 3, cat = 4, bird = nil })) -- 2 print(nkeys({ "a", dog = 3, cat = 4 })) -- 3 This Lua API can be JIT compiled. Signed-off-by: Yichun Zhang (agentzh) <agentzh@gmail.com>
- Loading branch information
Showing
7 changed files
with
244 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
# vim: set ss=4 ft= sw=4 et sts=4 ts=4: | ||
|
||
use lib '.'; | ||
use t::TestLJ; | ||
|
||
plan tests => 3 * blocks(); | ||
|
||
run_tests(); | ||
|
||
__DATA__ | ||
=== TEST 1: hash table, interpreted | ||
--- lua | ||
jit.off() | ||
local new_tab = require "table.new" | ||
local assert = assert | ||
local nkeys = require "table.nkeys" | ||
print(nkeys(new_tab(0, 4))) | ||
print(nkeys({})) | ||
print(nkeys({ cats = 4 })) | ||
print(nkeys({ dogs = 3, cats = 4 })) | ||
print(nkeys({ dogs = nil, cats = 4 })) | ||
--- jv | ||
--- out | ||
0 | ||
0 | ||
1 | ||
2 | ||
1 | ||
--- err | ||
=== TEST 2: hash table, JIT | ||
--- lua | ||
jit.on() | ||
jit.opt.start("minstitch=100000", "hotloop=2") | ||
local new_tab = require "table.new" | ||
local assert = assert | ||
local nkeys = require "table.nkeys" | ||
local list = { | ||
new_tab(0, 4), | ||
{}, | ||
{ cats = 4 }, | ||
{ dogs = 3, cats = 4 }, | ||
{ dogs = nil, cats = 4 }, | ||
} | ||
for i, t in ipairs(list) do | ||
local total = 0 | ||
for i = 1, 10 do | ||
total = total + nkeys(t) | ||
end | ||
print(total) | ||
end | ||
--- jv | ||
--- out | ||
0 | ||
0 | ||
10 | ||
20 | ||
10 | ||
--- err | ||
[TRACE 1 test.lua:18 loop] | ||
[TRACE 2 test.lua:16 -> 1] | ||
=== TEST 3: pure arrays, interpreted | ||
--- lua | ||
jit.off() | ||
local new_tab = require "table.new" | ||
local assert = assert | ||
local nkeys = require "table.nkeys" | ||
print(nkeys(new_tab(5, 0))) | ||
print(nkeys({})) | ||
print(nkeys({ "cats" })) | ||
print(nkeys({ "dogs", 3, "cats", 4 })) | ||
print(nkeys({ "dogs", nil, "cats", 4 })) | ||
--- jv | ||
--- out | ||
0 | ||
0 | ||
1 | ||
4 | ||
3 | ||
--- err | ||
=== TEST 4: pure array, JIT | ||
--- lua | ||
jit.on() | ||
jit.opt.start("minstitch=100000", "hotloop=2") | ||
local new_tab = require "table.new" | ||
local assert = assert | ||
local nkeys = require "table.nkeys" | ||
local list = { | ||
new_tab(0, 4), | ||
{}, | ||
{ 3 }, | ||
{ "cats", 4 }, | ||
{ "dogs", 3, "cats", 4 }, | ||
{ "dogs", nil, "cats", 4 }, | ||
} | ||
for i, t in ipairs(list) do | ||
local total = 0 | ||
for i = 1, 10 do | ||
total = total + nkeys(t) | ||
end | ||
print(total) | ||
end | ||
--- jv | ||
--- out | ||
0 | ||
0 | ||
10 | ||
20 | ||
40 | ||
30 | ||
--- err | ||
[TRACE 1 test.lua:19 loop] | ||
[TRACE 2 test.lua:17 -> 1] | ||
=== TEST 5: mixing array and hash table, interpreted | ||
--- lua | ||
jit.off() | ||
local new_tab = require "table.new" | ||
local assert = assert | ||
local nkeys = require "table.nkeys" | ||
print(nkeys({ cats = 4, 5, 6 })) | ||
print(nkeys({ nil, "foo", dogs = 3, cats = 4 })) | ||
--- jv | ||
--- out | ||
3 | ||
3 | ||
--- err | ||
=== TEST 6: mixing array & hash, JIT | ||
--- lua | ||
jit.on() | ||
jit.opt.start("minstitch=100000", "hotloop=2") | ||
local new_tab = require "table.new" | ||
local assert = assert | ||
local nkeys = require "table.nkeys" | ||
local list = { | ||
{ cats = 4, 5, 6 }, | ||
{ nil, "foo", dogs = 3, cats = 4 }, | ||
} | ||
for i, t in ipairs(list) do | ||
local total = 0 | ||
for i = 1, 10 do | ||
total = total + nkeys(t) | ||
end | ||
print(total) | ||
end | ||
--- jv | ||
--- out | ||
30 | ||
30 | ||
--- err | ||
[TRACE 1 test.lua:15 loop] | ||
[TRACE 2 test.lua:13 -> 1] |