summaryrefslogtreecommitdiff
path: root/src/tuple.c
blob: 92f2e6a27dfa858bf844340e381bc2bf9e59cf23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#include <stdio.h>
#include "tuple.h"

#define TUP(N, M) struct PASTER(tuple, N, _s) { \
    struct PASTER(tuple, M, _s) *array;         \
    BOOL initialized;                           \
  }

/* "lengthed" tuple */
#define LTUP(N) struct PASTER(luple, N, _s) {   \
    struct PASTER(tuple, N, _s) tuple;          \
    NUM *lengths;                               \
  }

#define CONSTRUCT(N, M) UNUSED static                           \
  PASTER(tuple, N,) *PASTER                                     \
       (new, _tuple, N)() {                                     \
    PASTER(tuple, N,) *result = NULL;                           \
    SAFE_CALLOC(PASTER(tuple, N,), result, 1, return NULL;);    \
    return result;                                              \
  }                                                             \
  MY_Static_MES("To require a semicolon!")

#define LONSTRUCT(N) PASTER(luple, N,) *PASTER                  \
       (new_luple, N,)(NUM *len) {                              \
    PASTER(luple, N,) *result = NULL;                           \
    SAFE_CALLOC(PASTER(luple, N,), result, 1, return NULL;);    \
    result->lengths = len;                                      \
    return result;                                              \
  }                                                             \
  MY_Static_MES("To require a semicolon!")

#define DESTRUCT(N, M) static void PASTER(destroy_tuple, N,)    \
       (PASTER(tuple, N,) tup, NUM *len) {                      \
    if (!(tup.initialized)) return;                             \
    for (NUM PASTER(i, N,) = 0;                                 \
         PASTER(i, N,) < *len;                                  \
         PASTER(i, N,)++) {                                     \
      PASTER(destroy_tuple, M,)(*(tup.array+PASTER(i, N,)),     \
                                len+1);                         \
    }                                                           \
    free(tup.array);                                            \
  }                                                             \
  MY_Static_MES("To require a semicolon!")

#define LESTRUCT(N) void PASTER(destroy_luple, N,)              \
       (PASTER(luple, N,) *lup) {                               \
    if (lup == NULL) return;                                    \
    PASTER(destroy_tuple, N,)(lup->tuple, lup->lengths);        \
    free(lup->lengths);                                         \
    free(lup);                                                  \
  }                                                             \
  MY_Static_MES("To require a semicolon!")

/* NOTE: The function call return should be the last thing in the
   function body, so that GCC knows to perform tail-call optimization,
   with optimization level at least 2 I guess. */
#define ADDT(N, M) static BOOL PASTER(add_to_tuple, N,)         \
       (PASTER(tuple, N,) *tup, NUM *len,                       \
        PASTER(pair, N,) label, NUM val) {                      \
    if (label.x < 0 || label.x >= *len) {                       \
      fleprintf("Invalid label = %ld, len = %ld\n",             \
                label.x, *len);                                 \
      goto cleanup;                                             \
    }                                                           \
    if (!(tup->initialized)) {                                  \
      SAFE_CALLOC                                               \
        (PASTER(tuple, M,), tup->array, *len, goto cleanup;);   \
      tup->initialized = 1;                                     \
    }                                                           \
    PASTER(pair, M,) newlabel = (PASTER(pair, M,)) { 0 };       \
    PASTER(COPY_PAIR, M,)(newlabel, label);                     \
    goto success;                                               \
  cleanup:                                                      \
    return 1;                                                   \
  success:                                                      \
    return PASTER(add_to_tuple, M,)                             \
      (tup->array+label.x, len+1, newlabel, val);               \
  }                                                             \
  MY_Static_MES("To require a semicolon!")

#define ADDL(N) BOOL PASTER(add_to_luple, N,)                   \
       (PASTER(luple, N,) *lup, PASTER(pair, N,) label,         \
        NUM val) {                                              \
    return PASTER(add_to_tuple, N,)                             \
      ((PASTER(tuple, N,) *) lup, lup->lengths, label, val);    \
  }                                                             \
  MY_Static_MES("To require a semicolon!")

