summaryrefslogtreecommitdiffstats
path: root/ast/table.c
blob: 0d283bc1fe7f84d6129aa4746617bbb32e44ea2d (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
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
/*
*class++
*  Name:
*     Table

*  Purpose:
*     A 2-dimensional table of values.

*  Constructor Function:
c     astTable
f     AST_TABLE

*  Description:
*     The Table class is a type of KeyMap that represents a two-dimensional
*     table of values. The
c     astMapGet... and astMapPut...
f     AST_MAPGET... and AST_MAPPUT...
*     methods provided by the KeyMap class should be used for storing and
*     retrieving values from individual cells within a Table. Each entry
*     in the KeyMap represents a single cell of the table and has an
*     associated key of the form "<COL>(i)" where "<COL>" is the
*     upper-case name of a table column and "i" is the row index (the
*     first row is row 1). Keys of this form should always be used when
*     using KeyMap methods to access entries within a Table.
*
*     Columns must be declared using the
c     astAddColumn
f     AST_ADDCOLUMN
*     method before values can be stored within them. This also fixes the
*     type and shape of the values that may be stored in any cell of the
*     column. Cells may contain scalar or vector values of any data type
*     supported by the KeyMap class. Multi-dimensional arrays may also be
*     stored, but these must be vectorised when storing and retrieving
*     them within a table cell. All cells within a single column must
*     have the same type and shape, as specified when the column is added
*     to the Table.
*
*     Tables may have parameters that describe global properties of the
*     entire table. These are stored as entries in the parent KeyMap and
*     can be access using the get and set method of the KeyMap class.
*     However, parameters must be declared using the
c     astAddParameter
f     AST_ADDPARAMETER
*     method before being accessed.
*
*     Note - since accessing entries within a KeyMap is a relatively slow
*     process, it is not recommended to use the Table class to store
*     very large tables.

*  Inheritance:
*     The Table class inherits from the KeyMap class.

*  Attributes:
*     In addition to those attributes common to all KeyMaps, every
*     Table also has the following attributes:
*
*     - ColumnLenC(column): The largest string length of any value in a column
*     - ColumnLength(column): The number of elements in each value in a column
*     - ColumnNdim(column): The number of axes spanned by each value in a column
*     - ColumnType(column): The data type of each value in a column
*     - ColumnUnit(column): The unit string describing each value in a column
*     - Ncolumn: The number of columns currently in the Table
*     - Nrow: The number of rows currently in the Table
*     - Nparameter: The number of global parameters currently in the Table

*  Functions:
c     In addition to those functions applicable to all KeyMaps, the
c     following functions may also be applied to all Tables:
f     In addition to those routines applicable to all KeyMaps, the
f     following routines may also be applied to all Tables:
*
c     - astAddColumn: Add a new column definition to a Table
c     - astAddParameter: Add a new global parameter definition to a Table
c     - astColumnName: Return the name of the column with a given index
c     - astColumnShape: Return the shape of the values in a named column
c     - astHasColumn: Checks if a column exists in a Table
c     - astHasParameter: Checks if a global parameter exists in a Table
c     - astParameterName: Return the name of the parameter with a given index
c     - astPurgeRows: Remove all empty rows from a Table
c     - astRemoveColumn: Remove a column from a Table
c     - astRemoveParameter: Remove a global parameter from a Table
c     - astRemoveRow: Remove a row from a Table
f     - AST_ADDCOLUMN: Add a new column definition to a Table
f     - AST_ADDPARAMETER: Add a new global parameter definition to a Table
f     - AST_COLUMNNAME: Return the name of the column with a given index
f     - AST_COLUMNSHAPE: Return the shape of the values in a named column
f     - AST_HASCOLUMN: Checks if a column exists in a Table
f     - AST_HASPARAMETER: Checks if a global parameter exists in a Table
f     - AST_PARAMETERNAME: Return the name of the parameter with a given index
f     - AST_PURGEROWS: Remove all empty rows from a Table
f     - AST_REMOVECOLUMN: Remove a column from a Table
f     - AST_REMOVEPARAMETER: Remove a global parameter from a Table
f     - AST_REMOVEROW: Remove a row from a Table

*  Copyright:
*     Copyright (C) 2010-2011 Science & Technology Facilities Council.
*     All Rights Reserved.

*  Licence:
*     This program is free software: you can redistribute it and/or
*     modify it under the terms of the GNU Lesser General Public
*     License as published by the Free Software Foundation, either
*     version 3 of the License, or (at your option) any later
*     version.
*     
*     This program is distributed in the hope that it will be useful,
*     but WITHOUT ANY WARRANTY; without even the implied warranty of
*     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*     GNU Lesser General Public License for more details.
*     
*     You should have received a copy of the GNU Lesser General
*     License along with this program.  If not, see
*     <http://www.gnu.org/licenses/>.

*  Authors:
*     DSB: David S. Berry (Starlink)

*  History:
*     22-NOV-2010 (DSB):
*        Original version.
*     13-MAY-2011 (DSB):
*        Added support for table parameters.
*     16-NOV-2013 (DSB):
*        Fix bug in forming keys in GetColumnLenC.
*class--
*/

/* Module Macros. */
/* ============== */
/* Set the name of the class we are implementing. This indicates to
   the header files that define class interfaces that they should make
   "protected" symbols available. */
#define astCLASS Table

/* Fixed KeyMap entry names */
#define LENGTH "Length"
#define NAME "Name"
#define NROW "Nrow"
#define SHAPE "Shape"
#define SIZE "Size"
#define TYPE "Type"
#define UNIT "Unit"

/* A function macro that puts quotes around a value */
#define STRING(w) #w

/* Include files. */
/* ============== */
/* Interface definitions. */
/* ---------------------- */

#include "globals.h"             /* Thread-safe global data access */
#include "error.h"               /* Error reporting facilities */
#include "memory.h"              /* Memory allocation facilities */
#include "object.h"              /* Base Object class */
#include "keymap.h"              /* Coordinate keymaps (parent class) */
#include "channel.h"             /* I/O channels */
#include "table.h"               /* Interface definition for this class */


/* Error code definitions. */
/* ----------------------- */
#include "ast_err.h"             /* AST error codes */

/* C header files. */
/* --------------- */
#include <ctype.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>


/* Module Variables. */
/* ================= */

/* Address of this static variable is used as a unique identifier for
   member of this class. */
static int class_check;

/* Pointers to parent class methods which are extended by this class. */
static const char *(* parent_getattrib)( AstObject *, const char *, int * );
static void (* parent_setkeycase)( AstKeyMap *, int, int * );
static void (* parent_clearkeycase)( AstKeyMap *, int * );
static int (* parent_equal)( AstObject *, AstObject *, int * );
static int (* parent_getobjsize)( AstObject *, int * );
static int (* parent_mapget0a)( AstKeyMap *, const char *, AstObject * *, int *);
static int (* parent_mapget0c)( AstKeyMap *, const char *, const char **, int *);
static int (* parent_mapget0d)( AstKeyMap *, const char *, double *, int *);
static int (* parent_mapget0f)( AstKeyMap *, const char *, float *, int *);
static int (* parent_mapget0i)( AstKeyMap *, const char *, int *, int *);
static int (* parent_mapget0p)( AstKeyMap *, const char *, void **, int *);
static int (* parent_mapget0b)( AstKeyMap *, const char *, unsigned char *, int *);
static int (* parent_mapget0s)( AstKeyMap *, const char *, short int *, int *);
static int (* parent_mapget1a)( AstKeyMap *, const char *, int, int *, AstObject **, int * );
static int (* parent_mapget1c)( AstKeyMap *, const char *, int, int, int *, char *, int * );
static int (* parent_mapget1d)( AstKeyMap *, const char *, int, int *, double *, int * );
static int (* parent_mapget1f)( AstKeyMap *, const char *, int, int *, float *, int * );
static int (* parent_mapget1i)( AstKeyMap *, const char *, int, int *, int *, int * );
static int (* parent_mapget1p)( AstKeyMap *, const char *, int, int *, void **, int * );
static int (* parent_mapget1s)( AstKeyMap *, const char *, int, int *, short int *, int * );
static int (* parent_mapget1b)( AstKeyMap *, const char *, int, int *, unsigned char *, int * );
static int (* parent_mapgetelema)( AstKeyMap *, const char *, int, AstObject **, int * );
static int (* parent_mapgetelemc)( AstKeyMap *, const char *, int, int, char *, int * );
static int (* parent_mapgetelemd)( AstKeyMap *, const char *, int, double *, int * );
static int (* parent_mapgetelemf)( AstKeyMap *, const char *, int, float *, int * );
static int (* parent_mapgetelemi)( AstKeyMap *, const char *, int, int *, int * );
static int (* parent_mapgetelemp)( AstKeyMap *, const char *, int, void **, int * );
static int (* parent_mapgetelems)( AstKeyMap *, const char *, int, short int *, int * );
static int (* parent_mapgetelemb)( AstKeyMap *, const char *, int, unsigned char *, int * );
static int (* parent_testattrib)( AstObject *, const char *, int * );
static void (* parent_clearattrib)( AstObject *, const char *, int * );
static void (* parent_mapput0a)( AstKeyMap *, const char *, AstObject *, const char *, int *);
static void (* parent_mapput0c)( AstKeyMap *, const char *, const char *, const char *, int *);
static void (* parent_mapput0d)( AstKeyMap *, const char *, double, const char *, int *);
static void (* parent_mapput0f)( AstKeyMap *, const char *, float, const char *, int *);
static void (* parent_mapput0i)( AstKeyMap *, const char *, int, const char *, int *);
static void (* parent_mapput0p)( AstKeyMap *, const char *, void *, const char *, int *);
static void (* parent_mapput0b)( AstKeyMap *, const char *, unsigned char, const char *, int *);
static void (* parent_mapput0s)( AstKeyMap *, const char *, short int, const char *, int *);
static void (* parent_mapput1a)( AstKeyMap *, const char *, int, AstObject *const [], const char *, int * );
static void (* parent_mapput1c)( AstKeyMap *, const char *, int, const char *const [], const char *, int * );
static void (* parent_mapput1d)( AstKeyMap *, const char *, int, const double *, const char *, int * );
static void (* parent_mapput1f)( AstKeyMap *, const char *, int, const float *, const char *, int * );
static void (* parent_mapput1i)( AstKeyMap *, const char *, int, const int *, const char *, int * );
static void (* parent_mapput1p)( AstKeyMap *, const char *, int, void *const [], const char *, int * );
static void (* parent_mapput1b)( AstKeyMap *, const char *, int, const unsigned char *, const char *, int * );
static void (* parent_mapput1s)( AstKeyMap *, const char *, int, const short int *, const char *, int * );
static void (* parent_mapputelema)( AstKeyMap *, const char *, int, AstObject *, int * );
static void (* parent_mapputelemc)( AstKeyMap *, const char *, int, const char *, int * );
static void (* parent_mapputelemd)( AstKeyMap *, const char *, int, double, int * );
static void (* parent_mapputelemf)( AstKeyMap *, const char *, int, float, int * );
static void (* parent_mapputelemi)( AstKeyMap *, const char *, int, int, int * );
static void (* parent_mapputelemp)( AstKeyMap *, const char *, int, void *, int * );
static void (* parent_mapputelemb)( AstKeyMap *, const char *, int, unsigned char, int * );
static void (* parent_mapputelems)( AstKeyMap *, const char *, int, short int, int * );
static void (* parent_mapremove)( AstKeyMap *, const char *, int * );
static void (* parent_setattrib)( AstObject *, const char *, int * );
static void (* parent_mapputu)( AstKeyMap *, const char *, const char *, int * );

#if defined(THREAD_SAFE)
static int (* parent_managelock)( AstObject *, int, int, AstObject **, int * );
#endif


/* Define macros for accessing each item of thread specific global data. */
#ifdef THREAD_SAFE

/* Define how to initialise thread-specific globals. */
#define GLOBAL_inits \
   globals->Class_Init = 0; \
   globals->GetAttrib_Buff[ 0 ] = 0;

/* Create the function that initialises global data for this module. */
astMAKE_INITGLOBALS(Table)

/* Define macros for accessing each item of thread specific global data. */
#define class_init astGLOBAL(Table,Class_Init)
#define class_vtab astGLOBAL(Table,Class_Vtab)
#define getattrib_buff astGLOBAL(Table,GetAttrib_Buff)



/* If thread safety is not needed, declare and initialise globals at static
   variables. */
#else

static char getattrib_buff[ 101 ];

/* Define the class virtual function table and its initialisation flag
   as static variables. */
static AstTableVtab class_vtab;   /* Virtual function table */
static int class_init = 0;       /* Virtual function table initialised? */

#endif

/* External Interface Function Prototypes. */
/* ======================================= */
/* The following functions have public prototypes only (i.e. no
   protected prototypes), so we must provide local prototypes for use
   within this module. */
AstTable *astTableId_( const char *, ... );

/* Prototypes for Private Member Functions. */
/* ======================================== */
static AstKeyMap *ColumnProps( AstTable *, int * );
static AstKeyMap *ParameterProps( AstTable *, int * );
static const char *ColumnName( AstTable *, int index, int * );
static const char *ParameterName( AstTable *, int index, int * );
static const char *GetColumnUnit( AstTable *, const char *, int * );
static const char *TypeString( int );
static int Equal( AstObject *, AstObject *, int * );
static int GetColumnLenC( AstTable *, const char *, int * );
static int GetColumnLength( AstTable *, const char *, int * );
static int GetColumnNdim( AstTable *, const char *, int * );
static int GetColumnType( AstTable *, const char *, int * );
static int GetNcolumn( AstTable *, int * );
static int GetNparameter( AstTable *, int * );
static int GetObjSize( AstObject *, int * );
static int HasColumn( AstTable *, const char *, int *);
static int HasParameter( AstTable *, const char *, int *);
static int MapGet0A( AstKeyMap *, const char *, AstObject **, int * );
static int MapGet0B( AstKeyMap *, const char *, unsigned char *, int * );
static int MapGet0C( AstKeyMap *, const char *, const char **, int * );
static int MapGet0D( AstKeyMap *, const char *, double *, int * );
static int MapGet0F( AstKeyMap *, const char *, float *, int * );
static int MapGet0I( AstKeyMap *, const char *, int *, int * );
static int MapGet0P( AstKeyMap *, const char *, void **, int * );
static int MapGet0S( AstKeyMap *, const char *, short int *, int * );
static int MapGet1A( AstKeyMap *, const char *, int, int *, AstObject **, int * );
static int MapGet1B( AstKeyMap *, const char *, int, int *, unsigned char *, int * );
static int MapGet1C( AstKeyMap *, const char *, int, int, int *, char *, int * );
static int MapGet1D( AstKeyMap *, const char *, int, int *, double *, int * );
static int MapGet1F( AstKeyMap *, const char *, int, int *, float *, int * );
static int MapGet1I( AstKeyMap *, const char *, int, int *, int *, int * );
static int MapGet1P( AstKeyMap *, const char *, int, int *, void **, int * );
static int MapGet1S( AstKeyMap *, const char *, int, int *, short int *, int * );
static int MapGetElemA( AstKeyMap *, const char *, int, AstObject **, int * );
static int MapGetElemB( AstKeyMap *, const char *, int, unsigned char *, int * );
static int MapGetElemC( AstKeyMap *, const char *, int, int, char *, int * );
static int MapGetElemD( AstKeyMap *, const char *, int, double *, int * );
static int MapGetElemF( AstKeyMap *, const char *, int, float *, int * );
static int MapGetElemI( AstKeyMap *, const char *, int, int *, int * );
static int MapGetElemP( AstKeyMap *, const char *, int, void **, int * );
static int MapGetElemS( AstKeyMap *, const char *, int, short int *, int * );
static int ParseKey( AstTable *, const char *, int, char *, int *, AstKeyMap **, const char *, int * );
static void AddColumn( AstTable *, const char *, int, int, int *, const char *, int * );
static void AddParameter( AstTable *, const char *, int * );
static void ColumnShape( AstTable *, const char *, int, int *, int *, int *);
static void Copy( const AstObject *, AstObject *, int * );
static void Delete( AstObject *, int * );
static void Dump( AstObject *, AstChannel *, int * );
static void MapPut0A( AstKeyMap *, const char *, AstObject *, const char *, int * );
static void MapPut0B( AstKeyMap *, const char *, unsigned char, const char *, int * );
static void MapPut0C( AstKeyMap *, const char *, const char *, const char *, int * );
static void MapPut0D( AstKeyMap *, const char *, double, const char *, int * );
static void MapPut0F( AstKeyMap *, const char *, float, const char *, int * );
static void MapPut0I( AstKeyMap *, const char *, int, const char *, int * );
static void MapPut0P( AstKeyMap *, const char *, void *, const char *, int * );
static void MapPut0S( AstKeyMap *, const char *, short int, const char *, int * );
static void MapPut1A( AstKeyMap *, const char *, int, AstObject *const [], const char *, int * );
static void MapPut1B( AstKeyMap *, const char *, int, const unsigned char *, const char *, int * );
static void MapPut1C( AstKeyMap *, const char *, int, const char *const [], const char *, int * );
static void MapPut1D( AstKeyMap *, const char *, int, const double *, const char *, int * );
static void MapPut1F( AstKeyMap *, const char *, int, const float *, const char *, int * );
static void MapPut1I( AstKeyMap *, const char *, int, const int *, const char *, int * );
static void MapPut1P( AstKeyMap *, const char *, int, void *const [], const char *, int * );
static void MapPut1S( AstKeyMap *, const char *, int, const short int *, const char *, int * );
static void MapPutElemA( AstKeyMap *, const char *, int, AstObject *, int * );
static void MapPutElemB( AstKeyMap *, const char *, int, unsigned char, int * );
static void MapPutElemC( AstKeyMap *, const char *, int, const char *, int * );
static void MapPutElemD( AstKeyMap *, const char *, int, double, int * );
static void MapPutElemF( AstKeyMap *, const char *, int, float, int * );
static void MapPutElemI( AstKeyMap *, const char *, int, int, int * );
static void MapPutElemP( AstKeyMap *, const char *, int, void *, int * );
static void MapPutElemS( AstKeyMap *, const char *, int, short int, int * );
static void MapPutU( AstKeyMap *, const char *, const char *, int * );
static void PurgeRows( AstTable *, int * );
static void RemoveColumn( AstTable *, const char *, int * );
static void RemoveParameter( AstTable *, const char *, int * );
static void RemoveRow( AstTable *, int, int * );
static void SetKeyCase( AstKeyMap *, int, int * );
static void ClearKeyCase( AstKeyMap *, int * );

#if defined(THREAD_SAFE)
static int ManageLock( AstObject *, int, int, AstObject **, int * );
#endif

static const char *GetAttrib( AstObject *, const char *, int * );
static int TestAttrib( AstObject *, const char *, int * );
static void ClearAttrib( AstObject *, const char *, int * );
static void SetAttrib( AstObject *, const char *, int * );

static int GetNrow( AstTable *, int * );
static void SetNrow( AstTable *, int, int * );

static int GetNcolumn( AstTable *, int * );
static int GetNparameter( AstTable *, int * );


/* Member functions. */
/* ================= */
static void AddColumn( AstTable *this, const char *name, int type,
                       int ndim, int *dims, const char *unit, int *status ) {
/*
*++
*  Name:
c     astAddColumn
f     AST_ADDCOLUMN

*  Purpose:
*     Add a new column definition to a table.

*  Type:
*     Public virtual function.

*  Synopsis:
c     #include "table.h"
c     void astAddColumn( AstTable *this, const char *name, int type, int ndim,
c                        int *dims, const char *unit )
f     CALL AST_ADDCOLUMN( THIS, NAME, TYPE, NDIM, DIMS, UNIT, STATUS )

*  Class Membership:
*     Table method.

*  Description:
*     Adds the definition of a new column to the supplied table. Initially,
*     the column is empty. Values may be added subsequently using the
*     methods of the KeyMap class.

*  Parameters:
c     this
f     THIS = INTEGER (Given)
*        Pointer to the Table.
c     name
f     NAME = CHARACTER * ( * ) (Given)
*        The column name. Trailing spaces are ignored (all other spaces
*        are significant). The supplied string is converted to upper case.
c     type
f     TYPE = INTEGER (Given)
*        The data type associated with the column. See "Applicability:"
*        below.
c     ndim
f     NDIM = INTEGER (Given)
*        The number of dimensions spanned by the values stored in a single
*        cell of the column. Zero if the column holds scalar values.
c     dims
f     DIMS( NDIM ) = INTEGER (Given)
*        An array holding the the lengths of each of the axes spanned by
*        the values stored in a single cell of the column. Ignored if the
*        column holds scalara values.
c     unit
f     UNIT = CHARACTER * ( * ) (Given)
*        A string specifying the units of the column. Supply a blank
*        string if the column is unitless.
f     STATUS = INTEGER (Given and Returned)
f        The global status.

*  Applicability:
*     Table
*        Tables can hold columns with any of the following data types -
*        AST__INTTYPE (for integer), AST__SINTTYPE (for
c        short int),
f        INTEGER*2),
*        AST__BYTETYPE (for
c        unsigned bytes - i.e. unsigned chars),
f        bytes),
*        AST__DOUBLETYPE (for double
*        precision floating point), AST__FLOATTYPE (for single
*        precision floating point), AST__STRINGTYPE (for character string),
*        AST__OBJECTTYPE (for AST Object pointer), AST__POINTERTYPE (for
*        arbitrary C pointer) or AST__UNDEFTYPE (for undefined values
*        created by
c        astMapPutU).
f        AST_MAPPUTU).
*     FitsTable
*        FitsTables can hold columns with any of the following data types -
*        AST__INTTYPE (for integer), AST__SINTTYPE (for
c        short int),
f        INTEGER*2),
*        AST__BYTETYPE (for
c        unsigned bytes - i.e. unsigned chars),
f        bytes),
*        AST__DOUBLETYPE (for double
*        precision floating point), AST__FLOATTYPE (for single
*        precision floating point), AST__STRINGTYPE (for character string).

*  Notes:
*     - This
c     function
f     routine
*     returns without action if a column already exists in the Table
*     with the supplied name and properties. However an error is
*     reported if any of the properties differ.

*--
*/

/* Local Variables: */
   AstKeyMap *cols;      /* KeyMap holding all column details */
   AstKeyMap *col_km;    /* KeyMap holding new column details */
   const char *oldunit;  /* Pointer to the old coumn unit string */
   int *olddims;         /* Shape of pre-existing column */
   int idim;             /* Axis index */
   int namlen;           /* Used length of "name" */
   int nval;             /* Number of values returned */
   int oldtype;          /* Data type of pre-existing column */

/* Check the global error status. */
   if ( !astOK ) return;

/* Verify supplied values. */
   namlen = astChrLen( name );
   if( namlen == 0 ) {
      astError( AST__BADKEY, "astAddColumn(%s): Illegal blank column name "
               "supplied.", status, astGetClass( this ) );

   } else if( namlen > AST__MXCOLNAMLEN ) {
      astError( AST__BADKEY, "astAddColumn(%s): Column name '%s' is too "
               "long (must be no more than %d characters).", status,
               astGetClass( this ), name, AST__MXCOLNAMLEN );

   } else if( ndim < 0 ) {
      astError( AST__NAXIN, "astAddColumn(%s): No of axes (%d) for values in "
               "new column %s is invalid.", status, astGetClass( this ),
               ndim, name );

   } else if( TypeString( type ) == NULL ) {
      astError( AST__NAXIN, "astAddColumn(%s): Bad data type supplied (%d) "
                "for new column %s.", status, astGetClass( this ), type,
                name );

   } else {
      for( idim = 0; idim < ndim; idim++ ) {
         if( dims[ idim ] < 1 ) {
            astError( AST__DIMIN, "astAddColumn(%s): Length of axis %d (%d) "
                      "for new column %s is invalid.", status,
                      astGetClass( this ), idim + 1, dims[ idim ], name );
            break;
         }
      }
   }

/* If there is already a column with the given name, check its properties
   match the supplied properties. */
   if( astOK ) {
      cols = astColumnProps( this );
      if( astMapGet0A( cols, name, &col_km ) ) {

         astMapGet0I( col_km, TYPE, &oldtype );
         if( oldtype != type && astOK ) {
            astError( AST__OLDCOL, "astAddColumn(%s): A column called "
                      "%s already exists in the table with a different "
                      "data type (%s).", status, astGetClass( this ),
                      name, TypeString( oldtype ) );
         }

         if( !astMapGet0C( col_km, UNIT, &oldunit ) ) oldunit = "";
         if( strcmp( oldunit, unit ) && astOK ) {
            astError( AST__OLDCOL, "astAddColumn(%s): A column called "
                      "%s already exists in the table with a different "
                      "unit string ('%s').", status, astGetClass( this ),
                      name, oldunit );
         }

         if( ndim != astMapLength( col_km, SHAPE ) && astOK ) {
            astError( AST__OLDCOL, "astAddColumn(%s): A column called "
                      "%s already exists in the table with a different "
                      "number of axes (%d).", status, astGetClass( this ),
                      name, astMapLength( col_km, SHAPE ) );
         }

         if( ndim > 0 && astOK ) {
            olddims = astMalloc( sizeof( int )*ndim );
            (void) astMapGet1I( col_km, SHAPE, ndim, &nval, olddims );
            for( idim = 0; idim < ndim && astOK; idim++ ) {
               if( dims[ idim ] != olddims[ idim ] ) {
                  astError( AST__OLDCOL, "astAddColumn(%s): A column called "
                            "%s already exists in the table with a different "
                            "shape.", status, astGetClass( this ), name );
               }
            }
            olddims = astFree( olddims );
         }

/* Otherwise, add a new column to the table. */
      } else {

/* Add a suitable entry describing the column to the Columns KeyMap. */
         col_km = astKeyMap( " ", status );
         astMapPut0C( col_km, NAME, name, NULL );
         astMapPut0I( col_km, TYPE, type, NULL );
         if( ndim ) astMapPut1I( col_km, SHAPE, ndim, dims, NULL );
         astMapPut0C( col_km, UNIT, unit, NULL );

/* Put the column KeyMap into the KeyMap holding details of all columns.
   Use the column name as the key. */
         astMapPut0A( cols, name, col_km, NULL );
      }

/* Annul the local KeyMap pointers. */
      col_km = astAnnul( col_km );
      cols = astAnnul( cols );
   }
}

static void AddParameter( AstTable *this, const char *name, int *status ) {
/*
*++
*  Name:
c     astAddParameter
f     AST_ADDPARAMETER

*  Purpose:
*     Add a new global parameter definition to a table.

*  Type:
*     Public virtual function.

*  Synopsis:
c     #include "table.h"
c     void astAddParameter( AstTable *this, const char *name )
f     CALL AST_ADDPARAMETER( THIS, NAME, STATUS )

*  Class Membership:
*     Table method.

*  Description:
*     Adds the definition of a new global parameter to the supplied
*     table. Note, this does not store a value for the parameter. To get
*     or set the parameter value, the methods of the paremt KeyMap class
*     should be used, using the name of the parameter as the key.

*  Parameters:
c     this
f     THIS = INTEGER (Given)
*        Pointer to the Table.
c     name
f     NAME = CHARACTER * ( * ) (Given)
*        The parameter name. Trailing spaces are ignored (all other spaces
*        are significant). The supplied string is converted to upper case.
f     STATUS = INTEGER (Given and Returned)
f        The global status.

*  Notes:
*     - Unlike columns, the definition of a parameter does not specify its type,
*     size or dimensionality.

*--
*/

/* Local Variables: */
   AstKeyMap *pars;      /* KeyMap holding all parameter details */
   int namlen;           /* Used length of "name" */

/* Check the global error status. */
   if ( !astOK ) return;

/* Verify supplied values. */
   namlen = astChrLen( name );
   if( namlen == 0 ) {
      astError( AST__BADKEY, "astAddParameter(%s): Illegal blank parameter name "
               "supplied.", status, astGetClass( this ) );

   } else if( namlen > AST__MXCOLNAMLEN ) {
      astError( AST__BADKEY, "astAddParameter(%s): Parameter name '%s' is too "
               "long (must be no more than %d characters).", status,
               astGetClass( this ), name, AST__MXCOLNAMLEN );
   }

/* Do nothing if there is already a parameter with the given name. */
   if( astOK ) {
      pars = astParameterProps( this );
      if( !astMapHasKey( pars, name ) ) {

/* Add a suitable entry to the Parameters KeyMap. The value is arbitrary
   and currently unused. */
         astMapPut0I( pars, name, 1, NULL );
      }

/* Annul the local KeyMap pointer. */
      pars = astAnnul( pars );
   }
}

static void ClearAttrib( AstObject *this_object, const char *attrib, int *status ) {
/*
*  Name:
*     ClearAttrib

*  Purpose:
*     Clear an attribute value for a Table.

*  Type:
*     Private function.

*  Synopsis:
*     #include "table.h"
*     void ClearAttrib( AstObject *this, const char *attrib )

*  Class Membership:
*     Table member function (over-rides the astClearAttrib protected
*     method inherited from the KeyMap class).

*  Description:
*     This function clears the value of a specified attribute for a
*     Table, so that the default value will subsequently be used.

*  Parameters:
*     this
*        Pointer to the Table.
*     attrib
*        Pointer to a null terminated string specifying the attribute
*        name.  This should be in lower case with no surrounding white
*        space.
*/

/* Local Variables: */
   AstTable *this;
   int nc;
   int len;

/* Check the global error status. */
   if ( !astOK ) return;

/* Obtain a pointer to the Table structure. */
   this = (AstTable *) this_object;

/* Get the length of the attribute string. */
   len = strlen( attrib );

/* Check the attribute name and clear the appropriate attribute. */
   /* None yet */

/* Define a macro to see if the attribute string matches any of the
   read-only column attributes of this class. */
#define MATCH(attr) \
        ( nc = 0, ( 0 == astSscanf( attrib, attr "(%*s)%n", &nc ) ) && \
                  ( nc >= len ) )

/* If the name was not recognised, test if it matches any of the
   read-only attributes of this class. If it does, then report an
   error. */
   if ( !strcmp( attrib, "nrow" ) ||
        !strcmp( attrib, "ncolumn" ) ||
        !strcmp( attrib, "nparameter" ) ||
        MATCH( "columnlenc" ) ||
        MATCH( "columnlength" ) ||
        MATCH( "columnndim" ) ||
        MATCH( "columntype" ) ||
        MATCH( "columnunit" ) ) {
      astError( AST__NOWRT, "astClear: Invalid attempt to clear the \"%s\" "
                "value for a %s.", status, attrib, astGetClass( this ) );
      astError( AST__NOWRT, "This is a read-only attribute." , status);

/* If the attribute is still not recognised, pass it on to the parent
   method for further interpretation. */
   } else {
      (*parent_clearattrib)( this_object, attrib, status );
   }

#undef MATCH

}

static void ClearKeyCase( AstKeyMap *this, int *status ) {
/*
*  Name:
*     ClearKeyCase

*  Purpose:
*     Clear the KeyCase attribute value for a Table.

*  Type:
*     Private function.

*  Synopsis:
*     #include "keymape.h"
*     void ClearKeyCase( AstKeyMap *this, int *status )

*  Class Membership:
*     Table member function (over-rides the astClearKeyCase protected
*     method inherited from the KeyMap class).

*  Description:
*     This function clears the value of the KeyCase attribute for a
*     Table. For a Table, the KeyCase attribute cannot be changed so this
*     function exits without action.

*  Parameters:
*     this
*        Pointer to the Table.
*/

}

static const char *ColumnName( AstTable *this, int index, int *status ) {
/*
*++
*  Name:
c     astColumnName
f     AST_COLUMNNAME

*  Purpose:
*     Get the name of the column at a given index within the Table.

*  Type:
*     Public virtual function.

*  Synopsis:
c     #include "table.h"
c     const char *astColumnName( AstTable *this, int index )
f     RESULT = AST_COLUMNNAME( THIS, INDEX, STATUS )

*  Class Membership:
*     Table method.

*  Description:
*     This function returns a string holding the name of the column with
*     the given index within the Table.
*
*     This function is intended primarily as a means of iterating round all
*     the columns in a Table. For this purpose, the number of columns in
*     the Table is given by the Ncolumn attribute of the Table. This function
*     could then be called in a loop, with the index value going from
c     zero to one less than Ncolumn.
f     one to Ncolumn.
*
*     Note, the index associated with a column decreases monotonically with
*     the age of the column: the oldest Column in the Table will have index
*     one, and the Column added most recently to the Table will have the
*     largest index.

*  Parameters:
c     this
f     THIS = INTEGER (Given)
*        Pointer to the Table.
c     index
f     INDEX = INTEGER (Given)
*        The index into the list of columns. The first column has index
*        one, and the last has index "Ncolumn".
f     STATUS = INTEGER (Given and Returned)
f        The global status.

*  Returned Value:
c     astColumnName()
c        A pointer to a null-terminated string containing the
f     AST_COLUMNNAME = CHARACTER * ( AST__SZCHR )
f        The
*        upper case column name.

*  Notes:
c     - The returned pointer is guaranteed to remain valid and the
c     string to which it points will not be over-written for a total
c     of 50 successive invocations of this function. After this, the
c     memory containing the string may be re-used, so a copy of the
c     string should be made if it is needed for longer than this.
c     - A NULL pointer will be returned if this function is invoked
c     with the AST error status set, or if it should fail for any
c     reason.
f     - A blank string will be returned if this function is invoked
f     with STATUS set to an error value, or if it should fail for any
f     reason.
*--
*/

/* Local Variables: */
   AstKeyMap *cols;        /* KeyMap holding column definitions */
   const char *result;

/* Check the global error status. */
   if ( !astOK ) return NULL;

/* Get apointer to the KeyMap holding all column definitions. */
   cols = astColumnProps( this );

/* Issue a more useful error message than that issued by astMapKey if the
   index is invalid. */
   if( index < 1 || index > astMapSize( cols ) ) {
      astError( AST__MPIND, "astColumnName(%s): Cannot find column "
                "%d (zero-based) of the %s - invalid index.", status,
                astGetClass( this ), index, astGetClass( this ) );
   }

/* Get the column name. */
   result = astMapKey( cols, index - 1 );

/* Free resources. */
   cols = astAnnul( cols );

/* Return a pointer to the required column name. */
   return result;
}

static AstKeyMap *ColumnProps( AstTable *this, int *status ) {
/*
*+
*  Name:
*     astColumnProps

*  Purpose:
*     Returns a pointer to the KeyMap holding column properties.

*  Type:
*     Protected virtual function.

*  Synopsis:
*     #include "table.h"
*     AstKeyMap *astColumnProps( AstTable *this )

*  Class Membership:
*     Table method.

*  Description:
*     This function returns a pointer to the KeyMap that holds
*     definitions of all the coumns added to the Table.

*  Parameters:
*     this
*        Pointer to the Table.

*  Returned Value:
*        A pointer to the KeyMap. It shpould be annulled using astAnnul
*        when no longer needed.

*-
*/

/* Check the global error status. */
   if ( !astOK ) return NULL;

/* Return a cloned pointer to the required KeyMap. */
   return astClone( this->columns );
}

static void ColumnShape( AstTable *this, const char *column, int mxdim,
                         int *ndim, int *dims, int *status ){
/*
*++
*  Name:
c     astColumnShape
f     AST_COLUMNSHAPE

*  Purpose:
*     Returns the shape of the values in a named column.

*  Type:
*     Public virtual function.

*  Synopsis:
c     #include "table.h"
c     void astColumnShape( AstTable *this, const char *column, int mxdim,
c                          int *ndim, int *dims )
f     CALL AST_COLUMNSHAPE( THIS, COLUMN, MXDIM, NDIM, DIMS, STATUS )

*  Class Membership:
*     Table method.

*  Description:
c     This function
f     This routine
*     returns the number of dimensions spaned by each value in a named
*     column of a Table, together with the length of each dimension.
*     These are the values supplied when the column was created using
c     astAddColumn.
f     AST_ADDCOLUMN.

*  Parameters:
c     this
f     THIS = INTEGER (Given)
*        Pointer to the Table.
c     column
f     COLUMN = CHARACTER * ( * ) (Given)
*        The character string holding the upper case name of the column. Trailing
*        spaces are ignored.
c     mxdim
f     MXDIM = INTEGER (Given)
*        The length of the
c        "dims" array.
f        DIMS array.
c     ndim
f     NDIM = INTEGER (Returned)
c        Pointer to an int in which to return the
f        The
*        number of dimensions spanned by values in the named column.
*        This will be zero if the column contains scalar values.
c     dims
f     DIMS( MXDIM ) = INTEGER (Returned)
c        Pointer to an
f        An
*        array in which to return the length of each dimension. Any
*        excess trailing elements will be filled with the value 1.
f     STATUS = INTEGER (Given and Returned)
f        The global status.

*  Notes:
*     - No error is reported if the requested column cannot be found in the
*     given Table. A value of zero is returned for
c     "ndim" and the supplied values in "dims"
f     NDIM and the supplied values in DIMS
*     are left unchanged.
*     - A value of zero is returned for
c     "ndim"
f     NDIM
*     if an error occurs.

*--
*/

/* Local Variables: */
   AstKeyMap *cols;        /* Pointer to KeyMap holding all column info */
   AstKeyMap *col_km;      /* Pointer to KeyMap holding requested column info */
   int idim;               /* Axis index */

/* Initialise */
   *ndim = 0;

/* Check the inherited status. */
   if( !astOK ) return;


/* Get the KeyMap holding information about the requested column. */
   cols = astColumnProps( this );
   if( astMapGet0A( cols, column, &col_km ) ) {

/* Get the shape of the column values. */
      (void) astMapGet1I( col_km, SHAPE, mxdim, ndim, dims );

/* Fill excess array elements with 1. */
      for( idim = *ndim; idim < mxdim; idim++ ) dims[ idim ] = 1;

/* Free resources. */
      col_km = astAnnul( col_km );
   }
   cols = astAnnul( cols );

/* If an error has occurred, set ndim to zero. */
   if( !astOK ) *ndim = 0;

}

static int Equal( AstObject *this_object, AstObject *that_object, int *status ) {
/*
*  Name:
*     Equal

*  Purpose:
*     Test if two Tables are equivalent.

*  Type:
*     Private function.

*  Synopsis:
*     #include "table.h"
*     int Equal( AstObject *this, AstObject *that, int *status )

*  Class Membership:
*     Table member function (over-rides the astEqual protected
*     method inherited from the astKeyMap class).

*  Description:
*     This function returns a boolean result (0 or 1) to indicate whether
*     two Tables are equivalent.

*  Parameters:
*     this
*        Pointer to the first Object (a Table).
*     that
*        Pointer to the second Object.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     One if the Tables are equivalent, zero otherwise.

*  Notes:
*     - A value of zero will be returned if this function is invoked
*     with the global status set, or if it should fail for any reason.
*/

/* Local Variables: */
   AstKeyMap *this_km;
   AstKeyMap *that_km;
   AstTable *that;
   AstTable *this;
   int result;

/* Initialise. */
   result = 0;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Obtain pointers to the two Table structures. */
   this = (AstTable *) this_object;
   that = (AstTable *) that_object;

/* Check the second object is a Table. We know the first is a
   Table since we have arrived at this implementation of the virtual
   function. */
   if( astIsATable( that ) ) {

/* Check the Tables are equal when compared as KeyMaps. */
      if( (*parent_equal)( this_object, that_object, status ) ) {

/* Check the Columns KeyMaps are equal.  */
         this_km = astColumnProps( this );
         that_km = astColumnProps( that );
         result = astEqual( this_km, that_km );
         this_km = astAnnul( this_km );
         that_km = astAnnul( that_km );

/* Check the Parameter KeyMaps are equal.  */
         this_km = astParameterProps( this );
         that_km = astParameterProps( that );
         result = astEqual( this_km, that_km );
         this_km = astAnnul( this_km );
         that_km = astAnnul( that_km );
      }
   }

/* If an error occurred, clear the result value. */
   if ( !astOK ) result = 0;

/* Return the result, */
   return result;
}

static const char *GetAttrib( AstObject *this_object, const char *attrib, int *status ) {
/*
*  Name:
*     GetAttrib

*  Purpose:
*     Get the value of a specified attribute for a Table.

*  Type:
*     Private function.

*  Synopsis:
*     #include "table.h"
*     const char *GetAttrib( AstObject *this, const char *attrib, int *status )

*  Class Membership:
*     Table member function (over-rides the protected astGetAttrib
*     method inherited from the KeyMap class).

*  Description:
*     This function returns a pointer to the value of a specified
*     attribute for a Table, formatted as a character string.

*  Parameters:
*     this
*        Pointer to the Table.
*     attrib
*        Pointer to a null terminated string containing the name of
*        the attribute whose value is required. This name should be in
*        lower case, with all white space removed.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     - Pointer to a null terminated string containing the attribute
*     value.

*  Notes:
*     - The returned string pointer may point at memory allocated
*     within the Table, or at static memory. The contents of the
*     string may be over-written or the pointer may become invalid
*     following a further invocation of the same function or any
*     modification of the Table. A copy of the string should
*     therefore be made if necessary.
*     - A NULL pointer will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*/

/* Local Variables: */
   astDECLARE_GLOBALS            /* Pointer to thread-specific global data */
   char cname[ AST__MXCOLNAMLEN + 1 ]; /* Column name */
   AstTable *this;               /* Pointer to the Table structure */
   const char *result;           /* Pointer value to return */
   int ival;                     /* Int attribute value */
   int len;                      /* Length of attrib string */
   int nc;                       /* No. characters read by astSscanf */

/* Initialise. */
   result = NULL;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Get a pointer to the thread specific global data structure. */
   astGET_GLOBALS(this_object);

/* Obtain a pointer to the Table structure. */
   this = (AstTable *) this_object;

/* Obtain the length of the attrib string. */
   len = strlen( attrib );

/* Compare "attrib" with each recognised attribute name in turn,
   obtaining the value of the required attribute. If necessary, write
   the value into "getattrib_buff" as a null terminated string in an
   appropriate format.  Set "result" to point at the result string. */

/* Table properties */
/* ================ */

/* Ncolumn */
/* ------- */
   if( !strcmp( attrib, "ncolumn" ) ) {
      ival = astGetNcolumn( this );
      if ( astOK ) {
         (void) sprintf( getattrib_buff, "%d", ival );
         result = getattrib_buff;
      }

/* Nrow */
/* ---- */
   } else if( !strcmp( attrib, "nrow" ) ) {
      ival = astGetNrow( this );
      if ( astOK ) {
         (void) sprintf( getattrib_buff, "%d", ival );
         result = getattrib_buff;
      }

/* Nparameter */
/* ---------- */
   } else if( !strcmp( attrib, "nparameter" ) ) {
      ival = astGetNparameter( this );
      if ( astOK ) {
         (void) sprintf( getattrib_buff, "%d", ival );
         result = getattrib_buff;
      }



/* Column properties */
/* ================= */

/* A macro that gives the scannf pattern to test for a given column
   property. Needed since the buffer length is defined by a macro
   (AST__MXCOLNAMLEN). */
#define PATTERN(cnam,blen) #cnam "(%" STRING(blen) "[^()])%n"

/* ColumnNdim */
   } else if ( nc = 0,
             ( 1 == astSscanf( attrib, PATTERN(columnndim,AST__MXCOLNAMLEN),
                               cname, &nc ) ) && ( nc >= len ) ) {
      ival = astGetColumnNdim( this, cname );
      if ( astOK ) {
         (void) sprintf( getattrib_buff, "%d", ival );
         result = getattrib_buff;
      }

/* ColumnLenC */
   } else if ( nc = 0,
             ( 1 == astSscanf( attrib, PATTERN(columnlenc,AST__MXCOLNAMLEN),
                               cname, &nc ) ) && ( nc >= len ) ) {
      ival = astGetColumnLenC( this, cname );
      if ( astOK ) {
         (void) sprintf( getattrib_buff, "%d", ival );
         result = getattrib_buff;
      }

/* ColumnType */
   } else if ( nc = 0,
             ( 1 == astSscanf( attrib, PATTERN(columntype,AST__MXCOLNAMLEN),
                               cname, &nc ) ) && ( nc >= len ) ) {
      ival = astGetColumnType( this, cname );
      if ( astOK ) {
         (void) sprintf( getattrib_buff, "%d", ival );
         result = getattrib_buff;
      }

/* ColumnLength */
   } else if ( nc = 0,
             ( 1 == astSscanf( attrib, PATTERN(columnlength,AST__MXCOLNAMLEN),
                               cname, &nc ) ) && ( nc >= len ) ) {
      ival = astGetColumnLength( this, cname );
      if ( astOK ) {
         (void) sprintf( getattrib_buff, "%d", ival );
         result = getattrib_buff;
      }

/* ColumnUnit */
   } else if ( nc = 0,
             ( 1 == astSscanf( attrib, PATTERN(columnunit,AST__MXCOLNAMLEN),
                               cname, &nc ) ) && ( nc >= len ) ) {
      result = astGetColumnUnit( this, cname );

#undef PATTERN


/* Unknown attributes */
/* ================== */

/* If the attribute name was not recognised, pass it on to the parent
   method for further interpretation. */
   } else {
      result = (*parent_getattrib)( this_object, attrib, status );
   }

/* Return the result. */
   return result;
}

static int GetColumnLenC( AstTable *this, const char *column, int *status ) {
/*
*+
*  Name:
*     astGetColumnLenC

*  Purpose:
*     Get the maximum formatted length of any value in a column.

*  Type:
*     Protected virtual function.

*  Synopsis:
*     #include "table.h"
*     int astGetColumnLenC( AstTable *this, const char *column )

*  Class Membership:
*     Table method.

*  Description:
*     This function returns the minimum length which a character variable
*     must have in order to be able to store the longest value currently
*     present (at any row) in a specified column of the supplied Table. If
*     the named column holds vector values, then the returned value is
*     the length of the longest element of the vector value.

*  Parameters:
*     this
*        Pointer to the Table.
*     column
*        The character string holding the upper-case name of the column.
*        Trailing spaces are ignored. An error is reported if the supplied
*        column is not found in the Table.

*  Returned Value:
*     The length (i.e. number of characters) of the longest formatted
*     value associated with the named column. This does not include the
*     trailing null character.

*  Notes:
*     - Automatic data type conversion occurs if the named column holds
*     numerical values.
*     - An error will be reported if named column does not exist or cannot
*     be formatted as a character
*     string.
*     - A function value of zero will be returned if an error has already
*     occurred, or if this function should fail for any reason.

*-
*/

/* Local Variables: */
   AstKeyMap *cols;        /* KeyMap holding column definitions */
   char key[ AST__MXCOLKEYLEN ]; /* Current cell key string */
   int irow;               /* Current row index */
   int len;                /* Length needed to format current cell */
   int nrow;               /* Number of rows in table */
   int result;             /* Returned value */

/* Initialise */
   result = 0;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Get the KeyMap holding information about all columns. */
   cols = astColumnProps( this );

/* Check the table contains the requested column. */
   if( astMapHasKey( cols, column ) ) {

/* Loop round all rows in the table. */
      nrow = astGetNrow( this );
      for( irow = 1; irow <= nrow; irow++ ) {

/* Format the cell name. */
         sprintf( key, "%.*s(%d)", (int) astChrLen(column), column, irow );

/* Get the maximum length needed to format a string in the current
   row/column. */
         len = astMapLenC( this, key );

/* Return the largest value found for any row. */
         if( len > result ) result = len;
      }

/* Report an error if the column does not exist. */
   } else if( astOK ) {
      astError( AST__BADCOL, "astGetColumnLenC(%s): No column named '%s' "
                "exists in the table.", status, astGetClass( this ), column );
   }

/* Free resources */
   cols = astAnnul( cols );

/* Return AST__BADTYPE if an error occurred. */
   if( !astOK ) result = 0;

/* Return the result. */
   return result;
}

static int GetColumnLength( AstTable *this, const char *column, int *status ) {
/*
*+
*  Name:
*     astGetColumnLength

*  Purpose:
*     Get the number of elements in each value in a column.

*  Type:
*     Protected virtual function.

*  Synopsis:
*     #include "table.h"
*     int astGetColumnLength( AstTable *this, const char *column )

*  Class Membership:
*     Table method.

*  Description:
*     This function returns the number of elements in each value stored
*     in a named column. Each value can be a scalar (in which case the
*     ColumnLength attribute has a value of 1), or a multi-dimensional
*     array ( in which case the ColumnLength value is equal to the
*     product of the array dimensions).

*  Parameters:
*     this
*        Pointer to the Table.
*     column
*        The character string holding the upper-case name of the column.
*        Trailing spaces are ignored. An error is reported if the supplied
*        column is not found in the Table.

*  Returned Value:
*     The number of elements in each column value.

*  Notes:
*     - An error will be reported if named column does not exist or cannot
*     be formatted as a character
*     string.
*     - A function value of zero will be returned if an error has already
*     occurred, or if this function should fail for any reason.

*-
*/

/* Local Variables: */
   AstKeyMap *col_km;      /* KeyMap holding requested column definition */
   AstKeyMap *cols;        /* KeyMap holding all column definitions */
   int *dims;              /* Pointer to array holding dimensions */
   int idim;               /* Index of dimension */
   int ndim;               /* Number of dimensions */
   int result;             /* Returned value */

/* Initialise */
   result = 0;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Get the KeyMap holding information about all columns. */
   cols = astColumnProps( this );

/* Get the KeyMap holding information about the requested column. */
   if( astMapGet0A( cols, column, &col_km ) ) {

/* If the Column properties includes the length, return it. Otherwise,
   calculate the length and store it in the KeyMap as a column property. */
      if( ! astMapGet0I( col_km, LENGTH, &result ) ) {

/* Get the number of axes spanned by each column value, and allocate an
   array big enough to hold the dimensions of these axes. */
         ndim = astMapLength( col_km, SHAPE );
         dims = astMalloc( sizeof( int )*ndim );
         if( astOK ) {

/* Get the dimensions. */
            astMapGet1I( col_km, SHAPE, ndim, &ndim, dims );

/* Find the number of elements. */
            result = 1;
            for( idim = 0; idim < ndim; idim++ ) {
               result *= dims[ idim ];
            }

/* Store the result in the column KeyMap. */
            astMapPut0I( col_km, LENGTH, result, NULL );
         }
         dims = astFree( dims );
      }

/* Free resources */
      col_km = astAnnul( col_km );

/* Report an error if the column does not exist. */
   } else if( astOK ) {
      astError( AST__BADCOL, "astGetColumnLength(%s): No column named '%s' "
                "exists in the table.", status, astGetClass( this ), column );
   }

/* Free resources */
   cols = astAnnul( cols );

/* Return AST__BADTYPE if an error occurred. */
   if( !astOK ) result = 0;

/* Return the result. */
   return result;
}

static int GetColumnNdim( AstTable *this, const char *column, int *status ) {
/*
*+
*  Name:
*     astGetColumnNdim

*  Purpose:
*     Get the number of dimensions for a column in a Table.

*  Type:
*     Protected virtual function.

*  Synopsis:
*     #include "table.h"
*     int astGetColumnNdim( AstTable *this, const char *column )

*  Class Membership:
*     Table method.

*  Description:
*     This function attribute holds the number of axes spanned by each value
*     in a column. If each cell in the column is a scalar, ColumnNdim will
*     be zero. If each cell in the column is a 1D spectrum, ColumnNdim will
*     be one. If each cell in the column is a 2D image, ColumnNdim will be
*     two, etc.

*  Parameters:
*     this
*        Pointer to the Table.
*     column
*        The character string holding the upper-case name of the column.
*        Trailing spaces are ignored. An error is reported if the supplied
*        column is not found in the Table.

*  Returned Value:
*     The number of dimensions - zero for a scalar.

*  Notes:
*     - A function value of zero will be returned if an error has
*     already occurred, or if this function should fail for any reason.

*-
*/

/* Local Variables: */
   AstKeyMap *cols;        /* KeyMap holding column definitions */
   AstKeyMap *col_km;      /* Pointer to KeyMap holding column info */
   int result;             /* Returned value */

/* Initialise */
   result = 0;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Get the KeyMap holding information about all columns. */
   cols = astColumnProps( this );

/* Get the KeyMap holding information about the requested column. */
   if( astMapGet0A( cols, column, &col_km ) ) {

/* Get the number of dimensions. */
      result = astMapLength( col_km, SHAPE );

/* Free resources */
      col_km = astAnnul( col_km );

/* Report an error if the column does not exist. */
   } else if( astOK ) {
      astError( AST__BADCOL, "astGetColumnNdim(%s): No column named '%s' "
                "exists in the table.", status, astGetClass( this ), column );
   }
   cols = astAnnul( cols );

/* Return AST__BADTYPE if an error occurred. */
   if( !astOK ) result = 0;

/* Return the result. */
   return result;
}

static int GetColumnType( AstTable *this, const char *column, int *status ) {
/*
*+
*  Name:
*     astGetColumnType

*  Purpose:
*     Get the data type of a column in a Table.

*  Type:
*     Protected virtual function.

*  Synopsis:
*     #include "table.h"
*     int astGetColumnType( AstTable *this, const char *column )

*  Class Membership:
*     Table method.

*  Description:
*     This function returns a value indicating the data type of a
*     named column in a Table. This is the data type which was used
*     when the column was added to the Table using astAddColumn.

*  Parameters:
*     this
*        Pointer to the Table.
*     column
*        The character string holding the upper-case name of the column.
*        Trailing spaces are ignored. An error is reported if the supplied
*        column is not found in the Table.

*  Returned Value:
*     One of AST__INTTYPE (for integer), AST__SINTTYPE (for short int),
*     AST__BYTETYPE (for unsigned bytes - i.e. unsigned chars),
*     AST__DOUBLETYPE (for double precision floating point),
*     AST__FLOATTYPE (for single precision floating point), AST__STRINGTYPE
*     (for character string), AST__OBJECTTYPE (for AST Object pointer),
*     AST__POINTERTYPE (for arbitrary C pointer) or AST__UNDEFTYPE (for
*     undefined values created by astMapPutU).

*  Notes:
*     - A function value of AST__BADTYPE will be returned if an error has
*     already occurred, or if this function should fail for any reason.

*-
*/

/* Local Variables: */
   AstKeyMap *cols;        /* Pointer to KeyMap holding all column info */
   AstKeyMap *col_km;      /* Pointer to KeyMap holding requested column info */
   int result;             /* Returned value */

/* Initialise */
   result = AST__BADTYPE;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Get the KeyMap holding information about the requested column. */
   cols = astColumnProps( this );
   if( astMapGet0A( cols, column, &col_km ) ) {

/* Get the column data type. */
      (void) astMapGet0I( col_km, TYPE, &result );

/* Annul the KeyMap pointer. */
      col_km = astAnnul( col_km );

/* Report an error if the column does not exist. */
   } else if( astOK ) {
      astError( AST__BADCOL, "astGetColumnType(%s): No column named '%s' "
                "exists in the table.", status, astGetClass( this ), column );
   }
   cols = astAnnul( cols );

/* Return AST__BADTYPE if an error occurred. */
   if( !astOK ) result = AST__BADTYPE;

/* Return the result. */
   return result;
}

static const char *GetColumnUnit( AstTable *this, const char *column, int *status ) {
/*
*+
*  Name:
*     astGetColumnUnit

*  Purpose:
*     Get the unit string for a column in a Table.

*  Type:
*     Protected virtual function.

*  Synopsis:
*     #include "table.h"
*     const char *astGetColumnUnit( AstTable *this, const char *column )

*  Class Membership:
*     Table method.

*  Description:
*     This function returns the unit string for a named column in a Table.
*     This is the unit string that was provided when the column was added to
*     the Table using astAddColumn.

*  Parameters:
*     this
*        Pointer to the Table.
*     column
*        The character string holding the upper-case name of the column.
*        Trailing spaces are ignored. An error is reported if the supplied
*        column is not found in the Table.

*  Returned Value:
*     A pointer to a null-terminated string containing the column units.

*-
*/

/* Local Variables: */
   AstKeyMap *col_km;    /* Pointer to KeyMap holding requested column info */
   AstKeyMap *cols;      /* Pointer to KeyMap holding all column info */
   const char *result;   /* Returned value */

/* Initialise */
   result = NULL;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Get the KeyMap holding information about the requested column. */
   cols = astColumnProps( this );
   if( astMapGet0A( cols, column, &col_km ) ) {

/* Get the column unit string. */
      (void) astMapGet0C( col_km, UNIT, &result );

/* Annul the KeyMap pointer. */
      col_km = astAnnul( col_km );

/* Report an error if the column does not exist. */
   } else if( astOK ) {
      astError( AST__BADCOL, "astGetColumnUnit(%s): No column named '%s' "
                "exists in the table.", status, astGetClass( this ), column );
   }
   cols = astAnnul( cols );

/* Return NULL if an error occurred. */
   if( !astOK ) result = NULL;

/* Return the result. */
   return result;
}

static int GetNcolumn( AstTable *this, int *status ) {
/*
*+
*  Name:
*     astGetNcolumn

*  Purpose:
*     Get the number of columns in a Table.

*  Type:
*     Protected virtual function.

*  Synopsis:
*     #include "table.h"
*     int astGetNcolumn( AstTable *this )

*  Class Membership:
*     Table method.

*  Description:
*     This function returns the number of columns currently in the Table.

*  Parameters:
*     this
*        Pointer to the Table.

*  Returned Value:
*     Number of columns.

*  Notes:
*     - A value of zero will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*-
*/

/* Local Variables: */
   AstKeyMap *cols;
   int result;

/* Check the global error status. */
   if ( !astOK ) return 0;

/* Get a pointer to the KeyMap holding the column definitions. */
   cols = astColumnProps( this );

/* Get the number of column definitions in the KeyMap. */
   result = astMapSize( cols );

/* Annul the KeyMap pointer. */
   cols = astAnnul( cols );

/* Return the result. */
   return result;
}

static int GetNparameter( AstTable *this, int *status ) {
/*
*+
*  Name:
*     astGetNparameter

*  Purpose:
*     Get the number of global parameters in a Table.

*  Type:
*     Protected virtual function.

*  Synopsis:
*     #include "table.h"
*     int astGetNparameter( AstTable *this )

*  Class Membership:
*     Table method.

*  Description:
*     This function returns the number of global parameters currently in the Table.

*  Parameters:
*     this
*        Pointer to the Table.

*  Returned Value:
*     Number of parameters.

*  Notes:
*     - A value of zero will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*-
*/

/* Local Variables: */
   AstKeyMap *pars;
   int result;

/* Check the global error status. */
   if ( !astOK ) return 0;

/* Get a pointer to the KeyMap holding the parameter definitions. */
   pars = astParameterProps( this );

/* Get the number of parameter definitions in the KeyMap. */
   result = astMapSize( pars );

/* Annul the KeyMap pointer. */
   pars = astAnnul( pars );

/* Return the result. */
   return result;
}

static int GetObjSize( AstObject *this_object, int *status ) {
/*
*  Name:
*     GetObjSize

*  Purpose:
*     Return the in-memory size of an Object.

*  Type:
*     Private function.

*  Synopsis:
*     #include "table.h"
*     int GetObjSize( AstObject *this, int *status )

*  Class Membership:
*     Table member function (over-rides the astGetObjSize protected
*     method inherited from the parent class).

*  Description:
*     This function returns the in-memory size of the supplied Tables,
*     in bytes.

*  Parameters:
*     this
*        Pointer to the Table.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     The Table size, in bytes.

*  Notes:
*     - A value of zero will be returned if this function is invoked
*     with the global status set, or if it should fail for any reason.
*/

/* Local Variables: */
   AstKeyMap *km;             /* KeyMap holding column/parameter definitions */
   AstTable *this;            /* Pointer to Table structure */
   int result;                /* Result value to return */

/* Initialise. */
   result = 0;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Obtain a pointers to the Table structure. */
   this = (AstTable *) this_object;

/* Invoke the GetObjSize method inherited from the parent KeyMap class, and
   then add on any components of the class structure defined by this class
   which are stored in dynamically allocated memory. */
   result = (*parent_getobjsize)( this_object, status );

   km = astColumnProps( this );
   result += astGetObjSize( km );
   km = astAnnul( km );

   km = astParameterProps( this );
   result += astGetObjSize( km );
   km = astAnnul( km );

/* If an error occurred, clear the result value. */
   if ( !astOK ) result = 0;

/* Return the result, */
   return result;
}

static int HasColumn( AstTable *this, const char *column, int *status ){
/*
*++
*  Name:
c     astHasColumn
f     AST_HASCOLUMN

*  Purpose:
*     Returns a flag indicating if a column is present in a Table.

*  Type:
*     Public virtual function.

*  Synopsis:
c     #include "table.h"
c     int astHasColumn( AstTable *this, const char *column )
f     RESULT = AST_HASCOLUMN( THIS, COLUMN, STATUS )

*  Class Membership:
*     Table method.

*  Description:
c     This function
f     This routine
*     returns a flag indicating if a named column exists in a Table, for
*     instance, by having been added to to the Table using
c     astAddColumn.
f     AST_ADDCOLUMN.

*  Parameters:
c     this
f     THIS = INTEGER (Given)
*        Pointer to the Table.
c     column
f     COLUMN = CHARACTER * ( * ) (Given)
*        The character string holding the upper case name of the column. Trailing
*        spaces are ignored.
f     STATUS = INTEGER (Given and Returned)
f        The global status.

*  Notes:
*     - A value of
c     zero
f     .FALSE.
*     is returned for if an error occurs.

*--
*/

/* Local Variables: */
   AstKeyMap *cols;
   int result;

/* Initialise */
   result = 0;

/* Check the inherited status. */
   if( !astOK ) return result;

/* Get the KeyMap holding information about all columns. */
   cols = astColumnProps( this );

/* Seeif it contains an entry for the named column. */
   result = astMapHasKey( cols, column );

/* Free resources. */
   cols = astAnnul( cols );

/* If an error has occurred, return zero. */
   if( !astOK ) result = 0;
   return result;
}

static int HasParameter( AstTable *this, const char *parameter, int *status ){
/*
*++
*  Name:
c     astHasParameter
f     AST_HASPARAMETER

*  Purpose:
*     Returns a flag indicating if a named global parameter is present in a Table.

*  Type:
*     Public virtual function.

*  Synopsis:
c     #include "table.h"
c     int astHasParameter( AstTable *this, const char *parameter )
f     RESULT = AST_HASPARAMETER( THIS, PARAMETER, STATUS )

*  Class Membership:
*     Table method.

*  Description:
c     This function
f     This routine
*     returns a flag indicating if a named parameter exists in a Table, for
*     instance, by having been added to to the Table using
c     astAddParameter.
f     AST_ADDPARAMETER.

*  Parameters:
c     this
f     THIS = INTEGER (Given)
*        Pointer to the Table.
c     parameter
f     PARAMETER = CHARACTER * ( * ) (Given)
*        The character string holding the upper case name of the parameter. Trailing
*        spaces are ignored.
f     STATUS = INTEGER (Given and Returned)
f        The global status.

*  Notes:
*     - A value of
c     zero
f     .FALSE.
*     is returned for if an error occurs.

*--
*/

/* Local Variables: */
   AstKeyMap *pars;
   int result;

/* Initialise */
   result = 0;

/* Check the inherited status. */
   if( !astOK ) return result;

/* Get the KeyMap holding information about all parameters. */
   pars = astParameterProps( this );

/* See if it contains an entry for the named parameter. */
   result = astMapHasKey( pars, parameter );

/* Free resources. */
   pars = astAnnul( pars );

/* If an error has occurred, return zero. */
   if( !astOK ) result = 0;
   return result;
}

void astInitTableVtab_(  AstTableVtab *vtab, const char *name, int *status ) {
/*
*+
*  Name:
*     astInitTableVtab

*  Purpose:
*     Initialise a virtual function table for a Table.

*  Type:
*     Protected function.

*  Synopsis:
*     #include "table.h"
*     void astInitTableVtab( AstTableVtab *vtab, const char *name )

*  Class Membership:
*     Table vtab initialiser.

*  Description:
*     This function initialises the component of a virtual function
*     table which is used by the Table class.

*  Parameters:
*     vtab
*        Pointer to the virtual function table. The components used by
*        all ancestral classes will be initialised if they have not already
*        been initialised.
*     name
*        Pointer to a constant null-terminated character string which contains
*        the name of the class to which the virtual function table belongs (it
*        is this pointer value that will subsequently be returned by the Object
*        astClass function).
*-
*/

/* Local Variables: */
   astDECLARE_GLOBALS            /* Pointer to thread-specific global data */
   AstObjectVtab *object;        /* Pointer to Object component of Vtab */
   AstKeyMapVtab *keymap;        /* Pointer to KeyMap component of Vtab */

/* Check the local error status. */
   if ( !astOK ) return;

/* Get a pointer to the thread specific global data structure. */
   astGET_GLOBALS(NULL);

/* Initialize the component of the virtual function table used by the
   parent class. */
   astInitKeyMapVtab( (AstKeyMapVtab *) vtab, name );

/* Store a unique "magic" value in the virtual function table. This
   will be used (by astIsATable) to determine if an object belongs
   to this class.  We can conveniently use the address of the (static)
   class_check variable to generate this unique value. */
   vtab->id.check = &class_check;
   vtab->id.parent = &(((AstKeyMapVtab *) vtab)->id);

/* Initialise member function pointers. */
/* ------------------------------------ */
/* Store pointers to the member functions (implemented here) that provide
   virtual methods for this class. */
   vtab->AddColumn = AddColumn;
   vtab->AddParameter = AddParameter;
   vtab->ColumnName = ColumnName;
   vtab->ParameterName = ParameterName;
   vtab->ColumnProps = ColumnProps;
   vtab->ColumnShape = ColumnShape;
   vtab->GetColumnLenC = GetColumnLenC;
   vtab->GetColumnLength = GetColumnLength;
   vtab->GetColumnNdim = GetColumnNdim;
   vtab->GetColumnType = GetColumnType;
   vtab->GetColumnUnit = GetColumnUnit;
   vtab->GetNcolumn = GetNcolumn;
   vtab->GetNparameter = GetNparameter;
   vtab->GetNrow = GetNrow;
   vtab->HasColumn = HasColumn;
   vtab->HasParameter = HasParameter;
   vtab->ParameterProps = ParameterProps;
   vtab->PurgeRows = PurgeRows;
   vtab->RemoveColumn = RemoveColumn;
   vtab->RemoveParameter = RemoveParameter;
   vtab->RemoveRow = RemoveRow;
   vtab->SetNrow = SetNrow;

/* Save the inherited pointers to methods that will be extended, and
   replace them with pointers to the new member functions. */
   object = (AstObjectVtab *) vtab;
   keymap = (AstKeyMapVtab *) vtab;

   parent_equal = object->Equal;
   object->Equal = Equal;

   parent_getobjsize = object->GetObjSize;
   object->GetObjSize = GetObjSize;

   parent_clearattrib = object->ClearAttrib;
   object->ClearAttrib = ClearAttrib;

   parent_getattrib = object->GetAttrib;
   object->GetAttrib = GetAttrib;

   parent_setattrib = object->SetAttrib;
   object->SetAttrib = SetAttrib;

   parent_testattrib = object->TestAttrib;
   object->TestAttrib = TestAttrib;

#if defined(THREAD_SAFE)
   parent_managelock = object->ManageLock;
   object->ManageLock = ManageLock;
#endif

   parent_mapremove = keymap->MapRemove;

/* Define convenience macros for overriding methods inherited from the
   parent KeyMap class using all data type supported by KeyMap. */
#define OVERRIDE(method,code,methodlc,codelc) \
   parent_##methodlc##codelc = keymap->method##code; \
   keymap->method##code = method##code; \

#define OVERRIDE_METHOD(method,methodlc) \
   OVERRIDE(method,A,methodlc,a) \
   OVERRIDE(method,P,methodlc,p) \
   OVERRIDE(method,C,methodlc,c) \
   OVERRIDE(method,D,methodlc,d) \
   OVERRIDE(method,F,methodlc,f) \
   OVERRIDE(method,I,methodlc,i) \
   OVERRIDE(method,S,methodlc,s) \
   OVERRIDE(method,B,methodlc,b)

/* Use these macros to override the required methods. */
   OVERRIDE_METHOD(MapPut0,mapput0)
   OVERRIDE_METHOD(MapGet0,mapget0)
   OVERRIDE_METHOD(MapPut1,mapput1)
   OVERRIDE_METHOD(MapGet1,mapget1)
   OVERRIDE_METHOD(MapPutElem,mapputelem)
   OVERRIDE_METHOD(MapGetElem,mapgetelem)
   OVERRIDE(MapPut,U,mapput,u)
   OVERRIDE(SetKeyCase,,setkeycase,)
   OVERRIDE(ClearKeyCase,,clearkeycase,)

/* Remove the macros. */
#undef OVERRIDE_METHOD
#undef OVERRIDE

/* Store replacement pointers for methods which will be over-ridden by
   new member functions implemented here. */

/* Declare the copy constructor, destructor and class dump function. */
   astSetCopy( vtab, Copy );
   astSetDelete( vtab, Delete );
   astSetDump( vtab, Dump, "Table", "Two-dimensional table of data values" );

/* If we have just initialised the vtab for the current class, indicate
   that the vtab is now initialised, and store a pointer to the class
   identifier in the base "object" level of the vtab. */
   if( vtab == &class_vtab ) {
      class_init = 1;
      astSetVtabClassIdentifier( vtab, &(vtab->id) );
   }
}

#if defined(THREAD_SAFE)
static int ManageLock( AstObject *this_object, int mode, int extra,
                       AstObject **fail, int *status ) {
/*
*  Name:
*     ManageLock

*  Purpose:
*     Manage the thread lock on an Object.

*  Type:
*     Private function.

*  Synopsis:
*     #include "object.h"
*     AstObject *ManageLock( AstObject *this, int mode, int extra,
*                            AstObject **fail, int *status )

*  Class Membership:
*     Table member function (over-rides the astManageLock protected
*     method inherited from the parent class).

*  Description:
*     This function manages the thread lock on the supplied Object. The
*     lock can be locked, unlocked or checked by this function as
*     deteremined by parameter "mode". See astLock for details of the way
*     these locks are used.

*  Parameters:
*     this
*        Pointer to the Object.
*     mode
*        An integer flag indicating what the function should do:
*
*        AST__LOCK: Lock the Object for exclusive use by the calling
*        thread. The "extra" value indicates what should be done if the
*        Object is already locked (wait or report an error - see astLock).
*
*        AST__UNLOCK: Unlock the Object for use by other threads.
*
*        AST__CHECKLOCK: Check that the object is locked for use by the
*        calling thread (report an error if not).
*     extra
*        Extra mode-specific information.
*     fail
*        If a non-zero function value is returned, a pointer to the
*        Object that caused the failure is returned at "*fail". This may
*        be "this" or it may be an Object contained within "this". Note,
*        the Object's reference count is not incremented, and so the
*        returned pointer should not be annulled. A NULL pointer is
*        returned if this function returns a value of zero.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*    A local status value:
*        0 - Success
*        1 - Could not lock or unlock the object because it was already
*            locked by another thread.
*        2 - Failed to lock a POSIX mutex
*        3 - Failed to unlock a POSIX mutex
*        4 - Bad "mode" value supplied.

*  Notes:
*     - This function attempts to execute even if an error has already
*     occurred.
*/

/* Local Variables: */
   AstTable *this;         /* Pointer to Table structure */
   int result;             /* Returned status value */

/* Initialise */
   result = 0;

/* Check the supplied pointer is not NULL. */
   if( !this_object ) return result;

/* Obtain a pointers to the Table structure. */
   this = (AstTable *) this_object;

/* Invoke the ManageLock method inherited from the parent class. */
   if( !result ) result = (*parent_managelock)( this_object, mode, extra,
                                                fail, status );

/* Invoke the astManageLock method on any Objects contained within
   the supplied Object. */
   if( !result && this->columns ) result = astManageLock( this->columns, mode, extra,
                                                          fail );

   if( !result && this->parameters ) result = astManageLock( this->parameters, mode, extra,
                                                             fail );

/* Return the result. */
   return result;
}
#endif

/*
*  Name:
*     MapGet0<X>

*  Purpose:
*     Get a scalar value from a cell of a Table.

*  Type:
*     Private function.

*  Synopsis:
*     #include "table.h"
*     int MapGet0<X>( AstKeyMap *this, const char *key, <X>type *value );

*  Class Membership:
*     Table member function (over-rides the astMapGet0<X> method inherited
*     from the KeyMap class).

*  Description:
*     This is a set of functions for retrieving a scalar value from a
*     cell of a Table. You should replace <X> in the generic function name
*     MapGet0<X> by an appropriate 1-character type code (see the "Data
*     Type Codes" section in the astMapGet0<X> docs). The stored value is
*     converted to the data type indiced by <X> before being returned (an
*     error is reported if it is not possible to convert the stored value
*     to the requested data type).

*  Parameters:
*     this
*        Pointer to the Table.
*     key
*        A character string identifying the cell from which the value is
*        to be retrieved. It should have the form "COLNAME(irow)", where
*        "COLNAME" is replaced by the name of a column that has been
*        defined previously using the astAddColumn method, and "irow" is
*        an integer row index (the first row is row 1).
*     value
*        A pointer to a buffer in which to return the requested value.
*        If the requested cell is not found, or if it is found but has an
*        undefined value (see astMapPutU), then the contents of the buffer
*        on entry to this function will be unchanged on exit. For pointer
*        types ("A" and "C"), the buffer should be a suitable pointer, and
*        the address of this pointer should be supplied as the "value"
*        parameter.
*     status
*        Pointer to inherited status value.

*  Returned Value:
*     A non-zero value is returned if the requested key name was found, and
*     does not have an undefined value (see astMapPutU). Zero is returned
*     otherwise.

*  Notes:
*     - No error is reported if the requested cell cannot be found in the
*     given KeyMap, but a zero value will be returned as the function value.
*     The supplied buffer will be returned unchanged.
*     - Key names are case insensitive, and white space is considered
*     significant.
*     - If the stored value is a vector value, then the first value in
*     the vector will be returned.
*     - A string pointer returned by astMapGet0C is guaranteed to remain
*     valid and the string to which it points will not be over-written for
*     a total of 50 successive invocations of this function. After this,
*     the memory containing the string may be re-used, so a copy of
*     the string should be made if it is needed for longer than this.
*     - If the returned value is an AST Object pointer, the Object's reference
*     count is incremented by this call. Any subsequent changes made to
*     the Object using the returned pointer will be reflected in any
*     any other active pointers for the Object. The returned pointer
*     should be annulled using astAnnul when it is no longer needed.
*/

/* Define a macro to implement the function for a specific data type. */
#define MAKE_MAPGET0(X,Xlc,Xtype,Itype) \
static int MapGet0##X( AstKeyMap *this_keymap, const char *key, Xtype *value, \
                       int *status ) { \
\
/* Local Variables: */ \
   AstTable *this;     /* Pointer to Table structure */ \
   char colname[ AST__MXCOLNAMLEN + 1 ]; /* Column name read from string */ \
   int irow;           /* Row index within key string */ \
   int result;         /* Returned flag */ \
\
/* Initialise */ \
   result = 0; \
\
/* Check the global error status. */ \
   if ( !astOK ) return result; \
\
/* Get a pointer to the Table structure. */ \
   this = (AstTable *) this_keymap; \
\
/* If the key is the name of a global table parameter, use the parent \
   method to get the value of hte parameter. */ \
   if( astHasParameter( this, key ) ) { \
      result = (*parent_mapget0##Xlc)( this_keymap, key, value, status ); \
\
/* Check the supplied key looks like a table cell key, and get the \
   the column name and the row number. Also checks that the table \
   contains a column with the specified name. */ \
   } else if( ParseKey( this, key, astGetKeyError( this ), colname, &irow, \
                        NULL, "astMapGet0" #X, status ) ) { \
\
/* If the row index is larger than the current number of rows in the \
   table, do nothing more. */ \
      if( irow <= astGetNrow( this ) ){ \
\
/* Use the astMapGet0<X> method in the parent keyMap class to get the \
   cell contents. */ \
         result = (*parent_mapget0##Xlc)( this_keymap, key, value, status ); \
      } \
   } \
\
/* If an error occurred, return zero. */ \
   if( !astOK ) result = 0; \
\
/* Return the result.*/ \
   return result; \
}

/* Expand the above macro to generate a function for each required
   data type. */
MAKE_MAPGET0(I,i,int,AST__INTTYPE)
MAKE_MAPGET0(D,d,double,AST__DOUBLETYPE)
MAKE_MAPGET0(F,f,float,AST__FLOATTYPE)
MAKE_MAPGET0(C,c,const char *,AST__STRINGTYPE)
MAKE_MAPGET0(A,a,AstObject *,AST__OBJECTTYPE)
MAKE_MAPGET0(P,p,void *,AST__POINTERTYPE)
MAKE_MAPGET0(S,s,short int,AST__SINTTYPE)
MAKE_MAPGET0(B,b,unsigned char,AST__BYTETYPE)

/* Undefine the macro. */
#undef MAKE_MAPGET0

/*
*  Name:
*     MapGet1<X>

*  Purpose:
*     Get a vector value from a cell of a Table.

*  Type:
*     Private function.

*  Synopsis:
*     #include "table.h"
*     int MapGet1<X>( AstKeyMap *this, const char *key, int mxval,
*                     int *nval, <X>type *value )
*     int MapGet1C( AstKeyMap *this, const char *key, int l, int mxval,
*                   int *nval, const char *value )

*  Class Membership:
*     Table member function (over-rides the astMapGet1<X> method inherited
*     from the KeyMap class).

*  Description:
*     This is a set of functions for retrieving a vector value from a
*     cell of a Table. You should replace <X> in the generic function name
*     MapGet1<X> by an appropriate 1-character type code (see the "Data
*     Type Codes" section in the astMapGet1<X> docs). The stored value is
*     converted to the data type indiced by <X> before being returned (an
*     error is reported if it is not possible to convert the stored value
*     to the requested data type).
*
*     Note, the MapGet1C function has an extra parameter "l" which
*     specifies the maximum length of each string to be stored in the
*     "value" buffer (see the "astMapGet1C" docs).

*  Parameters:
*     this
*        Pointer to the Table.
*     key
*        A character string identifying the cell from which the value is
*        to be retrieved. It should have the form "COLNAME(irow)", where
*        "COLNAME" is replaced by the name of a column that has been
*        defined previously using the astAddColumn method, and "irow" is
*        an integer row index (the first row is row 1).
*     mxval
*        The number of elements in the "value" array.
*     nval
*        The address of an integer in which to put the number of elements
*        stored in the "value" array. Any unused elements of the array are
*        left unchanged.
*     value
*        A pointer to an array in which to return the requested values.
*        If the requested cell is not found, or if it is found but has an
*        undefined value (see astMapPutU), then the contents of the buffer
*        on entry to this function will be unchanged on exit.
*     status
*        Pointer to inherited status value.

*  Returned Value:
*     A non-zero value is returned if the requested key name was found, and
*     does not have an undefined value (see astMapPutU). Zero is returned
*     otherwise.

*  MapGet1C:
*     The "value" buffer supplied to the MapGet1C function should be a
*     pointer to a character array with "mxval*l" elements, where "l" is
*     the maximum length of a string to be returned. The value of "l"
*     should be supplied as an extra parameter following "key" when
*     invoking MapGet1C, and should include space for a terminating
*     null character.

*  Notes:
*     - No error is reported if the requested cell cannot be found in the
*     given KeyMap, but a zero value will be returned as the function value.
*     The supplied buffer will be returned unchanged.
*     - Key names are case insensitive, and white space is considered
*     significant.
*     - If the stored value is a scalar value, then the value will be
*     returned in the first element of the supplied array, and "nval"
*     will be returned set to 1.
*/

/* Define a macro to implement the function for a specific data type
   (excluding "C" since that needs an extra parameter). */
#define MAKE_MAPGET1(X,Xlc,Xtype,Itype) \
static int MapGet1##X( AstKeyMap *this_keymap, const char *key, int mxval, int *nval, \
                       Xtype *value, int *status ) { \
\
/* Local Variables: */ \
   AstTable *this;     /* Pointer to Table structure */ \
   char colname[ AST__MXCOLNAMLEN + 1 ]; /* Column name read from string */ \
   int irow;           /* Row index within key string */ \
   int result;         /* Returned flag */ \
\
/* Initialise */ \
   result = 0; \
\
/* Check the global error status. */ \
   if ( !astOK ) return result; \
\
/* Get a pointer to the Table structure. */ \
   this = (AstTable *) this_keymap; \
\
/* If the key is the name of a global table parameter, use the parent \
   method to get the value of hte parameter. */ \
   if( astHasParameter( this, key ) ) { \
      result = (*parent_mapget1##Xlc)( this_keymap, key, mxval, nval, \
                value, status ); \
\
/* Check the supplied key looks like a table cell key, and get the \
   the column name and the row number. Also checks that the table \
   contains a column with the specified name. */ \
   } else if( ParseKey( this, key, astGetKeyError( this ), colname, &irow, \
                 NULL, "astMapGet1" #X, status ) ) { \
\
/* If the row index is larger than the current number of rows in the \
   table, do nothing more. */ \
      if( irow <= astGetNrow( this ) ){ \
\
/* Use the astMapGet1<X> method in the parent keyMap class to get the \
   cell contents. */ \
         result = (*parent_mapget1##Xlc)( this_keymap, key, mxval, nval, \
                   value, status ); \
      } \
   } \
\
/* If an error occurred, return zero. */ \
   if( !astOK ) result = 0; \
\
/* Return the result.*/ \
   return result; \
}

/* Expand the above macro to generate a function for each required
   data type. */
MAKE_MAPGET1(I,i,int,AST__INTTYPE)
MAKE_MAPGET1(D,d,double,AST__DOUBLETYPE)
MAKE_MAPGET1(F,f,float,AST__FLOATTYPE)
MAKE_MAPGET1(A,a,AstObject *,AST__OBJECTTYPE)
MAKE_MAPGET1(P,p,void *,AST__POINTERTYPE)
MAKE_MAPGET1(S,s,short int,AST__SINTTYPE)
MAKE_MAPGET1(B,b,unsigned char,AST__BYTETYPE)

/* Undefine the macro. */
#undef MAKE_MAPGET1


static int MapGet1C( AstKeyMap *this_keymap, const char *key, int l, int mxval,
                     int *nval, char *value, int *status ) {
/*
*  Name:
*     MapGet1C

*  Purpose:
*     Get a vector value from a cell of a Table.

*  Type:
*     Private function.

*  Synopsis:
*     #include "table.h"
*     int MapGet1C( AstKeyMap *this, const char *key, int l, int mxval,
*                   int *nval, const char *value )

*  Class Membership:
*     Table member function (over-rides the astMapGet1C method inherited
*     from the KeyMap class).

*  Description:
*     This is the implementation of MapGet1<X> for <X> = "C". We
*     cannot use the MAKE_MAPGET1 macro for this because the string
*     version of this function has an extra parameter giving the maximum
*     length of each string which can be stored in the supplied buffer.

*  Parameters:
*     (see MapGet1<X>)
*/

/* Local Variables: */
   AstTable *this;     /* Pointer to Table structure */
   char colname[ AST__MXCOLNAMLEN + 1 ]; /* Column name read from string */
   int irow;           /* Row index within key string */
   int result;         /* Returned flag */

/* Initialise */
   result = 0;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Get a pointer to the Table structure. */
   this = (AstTable *) this_keymap;

/* If the key is the name of a global table parameter, use the parent
   method to get the value of hte parameter. */
   if( astHasParameter( this, key ) ) {
      result = (*parent_mapget1c)( this_keymap, key, l, mxval, nval,
                value, status );

/* Check the supplied key looks like a table cell key, and get the
   the column name and the row number. Also checks that the table
   contains a column with the specified name. */
   } else if( ParseKey( this, key, astGetKeyError( this ), colname, &irow,
                        NULL, "astMapGet1C", status ) ) {

/* If the row index is larger than the current number of rows in the
   table, do nothing more. */
      if( irow <= astGetNrow( this ) ){

/* Use the astMapGet1<X> method in the parent keyMap class to get the
   cell contents. */
         result = (*parent_mapget1c)( this_keymap, key, l, mxval, nval,
                   value, status );
      }
   }

/* If an error occurred, return zero. */
   if( !astOK ) result = 0;

/* Return the result.*/
   return result;
}

/*
*  Name:
*     MapGetElem<X>

*  Purpose:
*     Get a single element of a vector value from a cell of a Table.

*  Type:
*     Private function.

*  Synopsis:
*     #include "table.h"
*     int MapGetElem<X>( AstKeyMap *this, const char *key, int elem,
*                        <X>type *value, int *status )
*     int MapGetElemC( AstKeyMap *this, const char *key, int l, int elem,
*                      char *value, int *status )

*  Class Membership:
*     Table member function (over-rides the astMapGetElem<X> method inherited
*     from the KeyMap class).

*  Description:
*     This is a set of functions for retrieving a single element of a vector
*     value from a cell of a Table. You should replace <X> in the generic
*     function name MapGetElem<X> by an appropriate 1-character type code
*     (see the "Data Type Codes" section in the astMapGetElem<X> docs). The
*     stored value is converted to the data type indiced by <X> before being
*     returned (an error is reported if it is not possible to convert the
*     stored value to the requested data type).
*
*     Note, the MapGetElemC function has an extra parameter "l" which
*     specifies the maximum length of each string to be stored in the
*     "value" buffer (see the "MapGetElemC" docs).

*  Parameters:
*     this
*        Pointer to the Table.
*     key
*        A character string identifying the cell from which the value is
*        to be retrieved. It should have the form "COLNAME(irow)", where
*        "COLNAME" is replaced by the name of a column that has been
*        defined previously using the astAddColumn method, and "irow" is
*        an integer row index (the first row is row 1).
*     elem
*        The index of the vector element to modify, starting at zero.
*        If the index is outside the range of the vector, an error will
*        be reported.
*     value
*        A pointer to a buffer in which to return the requested values.
*        If the requested cell is not found, or if it is found but has an
*        undefined value (see astMapPutU), then the contents of the buffer
*        on entry to this function will be unchanged on exit.
*     status
*        Pointer to inherited status value.

*  Returned Value:
*     A non-zero value is returned if the requested key name was found, and
*     does not have an undefined value (see astMapPutU). Zero is returned
*     otherwise.

*  MapGetElemC:
*     The "value" buffer supplied to the MapGetElemC function should be a
*     pointer to a character array with "l" elements, where "l" is
*     the maximum length of a string to be returned. The value of "l"
*     should be supplied as an extra parameter following "key" when
*     invoking MapGetElemC, and should include space for a terminating
*     null character.

*  Notes:
*     - No error is reported if the requested cell cannot be found in the
*     given KeyMap, but a zero value will be returned as the function value.
*     The supplied buffer will be returned unchanged.
*     - Key names are case insensitive, and white space is considered
*     significant.
*     - If the stored value is a scalar value, then the value will be
*     returned in the first element of the supplied array, and "nval"
*     will be returned set to 1.
*/

/* Define a macro to implement the function for a specific data type
(excluding "C" since that needs an extra parameter). */
#define MAKE_MAPGETELEM(X,Xlc,Xtype,Itype) \
static int MapGetElem##X( AstKeyMap *this_keymap, const char *key, int elem, \
                          Xtype *value, int *status ) { \
\
/* Local Variables: */ \
   AstTable *this;     /* Pointer to Table structure */ \
   char colname[ AST__MXCOLNAMLEN + 1 ]; /* Column name read from string */ \
   int irow;           /* Row index within key string */ \
   int result;         /* Returned flag */ \
\
/* Initialise */ \
   result = 0; \
\
/* Check the global error status. */ \
   if ( !astOK ) return result; \
\
/* Get a pointer to the Table structure. */ \
   this = (AstTable *) this_keymap; \
\
/* If the key is the name of a global table parameter, use the parent \
   method to get the value of hte parameter. */ \
   if( astHasParameter( this, key ) ) { \
      result = (*parent_mapgetelem##Xlc)( this_keymap, key, elem, \
                                          value, status ); \
\
/* Check the supplied key looks like a table cell key, and get the \
   the column name and the row number. Also checks that the table \
   contains a column with the specified name. */ \
   } else if( ParseKey( this, key, astGetKeyError( this ), colname, &irow, \
                        NULL, "astMapGetElem" #X, status ) ) { \
\
/* If the row index is larger than the current number of rows in the \
   table, do nothing more. */ \
      if( irow <= astGetNrow( this ) ){ \
\
/* Use the astMapGetElem<X> method in the parent keyMap class to get the \
   cell contents. */ \
         result = (*parent_mapgetelem##Xlc)( this_keymap, key, elem, \
                                             value, status ); \
      } \
   } \
\
/* If an error occurred, return zero. */ \
   if( !astOK ) result = 0; \
\
/* Return the result.*/ \
   return result; \
}

/* Expand the above macro to generate a function for each required
   data type. */
MAKE_MAPGETELEM(I,i,int,AST__INTTYPE)
MAKE_MAPGETELEM(D,d,double,AST__DOUBLETYPE)
MAKE_MAPGETELEM(F,f,float,AST__FLOATTYPE)
MAKE_MAPGETELEM(A,a,AstObject *,AST__OBJECTTYPE)
MAKE_MAPGETELEM(P,p,void *,AST__POINTERTYPE)
MAKE_MAPGETELEM(S,s,short int,AST__SINTTYPE)
MAKE_MAPGETELEM(B,b,unsigned char,AST__BYTETYPE)

/* Undefine the macro. */
#undef MAKE_MAPGETELEM

static int MapGetElemC( AstKeyMap *this_keymap, const char *key, int l,
                        int elem, char *value, int *status ) {
/*
*  Name:
*     MapGetElemC

*  Purpose:
*     Get a single element of a vector value from a cell of a Table.

*  Type:
*     Private function.

*  Synopsis:
*     #include "table.h"
*     int MapGetElemC( AstKeyMap *this, const char *key, int l, int elem,
*                      char *value, int *status )

*  Class Membership:
*     Table member function (over-rides the astMapGetElemC method inherited
*     from the KeyMap class).

*  Description:
*     This is the implementation of MapGetElem<X> for <X> = "C". We
*     cannot use the MAKE_MAPGETELEM macro for this because the string
*     version of this function has an extra parameter giving the maximum
*     length of each string which can be stored in the supplied buffer.

*  Parameters:
*     (see MapGetElem<X>)
*/

/* Local Variables: */
   AstTable *this;     /* Pointer to Table structure */
   char colname[ AST__MXCOLNAMLEN + 1 ]; /* Column name read from string */
   int irow;           /* Row index within key string */
   int result;         /* Returned flag */

/* Initialise */
   result = 0;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Get a pointer to the Table structure. */
   this = (AstTable *) this_keymap;

/* If the key is the name of a global table parameter, use the parent
   method to get the value of hte parameter. */
   if( astHasParameter( this, key ) ) {
      result = (*parent_mapgetelemc)( this_keymap, key, l, elem,
                                      value, status );

/* Check the supplied key looks like a table cell key, and get the
   the column name and the row number. Also checks that the table
   contains a column with the specified name. */
   } else if( ParseKey( this, key, astGetKeyError( this ), colname, &irow,
                        NULL, "astMapGetElemC", status ) ) {

/* If the row index is larger than the current number of rows in the
   table, do nothing more. */
      if( irow <= astGetNrow( this ) ){

/* Use the astMapGetElem<X> method in the parent keyMap class to get the
   cell contents. */
         result = (*parent_mapgetelemc)( this_keymap, key, l, elem,
                                         value, status );
      }
   }

/* If an error occurred, return zero. */
   if( !astOK ) result = 0;

/* Return the result.*/
   return result;
}

/*
*  Name:
*     MapPut0<X>

*  Purpose:
*     Stores a scalar value in a cell of a Table.

*  Type:
*     Private function.

*  Synopsis:
*     #include "table.h"
*     void MapPut0<X>( AstKeyMap *this, const char *key, <X>type value,
*                      const char *comment, int *status )

*  Class Membership:
*     Table member function (over-rides the astMapPut0<X> method inherited
*     from the KeyMap class).

*  Description:
*     This is a set of functions for storing a scalar value in a cell of
*     a Table. You should use a function which matches the data type of the
*     data you wish to add to the Table by replacing <X> in the generic
*     function name MapPut0<X> by an appropriate 1-character type code (see
*     the "Data Type Codes" section in the astMapPut0<X> docs).

*  Parameters:
*     this
*        Pointer to the Table in which to store the supplied value.
*     key
*        A character string identifying the cell in which the value is
*        to be stored. It should have the form "COLNAME(irow)", where
*        "COLNAME" is replaced by the name of a column that has been
*        defined previously using the astAddColumn method, and "irow" is
*        an integer row index (the first row is row 1).
*     value
*        The value to be stored. The data type of this value should match
*        the 1-character type code appended to the function name (e.g. if
*        you are using astMapPut0A, the type of this value should be "pointer
*        to AstObject"). An error will be reported if this data type is
*        different to the data type assigned to the column when it was
*        created via astAddColumn.
*     comment
*        A pointer to a null-terminated comment string to be stored with the
*        value. A NULL pointer may be supplied, in which case no comment is
*        stored.
*     status
*        Pointer to inherited status value.

*  Notes:
*     - Key names are case insensitive, and white space is considered
*     significant.
*     - The new value will replace any old value already stored in the
*     Table for the specified cell.
*     - If the stored value is an AST Object pointer, the Object's reference
*     count is incremented by this call. Any subsequent changes made to
*     the Object using the returned pointer will be reflected in any
*     any other active pointers for the Object, including any obtained
*     later using astMapget0A. The reference count for the Object will be
*     decremented when the KeyMap is destroyed, or the entry is removed or
*     over-written with a different pointer.

*/
/* Define a macro to implement the function for a specific data type. */
#define MAKE_MAPPUT0(X,Xlc,Xtype,Itype,ValExp) \
static void MapPut0##X( AstKeyMap *this_keymap, const char *key, Xtype value, \
                        const char *comment, int *status ) { \
\
/* Local Variables: */ \
   AstKeyMap *col_km;  /* KeyMap holding details of the requested column */ \
   AstTable *this;     /* Pointer to Table structure */ \
   char colname[ AST__MXCOLNAMLEN + 1 ]; /* Column name read from string */ \
   int irow;           /* Row index within key string */ \
   int type;           /* Data type of the requested column */ \
\
/* Check the global error status. */ \
   if ( !astOK ) return; \
\
/* Get a pointer to the Table structure. */ \
   this = (AstTable *) this_keymap; \
\
/* If the key is the name of a global table parameter, use the parent \
   method to put the value of the parameter. */ \
   if( astHasParameter( this, key ) ) { \
      (*parent_mapput0##Xlc)( this_keymap, key, value, comment, status ); \
\
/* Check the supplied key looks like a table cell key, and get the \
   the column name and the row number. Also checks that the table \
   contains a column with the specified name. */ \
   } else if( ParseKey( this, key, 1, colname, &irow, &col_km, "astMapPut0" #X, \
                        status ) ) { \
\
/* Check the column holds scalar values of the type implied by the <X> \
   code in the function name. */ \
      (void) astMapGet0I( col_km, TYPE, &type ); \
      if( type != Itype && astOK ) { \
         astError( AST__BADTYP, "astMapPut0" #X "(%s): Failed to store a " \
                   #Xtype " value for cell \"%s\": column %s holds %s " \
                   "values.", status, astGetClass( this ), key, colname, \
                   TypeString( type ) ); \
      } \
\
      if( astMapHasKey( col_km, SHAPE ) && astOK ) { \
         astError( AST__BADTYP, "astMapPut0" #X "(%s): Failed to store a " \
                   "scalar value for cell \"%s\": column %s holds vector " \
                   " values.", status, astGetClass( this ), key, colname ); \
      } \
\
/* If the row index is larger than the current number of rows in the \
   table, update the number of rows in the table. */ \
      if( irow > astGetNrow( this ) ) astSetNrow( this, irow ); \
\
/* Use the astMapPut0<X> method in the parent keyMap class to store the \
   new cell contents. */ \
      (*parent_mapput0##Xlc)( this_keymap, key, value, comment, status ); \
\
/* Free resources. */ \
      col_km = astAnnul( col_km ); \
   } \
}

/* Expand the above macro to generate a function for each required
   data type. */
MAKE_MAPPUT0(I,i,int,AST__INTTYPE,value)
MAKE_MAPPUT0(D,d,double,AST__DOUBLETYPE,value)
MAKE_MAPPUT0(F,f,float,AST__FLOATTYPE,value)
MAKE_MAPPUT0(C,c,const char *,AST__STRINGTYPE,astStore(NULL,value,strlen(value)+1))
MAKE_MAPPUT0(A,a,AstObject *,AST__OBJECTTYPE,(value?astClone(value):NULL))
MAKE_MAPPUT0(P,p,void *,AST__POINTERTYPE,value)
MAKE_MAPPUT0(S,s,short int,AST__SINTTYPE,value)
MAKE_MAPPUT0(B,b,unsigned char,AST__BYTETYPE,value)

/* Undefine the macro. */
#undef MAKE_MAPPUT0

/*
*  Name:
*     MapPut1<X>

*  Purpose:
*     Stores a vectorised value in a cell of a Table.

*  Type:
*     Private function.

*  Synopsis:
*     #include "table.h"
*     void MapPut1<X>( AstKeyMap *this, const char *key, int size,
*                      const <X>type value[], const char *comment,
*                      int *status );

*  Class Membership:
*     Table member function (over-rides the astMapPut1<X> method inherited
*     from the KeyMap class).

*  Description:
*     This is a set of functions for storing a vectorised value in a cell of
*     a Table. You should use a function which matches the data type of the
*     data you wish to add to the Table by replacing <X> in the generic
*     function name MapPut1<X> by an appropriate 1-character type code (see
*     the "Data Type Codes" section in the astMapPut1<X> docs).

*  Parameters:
*     this
*        Pointer to the Table in which to store the supplied value.
*     key
*        A character string identifying the cell in which the value is
*        to be stored. It should have the form "COLNAME(irow)", where
*        "COLNAME" is replaced by the name of a column that has been
*        defined previously using the astAddColumn method, and "irow" is
*        an integer row index (the first row is row 1).
*     size
*        The number of elements in the supplied array of values.
*     value
*        The value to be stored. The data type of this value should match
*        the 1-character type code appended to the function name (e.g. if
*        you are using astMapPut0A, the type of this value should be "pointer
*        to AstObject"). An error will be reported if this data type is
*        different to the data type assigned to the column when it was
*        created via astAddColumn.
*     comment
*        A pointer to a null-terminated comment string to be stored with the
*        value. A NULL pointer may be supplied, in which case no comment is
*        stored.
*     status
*        Pointer to inherited status value.

*  Notes:
*     - Key names are case insensitive, and white space is considered
*     significant.
*     - The new value will replace any old value already stored in the
*     Table for the specified cell.

*/

/* Define a macro to implement the function for a specific data type. */
#define MAKE_MAPPUT1(X,Xlc,Xtype,Itype,ValExp) \
static void MapPut1##X( AstKeyMap *this_keymap, const char *key, int size, \
                        Xtype value[], const char *comment, \
                        int *status ) { \
\
/* Local Variables: */ \
   AstTable *this;     /* Pointer to Table structure */ \
   char colname[ AST__MXCOLNAMLEN + 1 ]; /* Column name read from string */ \
   int irow;           /* Row index within key string */ \
\
/* Check the global error status. */ \
   if ( !astOK ) return; \
\
/* Get a pointer to the Table structure. */ \
   this = (AstTable *) this_keymap; \
\
/* If the key is the name of a global table parameter, use the parent \
   method to put the value of the parameter. */ \
   if( astHasParameter( this, key ) ) { \
      (*parent_mapput1##Xlc)( this_keymap, key, size, value, \
                              comment, status ); \
\
/* Check the supplied key looks like a table cell key, and get the \
   the column name and the row number. Also checks that the table \
   contains a column with the specified name. */ \
   } else if( ParseKey( this, key, 1, colname, &irow, NULL, "astMapPut1" #X, \
                        status ) ) { \
\
/* Check the column holds vector values of the type implied by the <X> \
   code in the function name. */ \
      if(  astGetColumnType( this, colname ) != Itype && astOK ) { \
         astError( AST__BADTYP, "astMapPut1" #X "(%s): Failed to store " \
                   #Xtype " values for cell \"%s\": column %s holds %s values.", \
                   status, astGetClass( this ), key, colname, \
                   TypeString( astGetColumnType( this, colname ) ) ); \
      } \
\
/* Check the column holds vectors with length equal to the supplied vector. */ \
      if( astGetColumnLength( this, colname ) != size && astOK ) { \
         astError( AST__BADTYP, "astMapPut1" #X "(%s): Failed to " \
                   "store a vector value for cell \"%s\": column " \
                   "%s needs %d values per cell but %d were supplied.", \
                   status, astGetClass( this ), key, colname, \
                   astGetColumnLength( this, colname ), size ); \
      } \
\
/* If all is OK, update the number of rows in the table if required, and \
   store the vector in the parent KeyMap. */ \
      if( astOK ) { \
         if( irow > astGetNrow( this ) ) astSetNrow( this, irow ); \
         (*parent_mapput1##Xlc)( this_keymap, key, size, value, \
                                 comment, status ); \
      } \
\
   } \
}

/* Expand the above macro to generate a function for each required
   data type. */
MAKE_MAPPUT1(D,d,const double,AST__DOUBLETYPE,value[i])
MAKE_MAPPUT1(F,f,const float,AST__FLOATTYPE,value[i])
MAKE_MAPPUT1(I,i,const int,AST__INTTYPE,value[i])
MAKE_MAPPUT1(C,c,const char *const,AST__STRINGTYPE,astStore(NULL,value[i],strlen(value[i])+1))
MAKE_MAPPUT1(A,a,AstObject *const,AST__OBJECTTYPE,(value[i]?astClone(value[i]):NULL))
MAKE_MAPPUT1(P,p,void *const,AST__POINTERTYPE,value[i])
MAKE_MAPPUT1(S,s,const short int,AST__SINTTYPE,value[i])
MAKE_MAPPUT1(B,b,const unsigned char,AST__BYTETYPE,value[i])

/* Undefine the macro. */
#undef MAKE_MAPPUT1

/*
*  Name:
*     MapPutElem<X>

*  Purpose:
*     Put a value into an element of a vector value in a cell of a Table.

*  Type:
*     Private function.

*  Synopsis:
*     #include "table.h"
*     void MapPutElem<X>( AstKeyMap *this, const char *key, int elem,
*                         <X>type *value, int *status )

*  Class Membership:
*     Table member function (over-rides the astMapPutElem<X> method inherited
*     from the KeyMap class).

*  Description:
*     This is a set of functions for storing a value in a single element
*     of a vector value stored in a cell of a Table. You should use a
*     function which matches the data type of the data you wish to add to
*     the Table by replacing <X> in the generic function name MapPutElem<X>
*     by an appropriate 1-character type code (see the "Data Type Codes"
*     section in the astMapPutElem<X> docs).

*  Parameters:
*     this
*        Pointer to the Table in which to store the supplied value.
*     key
*        A character string identifying the cell in which the value is
*        to be stored. It should have the form "COLNAME(irow)", where
*        "COLNAME" is replaced by the name of a column that has been
*        defined previously using the astAddColumn method, and "irow" is
*        an integer row index (the first row is row 1).
*     elem
*        The index of the vector element to modify, starting at zero.
*        If the index is outside the range of the vector, an error will
*        be reported.
*     value
*        The value to be stored. The data type of this value should match
*        the 1-character type code appended to the function name (e.g. if
*        you are using astMapPut0A, the type of this value should be "pointer
*        to AstObject"). An error will be reported if this data type is
*        different to the data type assigned to the column when it was
*        created via astAddColumn.
*     status
*        Pointer to inherited status value.

*  Notes:
*     - Key names are case insensitive, and white space is considered
*     significant.
*     - The new value will replace any old value already stored in the
*     Table for the specified cell.
*     - If the stored value is an AST Object pointer, the Object's reference
*     count is incremented by this call. Any subsequent changes made to
*     the Object using the returned pointer will be reflected in any
*     any other active pointers for the Object, including any obtained
*     later using astMapget0A. The reference count for the Object will be
*     decremented when the KeyMap is destroyed, or the entry is removed or
*     over-written with a different pointer.

*/
/* Define a macro to implement the function for a specific data type. */

#define MAKE_MAPPUTELEM(X,Xlc,Xtype,Itype) \
static void MapPutElem##X( AstKeyMap *this_keymap, const char *key, int elem, \
                           Xtype value, int *status ) { \
\
/* Local Variables: */ \
   AstTable *this;     /* Pointer to Table structure */ \
   char colname[ AST__MXCOLNAMLEN + 1 ]; /* Column name read from string */ \
   int irow;           /* Row index within key string */ \
   int type;           /* Data type of the requested column */ \
\
/* Check the global error status. */ \
   if ( !astOK ) return; \
\
/* Get a pointer to the Table structure. */ \
   this = (AstTable *) this_keymap; \
\
/* If the key is the name of a global table parameter, use the parent \
   method to put the value of the parameter. */ \
   if( astHasParameter( this, key ) ) { \
      (*parent_mapputelem##Xlc)( this_keymap, key, elem, value, \
                                 status ); \
\
/* Check the supplied key looks like a table cell key, and get the \
   the column name and the row number. Also checks that the table \
   contains a column with the specified name. */ \
   } else if( ParseKey( this, key, 1, colname, &irow, NULL, "astMapPutElem" #X, \
                        status ) ) { \
\
/* Check the column holds vector values of the type implied by the <X> \
   code in the function name. */ \
      type = astGetColumnType( this, colname ); \
      if(  type != Itype && astOK ) { \
         astError( AST__BADTYP, "astMapPutElem" #X "(%s): Failed to store a " \
                   #Xtype " value in cell \"%s\": column %s holds %s values.", \
                   status, astGetClass( this ), key, colname, \
                   TypeString( type ) ); \
      } \
\
/* Check the column holds vectors with length equal to the supplied vector. */ \
      if( astGetColumnLength( this, colname ) <= elem && astOK ) { \
         astError( AST__BADTYP, "astMapPutElem" #X "(%s): Failed to " \
                   "store a value for element %d (zero-based) of " \
                   "cell \"%s\": column %s has only %d values per " \
                   "cell.", status, astGetClass( this ), elem, key, \
                   colname, astGetColumnLength( this, colname ) ); \
      } \
\
/* If all is OK, update the number of rows in the table if required, and \
   store the value in the parent KeyMap. */ \
      if( astOK ) { \
         if( irow > astGetNrow( this ) ) astSetNrow( this, irow ); \
         (*parent_mapputelem##Xlc)( this_keymap, key, elem, value, \
                                    status ); \
      } \
   } \
}

/* Expand the above macro to generate a function for each required
   data type. */
MAKE_MAPPUTELEM(I,i,int,AST__INTTYPE)
MAKE_MAPPUTELEM(D,d,double,AST__DOUBLETYPE)
MAKE_MAPPUTELEM(F,f,float,AST__FLOATTYPE)
MAKE_MAPPUTELEM(A,a,AstObject *,AST__OBJECTTYPE)
MAKE_MAPPUTELEM(P,p,void *,AST__POINTERTYPE)
MAKE_MAPPUTELEM(C,c,const char *,AST__STRINGTYPE)
MAKE_MAPPUTELEM(S,s,short int,AST__SINTTYPE)
MAKE_MAPPUTELEM(B,b,unsigned char,AST__BYTETYPE)

/* Undefine the macro. */
#undef MAKE_MAPPUTELEM

static void MapPutU( AstKeyMap *this_keymap, const char *key, const char *comment,
                     int *status ) {
/*
*  Name:
*     MapPutU

*  Purpose:
*     Stores a undefined value in a cell of a Table.

*  Type:
*     Private function.

*  Synopsis:
*     #include "table.h"
*     void MapPutU( AstKeyMap *this, const char *key, const char *comment,
*                   int *status )

*  Class Membership:
*     Table member function (over-rides the astMapPutU method inherited
*     from the KeyMap class).

*  Description:
*     This function adds a new cell to a Table, but no value is stored with
*     the cell. The cell therefore has a special data type represented by
*     symbolic constant AST__UNDEFTYPE.
*
*     An example use is to add cells with undefined values to a Table
*     prior to locking them with the MapLocked attribute. Such cells
*     can act as placeholders for values that can be added to the KeyMap
*     later.

*  Parameters:
*     this
*        Pointer to the Table in which to store the supplied value.
*     key
*        A character string identifying the cell in which the value is
*        to be stored. It should have the form "COLNAME(irow)", where
*        "COLNAME" is replaced by the name of a column that has been
*        defined previously using the astAddColumn method, and "irow" is
*        an integer row index (the first row is row 1).
*     comment
*        A pointer to a null-terminated comment string to be stored with the
*        value. A NULL pointer may be supplied, in which case no comment is
*        stored.
*     status
*        Pointer to inherited status value.

*  Notes:
*     - Key names are case insensitive, and white space is considered
*     significant.
*     - The new undefined value will replace any old value already stored in
*     the Table for the specified cell.

*/

/* Local Variables: */
   AstTable *this;     /* Pointer to Table structure */
   char colname[ AST__MXCOLNAMLEN + 1 ]; /* Column name read from string */
   int irow;           /* Row index within key string */

/* Check the global error status. */
   if ( !astOK ) return;

/* Get a pointer to the Table structure. */
   this = (AstTable *) this_keymap;

/* If the key is the name of a global table parameter, use the parent
   method to put the value of the parameter. */
   if( astHasParameter( this, key ) ) {
      (*parent_mapputu)( this_keymap, key, comment, status );

/* Check the supplied key looks like a table cell key, and get the
   the column name and the row number. Also checks that the table
   contains a column with the specified name. */
   } else if( ParseKey( this, key, 1, colname, &irow, NULL, "astMapPutU",
                        status ) ) {

/* If the row index is larger than the current number of rows in the
   table, update the number of rows in the table. */
      if( irow > astGetNrow( this ) ) astSetNrow( this, irow );

/* Use the astMapPutU method in the parent keyMap class to store the
   new cell contents. */
      (*parent_mapputu)( this_keymap, key, comment, status );
   }
}

static const char *ParameterName( AstTable *this, int index, int *status ) {
/*
*++
*  Name:
c     astParameterName
f     AST_PARAMETERNAME

*  Purpose:
*     Get the name of the global parameter at a given index within the Table.

*  Type:
*     Public virtual function.

*  Synopsis:
c     #include "table.h"
c     const char *astParameterName( AstTable *this, int index )
f     RESULT = AST_PARAMETERNAME( THIS, INDEX, STATUS )

*  Class Membership:
*     Table method.

*  Description:
*     This function returns a string holding the name of the global parameter with
*     the given index within the Table.
*
*     This function is intended primarily as a means of iterating round all
*     the parameters in a Table. For this purpose, the number of parameters in
*     the Table is given by the Nparameter attribute of the Table. This function
*     could then be called in a loop, with the index value going from
c     zero to one less than Nparameter.
f     one to Nparameter.
*
*     Note, the index associated with a parameter decreases monotonically with
*     the age of the parameter: the oldest Parameter in the Table will have index
*     one, and the Parameter added most recently to the Table will have the
*     largest index.

*  Parameters:
c     this
f     THIS = INTEGER (Given)
*        Pointer to the Table.
c     index
f     INDEX = INTEGER (Given)
*        The index into the list of parameters. The first parameter has index
*        one, and the last has index "Nparameter".
f     STATUS = INTEGER (Given and Returned)
f        The global status.

*  Returned Value:
c     astParameterName()
c        A pointer to a null-terminated string containing the
f     AST_PARAMETERNAME = CHARACTER * ( AST__SZCHR )
f        The
*        upper case parameter name.

*  Notes:
c     - The returned pointer is guaranteed to remain valid and the
c     string to which it points will not be over-written for a total
c     of 50 successive invocations of this function. After this, the
c     memory containing the string may be re-used, so a copy of the
c     string should be made if it is needed for longer than this.
c     - A NULL pointer will be returned if this function is invoked
c     with the AST error status set, or if it should fail for any
c     reason.
f     - A blank string will be returned if this function is invoked
f     with STATUS set to an error value, or if it should fail for any
f     reason.
*--
*/

/* Local Variables: */
   AstKeyMap *pars;        /* KeyMap holding parameter definitions */
   const char *result;

/* Check the global error status. */
   if ( !astOK ) return NULL;

/* Get apointer to the KeyMap holding all parameter definitions. */
   pars = astParameterProps( this );

/* Issue a more useful error message than that issued by astMapKey if the
   index is invalid. */
   if( index < 1 || index > astMapSize( pars ) ) {
      astError( AST__MPIND, "astParameterName(%s): Cannot find parameter "
                "%d (zero-based) of the %s - invalid index.", status,
                astGetClass( this ), index, astGetClass( this ) );
   }

/* Get the parameter name. */
   result = astMapKey( pars, index - 1 );

/* Free resources. */
   pars = astAnnul( pars );

/* Return a pointer to the required parameter name. */
   return result;
}

static AstKeyMap *ParameterProps( AstTable *this, int *status ) {
/*
*+
*  Name:
*     astParameterProps

*  Purpose:
*     Returns a pointer to the KeyMap holding parameter properties.

*  Type:
*     Protected virtual function.

*  Synopsis:
*     #include "table.h"
*     AstKeyMap *astParameterProps( AstTable *this )

*  Class Membership:
*     Table method.

*  Description:
*     This function returns a pointer to the KeyMap that holds
*     definitions of all the parameters added to the Table.

*  Parameters:
*     this
*        Pointer to the Table.

*  Returned Value:
*        A pointer to the KeyMap. It should be annulled using astAnnul
*        when no longer needed.

*-
*/

/* Check the global error status. */
   if ( !astOK ) return NULL;

/* Return a cloned pointer to the required KeyMap. */
   return astClone( this->parameters );
}

static int ParseKey( AstTable *this, const char *key, int report,
                     char colname[ AST__MXCOLNAMLEN + 1 ], int *irow,
                     AstKeyMap **col_km, const char *method, int *status ){
/*
*  Name:
*     ParseKey

*  Purpose:
*     Find the column name and row index in a KeyMap key.

*  Type:
*     Private function.

*  Synopsis:
*     #include "table.h"
*     int ParseKey( AstTable *this, const char *key, int report,
*                   char colname[ AST__MXCOLNAMLEN + 1 ], int *irow,
*                   AstKeyMap **col_km, const char *method, int *status )

*  Class Membership:
*     Table member function

*  Description:
*     This function checks that the supplied KeyMap key conforms to the
*     format expected for Table cells: i.e. "COLNAME(irow)", where
*     "COLNAME" is the name of a column and "irow" is an integer row
*     index (the first row is row 1), An error is reported if this is
*     not the case.

*  Parameters:
*     this
*        Pointer to the table.
*     key
*        The key string to test.
*     report
*        If non-zero, an error will be reported if the key does not
*        correspond to a cell of an existing column. Otherwise, no
*        error will be reported.
*     colname
*        A buffer in which to return the column name.
*     irow
*        Address of an int in which to return the row index.
*     col_km
*        Address of a KeyMap pointer in which to return a pointer to the
*        KeyMap holding the information about the column. The returned
*        KeyMap pointer should be annulled using astAnnul when no lngerr
*        needed. If "col_km" is NULL, no KeyMap pointer is returned.
*     method
*        Pointer to a string holding the name of the method to include in
*        any error message.
*     status
*        Address of the inherited status value.

*  Returned Value:
*     Zero is returned if the key is not a valid Table cell key, or if an
*     error occurs.

*/

/* Local Variables: */
   AstKeyMap *cols;    /* KeyMap holding column definitions */
   int result;         /* Returned flag */
   int collen;         /* Length of column name */
   int nctot;          /* Number of characters read */

/* Initialise */
   result = 0;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Check the supplied key looks like a table cell key, and extract the
   column name and row number. */
   nctot = 0;
   if( 1 == astSscanf( key, "%*[^(]%n(%d) %n", &collen, irow, &nctot )
       && ( nctot >= strlen( key ) ) ) {

/* Check the column name is not too long. */
      if( collen > AST__MXCOLNAMLEN ) {
         if( report ) {
            astError( AST__BADKEY, "%s(%s): Failed to store a value for cell "
                      "\"%s\": column name is too long.", status, method,
                      astGetClass( this ), key );
         }

/* Check the row index is positive. */
      } else if( *irow < 1 ) {
         if( report ) {
            astError( AST__BADKEY, "%s(%s): Failed to store a value for cell "
                      "\"%s\": row index %d is invalid.", status, method,
                      astGetClass( this ), key, *irow );
         }

/* If the key looks OK so far... */
      } else {

/* Convert the column name to upper case and store in the returned buffer. */
         astChrCase( key, colname, 1, collen + 1 );
         colname[ collen ] = 0;

/* check that the column exists in the Table, returning a pointer to the
   column KeyMap is reequired. */
         cols = astColumnProps( this );
         if( col_km ) {
            result = astMapGet0A( cols, colname, col_km );
         } else {
            result = astMapHasKey( cols, colname );
         }
         cols = astAnnul( cols );

/* Report an error if the table does not contain the specified column. */
         if( !result && astOK && report) {
            astError( AST__BADKEY, "%s(%s): Failed to store a value for "
                      "cell \"%s\": the table does not contain a column "
                      "called '%s'.", status, method, astGetClass( this ),
                      key, colname );
         }
      }

/* Report an error if the cell key has the wrong format. */
   } else if( report ) {
      astError( AST__BADKEY, "%s(%s): Failed to store a value for cell "
                "\"%s\": the cell name is invalid.", status, method,
                astGetClass( this ), key );
   }

/* Return the result.*/
   return result;
}

static void PurgeRows( AstTable *this, int *status ) {
/*
*++
*  Name:
c     astPurgeRows
f     AST_PURGEROWS

*  Purpose:
*     Remove all empty rows from a table.

*  Type:
*     Public virtual function.

*  Synopsis:
c     #include "table.h"
c     void astPurgeRows( AstTable *this )
f     CALL AST_PURGEROWS( THIS, STATUS )

*  Class Membership:
*     Table method.

*  Description:
*     This function removes all empty rows from the Table, renaming
*     the key associated with each table cell accordingly.

*  Parameters:
c     this
f     THIS = INTEGER (Given)
*        Pointer to the Table.
f     STATUS = INTEGER (Given and Returned)
f        The global status.

*--
*/

/* Local Variables: */
   char newkey[ AST__MXCOLKEYLEN + 1 ]; /* New cell key string */
   char oldkey[ AST__MXCOLKEYLEN + 1 ]; /* Old cell key string */
   const char *col;              /* Column name */
   const char *key;              /* Pointer to key string */
   const char *op;               /* Pointer to opening parenthesis */
   int *w1;                      /* Work space pointer */
   int icol;                     /* Column index */
   int inew;                     /* New row index */
   int iold;                     /* Old row index */
   int ncol;                     /* Number of columns in table */
   int nrow;                     /* Number of rows in table */
   int reset;                    /* Start a new pass through the KeyMap? */

/* Check the global error status. */
   if ( !astOK ) return;

/* Get the number of rows in the table. */
   nrow = astGetNrow( this );

/* Create workspace to hold the number of defined values stored in each
   row. Initialise every row to have zero defined values. */
   w1 = astCalloc( nrow, sizeof( int ) );
   if( astOK ) {

/* Iterate round all keys in the KeyMap. */
      reset = 1;
      while( ( key = astMapIterate( this, reset ) ) && astOK ) {
         reset = 0;

/* Extract the row number from the key. */
         op = strchr( key, '(' );
         if( !op || astSscanf( op + 1, "%d", &iold ) != 1 ||
             iold > nrow ) {
            astError( AST__INTER, "astPurgeRows(%s): Illegal key '%s' "
                      "found in a %s (internal programming error).",
                      status, astGetClass( this ), key, astGetClass( this ) );

/* Increment the number of values in this row. Note row indices are
   one-based. */
         } else {
            w1[ iold - 1 ]++;
         }
      }

/* Loop round all columns in the Table. */
      ncol = astGetNcolumn( this );
      inew = nrow;
      for( icol = 1; icol <= ncol; icol++ ) {

/* Get the column name */
         col = astColumnName( this, icol );

/* Loop round all the old row numbers. Skip empty rows.*/
         inew = 0;
         for( iold = 0; iold < nrow; iold++ ) {
            if( w1[ iold ] > 0 ) {

/* Increment the row number to use in place of the old row number. If the
   old and new row numbers are the same, we do not need to rename the cell. */
               if( iold != inew++ ) {

/* For the old and new cell names */
                  sprintf( oldkey, "%s(%d)", col, iold + 1 );
                  sprintf( newkey, "%s(%d)", col, inew );

/* Rename the KeyMap entry. */
                  astMapRename( this, oldkey, newkey );
               }
            }
         }

/* If all rows were used, we do not need to check any more columns. */
         if( iold == inew ) break;
      }

/* Store the new number of rows. */
      astSetNrow( this, inew );
   }

/* Free resources. */
   w1 = astFree( w1 );

}

static void RemoveColumn( AstTable *this, const char *name, int *status ) {
/*
*++
*  Name:
c     astRemoveColumn
f     AST_REMOVECOLUMN

*  Purpose:
*     Remove a column from a table.

*  Type:
*     Public virtual function.

*  Synopsis:
c     #include "table.h"
c     void astRemoveColumn( AstTable *this, const char *name )
f     CALL AST_REMOVECOLUMN( THIS, NAME, STATUS )

*  Class Membership:
*     Table method.

*  Description:
*     This function removes a specified column from the supplied table.
*     The
c     function
f     routine
*     returns without action if the named column does not exist in the
*     Table (no error is reported).

*  Parameters:
c     this
f     THIS = INTEGER (Given)
*        Pointer to the Table.
c     name
f     NAME = CHARACTER * ( * ) (Given)
*        The column name. Trailing spaces are ignored (all other spaces
*        are significant). Case is significant.
f     STATUS = INTEGER (Given and Returned)
f        The global status.

*--
*/

/* Local Variables: */
   AstKeyMap *cols;      /* KeyMap holding column definitions */
   char key[ AST__MXCOLKEYLEN + 1 ]; /* Cell key string */
   int irow;             /* Row index */
   int namlen;           /* Used length of "name" */
   int nrow;             /* Number of rows in table */

/* Check the global error status. */
   if ( !astOK ) return;

/* Verify supplied values. */
   namlen = astChrLen( name );
   if( namlen == 0 ) {
      astError( AST__BADKEY, "astRemoveColumn(%s): Illegal blank column name "
               "supplied.", status, astGetClass( this ) );
   }

/* Get the number of rows in the table. */
   nrow = astGetNrow( this );

/* If there is no column with the given name in the Table, do nothing more. */
   cols = astColumnProps( this );
   if( astOK && astMapHasKey( cols, name ) ) {

/* Remove the column description from the columns keymap. */
      astMapRemove( cols, name );

/* Remove any column cells with defined values from the parent KeyMap. */
      for( irow = 1; irow <= nrow; irow++ ) {
         sprintf( key, "%.*s(%d)", namlen, name, irow );
         (*parent_mapremove)( (AstKeyMap *) this, key, status );
      }
   }
   cols = astAnnul( cols );
}

static void RemoveParameter( AstTable *this, const char *name, int *status ) {
/*
*++
*  Name:
c     astRemoveParameter
f     AST_REMOVEPARAMETER

*  Purpose:
*     Remove a global parameter from a table.

*  Type:
*     Public virtual function.

*  Synopsis:
c     #include "table.h"
c     void astRemoveParameter( AstTable *this, const char *name )
f     CALL AST_REMOVEPARAMETER( THIS, NAME, STATUS )

*  Class Membership:
*     Table method.

*  Description:
*     This function removes a specified global parameter from the supplied table.
*     The
c     function
f     routine
*     returns without action if the named parameter does not exist in the
*     Table (no error is reported).

*  Parameters:
c     this
f     THIS = INTEGER (Given)
*        Pointer to the Table.
c     name
f     NAME = CHARACTER * ( * ) (Given)
*        The parameter name. Trailing spaces are ignored (all other spaces
*        are significant). Case is significant.
f     STATUS = INTEGER (Given and Returned)
f        The global status.

*--
*/

/* Local Variables: */
   AstKeyMap *pars;      /* KeyMap holding parameter definitions */

/* Check the global error status. */
   if ( !astOK ) return;

/* Verify supplied values. */
   if( astChrLen( name ) == 0 ) {
      astError( AST__BADKEY, "astRemoveParameter(%s): Illegal blank parameter name "
               "supplied.", status, astGetClass( this ) );
   }

/* If there is no parameter with the given name in the Table, do nothing more. */
   pars = astParameterProps( this );
   if( astOK && astMapHasKey( pars, name ) ) {

/* Remove the parameter description from the parameters keymap. */
      astMapRemove( pars, name );

/* Remove any entry holding the parameter value from the parent KeyMap. */
      (*parent_mapremove)( (AstKeyMap *) this, name, status );
   }
   pars = astAnnul( pars );
}

static void RemoveRow( AstTable *this, int index, int *status ) {
/*
*++
*  Name:
c     astRemoveRow
f     AST_REMOVEROW

*  Purpose:
*     Remove a row from a table.

*  Type:
*     Public virtual function.

*  Synopsis:
c     #include "table.h"
c     void astRemoveRow( AstTable *this, int index )
f     CALL AST_REMOVEROW( THIS, INDEX, STATUS )

*  Class Membership:
*     Table method.

*  Description:
*     This function removes a specified row from the supplied table.
*     The
c     function
f     routine
*     returns without action if the row does not exist in the
*     Table (no error is reported).

*  Parameters:
c     this
f     THIS = INTEGER (Given)
*        Pointer to the Table.
c     index
f     INDEX = INTEGER (Given)
*        The index of the row to be removed. The first row has index 1.
f     STATUS = INTEGER (Given and Returned)
f        The global status.

*--
*/

/* Local Variables: */
   AstKeyMap *cols;              /* KeyMap holding column definitions */
   char key[ AST__MXCOLKEYLEN + 1 ]; /* Cell key string */
   const char *col;              /* Column name */
   int icol;                     /* Column index */
   int ncol;                     /* Number of columns in table */
   int nrow;                     /* Number of rows in table */

/* Check the global error status. */
   if ( !astOK ) return;

/* Get the number of rows in the table. */
   nrow = astGetNrow( this );

/* Do nothing if the specified row is out of bounds. */
   if( index > 0 && index <= nrow ) {

/* Loop round all columns in the table. */
      cols = astColumnProps( this );
      ncol = astMapSize( cols );
      for( icol = 0; icol < ncol; icol++ ) {
         col = astMapKey( cols, icol );

/* Remove the cell of the current column at the requested row. */
         sprintf( key, "%s(%d)", col, index );
         (*parent_mapremove)( (AstKeyMap *) this, key, status );
      }
      cols = astAnnul( cols );

/* If the removed row was the last row, reduce the number of rows in the
   Table. */
      if( index == nrow ) astSetNrow( this, index - 1 );
   }
}

static void SetAttrib( AstObject *this_object, const char *setting, int *status ) {
/*
*  Name:
*     SetAttrib

*  Purpose:
*     Set an attribute value for a Table.

*  Type:
*     Private function.

*  Synopsis:
*     #include "table.h"
*     void SetAttrib( AstObject *this, const char *setting, int *status )

*  Class Membership:
*     Table member function (over-rides the astSetAttrib protected
*     method inherited from the KeyMap class).

*  Description:
*     This function assigns an attribute value for a Table, the
*     attribute and its value being specified by means of a string of
*     the form:
*
*        "attribute= value "
*
*     Here, "attribute" specifies the attribute name and should be in
*     lower case with no white space present. The value to the right
*     of the "=" should be a suitable textual representation of the
*     value to be assigned and this will be interpreted according to
*     the attribute's data type.  White space surrounding the value is
*     only significant for string attributes.

*  Parameters:
*     this
*        Pointer to the Table.
*     setting
*        Pointer to a null terminated string specifying the new attribute
*        value.
*     status
*        Pointer to the inherited status variable.
*/

/* Local Variables: */
   AstTable *this;               /* Pointer to the Table structure */
   int len;                      /* Length of setting string */
   int nc;                       /* Number of characters read by astSscanf */

/* Check the global error status. */
   if ( !astOK ) return;

/* Obtain a pointer to the Table structure. */
   this = (AstTable *) this_object;

/* Obtain the length of the setting string. */
   len = (int) strlen( setting );

/* Test for each recognised attribute in turn, using "astSscanf" to parse
   the setting string and extract the attribute value (or an offset to
   it in the case of string values). In each case, use the value set
   in "nc" to check that the entire string was matched. Once a value
   has been obtained, use the appropriate method to set it. */
   /* None as yet */

/* Define a macro to see if the setting string matches any of the
   read-only attributes of this class. */
#define MATCH(attrib) \
        ( nc = 0, ( 0 == astSscanf( setting, attrib "=%*[^\n]%n", &nc ) ) && \
                  ( nc >= len ) )

#define MATCH2(attrib) \
        ( nc = 0, ( 0 == astSscanf( setting, attrib "(%*s) =%*[^\n]%n", &nc ) ) && \
                  ( nc >= len ) )

/* If the attribute was not recognised, use this macro to report an error
   if a read-only attribute has been specified. */
   if ( MATCH( "ncolumn" ) ||
        MATCH( "nparameter" ) ||
        MATCH( "nrow" ) ||
        MATCH2( "columnlenc" ) ||
        MATCH2( "columnlength" ) ||
        MATCH2( "columnndim" ) ||
        MATCH2( "columntype" ) ||
        MATCH2( "columnunit" ) ) {
      astError( AST__NOWRT, "astSet: The setting \"%s\" is invalid for a %s.", status,
                setting, astGetClass( this ) );
      astError( AST__NOWRT, "This is a read-only attribute." , status);

/* If the attribute is still not recognised, pass it on to the parent
   method for further interpretation. */
   } else {
      (*parent_setattrib)( this_object, setting, status );
   }

/* Undefine macros local to this function. */
#undef MATCH
#undef MATCH2
}

static void SetKeyCase( AstKeyMap *this, int keycase, int *status ) {
/*
*  Name:
*     SetKeyCase

*  Purpose:
*     Set a value for the KeyCase attribute value for a Table.

*  Type:
*     Private function.

*  Synopsis:
*     #include "keymape.h"
*     void SetKeyCase( AstKeyMap *this, int keycase, int *status )

*  Class Membership:
*     Table member function (over-rides the astSetKeyCase protected
*     method inherited from the KeyMap class).

*  Description:
*     This function assigns a new valeu to the KeyCase attribute for a
*     Table. For a Table, the KeyCase attribute cannot be changed so this
*     function exits without action.

*  Parameters:
*     this
*        Pointer to the Table.
*     keycase
*        The new value to set.
*/

}

static int TestAttrib( AstObject *this_object, const char *attrib, int *status ) {
/*
*  Name:
*     TestAttrib

*  Purpose:
*     Test if a specified attribute value is set for a Table.

*  Type:
*     Private function.

*  Synopsis:
*     #include "table.h"
*     int TestAttrib( AstObject *this, const char *attrib, int *status )

*  Class Membership:
*     Table member function (over-rides the astTestAttrib protected
*     method inherited from the KeyMap class).

*  Description:
*     This function returns a boolean result (0 or 1) to indicate whether
*     a value has been set for one of a Table's attributes.

*  Parameters:
*     this
*        Pointer to the Table.
*     attrib
*        Pointer to a null terminated string specifying the attribute
*        name.  This should be in lower case with no surrounding white
*        space.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     One if a value has been set, otherwise zero.

*  Notes:
*     - A value of zero will be returned if this function is invoked
*     with the global status set, or if it should fail for any reason.
*/

/* Local Variables: */
   int len;                      /* Length of attribute string */
   int nc;                       /* Number of characters read by astSscanf */
   int result;                   /* Result value to return */

/* Initialise. */
   result = 0;

/* Check the global error status. */
   if ( !astOK ) return result;

/* Get the length of the attribute string. */
   len = strlen( attrib );

/* Check the attribute name and test the appropriate attribute. */
   /* None as yet */

/* Define a macro to see if the attribute string matches any of the
   read-only column attributes of this class. */
#define MATCH(attr) \
        ( nc = 0, ( 0 == astSscanf( attrib, attr "(%*s)%n", &nc ) ) && \
                  ( nc >= len ) )

/* If the name is not recognised, test if it matches any of the
   read-only attributes of this class. If it does, then return
   zero. */
   if ( !strcmp( attrib, "ncolumn" ) ||
        !strcmp( attrib, "nparameter" ) ||
        !strcmp( attrib, "nrow" ) ||
        MATCH( "columnlenc" ) ||
        MATCH( "columnlength" ) ||
        MATCH( "columnndim" ) ||
        MATCH( "columntype" ) ||
        MATCH( "columnunit" ) ) {
      result = 0;

/* If the attribute is still not recognised, pass it on to the parent
   method for further interpretation. */
   } else {
      result = (*parent_testattrib)( this_object, attrib, status );
   }

/* Return the result, */
   return result;

#undef MATCH
}

static const char *TypeString( int type ) {
/*
*  Name:
*     TypeString

*  Purpose:
*     Return a pointer to a string describing a data type.

*  Type:
*     Private function.

*  Synopsis:
*      const char *TypeString( int type );

*  Description:
*     This function returns a pointer to a string describing a data type.

*  Parameters:
*     type
*        The integer data type code.

*  Returned Value:
*     Pointer to a a string descirbing the data type (typically the C
*     data type).

*/

/* Local Variables: */
   const char *result;

/* Compare the supplied type code against each supported value. */
   if( type == AST__INTTYPE ) {
      result = "int";

   } else if( type == AST__BYTETYPE ) {
      result = "byte";

   } else if( type == AST__DOUBLETYPE ) {
      result = "double";

   } else if( type == AST__STRINGTYPE ) {
      result = "string";

   } else if( type == AST__OBJECTTYPE ) {
      result = "Object";

   } else if( type == AST__FLOATTYPE ) {
      result = "float";

   } else if( type == AST__POINTERTYPE ) {
      result = "pointer";

   } else if( type == AST__SINTTYPE ) {
      result = "short int";

   } else if( type == AST__UNDEFTYPE ) {
      result = "undefined";

   } else {
      result = NULL;
   }

/* Return the result. */
   return result;
}


/* Functions which access class attributes. */
/* ---------------------------------------- */
/* Implement member functions to access the attributes associated with
   this class using the macros defined for this purpose in the
   "object.h" file. For a description of each attribute, see the class
   interface (in the associated .h file). */

/*
*att++
*  Name:
*     ColumnLenC(column)

*  Purpose:
*     The largest string length of any value in a column

*  Type:
*     Public attribute.

*  Synopsis:
*     Integer, read-only.

*  Description:
*     This attribute holds the minimum length which a character variable
*     must have in order to be able to store the longest value currently
*     present (at any row) in a specified column of the supplied Table.
c     This does not include room for a trailing null character.
*     The required column name should be placed inside the parentheses in
*     the attribute name. If the named column holds vector values, then
*     the attribute value is the length of the longest element of the
*     vector value.

*  Applicability:
*     Table
*        All Tables have this attribute.

*  Notes:
*     - If the named column holds numerical values, the length returned
*     is the length of the largest string that would be generated if the
*     column values were accessed as strings.

*att--
*/

/*
*att++
*  Name:
*     ColumnLength(column)

*  Purpose:
*     The number of elements in each value in a column

*  Type:
*     Public attribute.

*  Synopsis:
*     Integer, read-only.

*  Description:
*     This attribute holds the number of elements in each value stored
*     in a named column. Each value can be a scalar (in which case the
*     ColumnLength attribute has a value of 1), or a multi-dimensional
*     array ( in which case the ColumnLength value is equal to the
*     product of the array dimensions).

*  Applicability:
*     Table
*        All Tables have this attribute.

*att--
*/

/*
*att++
*  Name:
*     ColumnNdim(column)

*  Purpose:
*     The number of axes spanned by each value in a column

*  Type:
*     Public attribute.

*  Synopsis:
*     Integer, read-only.

*  Description:
*     This attribute holds the number of axes spanned by each value in a
*     column. If each cell in the column is a scalar, ColumnNdim will be
*     zero. If each cell in the column is a 1D spectrum, ColumnNdim will
*     be one. If each cell in the column is a 2D image, ColumnNdim will be
*     two, etc. The required column name should be placed inside the
*     parentheses in the attribute name.

*  Applicability:
*     Table
*        All Tables have this attribute.

*att--
*/

/*
*att++
*  Name:
*     ColumnType(column)

*  Purpose:
*     The data type of each value in a column

*  Type:
*     Public attribute.

*  Synopsis:
*     Integer, read-only.

*  Description:
*     This attribute holds a integer value indicating the data type of
*     a named column in a Table. This is the data type which was used
*     when the column was added to the Table using astAddColumn. The
*     required column name should be placed inside the parentheses in
*     the attribute name.
*
*     The attribute value will be one of AST__INTTYPE (for integer),
*     AST__SINTTYPE (for
c     short int),
f     INTEGER*2),
*     AST__BYTETYPE (for
c     unsigned bytes - i.e. unsigned chars),
f     bytes),
*     AST__DOUBLETYPE (for double
*     precision floating point), AST__FLOATTYPE (for single
*     precision floating point), AST__STRINGTYPE (for character string),
*     AST__OBJECTTYPE (for AST Object pointer), AST__POINTERTYPE (for
*     arbitrary C pointer) or AST__UNDEFTYPE (for undefined values
*     created by
c     astMapPutU).
f     AST_MAPPUTU).

*  Applicability:
*     Table
*        All Tables have this attribute.

*att--
*/

/*
*att++
*  Name:
*     Ncolumn

*  Purpose:
*     The number of columns in the table.

*  Type:
*     Public attribute.

*  Synopsis:
*     Integer, read-only.

*  Description:
*     This attribute holds the number of columns currently in the table. Columns
*     are added and removed using the
c     astAddColumn and astRemoveColumn
f     AST_ADDCOLUMN and AST_REMOVECOLUMN
*     functions.

*  Applicability:
*     Table
*        All Tables have this attribute.

*att--
*/

/*
*att++
*  Name:
*     Nparameter

*  Purpose:
*     The number of global parameters in the table.

*  Type:
*     Public attribute.

*  Synopsis:
*     Integer, read-only.

*  Description:
*     This attribute holds the number of global parameters currently in the table.
*     Parameters are added and removed using the
c     astAddParameter and astRemoveParameter
f     AST_ADDPARAMETER and AST_REMOVEPARAMETER
*     functions.

*  Applicability:
*     Table
*        All Tables have this attribute.

*att--
*/

/*
*att++
*  Name:
*     Nrow

*  Purpose:
*     The number of rows in the table.

*  Type:
*     Public attribute.

*  Synopsis:
*     Integer, read-only.

*  Description:
*     This attribute holds the index of the last row to which any
*     contents have been added using any of the
*     astMapPut...
*     AST_MAPPUT...
*     functions. The first row has index 1.

*  Applicability:
*     Table
*        All Tables have this attribute.

*att--
*/
astMAKE_GET(Table,Nrow,int,0,this->nrow)
astMAKE_SET(Table,Nrow,int,nrow,value)



/* Copy constructor. */
/* ----------------- */
static void Copy( const AstObject *objin, AstObject *objout, int *status ) {
/*
*  Name:
*     Copy

*  Purpose:
*     Copy constructor for Table objects.

*  Type:
*     Private function.

*  Synopsis:
*     void Copy( const AstObject *objin, AstObject *objout, int *status )

*  Description:
*     This function implements the copy constructor for Table objects.

*  Parameters:
*     objin
*        Pointer to the object to be copied.
*     objout
*        Pointer to the object being constructed.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     void

*  Notes:
*     -  This constructor makes a deep copy, including a copy of the component
*     Mappings within the Table.
*/

/* Local Variables: */
   AstTable *in;                /* Pointer to input Table */
   AstTable *out;               /* Pointer to output Table */

/* Check the global error status. */
   if ( !astOK ) return;

/* Obtain pointers to the input and output Tables. */
   in = (AstTable *) objin;
   out = (AstTable *) objout;

/* Make copies of the component KeyMaps and store pointers to them in the
   output Table structure. */
   out->columns = in->columns ? astCopy( in->columns ) : NULL;
   out->parameters = in->parameters ? astCopy( in->parameters ) : NULL;
}


/* Destructor. */
/* ----------- */
static void Delete( AstObject *obj, int *status ) {
/*
*  Name:
*     Delete

*  Purpose:
*     Destructor for Table objects.

*  Type:
*     Private function.

*  Synopsis:
*     void Delete( AstObject *obj, int *status )

*  Description:
*     This function implements the destructor for Table objects.

*  Parameters:
*     obj
*        Pointer to the object to be deleted.
*     status
*        Pointer to the inherited status variable.

*  Returned Value:
*     void

*  Notes:
*     This function attempts to execute even if the global error status is
*     set.
*/

/* Local Variables: */
   AstTable *this;              /* Pointer to Table */

/* Obtain a pointer to the Table structure. */
   this = (AstTable *) obj;

/* Annul the pointers to the component KeyMaps. */
   if( this->columns ) this->columns = astAnnul( this->columns );
   if( this->parameters ) this->parameters = astAnnul( this->parameters );

}


/* Dump function. */
/* -------------- */
static void Dump( AstObject *this_object, AstChannel *channel, int *status ) {
/*
*  Name:
*     Dump

*  Purpose:
*     Dump function for Table objects.

*  Type:
*     Private function.

*  Synopsis:
*     void Dump( AstObject *this, AstChannel *channel, int *status )

*  Description:
*     This function implements the Dump function which writes out data
*     for the Table class to an output Channel.

*  Parameters:
*     this
*        Pointer to the Table whose data are being written.
*     channel
*        Pointer to the Channel to which the data are being written.
*     status
*        Pointer to the inherited status variable.
*/

/* Local Variables: */
   AstTable *this;             /* Pointer to the Table structure */

/* Check the global error status. */
   if ( !astOK ) return;

/* Obtain a pointer to the Table structure. */
   this = (AstTable *) this_object;

/* Write out values representing the instance variables for the Table
   class.  Note, the primitive data in the Table will be written out
   by the parent KeyMap Dump function. This function deals just with the
   extra information held in the Table structure. */

/* Write out the number of rows in the table. */
   astWriteInt( channel, "Nrow", 1, 1, astGetNrow( this ),
                "Number of rows in table" );

/* Write out the KeyMap holding definitions of each column. */
   if( this->columns ) {
      astWriteObject( channel, "Columns", 1, 0, this->columns, "KeyMap holding "
                      "column definitions" );
   }

/* Write out the KeyMap holding definitions of each global parameter. */
   if( this->parameters ) {
      astWriteObject( channel, "Params", 1, 0, this->parameters, "KeyMap holding "
                      "parameter definitions" );
   }
}






/* Standard class functions. */
/* ========================= */
/* Implement the astIsATable and astCheckTable functions using the macros
   defined for this purpose in the "object.h" header file. */
astMAKE_ISA(Table,KeyMap)
astMAKE_CHECK(Table)

AstTable *astTable_( const char *options, int *status, ...) {
/*
*++
*  Name:
c     astTable
f     AST_TABLE

*  Purpose:
*     Create a Table.

*  Type:
*     Public function.

*  Synopsis:
c     #include "table.h"
c     AstTable *astTable( const char *options, ... )
f     RESULT = AST_TABLE( OPTIONS, STATUS )

*  Class Membership:
*     Table constructor.

*  Description:
*     This function creates a new empty Table and optionally initialises
*     its attributes.
*
*     The Table class is a type of KeyMap that represents a two-dimensional
*     table of values. The
c     astMapGet... and astMapPut...
f     AST_MAPGET... and AST_MAPPUT...
*     methods provided by the KeyMap class should be used for storing and
*     retrieving values from individual cells within a Table. Each entry
*     in the KeyMap represents a single cell of the table and has an
*     associated key of the form "<COL>(i)" where "<COL>" is the name of a
*     table column and "i" is the row index (the first row is row 1). Keys
*     of this form should always be used when using KeyMap methods to access
*     entries within a Table.
*
*     Columns must be declared using the
c     astAddColumn
f     AST_ADDCOLUMN
*     method before values can be stored within them. This also fixes the
*     type and shape of the values that may be stored in any cell of the
*     column. Cells may contain scalar or vector values of any data type
*     supported by the KeyMap class. Multi-dimensional arrays may also be
*     stored, but these must be vectorised when storing and retrieving
*     them within a table cell. All cells within a single column must
*     have the same type and shape (specified when the column is declared).
*
*     Tables may have parameters that describe global properties of the
*     entire table. These are stored as entries in the parent KeyMap and
*     can be access using the get and set method of the KeyMap class.
*     However, parameters must be declared using the
c     astAddParameter
f     AST_ADDPARAMETER
*     method before being accessed.
*
*     Note - since accessing entries within a KeyMap is a relatively slow
*     process, it is not recommended to use the Table class to store
*     very large tables.

*  Parameters:
c     options
f     OPTIONS = CHARACTER * ( * ) (Given)
c        Pointer to a null-terminated string containing an optional
c        comma-separated list of attribute assignments to be used for
c        initialising the new Table. The syntax used is identical to
c        that for the astSet function and may include "printf" format
c        specifiers identified by "%" symbols in the normal way.
f        A character string containing an optional comma-separated
f        list of attribute assignments to be used for initialising the
f        new Table. The syntax used is identical to that for the
f        AST_SET routine.
c     ...
c        If the "options" string contains "%" format specifiers, then
c        an optional list of additional arguments may follow it in
c        order to supply values to be substituted for these
c        specifiers. The rules for supplying these are identical to
c        those for the astSet function (and for the C "printf"
c        function).
f     STATUS = INTEGER (Given and Returned)
f        The global status.

*  Returned Value:
c     astTable()
f     AST_TABLE = INTEGER
*        A pointer to the new Table.

*  Notes:
*     - A null Object pointer (AST__NULL) will be returned if this
c     function is invoked with the AST error status set, or if it
f     function is invoked with STATUS set to an error value, or if it
*     should fail for any reason.

*  Status Handling:
*     The protected interface to this function includes an extra
*     parameter at the end of the parameter list described above. This
*     parameter is a pointer to the integer inherited status
*     variable: "int *status".

*--
*/

/* Local Variables: */
   astDECLARE_GLOBALS            /* Pointer to thread-specific global data */
   AstTable *new;              /* Pointer to new Table */
   va_list args;                 /* Variable argument list */

/* Get a pointer to the thread specific global data structure. */
   astGET_GLOBALS(NULL);

/* Check the global status. */
   if ( !astOK ) return NULL;

/* Initialise the Table, allocating memory and initialising the
   virtual function table as well if necessary. */
   new = astInitTable( NULL, sizeof( AstTable ), !class_init, &class_vtab,
                       "Table" );

/* If successful, note that the virtual function table has been
   initialised. */
   if ( astOK ) {
      class_init = 1;

/* Obtain the variable argument list and pass it along with the options string
   to the astVSet method to initialise the new Table's attributes. */
      va_start( args, status );
      astVSet( new, options, NULL, args );
      va_end( args );

/* If an error occurred, clean up by deleting the new object. */
      if ( !astOK ) new = astDelete( new );
   }

/* Return a pointer to the new Table. */
   return new;
}

AstTable *astTableId_( const char *options, ... ) {
/*
*  Name:
*     astTableId_

*  Purpose:
*     Create a Table.

*  Type:
*     Private function.

*  Synopsis:
*     #include "table.h"
*     AstTable *astTableId_( const char *options, ... )

*  Class Membership:
*     Table constructor.

*  Description:
*     This function implements the external (public) interface to the
*     astTable constructor function. It returns an ID value (instead
*     of a true C pointer) to external users, and must be provided
*     because astTable_ has a variable argument list which cannot be
*     encapsulated in a macro (where this conversion would otherwise
*     occur).
*
*     The variable argument list also prevents this function from
*     invoking astTable_ directly, so it must be a re-implementation
*     of it in all respects, except for the final conversion of the
*     result to an ID value.

*  Parameters:
*     As for astTable_.

*  Returned Value:
*     The ID value associated with the new Table.
*/

/* Local Variables: */
   astDECLARE_GLOBALS            /* Pointer to thread-specific global data */
   AstTable *new;                /* Pointer to new Table */
   va_list args;                 /* Variable argument list */

   int *status;                  /* Pointer to inherited status value */

/* Get a pointer to the inherited status value. */
   status = astGetStatusPtr;

/* Get a pointer to the thread specific global data structure. */
   astGET_GLOBALS(NULL);

/* Check the global status. */
   if ( !astOK ) return NULL;

/* Initialise the Table, allocating memory and initialising the
   virtual function table as well if necessary. */
   new = astInitTable( NULL, sizeof( AstTable ), !class_init, &class_vtab,
                         "Table" );

/* If successful, note that the virtual function table has been
   initialised. */
   if ( astOK ) {
      class_init = 1;

/* Obtain the variable argument list and pass it along with the options string
   to the astVSet method to initialise the new Table's attributes. */
      va_start( args, options );
      astVSet( new, options, NULL, args );
      va_end( args );

/* If an error occurred, clean up by deleting the new object. */
      if ( !astOK ) new = astDelete( new );
   }

/* Return an ID value for the new Table. */
   return astMakeId( new );
}

AstTable *astInitTable_( void *mem, size_t size, int init,
                         AstTableVtab *vtab, const char *name, int *status ) {
/*
*+
*  Name:
*     astInitTable

*  Purpose:
*     Initialise a Table.

*  Type:
*     Protected function.

*  Synopsis:
*     #include "table.h"
*     AstTable *astInitTable( void *mem, size_t size, int init,
*                                 AstTableVtab *vtab, const char *name )

*  Class Membership:
*     Table initialiser.

*  Description:
*     This function is provided for use by class implementations to initialise
*     a new Table object. It allocates memory (if necessary) to accommodate
*     the Table plus any additional data associated with the derived class.
*     It then initialises a Table structure at the start of this memory. If
*     the "init" flag is set, it also initialises the contents of a virtual
*     function table for a Table at the start of the memory passed via the
*     "vtab" parameter.

*  Parameters:
*     mem
*        A pointer to the memory in which the Table is to be initialised.
*        This must be of sufficient size to accommodate the Table data
*        (sizeof(Table)) plus any data used by the derived class. If a value
*        of NULL is given, this function will allocate the memory itself using
*        the "size" parameter to determine its size.
*     size
*        The amount of memory used by the Table (plus derived class data).
*        This will be used to allocate memory if a value of NULL is given for
*        the "mem" parameter. This value is also stored in the Table
*        structure, so a valid value must be supplied even if not required for
*        allocating memory.
*     init
*        A logical flag indicating if the Table's virtual function table is
*        to be initialised. If this value is non-zero, the virtual function
*        table will be initialised by this function.
*     vtab
*        Pointer to the start of the virtual function table to be associated
*        with the new Table.
*     name
*        Pointer to a constant null-terminated character string which contains
*        the name of the class to which the new object belongs (it is this
*        pointer value that will subsequently be returned by the astGetClass
*        method).

*  Returned Value:
*     A pointer to the new Table.

*  Notes:
*     -  A null pointer will be returned if this function is invoked with the
*     global error status set, or if it should fail for any reason.
*-
*/

/* Local Variables: */
   AstTable *new;              /* Pointer to new Table */

/* Check the global status. */
   if ( !astOK ) return NULL;

/* If necessary, initialise the virtual function table. */
   if ( init ) astInitTableVtab( vtab, name );

/* Initialise. */
   new = NULL;

/* Initialise a KeyMap structure (the parent class) as the first component
   within the Table structure, allocating memory if necessary. Specify that
   the KeyMap should be defined in both the forward and inverse directions. */
   new = (AstTable *) astInitKeyMap( mem, size, 0, (AstKeyMapVtab *) vtab,
                                     name );
   if ( astOK ) {

/* Initialise the Table data. */
/* ---------------------------- */
      new->nrow = 0;
      new->columns = astKeyMap( "KeyCase=0,Sortby=AgeDown", status );
      new->parameters = astKeyMap( "KeyCase=0,Sortby=AgeDown", status );

/* Tables require the KeyCase attribute to be zero. */
      (*parent_setkeycase)( (AstKeyMap *) new, 0, status );

/* If an error occurred, clean up by deleting the new Table. */
      if ( !astOK ) new = astDelete( new );
   }

/* Return a pointer to the new Table. */
   return new;
}

AstTable *astLoadTable_( void *mem, size_t size, AstTableVtab *vtab,
                         const char *name, AstChannel *channel, int *status ) {
/*
*+
*  Name:
*     astLoadTable

*  Purpose:
*     Load a Table.

*  Type:
*     Protected function.

*  Synopsis:
*     #include "table.h"
*     AstTable *astLoadTable( void *mem, size_t size, AstTableVtab *vtab,
*                             const char *name, AstChannel *channel )

*  Class Membership:
*     Table loader.

*  Description:
*     This function is provided to load a new Table using data read
*     from a Channel. It first loads the data used by the parent class
*     (which allocates memory if necessary) and then initialises a
*     Table structure in this memory, using data read from the input
*     Channel.
*
*     If the "init" flag is set, it also initialises the contents of a
*     virtual function table for a Table at the start of the memory
*     passed via the "vtab" parameter.


*  Parameters:
*     mem
*        A pointer to the memory into which the Table is to be
*        loaded.  This must be of sufficient size to accommodate the
*        Table data (sizeof(Table)) plus any data used by derived
*        classes. If a value of NULL is given, this function will
*        allocate the memory itself using the "size" parameter to
*        determine its size.
*     size
*        The amount of memory used by the Table (plus derived class
*        data).  This will be used to allocate memory if a value of
*        NULL is given for the "mem" parameter. This value is also
*        stored in the Table structure, so a valid value must be
*        supplied even if not required for allocating memory.
*
*        If the "vtab" parameter is NULL, the "size" value is ignored
*        and sizeof(AstTable) is used instead.
*     vtab
*        Pointer to the start of the virtual function table to be
*        associated with the new Table. If this is NULL, a pointer
*        to the (static) virtual function table for the Table class
*        is used instead.
*     name
*        Pointer to a constant null-terminated character string which
*        contains the name of the class to which the new object
*        belongs (it is this pointer value that will subsequently be
*        returned by the astGetClass method).
*
*        If the "vtab" parameter is NULL, the "name" value is ignored
*        and a pointer to the string "Table" is used instead.

*  Returned Value:
*     A pointer to the new Table.

*  Notes:
*     - A null pointer will be returned if this function is invoked
*     with the global error status set, or if it should fail for any
*     reason.
*-
*/

/* Local Variables: */
   astDECLARE_GLOBALS            /* Pointer to thread-specific global data */
   AstTable *new;                /* Pointer to the new Table */

/* Initialise. */
   new = NULL;

/* Check the global error status. */
   if ( !astOK ) return new;

/* Get a pointer to the thread specific global data structure. */
   astGET_GLOBALS(channel);

/* If a NULL virtual function table has been supplied, then this is
   the first loader to be invoked for this Table. In this case the
   Table belongs to this class, so supply appropriate values to be
   passed to the parent class loader (and its parent, etc.). */
   if ( !vtab ) {
      size = sizeof( AstTable );
      vtab = &class_vtab;
      name = "Table";

/* If required, initialise the virtual function table for this class. */
      if ( !class_init ) {
         astInitTableVtab( vtab, name );
         class_init = 1;
      }
   }

/* Invoke the parent class loader to load data for all the ancestral
   classes of the current one, returning a pointer to the resulting
   partly-built Table. */
   new = astLoadKeyMap( mem, size, (AstKeyMapVtab *) vtab, name,
                         channel );

   if ( astOK ) {

/* Read input data. */
/* ================ */
/* Request the input Channel to read all the input data appropriate to
   this class into the internal "values list". */
      astReadClassData( channel, "Table" );

/* Now read each individual data item from this list and use it to
   initialise the appropriate instance variable(s) for this class. */

/* The number of rows. */
      new->nrow = astReadInt( channel, "nrow", 0 );

/* KeyMap holding columns definitions. */
      new->columns = astReadObject( channel, "columns", NULL );

/* KeyMap holding parameter definitions. */
      new->parameters = astReadObject( channel, "params", NULL );

/* If an error occurred, clean up by deleting the new Table. */
      if ( !astOK ) new = astDelete( new );
   }

/* Return the new Table pointer. */
   return new;
}

/* Virtual function interfaces. */
/* ============================ */
/* These provide the external interface to the virtual functions defined by
   this class. Each simply checks the global error status and then locates and
   executes the appropriate member function, using the function pointer stored
   in the object's virtual function table (this pointer is located using the
   astMEMBER macro defined in "object.h").

   Note that the member function may not be the one defined here, as it may
   have been over-ridden by a derived class. However, it should still have the
   same interface. */

void astAddColumn_( AstTable *this, const char *name, int type,
                    int ndim, int *dims, const char *unit, int *status ) {
   if ( !astOK ) return;
   (**astMEMBER(this,Table,AddColumn))(this,name,type,ndim,dims,unit,status);
}
void astAddParameter_( AstTable *this, const char *name, int *status ) {
   if ( !astOK ) return;
   (**astMEMBER(this,Table,AddParameter))(this,name,status);
}
void astRemoveColumn_( AstTable *this, const char *name, int *status ){
   if ( !astOK ) return;
   (**astMEMBER(this,Table,RemoveColumn))(this,name,status);
}
void astRemoveParameter_( AstTable *this, const char *name, int *status ){
   if ( !astOK ) return;
   (**astMEMBER(this,Table,RemoveParameter))(this,name,status);
}
void astRemoveRow_( AstTable *this, int index, int *status ){
   if ( !astOK ) return;
   (**astMEMBER(this,Table,RemoveRow))(this,index,status);
}
void astPurgeRows_( AstTable *this, int *status ){
   if ( !astOK ) return;
   (**astMEMBER(this,Table,PurgeRows))(this,status);
}
int astGetNcolumn_( AstTable *this, int *status ) {
   if ( !astOK ) return 0;
   return (**astMEMBER(this,Table,GetNcolumn))( this, status );
}
int astGetNparameter_( AstTable *this, int *status ) {
   if ( !astOK ) return 0;
   return (**astMEMBER(this,Table,GetNparameter))( this, status );
}
const char *astColumnName_( AstTable *this, int index, int *status ){
   if ( !astOK ) return NULL;
   return (**astMEMBER(this,Table,ColumnName))(this,index,status);
}
const char *astParameterName_( AstTable *this, int index, int *status ){
   if ( !astOK ) return NULL;
   return (**astMEMBER(this,Table,ParameterName))(this,index,status);
}
int astGetColumnType_( AstTable *this, const char *column, int *status ){
   if ( !astOK ) return 0;
   return (**astMEMBER(this,Table,GetColumnType))(this,column,status);
}
const char *astGetColumnUnit_( AstTable *this, const char *column, int *status ){
   if ( !astOK ) return NULL;
   return (**astMEMBER(this,Table,GetColumnUnit))(this,column,status);
}
int astGetColumnLenC_( AstTable *this, const char *column, int *status ){
   if ( !astOK ) return 0;
   return (**astMEMBER(this,Table,GetColumnLenC))(this,column,status);
}
int astGetColumnLength_( AstTable *this, const char *column, int *status ){
   if ( !astOK ) return 0;
   return (**astMEMBER(this,Table,GetColumnLength))(this,column,status);
}
int astGetColumnNdim_( AstTable *this, const char *column, int *status ){
   if ( !astOK ) return 0;
   return (**astMEMBER(this,Table,GetColumnNdim))(this,column,status);
}
void astColumnShape_( AstTable *this, const char *column, int mxdim,
                      int *ndim, int *dims, int *status ){
   if ( !astOK ) return;
   (**astMEMBER(this,Table,ColumnShape))( this, column, mxdim, ndim,
                                          dims, status );
}
AstKeyMap *astColumnProps_( AstTable *this, int *status ){
   if ( !astOK ) return NULL;
   return (**astMEMBER(this,Table,ColumnProps))(this,status);
}
AstKeyMap *astParameterProps_( AstTable *this, int *status ){
   if ( !astOK ) return NULL;
   return (**astMEMBER(this,Table,ParameterProps))(this,status);
}
int astHasColumn_( AstTable *this, const char *column, int *status ){
   if ( !astOK ) return 0;
   return (**astMEMBER(this,Table,HasColumn))(this,column,status);
}
int astHasParameter_( AstTable *this, const char *parameter, int *status ){
   if ( !astOK ) return 0;
   return (**astMEMBER(this,Table,HasParameter))(this,parameter,status);
}