PULSAR  2.0.0
Parallel Ultra-Light Systolic Array Runtime
 All Data Structures Files Functions Typedefs Enumerations 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 
31 int *prt_tuple_new(int len, ...)
32 {
33  int *tuple = (int*)malloc((len+1)*sizeof(int));
34  prt_assert(tuple != NULL, "malloc failed");
35  int i;
36  va_list ap;
37  va_start(ap, len);
38  for (i = 0; i < len; i++)
39  tuple[i] = va_arg(ap, int);
40  va_end(ap);
41  tuple[len] = INT_MAX;
42  return tuple;
43 }
44 
46 
53 int prt_tuple_len(int *tuple)
54 {
55  int *p = tuple;
56  while (*p++ != INT_MAX);
57  return (p-tuple-1);
58 }
59 
61 
70 int* prt_tuple_cat(int *first_tuple, ...)
71 {
72  int len;
73  int *tuple;
74  va_list ap;
75  va_start(ap, first_tuple);
76  len = prt_tuple_len(first_tuple);
77  while ((tuple = va_arg(ap, int*)) != NULL)
78  len += prt_tuple_len(tuple);
79  va_end(ap);
80 
81  int *out_tuple = (int*)malloc((len+1)*sizeof(int));
82  prt_assert(out_tuple != NULL, "malloc failed");
83  va_start(ap, first_tuple);
84  while(*first_tuple != INT_MAX)
85  *out_tuple++ = *first_tuple++;
86  while ((tuple = va_arg(ap, int*)) != NULL)
87  while(*tuple != INT_MAX)
88  *out_tuple++ = *tuple++;
89  va_end(ap);
90 
91  *out_tuple = INT_MAX;
92  return (out_tuple-len);
93 }
94 
96 
101 void prt_tuple_delete(int *tuple)
102 {
103  // Free the tuple.
104  free(tuple);
105 }
106 
108 
115 int *prt_tuple_copy(int *in_tuple)
116 {
117  int i;
118  int len = 0;
119  int *out_tuple;
120  while (in_tuple[len] != INT_MAX)
121  len++;
122  out_tuple = (int*)malloc((len+1)*sizeof(int));
123  prt_assert(out_tuple != NULL, "malloc failed");
124  for (i = 0; i < len; i++)
125  out_tuple[i] = in_tuple[i];
126  out_tuple[len] = INT_MAX;
127  return out_tuple;
128 }
129 
131 
141 int prt_tuple_compare(void *tuple_a, void *tuple_b)
142 {
143  int *tupa = (int*)tuple_a;
144  int *tupb = (int*)tuple_b;
145  while (*tupa != INT_MAX && *tupb != INT_MAX) {
146  if (*tupa < *tupb) return -1;
147  else if (*tupa > *tupb) return 1;
148  tupa++;
149  tupb++;
150  }
151  if (*tupa == INT_MAX && *tupb == INT_MAX) return 0;
152  else if (*tupa == INT_MAX) return -1;
153  else if (*tupb == INT_MAX) return 1;
154 }
155 
157 
167 int prt_tuple_equal(void *tuple_a, void *tuple_b)
168 {
169  return (prt_tuple_compare(tuple_a, tuple_b) == 0);
170 }
171 
173 
178 void prt_tuple_print(int *tuple)
179 {
180  while (*tuple != INT_MAX)
181  printf("%3d", *tuple++);
182 }
183 
185 
194 unsigned int prt_tuple_hash(void *tuple)
195 {
196  int len = 0;
197  int *tup = (int*)tuple;
198  while (tup[len] != INT_MAX)
199  len++;
200  return fnv_hash(tuple, len*sizeof(int));
201 }
202 
204 
213 static unsigned int fnv_hash(void *key, int len)
214 {
215  unsigned char *p = key;
216  unsigned h = 2166136261;
217  int i;
218  for (i = 0; i < len; i++)
219  h = (h * 16777619) ^ p[i];
220  return h;
221 }