summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLaurent Bercot <ska-skaware@skarnet.org>2017-12-29 14:16:27 +0000
committerLaurent Bercot <ska-skaware@skarnet.org>2017-12-29 14:16:27 +0000
commited40369c8610914085d568ec57701a035bf8dd29 (patch)
tree26425c589b590306fbba48140fabdd0abe4877bf /src
parenta8e40d0287f05f6e444035431b1cc7b340fe8d79 (diff)
downloadskalibs-ed40369c8610914085d568ec57701a035bf8dd29.tar.xz
Add genqdyn
Diffstat (limited to 'src')
-rw-r--r--src/include/skalibs/datastruct.h1
-rw-r--r--src/include/skalibs/genqdyn.h33
-rw-r--r--src/libdatastruct/genqdyn-internal.h10
-rw-r--r--src/libdatastruct/genqdyn_clean.c11
-rw-r--r--src/libdatastruct/genqdyn_free.c10
-rw-r--r--src/libdatastruct/genqdyn_init.c13
-rw-r--r--src/libdatastruct/genqdyn_pop.c13
-rw-r--r--src/libdatastruct/genqdyn_push.c9
-rw-r--r--src/libdatastruct/genqdyn_zero.c5
9 files changed, 105 insertions, 0 deletions
diff --git a/src/include/skalibs/datastruct.h b/src/include/skalibs/datastruct.h
index 1c7d816..7e2813e 100644
--- a/src/include/skalibs/datastruct.h
+++ b/src/include/skalibs/datastruct.h
@@ -3,6 +3,7 @@
#ifndef DATASTRUCT_H
#define DATASTRUCT_H
+#include <skalibs/genqdyn.h>
#include <skalibs/genset.h>
#include <skalibs/gensetdyn.h>
#include <skalibs/avlnode.h>
diff --git a/src/include/skalibs/genqdyn.h b/src/include/skalibs/genqdyn.h
new file mode 100644
index 0000000..0f7751b
--- /dev/null
+++ b/src/include/skalibs/genqdyn.h
@@ -0,0 +1,33 @@
+/* ISC license. */
+
+#ifndef SKALIBS_GENQDYN_H
+#define SKALIBS_GENQDYN_H
+
+#include <sys/types.h>
+#include <skalibs/stralloc.h>
+
+typedef struct genqdyn_s genqdyn, *genqdyn_ref ;
+struct genqdyn_s
+{
+ stralloc queue ;
+ size_t esize ;
+ size_t head ;
+ unsigned int num ;
+ unsigned int den ;
+} ;
+
+#define GENQDYN_ZERO { .queue = STRALLOC_ZERO, .esize = 1, .head = 0, .num = 0, .den = 1 }
+extern genqdyn const genqdyn_zero ;
+
+#define GENQDYN_INIT(type, n, d) { .queue = STRALLOC_ZERO, .esize = sizeof(type), .head = 0, .num = n, .den = d }
+extern void genqdyn_init (genqdyn *, size_t, unsigned int, unsigned int) ;
+
+#define genqdyn_n(g) ((g)->queue.len / (g)->esize - (g)->head) ;
+
+extern void genqdyn_free (genqdyn *) ;
+extern int genqdyn_push (genqdyn *, void const *) ;
+#define GENQDYN_PEEK(type, g) ((type *)((g)->queue.s + (g)->esize * (g)->head))
+#define genqdyn_peek(g) GENQDYN_PEEK(void, (g))
+extern int genqdyn_pop(genqdyn *) ;
+
+#endif
diff --git a/src/libdatastruct/genqdyn-internal.h b/src/libdatastruct/genqdyn-internal.h
new file mode 100644
index 0000000..17ccc58
--- /dev/null
+++ b/src/libdatastruct/genqdyn-internal.h
@@ -0,0 +1,10 @@
+/* ISC license. */
+
+#ifndef SKALIBS_GENQDYN_INTERNAL_H
+#define SKALIBS_GENQDYN_INTERNAL_H
+
+#include <skalibs/genqdyn.h>
+
+extern void genqdyn_clean (genqdyn *) ;
+
+#endif
diff --git a/src/libdatastruct/genqdyn_clean.c b/src/libdatastruct/genqdyn_clean.c
new file mode 100644
index 0000000..9564d51
--- /dev/null
+++ b/src/libdatastruct/genqdyn_clean.c
@@ -0,0 +1,11 @@
+/* ISC license. */
+
+#include <string.h>
+#include "genqdyn-internal.h"
+
+void genqdyn_clean (genqdyn *g)
+{
+ memmove(g->queue.s, g->queue.s + g->head, g->queue.len - g->head) ;
+ g->queue.len -= g->head ;
+ g->head = 0 ;
+}
diff --git a/src/libdatastruct/genqdyn_free.c b/src/libdatastruct/genqdyn_free.c
new file mode 100644
index 0000000..d2dd007
--- /dev/null
+++ b/src/libdatastruct/genqdyn_free.c
@@ -0,0 +1,10 @@
+/* ISC license. */
+
+#include <skalibs/stralloc.h>
+#include <skalibs/genqdyn.h>
+
+void genqdyn_free (genqdyn *g)
+{
+ stralloc_free(&g->queue) ;
+ *g = genqdyn_zero ;
+}
diff --git a/src/libdatastruct/genqdyn_init.c b/src/libdatastruct/genqdyn_init.c
new file mode 100644
index 0000000..08e3370
--- /dev/null
+++ b/src/libdatastruct/genqdyn_init.c
@@ -0,0 +1,13 @@
+/* ISC license. */
+
+#include <skalibs/stralloc.h>
+#include <skalibs/genqdyn.h>
+
+void genqdyn_init (genqdyn *g, size_t esize, unsigned int num, unsigned int den)
+{
+ g->queue = stralloc_zero ;
+ g->esize = esize ;
+ g->head = 0 ;
+ g->num = num ;
+ g->den = den ;
+}
diff --git a/src/libdatastruct/genqdyn_pop.c b/src/libdatastruct/genqdyn_pop.c
new file mode 100644
index 0000000..c512099
--- /dev/null
+++ b/src/libdatastruct/genqdyn_pop.c
@@ -0,0 +1,13 @@
+/* ISC license. */
+
+#include <errno.h>
+#include <skalibs/genqdyn.h>
+#include "genqdyn-internal.h"
+
+int genqdyn_pop (genqdyn *g)
+{
+ if (g->head >= g->queue.len) return (errno = EINVAL, 0) ;
+ g->head += g->esize ;
+ if (g->den * (g->queue.len - g->head) <= g->num * g->queue.len) genqdyn_clean(g) ;
+ return 1 ;
+}
diff --git a/src/libdatastruct/genqdyn_push.c b/src/libdatastruct/genqdyn_push.c
new file mode 100644
index 0000000..07c5384
--- /dev/null
+++ b/src/libdatastruct/genqdyn_push.c
@@ -0,0 +1,9 @@
+/* ISC license. */
+
+#include <skalibs/stralloc.h>
+#include <skalibs/genqdyn.h>
+
+int genqdyn_push (genqdyn *g, void const *p)
+{
+ return stralloc_catb(&g->queue, (char const *)p, g->esize) ;
+}
diff --git a/src/libdatastruct/genqdyn_zero.c b/src/libdatastruct/genqdyn_zero.c
new file mode 100644
index 0000000..eab70a7
--- /dev/null
+++ b/src/libdatastruct/genqdyn_zero.c
@@ -0,0 +1,5 @@
+/* ISC license. */
+
+#include <skalibs/genqdyn.h>
+
+genqdyn const genqdyn_zero = GENQDYN_ZERO ;