| Line | +Branch | +Exec | +Source | +
|---|---|---|---|
| 1 | ++ | ++ | /* | +
| 2 | ++ | ++ | ** EPITECH PROJECT, 2024 | +
| 3 | ++ | ++ | ** cvector | +
| 4 | ++ | ++ | ** File description: | +
| 5 | ++ | ++ | ** Core functions for a C dynamic vector similar to std::vector in C++ | +
| 6 | ++ | ++ | */ | +
| 7 | ++ | ++ | + |
| 8 | ++ | ++ | #include "cvector.h" | +
| 9 | ++ | ++ | + |
| 10 | ++ | +395 | +void *vector_init(size_t item_size, size_t capacity) | +
| 11 | ++ | ++ | { | +
| 12 | ++ | ++ | vector_header_t *header; | +
| 13 | ++ | ++ | + |
| 14 | +
+
+
+ 4/4+
+
+ ✓ Branch 0 taken 359 times.
+ ✓ Branch 1 taken 36 times.
+ ✓ Branch 2 taken 18 times.
+ ✓ Branch 3 taken 341 times.
+ |
+ 395 | +if (item_size == 0 || capacity == 0) | +
| 15 | ++ | +54 | +return NULL; | +
| 16 | ++ | +341 | +header = malloc(sizeof(vector_header_t) + item_size * capacity); | +
| 17 | +
+
+
+ 1/2+
+
+ ✗ Branch 0 not taken.
+ ✓ Branch 1 taken 341 times.
+ |
+ 341 | +if (!header) | +
| 18 | ++ | +✗ | +return NULL; | +
| 19 | ++ | +341 | +header->size = 0; | +
| 20 | ++ | +341 | +header->capacity = capacity; | +
| 21 | ++ | +341 | +header->initial_capacity = capacity; | +
| 22 | ++ | ++ | + |
| 23 | ++ | +341 | +return (header + 1); | +
| 24 | ++ | +395 | +} | +
| 25 | ++ | ++ | + |
| 26 | ++ | +2892 | +void *vector_ensure_capacity(void *v, size_t item_count, size_t item_size) | +
| 27 | ++ | ++ | { | +
| 28 | ++ | ++ | vector_header_t *header; | +
| 29 | ++ | ++ | size_t new_capacity; | +
| 30 | ++ | ++ | size_t required_capacity; | +
| 31 | ++ | ++ | + |
| 32 | +
+
+
+ 2/2+
+
+ ✓ Branch 0 taken 2873 times.
+ ✓ Branch 1 taken 19 times.
+ |
+ 2892 | +if (!v) | +
| 33 | ++ | +19 | +return vector_init(item_size, item_count); | +
| 34 | ++ | +2873 | +header = VECTOR_HEADER(v); | +
| 35 | ++ | +2873 | +required_capacity = header->size + item_count; | +
| 36 | +
+
+
+ 2/2+
+
+ ✓ Branch 0 taken 2741 times.
+ ✓ Branch 1 taken 132 times.
+ |
+ 2873 | +if (required_capacity <= header->capacity) | +
| 37 | ++ | +2741 | +return v; | +
| 38 | ++ | +132 | +new_capacity = header->capacity * 2; | +
| 39 | +
+
+
+ 2/2+
+
+ ✓ Branch 0 taken 3 times.
+ ✓ Branch 1 taken 132 times.
+ |
+ 135 | +while (new_capacity < required_capacity) | +
| 40 | ++ | +3 | +new_capacity *= 2; | +
| 41 | ++ | +132 | +header = realloc(header, sizeof(vector_header_t) + (new_capacity * item_size)); | +
| 42 | +
+
+
+ 1/2+
+
+ ✗ Branch 0 not taken.
+ ✓ Branch 1 taken 132 times.
+ |
+ 132 | +if (!header) | +
| 43 | ++ | +✗ | +return NULL; | +
| 44 | ++ | +132 | +header->capacity = new_capacity; | +
| 45 | ++ | ++ | + |
| 46 | ++ | +132 | +return (header + 1); | +
| 47 | ++ | +2892 | +} | +
| 48 | ++ | ++ | + |
| 49 | ++ | +2925 | +int vector_push_back_impl(void **v, const void *val, size_t count, size_t item_size) | +
| 50 | ++ | ++ | { | +
| 51 | ++ | ++ | vector_header_t *header; | +
| 52 | ++ | ++ | void *tmp; | +
| 53 | ++ | ++ | + |
| 54 | +
+
+
+ 7/8+
+
+ ✓ Branch 0 taken 2925 times.
+ ✗ Branch 1 not taken.
+ ✓ Branch 2 taken 2907 times.
+ ✓ Branch 3 taken 18 times.
+ ✓ Branch 4 taken 2889 times.
+ ✓ Branch 5 taken 18 times.
+ ✓ Branch 6 taken 18 times.
+ ✓ Branch 7 taken 2871 times.
+ |
+ 2925 | +if (!v || !val || count == 0 || item_size == 0) | +
| 55 | ++ | +54 | +return VECTOR_FAILURE; | +
| 56 | +
+
+
+ 2/2+
+
+ ✓ Branch 0 taken 18 times.
+ ✓ Branch 1 taken 2853 times.
+ |
+ 2871 | +if ((uintptr_t)val < 4096) | +
| 57 | ++ | +18 | +return VECTOR_FAILURE; | +
| 58 | ++ | +2853 | +tmp = vector_ensure_capacity(*v, count, item_size); | +
| 59 | +
+
+
+ 1/2+
+
+ ✗ Branch 0 not taken.
+ ✓ Branch 1 taken 2853 times.
+ |
+ 2853 | +if (!tmp) | +
| 60 | ++ | +✗ | +return VECTOR_FAILURE; | +
| 61 | ++ | +2853 | +*v = tmp; | +
| 62 | ++ | +2853 | +header = VECTOR_HEADER(*v); | +
| 63 | ++ | +2853 | +memcpy((unsigned char*)(*v) + (header->size * item_size), val, count * item_size); | +
| 64 | ++ | +2853 | +header->size += count; | +
| 65 | ++ | ++ | + |
| 66 | ++ | +2853 | +return VECTOR_SUCCESS; | +
| 67 | ++ | +2925 | +} | +
| 68 | ++ | ++ | + |
| 69 | ++ | +66 | +void vector_pop_back_impl(void **v, size_t count, size_t item_size) | +
| 70 | ++ | ++ | { | +
| 71 | ++ | ++ | vector_header_t *header; | +
| 72 | ++ | ++ | + |
| 73 | +
+
+
+ 6/8+
+
+ ✓ Branch 0 taken 66 times.
+ ✗ Branch 1 not taken.
+ ✓ Branch 2 taken 50 times.
+ ✓ Branch 3 taken 16 times.
+ ✓ Branch 4 taken 34 times.
+ ✓ Branch 5 taken 16 times.
+ ✗ Branch 6 not taken.
+ ✓ Branch 7 taken 34 times.
+ |
+ 66 | +if (!v || !*v || count == 0 || item_size == 0) | +
| 74 | ++ | +32 | +return; | +
| 75 | ++ | +34 | +header = VECTOR_HEADER(*v); | +
| 76 | +
+
+
+ 2/2+
+
+ ✓ Branch 0 taken 16 times.
+ ✓ Branch 1 taken 18 times.
+ |
+ 34 | +if (count >= header->size) { | +
| 77 | ++ | +16 | +header->size = 0; | +
| 78 | ++ | +16 | +} else { | +
| 79 | ++ | +18 | +header->size -= count; | +
| 80 | ++ | ++ | } | +
| 81 | ++ | +66 | +} | +
| 82 | ++ | ++ | + |
+