From 89b7b67a13ebb7965cc7f13ad0595e2194a2d34c Mon Sep 17 00:00:00 2001 From: Jannis Hoffmann Date: Wed, 3 Jul 2024 15:48:04 +0200 Subject: add sqmail-4.2.29a --- src/prioq.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/prioq.c (limited to 'src/prioq.c') diff --git a/src/prioq.c b/src/prioq.c new file mode 100644 index 0000000..9559d31 --- /dev/null +++ b/src/prioq.c @@ -0,0 +1,54 @@ +#include "alloc.h" +#include "genalloc.h" +#include "prioq.h" + +GEN_ALLOC_readyplus(prioq,struct prioq_elt,p,len,a,i,n,x,100,prioq_readyplus) + +int prioq_insert(prioq *pq, struct prioq_elt *pe) +{ + int i; + int j; + + if (!prioq_readyplus(pq,1)) return 0; + j = pq->len++; + while (j) { + i = (j - 1)/2; + if (pq->p[i].dt <= pe->dt) break; + pq->p[j] = pq->p[i]; + j = i; + } + pq->p[j] = *pe; + return 1; +} + +int prioq_min(prioq *pq, struct prioq_elt *pe) +{ + if (!pq->p) return 0; + if (!pq->len) return 0; + *pe = pq->p[0]; + return 1; +} + +void prioq_delmin(prioq *pq) +{ + int i; + int j; + int n; + + if (!pq->p) return; + n = pq->len; + if (!n) return; + i = 0; + --n; + + for (;;) { + j = i + i + 2; + if (j > n) break; + if (pq->p[j - 1].dt <= pq->p[j].dt) --j; + if (pq->p[n].dt <= pq->p[j].dt) break; + pq->p[i] = pq->p[j]; + i = j; + } + pq->p[i] = pq->p[n]; + pq->len = n; +} -- cgit v1.2.3