summaryrefslogtreecommitdiff
path: root/src/libdatastruct
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/libdatastruct
parenta8e40d0287f05f6e444035431b1cc7b340fe8d79 (diff)
downloadskalibs-ed40369c8610914085d568ec57701a035bf8dd29.tar.xz
Add genqdyn
Diffstat (limited to 'src/libdatastruct')
-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
7 files changed, 71 insertions, 0 deletions
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 ;