This commit is contained in:
ame 2023-10-15 22:45:54 -05:00
commit ea8f0940f0
11 changed files with 680 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.so

7
license.md Normal file
View File

@ -0,0 +1,7 @@
Copyright © 2023 <ameliasquires@disroot.org>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

4
readme.md Normal file
View File

@ -0,0 +1,4 @@
build with `clang -shared src/*.c -o llib.so`
useage and docs coming soon:3

15
s.lua Normal file
View File

@ -0,0 +1,15 @@
require "llib"
local a = llib.array
local tab = {}
math.randomseed(os.time())
for i=1,5 do
table.insert(tab,math.random(1,999));-- + math.random(1,999));
end
local l1 = a.bogosort(tab)
for l,i in pairs(llib.array.reverse(l1)) do
print(i)
end

15
src/len.c Normal file
View File

@ -0,0 +1,15 @@
#include "lua.h"
#include "table.h"
int luaopen_llib(lua_State* L) {
lua_newtable(L);
//create <lib>.array functions
lua_newtable(L);
luaL_register(L, NULL, array_function_list);
lua_setfield(L, -2, "array");
//make llib global
lua_setglobal(L, "llib");
return 1;
}

3
src/lua.h Normal file
View File

@ -0,0 +1,3 @@
#include <lua5.1/lua.h>
#include <lua5.1/lualib.h>
#include <lua5.1/lauxlib.h>

514
src/table.c Normal file
View File

@ -0,0 +1,514 @@
#include "table.h"
#include <stdlib.h>
int l_len(lua_State* L) {
luaL_checktype(L, 1, LUA_TTABLE);
lua_pushnumber(L,lua_objlen(L,1));
return 1;
}
int l_reverse(lua_State* L) {
luaL_checktype(L, 1, LUA_TTABLE);
size_t len = lua_objlen(L,1);
double 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);
}
return 1;
}
int l_greatest(lua_State* L) {
luaL_checktype(L, 1, LUA_TTABLE);
size_t len = lua_objlen(L,1);
int touched = 0;
double cur = 0;
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(!touched || t > cur) cur = t;
touched = 1;
lua_pop(L,1);
}
lua_pushnumber(L, cur);
return 1;
}
int l_least(lua_State* L) {
luaL_checktype(L, 1, LUA_TTABLE);
size_t len = lua_objlen(L,1);
int touched = 0;
double cur = 0;
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(!touched || t < cur) cur = t;
touched = 1;
lua_pop(L,1);
}
lua_pushnumber(L, cur);
return 1;
}
void i_shuffle(double* arr, size_t len){
for(size_t i = 0; i < len; i++){
int i2 = rand()%len;
int d = rand()%len;
i_swap(arr[i2], arr[d]);
}
}
int l_shuffle(lua_State* L) {
luaL_checktype(L, 1, LUA_TTABLE);
size_t len = lua_objlen(L,1);
double nums[len];
for(int i = 0; i <= len-1; i++){
lua_pushinteger(L,i+1);
lua_gettable(L,1);
nums[i] = luaL_checknumber(L, -1);
lua_pop(L,1);
}
i_shuffle(nums, len);
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);
}
return 1;
}
int i_hoarepartition(double* arr, int low, int high){
double pivot = arr[((int)((high - low) / 2)) + low];
int i = low - 1;
int j = high + 1;
for(;;){
i++; j--;
while(arr[i] > pivot) i++;
while(arr[j] < pivot) j--;
if(i >= j) return j;
i_swap(arr[i],arr[j]);
}
}
void i_quicksort(double* arr, int low, int high){
if(low >= 0 && high >= 0 && low < high){
int p = i_hoarepartition(arr, low, high);
i_quicksort(arr, low, p);
i_quicksort(arr, p + 1, high);
}
}
int l_quicksort(lua_State* L) {
luaL_checktype(L, 1, LUA_TTABLE);
size_t len = lua_objlen(L,1);
double nums[len];
for(size_t i = 0; i <= len-1; i++){
lua_pushinteger(L,i+1);
lua_gettable(L,1);
nums[i] = luaL_checknumber(L, -1);
lua_pop(L,1);
}
i_quicksort(nums, 0, len - 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);
}
return 1;
}
void i_merge(double* arr, int b, int m, int e){
int n1 = m - b + 1;
int n2 = e - m;
double left[n1];
double right[n2];
for(int i = 0; i < n1; i++) left[i] = arr[b + i];
for(int i = 0; i < n2; i++) right[i] = arr[m + 1 + i];
int l_ind = 0;
int r_ind = 0;
int k = b;
for(; l_ind < n1 && r_ind < n2; k++){
if(left[l_ind] >= right[r_ind]){
arr[k] = left[l_ind];
l_ind++;
} else {
arr[k] = right[r_ind];
r_ind++;
}
}
for(; l_ind < n1; k++){
arr[k] = left[l_ind];
l_ind++;
}
for(; r_ind < n2; k++){
arr[k] = right[r_ind];
r_ind++;
}
}
void i_mergesort(double* arr, int b, int e){
if(b < e){
int mid = (b + e) /2;
i_mergesort(arr, b, mid);
i_mergesort(arr, mid + 1, e);
i_merge(arr, b, mid, e);
}
}
int l_mergesort(lua_State* L) {
luaL_checktype(L, 1, LUA_TTABLE);
size_t len = lua_objlen(L,1);
double nums[len];
for(size_t i = 0; i <= len-1; i++){
lua_pushinteger(L,i+1);
lua_gettable(L,1);
nums[i] = luaL_checknumber(L, -1);
lua_pop(L,1);
}
i_mergesort(nums, 0, len - 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);
}
return 1;
}
void i_heapify(double* arr, int n, int i){
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if(left < n && arr[left] < arr[largest])
largest = left;
if(right < n && arr[right] < arr[largest])
largest = right;
if(largest != i){
i_swap(arr[i],arr[largest]);
i_heapify(arr,n,largest);
}
}
int l_heapsort(lua_State* L) {
luaL_checktype(L, 1, LUA_TTABLE);
size_t len = lua_objlen(L,1);
double nums[len];
for(size_t i = 0; i <= len-1; i++){
lua_pushinteger(L,i+1);
lua_gettable(L,1);
nums[i] = luaL_checknumber(L, -1);
lua_pop(L,1);
}
for(int i = len / 2 - 1; i >= 0; i--)
i_heapify(nums,len,i);
for(int i = len - 1; i >= 0; i--){
i_swap(nums[i],nums[0]);
i_heapify(nums, i, 0);
}
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);
}
return 1;
}
int l_shellsort(lua_State* L) {
luaL_checktype(L, 1, LUA_TTABLE);
size_t len = lua_objlen(L,1);
double nums[len];
for(size_t i = 0; i <= len-1; i++){
lua_pushinteger(L,i+1);
lua_gettable(L,1);
nums[i] = luaL_checknumber(L, -1);
lua_pop(L,1);
}
for(int interval = len/2; interval > 0; interval /=2){
for(int i = interval; i < len; i++){
double temp = nums[i];
int j;
for(j = i; j >= interval && nums[j - interval] < temp; j -= interval){
nums[j] = nums[j - interval];
}
nums[j] = temp;
}
}
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);
}
return 1;
}
int l_bubblesort(lua_State* L) {
luaL_checktype(L, 1, LUA_TTABLE);
size_t len = lua_objlen(L,1);
double nums[len];
for(size_t i = 0; i <= len-1; i++){
lua_pushinteger(L,i+1);
lua_gettable(L,1);
nums[i] = luaL_checknumber(L, -1);
lua_pop(L,1);
}
int n = len;
for(;n > 0;){
int new = 0;
for(int i = 0; i != n-1; i++){
if(nums[i+1]>nums[i]){
double temp = nums[i];
nums[i] = nums[i+1];
nums[i+1] = temp;
new = i+1;
}
}
n = new;
}
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);
}
return 1;
}
int l_countingsort(lua_State* L) {
luaL_checktype(L, 1, LUA_TTABLE);
size_t len = lua_objlen(L,1);
int nums[len], out[len];
int max = 0;
for(int i = 0; i <= len-1; i++){
lua_pushinteger(L,i+1);
lua_gettable(L,1);
nums[i] = luaL_checknumber(L, -1);
out[i] = 0;
if(nums[i]<0) p_fatal("array.countingsort(<table>) requires all indices to be >= 0");
max = max < nums[i] ? nums[i] : max;
lua_pop(L,1);
}
int count[max + 1];
for(size_t i = 0; i <= max; i++) count[i] = 0;
for(size_t i = 0; i < len; i++){
count[nums[i]]++;
}
for(size_t i = 1; i <= max; i++){
count[i] += count[i - 1];
}
for(int i = len - 1; i >= 0; i--){
out[count[nums[i]] - 1] = nums[i];
count[nums[i]]--;
}
lua_newtable(L);
for(size_t i = 0; i != len; i++){
lua_pushnumber(L,i+1);
lua_pushnumber(L,out[i]);
lua_settable(L, -3);
}
return 1;
}
int i_sorted(double* arr, size_t len){
for(size_t i = 0; i != len - 1; i++)
if(arr[i] > arr[i+1]) return 0;
return 1;
}
int l_miraclesort(lua_State* L) {
luaL_checktype(L, 1, LUA_TTABLE);
size_t len = lua_objlen(L,1);
double nums[len];
for(size_t i = 0; i <= len-1; i++){
lua_pushinteger(L,i+1);
lua_gettable(L,1);
nums[i] = luaL_checknumber(L, -1);
lua_pop(L,1);
}
for(;!i_sorted(nums,len););
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);
}
return 1;
}
int l_stalinsort(lua_State* L) {
luaL_checktype(L, 1, LUA_TTABLE);
size_t len = lua_objlen(L,1);
size_t rlen = 0;
double nums[len];
for(size_t i = 0; i <= len-1; i++){
lua_pushinteger(L,i+1);
lua_gettable(L,1);
double n = luaL_checknumber(L, -1);
if(rlen == 0 || nums[rlen - 1] <= n){
nums[rlen] = n;
rlen++;
}
lua_pop(L,1);
}
lua_newtable(L);
for(size_t i = 0; i != rlen; i++){
lua_pushnumber(L,i+1);
lua_pushnumber(L,nums[i]);
lua_settable(L, -3);
}
return 1;
}
void i_slowsort(double* arr, int i, int j){
if(i >= j) return;
int m = (i + j) /2;
i_slowsort(arr, i, m);
i_slowsort(arr, m + 1, j);
if(arr[j] < arr[m]){
i_swap(arr[j], arr[m]);
}
i_slowsort(arr, i, j - 1);
}
int l_slowsort(lua_State* L) {
luaL_checktype(L, 1, LUA_TTABLE);
size_t len = lua_objlen(L,1);
double nums[len];
for(int i = 0; i <= len-1; i++){
lua_pushinteger(L,i+1);
lua_gettable(L,1);
nums[i] = luaL_checknumber(L, -1);
lua_pop(L,1);
}
i_slowsort(nums, 0, len - 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);
}
return 1;
}
int l_bogosort(lua_State* L) {
luaL_checktype(L, 1, LUA_TTABLE);
size_t len = lua_objlen(L,1);
double nums[len];
for(size_t i = 0; i <= len-1; i++){
lua_pushinteger(L,i+1);
lua_gettable(L,1);
nums[i] = luaL_checknumber(L, -1);
lua_pop(L,1);
}
for(;!i_sorted(nums, len);){
i_shuffle(nums, len);
}
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);
}
return 1;
}

50
src/table.h Normal file
View File

@ -0,0 +1,50 @@
#include "lua.h"
#include "util.h"
#define i_swap(A,B) double temp = A; A = B; B = temp;
int l_len(lua_State*); //[double+int] -> i
int l_reverse(lua_State*); //[double+int] -> arr[N]
int l_greatest(lua_State*); //[double+int] -> i
int l_least(lua_State*); //[double+int] -> i
int l_shuffle(lua_State*); //[double+int] -> arr[N]
//comparison sorts
int l_quicksort(lua_State*); //[double+int] -> arr[N] (greatest -> least)
int l_mergesort(lua_State*); //[double+int] -> arr[N] (greatest -> least)
int l_shellsort(lua_State*); //[double+int] -> arr[N] (greatest -> least)
int l_bubblesort(lua_State*); //[double+int] -> arr[N] (greatest -> least)
int l_heapsort(lua_State*); //[double+int] -> arr[N] (greatest -> least)
//non-comparison sorts
int l_countingsort(lua_State*); //[int], arr[N] >= 0 -> arr[N] (greatest -> least)
//esoteric sorts
int l_miraclesort(lua_State*); //[double+int] -> arr[-∞<=N<=∞] (greatest -> least)
int l_stalinsort(lua_State*); //[double+int] -> arr[?<=N] (greatest -> least)
int l_slowsort(lua_State*); //[double+int] -> arr[N] (greatest -> least)
int l_bogosort(lua_State*); //[double+int] -> arr[N] (greatest -> least)
static const luaL_Reg array_function_list [] = {
{"len",l_len},
{"reverse",l_reverse},
{"greatest",l_greatest},
{"least",l_least},
{"shuffle",l_shuffle},
{"quicksort",l_quicksort},
{"mergesort",l_mergesort},
{"shellsort",l_shellsort},
{"bubblesort",l_bubblesort},
{"heapsort",l_heapsort},
{"countingsort",l_countingsort},
{"miraclesort",l_miraclesort},
{"stalinsort",l_stalinsort},
{"slowsort",l_slowsort},
{"bogosort",l_bogosort},
{NULL,NULL}
};

