aboutsummaryrefslogtreecommitdiff
path: root/example_vec/new/vec.h
diff options
context:
space:
mode:
authorKimplul <kimi.h.kuparinen@gmail.com>2025-09-01 22:00:02 +0300
committerKimplul <kimi.h.kuparinen@gmail.com>2025-09-01 22:00:02 +0300
commite68480f2fc508ce73cad331ce4e8439e07d25702 (patch)
treebfa99b6f20402ba46230771a0d42172fc2169481 /example_vec/new/vec.h
parent20ce6fa81cd9f55dca3212699d5949f8ebe7d95b (diff)
downloadngc-e68480f2fc508ce73cad331ce4e8439e07d25702.tar.gz
ngc-e68480f2fc508ce73cad331ce4e8439e07d25702.zip
use <> as expansion charactersHEADmaster
+ I kind of like the 'symmetry' of <> being 'self' and <param> being some parameter, but having to type lots of extra <*> is a bit annoying, and might also be an issue for C++. Still, enough functionality for a hashmap example
Diffstat (limited to 'example_vec/new/vec.h')
-rw-r--r--example_vec/new/vec.h18
1 files changed, 9 insertions, 9 deletions
diff --git a/example_vec/new/vec.h b/example_vec/new/vec.h
index 613af51..6842a42 100644
--- a/example_vec/new/vec.h
+++ b/example_vec/new/vec.h
@@ -10,10 +10,10 @@ typedef vec[any type]() {
struct <> {
size_t n;
size_t s;
- type *buf;
+ <type> *buf;
};
- typedef type *<>_iter;
+ typedef <type> *<>_iter;
struct <> <>_create(size_t reserve)
{
@@ -23,7 +23,7 @@ typedef vec[any type]() {
return (struct <>){
.n = 0,
.s = reserve,
- .buf = malloc(reserve * sizeof(type))
+ .buf = malloc(reserve * sizeof(<type>))
};
}
@@ -38,25 +38,25 @@ typedef vec[any type]() {
}
- type *<>_at(struct <> *v, size_t i)
+ <type> *<>_at(struct <> *v, size_t i)
{
assert(i < v->n && "out of vector bounds");
return &v->buf[i];
}
- type *<>_pop(struct <> *v)
+ <type> *<>_pop(struct <> *v)
{
assert(v->n && "attempting to pop empty vector");
v->n--;
return &v->buf[v->n];
}
- type* <>_append(struct <> *v, type n)
+ <type>* <>_append(struct <> *v, <type> n)
{
v->n++;
if (v->n >= v->s) {
v->s = v->s == 0 ? 1 : 2 * v->s;
- v->buf = realloc(v->buf, v->s * sizeof(type));
+ v->buf = realloc(v->buf, v->s * sizeof(<type>));
}
v->buf[v->n - 1] = n;
@@ -80,7 +80,7 @@ typedef vec[any type]() {
while (v->s < v->n)
v->s = v->s == 0 ? 1 : 2 * v->s;
- v->buf = realloc(v->buf, v->s * sizeof(type));
+ v->buf = realloc(v->buf, v->s * sizeof(<type>));
}
void <>_shrink(struct <> *v, size_t n)
@@ -92,7 +92,7 @@ typedef vec[any type]() {
void <>_remove(struct <> *v, size_t i)
{
assert(v->n > i);
- size_t c = sizeof(type) * (v->n - i);
+ size_t c = sizeof(<type>) * (v->n - i);
memcpy(&v->buf[i], &v->buf[i + 1], c);
v->n--;
}