#define INDEXT(N, M) HP_ATTR static NUM *                       \
  PASTER(tuple, N, _find)                                       \
       (PASTER(tuple, N,) *tup, NUM *len,                       \
        PASTER(pair, N,) label) {                               \
    if (!(tup->initialized)) return NULL;                       \
    if (label.x < 0 || label.x >= *len) return NULL;            \
    PASTER(pair, M,) newlabel = (PASTER(pair, M,)) { 0 };       \
    PASTER(COPY_PAIR, M,)(newlabel, label);                     \
    return PASTER(tuple, M, _find)                              \
      (tup->array+label.x, len+1, newlabel);                    \
  }                                                             \
  MY_Static_MES("To require a semicolon!")

#define INDEXL(N) HP_ATTR NUM *PASTER(luple, N, _find)          \
       (PASTER(luple, N,) *lup, PASTER(pair, N,) label) {       \
    return PASTER(tuple, N, _find)                              \
      ((PASTER(tuple, N,) *) lup, lup->lengths, label);         \
  }                                                             \
  MY_Static_MES("To require a semicolon!")

#define INDEXPT(N, M, P, Q) HP_ATTR static BOOL                 \
  PASTER(PASTER(tuple, N, _pf), _, M)                           \
       (PASTER(tuple, N,) *tup, NUM *len,                       \
        PASTER(pair, M,) label) {                               \
    if (!(tup->initialized)) return 0;                          \
    if (label.x < 0 || label.x >= *len) return 0;               \
    PASTER(pair, Q,) newlabel = (PASTER(pair, Q,)) { 0 };       \
    PASTER(COPY_PAIR, Q,)(newlabel, label);                     \
    return PASTER(PASTER(tuple, P, _pf), _, Q)                  \
      (tup->array+label.x, len+1, newlabel);                    \
  }                                                             \
  MY_Static_MES("To require a semicolon!")

#define INDEXPL(N, M) HP_ATTR BOOL                              \
  PASTER(PASTER(luple, N, _pf), _, M)                           \
       (PASTER(luple, N,) *lup, PASTER(pair, M,) label) {       \
    return PASTER(PASTER(tuple, N, _pf), _, M)                  \
      ((PASTER(tuple, N,) *) lup, lup->lengths, label);         \
  }                                                             \
  MY_Static_MES("To require a semicolon!")

#define LENGHL(N) P_ATTR NUM *PASTER(luple, N, _len)    \
       (PASTER(luple, N,) *lup) {                       \
    return lup->lengths;                                \
  }                                                     \
  MY_Static_MES("To require a semicolon!")

/* TODO: Memory report */

struct tuple1_s {
  NUM *array;
  BOOL initialized;
};

TUP(2, 1);
TUP(3, 2);
TUP(4, 3);
TUP(5, 4);
TUP(6, 5);

LTUP(2);
LTUP(3);
LTUP(4);
LTUP(5);
LTUP(6);

UNUSED
static tuple1 *
new_tuple1(NUM len)
{
  tuple1 *result = NULL;
  SAFE_CALLOC(tuple1, result, 1, return NULL;);
  SAFE_CALLOC(NUM, result->array, len, free(result); return NULL;);
  return result;
}

CONSTRUCT(2, 1);
CONSTRUCT(3, 2);

tuple4 *
new_tuple4()
{
  tuple4 *result = NULL;
  SAFE_CALLOC(tuple4, result, 1, return NULL;);
  return result;
}

CONSTRUCT(5, 4);
CONSTRUCT(6, 5);

LONSTRUCT(2);
LONSTRUCT(3);
LONSTRUCT(4);
LONSTRUCT(5);
LONSTRUCT(6);

static void
destroy_tuple1(tuple1 tup, NUM * UNUSED len) {
  if (!(tup.initialized)) return;
  free(tup.array);
}

