From 5c00a8487f44a5a382158e73e4cb4764b13dc18c Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Fri, 22 Feb 2019 10:17:58 -0600 Subject: [PATCH] fourth commit (cleaning up code) --- main.lua | 4 ++++ modules/graph_algorithms.h | 2 ++ modules/graph_standard.c | 1 - modules/graph_utils.c | 12 +++++++----- modules/utils.c | 14 +++++++------- modules/utils.h | 15 +++++++++++++++ 6 files changed, 35 insertions(+), 13 deletions(-) diff --git a/main.lua b/main.lua index 409a62b..19d5536 100644 --- a/main.lua +++ b/main.lua @@ -7,8 +7,12 @@ function love.load() local node2 = graphs.add_node(graph) local node3 = graphs.add_node(graph) + print("NODES", node1, node2, node3) + graphs.add_directed(graph, node1, node2, 2) graphs.add_directed(graph, node2, node3, 4) + + graphs.print_graph(graph) end function love.update() diff --git a/modules/graph_algorithms.h b/modules/graph_algorithms.h index ceead3d..838f9ef 100644 --- a/modules/graph_algorithms.h +++ b/modules/graph_algorithms.h @@ -1,4 +1,6 @@ #ifndef __GRAPH_ALGORITHM_H__ #define __GRAPH_ALGORITHM_H__ + + #endif \ No newline at end of file diff --git a/modules/graph_standard.c b/modules/graph_standard.c index 24405aa..f7b0aea 100644 --- a/modules/graph_standard.c +++ b/modules/graph_standard.c @@ -243,7 +243,6 @@ int del_edge(lua_State *L) } return 0; - } static connect find_node(header head, int node_id) diff --git a/modules/graph_utils.c b/modules/graph_utils.c index cb9e4d1..809b706 100644 --- a/modules/graph_utils.c +++ b/modules/graph_utils.c @@ -3,14 +3,16 @@ int get_nodes(lua_State *L) { header head = (header) lua_touserdata(L, 1); - lua_pop(L, 1); + lua_pop(L, 1); // Take off the pointer parameter - lua_newtable(L); + lua_newtable(L); // Push a new table onto the stack int index = 1; for (connect jogger = head->front; jogger != NULL; jogger = jogger->next_node) { - lua_pushnumber(L, index); - lua_newtable(L); + lua_pushnumber(L, index); // Push the next index of the node, sets up for settable call + lua_newtable(L); // Push on the table that will store each node + + // Add the fields to our node table lua_pushnumber(L, jogger->node_id); lua_setfield(L, 3, "id"); @@ -20,6 +22,7 @@ int get_nodes(lua_State *L) lua_pushnumber(L, jogger->y); lua_setfield(L, 3, "y"); + // Put our node table in the table we are returning lua_settable(L, 1); index++; @@ -64,7 +67,6 @@ int get_edges(lua_State *L) return 1; } - int print_graph(lua_State *L) { header head = (header)lua_touserdata(L,1); diff --git a/modules/utils.c b/modules/utils.c index be76908..d5fa0c1 100644 --- a/modules/utils.c +++ b/modules/utils.c @@ -3,7 +3,7 @@ #include "utils.h" -int top(sq_node_p stack_top) +int sq_top(sq_node_p stack_top) { if(stack_top == NULL) { @@ -13,7 +13,7 @@ int top(sq_node_p stack_top) return stack_top->value; } -int front(sq_node_p queue_front) +int sq_front(sq_node_p queue_front) { if(queue_front == NULL) { @@ -24,7 +24,7 @@ int front(sq_node_p queue_front) } //FUNCTIONS HANDELING CHANGING THE STACK -sq_node_p push(sq_node_p stack_top, int val) +sq_node_p sq_push(sq_node_p stack_top, int val) { sq_node_p new_node = (sq_node_p)malloc(sizeof(sq_node)); new_node->value = val; @@ -32,7 +32,7 @@ sq_node_p push(sq_node_p stack_top, int val) return new_node; } -sq_node_p pop(sq_node_p stack_top) +sq_node_p sq_pop(sq_node_p stack_top) { sq_node_p temp = stack_top; @@ -51,7 +51,7 @@ sq_node_p pop(sq_node_p stack_top) } //FUNCTIONS HANDELING CHANGING THE QUEUE -sq_node_p enque(sq_node_p front_of_queue, int val) +sq_node_p sq_enque(sq_node_p front_of_queue, int val) { sq_node_p new_node = (sq_node_p)malloc(sizeof(sq_node)); new_node->value = val; @@ -74,7 +74,7 @@ sq_node_p enque(sq_node_p front_of_queue, int val) return front_of_queue; } -sq_node_p deque(sq_node_p front_of_queue) +sq_node_p sq_deque(sq_node_p front_of_queue) { if(front_of_queue == NULL) { @@ -91,7 +91,7 @@ sq_node_p deque(sq_node_p front_of_queue) return front_of_queue; } -void print_sq(sq_node_p sq) +void sq_print(sq_node_p sq) { sq_node_p runner = sq; diff --git a/modules/utils.h b/modules/utils.h index 30e4052..eee22b8 100644 --- a/modules/utils.h +++ b/modules/utils.h @@ -8,5 +8,20 @@ typedef struct sq_node { struct sq_node * next; } sq_node, *sq_node_p; +int sq_top(sq_node_p stack_top); +int sq_front(sq_node_p queue_front); + +sq_node_p sq_push(sq_node_p stack_top, int val); +sq_node_p sq_pop(sq_node_p stack_top); +sq_node_p sq_enque(sq_node_p front_of_queue, int val); +sq_node_p sq_deque(sq_node_p front_of_queue); + +void sq_print(sq_node_p sq); + +// Helper macros that make the call site of these functions cleaner +#define push(sq, val) ((sq) = sq_push( (sq), (val))) +#define pop(sq, val) ((sq) = sq_pop( (sq), (val))) +#define enque(sq, val) ((sq) = sq_enque((sq), (val))) +#define deque(sq, val) ((sq) = sq_deque((sq), (val))) #endif \ No newline at end of file -- 2.25.1