summaryrefslogtreecommitdiff
path: root/src/crf.c
blob: dbcb0501f71d005ff3ea0b59c43f280e27fbd6fa (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
#include <stdio.h>
#include <string.h>
#include "util.h"
#include "list.h"
#include "crf.h"

struct crf_s {
  ht *table;
};

crf *
new_crf()
{
  crf *crfp = NULL;
  SAFE_MALLOC(crf, crfp, 1, goto cleanup;);

  crfp->table = new_ht2(HT_INIT_CAP);

  if (crfp->table == NULL) goto cleanup;

  return crfp;

 cleanup:

  if (crfp) free(crfp);

  return NULL;
}

BOOL
crf_add_node(crf * const restrict crfp, pair2 node)
{
  if (ht_find(crfp->table, &node)) return 0;

  pair2 *p2 = NULL;

  NEW_P2(p2, node.x, node.y, return 1;);

  ht *value_table = new_ht4(HT_INIT_CAP);

  if (value_table == NULL) {
    free(p2);
    return 1;
  }

  if (ht_insert(crfp->table, p2, value_table)) {
    fleprintf0("Fail to insert\n");
    destroy_ht(value_table, DESTROY_NONE_SELF);
    free(p2);
    return 1;
  }

  return 0;
}

ht *
crf_find_node(crf * const restrict crfp, pair2 node)
{
  return ht_find(crfp->table, &node);
}

BOOL
crf_add_edge(crf * const restrict crfp, pair2 source, pair4 label)
{
  if (ht_find(crfp->table, &source) == NULL) {
    fleprintf0("add the node before adding its edges\n");
    return 1;
  }

  if (ht_find(ht_find(crfp->table, &source), &label)) return 0;

  pair4 *p = NULL;

  NEW_P4(p, label.x, label.y, label.z, label.w, return 1;);

  if (ht_insert(ht_find(crfp->table, &source), p, (void*)1)) {
    fleprintf0("Fail to insert\n");
    free(p);
    return 1;
  }

  return 0;
}

void
destroy_crf(crf * const restrict crfp)
{

#define SIZE (ht_size(crfp->table))
#define VALUES ((ht**) ht_values(crfp->table))

  for (NUM i = 0; i < SIZE;)
    destroy_ht(*(VALUES+i++), DESTROY_KEY_SELF);

  destroy_ht(crfp->table, DESTROY_KEY_SELF);

  free(crfp);

#undef SIZE
#undef VALUES

}

struct spa_s {
  ht *table;
};

spa *
new_spa()
{
  spa *spap = NULL;
  SAFE_MALLOC(spa, spap, 1, return NULL;);

  spap->table = new_ht2(HT_INIT_CAP);

  if (spap->table == NULL) {
    free(spap);
    return NULL;
  }

  return spap;
}

void
destroy_spa(spa * const restrict spap)
{
  for (NUM i = 0; i < ht_size(spap->table); i++)
    destroy_ht(*((ht **) ht_values(spap->table)+i), DESTROY_KEY_SELF);

  /* fleprintf0("ho\n"); */

  destroy_ht(spap->table, DESTROY_KEY_SELF);

  free(spap);
}

BOOL
spa_insert(spa * const restrict spap, pair3 label)
{
  pair2 first_two = (pair2) { 0 }, *p2 = NULL;

  first_two.x = label.x;
  first_two.y = label.y;

  NUM j = label.z, *np = NULL;

  ht *value_table = NULL;

  if (ht_find(spap->table, &first_two) == NULL) {
    value_table = new_ht(HT_INIT_CAP, 0);

    if (value_table == NULL) {
      fleprintf0("Fail to have new hash table\n");
      goto cleanup;
    }

    SAFE_MALLOC(NUM, np, 1, goto cleanup;);
    *np = label.z;

    if (ht_insert(value_table, np, (void*)1)) {
      fleprintf0("Fail to insert\n");
      goto cleanup;
    }

    np = NULL;

    NEW_P2(p2, label.x, label.y, goto cleanup;);

    if (ht_insert(spap->table, p2, value_table)) {
      fleprintf0("Fail to insert\n");
      goto cleanup;
    }

    p2 = NULL;

    goto success;
  }

  if (ht_find(ht_find(spap->table, &first_two), &j) == NULL) {
    SAFE_MALLOC(NUM, np, 1, goto cleanup;);
    *np = label.z;

    if (ht_insert(ht_find(spap->table, &first_two), np, (void*)1)) {
      fleprintf0("Fail to insert\n");
      goto cleanup;
    }

    np = NULL;
  }

  goto success;

 cleanup:
  if (value_table) destroy_ht(value_table, DESTROY_KEY_SELF);
  if (p2) free(p2);
  if (np) free(np);

  return 1;

 success:
  return 0;
}

P_ATTR
BOOL
spa_belong(CCR_MOD(spa *) spap, pair3 label)
{
  pair2 first_two = (pair2) { 0 };

  first_two.x = label.x;
  first_two.y = label.y;

  if (ht_find(spap->table, &first_two) == NULL) return 0;

  NUM j = label.z;

  if (ht_find(ht_find(spap->table, &first_two), &j) == NULL) return 0;

  return 1;
}

P_ATTR
ht *
spa_find(CCR_MOD(spa *) spap, pair2 label)
{
  return ht_find(spap->table, &label);
}

typedef struct procr_array_element_s procr_array_element;

/* The list at index k is the set R_k. */
struct procr_array_element_s {
  BOOL initialized;
  List *ls;
};

struct procr_s {
  /* input length */
  NUM len;
  /* an array of lists */
  procr_array_element *array;
  /* minimal grade that is non-empty */
  NUM mg;
};

typedef struct procu_array_element_s procu_array_element;

/* The table at index k represents the set U_k.  Its keys are those
   (X, a, i, j) such that (X, a, i, j, k) belongs to U_k. */
struct procu_array_element_s {
  BOOL initialized;
  ht *table;
};

struct procu_s {
  /* input length */
  NUM len;
  /* an array of hash tables */
  procu_array_element *array;
};

procr *
new_procr(NUM size, BOOL * const restrict errorp)
{
  *errorp = 0;

  procr *pr = NULL;

  SAFE_MALLOC(procr, pr, 1, goto cleanup;);

  pr->len = size;
  pr->array = NULL;

  SAFE_MALLOC(procr_array_element, pr->array,
              sizeof(procr_array_element) * size,
              goto cleanup;);

  memset(pr->array, 0, sizeof(procr_array_element) * size);

  goto success;

 cleanup:

  *errorp = 1;

  if (pr && pr->array) free(pr->array);

  if (pr) free(pr);

 success:

  return pr;
}

procu *
new_procu(NUM size, BOOL * const restrict errorp)
{
  *errorp = 0;

  procu *pu = NULL;

  SAFE_MALLOC(procu, pu, 1, goto cleanup;);

  pu->len = size;
  pu->array = NULL;

  SAFE_MALLOC(procu_array_element, pu->array,
              sizeof(procu_array_element) * size,
              goto cleanup;);

  memset(pu->array, 0, sizeof(procu_array_element) * size);

  goto success;

 cleanup:

  *errorp = 1;

  if (pu && pu->array) free(pu->array);

  if (pu) free(pu);

 success:

  return pu;
}

void
destroy_procr(procr * const restrict pr)
{
  for (NUM i = 0; i < pr->len; i++)
    if ((pr->array+i)->initialized)
      destroy_list((pr->array+i)->ls, 1);

  free(pr->array);

  free(pr);
}

void
destroy_procu(procu * const restrict pu)
{
  for (NUM i = 0; i < pu->len; i++)
    if ((pu->array+i)->initialized)
      destroy_ht((pu->array+i)->table, DESTROY_KEY_SELF);

  free(pu->array);

  free(pu);
}

BOOL
desc_add(procr * const restrict pr, procu * const restrict pu,
         NUM grade, prodecor dec)
{
  if (grade < 0 || grade >= pu->len) {
    fleprintf("Invalid grade: %ld\n", grade);
    goto cleanup;
  }

  pair4 *p4 = NULL;

#define HELE (pu->array+grade)
#define LELE (pr->array+grade)

  if (HELE->initialized) {
    /* If HELE is initialized then LELE should also be initialized. */
    if (ht_find(HELE->table, &dec) != NULL) goto success;

    NEW_P4(p4, dec.x, dec.y, dec.z, dec.w, goto cleanup;);

    if (ht_insert(HELE->table, p4, (void*)1)) {
      fleprintf0("Fail to insert\n");
      goto cleanup;
    }

    NEW_P4(p4, dec.x, dec.y, dec.z, dec.w,
           ht_delete(HELE->table, &dec, DELETE_KEY); goto cleanup;);

    if (add_to_list(LELE->ls, p4)) {
      fleprintf0("Fail to add to list\n");
      ht_delete(HELE->table, &dec, DELETE_KEY);
      goto cleanup;
    }

    goto success;
  }

  HELE->table = new_ht4(HT_INIT_CAP);

  if (HELE->table == NULL) {
    fleprintf0("Fail to have new hash table\n");
    goto cleanup;
  }

  HELE->initialized = 1;

  NEW_P4(p4, dec.x, dec.y, dec.z, dec.w,
         destroy_ht(HELE->table, DESTROY_KEY_SELF);
         HELE->initialized = 0;
         goto cleanup;);

  if (ht_insert(HELE->table, p4, (void*)1)) {
    fleprintf0("Fail to insert\n");
    destroy_ht(HELE->table, DESTROY_KEY_SELF);
    HELE->initialized = 0;
    goto cleanup;
  }

  LELE->ls = new_list();

  if (LELE->ls == NULL) {
    fleprintf0("Fail to have new list\n");
    destroy_ht(HELE->table, DESTROY_KEY_SELF);
    HELE->initialized = 0;
    goto cleanup;
  }

  LELE->initialized = 1;

  NEW_P4(p4, dec.x, dec.y, dec.z, dec.w,
         destroy_ht(HELE->table, DESTROY_KEY_SELF);
         HELE->initialized = 0;
         destroy_list(LELE->ls, 1);
         LELE->initialized = 0;
         goto cleanup;);

  if (add_to_list(LELE->ls, p4)) {
    fleprintf0("Fail to add to list\n");
    destroy_ht(HELE->table, DESTROY_KEY_SELF);
    HELE->initialized = 0;
    destroy_list(LELE->ls, 1);
    LELE->initialized = 0;
    goto cleanup;
  }

#undef HELE
#undef LELE

  goto success;

 cleanup:

  return 1;

 success:

  return 0;
}

prodecor *
pop_procr(procr * pr, procu * pu, NUM *gradep)
{
  prodecor *result = NULL;

  if (pr->mg >= pr->len) goto success;

#define ELEMENT (pr->array+pr->mg)

  if (!(ELEMENT->initialized)) goto reset_mg;

  *gradep = pr->mg;

  result = pop_from_list(ELEMENT->ls);

  if (list_length(ELEMENT->ls) == 0) {
    destroy_ht((pu->array+pr->mg)->table, DESTROY_KEY_SELF);
    (pu->array+pr->mg)->initialized = 0;
    (pu->array+pr->mg)->table = NULL;

    destroy_list(ELEMENT->ls, 1);
    ELEMENT->initialized = 0;
    ELEMENT->ls = NULL;

    /* REVIEW: Maybe we can just increment the minimal grade, as we
       won't jump over two steps? */
  reset_mg:
    while (++(pr->mg) < pr->len) {
      if (ELEMENT->initialized) break;
    }
  }

#undef ELEMENT

 success:
  return result;
}