shithub: asif

Download patch

ref: 4fe8db493bb0fe8d4f6e280d685e94f6134b17f6
parent: 13a7d1ca0b2e2fe5fae116d92e475ff0bfad6a9a
author: qwx <qwx@sciops.net>
date: Wed Mar 23 19:20:01 EDT 2022

vec: add tail pop function

--- a/asif.h
+++ b/asif.h
@@ -29,6 +29,7 @@
 void	clearvec(Vector*);
 void	popsparsevec(Vector*, int);
 void*	pushsparsevec(Vector*, void*);
+void*	popvec(Vector*);
 void*	pushvec(Vector*, void*);
 void*	newvec(Vector*, int, int);
 
--- a/vec.c
+++ b/vec.c
@@ -67,6 +67,21 @@
 }
 
 void *
+popvec(Vector *v)
+{
+	uchar *p;
+
+	assert(v != nil && v->elsz > 0);
+	if(v->n <= 0)
+		return nil;
+	p = (uchar *)v->p + v->elsz * (v->n - 1);
+	if(v->firstempty > v->n - 1)
+		v->firstempty = v->n - 1;
+	v->n--;
+	return p;
+}
+
+void *
 pushvec(Vector *v, void *e)
 {
 	uchar *p;
@@ -76,6 +91,8 @@
 	memcpy(p, e, v->elsz);
 	v->n++;
 	v->firstempty = v->n;
+	if(v->firstempty > v->n)
+		v->firstempty = v->n;
 	return p;
 }