Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@ on:
jobs:
make-check:
runs-on: ubuntu-latest
strategy:
matrix:
compiler: [
{cc: gcc, std: c99},
{cc: clang, std: c23},
{cc: g++, std: c++23},
{cc: clang++, std: c++23}
]

steps:
- uses: actions/checkout@v3
- name: Run make check
run: make check
run: make check CC=${{ matrix.compiler.cc }} STD=${{ matrix.compiler.std }}

make-check-valgrind:
runs-on: ubuntu-latest
Expand Down
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ else
endif

CC = gcc
STD = c99
WARN = -Wall -Wextra
DEBUG ?= -g
SRCDIR = src
LIBNAME = libds-lerax
LIBDIR = lib
HEADER = ds-lerax.h
override CFLAGS += $(DEBUG) -pedantic $(WARN) -std=c99 -fPIC
override CFLAGS += $(DEBUG) -pedantic $(WARN) -std=$(STD) -fPIC
SOURCES = $(shell find $(SRCDIR) -iname '*.c')
COMPILED = $(shell find $(SRCDIR) -type f -iname '*.o' -or -iname "*.out" -or -iname "*.a")
TEST_TRASH = $(shell find $(SRCDIR) -type f -iname 'test*.dot*')
Expand Down Expand Up @@ -107,6 +108,9 @@ test: all

check: test

check-cpp:
make check CC=g++ STD=c++23

check/%:
make test -C src/$*

Expand Down
3 changes: 2 additions & 1 deletion src/circle/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CC = gcc
override CFLAGS += -fPIC -g -pedantic -Wall -Wextra -std=c99
STD = c99
override CFLAGS += -fPIC -g -pedantic -Wall -Wextra -std=$(STD)
LDFLAGS = -lm
TARGET = circle
OBJ = $(TARGET).o
Expand Down
3 changes: 2 additions & 1 deletion src/graph/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ endif
CC = gcc
WARN = -Wall -Wextra
DEBUG = -g
override CFLAGS += -fPIC $(DEBUG) -pedantic $(WARN) -std=c99
STD = c99
override CFLAGS += -fPIC $(DEBUG) -pedantic $(WARN) -std=$(STD)
LDFLAGS := -lset -lhash-table -llist -lqueue -lstack -lpqueue -lm
INCLUDE := -I../ -L../list/single -L../hash-table -L../set -L../queue -L../stack -L../pqueue

Expand Down
6 changes: 3 additions & 3 deletions src/graph/acyclical.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ static void graph_dfsa(
}