DESTRUCT(2, 1);
DESTRUCT(3, 2);

void
destroy_tuple4(tuple4 tup, NUM *len)
{
  if (!(tup.initialized)) return;
  for (NUM i4 = 0; i4 < *len; i4++)
    destroy_tuple3(*(tup.array+i4), len+1);
  free(tup.array);
}

DESTRUCT(5, 4);
DESTRUCT(6, 5);

LESTRUCT(2);
LESTRUCT(3);
LESTRUCT(4);
LESTRUCT(5);
LESTRUCT(6);

static BOOL
add_to_tuple1(tuple1 *tup, NUM *len, pair1 label, NUM val)
{
  if (!(tup->initialized)) {
    SAFE_CALLOC(NUM, tup->array, *len, goto cleanup;);
  }
  tup->initialized = 1;

  if (label < 0 || label >= *len) {
    fleprintf("Invalid label = %ld, len = %ld\n",
              label, *len);
    goto cleanup;
  }

  *(tup->array+label) = val;

  return 0;

 cleanup:
  return 1;
}

static BOOL
add_to_tuple2(tuple2 *tup, NUM *len, pair2 label, NUM val)
{
  if (label.x < 0 || label.x >= *len) {
    fleprintf("Invalid label = %ld, len = %ld\n",
              label.x, *len);
    goto cleanup;
  }

  if (!(tup->initialized)) {
    SAFE_CALLOC(tuple1, tup->array, *len, goto cleanup;);
    tup->initialized = 1;
  }

  pair1 newlabel = label.y;

  goto success;

 cleanup:
  return 1;

 success:
  return add_to_tuple1(tup->array+label.x, len+1, newlabel, val);
}

ADDT(3, 2);
ADDT(4, 3);
ADDT(5, 4);
ADDT(6, 5);

ADDL(2);
ADDL(3);
ADDL(4);
ADDL(5);
ADDL(6);

H_ATTR
static BOOL
add_to_tuple_6_pt_2(tuple6 *tup, NUM *len, pair2 label)
{
  if (label.x < 0 || label.x >= *len) {
    fleprintf("Invalid label = %ld, len = %ld\n", label.x, *len);
    goto cleanup;
  }

  if (!(tup->initialized)) {
    SAFE_CALLOC(tuple5, tup->array, *len, goto cleanup;);
    tup->initialized = 1;
  }

  if (!((tup->array+label.x)->initialized)) {
    SAFE_CALLOC(tuple4, (tup->array+label.x)->array, *(len+1),
                goto cleanup;);
    (tup->array+label.x)->initialized = 1;
  }

  if (label.y < 0 || label.y >= *(len+1)) {
    fleprintf("Invalid label = %ld, len = %ld\n", label.y, *(len+1));
    goto cleanup;
  }

  if (!(((tup->array+label.x)->array+label.y)->initialized)) {
    SAFE_CALLOC(tuple3,
                ((tup->array+label.x)->array+label.y)->array,
                *(len+1), goto cleanup;);
    ((tup->array+label.x)->array+label.y)->initialized = 1;
  }

  goto success;

 cleanup:
  return 1;

 success:
  return 0;
}

H_ATTR
BOOL
add_to_luple6_pt_2(luple6 *lup, pair2 label)
{
  return add_to_tuple_6_pt_2((tuple6 *) lup, lup->lengths, label);
}

HP_ATTR
static NUM *
tuple1_find(tuple1 *tup, NUM *len, NUM label)
{
  if (!(tup->initialized)) return NULL;
  if (label < 0 || label >= *len) return NULL;
  return tup->array+label;
}

HP_ATTR
static NUM *
tuple2_find(tuple2 *tup, NUM *len, pair2 label)
{
  if (!(tup->initialized)) return NULL;
  if (label.x < 0 || label.x >= *len) return NULL;

  return tuple1_find(tup->array+label.x, len+1, label.y);
}

INDEXT(3, 2);
INDEXT(4, 3);
INDEXT(5, 4);
INDEXT(6, 5);

