Last active
March 24, 2024 06:15
-
-
Save pervognsen/d4dc1dc159f93a0ad5b6 to your computer and use it in GitHub Desktop.
somewhat palatable templates in c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct A { | |
int size; | |
int capacity; | |
T *data; | |
}; | |
typedef struct A A; | |
void A_(Create)(A *array, int capacity); | |
void A_(Destroy)(A *array); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
void A_(Create)(A *array, int capacity) { | |
array->size = 0; | |
array->capacity = capacity; | |
array->data = malloc(sizeof(T) * capacity); | |
} | |
void A_(Destroy)(A *array) { | |
free(array->data); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma push_macro("T") | |
#pragma push_macro("A") | |
#pragma push_macro("A_") | |
#pragma push_macro("J1") | |
#pragma push_macro("J2") | |
#undef T | |
#undef A | |
#undef A_ | |
#undef J1 | |
#undef J2 | |
#define J1(x,y) x##y | |
#define J2(x,y) J1(x,y) | |
#define T ARRAY_ELEMENT | |
#define A ARRAY_NAME | |
#define A_(x) J2(J2(ARRAY_NAME,_),x) | |
#include "array.inl.h" | |
#ifdef ARRAY_IMPLEMENTATION | |
#include "array.inl.c" | |
#undef ARRAY_IMPLEMENTATION | |
#endif | |
#undef ARRAY_ELEMENT | |
#undef ARRAY_NAME | |
#pragma pop_macro("T") | |
#pragma pop_macro("A") | |
#pragma pop_macro("A_") | |
#pragma pop_macro("J1") | |
#pragma pop_macro("J2") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define T "asdf" | |
#define A_ "afafa" | |
#define A "xzf" | |
#define ARRAY_NAME IntArray | |
#define ARRAY_ELEMENT int | |
#define ARRAY_IMPLEMENTATION | |
#include "array.inl" | |
#define ARRAY_NAME FloatArray | |
#define ARRAY_ELEMENT float | |
#define ARRAY_IMPLEMENTATION | |
#include "array.inl" | |
int main() { | |
int n = 10; | |
IntArray ai; | |
IntArray_Create(&ai, n); | |
for (int i = 0; i < n; i++) { | |
ai.data[i] = 3*i + 5; | |
} | |
for (int i = 0; i < n; i++) { | |
printf("%d => %d\n", i, ai.data[i]); | |
} | |
IntArray_Destroy(&ai); | |
FloatArray af; | |
FloatArray_Create(&af, n); | |
for (int i = 0; i < n; i++) { | |
af.data[i] = 3.0f*i + 5.0f; | |
} | |
for (int i = 0; i < n; i++) { | |
printf("%d => %f\n", i, af.data[i]); | |
} | |
FloatArray_Destroy(&af); | |
printf("%s %s %s\n", T, A, A_); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment