voidptrdance

Example code for a blog post on generic programming in C
git clone https://git.tronto.net/voidptrdance
Download | Log | Files | Refs | README

commit 75986f1a7c5169f0d736f2b34bcd88dd30da1d69
parent a8ad2d8a2fba1f8c51095f5ee8a361de130a3ed4
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Sat, 25 Feb 2023 23:44:39 +0100

Added some code

Diffstat:
APriorityQueue/PriorityQueue.c | 35+++++++++++++++++++++++++++++++++++
MPriorityQueue/PriorityQueue.h | 7+------
APriorityQueue/a.out | 0
APriorityQueue/main1.c | 14++++++++++++++
APriorityQueue/main2.c | 25+++++++++++++++++++++++++
MREADME.md | 3+++
6 files changed, 78 insertions(+), 6 deletions(-)

diff --git a/PriorityQueue/PriorityQueue.c b/PriorityQueue/PriorityQueue.c @@ -0,0 +1,35 @@ +#include <stdlib.h> +#include "PriorityQueue.h" + +typedef struct { + T e[255]; + int n; +} QData; + +PriorityQueue *NewPriorityQueue(TDescription desc) { + PriorityQueue *q = malloc(sizeof(PriorityQueue)); + q->desc = desc; + q->data = malloc(sizeof(QData)); + ((QData *)q->data)->n = 0; + return q; +} + +void Add(PriorityQueue *q, T t) { + QData *qdata = (QData *)q->data; + qdata->e[qdata->n++] = t; +} + +T Pop(PriorityQueue *q) { + QData *qdata = (QData *)q->data; + if (qdata->n == 0) return NULL; + int i, itop = 0; + for (i = 0; i < qdata->n; i++) + if (q->desc.priority(qdata->e[i]) > + q->desc.priority(qdata->e[itop])) + itop = i; + T top = qdata->e[itop]; + for (int j = i; j < qdata->n-1; j++) + qdata->e[j] = qdata->e[j+1]; + qdata->n--; + return top; +} diff --git a/PriorityQueue/PriorityQueue.h b/PriorityQueue/PriorityQueue.h @@ -1,18 +1,13 @@ -/* TODO: maybe v1 with exposed priority queue data, then v2 with - just void * to better encapsulate? */ - typedef void * T; typedef struct { int (*priority)(T); - char *ToString(T); } TDescription; typedef struct { + TDescription desc; void *data; - TDescription; } PriorityQueue; PriorityQueue *NewPriorityQueue(TDescription); void Add(PriorityQueue *, T); T Pop(PriorityQueue *); -void DeletePriorityQueue(PriorityQueue *); diff --git a/PriorityQueue/a.out b/PriorityQueue/a.out Binary files differ. diff --git a/PriorityQueue/main1.c b/PriorityQueue/main1.c @@ -0,0 +1,14 @@ +#include <stdio.h> +#include "PriorityQueue.h" + +int priority_int(void *i) { return *(int *)i; } +TDescription int_desc = { .priority = priority_int }; + +int main() { + PriorityQueue *q = NewPriorityQueue(int_desc); + int x = -12, y = 42; + Add(q, &x); + Add(q, &y); + printf("The highest-priority element is %d\n", *(int *)Pop(q)); + return 0; +} diff --git a/PriorityQueue/main2.c b/PriorityQueue/main2.c @@ -0,0 +1,25 @@ +#include <stdio.h> +#include <string.h> +#include "PriorityQueue.h" + +int count_stars(void *v) { + char *s = (char *)v; + int i = 0; + while (i < strlen(s) && s[i] == '*') i++; + return i; +} +TDescription str_desc = { .priority = count_stars }; + +int main() { + PriorityQueue *q = NewPriorityQueue(str_desc); + char *s = "Boring task"; + char *t = "* Eat dinner"; + char *u = "**** Write blog post!"; + Add(q, s); + Add(q, t); + Add(q, u); + char *first = Pop(q); + char *second = Pop(q); + printf("First thing to do is '%s', followed by '%s'\n", first, second); + return 0; +} diff --git a/README.md b/README.md @@ -1 +1,4 @@ Just some code for a future blog post (stay tuned!) + +TODO: add ToString function to TDescription and use it to print? +Add also ToString for PriorityQueue. ToString for tasks should remove stars.