123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- #include <stddef.h>
- #include <stdlib.h>
- #include <string.h>
- #include "simple_vector.h"
- #define INIT_SIZE 10
- #define MAX_SIZE 10000
- void vector_init(simple_vector* v)
- {
- if (v)
- {
- v->size = 0;
- v->capacity = 0;
- v->data = NULL;
- }
- }
- uint32_t vector_size(const simple_vector* v)
- {
- if (v)
- return v->size;
- else
- return 0;
- }
- errno_t vector_push_back(simple_vector* v, const void* data)
- {
- if (v)
- {
- if (v->capacity == 0) {
-
- v->data = (void**)malloc(sizeof(void*) * INIT_SIZE);
- if (v->data ==NULL)
- return 1;
- v->capacity = INIT_SIZE;
- memset(v->data, '\0', sizeof(void*) * INIT_SIZE);
- }
- else if (v->size == v->capacity) {
- void** new_data;
- if( v->capacity >= MAX_SIZE - INIT_SIZE)
- return 1;
-
- new_data = (void**)realloc(v->data, sizeof(void*) *( v->capacity + INIT_SIZE));
- if (new_data ==NULL)
- return 1;
- memset(&new_data[v->capacity], '\0', sizeof(void*) * INIT_SIZE);
- v->data = new_data;
- v->capacity += INIT_SIZE;
- }
-
- v->data[v->size] = const_cast<void*>(data);
- v->size++;
- return 0;
- }
- return 1;
- }
- errno_t vector_get(const simple_vector* v, uint32_t index, void** data)
- {
- if (!v || index >= v->size || !data)
- return 1;
- *data = v->data[index];
- return 0;
- }
- errno_t vector_set(simple_vector* v, uint32_t index, const void* data)
- {
- if (!v || index >= v->size)
- return 1;
- v->data[index] = const_cast<void*>(data);
- return 0;
- }
- void vector_free(simple_vector* v)
- {
- if (v)
- {
- v->size = 0;
- v->capacity = 0;
- if(v->data)
- {
- free(v->data);
- v->data = NULL;
- }
- }
- }
|