fix invalid syntax not being called on an invalid assignment

This commit is contained in:
2025-06-02 00:13:24 +01:00
parent d2518afb8e
commit e4c2af3cc7
7 changed files with 131 additions and 569 deletions

View File

@@ -1,77 +1,109 @@
#include "darray.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "darray.h"
void darray_init(DArray *arr, size_t element_size) {
arr->element_size = element_size;
arr->size = 0;
arr->capacity = CHUNK_SIZE;
arr->data = malloc(CHUNK_SIZE * element_size);
if (!arr->data) {
fprintf(stderr, "darray_init: allocation failed\n");
exit(EXIT_FAILURE);
}
arr->element_size = element_size;
arr->size = 0;
arr->capacity = CHUNK_SIZE;
arr->data = malloc(CHUNK_SIZE * element_size);
arr->resizable = true;
if (!arr->data) {
fprintf(stderr, "darray_init: allocation failed\n");
exit(EXIT_FAILURE);
}
}
void darray_resize(DArray *arr, size_t new_size) {
size_t new_capacity = ((new_size + CHUNK_SIZE) / CHUNK_SIZE) * CHUNK_SIZE;
if (new_capacity != arr->capacity) {
void *new_data = realloc(arr->data, new_capacity * arr->element_size);
if (!new_data) {
fprintf(stderr, "darray_resize: reallocation failed\n");
exit(EXIT_FAILURE);
}
arr->data = new_data;
arr->capacity = new_capacity;
if (!arr->resizable) {
fprintf(stderr, "darray_resize: unresizable darray\n");
exit(EXIT_FAILURE);
}
size_t new_capacity = ((new_size + CHUNK_SIZE) / CHUNK_SIZE) * CHUNK_SIZE;
if (new_capacity != arr->capacity) {
void *new_data = realloc(arr->data, new_capacity * arr->element_size);
if (!new_data) {
fprintf(stderr, "darray_resize: reallocation failed\n");
exit(EXIT_FAILURE);
}
arr->data = new_data;
arr->capacity = new_capacity;
}
arr->size = new_size;
arr->size = new_size;
}
void darray_push(DArray *arr, void *element) {
if (arr->size >= arr->capacity) {
darray_resize(arr, arr->size + 1);
} else {
arr->size++;
}
if (!arr->resizable) {
fprintf(stderr, "darray_resize: unresizable darray\n");
exit(EXIT_FAILURE);
}
if (arr->size >= arr->capacity) {
darray_resize(arr, arr->size + 1);
} else {
arr->size++;
}
void *target = (void *)arr->data + (arr->size - 1) * arr->element_size;
memcpy(target, element, arr->element_size);
void *target = (char *)arr->data + (arr->size - 1) * arr->element_size;
memcpy(target, element, arr->element_size);
}
void darray_pop(DArray *arr, void (*free_data)(void *)) {
if (arr->size == 0)
return;
if (!arr->resizable) {
fprintf(stderr, "darray_resize: unresizable darray\n");
exit(EXIT_FAILURE);
}
if (arr->size == 0)
return;
arr->size--;
if (free_data) {
void *target = (char *)arr->data + (arr->size-1) * arr->element_size;
free_data(target);
}
if (free_data) {
void *target = (void *)arr->data + arr->size * arr->element_size;
free_data(target);
}
darray_resize(arr, arr->size);
darray_resize(arr, arr->size);
}
void *darray_get(DArray *arr, size_t index) {
if (index >= arr->size) {
fprintf(stderr, "darray_get: index out of bounds\n");
exit(EXIT_FAILURE);
}
return (void *)arr->data + index * arr->element_size;
if (index >= arr->size) {
fprintf(stderr, "darray_get: index out of bounds\n");
exit(EXIT_FAILURE);
}
return (char *)arr->data + index * arr->element_size;
}
DArray darray_slice(DArray *arr, size_t start, size_t end) {
if (start > end || end > arr->size) {
fprintf(stderr, "darray_slice: invalid slice range\n");
exit(EXIT_FAILURE);
}
DArray slice;
slice.data = (char *)arr->data + start * arr->element_size;
slice.size = (end - start);
slice.element_size = arr->element_size;
slice.capacity = ((slice.size + CHUNK_SIZE) / CHUNK_SIZE) * CHUNK_SIZE;
slice.resizable = false;
return slice;
}
void darray_free(DArray *arr, void (*free_data)(void *)) {
if (free_data) {
for (size_t i = 0; i < arr->size; ++i) {
void *element = (void *)arr->data + i * arr->element_size;
free_data(element);
}
if (!arr->resizable) {
// It's a view/slice — don't free
return;
}
if (free_data) {
for (size_t i = 0; i < arr->size; ++i) {
void *element = (char *)arr->data + i * arr->element_size;
free_data(element);
}
free(arr->data);
arr->data = NULL;
arr->size = 0;
arr->capacity = 0;
arr->element_size = 0;
}
free(arr->data);
arr->data = NULL;
arr->size = 0;
arr->capacity = 0;
arr->element_size = 0;
}

View File

@@ -1,15 +1,17 @@
#ifndef DARRAY_H
#define DARRAY_H
#include <stddef.h> // for size_t
#include <stdbool.h>
#include <stddef.h> // for size_t
#define CHUNK_SIZE 16
typedef struct {
void *data;
size_t element_size;
size_t size;
size_t capacity;
void *data;
size_t element_size;
size_t size;
size_t capacity;
bool resizable;
} DArray;
// Initializes the dynamic_array
@@ -30,4 +32,6 @@ void darray_free(DArray *arr, void (*free_data)(void *));
// Resizes the array to a new size (internal use, but exposed)
void darray_resize(DArray *arr, size_t new_size);
DArray darray_slice(DArray *arr, size_t start, size_t end);
#endif // DARRAY_H