INDEXL(2);
INDEXL(3);
INDEXL(4);
INDEXL(5);
INDEXL(6);

HP_ATTR
static BOOL
tuple5_pf_1(tuple5 *tup, NUM *len, NUM label)
{
  if (!(tup->initialized)) return 0;
  if (label < 0 || label >= *len) return 0;

  return (tup->array+label)->initialized;
}

HP_ATTR
static BOOL
tuple6_pf_2(tuple6 *tup, NUM *len, pair2 label)
{
  if (!(tup->initialized)) return 0;
  if (label.x < 0 || label.x >= *len) return 0;
  NUM newlabel = label.y;

  return tuple5_pf_1(tup->array+label.x, len+1, newlabel);
}

INDEXPL(6, 2);

HP_ATTR
static BOOL
tuple3_pf_1(tuple3 *tup, NUM *len, NUM label)
{
  if (!(tup->initialized)) return 0;
  if (label < 0 || label >= *len) return 0;

  return (tup->array+label)->initialized;
}

HP_ATTR
static BOOL
tuple4_pf_2(tuple4 *tup, NUM *len, pair2 label)
{
  if (!(tup->initialized)) return 0;
  if (label.x < 0 || label.x >= *len) return 0;
  NUM newlabel = label.y;

  return tuple3_pf_1(tup->array+label.x, len+1, newlabel);
}

INDEXPT(5, 3, 4, 2);

INDEXPL(5, 3);

HP_ATTR
static BOOL
tuple2_pf_1(tuple2 *tup, NUM *len, NUM label)
{
  if (!(tup->initialized)) return 0;
  if (label < 0 || label >= *len) return 0;

  return (tup->array+label)->initialized;
}

HP_ATTR
static BOOL
tuple3_pf_2(tuple3 *tup, NUM *len, pair2 label)
{
  if (!(tup->initialized)) return 0;
  if (label.x < 0 || label.x >= *len) return 0;
  NUM newlabel = label.y;

  return tuple2_pf_1(tup->array+label.x, len+1, newlabel);
}

INDEXPL(3, 2);

LENGHL(2);
LENGHL(3);
LENGHL(4);
LENGHL(5);
LENGHL(6);

H_ATTR
static void
tuple3_map_2(tuple3 *tup, NUM *len, pair2 label, map_pair3_t fn)
{
  if (!(tup->initialized)) return;
  if (label.x < 0 || label.x >= *len) return;

#define AR (tup->array+label.x)
#define ARR (AR->array+label.y)
#define ARRR (ARR->array+i)

  if (!(AR->initialized)) return;

  if (label.y < 0 || label.y >= *(len+1)) return;

  if (!(ARR->initialized)) return;

  for (NUM i = 0; i < *(len+2); i++)
    if (*ARRR) fn((pair3) { label.x, label.y, i });

#undef AR
#undef ARR
#undef ARRR

}

H_ATTR
static void
tuple6_map_2(tuple6 *tup, NUM *len, pair2 label,
             NUM max, map_pair6_t fn)
{
  if (!(tup->initialized)) return;
  if (label.x < 0 || label.x >= *len) return;

  tuple5 *AR1 = (tup->array+label.x);

  if (!(AR1->initialized)) return;

  if (label.y < 0 || label.y >= *(len+1)) return;

  tuple4 *AR2 = (AR1->array+label.y);

  if (!(AR2->initialized)) return;

  max = (max < 0 || max+1 >= *(len+5)) ? *(len+5) : max+1;

  for (NUM i = 0; i < *(len+2); i++) {
    tuple3 *AR3 = (AR2->array+i);
    if (!(AR3->initialized)) continue;

    for (NUM j = 0; j < *(len+3); j++) {
      tuple2 *AR4 = (AR3->array+j);
      if (!(AR4->initialized)) continue;

      for (NUM k = 0; k < *(len+4); k++) {
        tuple1 *AR5 = (AR4->array+k);
        if (!(AR5->initialized)) continue;

        for (NUM ell = 0; ell < max; ell++) {
          if (*(AR5->array+ell))
            fn((pair6) { label.x, label.y, i, j, k, ell });
        }
      }
    }
  }
}

