Initialized ehan.h and some other minor changes
This commit is contained in:
parent
1c135b3d09
commit
a89544beb5
34
da.h
34
da.h
|
@ -6,6 +6,9 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define EHAN_IMPLEMENTATION
|
||||
#include "ehan.h"
|
||||
|
||||
enum daFields {
|
||||
SIZE = 0,
|
||||
CAPACITY = 1,
|
||||
|
@ -15,7 +18,7 @@ enum daFields {
|
|||
|
||||
typedef void * dynarr;
|
||||
dynarr _daCreate(size_t initCapacity, size_t type, size_t size);
|
||||
dynarr _daPush(dynarr da, void * item);
|
||||
size_t _daPush(dynarr * da, void * item);
|
||||
void _daPop(dynarr da, void * element);
|
||||
void daPopDiscard(dynarr da);
|
||||
void daFree(dynarr da);
|
||||
|
@ -36,7 +39,7 @@ void daBzero(dynarr da);
|
|||
|
||||
#define dynarr(type) type *
|
||||
#define daCreate(type, cap) _daCreate(cap, sizeof(type), 0)
|
||||
#define daPush(da, item) da = _daPush(da, &item)
|
||||
#define daPush(da, item) _daPush((void **) &da, &item)
|
||||
#define daPop(da, item) _daPop(da, &item)
|
||||
|
||||
#endif // DA_H_
|
||||
|
@ -45,7 +48,11 @@ void daBzero(dynarr da);
|
|||
|
||||
dynarr _daCreate(size_t cap, size_t type, size_t size)
|
||||
{
|
||||
if (cap == 0) cap = 1;
|
||||
|
||||
size_t * da = (size_t *) malloc(type * cap + DATA * sizeof(size_t));
|
||||
if (da == NULL) return NULL;
|
||||
|
||||
da[SIZE] = size;
|
||||
da[CAPACITY] = cap;
|
||||
da[TYPE] = type;
|
||||
|
@ -55,12 +62,15 @@ dynarr _daCreate(size_t cap, size_t type, size_t size)
|
|||
|
||||
void daFree(dynarr da)
|
||||
{
|
||||
if (da == NULL) return;
|
||||
free((size_t *) da - DATA);
|
||||
}
|
||||
|
||||
dynarr daCopy(dynarr da)
|
||||
{
|
||||
dynarr temp = _daCreate(daCap(da), daType(da), daSize(da));
|
||||
if (temp == NULL) return NULL;
|
||||
|
||||
memcpy(temp, da, daSize(da) * daType(da));
|
||||
return temp;
|
||||
}
|
||||
|
@ -68,17 +78,25 @@ dynarr daCopy(dynarr da)
|
|||
dynarr daResize(dynarr da)
|
||||
{
|
||||
dynarr temp = _daCreate(daCap(da) * 2, daType(da), daSize(da));
|
||||
if (temp == NULL) return NULL;
|
||||
|
||||
memcpy(temp, da, daSize(da) * daType(da));
|
||||
daFree(da);
|
||||
return temp;
|
||||
}
|
||||
|
||||
dynarr _daPush(dynarr da, void * item)
|
||||
size_t _daPush(dynarr * da, void * item)
|
||||
{
|
||||
if (daSize(da) >= daCap(da)) da = daResize(da);
|
||||
memcpy((char *)da + daSize(da) * daType(da), item, daType(da));
|
||||
*daField(da, SIZE) += 1;
|
||||
return da;
|
||||
if (daSize(*da) >= daCap(*da))
|
||||
{
|
||||
dynarr resized = daResize(*da);
|
||||
if (resized == NULL) return 1;
|
||||
*da = resized;
|
||||
}
|
||||
|
||||
memcpy((char *)*da + daSize(*da) * daType(*da), item, daType(*da));
|
||||
*daField(*da, SIZE) += 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void _daPop(dynarr da, void * elem)
|
||||
|
@ -131,6 +149,8 @@ void daForeach(dynarr da, func f)
|
|||
heapstr daToCStr(dynarr da)
|
||||
{
|
||||
char * buffer = (char *) calloc(1, daSize(da) + 1);
|
||||
if (buffer == NULL) return NULL;
|
||||
|
||||
memcpy(buffer, da, daSize(da) * daType(da));
|
||||
return buffer;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,87 @@
|
|||
#ifndef EHAN_H_
|
||||
#define EHAN_H_
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct GenericReturn {
|
||||
long double fp;
|
||||
void * val;
|
||||
size_t err;
|
||||
} GenericReturn;
|
||||
|
||||
GenericReturn ehanError(size_t error);
|
||||
#define ehanValue(v) _ehanValue((void *) v)
|
||||
#define ehanValueFP(fp) _ehanValueFP((long double) fp)
|
||||
GenericReturn _ehanValue(void * value);
|
||||
GenericReturn _ehanValueFP(long double value);
|
||||
|
||||
#define resultGeneric(type, ret) (*(type *) (&ret.val))
|
||||
#define resultFP(type, ret) ((type) (ret.fp))
|
||||
|
||||
const char * errorToString(const char * errorList[], size_t err);
|
||||
|
||||
#define ILog(format, ...) \
|
||||
do { \
|
||||
fprintf(stderr, "\x1b[34m""info""\x1b[0m"":%s:%d: " format, __FILE__, __LINE__, ##__VA_ARGS__); \
|
||||
} while (0)
|
||||
|
||||
#define WLog(format, ...) \
|
||||
do { \
|
||||
fprintf(stderr, "\x1b[33m""warn""\x1b[0m"":%s:%d: " format, __FILE__, __LINE__, ##__VA_ARGS__); \
|
||||
} while (0)
|
||||
|
||||
#define ELog(format, ...) \
|
||||
do { \
|
||||
fprintf(stderr, "\x1b[31m""error""\x1b[0m"":%s:%d: " format, __FILE__, __LINE__, ##__VA_ARGS__); \
|
||||
} while (0)
|
||||
|
||||
#ifdef NDEBUG
|
||||
|
||||
#define DLog(format, ...)
|
||||
|
||||
#else
|
||||
|
||||
#define DLog(format, ...) \
|
||||
do { \
|
||||
fprintf(stderr, "\x1b[32m""debug""\x1b[0m"":%s:%d: " format, __FILE__, __LINE__, ##__VA_ARGS__); \
|
||||
} while (0)
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef EHAN_IMPLEMENTATION
|
||||
|
||||
GenericReturn _ehanValue(void * value)
|
||||
{
|
||||
return (GenericReturn) {
|
||||
.val = value,
|
||||
.err = 0,
|
||||
};
|
||||
}
|
||||
|
||||
GenericReturn _ehanValueFP(long double value)
|
||||
{
|
||||
return (GenericReturn) {
|
||||
.fp = value,
|
||||
.val = 0,
|
||||
.err = 0,
|
||||
};
|
||||
}
|
||||
|
||||
GenericReturn ehanError(size_t error)
|
||||
{
|
||||
return (GenericReturn) {
|
||||
.val = 0,
|
||||
.err = error,
|
||||
};
|
||||
}
|
||||
|
||||
const char * errorToString(const char * errorList[], size_t err)
|
||||
{
|
||||
return errorList[err - 1];
|
||||
}
|
||||
|
||||
#endif // EHAN_IMPLEMENTATION
|
||||
|
||||
#endif // EHAN_H_
|
29
example.c
29
example.c
|
@ -19,12 +19,16 @@ int main(void)
|
|||
printf("DA1\n");
|
||||
printf("----------\n");
|
||||
|
||||
dynarr(unsigned short) * da = daCreate(unsigned short, 1);
|
||||
dynarr(unsigned short) da = daCreate(unsigned short, 1);
|
||||
|
||||
unsigned short x = 69;
|
||||
daPush(da, x);
|
||||
if (daPush(da, x) > 0)
|
||||
goto cleanup;
|
||||
|
||||
x = 42069;
|
||||
daPush(da, x);
|
||||
if (daPush(da, x) > 0)
|
||||
goto cleanup;
|
||||
|
||||
x = 1337;
|
||||
daPush(da, x);
|
||||
x = 34;
|
||||
|
@ -69,8 +73,25 @@ int main(void)
|
|||
heapstr message = daToCStr(da2);
|
||||
|
||||
printf("%s\n", message);
|
||||
free(message);
|
||||
|
||||
printf("----------\n");
|
||||
printf("DA3\n");
|
||||
printf("----------\n");
|
||||
|
||||
dynarr(char *) da3 = daCreate(char *, 1);
|
||||
|
||||
const char * someString = "Test";
|
||||
for (size_t i = 0; i < 100000000; ++i) {
|
||||
daPush(da3, someString);
|
||||
}
|
||||
|
||||
char * str;
|
||||
daPop(da3, str);
|
||||
printf("%s\n", str);
|
||||
|
||||
cleanup:
|
||||
free(message);
|
||||
daFree(da);
|
||||
daFree(da2);
|
||||
daFree(da3);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue