PULSAR  1.0.0
Parallel Ultra Light Systolic Array Runtime
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
prt_tuple.c
Go to the documentation of this file.
1 
11 #include "prt_tuple.h"
12 
13 static unsigned int fnv_hash(void *key, int len);
14 
16 
25 int *prt_tuple_new(int len, ...)
26 {
27  int *tuple = (int*)malloc((len+1)*sizeof(int));
28  prt_assert(tuple != NULL, "malloc failed");
29  int i;
30  va_list ap;
31  va_start(ap, len);
32  for (i = 0; i < len; i++)
33  tuple[i] = va_arg(ap, int);
34  va_end(ap);
35  tuple[len] = INT_MAX;
36  return tuple;
37 }
38 
40 
47 int prt_tuple_len(int *tuple)
48 {
49  int *p = tuple;
50  while (*p++ != INT_MAX);
51  return (p-tuple-1);
52 }
53 
55 
64 int* prt_tuple_cat(int *first_tuple, ...)
65 {
66  int len;
67  int *tuple;
68  va_list ap;
69  va_start(ap, first_tuple);
70  len = prt_tuple_len(first_tuple);
71  while ((tuple = va_arg(ap, int*)) != NULL)
72  len += prt_tuple_len(tuple);
73  va_end(ap);
74 
75  int *out_tuple = (int*)malloc((len+1)*sizeof(int));
76  prt_assert(out_tuple != NULL, "malloc failed");
77  va_start(ap, first_tuple);
78  while(*first_tuple != INT_MAX)
79  *out_tuple++ = *first_tuple++;
80  while ((tuple = va_arg(ap, int*)) != NULL)
81  while(*tuple != INT_MAX)
82  *out_tuple++ = *tuple++;
83  va_end(ap);
84 
85  *out_tuple = INT_MAX;
86  return (out_tuple-len);
87 }
88 
90 
95 void prt_tuple_delete(int *tuple)
96 {
97  // Free the tuple.
98  free(tuple);
99 }
100 
102 
109 int *prt_tuple_copy(int *in_tuple)
110 {
111  int i;
112  int len = 0;
113  int *out_tuple;
114  while (in_tuple[len] != INT_MAX)
115  len++;
116  out_tuple = (int*)malloc((len+1)*sizeof(int));
117  prt_assert(out_tuple != NULL, "malloc failed");
118  for (i = 0; i < len; i++)
119  out_tuple[i] = in_tuple[i];
120  out_tuple[len] = INT_MAX;
121  return out_tuple;
122 }
123 
125 
135 int prt_tuple_compare(void *tuple_a, void *tuple_b)
136 {
137  int *tupa = (int*)tuple_a;
138  int *tupb = (int*)tuple_b;
139  while (*tupa != INT_MAX && *tupb != INT_MAX) {
140  if (*tupa < *tupb) return -1;
141  else if (*tupa > *tupb) return 1;
142  tupa++;
143  tupb++;
144  }
145  if (*tupa == INT_MAX && *tupb == INT_MAX) return 0;
146  else if (*tupa == INT_MAX) return -1;
147  else if (*tupb == INT_MAX) return 1;
148 }
149 
151 
161 int prt_tuple_equal(void *tuple_a, void *tuple_b)
162 {
163  return (prt_tuple_compare(tuple_a, tuple_b) == 0);
164 }
165 
167 
172 void prt_tuple_print(int *tuple)
173 {
174  while (*tuple != INT_MAX)
175  printf("%3d", *tuple++);
176 }
177 
179 
188 unsigned int prt_tuple_hash(void *tuple)
189 {
190  int len = 0;
191  int *tup = (int*)tuple;
192  while (tup[len] != INT_MAX)
193  len++;
194  return fnv_hash(tuple, len*sizeof(int));
195 }
196 
198 
207 static unsigned int fnv_hash(void *key, int len)
208 {
209  unsigned char *p = key;
210  unsigned h = 2166136261;
211  int i;
212  for (i = 0; i < len; i++)
213  h = (h * 16777619) ^ p[i];
214  return h;
215 }
void prt_tuple_print(int *tuple)
tuple print
Definition: prt_tuple.c:172
int * prt_tuple_cat(int *first_tuple,...)
tuple concatenate Join a variable length, NULL-terminated, list of tuples.
Definition: prt_tuple.c:64
int prt_tuple_compare(void *tuple_a, void *tuple_b)
tuple comparison
Definition: prt_tuple.c:135
PRT tuple.
unsigned int prt_tuple_hash(void *tuple)
tuple hash Required by the VSA&#39;s tuples hash table. Computes the lenght in characters and calls a str...
Definition: prt_tuple.c:188
int prt_tuple_len(int *tuple)
tuple length
Definition: prt_tuple.c:47
int * prt_tuple_copy(int *in_tuple)
tuple copy constructor
Definition: prt_tuple.c:109
void prt_tuple_delete(int *tuple)
tuple destructor
Definition: prt_tuple.c:95
int * prt_tuple_new(int len,...)
tuple constructor Allocates memory for the tuple plus the termination symbol (INT_MAX).
Definition: prt_tuple.c:25
#define prt_assert(cond, msg)
Definition: prt_assert.h:30
int prt_tuple_equal(void *tuple_a, void *tuple_b)
tuple equality check Check if tuples are identical in length and content.
Definition: prt_tuple.c:161