struct CycleContext* cycle_context_create(Graph *g) {
struct CycleContext *cc = malloc(sizeof(struct CycleContext));
struct CycleContext *cc = (struct CycleContext *) malloc(sizeof(struct CycleContext));
int max_nodes = graph_max_node_id(g) + 1;

cc->topological_sort = stack_create();
cc->cycle_path = stack_create();
cc->exploration = malloc(max_nodes * sizeof(int));
cc->complete = malloc(max_nodes * sizeof(int));
cc->exploration = (int*) malloc(max_nodes * sizeof(int));
cc->complete = (int*) malloc(max_nodes * sizeof(int));
cc->counter_complete = 0;
cc->counter_exploration = 0;
cc->has_cycles = false;
Expand Down
2 changes: 1 addition & 1 deletion src/graph/bfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Iterator* graph_bfs(Graph* g, int start_node) {
queue_insert(q, start_node);
set_add(visited, start_node);

GraphIteratorContext* it_context = malloc(sizeof(GraphIteratorContext));
GraphIteratorContext* it_context = (GraphIteratorContext*) malloc(sizeof(GraphIteratorContext));
it_context->graph = g;
it_context->nodes = graph_nodes_iterator(g);
it_context->visited = visited;
Expand Down
2 changes: 1 addition & 1 deletion src/graph/dfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Iterator* graph_dfs(Graph* g, int start_node) {
stack_push(s, start_node);
set_add(visited, start_node);

GraphIteratorContext* it_context = malloc(sizeof(GraphIteratorContext));
GraphIteratorContext* it_context = (GraphIteratorContext*) malloc(sizeof(GraphIteratorContext));
it_context->graph = g;
it_context->nodes = graph_nodes_iterator(g);
it_context->visited = visited;
Expand Down
6 changes: 4 additions & 2 deletions src/graph/dijkstra.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

Graph* graph_dijkstra(Graph* g, int source) {
int n = graph_max_node_id(g) + 1;
int dist[n];
int prev[n];
int *dist = (int*) malloc(sizeof(int) * n);
int *prev = (int*) malloc(sizeof(int) * n);

for (int i = 0; i < n; i++) {
dist[i] = DIJKSTRA_INFINITY;
Expand Down Expand Up @@ -63,6 +63,8 @@ Graph* graph_dijkstra(Graph* g, int source) {
}

pqueue_free(pq);
free(dist);
free(prev);

return g_new;
}
Expand Down
33 changes: 16 additions & 17 deletions src/graph/graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct Graph {
};

Graph* graph_create() {
Graph *g = malloc(sizeof(Graph));
Graph *g = (Graph*) malloc(sizeof(Graph));
if (!g) {
return NULL;
}
Expand Down Expand Up @@ -43,7 +43,7 @@ Graph* graph_tarjan_create(bool directed) {
}

static void graph_nodes_iterator_free(Iterator *it) {
list_free(it->begin);
list_free((List*)it->begin);
free(it);
}

Expand Down Expand Up @@ -108,31 +108,31 @@ void graph_add_node(Graph *g, int node) {
void graph_add_edge_with_weight(Graph *g, int u, int v, int weight) {
graph_add_node(g, u);
graph_add_node(g, v);
Set *set_u = hash_table_gen_get(g->adj, u, NULL);
Set *set_u = (Set*) hash_table_gen_get(g->adj, u, NULL);
set_add_with_value(set_u, v, weight);
g->weighted = true;

if (!g->directed) {
Set *set_v = hash_table_gen_get(g->adj, v, NULL);
Set *set_v = (Set*) hash_table_gen_get(g->adj, v, NULL);
set_add_with_value(set_v, u, weight);
}
}

void graph_add_edge(Graph *g, int u, int v) {
graph_add_node(g, u);
graph_add_node(g, v);
Set *set_u = hash_table_gen_get(g->adj, u, NULL);
Set *set_u = (Set*) hash_table_gen_get(g->adj, u, NULL);
set_add(set_u, v);

if (!g->directed) {
Set *set_v = hash_table_gen_get(g->adj, v, NULL);
Set *set_v = (Set*) hash_table_gen_get(g->adj, v, NULL);
set_add(set_v, u);
}
}

void graph__remove_edge(Graph *g, int u, int v) {
bool u_exists, v_exists;
Set *set_u = hash_table_gen_get(g->adj, u, &u_exists);
Set *set_u = (Set*) hash_table_gen_get(g->adj, u, &u_exists);
hash_table_gen_get(g->adj, v, &v_exists);

if (u_exists && v_exists) {
Expand All @@ -149,7 +149,7 @@ void graph_remove_edge(Graph *g, int u, int v) {

void graph_remove_node(Graph *g, int node) {
bool exists;
Set *adj_node = hash_table_gen_get(g->adj, node, &exists);
Set *adj_node = (Set*) hash_table_gen_get(g->adj, node, &exists);
if (exists) {
set_free(adj_node);
hash_table_gen_remove(g->adj, node);
Expand All @@ -159,7 +159,7 @@ void graph_remove_node(Graph *g, int node) {
Iterator *it = list_iterator_data(nodes);
while(!iterator_done(it)) {
int u = *(int*)iterator_next(it);
Set *s = hash_table_gen_get(g->adj, u, NULL);
Set *s = (Set*) hash_table_gen_get(g->adj, u, NULL);
set_remove(s, node);
}
list_free(nodes);
Expand All @@ -168,7 +168,7 @@ void graph_remove_node(Graph *g, int node) {

bool graph_has_edge(Graph *g, int u, int v) {
bool exists;
Set *set_u = hash_table_gen_get(g->adj, u, &exists);
Set *set_u = (Set*) hash_table_gen_get(g->adj, u, &exists);
if (!exists) {
return false;
}
Expand All @@ -177,7 +177,7 @@ bool graph_has_edge(Graph *g, int u, int v) {

Set* graph_get_neighbors(Graph *g, int node) {
bool exists;
Set *neighbors = hash_table_gen_get(g->adj, node, &exists);
Set *neighbors = (Set*) hash_table_gen_get(g->adj, node, &exists);
if (!exists) {
return set_create();
}
Expand All @@ -186,7 +186,7 @@ Set* graph_get_neighbors(Graph *g, int node) {

int graph_get_edge_weight(Graph *g, int u, int v) {
bool exists;
Set *set_u = hash_table_gen_get(g->adj, u, &exists);
Set *set_u = (Set*) hash_table_gen_get(g->adj, u, &exists);
if (!exists) {
return -1;
}
Expand All @@ -200,14 +200,13 @@ void graph_print(Graph *g) {
Iterator *it = graph_nodes_iterator(g);
while(!iterator_done(it)) {
int u = *(int*)iterator_next(it);
printf("%d: ", u);
Set *neighbors = hash_table_gen_get(g->adj, u, NULL);
Set *neighbors = (Set*) hash_table_gen_get(g->adj, u, NULL);
if (g->tarjan) {
Iterator *it = set_iterator_items(neighbors);
printf("{");
while (!iterator_done(it)) {
List *list = (List*) iterator_next(it);
printf("%d:%s", list->key, graph_edge_type_name(list->data));
printf("%d:%s", list->key, graph_edge_type_name((EdgeType)list->data));
if (!iterator_done(it)) {
printf(", ");
}
Expand Down Expand Up @@ -252,15 +251,15 @@ void graph_export_to_dot(Graph *g, const char* filename) {
current_node = nodes;
while (current_node) {
int u = (int)(long)current_node->data;
Set *neighbors = hash_table_gen_get(g->adj, u, NULL);
Set *neighbors = (Set*) hash_table_gen_get(g->adj, u, NULL);
Iterator *it = set_iterator(neighbors);
while (!iterator_done(it)) {
int v = *(int*)iterator_next(it);
if (g->directed || u < v) { // Avoid duplicate edges in undirected graphs
fprintf(fp, " %d %s %d", u, g->directed ? "->" : "--", v);
if (g->tarjan) {
int edge_type = set_get_value(neighbors, v);
fprintf(fp, " [label=\"%s\"]", graph_edge_type_name(edge_type));
fprintf(fp, " [label=\"%s\"]", graph_edge_type_name((EdgeType)edge_type));
} else if (g->weighted) {
int weight = set_get_value(neighbors, v);
fprintf(fp, " [label=\"%d\"]", weight);
Expand Down
18 changes: 10 additions & 8 deletions src/graph/tarjan.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,21 @@ static void graph_dfst(
Graph* graph_tarjan(Graph *g) {
struct TarjanContext tc;
int max_node_id = graph_max_node_id(g);
int exploration[max_node_id];
int complete[max_node_id];
int *exploration = (int*) malloc(sizeof(int) * (max_node_id + 1));
for (int i = 0; i <= max_node_id; i++) {
exploration[i] = 0;
}
int *complete = (int*) malloc(sizeof(int) * (max_node_id + 1));
for (int i = 0; i <= max_node_id; i++) {
complete[i] = 0;
}

tc.g_tarjan = graph_tarjan_create(graph_is_directed(g));
tc.counter_complete = 0;
tc.counter_exploration = 0;
tc.exploration = exploration;
tc.complete = complete;

// initialize arrays
for (int i = 0; i <= max_node_id; i++) {
exploration[i] = 0;
complete[i] = 0;
}

// dfs over each node
Iterator *nodes = graph_nodes_iterator(g);
while (!iterator_done(nodes)) {
Expand All @@ -77,5 +77,7 @@ Graph* graph_tarjan(Graph *g) {

}
iterator_free(nodes);
free(exploration);
free(complete);
return tc.g_tarjan;
}
3 changes: 2 additions & 1 deletion src/hash-table/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ endif
CC = gcc
WARN = -Wall -Wextra
DEBUG = -g
override CFLAGS += -fPIC $(DEBUG) -pedantic $(WARN) -std=c99
STD = c99
override CFLAGS += -fPIC $(DEBUG) -pedantic $(WARN) -std=$(STD)
LDFLAGS := -llist -lm
INCLUDE := -I../ -L../list/single

Expand Down
4 changes: 2 additions & 2 deletions src/hash-table/hash-table-gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ static unsigned int hash_int(int key, size_t n_buckets) {
}

HashTableGen* hash_table_gen_create(size_t n_buckets) {
HashTableGen *ht = malloc(sizeof(HashTableGen));
HashTableGen *ht = (HashTableGen*) malloc(sizeof(HashTableGen));
if (!ht) return NULL;

ht->n_buckets = n_buckets;
ht->size = 0;
ht->buckets = calloc(n_buckets, sizeof(ListGen*));
ht->buckets = (ListGen**) calloc(n_buckets, sizeof(ListGen*));
if (!ht->buckets) {
free(ht);
return NULL;
Expand Down
6 changes: 3 additions & 3 deletions src/hash-table/hash-table.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ static unsigned int hash_int(int key, size_t n_buckets) {
}

HashTable* hash_table_create(size_t n_buckets) {
HashTable *ht = malloc(sizeof(HashTable));
HashTable *ht = (HashTable*) malloc(sizeof(HashTable));
check_alloc(ht);

ht->n_buckets = n_buckets;
ht->size = 0;
ht->buckets = calloc(n_buckets, sizeof(List*));
ht->buckets = (List**) calloc(n_buckets, sizeof(List*));
if (!ht->buckets) {
free(ht);
return NULL;
Expand Down Expand Up @@ -182,7 +182,7 @@ void hash_table_free(HashTable *ht) {
}

static void hash_table_iterator_free(Iterator *it) {
list_free(it->begin);
list_free((List*) it->begin);
free(it);
}

Expand Down
2 changes: 1 addition & 1 deletion src/iterator/iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static inline Iterator* iterator_create(
void (*free)(Iterator*),
bool (*done)(Iterator*)
) {
Iterator* it = malloc(sizeof(Iterator));
Iterator* it = (Iterator*) malloc(sizeof(Iterator));
check_alloc(it);
it->container = container;
it->begin = container;
Expand Down
3 changes: 2 additions & 1 deletion src/list/circular/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ else
endif

CC = gcc
override CFLAGS += -fPIC -g -pedantic -Wall -Wextra -std=c99
STD = c99
override CFLAGS += -fPIC -g -pedantic -Wall -Wextra -std=$(STD)
LDFLAGS = -lm
LIST_CIRCULAR = list-circular
TEST_TARGET = test
Expand Down
3 changes: 2 additions & 1 deletion src/list/double/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ else
endif

CC = gcc
override CFLAGS += -fPIC -g -pedantic -Wall -Wextra -std=c99
STD = c99
override CFLAGS += -fPIC -g -pedantic -Wall -Wextra -std=$(STD)
LDFLAGS = --static -lm
LIST_DOUBLE = list-double
TEST_TARGET = test
Expand Down
3 changes: 2 additions & 1 deletion src/list/single/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ else
endif

CC = gcc
override CFLAGS += -fPIC -g -pedantic -Wall -Wextra -std=c99
STD = c99
override CFLAGS += -fPIC -g -pedantic -Wall -Wextra -std=$(STD)
LDFLAGS = -lm

TEST_TARGET = test
Expand Down
Loading