summaryrefslogtreecommitdiff
path: root/src/util/Table.c
blob: 0391457e18fa11ac642754e7339fb85e4fcdc7d3 (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
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
/* Table.c -- implementation for key-value tables */
/* $Header$ */

/*@@
 @file          Table.c
 @seeheader     util_Table.h
 @version       $Header$
 @date          Wed Oct 31 16:17:45 MET 2001
 @author        Jonathan Thornburg <jthorn@aei.mpg.de>
 @desc
        This program implements the key-value table API defined in util_Table.h
        This is documented in
           http://www.cactuscode.org/Development/Specs/KeyValueLookup.txt
        FIXME: this is a bit out of date :(
        FIXME: should write some latex docs
 @enddesc
 @@*/

/*
 * ***** table of contents for this file *****
 *
 * Growable Array Data Structures
 * Table Data Structures
 * Iterator Data Structures
 * Misc Macros for This File
 * Prototypes for Functions Private to This File
 * Main Table API
 *   Util_TableCreate
 *   Util_TableDestroy
 *   Util_TableQueryFlags
 *   Util_TableQueryNKeys
 *   Util_TableQueryMaxKeyLength
 *   Util_TableQueryValueInfo
 *   Util_TableDeleteKey
 *   Util_TableCreateFromString
 *   Util_TableSetString
 *   Util_TableGetString
 *   Util_TableSet*
 *   Util_TableSet*Array
 *   Util_TableGet*
 *   Util_TableGet*Array
 * Table Iterator API
 *   Util_TableItCreate
 *   Util_TableItDestroy
 *   Util_TableItQueryIsNull
 *   Util_TableItQueryIsNonNull
 *   Util_TableItQueryTableHandle
 *   Util_TableItQueryKeyValueInfo
 *   Util_TableItAdvance
 *   Util_TableItResetToStart
 *   Util_TableItSetToNull
 *   Util_TableItSetToKey
 * Internal Support Functions
 *   internal_set
 *   internal_get
 *   get_table_header_ptr
 *   delete_key
 *   free_table_entry
 *   bad_key
 *   find_table_entry
 *   get_iterator_ptr
 *   grow_pointer_array
#ifdef UTIL_TABLE_TEST
 * Table and Iterator Dump Routines
 *   print_all_tables
 *   print_table
 *   print_all_iterators
 * Standalone Test Driver
 *   TEST_SET_GET_{INT,REAL,COMPLEX}
 *   TEST_SET_GET_{INT,REAL,COMPLEX}_ARRAY
 *   main
 *   test_nonexistent_tables
 *   test_table_create_destroy
 *   test_set_get
 *   test_set_get_array
 *   test_iterators
 *   test_delete_key
 *   test_create_from_string
 *   test_set_get_string
#endif
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

/* FIXME: C99 defines <stdbool.h>, we should include that or a fake version */
typedef enum { false = 0, true = 1 } bool;

#ifndef CCODE
#define CCODE   /* signal Cactus header files that we're C, not Fortran */
#endif

#include "cctk_Types.h"
#include "cctk_Constants.h"
#include "cctk_Groups.h"
#include "cctk_Flesh.h"

#include "util_ErrorCodes.h"
#include "util_String.h"
#include "util_Table.h"

#ifdef UTIL_TABLE_TEST
  #include "cctk_Version.h"
#endif

#ifdef UTIL_TABLE_TEST
/* we build a standalone test driver */
#endif

#ifdef UTIL_TABLE_DEBUG
/* we print various debugging information */
#endif

#ifdef UTIL_TABLE_DEBUG2
  /* we print very verbose debugging information */
  #define UTIL_TABLE_DEBUG
#endif

#ifndef UTIL_TABLE_TEST
  static const char *rcsid = "$Header$";
  CCTK_FILEVERSION(util_Table_c)
#endif

/******************************************************************************/
/***** Growable Array Data Structures *****************************************/
/******************************************************************************/

/*
 * We use "growable arrays" to keep track of all tables and all table
 * iterators.  In both cases we use the same data structure:
 *
 *      int N_objects;          // actual number of tables/iterators
 *      int N_array;            // actual size of growable array
 *      void *array;            // pointer to malloc-allocated growable array
 *                              // indexed by handle/ihandle
 *
 * Note that the pointer must be  void *  so we can use the  grow_array()
 * function; this pointer should be cast into an actual usable type for
 * normal uses.  Null pointers in the array mark unused array elements.
 */

/*
 * growth policy for growable arrays
 * sequence is
#ifdef UTIL_TABLE_TEST
 *      0, 1, 3, 7, 15, ... entries     (very slow growth
 *                                       ==> better exercise growing code)
#else
 *      0, 10, 30, 70, 150, ... entries
#endif
 * n.b. this grows >= a geometric series
 *      ==> total time in realloc is linear in max array size
 *      (if we just grew in an arithmetic progression then the total
 *       time in realloc() would be quadratic in the max array size)
 */
#ifdef UTIL_TABLE_TEST
  #define GROW(old_n)   (2*(old_n) + 1)
#else
  #define GROW(old_n)   (2*(old_n) + 10)
#endif

/******************************************************************************/
/***** Table Data Structures **************************************************/
/******************************************************************************/

/*
 * The present implementation represents a table as a singly-linked
 * list of table entries.  The code is generally programmed for simplicity,
 * not for maximum performance: linear searches are used everywhere.
 * In practice, we don't expect tables to have very many entries, so
 * this shouldn't be a problem.
 */

struct  table_entry
        {
        struct table_entry *next;
        char *key;
        int type_code;
        int N_elements;
        void *value;
        };

struct  table_header
        {
        struct table_entry *head;
        int flags;
        int handle;
        };

/*
 * We keep track of all tables with the following variables
 * (all are static ==> private to this file)
 */

/* number of tables */
static int N_tables = 0;

/* number of elements in the following array */
static int N_thp_array = 0;

/*
 * pointer to growable array of pointers to table headers,
 *            indexed by table handle,
 * with unused array elements set to NULL pointers
 * ... name abbreviates "table-header-pointer array"
 */
void **thp_array = NULL;

/*
 * handle of table with auto-destroy flag set 
 * (there can be at most one such table at any time),
 * or AUTO_DESTROY_NONE if there is no such table
 */
#define AUTO_DESTROY_NONE       (-1)
static int auto_destroy_handle = AUTO_DESTROY_NONE;

/******************************************************************************/
/***** Iterator Data Structures ***********************************************/
/******************************************************************************/

/*
 * This structure represents a table interator.
 *
 * Note that we never modify the table through an iterator,
 * so all the pointers here are to const objects
 */
struct  iterator
        {
        const struct table_header *thp; /* must always be non-NULL */
        const struct table_entry *tep;  /* NULL for iterator in */
                                        /* "null-pointer" state */
        };

/*
 * We keep track of all iterators with the following variables
 * (all are static ==> private to this file)
 */

/* number of iterators */
static int N_iterators = 0;

/* number of elements in the following array */
static int N_ip_array = 0;

/*
 * pointer to growable array of pointers to iterators,
 *            indexed by iterator handle,
 * with unused array elements set to NULL pointers
 * ... name abbreviates "iterator-pointer array"
 */
void **ip_array = NULL;

/******************************************************************************/
/***** Misc Macros for This File **********************************************/
/******************************************************************************/

#define then    /* empty */

#define min(x,y)        ((x < y) ? (x) : (y))
#define max(x,y)        ((x > y) ? (x) : (y))

/******************************************************************************/
/***** Prototypes for Functions Private to This File **************************/
/******************************************************************************/

/*
 * This is the internal function implementing all the
 *      Util_TableSet*()
 *      Util_TableSet*Array()
 * functions.  It returns their desired return value, i.e.
 *      1 for key was already in table before this call
 *        (old value was replaced)
 *        (it doesn't matter what the old value's type_code and
 *         N_elements were, i.e. these do *not* have to match the
 *         new value),
 *      0 for key was not in table before this call,
 *      UTIL_ERROR_BAD_HANDLE           handle is invalid
 *      UTIL_ERROR_TABLE_BAD_KEY        key contains '/' character
 *      UTIL_ERROR_BAD_INPUT            N_elements < 0
 *      UTIL_ERROR_NO_MEMORY            unable to allocate memory
 */
static
  int internal_set(int handle,
                   int type_code, int N_elements, const void *value,
                   const char *key);

/*
 * This is the internal function implementing all the
 *      Util_TableGet*()
 *      Util_TableGet*Array()
 * functions.  It returns their desired return value, i.e.
 *      number of values stored in  array[]  if ok,
 *      -ve for error, including
 *      UTIL_ERROR_BAD_HANDLE           handle is invalid
 *      UTIL_ERROR_TABLE_BAD_KEY        key contains '/' character
 *      UTIL_ERROR_BAD_INPUT            array != NULL and N_elements < 0
 *      UTIL_ERROR_TABLE_NO_SUCH_KEY    no such key in table
 *      UTIL_ERROR_TABLE_WRONG_DATA_TYPE value has wrong data type
 */
static
  int internal_get(int handle,
                   int type_code, int N_value_buffer, void *value_buffer,
                   const char *key);

/* check table handle for validity, return pointer to table header */
static
  struct table_header *get_table_header_ptr(int handle);

/*
 * delete a key from a table
 * return same as Util_TableDeleteKey(), i.e.
 *      0 for ok (key existed before this call, and has now been deleted)
 *      -ve for error, including
 *      UTIL_ERROR_TABLE_NO_SUCH_KEY    no such key in table
 */
static
  int delete_key(struct table_header *thp, const char *key);

/* free table entry and key/value it points to */
static
  void free_table_entry(struct table_entry *tep);

/*
 * check if key is syntactically "bad" (eg contains '/' character)
 * returns true for bad key, false for ok
 */
static
  bool bad_key(const char *key);

/*
 * find table entry for a given key
 * return pointer to it, or NULL if no such key is present in table
 * if  prev_tep_ptr != NULL,
 *    then also set *prev_tep_ptr to point to table entry one *before*
 *         the one with the given key, or to NULL if the given key is
 *         the starting entry in the table
 */
static
  struct table_entry *find_table_entry
          (const struct table_header *thp, const char *key,
           struct table_entry **prev_tep_ptr);

/* check iterator handle for validity, return pointer to iterator */
static
  struct iterator *get_iterator_ptr(int ihandle);

/*
 * This function grows an malloc-allocated array of  void *  pointers
 * via realloc(), initializing the new space to NULL pointers.
 *
 * Arguments:
 * *pN = (in out) array size
 * *pvp_array = (in out) Pointer to growable array of  void *  pointers.
 *
 * Results:
 * This function returns
 *      0 for ok,
 *      -ve for error, including
 *      UTIL_ERROR_NO_MEMORY            can't allocate memory to grow table
 */
static
  int grow_pointer_array(int *pN, void ***pvp_array);

#ifdef UTIL_TABLE_TEST
/*
 * Print out the table and iterator data structures.
 */
static void print_all_tables(void);
static void print_table(int handle);
static void print_all_iterators(void);
#endif

#ifdef UTIL_TABLE_TEST
/*
 * test drivers
 */
static void test_nonexistent_tables(void);
static void test_table_create_destroy(void);
static void test_set_get(int handle, bool case_insensitive);
static void test_set_get_array(int handle);
static void test_iterators(int handle);
static void test_delete_key(int handle, bool case_insensitive);
static int test_create_from_string(void);
static void test_set_get_string(int handle, bool case_insensitive);
#endif

/******************************************************************************/
/***** Main Table API *********************************************************/
/******************************************************************************/

/*@@
  @routine      Util_TableCreate
  @desc         This function creates a new (empty) table.

  @var          flags
  @vtype        int
  @vdesc        inclusive-or of UTIL_TABLE_FLAGS_* bit flags, must be >= 0
                (n.b. for Fortran users: inclusive-or is the same as sum here,
                since the bit masks are all disjoint)
  @endvar

  @comment      We require flags >= 0 so other functions can distinguish
                flags from (negative) error codes
  @endcomment

  @returntype   int
  @returndesc   a handle to the newly-created table,
                -ve for error, including
                UTIL_ERROR_NO_MEMORY            unable to allocate memory
                UTIL_ERROR_TABLE_BAD_FLAGS      flags < 0

  @endreturndesc
  @@*/
int Util_TableCreate(int flags)
{
/* destroy any table noted for auto-destroy */
if (auto_destroy_handle >= 0)
   then {
        #ifdef UTIL_TABLE_DEBUG
        printf("auto-destroying auto_destroy_handle=%d\n", auto_destroy_handle);
        #endif
        Util_TableDestroy(auto_destroy_handle);
        auto_destroy_handle = AUTO_DESTROY_NONE;
        }

#ifdef UTIL_TABLE_DEBUG
printf("Util_TableCreate()\n");
#endif

if (flags < 0)
   then return UTIL_ERROR_TABLE_BAD_FLAGS;

if (N_tables == N_thp_array)
   then {
        /* grow  thp_array  to get some room to create the new table */
        #ifdef UTIL_TABLE_DEBUG
        printf("   growing thp_array[] from old size %d\n", N_thp_array);
        #endif
        if (grow_pointer_array(&N_thp_array, &thp_array) < 0)
           then return UTIL_ERROR_NO_MEMORY;
        #ifdef UTIL_TABLE_DEBUG
        printf("                         to new size %d\n", N_thp_array);
        #endif
        }

/* we should now have space to create the new table */
assert(N_tables < N_thp_array);

/* find an unused handle */
#ifdef UTIL_TABLE_DEBUG
printf("   searching for an unused handle (N_tables=%d N_thp_array=%d)\n",
       N_tables, N_thp_array);
#endif
  {
int handle;
        for (handle = 0 ; handle < N_thp_array ; ++handle)
        {
        #ifdef UTIL_TABLE_DEBUG2
        printf("      checking handle=%d\n", handle);
        #endif
        if (thp_array[handle] == NULL)
           then {
                /* we've found an unused handle ==> create the table */
                struct table_header *const thp
                        = (struct table_header *)
                          malloc(sizeof(struct table_header));
                if (thp == NULL)
                   then return UTIL_ERROR_NO_MEMORY;

                #ifdef UTIL_TABLE_DEBUG
                printf("   using handle=%d\n", handle);
                #endif

                thp->head = NULL;
                thp->flags = flags;
                thp->handle = handle;

                ++N_tables;
                thp_array[handle] = (void *) thp;

                /* mark this table for auto-destroy? */
                if (flags & UTIL_TABLE_FLAGS_AUTO_DESTROY)
                   then auto_destroy_handle = handle;

                return handle;
                }
        }

/* we should never get to here! */
assert(false);
abort();                                /* internal error (core dump) */
/* prevent compiler warning 'function should return a value' */
return(0);
  }
}

/******************************************************************************/

/*@@
  @routine      Util_TableDestroy
  @desc         This function destroys a table.

                (Of course, this invalidates any iterators for this table.)

  @var          handle
  @vtype        int
  @vdesc        handle to the table
  @endvar

  @returntype   int
  @returndesc   0 for ok,
                -ve for error, including
                UTIL_ERROR_BAD_HANDLE           handle is invalid
  @endreturndesc
  @@*/
int Util_TableDestroy(int handle)
{
struct table_header *const thp = get_table_header_ptr(handle);
if (thp == NULL)
   then return UTIL_ERROR_BAD_HANDLE;

#ifdef UTIL_TABLE_DEBUG
printf("Util_TableDestroy(handle=%d)\n", handle);
#endif

/*
 * If this table is noted for auto-destroy, that note was specific
 * to the table we're about to destroy, so cancel the note.  (I.e.
 * if a new table gets created later with this same handle, we don't
 * want to keep the old auto-destroy note referring to it!)
 */
if (auto_destroy_handle == handle)
   then auto_destroy_handle = AUTO_DESTROY_NONE;

/* delete all the keys */
  {
struct table_entry *tep, *next_tep;
        for (tep = thp->head ; tep != NULL ; tep = next_tep)
        {
        #ifdef UTIL_TABLE_DEBUG2
        printf("   deleting key \"%s\"\n", tep->key);
        #endif
        next_tep = tep->next;
        free_table_entry(tep);
        }

--N_tables;
thp_array[handle] = NULL;
free(thp);

return 0;
  }
}

/******************************************************************************/

/*@@
  @routine      Util_TableQueryFlags
  @desc         This function queries a table's flags word.

  @var          handle
  @vtype        int
  @vdesc        handle to the table
  @endvar

  @returntype   int
  @returndesc   flags if table exists,
                -ve for error, including
                UTIL_ERROR_BAD_HANDLE           handle is invalid
  @endreturndesc
  @@*/
int Util_TableQueryFlags(int handle)
{
const struct table_header *const thp = get_table_header_ptr(handle);
if (thp == NULL)
   then return UTIL_ERROR_BAD_HANDLE;

return thp->flags;
}

/******************************************************************************/

/*@@
  @routine      Util_TableQueryNKeys
  @desc         This function queries the total number of key/value entries
                in a table.

  @var          handle
  @vtype        int
  @vdesc        handle to the table
  @endvar

  @returntype   int
  @returndesc   number of entries (>= 0),
                -ve for error, including
                UTIL_ERROR_BAD_HANDLE           handle is invalid
  @endreturndesc
  @@*/
int Util_TableQueryNKeys(int handle)
{
const struct table_header *const thp = get_table_header_ptr(handle);
if (thp == NULL)
   then return UTIL_ERROR_BAD_HANDLE;

  {
int N = 0;
const struct table_entry *tep;
        for (tep = thp->head ; tep != NULL ; tep = tep->next)
        {
        ++N;
        }
return N;
  }
}

/******************************************************************************/

/*@@
  @routine      Util_TableQueryMaxKeyLength
  @desc         This function queries the maximum key length in a table.

  @var          handle
  @vtype        int
  @vdesc        handle to the table
  @endvar

  @returntype   int
  @returndesc   maximum key length (>= 0),
                -ve for error, including
                UTIL_ERROR_BAD_HANDLE           handle is invalid
  @endreturndesc
  @@*/
int Util_TableQueryMaxKeyLength(int handle)
{
const struct table_header *const thp = get_table_header_ptr(handle);
if (thp == NULL)
   then return UTIL_ERROR_BAD_HANDLE;

  {
int max_length = 0;
const struct table_entry *tep;
        for (tep = thp->head ; tep != NULL ; tep = tep->next)
        {
        const int length = strlen(tep->key);
        if (length > max_length)
           then max_length = length;
        }
return max_length;
  }
}

/******************************************************************************/

/*@@
  @routine      Util_TableQueryValueInfo
  @desc         This function queries the type and number of elements
                of the value corresponding to a specified key in a table.
                It can also be used to "just" determine whether or not
                a specified key is present in a table.

  @var          handle
  @vtype        int
  @vdesc        handle to the table
  @endvar

  @var          type_code
  @vtype        int *
  @vdesc        pointer to where this function should store
                the value's type code
                (one of the CCTK_VARIABLE_* constants from "cctk_Types.h"),
                or NULL pointer to skip storing this
  @endvar

  @var          N_elements
  @vtype        int *
  @vdesc        pointer to where this function should store
                the number of array elements in the value,
                or NULL pointer to skip storing this
  @endvar

  @var          key
  @vtype        const char *
  @vdesc        pointer to the key (a C-style null-terminated string)
  @endvar

  @returntype   int
  @returndesc   1 for key is in table,
                0 for no such key in table
                  (in this case nothing is stored in *type and *N_elements)
                -ve for error, including
                UTIL_ERROR_BAD_HANDLE           handle is invalid
                UTIL_ERROR_TABLE_BAD_KEY        key contains '/' character
  @comment      Unlike all the other query functions, this function
                returns 0 for no such key in table.  The rationale
                for this design is that by passing NULL pointers for
                type_code and N_elements, this function is then a
                Boolean "is key in table?" predicate.
  @endreturndesc
  @@*/
int Util_TableQueryValueInfo(int handle,
                             CCTK_INT *type_code, CCTK_INT *N_elements,
                             const char *key)
{
const struct table_header *const thp = get_table_header_ptr(handle);
if (thp == NULL)
   then return UTIL_ERROR_BAD_HANDLE;

if (bad_key(key))
   then return UTIL_ERROR_TABLE_BAD_KEY;
  {
const struct table_entry *const tep = find_table_entry(thp, key, NULL);
if (tep == NULL)
   then return 0;                       /* no such key in table */

if (type_code != NULL)
   then *type_code = tep->type_code;
if (N_elements != NULL)
   then *N_elements = tep->N_elements;
return 1;                               /* key is in table */
  }
}

/******************************************************************************/

/*@@
  @routine      Util_TableDeleteKey
  @desc         This function deletes a key (and the corresponding value)
                from a table.

                Note that this invalidates any iterators for this table.

  @var          handle
  @vtype        int
  @vdesc        handle to the table
  @endvar

  @var          key
  @vtype        const char *
  @vdesc        pointer to the key (a C-style null-terminated string)
  @endvar

  @returntype   int
  @returndesc   0 for ok (key existed before this call,
                          and has now been deleted)
                -ve for error, including
                UTIL_ERROR_BAD_HANDLE           handle is invalid
                UTIL_ERROR_TABLE_BAD_KEY        key contains '/' character
                UTIL_ERROR_TABLE_NO_SUCH_KEY    no such key in table
  @endreturndesc
  @@*/
int Util_TableDeleteKey(int handle, const char *key)
{
struct table_header *const thp = get_table_header_ptr(handle);
if (thp == NULL)
   then return UTIL_ERROR_BAD_HANDLE;

if (bad_key(key))
   then return UTIL_ERROR_TABLE_BAD_KEY;

return delete_key(thp, key);
}

/******************************************************************************/

/*@@
  @routine      Util_TableCreateFromString
  @desc         This function creates a new table based on a string
                argument, which is interpreted with "parameter-file"
                semantics.  The table has the case-insensitive and
                auto-destroy flags set.

  @comment      Implementation Restriction:
                The present implementation only recognises integer or
                real values (not complex or character), and only scalars
                (not arrays).  In more detail, the strings recognized
                are defined by the following BNF:
                        string -> assign*
                        assign -> whitespace* key = value ;? whitespace*
                        value  -> int_value | real_value
                        int_value -> contains only chars from int_chars,
                                     and is recognized as valid by sscanf()
                                     with a "%d" format
                        real_value -> contains one or more chars not in
                                      int_chars, and is recognized as
                                      valid by sscanf() with a "%lf" format
                where
                int_chars is the constant string defined in the code below,
                * denotes 0 or more repetitions, and
                ? denotes optional items, i.e. 0 or 1 repetitions.

                Notice that whitespace separates "key=value" assignments,
                and thus that no whitespace may appear with a "key=value"
                assignment.
  @endcomment

  @var          string
  @vtype        const char *
  @vdesc        C-style null-terminated string specifying table contents;
                string has parameter-file semantics
  @endvar

  @returntype   int
  @returndesc   a handle to the newly-created table,
                -ve for error, including
                UTIL_ERROR_NO_MEMORY    unable to allocate memory
                UTIL_ERROR_BAD_INPUT    invalid input: can't parse input string
                and any error codes returned by
                Util_TableCreate(), Util_TableSetInt(), or Util_TableSetReal()
  @endreturndesc
  @@*/
int Util_TableCreateFromString(const char string[])
{
const char *const delimiters = "; \t\n";
const char *const whitespace =  " \t\n";
const char *const int_chars  = "-+0123456789";

const int handle = Util_TableCreate(  UTIL_TABLE_FLAGS_CASE_INSENSITIVE
                                    | UTIL_TABLE_FLAGS_AUTO_DESTROY    );
if (handle < 0)
   then return handle;                  /* error creating table */

#ifdef UTIL_TABLE_DEBUG
printf("Util_TableCreateFromString(\"%s\")\n", string);
#endif

/* make a copy of the string so we can write null characters into it */
/* to partition it into substrings */
  {
char *const buffer = Util_Strdup(string);
if (buffer == NULL)
   then {
        Util_TableDestroy(handle);
        return UTIL_ERROR_NO_MEMORY;
        }

  {
char *p = buffer;
        while (*p != '\0')
        {
        /*
         * each pass through this loop processes a single key=value
         * assignment starting at p, creating a table entry for it
         */

        /* skip over any leading whitespace */
        const size_t N_white = strspn(p, whitespace);
        p += N_white;
        #ifdef UTIL_TABLE_DEBUG2
        printf("   skipped over delimiters to p-buffer=%d\n", (int) (p-buffer));
        #endif

          {
        const char *const key = p;      /* key -> "key=value..." */
        char *q = strchr(p, '=');
        if (q == NULL)
           then {
                free(buffer);
                Util_TableDestroy(handle);
                return UTIL_ERROR_BAD_INPUT;
                                        /* no '=" in "key=value" string */
                }
        *q++ = '\0';                    /* key -> "key", q -> "value..." */
          {
        char *const value = q;          /* value -> "value..." */
        const size_t value_length = strcspn(value, delimiters);
        q = value + value_length;       /* q -> delimiter */
        if (*q != '\0')         /* if we're already at the end of the */
                                /* buffer, we don't want to advance further */
           then *q++ = '\0';            /* value -> "value", q -> "..." */
        #ifdef UTIL_TABLE_DEBUG
        printf("   at p-buffer=%d, got key=\"%s\" value=\"%s\"\n",
               (int) (p-buffer), key, value);
        #endif

        if (strspn(value, int_chars) == value_length)
           then {
                /* value is made up solely of chars which can appear */ 
                /* in an integer ==> assume value is an integer */
                int value_int;
                if (sscanf(value, "%d", &value_int) != 1)
                   then {
                        free(buffer);
                        Util_TableDestroy(handle);
                        return UTIL_ERROR_BAD_INPUT;
                                        /* can't parse integer value */
                        }
                #ifdef UTIL_TABLE_DEBUG2
                printf("   ==> storing key=\"%s\", value_int=%d\n",
                       key, value_int);
                #endif
                  {
                const int status = Util_TableSetInt(handle, value_int, key);
                if (status < 0)
                   then {
                        free(buffer);
                        Util_TableDestroy(handle);
                        return status;  /* error setting key=integer in table */
                        }
                  }
                }
           else {
                /* value contains at least one character which can't */
                /* appear in an integer ==> assume value is a real */
                double value_double;
                if (sscanf(value, "%lf", &value_double) != 1)
                   then {
                        free(buffer);
                        Util_TableDestroy(handle);
                        return UTIL_ERROR_BAD_INPUT;
                                        /* can't parse real value */
                        }
                #ifdef UTIL_TABLE_DEBUG2
                printf("   ==> storing key=\"%s\", value_double=%g\n",
                       key, value_double);
                #endif
                  {
                const int status = Util_TableSetReal(handle, value_double, key);
                if (status < 0)
                   then {
                        free(buffer);
                        Util_TableDestroy(handle);
                        return status;  /* error setting key=real in table */
                        }
                  }
                }

        p = q;
        #ifdef UTIL_TABLE_DEBUG2
        printf("   after key=value, advanced p to p-buffer=%d\n",
               (int) (p-buffer));
        #endif
          }
          }
        }

free(buffer);
return handle;
  }
  }
}

/******************************************************************************/

/*@@
  @routine      Util_TableSetString
  @desc         This function sets the value associated with a specified
                key to be (a copy of) a specified character string.

                Note that this invalidates any iterators for this table.

  @comment      This function stores the value as array of strlen(string)
                CCTK_CHARs; the stored value does *not* include a terminating
                null character.  (This is convenient for Fortran.)
  @endcomment

  @comment      The implementation assumes (as is presently the case)
                that a string is in fact an array of CCTK_CHAR, i.e.
                that CCTK_CHAR is the same type as (or at least
                compatible with) char.
  @endcomment

  @var          handle
  @vtype        int
  @vdesc        handle to the table
  @endvar

  @var          string
  @vtype        const char *
  @vdesc        pointer to the (C-style null-terminated) string
  @endvar

  @var          key
  @vtype        const char *
  @vdesc        pointer to the key (a C-style null-terminated string)
  @endvar

  @returntype   int
  @returndesc   Same as all the other  Util_TableSet*  functions, namely
                1 for key was already in table before this call
                  (old value was replaced)
                  (it doesn't matter what the old value's type_code and
                   N_elements were, i.e. these do *not* have to match the
                   new value),
                0 for key was not in table before this call,
                -ve for error, including
                UTIL_ERROR_BAD_HANDLE           handle is invalid
                UTIL_ERROR_TABLE_BAD_KEY        key contains '/' character
                UTIL_ERROR_NO_MEMORY            unable to allocate memory
  @endreturndesc
  @@*/
int Util_TableSetString(int handle,
                        const char *string,
                        const char *key)
{
return internal_set(handle,
                    CCTK_VARIABLE_CHAR, strlen(string), (const void *) string,
                    key);
}

/******************************************************************************/

/*@@
  @routine      Util_TableGetString
  @desc         This function gets a copy of the character-string value
                associated with a specified key, and stores it (or at least
                as much of it as will fit) in a specified character string.

  @comment      This function assumes that the value stored in the table
                is an array of CCTK_CHARs, which does *not* include a
                terminating null character.
  @endcomment

  @comment      The implementation assumes (as is presently the case)
                that a string is in fact an array of CCTK_CHAR, i.e.
                that CCTK_CHAR is the same type as (or at least
                compatible with) char.
  @endcomment

  @var          handle
  @vtype        int
  @vdesc        handle to the table
  @endvar

  @var          buffer_length
  @vtype        int (must be >= 1 if buffer != NULL)
  @vdesc        size of  buffer[]
  @endvar

  @var          buffer
  @vtype        char[]
  @vdesc        a buffer into which this function should store
                (at most  buffer_length-1  characters of) the value,
                terminated by a null character as usual for C strings,
                or NULL pointer to skip storing this
  @endvar

  @var          key
  @vtype        const char *
  @vdesc        pointer to the key (a C-style null-terminated string)
  @endvar

  @returntype   int
  @returndesc   the string length of the value (as per strlen()),
                -ve for error, including
                UTIL_ERROR_BAD_HANDLE           handle is invalid
                UTIL_ERROR_TABLE_BAD_KEY        key contains '/' character
                UTIL_ERROR_BAD_INPUT            buffer != NULL
                                                and buffer_length <= 0
                UTIL_ERROR_TABLE_NO_SUCH_KEY    no such key in table
                UTIL_ERROR_TABLE_WRONG_DATA_TYPE    value has data type
                                                    other than CCTK_CHAR
                UTIL_ERROR_TABLE_STRING_TRUNCATED   buffer != NULL and
                                                    value was truncated
                                                    to fit in buffer[]
  @endreturndesc
  @@*/
int Util_TableGetString(int handle,
                        int buffer_length, char buffer[],
                        const char *key)
{
/* actual length of string, not counting terminating null character */
int string_length = internal_get(handle,
                                 CCTK_VARIABLE_CHAR,
                                 buffer_length-1, (void *) buffer,
                                 key);
if (string_length < 0)
   then return string_length;           /* error return from internal_get() */

/* explicitly add the terminating null character */
if (buffer != NULL)
   then {
        assert(buffer_length >= 1);     /* this should never fail: */
                                        /* internal_get() should return */
                                        /* an error if buffer != NULL */
                                        /* and buffer_length-1 < 0 */
          {
        int null_posn = min(string_length, buffer_length-1);
        buffer[null_posn] = '\0';
          }
        }

return ((buffer != NULL) && (string_length > buffer_length-1))
       ? UTIL_ERROR_TABLE_STRING_TRUNCATED
       : string_length;
}

/******************************************************************************/

/*@@
  @routine      Util_TableSet*
  @desc         This is a family of functions, one for each Cactus data type,
                to set the value associated with a specified key to be a
                specified value (treated as a 1-element array).

                Note that this invalidates any iterators for this table.

  @var          handle
  @vtype        int
  @vdesc        handle to the table
  @endvar

  @var          value
  @vtype        one of
                   CCTK_POINTER, CCTK_FN_POINTER,
                   CCTK_CHAR,
                   CCTK_INT, CCTK_INT2, CCTK_INT4, CCTK_INT8,
                   CCTK_REAL, CCTK_REAL4, CCTK_REAL8, CCTK_REAL16,
                   CCTK_COMPLEX, CCTK_COMPLEX8, CCTK_COMPLEX16, CCTK_COMPLEX32
                (not all of these may be supported on any given system)
  @vdesc        the value to be associated with the specified key
  @endvar

  @var          key
  @vtype        const char *
  @vdesc        pointer to the key (a C-style null-terminated string)
  @endvar

  @returntype   int
  @returndesc   1 for key was already in table before this call
                  (old value was replaced)
                  (it doesn't matter what the old value's type_code and
                   N_elements were, i.e. these do *not* have to match the
                   new value),
                0 for key was not in table before this call,
                -ve for error, including
                UTIL_ERROR_BAD_HANDLE           handle is invalid
                UTIL_ERROR_TABLE_BAD_KEY        key contains '/' character
                UTIL_ERROR_NO_MEMORY            unable to allocate memory
  @endreturndesc
  @@*/

/**************************************/

/*
 * pointers
 */

int Util_TableSetPointer(int handle, CCTK_POINTER value, const char *key)
{
return Util_TableSetPointerArray(handle, 1, &value, key);
}

int Util_TableSetFnPointer(int handle, CCTK_FN_POINTER value, const char *key)
{
return Util_TableSetFnPointerArray(handle, 1, &value, key);
}

/**************************************/

/*
 * a single character
 */

int Util_TableSetChar(int handle, CCTK_CHAR value, const char *key)
{
return Util_TableSetCharArray(handle, 1, &value, key);
}

/**************************************/

/*
 * integers
 */

int Util_TableSetInt(int handle, CCTK_INT value, const char *key)
{
return Util_TableSetIntArray(handle, 1, &value, key);
}

#ifdef CCTK_INTEGER_PRECISION_2
int Util_TableSetInt2(int handle, CCTK_INT2 value, const char *key)
{
return Util_TableSetInt2Array(handle, 1, &value, key);
}
#endif

#ifdef CCTK_INTEGER_PRECISION_4
int Util_TableSetInt4(int handle, CCTK_INT4 value, const char *key)
{
return Util_TableSetInt4Array(handle, 1, &value, key);
}
#endif

#ifdef CCTK_INTEGER_PRECISION_8
int Util_TableSetInt8(int handle, CCTK_INT8 value, const char *key)
{
return Util_TableSetInt8Array(handle, 1, &value, key);
}
#endif

/**************************************/

/*
 * real numbers
 */

int Util_TableSetReal(int handle, CCTK_REAL value, const char *key)
{
return Util_TableSetRealArray(handle, 1, &value, key);
}

#ifdef CCTK_REAL_PRECISION_4
int Util_TableSetReal4(int handle, CCTK_REAL4 value, const char *key)
{
return Util_TableSetReal4Array(handle, 1, &value, key);
}
#endif

#ifdef CCTK_REAL_PRECISION_8
int Util_TableSetReal8(int handle, CCTK_REAL8 value, const char *key)
{
return Util_TableSetReal8Array(handle, 1, &value, key);
}
#endif

#ifdef CCTK_REAL_PRECISION_16
int Util_TableSetReal16(int handle, CCTK_REAL16 value, const char *key)
{
return Util_TableSetReal16Array(handle, 1, &value, key);
}
#endif

/**************************************/

/*
 * complex numbers
 */

int Util_TableSetComplex(int handle, CCTK_COMPLEX value, const char *key)
{
return Util_TableSetComplexArray(handle, 1, &value, key);
}

#ifdef CCTK_COMPLEX_PRECISION_8
int Util_TableSetComplex8(int handle, CCTK_COMPLEX8 value, const char *key)
{
return Util_TableSetComplex8Array(handle, 1, &value, key);
}
#endif

#ifdef CCTK_COMPLEX_PRECISION_16
int Util_TableSetComplex16(int handle, CCTK_COMPLEX16 value, const char *key)
{
return Util_TableSetComplex16Array(handle, 1, &value, key);
}
#endif

#ifdef CCTK_COMPLEX_PRECISION_32
int Util_TableSetComplex32(int handle, CCTK_COMPLEX32 value, const char *key)
{
return Util_TableSetComplex32Array(handle, 1, &value, key);
}
#endif

/******************************************************************************/

/*@@
  @routine      Util_TableSet*Array
  @desc         This is a family of functions, one for each Cactus data type,
                to set the value associated with the specified key to be
                (a copy of) a specified array.

                Note that this invalidates any iterators for this table.

  @var          handle
  @vtype        int
  @vdesc        handle to the table
  @endvar

  @var          N_elements
  @vtype        int (must be >= 0)
  @vdesc        number of elements in  array[]
  @endvar

  @var          array
  @vtype        const T[], where T is one of
                   CCTK_POINTER, CCTK_FN_POINTER,
                   CCTK_CHAR,
                   CCTK_INT, CCTK_INT2, CCTK_INT4, CCTK_INT8,
                   CCTK_REAL, CCTK_REAL4, CCTK_REAL8, CCTK_REAL16,
                   CCTK_COMPLEX, CCTK_COMPLEX8, CCTK_COMPLEX16, CCTK_COMPLEX32
                (not all of these may be supported on any given system)
  @vdesc        a pointer to the array (a copy of) which
                is to be associated with the specified key
  @endvar

  @var          key
  @vtype        const char *
  @vdesc        pointer to the key (a C-style null-terminated string)
  @endvar

  @returntype   int
  @returndesc   1 for key was already in table before this call
                  (old value was replaced)
                  (it doesn't matter what the old value's type_code and
                   N_elements were, i.e. these do *not* have to match the
                   new value),
                0 for key was not in table before this call,
                -ve for error, including
                UTIL_ERROR_BAD_HANDLE           handle is invalid
                UTIL_ERROR_TABLE_BAD_KEY        key contains '/' character
                UTIL_ERROR_BAD_INPUT            N_elements < 0
                UTIL_ERROR_NO_MEMORY            unable to allocate memory
  @endreturndesc
  @@*/

/**************************************/

/*
 * arrays of pointers
 */

int Util_TableSetPointerArray(int handle,
                              int N_elements, const CCTK_POINTER array[],
                              const char *key)
{
return internal_set(handle,
                    CCTK_VARIABLE_POINTER, N_elements, (const void *) array,
                    key);
}

int Util_TableSetFnPointerArray(int handle,
                                int N_elements, const CCTK_FN_POINTER array[],
                                const char *key)
{
return internal_set(handle,
                    CCTK_VARIABLE_FN_POINTER, N_elements, (const void *) array,
                    key);
}

/**************************************/

/*
 * arrays of characters (i.e. character strings)
 */

int Util_TableSetCharArray(int handle,
                           int N_elements, const CCTK_CHAR array[],
                           const char *key)
{
return internal_set(handle,
                    CCTK_VARIABLE_CHAR, N_elements, (const void *) array,
                    key);
}

/**************************************/

/*
 * arrays of integers
 */

int Util_TableSetIntArray(int handle,
                          int N_elements, const CCTK_INT array[],
                          const char *key)
{
return internal_set(handle,
                    CCTK_VARIABLE_INT, N_elements, (const void *) array,
                    key);
}

#ifdef CCTK_INTEGER_PRECISION_2
int Util_TableSetInt2Array(int handle,
                           int N_elements, const CCTK_INT2 array[],
                           const char *key)
{
return internal_set(handle,
                    CCTK_VARIABLE_INT2, N_elements, (const void *) array,
                    key);
}
#endif

#ifdef CCTK_INTEGER_PRECISION_4
int Util_TableSetInt4Array(int handle,
                           int N_elements, const CCTK_INT4 array[],
                           const char *key)
{
return internal_set(handle,
                    CCTK_VARIABLE_INT4, N_elements, (const void *) array,
                    key);
}
#endif

#ifdef CCTK_INTEGER_PRECISION_8
int Util_TableSetInt8Array(int handle,
                           int N_elements, const CCTK_INT8 array[],
                           const char *key)
{
return internal_set(handle,
                    CCTK_VARIABLE_INT8, N_elements, (const void *) array,
                    key);
}
#endif

/**************************************/

/*
 * arrays of real numbers
 */

int Util_TableSetRealArray(int handle,
                           int N_elements, const CCTK_REAL array[],
                           const char *key)
{
return
  internal_set(handle,
                  CCTK_VARIABLE_REAL, N_elements, (const void *) array,
                  key);
}

#ifdef CCTK_REAL_PRECISION_4
int Util_TableSetReal4Array(int handle,
                            int N_elements, const CCTK_REAL4 array[],
                            const char *key)
{
return internal_set(handle,
                    CCTK_VARIABLE_REAL4, N_elements, (const void *) array,
                    key);
}
#endif

#ifdef CCTK_REAL_PRECISION_8
int Util_TableSetReal8Array(int handle,
                            int N_elements, const CCTK_REAL8 array[],
                            const char *key)
{
return internal_set(handle,
                    CCTK_VARIABLE_REAL8, N_elements, (const void *) array,
                    key);
}
#endif

#ifdef CCTK_REAL_PRECISION_16
int Util_TableSetReal16Array(int handle,
                             int N_elements, const CCTK_REAL16 array[],
                             const char *key)
{
return internal_set(handle,
                    CCTK_VARIABLE_REAL16, N_elements, (const void *) array,
                    key);
}
#endif

/**************************************/

/*
 * arrays of complex numbers
 */

int Util_TableSetComplexArray(int handle,
                              int N_elements, const CCTK_COMPLEX array[],
                              const char *key)
{
return internal_set(handle,
                    CCTK_VARIABLE_COMPLEX, N_elements, (const void *) array,
                    key);
}

#ifdef CCTK_COMPLEX_PRECISION_8
int Util_TableSetComplex8Array(int handle,
                               int N_elements, const CCTK_COMPLEX8 array[],
                               const char *key)
{
return internal_set(handle,
                    CCTK_VARIABLE_COMPLEX8, N_elements, (const void *) array,
                    key);
}
#endif

#ifdef CCTK_COMPLEX_PRECISION_16
int Util_TableSetComplex16Array(int handle,
                                int N_elements, const CCTK_COMPLEX16 array[],
                                const char *key)
{
return internal_set(handle,
                    CCTK_VARIABLE_COMPLEX16, N_elements, (const void *) array,
                    key);
}
#endif

#ifdef CCTK_COMPLEX_PRECISION_32
int Util_TableSetComplex32Array(int handle,
                                int N_elements, const CCTK_COMPLEX32 array[],
                                const char *key)
{
return internal_set(handle,
                    CCTK_VARIABLE_COMPLEX32, N_elements, (const void *) array,
                    key);
}
#endif

/******************************************************************************/

/*@@
  @routine      Util_TableGet*
  @desc         This is a family of functions, one for each Cactus data type,
                to get a copy of the scalar (1-element array) value, or more
                generally the first array element of the value, associated
                with a specified key.

  @var          handle
  @vtype        int
  @vdesc        handle to the table
  @endvar

  @var          value
  @vtype        T *, where T is one of
                   CCTK_POINTER, CCTK_FN_POINTER,
                   CCTK_CHAR,
                   CCTK_INT, CCTK_INT2, CCTK_INT4, CCTK_INT8,
                   CCTK_REAL, CCTK_REAL4, CCTK_REAL8, CCTK_REAL16,
                   CCTK_COMPLEX, CCTK_COMPLEX8, CCTK_COMPLEX16, CCTK_COMPLEX32
                (not all of these may be supported on any given system)
  @vdesc        pointer to where this function should store
                a copy of the value associated with the specified key,
                or NULL pointer to skip storing this
  @endvar

  @var          key
  @vtype        const char *
  @vdesc        pointer to the key (a C-style null-terminated string)
  @endvar

  @returntype   int
  @returndesc   the number of elements in the value,
                -ve for error, including
                UTIL_ERROR_BAD_HANDLE           handle is invalid
                UTIL_ERROR_TABLE_BAD_KEY        key contains '/' character
                UTIL_ERROR_TABLE_NO_SUCH_KEY    no such key in table
                UTIL_ERROR_TABLE_WRONG_DATA_TYPE value has wrong data type
                UTIL_ERROR_TABLE_VALUE_IS_EMPTY value is an empty
                                                (0-element) array
  @comment      Note that it is *not* an error for the value to actually
                be an array with > 1 elements elements; in this case only
                the first element is stored.

                The rationale for this design is that the caller may
                know or suspect that the value is a large array, but
                may only want the first array element; in this case
                this design avoids the caller having to allocate a
                large buffer unnecessarily.

                In contrast, it *is* an error for the value to actually
                be an empty (0-length) array, because then there is no
                ``first array element'' to get.
  @endcomment
  @endreturndesc
  @@*/

/**************************************/

/* pointers */
int Util_TableGetPointer(int handle, CCTK_POINTER *value, const char *key)
{
int status = Util_TableGetPointerArray(handle, 1, value, key);
return (status == 0)
       ? UTIL_ERROR_TABLE_VALUE_IS_EMPTY
       : status;
}

int Util_TableGetFnPointer(int handle, CCTK_FN_POINTER *value, const char *key)
{
int status = Util_TableGetFnPointerArray(handle, 1, value, key);
return (status == 0)
       ? UTIL_ERROR_TABLE_VALUE_IS_EMPTY
       : status;
}

/**************************************/

/* a single character */
int Util_TableGetChar(int handle, CCTK_CHAR *value, const char *key)
{
int status = Util_TableGetCharArray(handle, 1, value, key);
return (status == 0)
       ? UTIL_ERROR_TABLE_VALUE_IS_EMPTY
       : status;
}

/**************************************/

/* integers */
int Util_TableGetInt(int handle, CCTK_INT *value, const char *key)
{
int status = Util_TableGetIntArray(handle, 1, value, key);
return (status == 0)
       ? UTIL_ERROR_TABLE_VALUE_IS_EMPTY
       : status;
}

#ifdef CCTK_INTEGER_PRECISION_2
int Util_TableGetInt2(int handle, CCTK_INT2 *value, const char *key)
{
int status = Util_TableGetInt2Array(handle, 1, value, key);
return (status == 0)
       ? UTIL_ERROR_TABLE_VALUE_IS_EMPTY
       : status;
}
#endif

#ifdef CCTK_INTEGER_PRECISION_4
int Util_TableGetInt4(int handle, CCTK_INT4 *value, const char *key)
{
int status = Util_TableGetInt4Array(handle, 1, value, key);
return (status == 0)
       ? UTIL_ERROR_TABLE_VALUE_IS_EMPTY
       : status;
}
#endif

#ifdef CCTK_INTEGER_PRECISION_8
int Util_TableGetInt8(int handle, CCTK_INT8 *value, const char *key)
{
int status = Util_TableGetInt8Array(handle, 1, value, key);
return (status == 0)
       ? UTIL_ERROR_TABLE_VALUE_IS_EMPTY
       : status;
}
#endif

/**************************************/

/* real numbers */
int Util_TableGetReal(int handle, CCTK_REAL *value, const char *key)
{
int status = Util_TableGetRealArray(handle, 1, value, key);
return (status == 0)
       ? UTIL_ERROR_TABLE_VALUE_IS_EMPTY
       : status;
}

#ifdef CCTK_REAL_PRECISION_4
int Util_TableGetReal4(int handle, CCTK_REAL4 *value, const char *key)
{
int status = Util_TableGetReal4Array(handle, 1, value, key);
return (status == 0)
       ? UTIL_ERROR_TABLE_VALUE_IS_EMPTY
       : status;
}
#endif

#ifdef CCTK_REAL_PRECISION_8
int Util_TableGetReal8(int handle, CCTK_REAL8 *value, const char *key)
{
int status = Util_TableGetReal8Array(handle, 1, value, key);
return (status == 0)
       ? UTIL_ERROR_TABLE_VALUE_IS_EMPTY
       : status;
}
#endif

#ifdef CCTK_REAL_PRECISION_16
int Util_TableGetReal16(int handle, CCTK_REAL16 *value, const char *key)
{
int status = Util_TableGetReal16Array(handle, 1, value, key);
return (status == 0)
       ? UTIL_ERROR_TABLE_VALUE_IS_EMPTY
       : status;
}
#endif

/**************************************/

/* complex numbers */
int Util_TableGetComplex(int handle, CCTK_COMPLEX *value, const char *key)
{
int status = Util_TableGetComplexArray(handle, 1, value, key);
return (status == 0)
       ? UTIL_ERROR_TABLE_VALUE_IS_EMPTY
       : status;
}

#ifdef CCTK_COMPLEX_PRECISION_8
int Util_TableGetComplex8(int handle, CCTK_COMPLEX8 *value, const char *key)
{
int status = Util_TableGetComplex8Array(handle, 1, value, key);
return (status == 0)
       ? UTIL_ERROR_TABLE_VALUE_IS_EMPTY
       : status;
}
#endif

#ifdef CCTK_COMPLEX_PRECISION_16
int Util_TableGetComplex16(int handle, CCTK_COMPLEX16 *value, const char *key)
{
int status = Util_TableGetComplex16Array(handle, 1, value, key);
return (status == 0)
       ? UTIL_ERROR_TABLE_VALUE_IS_EMPTY
       : status;
}
#endif

#ifdef CCTK_COMPLEX_PRECISION_32
int Util_TableGetComplex32(int handle, CCTK_COMPLEX32 *value, const char *key)
{
int status = Util_TableGetComplex32Array(handle, 1, value, key);
return (status == 0)
       ? UTIL_ERROR_TABLE_VALUE_IS_EMPTY
       : status;
}
#endif

/******************************************************************************/

/*@@
  @routine      Util_TableGet*Array
  @desc         This is a family of functions, one for each Cactus data type,
                to get a copy of the value associated with a specified key
                (or at least as much of the value as will fit into the
                caller's array).

  @var          handle
  @vtype        int
  @vdesc        handle to the table
  @endvar

  @var          N_array
  @vtype        int (must be >= 0)
  @vdesc        number of elements in  array[]
  @endvar

  @var          array
  @vtype        T[], where T is one of
                   CCTK_POINTER, CCTK_FN_POINTER,
                   CCTK_CHAR,
                   CCTK_INT, CCTK_INT2, CCTK_INT4, CCTK_INT8,
                   CCTK_REAL, CCTK_REAL4, CCTK_REAL8, CCTK_REAL16,
                   CCTK_COMPLEX, CCTK_COMPLEX8, CCTK_COMPLEX16, CCTK_COMPLEX32
                (not all of these may be supported on any given system)
  @vdesc        an array into which this function should store
                (at most  N_array  elements of) a copy of the value
                associated with the specified key,
                or NULL pointer to skip storing this
  @endvar

  @var          key
  @vtype        const char *
  @vdesc        pointer to the key (a C-style null-terminated string)
  @endvar

  @returntype   int
  @returndesc   the number of elements in the value,
                -ve for error, including
                UTIL_ERROR_BAD_HANDLE           handle is invalid
                UTIL_ERROR_TABLE_BAD_KEY        key contains '/' character
                UTIL_ERROR_BAD_INPUT            array != NULL and N_array < 0
                UTIL_ERROR_TABLE_NO_SUCH_KEY    no such key in table
                UTIL_ERROR_TABLE_WRONG_DATA_TYPE value has wrong data type
  @comment      Note that it is *not* an error for the value to have
                > N_array elements; in this case only N_array are
                stored.  The caller can detect this by comparing the
                return value with N_array.

                The rationale for this design is that the caller may
                know or suspect that the value is a large array, but
                may only want the first few array elements; in this
                case this design avoids the caller having to allocate
                a large buffer unnecessarily.

                It is also *not* an error for the value to have < N_array
                elements; again the caller can detect this by comparing the
                return value with N_array.
  @endcomment
  @endreturndesc
  @@*/

/**************************************/

/* arrays of pointers */
int Util_TableGetPointerArray(int handle,
                              int N_array, CCTK_POINTER array[],
                              const char *key)
{
return internal_get(handle,
                    CCTK_VARIABLE_POINTER, N_array, (void *) array,
                    key);
}

int Util_TableGetFnPointerArray(int handle,
                                int N_array, CCTK_FN_POINTER array[],
                                const char *key)
{
return internal_get(handle,
                    CCTK_VARIABLE_FN_POINTER, N_array, (void *) array,
                    key);
}

/**************************************/

/* arrays of characters (i.e. character strings) */
int Util_TableGetCharArray(int handle,
                           int N_array, CCTK_CHAR array[],
                           const char *key)
{
return internal_get(handle,
                    CCTK_VARIABLE_CHAR, N_array, (void *) array,
                    key);
}

/**************************************/

/* integers */
int Util_TableGetIntArray(int handle,
                          int N_array, CCTK_INT array[],
                          const char *key)
{
return internal_get(handle,
                    CCTK_VARIABLE_INT, N_array, (void *) array,
                    key);
}

#ifdef CCTK_INTEGER_PRECISION_2
int Util_TableGetInt2Array(int handle,
                           int N_array, CCTK_INT2 array[],
                           const char *key)
{
return internal_get(handle,
                    CCTK_VARIABLE_INT2, N_array, (void *) array,
                    key);
}
#endif

#ifdef CCTK_INTEGER_PRECISION_4
int Util_TableGetInt4Array(int handle,
                           int N_array, CCTK_INT4 array[],
                           const char *key)
{
return internal_get(handle,
                    CCTK_VARIABLE_INT4, N_array, (void *) array,
                    key);
}
#endif

#ifdef CCTK_INTEGER_PRECISION_8
int Util_TableGetInt8Array(int handle,
                           int N_array, CCTK_INT8 array[],
                           const char *key)
{
return internal_get(handle,
                    CCTK_VARIABLE_INT8, N_array, (void *) array,
                    key);
}
#endif

/**************************************/

/* real numbers */
int Util_TableGetRealArray(int handle,
                           int N_array, CCTK_REAL array[],
                           const char *key)
{
return internal_get(handle,
                    CCTK_VARIABLE_REAL, N_array, (void *) array,
                    key);
}
#ifdef CCTK_REAL_PRECISION_4

int Util_TableGetReal4Array(int handle,
                            int N_array, CCTK_REAL4 array[],
                            const char *key)
{
return internal_get(handle,
                    CCTK_VARIABLE_REAL4, N_array, (void *) array,
                    key);
}
#endif

#ifdef CCTK_REAL_PRECISION_8
int Util_TableGetReal8Array(int handle,
                            int N_array, CCTK_REAL8 array[],
                            const char *key)
{
return internal_get(handle,
                    CCTK_VARIABLE_REAL8, N_array, (void *) array,
                    key);
}
#endif

#ifdef CCTK_REAL_PRECISION_16
int Util_TableGetReal16Array(int handle,
                             int N_array, CCTK_REAL16 array[],
                             const char *key)
{
return internal_get(handle,
                    CCTK_VARIABLE_REAL16, N_array, (void *) array,
                    key);
}
#endif

/**************************************/

/* complex numbers */
int Util_TableGetComplexArray(int handle,
                              int N_array, CCTK_COMPLEX array[],
                              const char *key)
{
return internal_get(handle,
                    CCTK_VARIABLE_COMPLEX, N_array, (void *) array,
                    key);
}
#ifdef CCTK_COMPLEX_PRECISION_8

int Util_TableGetComplex8Array(int handle,
                               int N_array, CCTK_COMPLEX8 array[],
                               const char *key)
{
return internal_get(handle,
                    CCTK_VARIABLE_COMPLEX8, N_array, (void *) array,
                    key);
}
#endif

#ifdef CCTK_COMPLEX_PRECISION_16
int Util_TableGetComplex16Array(int handle,
                                int N_array, CCTK_COMPLEX16 array[],
                                const char *key)
{
return internal_get(handle,
                    CCTK_VARIABLE_COMPLEX16, N_array, (void *) array,
                    key);
}
#endif

#ifdef CCTK_COMPLEX_PRECISION_32
int Util_TableGetComplex32Array(int handle,
                                int N_array, CCTK_COMPLEX32 array[],
                                const char *key)
{
return internal_get(handle,
                    CCTK_VARIABLE_COMPLEX16, N_array, (void *) array,
                    key);
}
#endif

/******************************************************************************/
/***** Table Iterator API *****************************************************/
/******************************************************************************/

/*@@
  @routine      Util_TableItCreate
  @desc         This function creates a new table iterator.  The iterator
                points to the starting entry in the table's traversal order.

  @var          handle
  @vtype        int
  @vdesc        handle to the table
  @endvar

  @returntype   int
  @returndesc   a handle to the newly-created iterator,
                -ve for error, including
                UTIL_ERROR_BAD_HANDLE           table handle is invalid
                UTIL_ERROR_NO_MEMORY            unable to allocate memory
  @endreturndesc
  @@*/
int Util_TableItCreate(int handle)
{
const struct table_header *const thp = get_table_header_ptr(handle);
if (thp == NULL)
   then return UTIL_ERROR_BAD_HANDLE;

#ifdef UTIL_TABLE_DEBUG
printf("Util_TableItCreate(handle=%d)\n", handle);
#endif

if (N_iterators == N_ip_array)
   then {
        /* grow  iterator_array  to get some room to create the new table */
        #ifdef UTIL_TABLE_DEBUG
        printf("   growing ip_array[] from old size %d\n",
               N_ip_array);
        #endif
        if (grow_pointer_array(&N_ip_array, &ip_array) < 0)
           then return UTIL_ERROR_NO_MEMORY;    /* can't grow array */
        #ifdef UTIL_TABLE_DEBUG
        printf("                        to new size %d\n",
               N_ip_array);
        #endif
        }

/* we should now have space to create the new iterator */
assert(N_iterators < N_ip_array);

/* find an unused iterator handle */
#ifdef UTIL_TABLE_DEBUG
printf("   searching for an unused iterator handle\n");
printf("   (N_iterators=%d N_ip_array=%d\n", N_iterators, N_ip_array);
#endif
  {
int ihandle;
        for (ihandle = 0 ; ihandle < N_ip_array ; ++ihandle)
        {
        #ifdef UTIL_TABLE_DEBUG2
        printf("      checking ihandle=%d\n", ihandle);
        #endif
        if (ip_array[ihandle] == NULL)
           then {
                /* we've found an unused ihandle ==> create the iterator */
                struct iterator *const ip
                        = (struct iterator *) malloc(sizeof(struct iterator));
                if (ip == NULL)
                   then return UTIL_ERROR_NO_MEMORY;
                                        /* can't allocate new iterator */

                #ifdef UTIL_TABLE_DEBUG2
                printf("   using ihandle=%d\n", ihandle);
                #endif

                ip->thp = thp;
                ip->tep = thp->head;    /* iterator initially */
                                        /* -> start of table */

                ++N_iterators;
                ip_array[ihandle] = (void *) ip;

                return ihandle;
                }
        }

/* we should never get to here! */
assert(false);
abort();                                /* internal error (core dump) */
/* prevent compiler warning 'function should return a value' */
  }
}

/******************************************************************************/

/*@@
  @routine      Util_TableItDestroy
  @desc         This function destroys a table iterator.

  @var          ihandle
  @vtype        int
  @vdesc        handle to the iterator
  @endvar

  @returntype   int
  @returndesc   0 for ok,
                -ve for error, including
                UTIL_ERROR_BAD_HANDLE           iterator handle is invalid
  @endreturndesc
  @@*/
int Util_TableItDestroy(int ihandle)
{
struct iterator *const ip = get_iterator_ptr(ihandle);
if (ip == NULL)
   then return UTIL_ERROR_BAD_HANDLE;

#ifdef UTIL_TABLE_DEBUG
printf("Util_TableItDestroy(ihandle=%d)\n", ihandle);
#endif

--N_iterators;
ip_array[ihandle] = NULL;
free(ip);

return 0;                               /* ok */
}

/******************************************************************************/

/*@@
  @routine      Util_TableItQueryIsNull
  @desc         This function queries whether a table iterator is in the
                "null-pointer" state, i.e. whether it does *not* point
                to some table entry.

                Bad things (garbage results, core dumps) may happen if
                you call this function on a table iterator which has been
                invalidated by a change in the table's contents.

  @var          ihandle
  @vtype        int
  @vdesc        handle to the iterator
  @endvar

  @returntype   int
  @returndesc   1 for iterator is in "null-pointer" state,
                0 for iterator points to some table entry,
                -ve for error, including
                UTIL_ERROR_BAD_HANDLE           iterator handle is invalid
  @endreturndesc
  @@*/
int Util_TableItQueryIsNull(int ihandle)
{
const struct iterator *const ip = get_iterator_ptr(ihandle);
if (ip == NULL)
   then return UTIL_ERROR_BAD_HANDLE;

return (ip->tep == NULL)
       ? 1                              /* iterator in "null-pointer" state */
       : 0;                             /* iterator -> some table entry */
}

/******************************************************************************/

/*@@
  @routine      Util_TableItQueryIsNonNull
  @desc         This function queries whether a table iterator is *not* in
                the "null-pointer" state, i.e. whether it points to some
                table entry.

                Bad things (garbage results, core dumps) may happen if
                you call this function on an iterator which has been
                invalidated by a change in the table's contents.

  @var          ihandle
  @vtype        int
  @vdesc        handle to the iterator
  @endvar

  @returntype   int
  @returndesc   1 for iterator points to some table entry,
                0 for iterator is in "null-pointer" state,
                -ve for error, including
                UTIL_ERROR_BAD_HANDLE           iterator handle is invalid
  @endreturndesc
  @@*/
int Util_TableItQueryIsNonNull(int ihandle)
{
const struct iterator *const ip = get_iterator_ptr(ihandle);
if (ip == NULL)
   then return UTIL_ERROR_BAD_HANDLE;

return (ip->tep == NULL)
       ? 0                              /* iterator in "null-pointer" state */
       : 1;                             /* iterator -> some table entry */
}

/******************************************************************************/

/*@@
  @routine      Util_TableItQueryTableHandle
  @desc         This function queries which table a table iterator points
                into.

                Note that this is always well-defined, even if the iterator
                is in the "null-pointer" state, and even if the iterator
                has been invalidated by a change in the table's contents.

  @var          ihandle
  @vtype        int
  @vdesc        handle to the iterator
  @endvar

  @returntype   int
  @returndesc   table handle,
                -ve for error, including
                UTIL_ERROR_BAD_HANDLE           iterator handle is invalid
  @endreturndesc
  @@*/
int Util_TableItQueryTableHandle(int ihandle)
{
const struct iterator *const ip = get_iterator_ptr(ihandle);
if (ip == NULL)
   then return UTIL_ERROR_BAD_HANDLE;

return ip->thp->handle;
}

/******************************************************************************/

/*@@
  @routine      Util_TableItQueryKeyValueInfo
  @desc         This function queries the key and the type and number of
                elements of the value corresponding to that key, of the
                table entry to which an iterator points.  This is in fact
                the main purpose of iterators.

                Bad things (garbage results, core dumps) may happen if
                you call this function on an iterator which has been
                invalidated by a change in the table's contents.

  @var          ihandle
  @vtype        int
  @vdesc        handle to the iterator
  @endvar

  @var          key_buffer_length,
  @vtype        int (must be >= 1 if key_buffer != NULL)
  @vdesc        length of  key_buffer[]  buffer
  @endvar

  @var          key_buffer,
  @vtype        char []
  @vdesc        a buffer into which this function should store
                (at most  key_buffer_length-1  characters of) the key,
                terminated by a null character as usual for C strings,
                or NULL pointer to skip storing this
  @endvar

  @var          type_code
  @vtype        CCTK_INT *
  @vdesc        pointer to where this function should store
                the value's type code
                (one of the CCTK_VARIABLE_* constants from "cctk_Types.h"),
                or NULL pointer to skip storing this
  @endvar

  @var          N_elements
  @vtype        CCTK_INT *
  @vdesc        pointer to where this function should store
                the number of array elements in the value,
                or NULL pointer to skip storing this
  @endvar

  @returntype   int
  @returndesc   the string length of the key (as per strlen()),
                -ve for error, including
                UTIL_ERROR_BAD_HANDLE           iterator handle is invalid
                UTIL_ERROR_TABLE_ITERATOR_IS_NULL  iterator is in
                                                   "null-pointer" state
                UTIL_ERROR_TABLE_STRING_TRUNCATED  key_buffer != NULL and
                                                   key was truncated
                                                   to fit in key_buffer[]
  @endreturndesc
  @@*/
int Util_TableItQueryKeyValueInfo(int ihandle,
                                  int key_buffer_length, char key_buffer[],
                                  CCTK_INT *type_code, CCTK_INT *N_elements)
{
const struct iterator *const ip = get_iterator_ptr(ihandle);
if (ip == NULL)
   then return UTIL_ERROR_BAD_HANDLE;

  {
const struct table_entry *const tep = ip->tep;
if (tep == NULL)
   then return UTIL_ERROR_TABLE_ITERATOR_IS_NULL;

  {
const int actual_key_length = strlen(tep->key);

/* store the fixed-length output arguments first, so the caller */
/* will have them even if we hit an error trying to copy the key */
if (type_code != NULL)
   then *type_code = tep->type_code;
if (N_elements != NULL)
   then *N_elements = tep->N_elements;

if (key_buffer != NULL)
   then {
        const int N_key_copy = min(key_buffer_length-1, actual_key_length);
        if (N_key_copy < 0)     /* can only happen if key_buffer_length <= 0 */
           then {
                /*
                 * We have to bail out now, before trying the memcpy(),
                 * because memcpy() takes a size_t (= unsigned) value for
                 * its count of how many chars to copy, and converting our
                 * -ve N_key_copy to size_t would give a huge +ve count :( :(
                 */
                return UTIL_ERROR_TABLE_STRING_TRUNCATED;
                }
        memcpy(key_buffer, tep->key, N_key_copy);
        key_buffer[N_key_copy] = '\0';
        if (N_key_copy < actual_key_length)
           then return UTIL_ERROR_TABLE_STRING_TRUNCATED;
        }

return actual_key_length;               /* ok */
  }
  }
}

/******************************************************************************/

/*@@
  @routine      Util_TableItAdvance
  @desc         This function advances a table iterator to the next entry
                in the table's traversal order.

                Bad things (garbage results, core dumps) may happen if
                you call this function on an iterator which has been
                invalidated by a change in the table's contents.

  @var          ihandle
  @vtype        int
  @vdesc        handle to the iterator
  @endvar

  @returntype   int
  @returndesc   same as that of Util_TableItQueryNonNull(ihandle)
                after advancing the iterator, i.e.
                1 for ok and iterator now points to some table element,
                0 for advance-past-last-entry
                  (sets iterator to "null-pointer" state),
                0 if iterator was already in "null-pointer" state)
                  (in this case this call is a no-op),
                -ve for error, including
                UTIL_ERROR_BAD_HANDLE           iterator handle is invalid
  @endreturndesc
  @@*/
int Util_TableItAdvance(int ihandle)
{
struct iterator *const ip = get_iterator_ptr(ihandle);
if (ip == NULL)
   then return UTIL_ERROR_BAD_HANDLE;

if (ip->tep == NULL)
   then return 0;                       /* iterator was already in */
                                        /* "null-pointer" state */

ip->tep = ip->tep->next;

return (ip->tep == NULL)
       ? 0              /* advance past last entry */
                        /* ==> iterator now in "null-pointer" state */
       : 1;             /* ok */
}

/******************************************************************************/

/*@@
  @routine      Util_TableItResetToStart
  @desc         This function resets a table iterator to point to the
                starting entry in the table's traversal order.

                Note that it is always ok to call this function, even
                if the iterator has been invalidated by a change in the
                table's contents.

  @var          ihandle
  @vtype        int
  @vdesc        handle to the iterator
  @endvar

  @returntype   int
  @returndesc   same as that of Util_TableItQueryNonNull(ihandle)
                after resetting the iterator, i.e.
                1 for ok and iterator now points to some table element,
                0 for ok and iterator is now in "null-pointer" state
                  (means table is empty)
                -ve for error, including
                UTIL_ERROR_BAD_HANDLE           iterator handle is invalid
  @endreturndesc
  @@*/
int Util_TableItResetToStart(int ihandle)
{
struct iterator *const ip = get_iterator_ptr(ihandle);
if (ip == NULL)
   then return UTIL_ERROR_BAD_HANDLE;

ip->tep = ip->thp->head;
return (ip->tep == NULL)
       ? 0              /* ok, iterator is now in "null-pointer" state */
                        /*     (table must be empty) */
       : 1;             /* ok, iterator points to some table element */
}

/******************************************************************************/

/*@@
  @routine      Util_TableItSetToNull
  @desc         This function sets a table iterator to the "null-pointer"
                state.

                Note that it is always ok to call this function, even
                if the iterator has been invalidated by a change in the
                table's contents.

  @var          ihandle
  @vtype        int
  @vdesc        handle to the iterator
  @endvar

  @returntype   int
  @returndesc   0 for ok,
                -ve for error, including
                UTIL_ERROR_BAD_HANDLE           iterator handle is invalid
  @endreturndesc
  @@*/
int Util_TableItSetToNull(int ihandle)
{
struct iterator *const ip = get_iterator_ptr(ihandle);
if (ip == NULL)
   then return UTIL_ERROR_BAD_HANDLE;

ip->tep = NULL;
return 0;                               /* ok */
}

/******************************************************************************/

/*@@
  @routine      Util_TableItSetToKey
  @desc         This function sets a table iterator to point to a
                specified table entry.  It has the same effect as
                Util_TableItResetToStart() followed by repeatedly
                calling Util_TableItAdvance() until the iterator
                points to the desired table entry.

                Note that it is always ok to call this function, even
                if the iterator has been invalidated by a change in the
                table's contents.

  @var          ihandle
  @vtype        int
  @vdesc        handle to the iterator
  @endvar

  @var          key
  @vtype        const char *
  @vdesc        pointer to the key (a C-style null-terminated string)
  @endvar

  @returntype   int
  @returndesc   0 for ok,
                UTIL_ERROR_BAD_HANDLE           iterator handle is invalid
                UTIL_ERROR_TABLE_BAD_KEY        key contains '/' character
                UTIL_ERROR_TABLE_NO_SUCH_KEY    no such key in table
  @endreturndesc
  @@*/
int Util_TableItSetToKey(int ihandle, const char *key)
{
struct iterator *const ip = get_iterator_ptr(ihandle);
if (ip == NULL)
   then return UTIL_ERROR_BAD_HANDLE;

if (bad_key(key))
   then return UTIL_ERROR_TABLE_BAD_KEY;

ip->tep = find_table_entry(ip->thp, key, NULL);
if (ip->tep == NULL)
   then return UTIL_ERROR_TABLE_NO_SUCH_KEY;

return 0;
}

/******************************************************************************/
/***** Internal Support Functions *********************************************/
/******************************************************************************/

/*@@
  @routine      internal_set
  @desc         This is the internal function implementing all the
                        Util_TableSet*()
                        Util_TableSet*Array()
                functions except Util_TableSetString().  It sets the
                value associated with a specified key, to be a copy
                of a specified array.

                Note that this invalidates any iterators for this table.

  @var          handle
  @vtype        int
  @vdesc        handle to the table
  @endvar

  @var          N_elements
  @vtype        int (must be >= 0)
  @vdesc        number of elements in  array[]
  @endvar

  @var          array
  @vtype        const T[], where T is one of
                   CCTK_POINTER, CCTK_FN_POINTER,
                   CCTK_CHAR,
                   CCTK_INT, CCTK_INT2, CCTK_INT4, CCTK_INT8,
                   CCTK_REAL, CCTK_REAL4, CCTK_REAL8, CCTK_REAL16,
                   CCTK_COMPLEX, CCTK_COMPLEX8, CCTK_COMPLEX16, CCTK_COMPLEX32
                (not all of these may be supported on any given system)
  @vdesc        the array (a copy of) which is to be associated with
                the specified key
  @endvar

  @var          key
  @vtype        const char *
  @vdesc        pointer to the key (a C-style null-terminated string)
  @endvar

  @returntype   int
  @returndesc   1 for key was already in table before this call
                  (old value was replaced)
                  (it doesn't matter what the old value's type_code and
                   N_elements were, i.e. these do *not* have to match the
                   new value),
                0 for key was not in table before this call,
                -ve for error, including
                UTIL_ERROR_BAD_HANDLE           handle is invalid
                UTIL_ERROR_TABLE_BAD_KEY        key contains '/' character
                UTIL_ERROR_BAD_INPUT            N_elements < 0
                UTIL_ERROR_NO_MEMORY            unable to allocate memory
  @endreturndesc
  @@*/
static
  int internal_set(int handle,
                   int type_code, int N_elements, const void *value,
                   const char *key)
{
struct table_header *const thp = get_table_header_ptr(handle);
if (thp == NULL)
   then return UTIL_ERROR_BAD_HANDLE;

#ifdef UTIL_TABLE_DEBUG
printf("internal_set(handle=%d, type_code=%d, N_elements=%d, key=\"%s\")\n",
       handle, type_code, N_elements, key);
#endif

if (bad_key(key))
   then return UTIL_ERROR_TABLE_BAD_KEY;
if (N_elements < 0)
   then return UTIL_ERROR_BAD_INPUT;

/* if key is already in table, delete it */
/* ... this is a harmless no-op if it's not already in the table */
  {
int return_value;
switch  (delete_key(thp, key))
        {
case 0:
        return_value = 1;       /* key was already in table before this call */
                                /* (we've just deleted it, and we're about */
                                /*  to set the replacement in the table) */
        break;
case UTIL_ERROR_TABLE_NO_SUCH_KEY:
        return_value = 0;       /* key was not in table before this call */
        break;
default:
        /* unexpected return code from  delete_key() */
        /* (this should never happen!) */
        assert(false);
        abort();                        /* internal error (core dump) */
        }

/* allocate a new table entry */
  {
struct table_entry *tep
        = (struct table_entry *) malloc(sizeof(struct table_entry));
if (tep == NULL)
   then return UTIL_ERROR_NO_MEMORY;    /* can't allocate new table entry */

/* set up the new table entry */
tep->key = Util_Strdup(key);
if (tep->key == NULL)
   then {
        free(tep);
        return UTIL_ERROR_NO_MEMORY;    /* can't allocate memory to copy key */
        }

tep->type_code = type_code;
tep->N_elements = N_elements;

  {
size_t sizeof_value = N_elements * CCTK_VarTypeSize(type_code);
#ifdef UTIL_TABLE_DEBUG2
printf("   allocating new buffer of size sizeof_value=%d bytes\n",
       (int) sizeof_value);
#endif
  {
void *buffer = malloc(sizeof_value);
if (buffer == NULL)
   then {
        free(tep->key);
        free(tep);
        return UTIL_ERROR_NO_MEMORY;    /* can't allocate memory */
                                        /* to copy value */
        }
#ifdef UTIL_TABLE_DEBUG
printf("   copying sizeof_value=%d bytes into buffer\n", (int) sizeof_value);
#endif
memcpy(buffer, value, sizeof_value);
tep->value = buffer;

/* insert the table entry into the table's linked list */
/* (we could insert it anywhere; for simplicity we insert it at the head) */
tep->next = thp->head;
thp->head = tep;

return return_value;
  }
  }
  }
  }
}

/******************************************************************************/

/*@@
  @routine      internal_get
  @desc         This is the internal function implementing all the
                        Util_TableGet*()
                        Util_TableGet*Array()
                functions except for Util_TableGetString().  It copies
                up to N_elements of the value associated with a specified
                key, into a user-supplied buffer.

  @var          handle
  @vtype        int
  @vdesc        handle to the table
  @endvar

  @var          N_value_buffer
  @vtype        int (must be >= 0)
  @vdesc        number of elements in  array[]
  @endvar

  @var          value_buffer
  @vtype        T[], where T is one of
                   CCTK_POINTER, CCTK_FN_POINTER,
                   CCTK_CHAR,
                   CCTK_INT, CCTK_INT2, CCTK_INT4, CCTK_INT8,
                   CCTK_REAL, CCTK_REAL4, CCTK_REAL8, CCTK_REAL16,
                   CCTK_COMPLEX, CCTK_COMPLEX8, CCTK_COMPLEX16, CCTK_COMPLEX32
                (not all of these may be supported on any given system)
  @vdesc        an array into which this function should store
                (at most  N_elements  elements of) a copy of the value
                associated with the specified key,
                or NULL pointer to skip storing this
  @endvar

  @var          key
  @vtype        const char *
  @vdesc        pointer to the key (a C-style null-terminated string)
  @endvar

  @returntype   int
  @returndesc   number of elements in the value,
                -ve for error, including
                UTIL_ERROR_BAD_HANDLE           handle is invalid
                UTIL_ERROR_TABLE_BAD_KEY        key contains '/' character
                UTIL_ERROR_BAD_INPUT            N_value_buffer < 0
                UTIL_ERROR_BAD_INPUT            value_buffer != NULL
                                                and N_value_buffer < 0
                UTIL_ERROR_TABLE_NO_SUCH_KEY    no such key in table
                UTIL_ERROR_TABLE_WRONG_DATA_TYPE value has wrong data type
  @comment      Note that it is *not* an error for the value to have
                > N_value_buffer elements; in this case only N_value_buffer
                are stored.  The caller can detect this by comparing the
                return value with N_value_buffer.
  @endcomment
  @endreturndesc
  @@*/
static
  int internal_get(int handle,
                   int type_code, int N_value_buffer, void *value_buffer,
                   const char *key)
{
const struct table_header *const thp = get_table_header_ptr(handle);
if (thp == NULL)
   then return UTIL_ERROR_BAD_HANDLE;

#ifdef UTIL_TABLE_DEBUG
printf("internal_get(handle=%d, type_code=%d, N_value_buffer=%d, key=\"%s\")\n",
       handle, type_code, N_value_buffer, key);
#endif

if (bad_key(key))
   then return UTIL_ERROR_TABLE_BAD_KEY;

  {
const struct table_entry *const tep = find_table_entry(thp, key, NULL);
if (tep == NULL)
   then return UTIL_ERROR_TABLE_NO_SUCH_KEY;    /* no such key in table */

if (tep->type_code != type_code)
   then return UTIL_ERROR_TABLE_WRONG_DATA_TYPE; /* value has wrong data type */

if (value_buffer != NULL)
   then {
        if (N_value_buffer < 0)
           then return UTIL_ERROR_BAD_INPUT;
          {
        const int N_copy = min(N_value_buffer, tep->N_elements);
        const size_t sizeof_N_copy_elements
                = N_copy * CCTK_VarTypeSize(type_code);
        #ifdef UTIL_TABLE_DEBUG
        printf(
           "   copying N_copy=%d elements (sizeof_N_copy_elements=%d bytes)\n",
               N_copy, (int) sizeof_N_copy_elements);
        #endif
        memcpy(value_buffer, tep->value, sizeof_N_copy_elements);
          }
        }

return tep->N_elements;
  }
}

/******************************************************************************/

/*
 * This function gets a pointer to a table's header, given the table handle.
 *
 * Arguments:
 * handle = The table handle.
 *
 * Results:
 * If the handle is invalid (i.e. there is no such table), this function
 *    returns NULL.
 * If the handle is valid, this function returns a pointer to the table header.
 */
static
  struct table_header *get_table_header_ptr(int handle)
{
return ((handle >= 0) && (handle < N_thp_array))
       ? (struct table_header *) thp_array[handle]      /* valid handle */
       : NULL;                                          /* invalid handle */
}

/******************************************************************************/

/*
 * This function deletes a key from a table.
 *
 * Results:
 * The return value is the same as for Util_TableDeleteKey(), i.e.
 *      0 for ok (key existed before this call, and has now been deleted)
 *      -ve for error, including
 *      UTIL_ERROR_TABLE_NO_SUCH_KEY    no such key in table
 */
static
  int delete_key(struct table_header *thp, const char *key)
{
struct table_entry *prev_tep;
struct table_entry *const tep = find_table_entry(thp, key, &prev_tep);
if (tep == NULL)
   then return UTIL_ERROR_TABLE_NO_SUCH_KEY;

  {
/* unlink the table entry from the list */
struct table_entry *next_tep = tep->next;
if (prev_tep == NULL)
   then thp->head = next_tep;           /* it was the starting entry */
                                        /* in the list */
   else prev_tep->next = next_tep;      /* it was somewhere in the middle */

free_table_entry(tep);
return 0;                               /* ok: key existed before this call, */
                                        /* and has now been deleted */
  }
}

/******************************************************************************/

/*
 * This function frees a table entry and the key/value it holds.
 *
 * Arguments:
 * tep -> The table entry to be freed.
 */
static
  void free_table_entry(struct table_entry *tep)
{
assert(tep != NULL);

assert(tep->key != NULL);
free(tep->key);

assert(tep->value != NULL);
free(tep->value);

free(tep);
}

/******************************************************************************/

/*
 * check if key is syntactically "bad" (eg contains '/' character)
 * returns true for bad key, false for ok
 */
static
  bool bad_key(const char *key)
{
assert(key != NULL);

if (strchr(key, '/') != NULL)
   then return true;

return false;                           /* ok */
}

/******************************************************************************/

/*
 * This function finds the (first) table entry with a given key.
 * Optionally, it also finds the table entry *before* that one in
 * the linked list.
 *
 * Arguments:
 * thp -> The table header.
 * key -> The key to search for.
 * prev_tep_ptr = If this is non-NULL, then this function sets
 *                *prev_tep_ptr to -> the table entry *before* the one
 *                with the given key, or NULL if the table entry with
 *                the given key is the starting one in the table.
 *                Thus if  prev_tep_ptr  is non-NULL, then after this
 *                function returns, the returned result is
 *                (*prev_tep_ptr == NULL) ? thp->head : (*prev_tep)->next
 *
 * Results:
 * The function returns a pointer to the table entry, or NULL if the
 * key isn't found in the table.
 */
static
  struct table_entry *find_table_entry(const struct table_header *thp,
                                       const char *key,
                                       struct table_entry **prev_tep_ptr)
{
assert(thp != NULL);
assert(key != NULL);

  {
const int flags = thp->flags;
struct table_entry *prev_tep = NULL;
struct table_entry *tep = thp->head;
        for ( ; tep != NULL ; prev_tep = tep, tep = tep->next)
        {
        if (  (flags & UTIL_TABLE_FLAGS_CASE_INSENSITIVE)
              ?  (Util_StrCmpi(key, tep->key) == 0)
              :  (     strcmp (key, tep->key) == 0)  )
           then {
                if (prev_tep_ptr != NULL)
                   *prev_tep_ptr = prev_tep;
                return tep;             /* key found in table */
                }
        }

return NULL;                            /* key not found in table */
  }
}

/******************************************************************************/

/*
 * This function gets a pointer to an iterator, given the iterator handle.
 *
 * Arguments:
 * ihandle = The iterator handle.
 *
 * Results:
 * If the handle is invalid (i.e. there is no such table), this function
 *    returns NULL.
 * If the handle is valid, this function returns a pointer to the iterator.
 */
static
  struct iterator *get_iterator_ptr(int ihandle)
{
return ((ihandle >= 0) && (ihandle < N_ip_array))
       ? (struct iterator *) ip_array[ihandle]          /* valid handle */
       : NULL;                                          /* invalid handle */
}

/******************************************************************************/

/*
 * This function grows an malloc-allocated array of  void *  pointers
 * via realloc(), initializing the new space to NULL pointers.
 *
 * Arguments:
 * *pN = (in out) array size
 * *pvp_array = (in out) Pointer to growable array of  void *  pointers.
 *
 * Results:
 * This function returns
 *      0 for ok,
 *      -ve for error, including
 *      UTIL_ERROR_NO_MEMORY            can't allocate memory to grow table
 */
static
  int grow_pointer_array(int *pN, void ***pvp_array)
{
int N = *pN;
void **vp_array = *pvp_array;
int new_N = GROW(N);
void **new_vp_array = realloc(vp_array, new_N*sizeof(void *));
if (new_vp_array == NULL)
   then return UTIL_ERROR_NO_MEMORY;    /* can't grow array */

/* initialize the new space to NULL pointers */
  {
int i;
        for (i = N ; i < new_N ; ++i)
        {
        new_vp_array[i] = NULL;
        }
  }

*pvp_array = new_vp_array;
*pN = new_N;
return 0;                               /* ok */
}

/******************************************************************************/
/***** Table and Iterator Dump Routines ***************************************/
/******************************************************************************/

#ifdef UTIL_TABLE_TEST
/*
 * This function prints out all the tables and their data structures.
 */
static
  void print_all_tables(void)
{
int handle;

printf("N_tables=%d N_thp_array=%d auto_destroy_handle=%d\n",
       N_tables, N_thp_array, auto_destroy_handle);
        for (handle = 0 ; handle < N_thp_array ; ++handle)
        {
        print_table(handle);
        }
}
#endif  /* UTIL_TABLE_TEST */

/******************************************************************************/

#ifdef UTIL_TABLE_TEST
/*
 * This function prints out a table.
 */
static
  void print_table(int handle)
{
printf("thp_array[%d]: ", handle);
  {
const struct table_header *const thp = get_table_header_ptr(handle);
if (thp == NULL)
   then printf("NULL\n");
   else {
        printf("flags=0x%x handle=%d\n", thp->flags, thp->handle);
          {
        const struct table_entry *tep = thp->head;
                for ( ; tep != NULL ; tep = tep->next)
                {
                printf("    [tep=%p]\n", (const void *) tep);
                printf("\tkey=\"%s\"\n", tep->key);
                printf("\ttype_code=%d N_elements=%d\n",
                       tep->type_code, tep->N_elements);
                  {
                int i;
                switch  (tep->type_code)
                        {
                case CCTK_VARIABLE_INT:
                        printf("\t[int]");
                                for (i = 0 ; i < tep->N_elements ; ++i)
                                {
                                const CCTK_INT *value_int
                                        = (const CCTK_INT *) tep->value;
                                printf("\t%d", (int) value_int[i]);
                                }
                        break;
                case CCTK_VARIABLE_REAL:
                        printf("\t[real]");
                                for (i = 0 ; i < tep->N_elements ; ++i)
                                {
                                const CCTK_REAL *value_real
                                        = (const CCTK_REAL *) tep->value;
                                printf("\t%g", (double) value_real[i]);
                                }
                        break;
                case CCTK_VARIABLE_COMPLEX:
                        printf("\t[complex]");
                                for (i = 0 ; i < tep->N_elements ; ++i)
                                {
                                const CCTK_COMPLEX *value_complex
                                        = (const CCTK_COMPLEX *) tep->value;
                                printf("\t(%g,%g)",
                                       (double) value_complex[i].Re,
                                       (double) value_complex[i].Im);
                                }
                        }
                printf("\n");
                  }
                }
          }
        }
  }
}
#endif  /* UTIL_TABLE_TEST */

/******************************************************************************/

#ifdef UTIL_TABLE_TEST
/*
 * This function prints out all the iterators and their data structures.
 */
static
  void print_all_iterators(void)
{
int ihandle;

printf("N_iterators=%d N_ip_array=%d\n", N_iterators, N_ip_array);
        for (ihandle = 0 ; ihandle < N_ip_array ; ++ihandle)
        {
        const struct iterator *const ip = get_iterator_ptr(ihandle);
        printf("ip_array[%d]: ", ihandle);
        if (ip == NULL)
           then printf("NULL\n");
           else printf("thp=%p tep=%p\n",
                       (const void *) ip->thp, (const void *) ip->tep);
        }
}
#endif  /* UTIL_TABLE_TEST */

/******************************************************************************/
/***** Standalone Test Driver *************************************************/
/******************************************************************************/

#ifdef UTIL_TABLE_TEST

/*
 * macros to test set/get of scalars of various types
 */

#define TEST_SET_GET_INT(handle, type,                                  \
                         key_already_exists, case_insensitive,          \
                         set_fn, get_fn)                                \
  {                                                                     \
type x = 42;                                                            \
assert( set_fn(handle, x, "int_x") == key_already_exists );             \
x = 1;                                                                  \
assert( get_fn(handle, &x, "int_x") == 1 );                             \
assert( x == 42 );                                                      \
if (case_insensitive)                                                   \
   then {                                                               \
        x = 2;                                                          \
        assert( get_fn(handle, &x, "Int_X") == 1 );                     \
        assert( x == 42 );                                              \
        }                                                               \
   else assert( get_fn(handle, &x, "Int_X")                             \
                == UTIL_ERROR_TABLE_NO_SUCH_KEY );                      \
  }                                                             /* end macro */

#define TEST_SET_GET_REAL(handle, type,                                 \
                          key_already_exists, case_insensitive, \
                          set_fn, get_fn)                               \
  {                                                                     \
type y = 42.25;                                                         \
assert( set_fn(handle, y, "REAL_y") == key_already_exists );            \
y = 1.25;                                                               \
assert( get_fn(handle, &y, "REAL_y") == 1 );                            \
assert( y == 42.25 );                                                   \
if (case_insensitive)                                                   \
   then {                                                               \
        y = 1.5;                                                        \
        assert( get_fn(handle, &y, "real_y") == 1 );                    \
        assert( y == 42.25 );                                           \
        }                                                               \
   else assert( get_fn(handle, &y, "real_y")                            \
                == UTIL_ERROR_TABLE_NO_SUCH_KEY );                      \
  }                                                             /* end macro */

#define TEST_SET_GET_COMPLEX(handle, type,                              \
                             key_already_exists, case_insensitive,      \
                             set_fn, get_fn)                            \
  {                                                                     \
static type z = { 42.25, 105.5 };                                       \
assert( set_fn(handle, z, "COMPlex_Z") == key_already_exists );         \
z.Re = 1.25;            z.Im = -2.78;                                   \
assert( get_fn(handle, &z, "COMPlex_Z") == 1 );                         \
assert( z.Re == 42.25 );                                                \
assert( z.Im == 105.5 );                                                \
if (case_insensitive)                                                   \
   then {                                                               \
        z.Re = 1.5;             z.Im = -2.83;                           \
        assert( get_fn(handle, &z, "COMPLEX_Z") == 1 );                 \
        assert( z.Re == 42.25 );                                        \
        assert( z.Im == 105.5 );                                        \
        }                                                               \
   else assert( get_fn(handle, &z, "COMPLEX_Z")                         \
                == UTIL_ERROR_TABLE_NO_SUCH_KEY );                      \
  }                                                             /* end macro */


/*
 * macros to test set/get of arrays of various types
 */

#define TEST_SET_GET_INT_ARRAY(handle, type, key_already_exists,        \
                               set_fn, get_fn)                          \
  {                                                                     \
static type xx[5] = { 41, 42, 48, 45, 47 };                             \
assert( set_fn(handle, 3, xx, "xx") == key_already_exists );            \
xx[0] = 14;  xx[1] = 15;  xx[2] = 16;  xx[3] = 17;  xx[4] = 19;         \
/* try to get 4 values, but only 3 were stored ==> only get 3 */        \
assert( get_fn(handle, 4, xx, "xx") == 3 );                             \
assert( xx[0] == 41 );                                                  \
assert( xx[1] == 42 );                                                  \
assert( xx[2] == 48 );                                                  \
assert( xx[3] == 17 );                                                  \
assert( xx[4] == 19 );                                                  \
  }                                                             /* end macro */

#define TEST_SET_GET_REAL_ARRAY(handle, type, key_already_exists,       \
                                set_fn, get_fn)                         \
  {                                                                     \
static type yy[5] = { 41.25, 42.5, 48.0, 45.75, 47.125 };               \
assert( set_fn(handle, 4, yy, "yy") == key_already_exists );            \
yy[0] = 14.0;  yy[1] = 15.5;  yy[2] = 16.0;                             \
yy[3] = 17.5;  yy[4] = 19.5;                                            \
/* only get 3 of 4 stored values */                                     \
assert( get_fn(handle, 3, yy, "yy") == 4 );                             \
assert( yy[0] == 41.25 );                                               \
assert( yy[1] == 42.5 );                                                \
assert( yy[2] == 48.0 );                                                \
assert( yy[3] == 17.5 );                                                \
assert( yy[4] == 19.5 );                                                \
  }                                                             /* end macro */

