commit aa7fd7837533098d0d27a9c20db902efb4cbf033
parent 6b0a1482cd53e253f55af83940232602472e8e31
Author: klewer-martin <martin.cachari@gmail.com>
Date: Tue, 5 Apr 2022 00:44:52 -0300
Added status bar info, comparisons, swaps, time.
Diffstat:
5 files changed, 42 insertions(+), 12 deletions(-)
diff --git a/drw.c b/drw.c
@@ -55,10 +55,23 @@ drw_status_bar(Drw *drw, SAV *sav) {
SDL_SetRenderDrawColor(drw->rend, 0, 0, 0, 0); /* RGBA */
SDL_RenderFillRect(drw->rend, &rect);
- if((sav->status == RUN) || (sav->status == UPDATE))
- drw_text(drw, "SORTING -- insertion sort", 10, drw->h - drw->font_size - 5);
- else if(sav->status == SORTED)
- drw_text(drw, "SORTED -- insertion sort", 10, drw->h - drw->font_size - 5);
+ char status_text[drw-> w / drw->font_size];
+
+ /* sprintf(status_text, "SORTING (insertion sort) C: %ld, S: %ld", sav->comparisons, sav->swaps); */
+ /* sprintf(status_text, "SORTING (insertion sort)", sav->comparisons, sav->swaps); */
+ /* sprintf(status_text, "C: %ld, S: %ld", sav->comparisons, sav->swaps); */
+
+ if((sav->status == RUN) || (sav->status == UPDATE)) {
+ /* sprintf(status_text, "Press SPACE to start sorting the array or ESC/q to quit"); */
+ sprintf(status_text, "SORTING (insertion sort) C: %ld, S: %ld", sav->cmps, sav->swps);
+ drw_text(drw, status_text, 0, drw->h - drw->font_size - 5);
+ } else if(sav->status == SORTED) {
+ sprintf(status_text, "(insertion sort) done in %.2fs, C: %ld, S: %ld",
+ (double)(sav->tf - sav->ti) / CLOCKS_PER_SEC,
+ sav->cmps, sav->swps);
+
+ drw_text(drw, status_text, 0, drw->h - drw->font_size - 5);
+ }
/* SDL_RenderCopy(drw->rend, drw->text_texture, NULL, &text_rect); */
}
@@ -76,7 +89,8 @@ status_t DRW_New(SDL_Renderer *rend, SDL_Window *win, Drw **drw) {
if((*drw = (Drw *)malloc(sizeof(Drw))) == NULL)
return ERROR_MEMORY_ALLOC;
- TTF_Font *font = TTF_OpenFont("/usr/share/fonts/TTF/DejaVuSansMono.ttf", FONT_SIZE);
+ TTF_Font *font = TTF_OpenFont(FONT_NAME, FONT_SIZE);
+
if(!font) fprintf(stderr, "TTF_OpenFont: %s\n", TTF_GetError());
(*drw)->rend = rend;
@@ -84,9 +98,9 @@ status_t DRW_New(SDL_Renderer *rend, SDL_Window *win, Drw **drw) {
(*drw)->font = font;
(*drw)->font_size = FONT_SIZE;
- (*drw)->text_color.r = 255;
- (*drw)->text_color.g = 255;
- (*drw)->text_color.b = 255;
+ (*drw)->text_color.r = (char)(FONT_COLOR >> 16) & 0xFF;
+ (*drw)->text_color.g = (char)(FONT_COLOR >> 8) & 0xFF;
+ (*drw)->text_color.b = (char)(FONT_COLOR) & 0xFF;
SDL_GetWindowSize(win, &((*drw)->w), &((*drw)->h));
diff --git a/drw.h b/drw.h
@@ -12,7 +12,11 @@
#define CMP_COLOR 0x00FFFF00
#define NORM_COLOR 0xFF000000
-#define FONT_SIZE 11
+#define FONT_SIZE 12
+/* #define FONT_NAME "/usr/share/fonts/termite-style/Ubuntu.ttf" */
+#define FONT_NAME "/home/mk/.local/share/fonts/VictorMono-Bold.ttf"
+#define FONT_COLOR 0xBBBBBB
+
#define BAR_HEIGHT 14
typedef struct {
diff --git a/sav.c b/sav.c
@@ -18,7 +18,7 @@ status_t SAV_New(SAV **sav) {
if((*sav = (SAV *)malloc(sizeof(SAV))) == NULL)
return ERROR_MEMORY_ALLOC;
- (*sav)->sel = (*sav)->cmp = 0;
+ (*sav)->sel = (*sav)->cmp = (*sav)->cmps = (*sav)->swps = 0;
(*sav)->status = RUN;
if(((*sav)->arr = (Arr *)malloc(sizeof(Arr))) == NULL)
@@ -69,9 +69,10 @@ main (void) {
for(size_t i = 0; i < sav->arr->len; i++)
while(!(sav->arr->v[i] = rand() % ARR_MAX));
+ /* start sorting thread */
pthread_create(&p1, NULL, &routine_wrapper, (void *)sav);
- /* main loop */
+/* main loop */
while(sav->status != STOP) {
check_events(&(sav->status));
if(sav->status == UPDATE) {
diff --git a/sav.h b/sav.h
@@ -3,11 +3,13 @@
#include "util.h"
+#include <time.h>
#include <SDL2/SDL.h>
typedef struct {
Arr *arr;
- size_t sel, cmp;
+ size_t sel, cmp, cmps, swps;
+ clock_t ti, tf;
status_t status;
} SAV;
diff --git a/sort.c b/sort.c
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdbool.h>
+#include <time.h>
#include "sort.h"
@@ -8,10 +9,13 @@ insertion_sort(SAV *sav) {
int key;
size_t i, j;
+ sav->ti = clock();
for(i = 1; i < sav->arr->len; i++) {
+ sav->cmps += 1;
key = sav->arr->v[i];
j = i - 1;
while((j >= 0) && (sav->arr->v[j] > key)) {
+ sav->cmps += 1;
sav->arr->v[j + 1] = sav->arr->v[j];
j--;
@@ -25,9 +29,14 @@ insertion_sort(SAV *sav) {
sav->arr->v[j + 1] = key;
sav->sel = i;
sav->cmp = j;
+ sav->swps += 1;
+
/* wait 'til main thread updates graphics */
wait_main_thread(&(sav->status));
if(sav->status == STOP) break;
}
+
+ sav->tf = clock();
+
if(sav->status != STOP) sav->status = SORTED;
}