aboutsummaryrefslogtreecommitdiff
path: root/src/stdlib/tables.c
blob: 284b6e8319bc80e8c6f2ded4be9ffb9d758832ea (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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
// table.c - C Hash table implementation
// Copyright 2024 Bruce Hill
// Provided under the MIT license with the Commons Clause
// See included LICENSE for details.

// Hash table (aka Dictionary) Implementation
// Hash keys and values are stored *by value*
// The hash insertion/lookup implementation is based on Lua's tables,
// which use a chained scatter with Brent's variation.

#include <assert.h>
#include <gc.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>

#include "c_strings.h"
#include "datatypes.h"
#include "lists.h"
#include "memory.h"
#include "metamethods.h"
#include "pointers.h"
#include "print.h"
#include "siphash.h"
#include "tables.h"
#include "text.h"
#include "types.h"
#include "util.h"

// #define DEBUG_TABLES

#ifdef DEBUG_TABLES
#define hdebug(...) print_inline("\x1b[2m", __VA_ARGS__, "\x1b[m")
#else
#define hdebug(...) (void)0
#endif

// Helper accessors for type functions/values:
#define HASH_KEY(t, k) (generic_hash((k), type->TableInfo.key) % ((t).bucket_info->count))
#define EQUAL_KEYS(x, y) (generic_equal((x), (y), type->TableInfo.key))
#define END_OF_CHAIN UINT32_MAX

#define GET_ENTRY(t, i) ((t).entries.data + (t).entries.stride*(i))

static TypeInfo_t MemoryPointer = {
    .size=sizeof(void*),
    .align=__alignof__(void*),
    .tag=PointerInfo,
    .PointerInfo={
        .sigil="@",
        .pointed=&Memoryヽinfo,
    },
    .metamethods=Pointerヽmetamethods,
};

const TypeInfo_t CStrToVoidStarTable = {
    .size=sizeof(Table_t),
    .align=__alignof__(Table_t),
    .tag=TableInfo,
    .TableInfo={.key=&CStringヽinfo, .value=&MemoryPointer},
    .metamethods=Tableヽmetamethods,
};

PUREFUNC static INLINE size_t entry_size(const TypeInfo_t *info)
{
    size_t size = (size_t)info->TableInfo.key->size;
    if (info->TableInfo.value->align > 1 && size % (size_t)info->TableInfo.value->align)
        size += (size_t)info->TableInfo.value->align - (size % (size_t)info->TableInfo.value->align); // padding
    size += (size_t)info->TableInfo.value->size;
    if (info->TableInfo.key->align > 1 && size % (size_t)info->TableInfo.key->align)
        size += (size_t)info->TableInfo.key->align - (size % (size_t)info->TableInfo.key->align); // padding
    return size;
}

PUREFUNC static INLINE size_t value_offset(const TypeInfo_t *info)
{
    size_t offset = (size_t)info->TableInfo.key->size;
    if ((size_t)info->TableInfo.value->align > 1 && offset % (size_t)info->TableInfo.value->align)
        offset += (size_t)info->TableInfo.value->align - (offset % (size_t)info->TableInfo.value->align); // padding
    return offset;
}

static INLINE void hshow(const Table_t *t)
{
    hdebug("{");
    for (uint32_t i = 0; t->bucket_info && i < t->bucket_info->count; i++) {
        if (i > 0) hdebug(" ");
        if (t->bucket_info->buckets[i].occupied)
            hdebug("[", i, "]=", (uint32_t)t->bucket_info->buckets[i].index, "(", t->bucket_info->buckets[i].next_bucket, ")");
        else
            hdebug("[", i, "]=_");
    }
    hdebug("}\n");
}

static void maybe_copy_on_write(Table_t *t, const TypeInfo_t *type)
{
    if (t->entries.data_refcount != 0)
        Listヽcompact(&t->entries, (int64_t)entry_size(type));

    if (t->bucket_info && t->bucket_info->data_refcount != 0) {
        size_t size = sizeof(bucket_info_t) + sizeof(bucket_t[t->bucket_info->count]);
        t->bucket_info = memcpy(GC_MALLOC(size), t->bucket_info, size);
        t->bucket_info->data_refcount = 0;
    }
}

// Return address of value or NULL
PUREFUNC public void *Tableヽget_raw(Table_t t, const void *key, const TypeInfo_t *type)
{
    assert(type->tag == TableInfo);
    if (!key || !t.bucket_info) return NULL;

    uint64_t hash = HASH_KEY(t, key);
    hshow(&t);
    hdebug("Getting value with initial probe at ", hash, "\n");
    bucket_t *buckets = t.bucket_info->buckets;
    for (uint64_t i = hash; buckets[i].occupied; i = buckets[i].next_bucket) {
        hdebug("Checking against key in bucket ", i, "\n");
        void *entry = GET_ENTRY(t, buckets[i].index);
        if (EQUAL_KEYS(entry, key)) {
            hdebug("Found key!\n");
            return entry + value_offset(type);
        }
        if (buckets[i].next_bucket == END_OF_CHAIN)
            break;
    }
    return NULL;
}

PUREFUNC public void *Tableヽget(Table_t t, const void *key, const TypeInfo_t *type)
{
    assert(type->tag == TableInfo);
    for (const Table_t *iter = &t; iter; iter = iter->fallback) {
        void *ret = Tableヽget_raw(*iter, key, type);
        if (ret) return ret;
    }
    return NULL;
}

static void Tableヽset_bucket(Table_t *t, const void *entry, int32_t index, const TypeInfo_t *type)
{
    assert(t->bucket_info);
    hshow(t);
    const void *key = entry;
    bucket_t *buckets = t->bucket_info->buckets;
    uint64_t hash = HASH_KEY(*t, key);
    hdebug("Hash value (mod ", (int32_t)t->bucket_info->count, ") = ", hash, "\n");
    bucket_t *bucket = &buckets[hash];
    if (!bucket->occupied) {
        hdebug("Got an empty space\n");
        // Empty space:
        bucket->occupied = 1;
        bucket->index = index;
        bucket->next_bucket = END_OF_CHAIN;
        hshow(t);
        return;
    }

    hdebug("Collision detected in bucket ", hash, " (entry ", (uint32_t)bucket->index, ")\n");

    while (buckets[t->bucket_info->last_free].occupied) {
        assert(t->bucket_info->last_free > 0);
        --t->bucket_info->last_free;
    }

    uint64_t collided_hash = HASH_KEY(*t, GET_ENTRY(*t, bucket->index));
    if (collided_hash != hash) { // Collided with a mid-chain entry
        hdebug("Hit a mid-chain entry at bucket ", hash, " (chain starting at ", collided_hash, ")\n");
        // Find chain predecessor
        uint64_t predecessor = collided_hash;
        while (buckets[predecessor].next_bucket != hash)
            predecessor = buckets[predecessor].next_bucket;

        // Move mid-chain entry to free space and update predecessor
        buckets[predecessor].next_bucket = t->bucket_info->last_free;
        buckets[t->bucket_info->last_free] = *bucket;

        bucket->occupied = 1;
        bucket->index = index;
        bucket->next_bucket = END_OF_CHAIN;
    } else { // Collided with the start of a chain, put the new entry in chain position #2
        hdebug("Hit start of a chain\n");
        buckets[t->bucket_info->last_free] = (bucket_t){
            .occupied = 1, .index=index, .next_bucket=bucket->next_bucket};
        bucket->next_bucket = t->bucket_info->last_free;
    }
    hshow(t);
}

static void hashmap_resize_buckets(Table_t *t, uint32_t new_capacity, const TypeInfo_t *type)
{
    if (unlikely(new_capacity > TABLE_MAX_BUCKETS))
        fail("Table has exceeded the maximum table size (2^31) and cannot grow further!");
    hdebug("About to resize from ", t->bucket_info ? (int32_t)t->bucket_info->count : 0, " to ", new_capacity, "\n");
    hshow(t);
    size_t alloc_size = sizeof(bucket_info_t) + sizeof(bucket_t[new_capacity]);
    t->bucket_info = GC_MALLOC_ATOMIC(alloc_size);
    memset(t->bucket_info->buckets, 0, sizeof(bucket_t[new_capacity]));
    t->bucket_info->count = new_capacity;
    t->bucket_info->last_free = new_capacity-1;
    // Rehash:
    for (int64_t i = 0; i < Tableヽlength(*t); i++) {
        hdebug("Rehashing ", i, "\n");
        Tableヽset_bucket(t, GET_ENTRY(*t, i), i, type);
    }

    hshow(t);
    hdebug("Finished resizing\n");
}

// Return address of value
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstack-protector"
#endif
public void *Tableヽreserve(Table_t *t, const void *key, const void *value, const TypeInfo_t *type)
{
    assert(type->tag == TableInfo);
    if (!t || !key) return NULL;
    hshow(t);

    t->hash = 0;

    int64_t key_size = type->TableInfo.key->size,
            value_size = type->TableInfo.value->size;
    if (!t->bucket_info || t->bucket_info->count == 0) {
        hashmap_resize_buckets(t, 8, type);
    } else {
        // Check if we are clobbering a value:
        void *value_home = Tableヽget_raw(*t, key, type);
        if (value_home) { // Update existing slot
            // Ensure that `value_home` is still inside t->entries, even if COW occurs
            ptrdiff_t offset = value_home - t->entries.data;
            maybe_copy_on_write(t, type);
            value_home = t->entries.data + offset;

            if (value && value_size > 0)
                memcpy(value_home, value, (size_t)value_size);

            return value_home;
        }
    }
    // Otherwise add a new entry:

    // Resize buckets if necessary
    if (t->entries.length >= (int64_t)t->bucket_info->count) {
        // Current resize policy: +50% at a time:
        uint32_t newsize = MAX(8, (uint32_t)(3*t->bucket_info->count)/2);
        if (unlikely(newsize > TABLE_MAX_BUCKETS))
            newsize = TABLE_MAX_BUCKETS;
        hashmap_resize_buckets(t, newsize, type);
    }

    if (!value && value_size > 0) {
        for (Table_t *iter = t->fallback; iter; iter = iter->fallback) {
            value = Tableヽget_raw(*iter, key, type);
            if (value) break;
        }
    }

    maybe_copy_on_write(t, type);

    char buf[entry_size(type)];
    memset(buf, 0, sizeof(buf));
    memcpy(buf, key, (size_t)key_size);
    if (value && value_size > 0)
        memcpy(buf + value_offset(type), value, (size_t)value_size);
    else if (value_size > 0)
        memset(buf + value_offset(type), 0, (size_t)value_size);
    Listヽinsert(&t->entries, buf, I(0), (int64_t)entry_size(type));

    int64_t entry_index = t->entries.length-1;
    void *entry = GET_ENTRY(*t, entry_index);
    Tableヽset_bucket(t, entry, entry_index, type);
    return entry + value_offset(type);
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif

public void Tableヽset(Table_t *t, const void *key, const void *value, const TypeInfo_t *type)
{
    assert(type->tag == TableInfo);
    (void)Tableヽreserve(t, key, value, type);
}

public void Tableヽremove(Table_t *t, const void *key, const TypeInfo_t *type)
{
    assert(type->tag == TableInfo);
    if (!t || Tableヽlength(*t) == 0) return;

    // TODO: this work doesn't need to be done if the key is already missing
    maybe_copy_on_write(t, type);

    // If unspecified, pop the last key:
    if (!key)
        key = GET_ENTRY(*t, t->entries.length-1);

    // Steps: look up the bucket for the removed key
    // If missing, then return immediately
    // Swap last key/value into the removed bucket's index1
    // Zero out the last key/value and decrement the count
    // Find the last key/value's bucket and update its index1
    // Look up the bucket for the removed key
    // If bucket is first in chain:
    //    Move bucket->next to bucket's spot
    //    zero out bucket->next's old spot
    //    maybe update lastfree_index1 to second-in-chain's index
    // Else:
    //    set prev->next = bucket->next
    //    zero out bucket
    //    maybe update lastfree_index1 to removed bucket's index

    uint64_t hash = HASH_KEY(*t, key);
    hdebug("Removing key with hash ", hash, "\n");
    bucket_t *bucket, *prev = NULL;
    for (uint64_t i = hash; t->bucket_info->buckets[i].occupied; i = t->bucket_info->buckets[i].next_bucket) {
        if (EQUAL_KEYS(GET_ENTRY(*t, t->bucket_info->buckets[i].index), key)) {
            bucket = &t->bucket_info->buckets[i];
            hdebug("Found key to delete in bucket ", i, "\n");
            goto found_it;
        }
        if (t->bucket_info->buckets[i].next_bucket == END_OF_CHAIN)
            return;
        prev = &t->bucket_info->buckets[i];
    }
    return;

  found_it:;
    assert(bucket->occupied);

    t->hash = 0;

    // Always remove the last entry. If we need to remove some other entry,
    // swap the other entry into the last position and then remove the last
    // entry. This disturbs the ordering of the table, but keeps removal O(1)
    // instead of O(N)
    int64_t last_entry = t->entries.length-1;
    if (bucket->index != last_entry) {
        hdebug("Removing key/value from the middle of the entries list\n");

        // Find the bucket that points to the last entry's index:
        uint64_t i = HASH_KEY(*t, GET_ENTRY(*t, last_entry));
        while (t->bucket_info->buckets[i].index != last_entry)
            i = t->bucket_info->buckets[i].next_bucket;
        // Update the bucket to point to the last entry's new home (the space
        // where the removed entry currently sits):
        t->bucket_info->buckets[i].index = bucket->index;

        // Clobber the entry being removed (in the middle of the list) with
        // the last entry:
        memcpy(GET_ENTRY(*t, bucket->index), GET_ENTRY(*t, last_entry), entry_size(type));
    }

    // Last entry is being removed, so clear it out to be safe:
    memset(GET_ENTRY(*t, last_entry), 0, entry_size(type));

    Listヽremove_at(&t->entries, I(t->entries.length), I(1), (int64_t)entry_size(type));

    int64_t bucket_to_clear;
    if (prev) { // Middle (or end) of a chain
        hdebug("Removing from middle of a chain\n");
        bucket_to_clear = (bucket - t->bucket_info->buckets);
        prev->next_bucket = bucket->next_bucket;
    } else if (bucket->next_bucket != END_OF_CHAIN) { // Start of a chain
        hdebug("Removing from start of a chain\n");
        bucket_to_clear = bucket->next_bucket;
        *bucket = t->bucket_info->buckets[bucket_to_clear];
    } else { // Empty chain
        hdebug("Removing from empty chain\n");
        bucket_to_clear = (bucket - t->bucket_info->buckets);
    }

    t->bucket_info->buckets[bucket_to_clear] = (bucket_t){0};
    if (bucket_to_clear > t->bucket_info->last_free)
        t->bucket_info->last_free = bucket_to_clear;

    hshow(t);
}

CONSTFUNC public void *Tableヽentry(Table_t t, int64_t n)
{
    if (n < 1 || n > Tableヽlength(t))
        return NULL;
    return GET_ENTRY(t, n-1);
}

public void Tableヽclear(Table_t *t)
{
    memset(t, 0, sizeof(Table_t));
}

public Table_t Tableヽsorted(Table_t t, const TypeInfo_t *type)
{
    Closure_t cmp = (Closure_t){.fn=generic_compare, .userdata=(void*)type->TableInfo.key};
    List_t entries = Listヽsorted(t.entries, cmp, (int64_t)entry_size(type));
    return Tableヽfrom_entries(entries, type);
}

PUREFUNC public bool Tableヽequal(const void *vx, const void *vy, const TypeInfo_t *type)
{
    if (vx == vy) return true;
    Table_t *x = (Table_t*)vx, *y = (Table_t*)vy;

    if (x->hash && y->hash && x->hash != y->hash)
        return false;

    assert(type->tag == TableInfo);
    if (x->entries.length != y->entries.length)
        return false;
    
    if ((x->fallback != NULL) != (y->fallback != NULL))
        return false;

    const TypeInfo_t *value_type = type->TableInfo.value;
    size_t offset = value_offset(type);
    for (int64_t i = 0; i < x->entries.length; i++) {
        void *x_key = x->entries.data + i*x->entries.stride;
        void *y_value = Tableヽget_raw(*y, x_key, type);
        if (!y_value) return false;
        void *x_value = x_key + offset;
        if (!generic_equal(y_value, x_value, value_type))
            return false;
    }
    return true;
}

PUREFUNC public int32_t Tableヽcompare(const void *vx, const void *vy, const TypeInfo_t *type)
{
    if (vx == vy) return 0;

    Table_t *x = (Table_t*)vx, *y = (Table_t*)vy;
    assert(type->tag == TableInfo);
    __typeof(type->TableInfo) table = type->TableInfo;

    // Sort empty tables before non-empty tables:
    if (x->entries.length == 0 || y->entries.length == 0)
        return ((x->entries.length > 0) - (y->entries.length > 0));

    // Table comparison rules:
    // - If two tables have different keys, then compare as if comparing a
    // sorted list of the keys of the two tables:
    //    `x.keys.sorted() <> y.keys.sorted()`
    // - Otherwise, compare as if comparing lists of values for the sorted key
    // lists:
    //    `[x[k] for k in x.keys.sorted()] <> [y[k] for k in y.keys.sorted()]`
    // 
    // We can do this in _linear_ time if we find the smallest `k` such that
    // `x[k] != y[k]`, as well as the largest key in `x` and `y`.

    void *mismatched_key = NULL, *max_x_key = NULL;
    for (int64_t i = 0; i < x->entries.length; i++) {
        void *key = x->entries.data + x->entries.stride * i;
        if (max_x_key == NULL || generic_compare(key, max_x_key, table.key) > 0)
            max_x_key = key;

        void *x_value = key + value_offset(type);
        void *y_value = Tableヽget_raw(*y, key, type);

        if (!y_value || (table.value->size > 0 && !generic_equal(x_value, y_value, table.value))) {
            if (mismatched_key == NULL || generic_compare(key, mismatched_key, table.key) < 0) 
                mismatched_key = key;
        }
    }

    // If the keys are not all equal, we gotta check to see if there exists a
    // `y[k]` such that `k` is smaller than all keys that `x` has and `y` doesn't:
    void *max_y_key = NULL;
    for (int64_t i = 0; i < y->entries.length; i++) {
        void *key = y->entries.data + y->entries.stride * i;
        if (max_y_key == NULL || generic_compare(key, max_y_key, table.key) > 0)
            max_y_key = key;

        void *y_value = key + value_offset(type);
        void *x_value = Tableヽget_raw(*x, key, type);
        if (!x_value || !generic_equal(x_value, y_value, table.value)) {
            if (mismatched_key == NULL || generic_compare(key, mismatched_key, table.key) < 0) 
                mismatched_key = key;
        }
    }

    if (mismatched_key) {
        void *x_value = Tableヽget_raw(*x, mismatched_key, type);
        void *y_value = Tableヽget_raw(*y, mismatched_key, type);
        if (x_value && y_value) {
            return generic_compare(x_value, y_value, table.value);
        } else if (y_value) {
            // The smallest mismatched key is in Y, but not X.
            // In this case, we should judge if the key is smaller than *any*
            // key in X or if it's bigger than *every* key in X.
            // Example 1:
            //     x={10, 20, 30} > y={10, 20, 25, 30}
            // The smallest mismatched key is `25`, and we know that `x` is
            // larger than `y` because `30 > 25`.
            // Example 2:
            //     x={10, 20, 30} > y={10, 20, 30, 999}
            // The smallest mismatched key is `999`, and we know that `x` is
            // smaller than `y` because `30 < 999`.
            return max_x_key ? generic_compare(max_x_key, mismatched_key, table.key) : -1;
        } else {
            assert(x_value);
            // The smallest mismatched key is in X, but not Y. The same logic
            // above applies, but reversed.
            return max_y_key ? -generic_compare(max_y_key, mismatched_key, table.key) : 1;
        }
    }

    assert(x->entries.length == y->entries.length);

    // Assuming keys are the same, compare values:
    if (table.value->size > 0) {
        for (int64_t i = 0; i < x->entries.length; i++) {
            void *key = x->entries.data + x->entries.stride * i;
            void *x_value = key + value_offset(type);
            void *y_value = Tableヽget_raw(*y, key, type);
            int32_t diff = generic_compare(x_value, y_value, table.value);
            if (diff != 0) return diff;
        }
    }

    if (!x->fallback != !y->fallback) {
        return (!x->fallback) - (!y->fallback);
    } else if (x->fallback && y->fallback) {
        return generic_compare(x->fallback, y->fallback, type);
    }

    return 0;
}

PUREFUNC public uint64_t Tableヽhash(const void *obj, const TypeInfo_t *type)
{
    assert(type->tag == TableInfo);
    Table_t *t = (Table_t*)obj;
    if (t->hash != 0)
        return t->hash;

    // Table hashes are computed as:
    // hash(t.length, (xor: t.keys), (xor: t.values), t.fallback)
    // Where fallback and default hash to zero if absent
    __typeof(type->TableInfo) table = type->TableInfo;
    uint64_t keys_hash = 0, values_hash = 0;
    size_t offset = value_offset(type);
    if (table.value->size > 0) {
        for (int64_t i = 0; i < t->entries.length; i++) {
            keys_hash ^= generic_hash(t->entries.data + i*t->entries.stride, table.key);
            values_hash ^= generic_hash(t->entries.data + i*t->entries.stride + offset, table.value);
        }
    } else {
        for (int64_t i = 0; i < t->entries.length; i++)
            keys_hash ^= generic_hash(t->entries.data + i*t->entries.stride, table.key);
    }

    volatile struct {
        int64_t length;
        uint64_t keys_hash, values_hash, fallback_hash;
    } components = {
        t->entries.length,
        keys_hash,
        values_hash,
        t->fallback ? Tableヽhash(t->fallback, type) : 0,
    };
    t->hash = siphash24((void*)&components, sizeof(components));
    if unlikely (t->hash == 0)
        t->hash = 1234567;
    return t->hash;
}

public Text_t Tableヽas_text(const void *obj, bool colorize, const TypeInfo_t *type)
{
    Table_t *t = (Table_t*)obj;
    assert(type->tag == TableInfo);
    __typeof(type->TableInfo) table = type->TableInfo;

    if (!t) {
        if (table.value != &Voidヽinfo) 
            return Textヽconcat(
                Text("{"),
                generic_as_text(NULL, false, table.key),
                Text("="),
                generic_as_text(NULL, false, table.value),
                Text("}"));
        else
            return Textヽconcat(
                Text("|"),
                generic_as_text(NULL, false, table.key),
                Text("|"));
    }

    int64_t val_off = (int64_t)value_offset(type);
    Text_t text = table.value == &Voidヽinfo ? Text("|") : Text("{");
    for (int64_t i = 0, length = Tableヽlength(*t); i < length; i++) {
        if (i > 0)
            text = Textヽconcat(text, Text(", "));
        void *entry = GET_ENTRY(*t, i);
        text = Textヽconcat(text, generic_as_text(entry, colorize, table.key));
        if (table.value != &Voidヽinfo) 
            text = Textヽconcat(text, Text("="), generic_as_text(entry + val_off, colorize, table.value));
    }

    if (t->fallback) {
        text = Textヽconcat(text, Text("; fallback="), Tableヽas_text(t->fallback, colorize, type));
    }

    text = Textヽconcat(text, table.value == &Voidヽinfo ? Text("|") : Text("}"));
    return text;
}

public Table_t Tableヽfrom_entries(List_t entries, const TypeInfo_t *type)
{
    assert(type->tag == TableInfo);
    if (entries.length == 0)
        return (Table_t){};

    Table_t t = {};
    int64_t length = entries.length + entries.length / 4;
    size_t alloc_size = sizeof(bucket_info_t) + sizeof(bucket_t[length]);
    t.bucket_info = GC_MALLOC_ATOMIC(alloc_size);
    memset(t.bucket_info->buckets, 0, sizeof(bucket_t[length]));
    t.bucket_info->count = length;
    t.bucket_info->last_free = length-1;

    size_t offset = value_offset(type);
    for (int64_t i = 0; i < entries.length; i++) {
        void *key = entries.data + i*entries.stride;
        Tableヽset(&t, key, key + offset, type);
    }
    return t;
}

// Overlap is "set intersection" in formal terms
public Table_t Tableヽoverlap(Table_t a, Table_t b, const TypeInfo_t *type)
{
    // Return a table such that t[k]==a[k] for all k such that a.has(k), b.has(k), and a[k]==b[k]
    Table_t result = {};
    const size_t offset = value_offset(type);
    for (Table_t *t = &a; t; t = t->fallback) {
        for (int64_t i = 0; i < Tableヽlength(*t); i++) {
            void *key = GET_ENTRY(*t, i);
            void *a_value = key + offset;
            void *b_value = Tableヽget(b, key, type);
            if (b_value && generic_equal(a_value, b_value, type->TableInfo.value))
                Tableヽset(&result, key, a_value, type);
        }
    }
    return result;
}

// With is "set union" in formal terms
public Table_t Tableヽwith(Table_t a, Table_t b, const TypeInfo_t *type)
{
    // return a table such that t[k]==b[k] for all k such that b.has(k), and t[k]==a[k] for all k such that a.has(k) and not b.has(k)
    Table_t result = {};
    const size_t offset = value_offset(type);
    for (Table_t *t = &a; t; t = t->fallback) {
        for (int64_t i = 0; i < Tableヽlength(*t); i++) {
            void *key = GET_ENTRY(*t, i);
            Tableヽset(&result, key, key + offset, type);
        }
    }
    for (Table_t *t = &b; t; t = t->fallback) {
        for (int64_t i = 0; i < Tableヽlength(*t); i++) {
            void *key = GET_ENTRY(*t, i);
            Tableヽset(&result, key, key + offset, type);
        }
    }
    return result;
}

// Xor is "disjunctive union" or "symmetric difference" in formal terms
public Table_t Tableヽxor(Table_t a, Table_t b, const TypeInfo_t *type)
{
    // return a table with elements in `a` or `b`, but not both
    Table_t result = {};
    const size_t offset = value_offset(type);
    for (Table_t *t = &a; t; t = t->fallback) {
        for (int64_t i = 0; i < Tableヽlength(*t); i++) {
            void *key = GET_ENTRY(*t, i);
            if (Tableヽget(b, key, type) == NULL)
                Tableヽset(&result, key, key + offset, type);
        }
    }
    for (Table_t *t = &b; t; t = t->fallback) {
        for (int64_t i = 0; i < Tableヽlength(*t); i++) {
            void *key = GET_ENTRY(*t, i);
            if (Tableヽget(a, key, type) == NULL)
                Tableヽset(&result, key, key + offset, type);
        }
    }
    return result;
}

// Without is "set difference" in formal terms
public Table_t Tableヽwithout(Table_t a, Table_t b, const TypeInfo_t *type)
{
    // Return a table such that t[k]==a[k] for all k such that not b.has(k) or b[k] != a[k]
    Table_t result = {};
    const size_t offset = value_offset(type);
    for (Table_t *t = &a; t; t = t->fallback) {
        for (int64_t i = 0; i < Tableヽlength(*t); i++) {
            void *key = GET_ENTRY(*t, i);
            void *a_value = key + offset;
            void *b_value = Tableヽget(b, key, type);
            if (!b_value || !generic_equal(a_value, b_value, type->TableInfo.value))
                Tableヽset(&result, key, a_value, type);
        }
    }
    return result;
}

public Table_t Tableヽwith_fallback(Table_t t, OptionalTable_t fallback)
{
    if (fallback.entries.length <= 0) {
        t.fallback = NULL;
        return t;
    } else {
        TABLE_INCREF(fallback);
        t.fallback = GC_MALLOC(sizeof(Table_t));
        *t.fallback = fallback;
        return t;
    }
}

PUREFUNC public bool Tableヽis_subset_of(Table_t a, Table_t b, bool strict, const TypeInfo_t *type)
{
    if (a.entries.length > b.entries.length || (strict && a.entries.length == b.entries.length))
        return false;

    for (int64_t i = 0; i < Tableヽlength(a); i++) {
        void *found = Tableヽget_raw(b, GET_ENTRY(a, i), type);
        if (!found) return false;
    }
    return true;
}

PUREFUNC public bool Tableヽis_superset_of(Table_t a, Table_t b, bool strict, const TypeInfo_t *type)
{
    return Tableヽis_subset_of(b, a, strict, type);
}

PUREFUNC public void *Tableヽstr_get(Table_t t, const char *key)
{
    void **ret = Tableヽget(t, &key, &CStrToVoidStarTable);
    return ret ? *ret : NULL;
}

PUREFUNC public void *Tableヽstr_get_raw(Table_t t, const char *key)
{
    void **ret = Tableヽget_raw(t, &key, &CStrToVoidStarTable);
    return ret ? *ret : NULL;
}

public void *Tableヽstr_reserve(Table_t *t, const char *key, const void *value)
{
    return Tableヽreserve(t, &key, &value, &CStrToVoidStarTable);
}

public void Tableヽstr_set(Table_t *t, const char *key, const void *value)
{
    Tableヽset(t, &key, &value, &CStrToVoidStarTable);
}

public void Tableヽstr_remove(Table_t *t, const char *key)
{
    return Tableヽremove(t, &key, &CStrToVoidStarTable);
}

CONSTFUNC public void *Tableヽstr_entry(Table_t t, int64_t n)
{
    return Tableヽentry(t, n);
}

PUREFUNC public bool Tableヽis_none(const void *obj, const TypeInfo_t *info)
{
    (void)info;
    return ((Table_t*)obj)->entries.length < 0;
}

public void Tableヽserialize(const void *obj, FILE *out, Table_t *pointers, const TypeInfo_t *type)
{
    Table_t *t = (Table_t*)obj;
    int64_t len = t->entries.length;
    Int64ヽserialize(&len, out, pointers, &Int64ヽinfo);

    size_t offset = value_offset(type);
    for (int64_t i = 0; i < len; i++) {
        _serialize(t->entries.data + i*t->entries.stride, out, pointers, type->TableInfo.key);
        if (type->TableInfo.value->size > 0)
            _serialize(t->entries.data + i*t->entries.stride + offset, out, pointers, type->TableInfo.value);
    }

    assert(fputc(t->fallback != NULL ? 1 : 0, out) != EOF);
    if (t->fallback)
        Tableヽserialize(t->fallback, out, pointers, type);
}

#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstack-protector"
#endif
public void Tableヽdeserialize(FILE *in, void *outval, List_t *pointers, const TypeInfo_t *type)
{
    int64_t len;
    Int64ヽdeserialize(in, &len, pointers, &Intヽinfo);

    Table_t t = {};
    for (int64_t i = 0; i < len; i++) {
        char key[type->TableInfo.key->size];
        _deserialize(in, key, pointers, type->TableInfo.key);
        char value[type->TableInfo.value->size];
        if (type->TableInfo.value->size > 0)
            _deserialize(in, value, pointers, type->TableInfo.value);
        Tableヽset(&t, key, value, type);
    }

    if (fgetc(in) != 0) {
        t.fallback = GC_MALLOC(sizeof(Table_t));
        Tableヽdeserialize(in, t.fallback, pointers, type);
    }

    *(Table_t*)outval = t;
}
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif

// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1