#define TEST_SET_GET_COMPLEX_ARRAY(handle, type, key_already_exists,    \
                                   set_fn, get_fn)                      \
  {                                                                     \
static type zz[5]                                                       \
        = { {3.5,1.25}, {9.5,4.5}, {0.5,8.0}, {5.0,5.5}, {4.5,7.25} };  \
assert( set_fn(handle, 4, zz, "zz") == key_already_exists );            \
zz[0].Re = 10.25;       zz[0].Im = 11.75;                               \
zz[1].Re = -2.5;        zz[1].Im = 3.5;                                 \
zz[2].Re = 14.0;        zz[2].Im = -8.5;                                \
zz[3].Re = 0.25;        zz[3].Im = 8.875;                               \
zz[4].Re = -0.25;       zz[4].Im = -0.75;                               \
/* only get 3 of 4 stored values */                                     \
assert( get_fn(handle, 3, zz, "zz") == 4 );                             \
assert( zz[0].Re == 3.5 );      assert( zz[0].Im == 1.25 );             \
assert( zz[1].Re == 9.5 );      assert( zz[1].Im == 4.5 );              \
assert( zz[2].Re == 0.5 );      assert( zz[2].Im == 8.0 );              \
assert( zz[3].Re == 0.25 );     assert( zz[3].Im == 8.875 );            \
assert( zz[4].Re == -0.25 );    assert( zz[4].Im == -0.75 );            \
  }                                                             /* end macro */

