Commit 6c1b5301 by Aaron Leung

Generic resizable array templates.

parent 10d5c72a
#define DECLARE_SASS_ARRAY_OF(type) \
typedef struct { \
size_t length; \
size_t capacity; \
type *contents; \
} sass_ ## type ## _array; \
sass_ ## type ## _array sass_make_ ## type ## _array(size_t cap); \
void sass_ ## type ## _array_push(sass_ ## type ## _array *a, type elem); \
void sass_ ## type ## _array_cat(sass_ ## type ## _array *a, sass_ ## type ## _array *b); \
void sass_free_ ## type ## _array(sass_ ## type ## _array *a)
#define DEFINE_SASS_ARRAY_OF(type) \
sass_ ## type ## _array sass_make_ ## type ## _array(size_t cap) { \
sass_ ## type ## _array a; \
a.contents = (type *) malloc(cap * sizeof(type)); \
if (!a.contents) { \
printf("ERROR: unable to allocate array of %s\n", #type); \
abort(); \
} \
a.capacity = cap; \
a.length = 0; \
return a; \
} \
void sass_ ## type ## _array_push(sass_ ## type ## _array *a, type elem) { \
size_t len = a->length, cap = a->capacity; \
if (len < cap) { \
a->contents[len] = elem; \
a->length++; \
} \
else { \
type *new = (type *) realloc(a->contents, len * 2); \
if (!new) { \
printf("ERROR: unable to resize array of %s\n", #type); \
abort(); \
} \
else { \
new[len] = elem; \
a->contents = new; \
a->length++; \
a->capacity = len * 2; \
} \
} \
} \
void sass_ ## type ## _array_cat(sass_ ## type ## _array *a, sass_ ## type ## _array *b) { \
size_t b_len = b->length; \
type *b_cont = b->contents; \
int i; \
for (i = 0; i < b_len; i++) sass_ ## type ## _array_push(a, b_cont[i]); \
}
#include <stdio.h>
#include <stdlib.h>
#include "test_array.h"
DEFINE_SASS_ARRAY_OF(char);
int main() {
sass_char_array a = sass_make_char_array(4);
printf("%ld, %ld\n", a.length, a.capacity);
sass_char_array_push(&a, 'h');
sass_char_array_push(&a, 'e');
sass_char_array_push(&a, 'l');
sass_char_array_push(&a, 'l');
printf("%ld, %ld\n", a.length, a.capacity);
int i;
for (i = 0; i < a.length; i++) putchar(a.contents[i]);
putchar('\n');
sass_char_array_push(&a, 'o');
printf("%ld, %ld\n", a.length, a.capacity);
for (i = 0; i < a.length; i++) putchar(a.contents[i]);
putchar('\n');
sass_char_array b = sass_make_char_array(7);
sass_char_array_push(&a, ' ');
sass_char_array_push(&a, 'w');
sass_char_array_push(&a, 'o');
sass_char_array_push(&a, 'r');
sass_char_array_push(&a, 'l');
sass_char_array_push(&a, 'd');
sass_char_array_cat(&a, &b);
printf("%ld, %ld\n", a.length, a.capacity);
for (i = 0; i < a.length; i++) putchar(a.contents[i]);
putchar('\n');
return 0;
}
\ No newline at end of file
#include "array.h"
DECLARE_SASS_ARRAY_OF(char);
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment