aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorame <[email protected]>2023-12-07 16:01:43 -0600
committerame <[email protected]>2023-12-07 16:01:43 -0600
commit79e6627bf92bb2bfd5a103cc4006994309b48246 (patch)
tree49c417e57164b7783c9c02f75b5b6356478594b5
parent3c06c70cccd643109d9ee8dde2767460a68dcfb0 (diff)
string stuff & closed file from reading
-rw-r--r--src/io.c43
-rw-r--r--src/io.h27
-rw-r--r--src/sort.c426
-rw-r--r--src/sort.h20
-rw-r--r--src/table.c451
-rw-r--r--src/table.h25
-rw-r--r--t.lua19
7 files changed, 575 insertions, 436 deletions
diff --git a/src/io.c b/src/io.c
index 3f2922c..6d07a44 100644
--- a/src/io.c
+++ b/src/io.c
@@ -30,7 +30,48 @@ int l_readfile(lua_State* L){
}
out[count] = '\0';
lua_pushstring(L, out);
-
+
+ fclose(fp);
free(out);
return 1;
};
+
+lua_Debug i_get_debug(lua_State* L){
+ lua_Debug ar;
+ lua_getstack(L, 1, &ar);
+ lua_getinfo(L, "nSl", &ar);
+ return ar;
+}
+
+int l_debug(lua_State* L){
+ size_t input_len = 0;
+ char* input = (char*)luaL_checklstring(L, 1, &input_len);
+ lua_Debug debug = i_get_debug(L);
+ printf(color_gray"[ debug ] (%s:%i) %s\n"color_reset, debug.source, debug.currentline, input);
+ return 0;
+}
+
+int l_log(lua_State* L){
+ size_t input_len = 0;
+ char* input = (char*)luaL_checklstring(L, 1, &input_len);
+ lua_Debug debug = i_get_debug(L);
+ printf(color_green"[ log ] (%s:%i)"color_gray" %s\n"color_reset, debug.source, debug.currentline, input);
+ return 0;
+}
+
+int l_warn(lua_State* L){
+ size_t input_len = 0;
+ char* input = (char*)luaL_checklstring(L, 1, &input_len);
+ lua_Debug debug = i_get_debug(L);
+ printf(color_yellow"[ warn ] (%s:%i) %s\n"color_reset, debug.source, debug.currentline, input);
+ return 0;
+}
+
+int l_error(lua_State* L){
+ size_t input_len = 0;
+ char* input = (char*)luaL_checklstring(L, 1, &input_len);
+ lua_Debug debug = i_get_debug(L);
+ printf(color_red"[ error ] (%s:%i) %s\n"color_reset, debug.source, debug.currentline, input);
+ return 0;
+}
+
diff --git a/src/io.h b/src/io.h
index ccfe5cc..7cbca95 100644
--- a/src/io.h
+++ b/src/io.h
@@ -1,8 +1,35 @@
#include "lua.h"
+#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"
+
int l_readfile(lua_State*);
+int l_debug(lua_State*);
+int l_log(lua_State*);
+int l_warn(lua_State*);
+int l_error(lua_State*);
static const luaL_Reg io_function_list [] = {
{"readfile",l_readfile},
+ {"debug",l_debug},
+ {"log",l_log},
+ {"warn",l_warn},
+ {"error",l_error},
+
{NULL,NULL}
};
diff --git a/src/sort.c b/src/sort.c
new file mode 100644
index 0000000..ee9a40b
--- /dev/null
+++ b/src/sort.c
@@ -0,0 +1,426 @@
+#include "table.h"
+#include <stdlib.h>
+
+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 = malloc(sizeof * 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);
+ }
+
+ free(nums);
+ return 1;
+}
+
+void i_merge(double* arr, int b, int m, int e){
+ int n1 = m - b + 1;
+ int n2 = e - m;
+
+ double* left = malloc(sizeof * left * n1);
+ double* right = malloc(sizeof * 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++;
+ }
+
+ free(left);
+ free(right);
+}
+
+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 = malloc(sizeof * 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);
+ }
+
+ free(nums);
+ 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 = malloc(sizeof * 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);
+ }
+ free(nums);
+ return 1;
+}
+
+int l_shellsort(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[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);
+ }
+
+ free(nums);
+ return 1;
+}
+
+int l_bubblesort(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[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);
+ }
+
+ free(nums);
+ return 1;
+}
+
+int l_countingsort(lua_State* L) {
+ luaL_checktype(L, 1, LUA_TTABLE);
+ size_t len = lua_objlen(L,1);
+ int* nums = malloc(sizeof * nums * len);
+ int* out = malloc(sizeof * nums * 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 = calloc(max + 1, sizeof * count);
+
+ 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);
+ }
+
+ free(count);
+ free(nums);
+ free(out);
+ 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 = malloc(sizeof * 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);
+ }
+
+ free(nums);
+ 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 = malloc(sizeof * 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);
+ }
+
+ free(nums);
+ 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 = malloc(sizeof * 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);
+ }
+
+ free(nums);
+ return 1;
+}
+
+int l_bogosort(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[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);
+ }
+
+ free(nums);
+ return 1;
+}
diff --git a/src/sort.h b/src/sort.h
new file mode 100644
index 0000000..1b54164
--- /dev/null
+++ b/src/sort.h
@@ -0,0 +1,20 @@
+#include "lua.h"
+#include "i_util.h"
+#include "i_common.h"
+
+//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
+ //good for large arrays filled with small values
+int l_countingsort(lua_State*); //[int] (arr[N] >= 0) -> arr[N] (least -> greatest)
+
+//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)
diff --git a/src/table.c b/src/table.c
index aae709f..4e99cf1 100644
--- a/src/table.c
+++ b/src/table.c
@@ -1,5 +1,6 @@
#include "table.h"
#include <stdlib.h>
+#include <string.h>
int l_len(lua_State* L) {
luaL_checktype(L, 1, LUA_TTABLE);
@@ -172,426 +173,52 @@ int l_sindexof(lua_State* L) {
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;
+int l_split(lua_State* L){
+ size_t input_len = 0;
+ size_t split_len = 0;
+ char* input = (char*)luaL_checklstring(L, 1, &input_len);
+ char* split = (char*)luaL_checklstring(L, 2, &split_len);
+ size_t table_len = 0;
+ lua_newtable(L);
- 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 = malloc(sizeof * nums * len);
- for(size_t i = 0; i <= len-1; i++){
+ size_t current_len = 0;
+ char current[input_len];
+ memset(current, 0, input_len);
- lua_pushinteger(L,i+1);
- lua_gettable(L,1);
-
- nums[i] = luaL_checknumber(L, -1);
- lua_pop(L,1);
+ for(size_t i = 0; i <= (input_len - 1) - (split_len - 1); i++){
+ int match = 1;
+ for(size_t z = 0; z <= split_len - 1 && match; z++){
+ if(split[z] != input[z + i]) match = 0;
}
-
- 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]);
+ if(match){
+ lua_pushnumber(L, table_len++);
+ lua_pushstring(L, current);
lua_settable(L, -3);
- }
-
- free(nums);
- return 1;
-}
-
-void i_merge(double* arr, int b, int m, int e){
- int n1 = m - b + 1;
- int n2 = e - m;
-
- double* left = malloc(sizeof * left * n1);
- double* right = malloc(sizeof * 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++;
- }
-
- free(left);
- free(right);
-}
-
-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 = malloc(sizeof * 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);
+ memset(current, 0, input_len);
+ current_len = 0;
+ } else {
+ current[current_len] = input[i];
+ current_len++;
}
-
- 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);
- }
-
- free(nums);
- 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 = malloc(sizeof * 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);
- }
- free(nums);
- return 1;
-}
-
-int l_shellsort(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[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);
- }
+ lua_pushnumber(L, table_len++);
+ lua_pushstring(L, current);
+ lua_settable(L, -3);
- free(nums);
- return 1;
+ return 1;
}
-int l_bubblesort(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[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);
- }
-
- free(nums);
- return 1;
-}
-
-int l_countingsort(lua_State* L) {
- luaL_checktype(L, 1, LUA_TTABLE);
- size_t len = lua_objlen(L,1);
- int* nums = malloc(sizeof * nums * len);
- int* out = malloc(sizeof * nums * 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 = calloc(max + 1, sizeof * count);
-
- 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);
- }
-
- free(count);
- free(nums);
- free(out);
- 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 = malloc(sizeof * 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);
- }
-
- free(nums);
- 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 = malloc(sizeof * 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);
- }
-
- free(nums);
- 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 = malloc(sizeof * 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);
- }
-
- free(nums);
- return 1;
-}
-
-int l_bogosort(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[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);
- }
-
- free(nums);
- return 1;
+int l_to_char_array(lua_State* L){
+ size_t input_len = 0;
+ char* input = (char*)luaL_checklstring(L, 1, &input_len);
+ lua_newtable(L);
+
+ for(size_t i = 0; i <= input_len - 1; i++){
+ lua_pushnumber(L, i + 1);
+ char uwu = input[i];
+ lua_pushstring(L, &uwu);
+ lua_settable(L, -3);
+ }
+ return 1;
}
diff --git a/src/table.h b/src/table.h
index f3c68ff..bf27ffc 100644
--- a/src/table.h
+++ b/src/table.h
@@ -1,6 +1,9 @@
#include "lua.h"
#include "i_util.h"
#include "i_common.h"
+#include "sort.h"
+
+void i_shuffle(double*, size_t);
int l_len(lua_State*); //[double+int] -> i
int l_reverse(lua_State*); //[double+int] -> arr[N]
@@ -11,24 +14,8 @@ int l_sum(lua_State*); //[double+int] -> i
int l_indexof(lua_State*); //[double+int], item -> i
int l_sindexof(lua_State*);//[double+int] (greatest -> least), item -> i
-
-//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
- //good for large arrays filled with small values
-int l_countingsort(lua_State*); //[int] (arr[N] >= 0) -> arr[N] (least -> greatest)
-
-//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)
-
+int l_split(lua_State*);
+int l_to_char_array(lua_State*);
static const luaL_Reg array_function_list [] = {
{"len",l_len},
@@ -37,6 +24,8 @@ static const luaL_Reg array_function_list [] = {
{"least",l_least},
{"shuffle",l_shuffle},
{"sum",l_sum},
+ {"split",l_split},
+ {"to_char_array", l_to_char_array},
{"index",l_indexof},
{"sindex",l_sindexof},
diff --git a/t.lua b/t.lua
index 56aa0f7..484d209 100644
--- a/t.lua
+++ b/t.lua
@@ -1,11 +1,20 @@
require "llib"
local a = llib.array
-local test = {5,"meow",3,2,2,1,8}
-test = a.reverse(test)
+--local test = {5,"meow",3,2,2,1,8}
+--test = a.reverse(test)
-for i=1,#test do
- print(test[i])
+--for i=1,#test do
+-- print(test[i])
+--end
+
+--print(a.index(test,"meow"))
+local ww = a.to_char_array("meow awa")--(a.split(llib.io.readfile("c.lua"),"\n"))
+for i=1,#ww do
+ print(i .. " " .. ww[i])
end
-print(a.index(test,"meow"))
+llib.io.debug("meow")
+llib.io.log("hmm")
+llib.io.warn("nuh uh!")
+llib.io.error("void")