From 28bbf00908f6518d5a8470b0081a298d3c22f024 Mon Sep 17 00:00:00 2001 From: DTB Date: Wed, 20 Mar 2024 18:18:53 -0600 Subject: [PATCH] FatPtr_index, FatPtr_prepend --- libfatptr/libfatptr.c | 25 +++++++++++++++++++++---- libfatptr/libfatptr.h | 15 +++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/libfatptr/libfatptr.c b/libfatptr/libfatptr.c index 92ae739..39a64b9 100644 --- a/libfatptr/libfatptr.c +++ b/libfatptr/libfatptr.c @@ -5,8 +5,8 @@ struct FatPtr * FatPtr_append(struct FatPtr *p, FatPtr_scalar_t c){ FatPtr_vector_t t; - if(p == NULL || (p->s + (sizeof *(p->v)) > p->a - && FatPtr_grow(p, sizeof FatPtr_scalar_t) == NULL)) + if(p == NULL || (p->s + (sizeof (FatPtr_scalar_t)) > p->a + && FatPtr_grow(p, sizeof (FatPtr_scalar_t)) == NULL)) return NULL; (p->v)[(p->s)++] = c; return p; @@ -15,7 +15,7 @@ FatPtr_append(struct FatPtr *p, FatPtr_scalar_t c){ struct FatPtr * FatPtr_construct(struct FatPtr *p){ - return FatPtr_grow(FatPtr_initialize(p), sizeof FatPtr_scalar_t); + return FatPtr_grow(FatPtr_initialize(p), sizeof (FatPtr_scalar_t)); } struct FatPtr * @@ -50,6 +50,23 @@ FatPtr_initialize(struct FatPtr *p){ return p; } +FatPtr_vector_t +FatPtr_index(struct FatPtr *p, size_t index){ + + return p == NULL + ? NULL + : &p->v[index]; +} + +struct FatPtr * +FatPtr_prepend(struct FatPtr *p, FatPtr_vector_t s, size_t units){ + size_t i; + + if(FatPtr_shiftleft(p, units) != NULL) + for(i = 0; units --> 0; p->v[i] = *s++); + return p; +} + struct FatPtr * FatPtr_shiftleft(struct FatPtr *p, size_t units){ size_t i; @@ -64,7 +81,7 @@ struct FatPtr * FatPtr_shiftright(struct FatPtr *p, size_t units){ size_t i; - if(FatPtr_grow(p, units * (sizeof FatPtr_scalar_t)) == NULL) + if(FatPtr_grow(p, units * (sizeof (FatPtr_scalar_t))) == NULL) return NULL; for(i = 0; i < p->s; ++i) (p->v)[i + units] = (p->v)[i]; diff --git a/libfatptr/libfatptr.h b/libfatptr/libfatptr.h index a685cc9..cc5443f 100644 --- a/libfatptr/libfatptr.h +++ b/libfatptr/libfatptr.h @@ -42,12 +42,27 @@ struct FatPtr *FatPtr_destruct(struct FatPtr *p); * Grows p's maximum length by units (in sizeof char). */ struct FatPtr *FatPtr_grow(struct FatPtr *p, size_t units); +/* FatPtr_index + * - FatPtr *p: object to index + * - size_t index: index of the scalar to get + * Returns a pointer to the FatPtr_scalar_t at the given index within the + * vector, or NULL if p was NULL. */ +FatPtr_vector_t FatPtr_index(struct FatPtr *p, size_t index); + /* FatPtr_initialize * - FatPtr *p: object to initialize * Returns p. * Initializes p to zero values. */ struct FatPtr *FatPtr_initialize(struct FatPtr *p); +/* FatPtr_prepend + * - FatPtr *p: object to which to prepend + * - FatPtr_vector_t s: vector to prepend + * - size_t units: amount of units from the vector to prepend + * Prepends units of s to the vector in p. Returns NULL if a memory allocation + * failed and p otherwise. */ +struct FatPtr *FatPtr_prepend(struct FatPtr *p, FatPtr_vector_t s, size_t units); + /* FatPtr_shiftleft * - FatPtr *p: object to shift * - size_t units: units (in sizeof *v) by which to shift p