Skip to content

Instantly share code, notes, and snippets.

@dpwright
Last active December 12, 2015 00:18
Show Gist options
  • Save dpwright/4682425 to your computer and use it in GitHub Desktop.
Save dpwright/4682425 to your computer and use it in GitHub Desktop.
Statically-defined tuples in C
/* Basic tuple definitions */
#define ctuple1(a) union { struct { a hd; }; struct { a idx0; }; }
#define ctuple2(a, b) union { struct { a hd; ctuple1(b) tl; }; \
struct { a idx0; b idx1; }; }
#define ctuple3(a, b, c) union { struct { a hd; ctuple2(b, c) tl; }; \
struct { a idx0; b idx1; c idx2; }; }
#define ctuple4(a, b, c, d) union { struct { a hd; ctuple3(b, c, d) tl; }; \
struct { a idx0; b idx1; c idx2; d idx3; }; }
#define ctuple5(a, b, c, d, e) union { struct { a hd; ctuple4(b, c, d, e) tl; }; \
struct { a idx0; b idx1; c idx2; d idx3; e idx4; }; }
/* Scheme-style accessors */
#define car(tuple) tuple.hd
#define cdr(tuple) tuple.tl
#define caar(tuple) tuple.hd.hd
#define cadr(tuple) tuple.hd.tl
#define cdar(tuple) tuple.tl.hd
#define cddr(tuple) tuple.tl.tl
#define caaar(tuple) tuple.hd.hd.hd
#define caadr(tuple) tuple.hd.hd.tl
#define cadar(tuple) tuple.hd.tl.hd
#define caddr(tuple) tuple.hd.tl.tl
#define cdaar(tuple) tuple.tl.hd.hd
#define cdadr(tuple) tuple.tl.hd.tl
#define cddar(tuple) tuple.tl.tl.hd
#define cdddr(tuple) tuple.tl.tl.tl
#define caaaar(tuple) tuple.hd.hd.hd.hd
#define caaadr(tuple) tuple.hd.hd.hd.tl
#define caadar(tuple) tuple.hd.hd.tl.hd
#define caaddr(tuple) tuple.hd.hd.tl.tl
#define cadaar(tuple) tuple.hd.tl.hd.hd
#define cadadr(tuple) tuple.hd.tl.hd.tl
#define caddar(tuple) tuple.hd.tl.tl.hd
#define cadddr(tuple) tuple.hd.tl.tl.tl
#define cdaaar(tuple) tuple.tl.hd.hd.hd
#define cdaadr(tuple) tuple.tl.hd.hd.tl
#define cdadar(tuple) tuple.tl.hd.tl.hd
#define cdaddr(tuple) tuple.tl.hd.tl.tl
#define cddaar(tuple) tuple.tl.tl.hd.hd
#define cddadr(tuple) tuple.tl.tl.hd.tl
#define cdddar(tuple) tuple.tl.tl.tl.hd
#define cddddr(tuple) tuple.tl.tl.tl.tl
#include <stdbool.h>
#include <stdio.h>
#include "ctuple.h"
int main(int argc, char** argv)
{
ctuple2(int, int) vec = {3, 4};
ctuple2(int, ctuple2(float, char*)) test = {3, {3.f, "hello"}};
test.hd = 3.f;
ctuple4(int, float, char*, bool) tuple4 = {3, 3.14, "tuples!", true};
printf("DIRECT ACCESS\n");
printf("The int value is %d\n", tuple4.idx0);
printf("The float value is %f\n", tuple4.idx1);
printf("The string value is %s\n", tuple4.idx2);
printf("The bool value is %s\n\n", tuple4.idx3 ? "true" : "false");
printf("LIST-STYLE ITERATION\n");
printf("The int value is %d\n", tuple4.hd);
printf("The float value is %f\n", tuple4.tl.hd);
printf("The string value is %s\n", tuple4.tl.tl.hd);
printf("The bool value is %s\n\n", tuple4.tl.tl.tl.hd ? "true" : "false");
printf("SCHEME-STYLE ACCESSORS\n");
printf("The int value is %d\n", car(tuple4));
printf("The float value is %f\n", cdar(tuple4));
printf("The string value is %s\n", cddar(tuple4));
printf("The bool value is %s\n\n", cdddar(tuple4) ? "true" : "false");
ctuple4(int, ctuple2(float, float), char*, bool) tuple2d = {3, 3.14, 3.4, "tuples!", true};
printf("idx %d x %f y %f\n", tuple2d.idx0, tuple2d.idx1.idx0, tuple2d.idx1.idx1 );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment