commit 9636ab1d6c1b86f2227871806773ef34a432e35b
parent 0e64de8ac00b13a58fd449a628ef521565f0fd90
Author: ssnf <ssnf@ssnf.xyz>
Date: Sun, 31 Aug 2025 21:31:21 +0000
libvec stuff
Diffstat:
6 files changed, 15 insertions(+), 49 deletions(-)
diff --git a/include/vec.h b/include/vec.h
@@ -6,8 +6,9 @@ AUTOLIB(vec)
#define Vecdel(p, n) Vecdel_((Type*)p, sizeof(*p[0]), n)
#define Vecinit(p) Vecinit_((Type*)p, sizeof(*p[0]), NULL, NULL)
#define Vecinitf(p, i, c) Vecinit_((Type*)p, sizeof(*p[0]), i, c)
-#define Vecinsure(p, n) Vecinsure_((Type*)p, sizeof(*p[0]), n);
-#define Veczero(p) Veczero_((Type*)p, sizeof(*p[0]));
+#define Vecinsure(p, n) Vecinsure_((Type*)p, sizeof(*p[0]), n)
+#define Vecsiz(p) Vecsiz_((Type)p)
+#define Veczero(p) Veczero_((Type*)p, sizeof(*p[0]))
typedef void *Type;
@@ -18,11 +19,10 @@ typedef struct {
ulong size;
} Vector;
-Vector* Vec(Type);
Type Vecadd_(Type*, ulong);
void Vecclose_(Type*, ulong);
void Vecdel_(Type*, ulong, ulong);
void Vecinit_(Type*, ulong, void(*)(), void(*)());
void Vecinsure_(Type*, ulong, ulong);
-ulong Vecsiz(Type);
+ulong Vecsiz_(Type);
void Veczero_(Type*, ulong);
diff --git a/man/man3/vec.3 b/man/man3/vec.3
@@ -1,6 +1,6 @@
.TH VEC 3
.SH NAME
-Vec, Vecadd, Vecaddv, Vecclose, Vecdel, Vecinit, Vecinitf, Vecinsure, Veczero, Vecsiz \- generic resizable vectors
+Vecadd, Vecaddv, Vecclose, Vecdel, Vecinit, Vecinitf, Vecinsure, Veczero, Vecsiz \- generic resizable vectors
.SH SYNOPSIS
.B #include <u.h>
.br
@@ -8,7 +8,6 @@ Vec, Vecadd, Vecaddv, Vecclose, Vecdel, Vecinit, Vecinitf, Vecinsure, Veczero, V
.br
.B #include <vec.h>
.PP
-.ft L
.nf
.ta \w'\fL 'u +\w'\fLulong 'u
typedef struct {
@@ -17,13 +16,7 @@ typedef struct {
ulong n;
ulong size;
} Vector;
-.fi
.PP
-.ta \w'\fLVector* 'u
-.B
-Vector* Vec(Type p)
-.PP
-.ta \w'\fLType 'u
.B
Type Vecadd(Type *p)
.PP
@@ -57,12 +50,6 @@ The vector maintains metadata about its size and capacity in a
.B Vector
structure stored immediately before the data.
.PP
-.I Vec
-returns a pointer to the
-.B Vector
-structure for vector
-.IR p .
-.PP
.I Vecinit
initializes a vector pointed to by
.IR p .
@@ -76,20 +63,9 @@ function is called when new elements are added to initialize them.
The
.I close
function is called when elements are removed to clean them up.
-If
-.I init
-is
-.BR nil ,
-new elements are zeroed.
-If
-.I close
-is
-.BR nil ,
-no cleanup is performed.
.PP
.I Vecadd
adds a new element to the vector and returns a pointer to the new element.
-The vector grows automatically if necessary.
.PP
.I Vecaddv
is a convenience macro that adds a value
@@ -102,28 +78,19 @@ to the vector.
removes the element at index
.I n
from the vector.
-If the vector is empty, it does nothing.
-Elements after the removed element are shifted down.
-If a cleanup function was provided to
-.IR Vecinitf ,
-it is called on the removed element.
.PP
.I Vecinsure
ensures that the vector has space for at least
.I n
elements.
-This can be used to preallocate space for better performance.
.PP
.I Veczero
removes all elements from the vector and frees any excess space.
-If a cleanup function was provided, it is called on each element.
The vector remains valid and can be used to add new elements.
.PP
.I Vecclose
frees the vector and all its elements.
-If a cleanup function was provided, it is called on each element.
-The vector pointer is set to
-.BR nil .
+The vector pointer is set to nil.
.PP
.I Vecsiz
returns the number of elements in the vector.
@@ -189,13 +156,7 @@ Vecclose(&v);
.MR malloc (3) ,
.MR qsort (3)
.SH DIAGNOSTICS
+.PP
All functions call
.I sysfatal
-if memory allocation fails or if passed nil or uninitialized vectors.
-.SH NOTES
-The vector implementation uses
-.I Type
-as a generic pointer type.
-This allows the same vector functions to work with any pointer type.
-The macros provide minimal type checking to ensure a pointer to pointer
-is passed rather than a plain pointer.
+if memory allocation fails, or if passed nil or uninitialized vectors.
diff --git a/src/libvec/Vecadd.c b/src/libvec/Vecadd.c
@@ -2,6 +2,8 @@
#include <libc.h>
#include <vec.h>
+extern Vector* Vec(Type);
+
Type
Vecadd_(Type *p, ulong ms)
{
diff --git a/src/libvec/Vecdel.c b/src/libvec/Vecdel.c
@@ -2,6 +2,8 @@
#include <libc.h>
#include <vec.h>
+extern Vector* Vec(Type);
+
void
Vecdel_(Type *p, ulong ms, ulong n)
{
diff --git a/src/libvec/Vecinit.c b/src/libvec/Vecinit.c
@@ -43,7 +43,7 @@ Vecclose_(Type *p, ulong ms)
}
ulong
-Vecsiz(Type p)
+Vecsiz_(Type p)
{
return Vec(p)->n;
}
diff --git a/src/libvec/Veczero.c b/src/libvec/Veczero.c
@@ -1,8 +1,9 @@
-
#include <u.h>
#include <libc.h>
#include <vec.h>
+extern Vector* Vec(Type);
+
void
Veczero_(Type *p, ulong ms)
{