H_ATTR
static void
tuple6_map_5(tuple6 *tup, NUM *len, pair5 label, map_pair6_t fn)
{
  if (!(tup->initialized)) return;
  if (label.x < 0 || label.x >= *len) return;

  tuple5 *AR1 = (tup->array+label.x);

  if (!(AR1->initialized)) return;

  if (label.y < 0 || label.y >= *(len+1)) return;

  tuple4 *AR2 = (AR1->array+label.y);

  if (!(AR2->initialized)) return;

  if (label.z < 0 || label.z >= *(len+2)) return;

  tuple3 *AR3 = (AR2->array+label.z);

  if (!(AR3->initialized)) return;

  if (label.u < 0 || label.u >= *(len+3)) return;

  tuple2 *AR4 = (AR3->array+label.u);

  if (!(AR4->initialized)) return;

  if (label.v < 0 || label.v >= *(len+4)) return;

  tuple1 *AR5 = (AR4->array+label.v);

  if (!(AR5->initialized)) return;

  for (NUM i = 0; i < *(len+5); i++) {
    if (*(AR5->array+i))
      fn((pair6) { label.x, label.y, label.z, label.u, label.v, i });
  }
}

H_ATTR
void
tuple5_map_3(tuple5 *tup, NUM *len, pair3 label, map_pair5_t fn)
{
  if (!(tup->initialized)) return;

  if (label.x < 0 || label.x >= *len) return;

  tuple4 *AR1 = tup->array+label.x;

  if (!(AR1->initialized)) return;

  if (label.y < 0 || label.y >= *(len+1)) return;

  tuple3 *AR2 = AR1->array+label.y;

  if (!(AR2->initialized)) return;

  if (label.z < 0 || label.z >= *(len+2)) return;

  tuple2 *AR3 = AR2->array+label.z;

  if (!(AR3->initialized)) return;

  for (NUM i = 0; i < *(len+3); i++) {
    tuple1 *AR4 = AR3->array+i;

    if (!(AR4->initialized)) continue;

    for (NUM j = 0; j < *(len+4); j++) {
      if (*(AR4->array+j))
        fn((pair5) { label.x, label.y, label.z, i, j });
    }
  }
}

H_ATTR
void
luple5_map_3(luple5 *lup, pair3 label, map_pair5_t fn)
{
  tuple5_map_3((tuple5 *) lup, lup->lengths, label, fn);
}

H_ATTR
void
luple3_map_2(luple3 *lup, pair2 label, map_pair3_t fn)
{
  tuple3_map_2((tuple3 *) lup, lup->lengths, label, fn);
}

H_ATTR
void
luple6_map_2(luple6 *lup, pair2 label, NUM max, map_pair6_t fn)
{
  tuple6_map_2((tuple6 *) lup, lup->lengths, label, max, fn);
}

H_ATTR
void
luple6_map_5(luple6 *lup, pair5 label, map_pair6_t fn)
{
  tuple6_map_5((tuple6 *) lup, lup->lengths, label, fn);
}

H_ATTR
void
luple5_free_1(luple5 *lup, NUM label)
{
  if (lup == NULL) return;

  tuple5 *tuple = (tuple5 *) lup;

  if (!(tuple->initialized)) return;

  if (label < 0 || label >= *(lup->lengths)) return;

  if (!((tuple->array+label)->initialized)) return;

  destroy_tuple4(*(tuple->array+label), lup->lengths+1);

  (tuple->array+label)->initialized = 0;
}

#undef TUP
#undef LTUP
#undef CONSTRUCT
#undef LONSTRUCT
#undef DESTRUCT
#undef LESTRUCT
#undef ADDT
#undef ADDL
#undef INDEXT
#undef INDEXL
#undef INDEXPT
#undef INDEXPL
#undef LENTHL