addition: Added basic header and structure
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
#ifndef ARRAYLIST_H
|
||||
#define ARRAYLIST_H
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct {
|
||||
void *data;
|
||||
size_t capacity;
|
||||
size_t len;
|
||||
size_t elem_size;
|
||||
} ArrayList;
|
||||
|
||||
typedef struct {
|
||||
ArrayList array;
|
||||
size_t offset;
|
||||
} ArrayListSlice;
|
||||
|
||||
typedef enum {
|
||||
ARRLIST_OK = 0,
|
||||
ARRLIST_OUT_OF_BOUNDS,
|
||||
ARRLIST_BAD_ALLOC,
|
||||
ARRLIST_EMPTY,
|
||||
ARRLIST_NULL_ARG,
|
||||
ARRLIST_INVALID_CAPACITY,
|
||||
ARRLIST_INVALID_ELEM_SIZE,
|
||||
} ArrayListErr;
|
||||
|
||||
typedef struct {
|
||||
bool is_valid;
|
||||
union {
|
||||
ArrayListErr err;
|
||||
ArrayList array;
|
||||
};
|
||||
} ArrayListResult;
|
||||
|
||||
ArrayListResult arraylist_init(size_t capacity, size_t elem_size);
|
||||
ArrayListErr arraylist_destroy(ArrayList *arr);
|
||||
ArrayListErr arraylist_clear(ArrayList *arr);
|
||||
|
||||
size_t arraylist_size(ArrayList *arr);
|
||||
size_t arraylist_capacity(ArrayList *arr);
|
||||
bool arraylist_is_empty(ArrayList *arr);
|
||||
|
||||
ArrayListErr arraylist_push_back(ArrayList *arr, void *data);
|
||||
ArrayListErr arraylist_insert(ArrayList*arr, size_t index, void *data);
|
||||
ArrayListErr arraylist_push_front(ArrayList* arr, void *data);
|
||||
|
||||
// Here out can be null for not having anything writen
|
||||
ArrayListErr arraylist_pop_back(ArrayList *arr, void *out);
|
||||
ArrayListErr arraylist_remove_at(ArrayList *arr, size_t index, void *out);
|
||||
ArrayListErr arraylist_pop_front(ArrayList *arr, void *out);
|
||||
|
||||
ArrayListErr arraylist_get(ArrayList *arr, size_t index, void *out);
|
||||
ArrayListErr arraylist_set(ArrayList *arr, size_t index, void *data);
|
||||
|
||||
ArrayListErr arraylist_resize(ArrayList *arr, size_t new_capacity);
|
||||
ArrayListErr arraylist_reserve(ArrayList *arr, size_t size_to_reserve);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
#include "arraylist.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
ArrayListResult arraylist_init(size_t capacity, size_t elem_size) {
|
||||
if (capacity == 0) {
|
||||
return (ArrayListResult) {
|
||||
.is_valid = false,
|
||||
.err = ARRLIST_INVALID_CAPACITY};
|
||||
}
|
||||
|
||||
if (elem_size == 0) {
|
||||
return (ArrayListResult) {
|
||||
.is_valid = false,
|
||||
.err = ARRLIST_INVALID_ELEM_SIZE,
|
||||
};
|
||||
}
|
||||
|
||||
ArrayList arr = {
|
||||
.data = malloc(capacity * elem_size),
|
||||
.capacity = capacity,
|
||||
.elem_size = elem_size,
|
||||
.len = 0
|
||||
};
|
||||
|
||||
if (arr.data == NULL) {
|
||||
return (ArrayListResult) {
|
||||
.is_valid = false,
|
||||
.err = ARRLIST_BAD_ALLOC,
|
||||
};
|
||||
}
|
||||
|
||||
return (ArrayListResult) {.is_valid = true, .array = arr};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void) {
|
||||
|
||||
printf("hola\n");
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ find_package(cmocka REQUIRED)
|
||||
add_executable(test_arraylist test_arraylist.c)
|
||||
|
||||
target_link_libraries(test_arraylist
|
||||
arena_lib
|
||||
arraylist_lib
|
||||
cmocka::cmocka
|
||||
)
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void) {
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user