1
0

FatPtr_index, FatPtr_prepend

This commit is contained in:
dtb 2024-03-20 18:18:53 -06:00
parent 38204abcab
commit 28bbf00908
2 changed files with 36 additions and 4 deletions

View File

@ -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];

View File

@ -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