diff options
Diffstat (limited to 'rushs/tinyprintf/vector')
| -rw-r--r-- | rushs/tinyprintf/vector/Makefile | 13 | ||||
| -rw-r--r-- | rushs/tinyprintf/vector/vector.c | 152 | ||||
| -rw-r--r-- | rushs/tinyprintf/vector/vector.h | 64 |
3 files changed, 0 insertions, 229 deletions
diff --git a/rushs/tinyprintf/vector/Makefile b/rushs/tinyprintf/vector/Makefile deleted file mode 100644 index 744241f..0000000 --- a/rushs/tinyprintf/vector/Makefile +++ /dev/null @@ -1,13 +0,0 @@ -CC = gcc -CFLAGS = -Wall -Werror -Wvla -Wextra -std=c99 -pedantic - -.PHONY: library clean - -library: vector.o - ar csr libvector.a vector.o - -vector.o: vector.c - $(CC) $(CFLAGS) -c -o vector.o vector.c - -clean: - $(RM) libvector.a vector.o diff --git a/rushs/tinyprintf/vector/vector.c b/rushs/tinyprintf/vector/vector.c deleted file mode 100644 index cbd19aa..0000000 --- a/rushs/tinyprintf/vector/vector.c +++ /dev/null @@ -1,152 +0,0 @@ -#include "vector.h" - -#include <stdio.h> -#include <stdlib.h> - -struct vector *vector_init(size_t n) -{ - struct vector *res = malloc(sizeof(struct vector)); - if (res == NULL) - { - return NULL; - } - - res->size = 0; - res->capacity = n; - res->data = malloc(n * sizeof(int)); - if (res->data == NULL) - { - return NULL; - } - return res; -} - -void vector_destroy(struct vector *v) -{ - if (v) - { - free(v->data); - free(v); - } -} - -struct vector *vector_resize(struct vector *v, size_t n) -{ - if (!v) - { - return NULL; - } - if (n == v->capacity) - { - return v; - } - if (v) - { - int *tmp = realloc(v->data, n * sizeof(int)); - if (tmp == NULL) - { - return NULL; - } - v->data = tmp; - - if (n < v->size) - { - v->size = n; - } - v->capacity = n; - return v; - } - return NULL; -} - -struct vector *vector_append(struct vector *v, int elt) -{ - if (v == NULL) - { - return NULL; - } - - if (v->size == v->capacity) - { - if (vector_resize(v, v->capacity * 2) == NULL) - { - return NULL; - } - } - v->data[v->size] = elt; - v->size++; - return v; -} - -void vector_print(const struct vector *v) -{ - if (v == NULL || v->size == 0) - { - printf("\n"); - return; - } - for (size_t i = 0; i < v->size - 1; i++) - { - printf("%d,", v->data[i]); - } - printf("%d\n", v->data[v->size - 1]); -} - -struct vector *vector_reset(struct vector *v, size_t n) -{ - if (vector_resize(v, n) == NULL) - { - return NULL; - } - - v->size = 0; - return v; -} - -struct vector *vector_insert(struct vector *v, size_t i, int elt) -{ - if (v == NULL || i > v->size) - return NULL; - if (i == v->size) - { - return vector_append(v, elt); - } - if (v->size == v->capacity) - { - if (vector_resize(v, v->capacity * 2) == NULL) - { - return NULL; - } - } - for (size_t j = v->size; j > i; j--) - { - v->data[j] = v->data[j - 1]; - } - v->size++; - - v->data[i] = elt; - return v; -} - -struct vector *vector_remove(struct vector *v, size_t i) -{ - if (v == NULL || v->size == 0 || i >= v->size) - { - return NULL; - } - for (size_t j = i; j < v->size - 1; j++) - { - v->data[j] = v->data[j + 1]; - } - - v->size--; - - if (v->size < v->capacity / 2) - { - if (vector_resize(v, v->capacity / 2) == NULL) - { - return NULL; - } - } - return v; -} diff --git a/rushs/tinyprintf/vector/vector.h b/rushs/tinyprintf/vector/vector.h deleted file mode 100644 index 5afada7..0000000 --- a/rushs/tinyprintf/vector/vector.h +++ /dev/null @@ -1,64 +0,0 @@ -#ifndef VECTOR_H -#define VECTOR_H - -#include <stddef.h> - -struct vector -{ - // The number of elements in the vector - size_t size; - // The maximum number of elements in the vector - size_t capacity; - // The elements themselves - int *data; -}; - -/* -** Initialize the vector with `n` capacity. -** Returns `NULL` if an error occured. -*/ -struct vector *vector_init(size_t n); - -/* -** Release the memory used by the vector. -** Does nothing if `v` is `NULL`. -*/ -void vector_destroy(struct vector *v); - -/* -** Resize the vector to `n` capacity. -** Returns `NULL` if an error occured. -*/ -struct vector *vector_resize(struct vector *v, size_t n); - -/* -** Append an element to the end of the vector. Expand the vector if needed. -** Returns `NULL` if an error occured. -*/ -struct vector *vector_append(struct vector *v, int elt); - -/* -** Display the vector contents on `stdout`. -** Displays `\n` if `v` is `NULL`. -*/ -void vector_print(const struct vector *v); - -/* -** Remove all the elements of the vector, and resize it to `n` capacity. -** Returns `NULL` if an error occured. -*/ -struct vector *vector_reset(struct vector *v, size_t n); - -/* -** Insert `n` at the index `i`. Expand the vector if needed. -** Returns `NULL` if an error occured. -*/ -struct vector *vector_insert(struct vector *v, size_t i, int elt); - -/* -** Remove the element at the index `i`. -** Returns `NULL` if an error occured. -*/ -struct vector *vector_remove(struct vector *v, size_t i); - -#endif /* !VECTOR_H */ |
