diff options
| author | Mistivia <i@mistivia.com> | 2025-06-09 14:18:20 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2025-06-09 14:18:20 +0800 |
| commit | a690e564d82a46c4e729d88fcc660e4e2f1e6ceb (patch) | |
| tree | c40c404eb61a73168050d7b57fd5bbd5709084dd /src/vec.h | |
| parent | 17690b812b7d59a7f37c858a55b25be91a02ff4c (diff) | |
add show trait
Diffstat (limited to 'src/vec.h')
| -rw-r--r-- | src/vec.h | 13 |
1 files changed, 13 insertions, 0 deletions
@@ -2,6 +2,7 @@ #define ALGDS_VEC_H_ #include <stdlib.h> +#include <stdio.h> #include "type_alias.h" @@ -23,6 +24,7 @@ T* T##Vector_ref(T##Vector *self, size_t n); \ void T##Vector_swap(T##Vector *self, int i, int j); \ T##Vector T##Vector_move(T##Vector *self); \ + void T##Vector_show(T##Vector *self, FILE* fp); \ void T##Vector_free(T##Vector *self); #define VECTOR_IMPL(T) \ @@ -81,6 +83,17 @@ self->cap = 0; \ return dup; \ } \ + void T##Vector_show(T##Vector *self, FILE* fp) { \ + fprintf(fp, "["); \ + for (int i = 0; i < self->size-1; i++) { \ + T##_show(self->buffer[i], fp); \ + fprintf(fp, ", "); \ + } \ + if (self->size > 1) { \ + T##_show(self->buffer[self->size - 1], fp); \ + } \ + fprintf(fp, "]"); \ + } \ void T##Vector_free(T##Vector *self) { free(self->buffer); } VECTOR_DEF(Int); |
