This commit is contained in:
2023-09-04 23:41:47 +02:00
parent 81a7e44c99
commit 773c8549f3
6 changed files with 271 additions and 0 deletions

93
clib/cdynarr.c Normal file
View File

@@ -0,0 +1,93 @@
/*
*
* Copyright 2023 Oleg Borodin <borodin@unix7.org>
*
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <cdynarr.h>
#define INIT_CAPA 64
/* Integer array */
cintarr_t* new_cintarr(void) {
cintarr_t* arr = malloc(sizeof(cintarr_t));
if (arr == NULL) return NULL;
if (cintarr_init(arr) == NULL) {
cintarr_free(arr);
return NULL;
}
return arr;
}
void* cintarr_init(cintarr_t* array) {
array->data = malloc(INIT_CAPA * sizeof(int64_t));
if (array->data == NULL) return NULL;
memset(array->data, 0, INIT_CAPA);
array->capa = INIT_CAPA;
array->size = 0;
return array->data;
}
void* cintarr_append(cintarr_t* array, int64_t add) {
if (array->size + 1 > array->capa) {
size_t newcapa = (array->capa * 10) / 6 ;
int64_t* newarray = realloc(array->data, newcapa);
if (newarray == NULL) return NULL;
array->capa = newcapa;
array->data = newarray;
}
array->data[array->size++] = add;
return array->data;
}
int64_t* cintarr_getref(cintarr_t* array) {
return array->data;
}
void cintarr_destroy(cintarr_t* array) {
if (array == NULL) return;
free(array->data);
}
void cintarr_free(cintarr_t* array) {
cintarr_destroy(array);
free(array->data);
}
/* Float array */
void* cfltarr_init(cfltarr_t* array) {
array->data = malloc(INIT_CAPA * sizeof(double));
if (array->data == NULL) return NULL;
memset(array->data, 0, INIT_CAPA);
array->capa = INIT_CAPA;
array->size = 0;
return array->data;
}
void* cfltarr_append(cfltarr_t* array, double add) {
if (array->size + 1 > array->capa) {
size_t newcapa = (array->capa * 10) / 6 ;
double* newarray = realloc(array->data, newcapa);
if (newarray == NULL) return NULL;
array->capa = newcapa;
array->data = newarray;
}
array->data[array->size++] = add;
return array->data;
}
double* cfltarr_getref(cfltarr_t* array) {
return array->data;
}
void cfltarr_destroy(cfltarr_t* array) {
if (array == NULL) return;
free(array->data);
}