12
src/util.c Normal file
View File

@ -0,0 +1,12 @@
#include "util.h"
#include <stdio.h>
#include <stdlib.h>
void p_fatal(const char* m){
fprintf(stderr, "%s[ fatal ] %s %s\n",color_red, m, color_reset);
exit(EXIT_FAILURE);
}
void p_error(const char* m){
fprintf(stderr, "%s[ error ]%s %s\n",color_red, color_reset, m);
}

19
src/util.h Normal file
View File

@ -0,0 +1,19 @@
#define color_black "\e[30m"
#define color_red "\e[31m"
#define color_green "\e[32m"
#define color_yellow "\e[33m"
#define color_blue "\e[34m"
#define color_magenta "\e[35m"
#define color_cyan "\e[36m"
#define color_lgray "\e[37m"
#define color_gray "\e[90m"
#define color_lred "\e[91m"
#define color_lgreen "\e[92m"
#define color_lyellow "\e[93m"
#define color_lblue "\e[94m"
#define color_lmagenta "\e[95m"
#define color_lcyan "\e[96m"
#define color_white "\e[97m"
#define color_reset "\e[0m"
void p_fatal(const char* m);

40
t.lua Normal file
View File

@ -0,0 +1,40 @@
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.countingsort(tab)
print("counting sort took "..os.clock()-time.."s")
for l,i in pairs(llib.array.reverse(l1)) do
print(l1[l].." "..l2[l].." "..l3[l].." "..l4[l].." "..l5[l])
end
--print(llib.array.greatest(l1))
--local l2 = llib.array.reverse(llib.array.bubblesort(tab))
--[[ aa
for l,i in pairs(l1) do
print(l1[l].." "..l2[l])
end
print(table.concat(l1) == table.concat(l2)) ]]--