#endif  /* UTIL_TABLE_TEST */

/******************************************************************************/

#ifdef UTIL_TABLE_TEST
/*
 * This program is a standalone test driver for the key/value table system.
 */
int main(void)
{
test_nonexistent_tables();
test_table_create_destroy();

  {
int handle = Util_TableCreate(UTIL_TABLE_FLAGS_DEFAULT);
assert( handle == 0 );
assert( Util_TableSetInt(handle, 42, "foo/") == UTIL_ERROR_TABLE_BAD_KEY );

  {
int HANDLE = Util_TableCreate(UTIL_TABLE_FLAGS_CASE_INSENSITIVE);

#ifdef UTIL_TABLE_DEBUG
printf("--- printing handle=%d table (should be empty)\n", handle);
print_table(handle);
printf("--- about to test set/get on handle=%d table\n", handle);
#endif
test_set_get(handle, false);
test_set_get(HANDLE, true);

test_iterators(handle);
test_delete_key(handle, false);
test_iterators(HANDLE);
test_delete_key(HANDLE, true);

test_set_get_array(handle);
test_set_get_array(HANDLE);

test_set_get_string(handle, false);

  {
int HANDLE2 = test_create_from_string();
test_set_get_string(HANDLE2, true);

printf("all ok!\n" );
return 0;
  }
  }
  }
}
#endif  /* UTIL_TABLE_TEST */

