major reorganisation
[rrq/rrqmisc.git] / logic / Tuple.c
1 #include <stdarg.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <Tuple.h>
5
6 // Allocate zero-ed
7 Tuple *Tuple_calloc(unsigned long arity) {
8     Tuple *t = (Tuple *) malloc( sizeof( Tuple ) + arity * sizeof( void* ) );
9     t->size = arity;
10     memset( t->elements, 0, arity * sizeof( void* ) );
11     return t;
12 }
13
14 // Allocate with values
15 Tuple *Tuple_create(int arity,...) {
16     va_list ap;
17     int i;
18     Tuple *t = (Tuple *) malloc( sizeof( Tuple ) + arity * sizeof( void* ) );
19     t->size = arity;
20     va_start( ap, arity );
21     for ( i = 0; i < arity; i++ ) {
22         t->elements[i] = va_arg( ap, void* );
23     }
24     va_end( ap );
25     return t;
26 }
27
28 // Duplicate
29 Tuple *Tuple_clone(Tuple *t) {
30     unsigned long size = t->size * sizeof( void* );
31     Tuple *ct = (Tuple *) malloc( sizeof( Tuple ) + size );
32     memcpy( ct, t, size );
33     return ct;
34 }