Const all the things!
This commit is contained in:
parent
ff59b301cc
commit
193bf1d75f
24
src/vector.c
24
src/vector.c
@ -11,7 +11,7 @@ Vector vec_new(Destructor destructor)
|
||||
return self;
|
||||
}
|
||||
|
||||
Vector vec_with_capacity(Destructor t_destructor, size_t t_capacity)
|
||||
Vector vec_with_capacity(Destructor const t_destructor, size_t const t_capacity)
|
||||
{
|
||||
Vector self = vec_new(t_destructor);
|
||||
free(self.elements);
|
||||
@ -20,17 +20,17 @@ Vector vec_with_capacity(Destructor t_destructor, size_t t_capacity)
|
||||
return self;
|
||||
}
|
||||
|
||||
void *vec_at(Vector *self, size_t t_index)
|
||||
void *vec_at(Vector const *const self, size_t const t_index)
|
||||
{
|
||||
return self->elements[t_index];
|
||||
}
|
||||
|
||||
void *vec_safe_at(Vector *self, size_t t_index)
|
||||
void *vec_safe_at(Vector const *const self, size_t const t_index)
|
||||
{
|
||||
return (t_index >= vec_length(self)) ? NULL : vec_at(self, t_index);
|
||||
}
|
||||
|
||||
static void vec_maybe_destroy_element(Vector *self, size_t t_index)
|
||||
static void vec_maybe_destroy_element(Vector const *self, size_t const t_index)
|
||||
{
|
||||
void *element = vec_at(self, t_index);
|
||||
if (self->destroy) {
|
||||
@ -38,22 +38,22 @@ static void vec_maybe_destroy_element(Vector *self, size_t t_index)
|
||||
}
|
||||
}
|
||||
|
||||
void *vec_last(Vector *self)
|
||||
void *vec_last(Vector const *const self)
|
||||
{
|
||||
return vec_at(self, vec_length(self) - 1);
|
||||
}
|
||||
|
||||
size_t vec_length(Vector *self)
|
||||
size_t vec_length(Vector const *const self)
|
||||
{
|
||||
return self->length;
|
||||
}
|
||||
|
||||
size_t vec_capacity(Vector *self)
|
||||
size_t vec_capacity(Vector const *const self)
|
||||
{
|
||||
return self->capacity;
|
||||
}
|
||||
|
||||
static void vec_realloc(Vector *self)
|
||||
static void vec_realloc(Vector *const self)
|
||||
{
|
||||
self->capacity *= 2;
|
||||
self->elements = realloc(self->elements, sizeof(void *) * vec_capacity(self));
|
||||
@ -63,7 +63,7 @@ static void vec_realloc(Vector *self)
|
||||
}
|
||||
}
|
||||
|
||||
void vec_push(Vector *self, void *t_element)
|
||||
void vec_push(Vector *const self, void const *const t_element)
|
||||
{
|
||||
if (vec_length(self) >= vec_capacity(self)) {
|
||||
vec_realloc(self);
|
||||
@ -71,7 +71,7 @@ void vec_push(Vector *self, void *t_element)
|
||||
self->elements[(*self).length++] = t_element;
|
||||
}
|
||||
|
||||
void vec_pop(Vector *self)
|
||||
void vec_pop(Vector *const self)
|
||||
{
|
||||
if (vec_length(self) <= 0) {
|
||||
return;
|
||||
@ -80,7 +80,7 @@ void vec_pop(Vector *self)
|
||||
--(*self).length;
|
||||
}
|
||||
|
||||
void vec_shrink_to_fit(Vector *self)
|
||||
void vec_shrink_to_fit(Vector *const self)
|
||||
{
|
||||
self->capacity = self->length;
|
||||
self->elements = realloc(self->elements, sizeof(void *) * vec_capacity(self));
|
||||
@ -90,7 +90,7 @@ void vec_shrink_to_fit(Vector *self)
|
||||
}
|
||||
}
|
||||
|
||||
void vec_delete(Vector *self)
|
||||
void vec_delete(Vector *const self)
|
||||
{
|
||||
if (self->destroy) {
|
||||
for (size_t i = 0; i < vec_length(self); ++i) {
|
||||
|
20
src/vector.h
20
src/vector.h
@ -41,16 +41,16 @@ typedef struct Vector_s {
|
||||
} Vector;
|
||||
|
||||
Vector vec_new(Destructor destructor);
|
||||
Vector vec_with_capacity(Destructor destructor, size_t capacity);
|
||||
Vector vec_with_capacity(Destructor const destructor, size_t const capacity);
|
||||
|
||||
void * vec_at(Vector *self, size_t index);
|
||||
void * vec_safe_at(Vector *self, size_t index);
|
||||
void * vec_last(Vector *self);
|
||||
size_t vec_length(Vector *self);
|
||||
size_t vec_capacity(Vector *self);
|
||||
void vec_push(Vector *self, void *element);
|
||||
void vec_pop(Vector *self);
|
||||
void vec_shrink_to_fit(Vector *self);
|
||||
void vec_delete(Vector *self);
|
||||
void * vec_at(Vector const *const self, size_t const index);
|
||||
void * vec_safe_at(Vector const *const self, size_t const index);
|
||||
void * vec_last(Vector const *const self);
|
||||
size_t vec_length(Vector const *const self);
|
||||
size_t vec_capacity(Vector const *const self);
|
||||
void vec_push(Vector *const self, void const *const element);
|
||||
void vec_pop(Vector *const self);
|
||||
void vec_shrink_to_fit(Vector *const self);
|
||||
void vec_delete(Vector *const self);
|
||||
|
||||
#endif /* VECTOR_H */
|
||||
|
Loading…
Reference in New Issue
Block a user