commit 2a48566bef7af181a2e6e5cd735e07f4a76e6370
parent 02e7616bb964b6627d79e7cbb4b40ecf3c1850cc
Author: klewer-martin <martin.cachari@gmail.com>
Date: Sun, 8 May 2022 17:19:34 -0300
Added shuffle handler
Added shuffle handler, an array of functions which it's used to switch
the array shuffle type (i.e. random, reversed, in order, etc)
Diffstat:
8 files changed, 70 insertions(+), 37 deletions(-)
diff --git a/Makefile b/Makefile
@@ -8,10 +8,11 @@ TARGET := sav
.PHONY: all clean
-all: $(TARGET) clean
+all: $(TARGET)
-$(TARGET): $(OBJS) $(HEADERS)
+$(TARGET): $(OBJS)
$(CC) $(CLIBS) $(CFLAGS) -o $@ $^
+ rm -f $(OBJS)
%.o: %.c
$(CC) $(CLIBS) $(CFLAGS) -c $< -o $@
diff --git a/array.c b/array.c
@@ -3,7 +3,13 @@
#include <stdio.h>
#include <time.h>
-void shuffle(Arr *arr) {
+static void (*shuffle_handler[MAX_SHUFFLE])(Arr *arr) = {
+ &arr_in_order,
+ &arr_reversed,
+ &arr_random
+};
+
+void arr_random(Arr *arr) {
srand((unsigned int)time(NULL));
for(size_t i = 0; i < arr->len; i++)
while(!(arr->v[i] = rand() % ARR_MAX));
@@ -11,7 +17,7 @@ void shuffle(Arr *arr) {
printf("ARRAY: Shuffling array done\n");
}
-void reversed(Arr *arr) {
+void arr_reversed(Arr *arr) {
size_t i;
int per_element;
@@ -20,7 +26,7 @@ void reversed(Arr *arr) {
arr->v[i] = (arr->len * per_element) - (i * per_element);
}
-void in_order(Arr *arr) {
+void arr_in_order(Arr *arr) {
size_t i;
int per_element;
@@ -29,3 +35,11 @@ void in_order(Arr *arr) {
for(i = 0; i < arr->len; i++)
arr->v[i] = (i * per_element);
}
+
+void arr_shuffle(Arr *arr) {
+ shuffle_handler[arr->shuffle_sel](arr);
+}
+
+void arr_shuffle_next(Arr *arr) {
+ arr->shuffle_sel = ((arr->shuffle_sel + 1) == MAX_SHUFFLE) ? 0 : arr->shuffle_sel + 1;
+}
diff --git a/array.h b/array.h
@@ -6,13 +6,31 @@
#define ARR_LEN 128
#define ARR_MAX 512
-typedef struct {
+typedef enum {
+ IN_ORDER,
+ REVERSED,
+ RANDOM,
+ MAX_SHUFFLE
+} shuffle_t;
+
+typedef struct _Arr {
int *v;
size_t len;
+ shuffle_t shuffle_sel;
+ void (*shuffle)(struct _Arr *arr);
} Arr;
-void shuffle(Arr *arr);
-void reversed(Arr *arr);
-void in_order(Arr *arr);
+static char *const shuffle_t_str[MAX_SHUFFLE] = {
+ "in_order",
+ "reversed",
+ "random",
+};
+
+void arr_random(Arr *arr);
+void arr_reversed(Arr *arr);
+void arr_in_order(Arr *arr);
+
+void arr_shuffle(Arr *arr);
+void arr_shuffle_next(Arr *arr);
#endif
diff --git a/drw.c b/drw.c
@@ -47,32 +47,32 @@ void drw_status_bar(Drw *drw, SAV *sav) {
/* TODO: create a function which fetchs the status text to be drawn based on the status */
if(sav->status == WELCOME) {
snprintf(drw->bar_text, drw->bar_text_len - 2,
- " Welcome to sorting algorithms visualized [%s] press SPACE to start sorting",
- algo_sel_str[sav->sort_algo]);
+ " Welcome to sorting algorithms visualized [%s] [%s] press SPACE to start sorting",
+ algo_sel_str[sav->sort_algo], shuffle_t_str[sav->arr->shuffle_sel]);
}
else if(sav->status == START) {
snprintf(drw->bar_text, drw->bar_text_len - 2,
- " %-8s [%s] press SPACE to start sorting", sort_status_str[OK],
- algo_sel_str[sav->sort_algo]);
+ " %-8s [%s] [%s] press SPACE to start sorting", sort_status_str[OK],
+ algo_sel_str[sav->sort_algo], shuffle_t_str[sav->arr->shuffle_sel]);
}
else if(sav->status == RUN) {
if(sav->sort_status == PAUSE)
snprintf(drw->bar_text, drw->bar_text_len - 2,
- " %-8s [%s] L: %ld, C: %ld, S: %ld Press SPACE to resume",
+ " %-8s [%s] [%s] L: %ld, C: %ld, S: %ld Press SPACE to resume",
sort_status_str[sav->sort_status],
- algo_sel_str[sav->sort_algo], sav->arr->len, sav->cmps,
- sav->swps);
+ algo_sel_str[sav->sort_algo], shuffle_t_str[sav->arr->shuffle_sel],
+ sav->arr->len, sav->cmps, sav->swps);
else if(sav->sort_status == SORTED)
snprintf(drw->bar_text, drw->bar_text_len - 2,
- " %-8s [%s] L: %ld, C: %ld, S: %ld, done in %lds, extra storage used: %ld Bytes",
+ " %-8s [%s] [%s] L: %ld, C: %ld, S: %ld, done in %lds, extra storage used: %ld Bytes",
sort_status_str[sav->sort_status],
- algo_sel_str[sav->sort_algo],
+ algo_sel_str[sav->sort_algo], shuffle_t_str[sav->arr->shuffle_sel],
sav->arr->len, sav->cmps, sav->swps, (sav->tf - sav->ti), sav->B_used);
else if(sav->sort_status == RUN)
snprintf(drw->bar_text, drw->bar_text_len - 2,
- " %-8s [%s] L: %ld, C: %ld, S: %ld", sort_status_str[sav->sort_status],
- algo_sel_str[sav->sort_algo], sav->arr->len, sav->cmps,
- sav->swps);
+ " %-8s [%s] [%s] L: %ld, C: %ld, S: %ld", sort_status_str[sav->sort_status],
+ algo_sel_str[sav->sort_algo], shuffle_t_str[sav->arr->shuffle_sel],
+ sav->arr->len, sav->cmps, sav->swps);
}
else snprintf(drw->bar_text, drw->bar_text_len - 2, " Exiting ..... ");
@@ -94,7 +94,7 @@ void drw_text(Drw *drw, const char *text, int x, int y) {
SDL_FreeSurface(drw->text_surface);
}
-status_t Drw_new(Drw **drw) {
+status_t Drw_create(Drw **drw) {
SDL_Renderer *rend;
SDL_Window *win;
TTF_Font *font;
diff --git a/drw.h b/drw.h
@@ -13,10 +13,6 @@
#define CMP_COLOR 0x00FFFF00
#define NORM_COLOR 0xFF000000
-/* #define SEL_COLOR 0xFF0000FF // RGBA (A not used rn) */
-/* #define CMP_COLOR 0x00FF00FF */
-/* #define NORM_COLOR 0xFFFFFFFF */
-
#define FONT_SIZE 12
#define FONT_NAME "/home/mk/.local/share/fonts/VictorMono-Bold.ttf"
#define FONT_COLOR 0xBBBBBB
@@ -64,7 +60,7 @@ static char * const sort_status_str[STATUS_MAX] = {
"STOPPED"
};
-status_t Drw_new(Drw **drw);
+status_t Drw_create(Drw **drw);
void Drw_destroy(Drw *drw);
void drw_element(SDL_Renderer *rend, int x, int y, int h);
diff --git a/main.c b/main.c
@@ -45,20 +45,20 @@ int main (void) {
SAV *sav = NULL;
Drw *drw = NULL;
time_t tic, toc;
- void (*reset_array)(Arr *arr);
pthread_t p1 = 0;
status_t st;
- if((st = SAV_new(&sav)) != OK) goto end;
- if((st = Drw_new(&drw)) != OK) goto end;
+ if((st = SAV_create(&sav)) != OK) goto end;
+ if((st = Drw_create(&drw)) != OK) goto end;
- reset_array = &in_order;
-
- /* selecting the sorting algorithm */
+ /* default sorting algorithm */
sav->sort_algo = SELECTION_SORT;
- reset_array(sav->arr);
+ /* default array shuffle mode */
+ sav->arr->shuffle_sel = IN_ORDER;
+
+ arr_shuffle(sav->arr);
sav->status = WELCOME;
sav->sort_status = PAUSE;
@@ -96,7 +96,7 @@ int main (void) {
pthread_join(p1, NULL);
sort_reset_stats(sav);
- reset_array(sav->arr);
+ arr_shuffle(sav->arr);
sav->status = START;
sav->sort_status = PAUSE;
@@ -145,6 +145,9 @@ void check_events(Drw *drw, SAV *sav) {
case SDL_SCANCODE_TAB:
sort_selector(sav);
break;
+ case SDL_SCANCODE_S:
+ arr_shuffle_next(sav->arr);
+ break;
default: break;
}
break;
diff --git a/sav.c b/sav.c
@@ -8,7 +8,7 @@ void sort_reset_stats(SAV *sav) {
sav->cmps = sav->swps = sav->B_used = 0;
}
-status_t SAV_new(SAV **sav) {
+status_t SAV_create(SAV **sav) {
if((*sav = (SAV *)malloc(sizeof(SAV))) == NULL)
return ERROR_MEMORY_ALLOC;
@@ -27,6 +27,7 @@ status_t SAV_new(SAV **sav) {
return ERROR_MEMORY_ALLOC;
(*sav)->arr->len = ARR_LEN;
+ (*sav)->arr->shuffle = NULL;
if((*sav)->arr == NULL) {
return ERROR_MEMORY_ALLOC;
diff --git a/sav.h b/sav.h
@@ -31,7 +31,7 @@ typedef struct {
size_t sort_delay;
} SAV;
-status_t SAV_new(SAV **sav);
+status_t SAV_create(SAV **sav);
void SAV_destroy(SAV *sav);
void sort_reset_stats(SAV *sav);