/******************************************************************************/

#ifdef UTIL_TABLE_TEST
/*
 * This function tests that various operations on nonexistent tables
 * and iterators give error returns.
 */
static
  void test_nonexistent_tables(void)
{
assert( Util_TableDestroy(42) == UTIL_ERROR_BAD_HANDLE );
assert( Util_TableQueryFlags(-42) == UTIL_ERROR_BAD_HANDLE );
assert( Util_TableQueryNKeys(0) == UTIL_ERROR_BAD_HANDLE );
assert( Util_TableDeleteKey(-1, "pickle") == UTIL_ERROR_BAD_HANDLE );
assert( Util_TableSetInt(-1, 42, "fourty-two") == UTIL_ERROR_BAD_HANDLE );
assert( Util_TableGetReal(-1, NULL, "something wierd")
        == UTIL_ERROR_BAD_HANDLE );

assert( Util_TableItCreate(42) == UTIL_ERROR_BAD_HANDLE );
assert( Util_TableItDestroy(42) == UTIL_ERROR_BAD_HANDLE );
assert( Util_TableItQueryIsNull(42) == UTIL_ERROR_BAD_HANDLE );
assert( Util_TableItQueryIsNonNull(42) == UTIL_ERROR_BAD_HANDLE );
assert( Util_TableItQueryTableHandle(42) == UTIL_ERROR_BAD_HANDLE );
assert( Util_TableItQueryKeyValueInfo(42,
                                      0, NULL,
                                      NULL, NULL) == UTIL_ERROR_BAD_HANDLE );
assert( Util_TableItAdvance(42) == UTIL_ERROR_BAD_HANDLE );
assert( Util_TableItResetToStart(42) == UTIL_ERROR_BAD_HANDLE );
assert( Util_TableItSetToNull(42) == UTIL_ERROR_BAD_HANDLE );
}
#endif  /* UTIL_TABLE_TEST */

