06eba85778e4e654623aa1c0ca869af9c2e6b84a
[rrq/rrqmisc.git] / vector / Tuple.c
1 #include <stdarg.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <Tuple.h>
5
6 // Allocate
7 Tuple *Tuple_create(int arity,...) {
8     va_list ap;
9     int i;
10     Tuple *t = (Tuple *) malloc( sizeof( Tuple ) + arity * sizeof( void* ) );
11     t->types = 0; // unknown types
12     va_start( ap, arity );
13     for ( i = 0; i < arity; i++ ) {
14         t->elements[i] = va_arg( ap, void* );
15     }
16     va_end( ap );
17     return t;
18 }
19
20 Tuple *Tuple_clone(int arity,Tuple *t) {
21     Tuple *ct = (Tuple *)malloc( sizeof( Tuple ) + arity * sizeof( void* ) );
22     memcpy( ct, t, arity * sizeof( void* ) );
23     return ct;
24 }
25
26 unsigned long Tuple_mask(int arity,Tuple *t) {
27     unsigned long mask = 0;
28     while ( arity-- > 0 ) {
29         mask <<= 1;
30         if ( t->elements[ arity ] ) {
31             mask |= 1;
32         }
33     }
34     return mask;
35 }