ref: 88d10fd7cc9f47e67c1813b98984e8b3393a71b3
parent: 0987d105600fc76ee3e0ebe92f13aa0128c2006e
author: qwx <[email protected]>
date: Tue Feb 22 19:08:24 EST 2022
move vector impl to its own file
--- a/mkfile
+++ b/mkfile
@@ -21,6 +21,7 @@
snd.$O\
sv.$O\
util.$O\
+ vec.$O\
HFILES=dat.h fns.h
</sys/src/cmd/mkone
--- a/util.c
+++ b/util.c
@@ -6,10 +6,6 @@
int debug;
-enum{
- Nvecinc = 32,
-};
-
int
max(int a, int b)
{
@@ -45,81 +41,6 @@
vseprint(s, s+sizeof s, fmt, arg);
va_end(arg);
fprint(2, "%s", s);
-}
-
-void
-clearvec(Vector *v)
-{
- if(v->p == nil)
- return;
- memset(v->p, 0, v->totsz);
- v->firstempty = 0;
- v->n = 0;
-}
-
-static void *
-growvec(Vector *v, int n)
-{
- if(n < v->bufsz)
- return (uchar *)v->p + n * v->elsz;
- v->p = erealloc(v->p, v->totsz + Nvecinc * v->elsz, v->totsz);
- v->bufsz += Nvecinc;
- v->totsz += Nvecinc * v->elsz;
- return (uchar *)v->p + n * v->elsz;
-}
-
-void
-popsparsevec(Vector *v, int n)
-{
- assert(v != nil && v->elsz > 0 && n >= 0 && n < v->n);
- memset((uchar *)v->p + n * v->elsz, 0, v->elsz);
- if(n < v->firstempty)
- v->firstempty = n;
-}
-
-/* assumes that zeroed element means empty; could fill with
- * magic values instead */
-void *
-pushsparsevec(Vector *v, void *e)
-{
- int n;
- uchar *p, *q;
-
- assert(v != nil && v->elsz > 0);
- n = v->firstempty;
- p = growvec(v, n);
- for(n++, q=p+v->elsz; n<v->n; n++, q+=v->elsz)
- if(memcmp(p, q, v->elsz) == 0)
- break;
- v->firstempty = n;
- memcpy(p, e, v->elsz);
- v->n++;
- return p;
-}
-
-void *
-pushvec(Vector *v, void *e)
-{
- uchar *p;
-
- assert(v != nil && v->elsz > 0);
- p = growvec(v, v->n);
- memcpy(p, e, v->elsz);
- v->n++;
- v->firstempty = v->n;
- return p;
-}
-
-void *
-newvec(Vector *v, int nel, int elsz)
-{
- assert(v != nil && elsz > 0);
- v->elsz = elsz;
- nel = nel + Nvecinc-1 & ~(Nvecinc-1);
- v->bufsz = nel;
- v->totsz = nel * elsz;
- v->p = emalloc(v->totsz);
- return v->p;
}
char *
--- /dev/null
+++ b/vec.c
@@ -1,0 +1,84 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include "dat.h"
+#include "fns.h"
+
+enum{
+ Nvecinc = 32,
+};
+
+void
+clearvec(Vector *v)
+{
+ if(v->p == nil)
+ return;
+ memset(v->p, 0, v->totsz);
+ v->firstempty = 0;
+ v->n = 0;
+}
+
+static void *
+growvec(Vector *v, int n)
+{
+ if(n < v->bufsz)
+ return (uchar *)v->p + n * v->elsz;
+ v->p = erealloc(v->p, v->totsz + Nvecinc * v->elsz, v->totsz);
+ v->bufsz += Nvecinc;
+ v->totsz += Nvecinc * v->elsz;
+ return (uchar *)v->p + n * v->elsz;
+}
+
+void
+popsparsevec(Vector *v, int n)
+{
+ assert(v != nil && v->elsz > 0 && n >= 0 && n < v->n);
+ memset((uchar *)v->p + n * v->elsz, 0, v->elsz);
+ if(n < v->firstempty)
+ v->firstempty = n;
+}
+
+/* assumes that zeroed element means empty; could fill with
+ * magic values instead */
+void *
+pushsparsevec(Vector *v, void *e)
+{
+ int n;
+ uchar *p, *q;
+
+ assert(v != nil && v->elsz > 0);
+ n = v->firstempty;
+ p = growvec(v, n);
+ for(n++, q=p+v->elsz; n<v->n; n++, q+=v->elsz)
+ if(memcmp(p, q, v->elsz) == 0)
+ break;
+ v->firstempty = n;
+ memcpy(p, e, v->elsz);
+ v->n++;
+ return p;
+}
+
+void *
+pushvec(Vector *v, void *e)
+{
+ uchar *p;
+
+ assert(v != nil && v->elsz > 0);
+ p = growvec(v, v->n);
+ memcpy(p, e, v->elsz);
+ v->n++;
+ v->firstempty = v->n;
+ return p;
+}
+
+void *
+newvec(Vector *v, int nel, int elsz)
+{
+ assert(v != nil && elsz > 0);
+ v->elsz = elsz;
+ nel = nel + Nvecinc-1 & ~(Nvecinc-1);
+ v->bufsz = nel;
+ v->totsz = nel * elsz;
+ v->p = emalloc(v->totsz);
+ return v->p;
+}