/******************************************************************************/

#ifdef UTIL_TABLE_TEST
/*
 * This function tests creation and destruction of tables.
 * It also tests
 * - querying flags words
 * - querying NKeys and MaxKeyLength for empty tables
 * - deleting keys from empty tables
 *
 * It assumes that no tables exist when the function is called,
 * and it eventually destroys all the tables it creates.
 *
 * Bugs:
 * Parts of this test are tied to the present implementation -- it
 * uses local variables of the implementation, and it assumes a specific
 * strategy for allocating handles.
 */
static
  void test_table_create_destroy(void)
{
assert( N_tables == 0 );

assert( Util_TableCreate(UTIL_TABLE_FLAGS_DEFAULT) == 0 );
assert( Util_TableCreate(UTIL_TABLE_FLAGS_CASE_INSENSITIVE) == 1 );
assert( Util_TableCreate(UTIL_TABLE_FLAGS_DEFAULT) == 2 );
assert( Util_TableCreate(  UTIL_TABLE_FLAGS_CASE_INSENSITIVE
                         | UTIL_TABLE_FLAGS_AUTO_DESTROY    ) == 3 );
assert( N_tables == 4 );
assert( auto_destroy_handle == 3 );
assert( get_table_header_ptr(0) != NULL );
assert( get_table_header_ptr(1) != NULL );
assert( get_table_header_ptr(2) != NULL );
assert( get_table_header_ptr(3) != NULL );
assert( Util_TableQueryFlags(3) == (  UTIL_TABLE_FLAGS_CASE_INSENSITIVE
                                    | UTIL_TABLE_FLAGS_AUTO_DESTROY    ) );

assert( Util_TableCreate(UTIL_TABLE_FLAGS_CASE_INSENSITIVE) == 3 );
assert( N_tables == 4 );
assert( auto_destroy_handle == -1 );
assert( get_table_header_ptr(0) != NULL );
assert( Util_TableQueryFlags(0) == UTIL_TABLE_FLAGS_DEFAULT );
assert( get_table_header_ptr(1) != NULL );
assert( Util_TableQueryFlags(1) == UTIL_TABLE_FLAGS_CASE_INSENSITIVE );
assert( get_table_header_ptr(2) != NULL );
assert( Util_TableQueryFlags(2) == UTIL_TABLE_FLAGS_DEFAULT );
assert( get_table_header_ptr(3) != NULL );
assert( Util_TableQueryFlags(3) == UTIL_TABLE_FLAGS_CASE_INSENSITIVE );
assert( Util_TableDeleteKey(3, "pickle") == UTIL_ERROR_TABLE_NO_SUCH_KEY );
assert( Util_TableDeleteKey(3, "Pickle") == UTIL_ERROR_TABLE_NO_SUCH_KEY );
assert( Util_TableDeleteKey(3, "PICKLE") == UTIL_ERROR_TABLE_NO_SUCH_KEY );

assert( Util_TableDestroy(2) == 0 );
assert( N_tables == 3 );
assert( auto_destroy_handle == -1 );
assert( get_table_header_ptr(0) != NULL );
assert( get_table_header_ptr(1) != NULL );
assert( get_table_header_ptr(2) == NULL );
assert( get_table_header_ptr(3) != NULL );

assert( Util_TableCreate(UTIL_TABLE_FLAGS_AUTO_DESTROY) == 2 );
assert( N_tables == 4 );
assert( auto_destroy_handle == 2 );
assert( get_table_header_ptr(0) != NULL );
assert( get_table_header_ptr(1) != NULL );
assert( get_table_header_ptr(2) != NULL );
assert( Util_TableQueryFlags(2) == UTIL_TABLE_FLAGS_AUTO_DESTROY );
assert( get_table_header_ptr(3) != NULL );

/* explicitly destroy table with auto-destroy flag set */
/* also check querying NKeys and MaxKeyLength for empty tables */
assert( Util_TableDestroy(2) == 0 );
assert( N_tables == 3 );
assert( auto_destroy_handle == -1 );
assert( get_table_header_ptr(0) != NULL );
assert( Util_TableQueryNKeys(0) == 0 );
assert( get_table_header_ptr(1) != NULL );
assert( Util_TableQueryMaxKeyLength(1) == 0 );
assert( get_table_header_ptr(2) == NULL );
assert( get_table_header_ptr(3) != NULL );
assert( Util_TableDeleteKey(3, "pickle") == UTIL_ERROR_TABLE_NO_SUCH_KEY );

assert( Util_TableDestroy(1) == 0 );
assert( N_tables == 2 );
assert( auto_destroy_handle == -1 );
assert( get_table_header_ptr(0) != NULL );
assert( get_table_header_ptr(1) == NULL );
assert( get_table_header_ptr(2) == NULL );
assert( get_table_header_ptr(3) != NULL );

assert( Util_TableDestroy(0) == 0 );
assert( Util_TableDestroy(3) == 0 );

assert( N_tables == 0 );
}
#endif  /* UTIL_TABLE_TEST */

/******************************************************************************/

#ifdef UTIL_TABLE_TEST
/*
 * This function tests set/get of various-typed scalars.
 * It also tests querying NKeys, MaxKeyLength, and some keys.
 *
 * It assumes the table is empty when this function is called;
 * it leaves entries in the table.
 */
static
  void test_set_get(int handle, bool case_insensitive)
{
/*
 * Note we put a test of a type that's guaranteed to be defined...
 * - at the *beginning* of each group of tests, so we can properly
 *   assert whether or not the key was already in table beforehand.
 * - at the *end* of each group of tests, so the final table contents
 *   are known independently of which types are and aren't defined.
 */

/* integers */
TEST_SET_GET_INT(handle, CCTK_INT, 0, case_insensitive,
                 Util_TableSetInt, Util_TableGetInt);
#ifdef CCTK_INTEGER_PRECISION_2
TEST_SET_GET_INT(handle, CCTK_INT2, 1, case_insensitive,
                 Util_TableSetInt2, Util_TableGetInt2);
#endif
#ifdef CCTK_INTEGER_PRECISION_4
TEST_SET_GET_INT(handle, CCTK_INT4, 1, case_insensitive,
                 Util_TableSetInt4, Util_TableGetInt4);
#endif
#ifdef CCTK_INTEGER_PRECISION_8
TEST_SET_GET_INT(handle, CCTK_INT8, 1, case_insensitive,
                 Util_TableSetInt8, Util_TableGetInt8);
#endif
TEST_SET_GET_INT(handle, CCTK_INT, 1, case_insensitive,
                 Util_TableSetInt, Util_TableGetInt);
assert( Util_TableQueryNKeys(handle) == 1 );
assert( Util_TableQueryMaxKeyLength(handle) == (int)strlen("int_x") );

/* complex numbers */
TEST_SET_GET_COMPLEX(handle, CCTK_COMPLEX, 0, case_insensitive,
                     Util_TableSetComplex, Util_TableGetComplex);
#ifdef CCTK_COMPLEX_PRECISION_8
TEST_SET_GET_COMPLEX(handle, CCTK_COMPLEX8, 1, case_insensitive,
                     Util_TableSetComplex8, Util_TableGetComplex8);
#endif
#ifdef CCTK_COMPLEX_PRECISION_16
TEST_SET_GET_COMPLEX(handle, CCTK_COMPLEX16, 1, case_insensitive,
                     Util_TableSetComplex16, Util_TableGetComplex16);
#endif
#ifdef CCTK_COMPLEX_PRECISION_32
TEST_SET_GET_COMPLEX(handle, CCTK_COMPLEX32, 1, case_insensitive,
                     Util_TableSetComplex32, Util_TableGetComplex32);
#endif
TEST_SET_GET_COMPLEX(handle, CCTK_COMPLEX, 1, case_insensitive,
                     Util_TableSetComplex, Util_TableGetComplex);
assert( Util_TableQueryNKeys(handle) == 2 );
assert( Util_TableQueryMaxKeyLength(handle) == (int)strlen("COMPlex_Z") );

/* reals */
TEST_SET_GET_REAL(handle, CCTK_REAL, 0, case_insensitive,
                  Util_TableSetReal, Util_TableGetReal);
#ifdef CCTK_REAL_PRECISION_4
TEST_SET_GET_REAL(handle, CCTK_REAL4, 1, case_insensitive,
                  Util_TableSetReal4, Util_TableGetReal4);
#endif
#ifdef CCTK_REAL_PRECISION_8
TEST_SET_GET_REAL(handle, CCTK_REAL8, 1, case_insensitive,
                  Util_TableSetReal8, Util_TableGetReal8);
#endif
#ifdef CCTK_REAL_PRECISION_16
TEST_SET_GET_REAL(handle, CCTK_REAL16, 1, case_insensitive,
                  Util_TableSetReal16, Util_TableGetReal16);
#endif
TEST_SET_GET_REAL(handle, CCTK_REAL, 1, case_insensitive,
                  Util_TableSetReal, Util_TableGetReal);
assert( Util_TableQueryNKeys(handle) == 3 );
assert( Util_TableQueryMaxKeyLength(handle) == (int)strlen("COMPlex_Z") );

  {
CCTK_INT type_code, N_elements;
assert( Util_TableQueryValueInfo(handle, &type_code, &N_elements, "COMPlex_Z") == 1 );
assert( type_code == CCTK_VARIABLE_COMPLEX );
assert( N_elements == 1 );

assert( Util_TableQueryValueInfo(handle, &type_code, &N_elements, "pickle") == 0 );

assert( Util_TableQueryValueInfo(handle, NULL, NULL, "int_x") == 1 );
assert( Util_TableQueryValueInfo(handle, NULL, NULL, "Int_x")
        == (case_insensitive ? 1 : 0) );
assert( Util_TableQueryValueInfo(handle, NULL, NULL, "real_y")
        == (case_insensitive ? 1 : 0) );
assert( Util_TableQueryValueInfo(handle, NULL, NULL, "COMPLEX_Z")
        == (case_insensitive ? 1 : 0) );
  }
}
#endif  /* UTIL_TABLE_TEST */

/******************************************************************************/

#ifdef UTIL_TABLE_TEST
/*
 * This function tests set/get of various-typed arrays.
 *
 * It assumes the table is empty when this function is called;
 * it leaves entries in the table.
 */
static
  void test_set_get_array(int handle)
{
/* the comments of  test_set_get()  about test ordering, also apply here */

/* integers */
TEST_SET_GET_INT_ARRAY(handle, CCTK_CHAR, 0,
                       Util_TableSetCharArray, Util_TableGetCharArray);
TEST_SET_GET_INT_ARRAY(handle, CCTK_INT, 1,
                       Util_TableSetIntArray, Util_TableGetIntArray);
#ifdef CCTK_INTEGER_PRECISION_2
TEST_SET_GET_INT_ARRAY(handle, CCTK_INT2, 1,
                       Util_TableSetInt2Array, Util_TableGetInt2Array);
#endif
#ifdef CCTK_INTEGER_PRECISION_4
TEST_SET_GET_INT_ARRAY(handle, CCTK_INT4, 1,
                       Util_TableSetInt4Array, Util_TableGetInt4Array);
#endif
#ifdef CCTK_INTEGER_PRECISION_8
TEST_SET_GET_INT_ARRAY(handle, CCTK_INT8, 1,
                       Util_TableSetInt8Array, Util_TableGetInt8Array);
#endif
TEST_SET_GET_INT_ARRAY(handle, CCTK_INT, 1,
                       Util_TableSetIntArray, Util_TableGetIntArray);

/* reals */
TEST_SET_GET_REAL_ARRAY(handle, CCTK_REAL, 0,
                        Util_TableSetRealArray, Util_TableGetRealArray);
#ifdef CCTK_REAL_PRECISION_4
TEST_SET_GET_REAL_ARRAY(handle, CCTK_REAL4, 1,
                        Util_TableSetReal4Array, Util_TableGetReal4Array);
#endif
#ifdef CCTK_REAL_PRECISION_8
TEST_SET_GET_REAL_ARRAY(handle, CCTK_REAL8, 1,
                        Util_TableSetReal8Array, Util_TableGetReal8Array);
#endif
#ifdef CCTK_REAL_PRECISION_16
TEST_SET_GET_REAL_ARRAY(handle, CCTK_REAL16, 1,
                        Util_TableSetReal16Array, Util_TableGetReal16Array);
#endif
TEST_SET_GET_REAL_ARRAY(handle, CCTK_REAL, 1,
                        Util_TableSetRealArray, Util_TableGetRealArray);

/* complex numbers */
TEST_SET_GET_COMPLEX_ARRAY(handle, CCTK_COMPLEX, 0,
                           Util_TableSetComplexArray,
                           Util_TableGetComplexArray);
#ifdef CCTK_COMPLEX_PRECISION_8
TEST_SET_GET_COMPLEX_ARRAY(handle, CCTK_COMPLEX8, 1,
                           Util_TableSetComplex8Array,
                           Util_TableGetComplex8Array);
#endif
#ifdef CCTK_COMPLEX_PRECISION_16
TEST_SET_GET_COMPLEX_ARRAY(handle, CCTK_COMPLEX16, 1,
                           Util_TableSetComplex16Array,
                           Util_TableGetComplex16Array);
#endif
#ifdef CCTK_COMPLEX_PRECISION_32
TEST_SET_GET_COMPLEX_ARRAY(handle, CCTK_COMPLEX32, 1,
                           Util_TableSetComplex32Array,
                           Util_TableGetComplex32Array);
#endif
TEST_SET_GET_COMPLEX_ARRAY(handle, CCTK_COMPLEX, 1,
                           Util_TableSetComplexArray,
                           Util_TableGetComplexArray);
}
#endif  /* UTIL_TABLE_TEST */

