From: Brendan Hansen Date: Thu, 21 Feb 2019 06:21:46 +0000 (-0600) Subject: initial commit X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=1fb12d8508e2e3b5f176716f495cff803bc7d2ff;p=graph-algorithms.git initial commit --- 1fb12d8508e2e3b5f176716f495cff803bc7d2ff diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..753984d --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.so +*.o +*.floo +*.flooignore \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c0f56f0 --- /dev/null +++ b/Makefile @@ -0,0 +1,19 @@ + +CC=gcc +OBJ_FILES=$(shell find ./modules -name '*.c' | grep -Eo '^\.[^\.]*' | xargs printf '%s.o\n') +FLAGS= +LIBS= +TARGET=./graphs.so + +%.o: %.c + $(CC) -c -fPIC -o $@ $< $(FLAGS) $(INCLUDES) + +compile: $(OBJ_FILES) +link: compile + $(CC) -shared -o $(TARGET) $(OBJ_FILES) $(FLAGS) $(LIBS) + +$(TARGET): link +all: $(TARGET) + +clean: + (rm $(OBJS) 2>/dev/null) || true \ No newline at end of file diff --git a/main.lua b/main.lua new file mode 100644 index 0000000..0a1670a --- /dev/null +++ b/main.lua @@ -0,0 +1,10 @@ +require "graphs" + +function love.load() +end + +function love.update() +end + +function love.draw() +end \ No newline at end of file diff --git a/modules/graph_standard.c b/modules/graph_standard.c new file mode 100644 index 0000000..2028492 --- /dev/null +++ b/modules/graph_standard.c @@ -0,0 +1,126 @@ +#include +#include +#include + +#include +#include +#include + +#include "graph_types.h" + +int create_graph(lua_State *L) +{ + header newGraph = (header)malloc(sizeof(head_structure)); + + newGraph->front = NULL; + newGraph->node_count = 0; + + lua_pushlightuserdata(L, (void*)newGraph); + + return 1; +} + +// node number (int) add_node(ptr) +int add_node(lua_State *L) +{ + header head = (header)lua_touserdata(L,1); + connect newNode = (connect)malloc(sizeof(node)); + newNode->next_node = NULL; + + if (head->node_count == 0) + { + newNode->id = 0; + head->front = newNode; + } + else + { + connect walker = head->front; + while (walker->next_node != NULL && walker->id == walker->next_node->id - 1) { + walker = walker->next_node; + } + if (walker->next_node == NULL) + { + newNode->id = head->node_count; + walker->next_node = newNode; + } + else + { + newNode->id = walker->id + 1; + newNode->next_node = walker->next_node; + walker->next_node = newNode; + } + } + head->node_count++; + + lua_pushnumber(L,newNode->id); + + return 1; +} + +int del_node(lua_State *L) +{ + header head = (header)lua_touserdata(L,1); + int nodeID = lua_tointeger(L,2); + + connect walker = head->front; + connect follower = walker; + while(walker->id != nodeID){ + + } + + + return 0; +} + + +int add_directed(lua_State *L) +{ + header head = (header)lua_touserdata(L,1); + int from_node = lua_tointeger(L, 2); + int to_node = lua_tointeger(L, 3); + + connect runner = head->front; + + if(runner == NULL) + { + puts("WTF bro you can't do this"); + return 0; //graph is empty + } + + while(runner->id != from_node) + { + if(runner == NULL) + return 0; //from id is never found + + runner = runner->next_node; + } + + edge edge_run = runner->neighbor_list; + edge new_edge; + + if(edge_run == NULL) + { + edge_run = (edge)malloc(sizeof(edge_struct)); + edge_run->to_id = to_node; + + } + + + return 1; +} + +int luaopen_graphs(lua_State *L) +{ + printf("LOADING MODULE\n"); + + const luaL_Reg functions[] = { + { "create_graph", create_graph }, + { "add_node", add_node }, + { "del_node", del_node }, + { "add_directed", add_directed }, + { NULL, NULL } + }; + + luaL_register(L, "graphs", functions); + return 1; +} \ No newline at end of file diff --git a/modules/graph_types.h b/modules/graph_types.h new file mode 100644 index 0000000..5640bea --- /dev/null +++ b/modules/graph_types.h @@ -0,0 +1,21 @@ +#ifndef __GRAPH_TYPES_H__ +#define __GRAPH_TYPES_H__ + +typedef struct { + int to_id; + int weight; +} edge_struct,*edge; + +typedef struct node { + edge neighbor_list; + int id; + int edge_count; + struct node* next_node; +} node,*connect; + +typedef struct { + connect front; + int node_count; +} head_structure, *header; + +#endif \ No newline at end of file