diff options
| author | ame <[email protected]> | 2023-12-07 11:52:39 -0600 |
|---|---|---|
| committer | ame <[email protected]> | 2023-12-07 11:52:39 -0600 |
| commit | 3c06c70cccd643109d9ee8dde2767460a68dcfb0 (patch) | |
| tree | 12d00056f1cde619db61584ab43315778ebdef75 | |
| parent | 843d6ce352bd0fbedb9b0945a19747ed409ebd46 (diff) | |
more string friendly
| -rw-r--r-- | src/table.c | 31 | ||||
| -rw-r--r-- | t.lua | 31 |
2 files changed, 17 insertions, 45 deletions
diff --git a/src/table.c b/src/table.c index fa786e7..aae709f 100644 --- a/src/table.c +++ b/src/table.c @@ -9,25 +9,20 @@ int l_len(lua_State* L) { int l_reverse(lua_State* L) { luaL_checktype(L, 1, LUA_TTABLE); + size_t len = lua_objlen(L,1); - double* nums = malloc(sizeof * nums * len); - for(size_t i = 0; i <= len-1; i++){ - - lua_pushinteger(L,i+1); - lua_gettable(L,1); - - nums[len - i - 1] = luaL_checknumber(L, -1); - lua_pop(L,1); - } - lua_newtable(L); - for(size_t i = 0; i != len; i++){ - lua_pushnumber(L,i+1); - lua_pushnumber(L,nums[i]); - lua_settable(L, -3); + for(size_t i = 0; i <= len - 1; i++){ + lua_pushnumber(L,len - i - 1); + lua_gettable(L,1); + + lua_pushnumber(L, i+1); + lua_pushvalue(L, -2); + + lua_settable(L,2); } - free(nums); + lua_pushvalue(L, 2); return 1; } @@ -124,7 +119,6 @@ int l_sum(lua_State* L) { int l_indexof(lua_State* L) { int argc = lua_gettop(L); - double target = luaL_checknumber(L, 2); luaL_checktype(L, 1, LUA_TTABLE); size_t len = lua_objlen(L,1); @@ -137,9 +131,8 @@ int l_indexof(lua_State* L) { for(size_t i = 0; i <= len-1; i++){ lua_pushinteger(L,i+1); lua_gettable(L,1); - - double t = luaL_checknumber(L, -1); - if(t==target){ + + if(lua_rawequal(L, -1, 2)){ lua_pushnumber(L, i); return 1; } @@ -1,32 +1,11 @@ require "llib" local a = llib.array -local tab = {} -math.randomseed(os.time()) -for i=1,19 do - table.insert(tab,math.random(1,999));-- + math.random(1,999)); -end ---print("length of 99999 :\n") ---time = os.clock() ---local l1 = a.quicksort(tab) ---print("quick sort took "..os.clock()-time.."s") ---time = os.clock() ---local l2 = a.mergesort(tab) ---print("merge sort took "..os.clock()-time.."s") ---time = os.clock() ---local l3 = a.shellsort(tab) ---print("shell sort took "..os.clock()-time.."s") ---time = os.clock() ---local l4 = a.bubblesort(tab) ---print("bubble sort took "..os.clock()-time.."s") ---time = os.clock() ---local l5 = a.heapsort(tab) ---print("heap sort took "..os.clock()-time.."s") ---time = os.clock() -local l6 = a.shellsort(tab) +local test = {5,"meow",3,2,2,1,8} +test = a.reverse(test) -for l,i in pairs(l6) do - print(l6[l]) +for i=1,#test do + print(test[i]) end -print(a.sum(l6)) +print(a.index(test,"meow")) |