/******************************************************************************/

#ifdef UTIL_TABLE_TEST
/*
 * This function tests iterating through a table and resetting an iterator.
 * It assumes the initial table contents are those generated by
 * test_get_set() , namely 3 keys "REAL_y", "COMPlex_Z", "int_x".
 *
 * Bugs:
 * This test is tied to the present implementation -- it assumes a
 * specific ordering of table elements returned by an iterator.
 */
static
  void test_iterators(int handle)
{
int ihandle = Util_TableItCreate(handle);
assert( ihandle >= 0 );
assert( Util_TableItQueryTableHandle(ihandle) == handle );
assert( Util_TableItQueryIsNonNull(ihandle) == 1);
assert( Util_TableItQueryIsNull(ihandle) == 0);

/* set up the key buffer */
  {
int max_key_length = Util_TableQueryMaxKeyLength(handle);
assert( max_key_length == (int)strlen("COMPlex_Z") );
  {
const int N_key_buffer = max_key_length + 1;
char *const key_buffer = malloc(N_key_buffer);
assert( key_buffer != NULL );

/* walk the table to verify iterator traversal */
  {
CCTK_INT type_code, N_elements;

/* REAL_y */
assert( Util_TableItQueryKeyValueInfo(ihandle,
                                      N_key_buffer, key_buffer,
                                      &type_code, &N_elements)
	== (int)strlen("REAL_y") );
assert( strcmp(key_buffer, "REAL_y") == 0 );
assert( type_code == CCTK_VARIABLE_REAL );
assert( N_elements == 1 );

/* COMPlex_Z */
type_code = 123456;
N_elements = 54321;
assert( Util_TableItAdvance(ihandle) == 1 );
assert( Util_TableItQueryKeyValueInfo(ihandle,
                                      N_key_buffer, key_buffer,
                                      &type_code, &N_elements)
	== (int)strlen("COMPlex_Z") );
assert( strcmp(key_buffer, "COMPlex_Z") == 0 );
assert( type_code = CCTK_VARIABLE_COMPLEX );
assert( N_elements == 1 );

/* int_x */
type_code = 123456;
N_elements = 54321;
assert( Util_TableItAdvance(ihandle) == 1 );
assert( Util_TableItQueryKeyValueInfo(ihandle,
                                      N_key_buffer, key_buffer,
                                      &type_code, &N_elements)
	== (int)strlen("int_x") );
assert( strcmp(key_buffer, "int_x") == 0 );
assert( type_code = CCTK_VARIABLE_INT );
assert( N_elements == 1 );

/* advance past last table entry ==> "null-pointer" state */
assert( Util_TableItAdvance(ihandle) == 0 );
assert( Util_TableItQueryIsNull(ihandle) == 1);
assert( Util_TableItQueryIsNonNull(ihandle) == 0);

/* advance again ==> stays in "null-pointer" state */
assert( Util_TableItAdvance(ihandle) == 0 );
assert( Util_TableItQueryIsNull(ihandle) == 1);
assert( Util_TableItQueryIsNonNull(ihandle) == 0);
assert( Util_TableItQueryKeyValueInfo(ihandle,
                                      0, NULL,
                                      NULL, NULL)
        == UTIL_ERROR_TABLE_ITERATOR_IS_NULL );

/* test reset to starting point */
assert( Util_TableItResetToStart(ihandle) == 1 );
assert( Util_TableItQueryIsNonNull(ihandle) == 1 );
assert( Util_TableItQueryIsNull(ihandle) == 0 );

/* COMPlex_Z */
type_code = 123456;
N_elements = 54321;
assert( Util_TableItAdvance(ihandle) == 1 );
assert( Util_TableItQueryKeyValueInfo(ihandle,
                                      N_key_buffer, key_buffer,
                                      &type_code, &N_elements)
	== (int)strlen("COMPlex_Z") );
assert( strcmp(key_buffer, "COMPlex_Z") == 0 );
assert( type_code = CCTK_VARIABLE_COMPLEX );
assert( N_elements == 1 );

/* test reset to "null-pointer" state */
assert( Util_TableItSetToNull(ihandle) == 0 );
assert( Util_TableItQueryIsNull(ihandle) == 1);
assert( Util_TableItQueryIsNonNull(ihandle) == 0);

/* test set to key "REAL_y" */
assert( Util_TableItSetToKey(ihandle, "REAL_y") == 0 );
assert( Util_TableItQueryIsNonNull(ihandle) == 1);
assert( Util_TableItQueryIsNull(ihandle) == 0);
assert( Util_TableItQueryKeyValueInfo(ihandle,
                                      N_key_buffer, key_buffer,
                                      &type_code, &N_elements)
	== (int)strlen("REAL_y") );
assert( strcmp(key_buffer, "REAL_y") == 0 );
assert( type_code == CCTK_VARIABLE_REAL );
assert( N_elements == 1 );

free(key_buffer);
  }
  }
  }
}

/******************************************************************************/

/*
 * This function tests deleting table entries.
 * It assumes the initial table contents are those generated by
 * test_get_set() , namely 3 keys {"REAL_y", "COMPlex_Z", "int_x"}.
 *
 * Bugs:
 * This test is tied to the present implementation -- it assumes a
 * specific ordering of table elements returned by an iterator.
 */
static
  void test_delete_key(int handle, bool case_insensitive)
{
/* set up the key buffer */
int max_key_length = Util_TableQueryMaxKeyLength(handle);
assert( max_key_length == (int)strlen("COMPlex_Z") );
  {
const int N_key_buffer = max_key_length + 1;
char *const key_buffer = malloc(N_key_buffer);
assert( key_buffer != NULL );

/*
 * delete the starting table entry "REAL_y"
 * (this is a special case in the implementation)
 */

assert( Util_TableQueryNKeys(handle) == 3 );
assert( Util_TableDeleteKey(handle,
                            case_insensitive ? "rEAL_y" : "REAL_y")
        == 0 );
assert( Util_TableQueryNKeys(handle) == 2 );

/* walk the table again to verify remaining keys {"COMPlex_Z", "int_x"} */
assert( Util_TableQueryNKeys(handle) == 2 );
  {
int ihandle = Util_TableItCreate(handle);
assert( ihandle >= 0 );
assert( Util_TableItQueryTableHandle(ihandle) == handle );
assert( Util_TableItQueryIsNonNull(ihandle) == 1);
assert( Util_TableItQueryIsNull(ihandle) == 0);

/* COMPlex_Z */
  {
CCTK_INT type_code = 123456;
CCTK_INT N_elements = 54321;
assert( Util_TableItQueryKeyValueInfo(ihandle,
                                      N_key_buffer, key_buffer,
                                      &type_code, &N_elements)
	== (int)strlen("COMPlex_Z") );
assert( strcmp(key_buffer, "COMPlex_Z") == 0 );
assert( type_code = CCTK_VARIABLE_COMPLEX );
assert( N_elements == 1 );

/* int_x */
type_code = 123456;
N_elements = 54321;
assert( Util_TableItAdvance(ihandle) == 1 );
assert( Util_TableItQueryKeyValueInfo(ihandle,
                                      N_key_buffer, key_buffer,
                                      &type_code, &N_elements)
	== (int)strlen("int_x") );
assert( strcmp(key_buffer, "int_x") == 0 );
assert( type_code = CCTK_VARIABLE_INT );
assert( N_elements == 1 );

/* advance past last table entry ==> "null-pointer" state */
assert( Util_TableItAdvance(ihandle) == 0 );
assert( Util_TableItQueryIsNull(ihandle) == 1);
assert( Util_TableItQueryIsNonNull(ihandle) == 0);

/* delete the last key "int_x" */
assert( Util_TableDeleteKey(handle,
                            case_insensitive ? "INT_X" : "int_x")
        == 0 );

/* walk the table again to verify remaining key {"COMPlex_Z"} */
assert( Util_TableQueryNKeys(handle) == 1 );
  {
int ihandle2 = Util_TableItCreate(handle);
assert( ihandle2 >= 0 );
assert( Util_TableItQueryTableHandle(ihandle2) == handle );
assert( Util_TableItQueryIsNonNull(ihandle2) == 1);
assert( Util_TableItQueryIsNull(ihandle2) == 0);

/* COMPlex_Z */
type_code = 123456;
N_elements = 54321;
assert( Util_TableItQueryKeyValueInfo(ihandle2,
                                      N_key_buffer, key_buffer,
                                      &type_code, &N_elements)
	== (int)strlen("COMPlex_Z") );
assert( strcmp(key_buffer, "COMPlex_Z") == 0 );
assert( type_code = CCTK_VARIABLE_COMPLEX );
assert( N_elements == 1 );

/* advance past last table entry ==> "null-pointer" state */
assert( Util_TableItAdvance(ihandle2) == 0 );
assert( Util_TableItQueryIsNull(ihandle2) == 1);
assert( Util_TableItQueryIsNonNull(ihandle2) == 0);

/* delete the last key "COMPlex_Z" */
assert( Util_TableQueryNKeys(handle) == 1 );
assert( Util_TableDeleteKey(handle,
                            case_insensitive ? "INT_X" : "int_x")
        == UTIL_ERROR_TABLE_NO_SUCH_KEY );
assert( Util_TableQueryNKeys(handle) == 1 );
assert( Util_TableDeleteKey(handle,
                            case_insensitive ? "compLEX_z" : "COMPlex_Z")
        == 0 );
assert( Util_TableQueryNKeys(handle) == 0 );

  {
/* check that table is indeed now empty */
int ihandle3 = Util_TableItCreate(handle);
assert( ihandle3 >= 0 );
assert( Util_TableItQueryIsNull(ihandle3) == 1);
assert( Util_TableItQueryIsNonNull(ihandle3) == 0);

/* clean up our iterators */
assert( Util_TableItDestroy(ihandle2) == 0 );
assert( Util_TableItDestroy(42) == UTIL_ERROR_BAD_HANDLE );
assert( Util_TableItDestroy(ihandle3) == 0 );
assert( Util_TableItDestroy(ihandle) == 0 );
free(key_buffer);
  }
  }
  }
  }
  }
}
#endif  /* UTIL_TABLE_TEST */

/******************************************************************************/

#ifdef UTIL_TABLE_TEST
/*
 * This function tests  Util_TableCreateFromString() .
 * It returns the handle of the newly-created table.
 */
static
  int test_create_from_string(void)
{
/*
 * Test an empty string
 */
  {
int handle2 = Util_TableCreateFromString("");
assert( Util_TableQueryNKeys(handle2) == 0 );

/*
 * Test some error cases
 */
assert( Util_TableCreateFromString("foo" ) == UTIL_ERROR_BAD_INPUT );
assert( Util_TableCreateFromString("foo/" ) == UTIL_ERROR_BAD_INPUT );
assert( Util_TableCreateFromString("foo/=12" ) == UTIL_ERROR_TABLE_BAD_KEY );
assert( Util_TableCreateFromString("foo= 12") == UTIL_ERROR_BAD_INPUT );

/*
 * Test a "good" string
 */
  {
int handle = Util_TableCreateFromString("ij=42 real1=3.5; real_e=2.75");
assert( handle >= 0 );
assert( Util_TableQueryFlags(handle)
        == (  UTIL_TABLE_FLAGS_CASE_INSENSITIVE
            | UTIL_TABLE_FLAGS_AUTO_DESTROY    ) );

assert( Util_TableQueryNKeys(handle) == 3 );

/* set up the key buffer */
  {
int max_key_length = Util_TableQueryMaxKeyLength(handle);
assert( max_key_length == (int)strlen("real_e") );
  {
const int N_key_buffer = max_key_length + 1;
char *const key_buffer = malloc(N_key_buffer);
assert( key_buffer != NULL );

/* walk through the table to verify contents {"real_e", "real1", "ij"} */
  {
int ihandle = Util_TableItCreate(handle);
CCTK_INT key_length, type_code, N_elements;

/* real_e = 2.75 */
type_code = 123456;
N_elements = 54321;
assert( Util_TableItQueryKeyValueInfo(ihandle,
                                      N_key_buffer, key_buffer,
                                      &type_code, &N_elements)
	== (int)strlen("real_e") );
assert( strcmp(key_buffer, "real_e") == 0 );
assert( type_code = CCTK_VARIABLE_REAL );
assert( N_elements == 1 );
  {
CCTK_REAL value_real;
assert( Util_TableGetReal(handle, &value_real, key_buffer) == 1 );
assert( value_real == 2.75 );

/* real1 = 3.5 */
assert( Util_TableItAdvance(ihandle) == 1 );
type_code = 123456;
N_elements = 54321;
assert( Util_TableItQueryKeyValueInfo(ihandle,
                                      N_key_buffer, key_buffer,
                                      &type_code, &N_elements)
	== (int)strlen("real1") );
assert( strcmp(key_buffer, "real1") == 0 );
assert( type_code = CCTK_VARIABLE_REAL );
assert( N_elements == 1 );
assert( Util_TableGetReal(handle, &value_real, key_buffer) == 1 );
assert( value_real == 3.5 );

/* ij = 42 */
assert( Util_TableItAdvance(ihandle) == 1 );
type_code = 123456;
N_elements = 54321;
assert( Util_TableItQueryKeyValueInfo(ihandle,
                                      N_key_buffer, key_buffer,
                                      &type_code, &N_elements)
	== (int)strlen("ij") );
assert( strcmp(key_buffer, "ij") == 0 );
assert( type_code = CCTK_VARIABLE_REAL );
assert( N_elements == 1 );
  {
CCTK_INT value_int;
assert( Util_TableGetInt(handle, &value_int, key_buffer) == 1 );
assert( value_int == 42 );

assert( Util_TableItAdvance(ihandle) == 0 );

return handle;
  }
  }
  }
  }
  }
  }
  }
}
#endif  /* UTIL_TABLE_TEST */

/******************************************************************************/

#ifdef UTIL_TABLE_TEST
/*
 * This function tests  Util_Table{Set,Get}String()
 */
static
  void test_set_get_string(int handle, bool case_insensitive)
{
assert( Util_TableSetString(handle, "Germany", "AEI") == 0 );
assert( Util_TableSetString(handle, "Golm", "AEI") == 1 );

  {
CCTK_INT type_code, N_elements;
assert( Util_TableQueryValueInfo(handle,
                                 &type_code, &N_elements,
                                 case_insensitive ? "aei" : "AEI") == 1 );
assert( type_code = CCTK_VARIABLE_CHAR );
assert( N_elements = (int)strlen("Golm") );

  {
const int N_buffer = N_elements+1;
char *const buffer = (char *) malloc(N_buffer);
assert( buffer != NULL );
assert( Util_TableGetCharArray(handle,
                               N_buffer, buffer,
                               "AEI") == (int)strlen("Golm") );
assert( Util_TableGetString(handle,
                            0, NULL,
                            "AEI") == (int)strlen("Golm") );
assert( Util_TableGetString(handle,
                            N_buffer, buffer,
                            case_insensitive ? "aEI" : "AEI")
        == (int)strlen("Golm") );
assert( strcmp(buffer, "Golm") == 0 );

/* check getting string longer than buffer */
assert( Util_TableSetString(handle, "Max-Planck", "famous") == 0 );
type_code = 123;
N_elements = 456;
assert( Util_TableQueryValueInfo(handle,
                                 &type_code, &N_elements,
                                 case_insensitive ? "aei" : "AEI") == 1 );
assert( type_code = CCTK_VARIABLE_CHAR );
assert( N_elements = (int)strlen("Max-Planck") );
assert( Util_TableGetString(handle,
                            N_buffer, buffer,
                            case_insensitive ? "FAMouS" : "famous")
        == UTIL_ERROR_TABLE_STRING_TRUNCATED );
assert( strcmp(buffer, "Max-") == 0 );
  }
  }
}
#endif  /* UTIL_TABLE_TEST */