summaryrefslogtreecommitdiffstats
path: root/src/H5Q.c
blob: 8f22ee33e1746a3c494778ec8bb6006070f968cc (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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the files COPYING and Copyright.html.  COPYING can be found at the root   *
 * of the source code distribution tree; Copyright.html can be found at the  *
 * root level of an installed copy of the electronic HDF5 document set and   *
 * is linked from the top-level documents page.  It can also be found at     *
 * http://hdfgroup.org/HDF5/doc/Copyright.html.  If you do not have          *
 * access to either file, you may request a copy from help@hdfgroup.org.     *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * Purpose:	Query routines.
 */

/****************/
/* Module Setup */
/****************/

#include "H5Qmodule.h"      /* This source code file is part of the H5Q module */

/***********/
/* Headers */
/***********/
#include "H5private.h"      /* Generic Functions */
#include "H5Qprivate.h"     /* Query */
#include "H5Eprivate.h"     /* Error handling */
#include "H5Iprivate.h"     /* IDs */
#include "H5MMprivate.h"    /* Memory management */
#include "H5Pprivate.h"
#include "H5FLprivate.h"    /* Free lists */
#include "H5Dprivate.h"     /* Datasets */
#include "H5Rprivate.h"     /* References */
#include "H5Aprivate.h"     /* Attributes */
#include "H5FDcore.h"       /* Core driver */


/****************/
/* Local Macros */
/****************/
#define H5Q_APPLY_MATCH_OP(result, op, x, y) \
    switch (op) { \
        case H5Q_MATCH_EQUAL: \
            H5_GCC_DIAG_OFF(float-equal) \
            result = (x == y); \
            H5_GCC_DIAG_ON(float-equal) \
            break; \
        case H5Q_MATCH_NOT_EQUAL: \
            H5_GCC_DIAG_OFF(float-equal) \
            result = (x != y); \
            H5_GCC_DIAG_ON(float-equal) \
            break; \
        case H5Q_MATCH_LESS_THAN: \
            result = (x < y); \
            break; \
        case H5Q_MATCH_GREATER_THAN: \
            result = (x > y); \
            break; \
        default: \
            break; \
    }

#define H5Q_CMP_DATA_ELEM(result, op, type, x, y) \
{ \
    type x_value; \
    type y_value; \
    x_value = *((type *) x); \
    y_value = *((type *) y); \
    \
    H5Q_APPLY_MATCH_OP(result, op, x_value, y_value) \
}

#define H5Q_CMP_TYPE(type1, type2, native_type) \
    (0 == H5T_cmp(type1, native_type, FALSE)) || \
    (0 == H5T_cmp(type2, native_type, FALSE))

/*
 * Singly-linked Tail queue declarations. (from sys/queue.h)
 */
#define H5Q_QUEUE_HEAD(name, type)                                          \
struct name {                                                               \
    struct type *stqh_first;    /* first element */                         \
    struct type **stqh_last;    /* addr of last next element */             \
    size_t n_elem;              /* number of elements */                    \
}

#define H5Q_QUEUE_HEAD_INITIALIZER(head)                                    \
    { NULL, &(head).stqh_first, 0 }

#define H5Q_QUEUE_ENTRY(type)                                               \
struct {                                                                    \
    struct type *stqe_next; /* next element */                              \
}

/*
 * Singly-linked Tail queue functions.
 */
#define H5Q_QUEUE_INIT(head) do {                                           \
    (head)->stqh_first = NULL;                                              \
    (head)->stqh_last = &(head)->stqh_first;                                \
    (head)->n_elem = 0;                                                     \
} while (/*CONSTCOND*/0)

#define H5Q_QUEUE_INSERT_HEAD(head, elm, field) do {                        \
    if (((elm)->field.stqe_next = (head)->stqh_first) == NULL)              \
        (head)->stqh_last = &(elm)->field.stqe_next;                        \
    (head)->stqh_first = (elm);                                             \
    (head)->n_elem++;                                                       \
} while (/*CONSTCOND*/0)

#define H5Q_QUEUE_INSERT_TAIL(head, elm, field) do {                        \
    (elm)->field.stqe_next = NULL;                                          \
    *(head)->stqh_last = (elm);                                             \
    (head)->stqh_last = &(elm)->field.stqe_next;                            \
    (head)->n_elem++;                                                       \
} while (/*CONSTCOND*/0)

#define H5Q_QUEUE_REMOVE_HEAD(head, field) do {                             \
    if (((head)->stqh_first = (head)->stqh_first->field.stqe_next) == NULL) { \
        (head)->stqh_last = &(head)->stqh_first;                            \
        (head)->n_elem--;                                                   \
    }                                                                       \
} while (/*CONSTCOND*/0)

#define H5Q_QUEUE_REMOVE(head, elm, type, field) do {                       \
    if ((head)->stqh_first == (elm)) {                                      \
        H5Q_QUEUE_REMOVE_HEAD((head), field);                               \
    } else {                                                                \
        struct type *curelm = (head)->stqh_first;                           \
        while (curelm->field.stqe_next != (elm))                            \
            curelm = curelm->field.stqe_next;                               \
        if ((curelm->field.stqe_next =                                      \
            curelm->field.stqe_next->field.stqe_next) == NULL)              \
                (head)->stqh_last = &(curelm)->field.stqe_next;             \
        (head)->n_elem--;                                                   \
    }                                                                       \
} while (/*CONSTCOND*/0)

#define H5Q_QUEUE_FOREACH(var, head, field)                                 \
    for ((var) = ((head)->stqh_first);                                      \
        (var);                                                              \
        (var) = ((var)->field.stqe_next))

#define H5Q_QUEUE_CONCAT(head1, head2) do {                                 \
    if (!H5Q_QUEUE_EMPTY((head2))) {                                        \
        *(head1)->stqh_last = (head2)->stqh_first;                          \
        (head1)->stqh_last = (head2)->stqh_last;                            \
        (head1)->n_elem += (head2)->n_elem;                                 \
        H5Q_QUEUE_INIT((head2));                                            \
    }                                                                       \
} while (/*CONSTCOND*/0)

/*
 * Singly-linked Tail queue access methods.
 */
#define H5Q_QUEUE_EMPTY(head)       ((head)->stqh_first == NULL)
#define H5Q_QUEUE_FIRST(head)       ((head)->stqh_first)
#define H5Q_QUEUE_NEXT(elm, field)  ((elm)->field.stqe_next)

#define H5Q_VIEW_INITIALIZER(view) \
    {H5Q_QUEUE_HEAD_INITIALIZER(view.reg_refs), H5Q_QUEUE_HEAD_INITIALIZER(view.obj_refs), H5Q_QUEUE_HEAD_INITIALIZER(view.attr_refs)}

#define H5Q_VIEW_REF_NTYPES      3          /* number of reference types */
#define H5Q_VIEW_CORE_INCREMENT  1024       /* increment for core VFD */

//#define H5Q_DEBUG

#ifdef H5Q_DEBUG
#define H5Q_LOG_DEBUG(...) do {                                 \
      fprintf(stdout, " # %s(): ", __func__);                   \
      fprintf(stdout, __VA_ARGS__);                             \
      fprintf(stdout, "\n");                                    \
      fflush(stdout);                                           \
  } while (0)
#else
#define H5Q_LOG_DEBUG(...) do { \
  } while (0)
#endif

/******************/
/* Local Typedefs */
/******************/
typedef enum H5Q_match_type_t { /* The different kinds of native types we can match */
    H5Q_NATIVE_INT_MATCH_CHAR,
    H5Q_NATIVE_INT_MATCH_SHORT,
    H5Q_NATIVE_INT_MATCH_INT,
    H5Q_NATIVE_INT_MATCH_LONG,
    H5Q_NATIVE_INT_MATCH_LLONG,
    H5Q_NATIVE_FLOAT_MATCH_FLOAT,
    H5Q_NATIVE_FLOAT_MATCH_DOUBLE,
    H5Q_INVALID_MATCH_TYPE
} H5Q_match_type_t;

typedef struct H5Q_ref_entry_t H5Q_ref_entry_t;

struct H5Q_ref_entry_t {
    H5R_type_t ref_type;
    union {
        hreg_ref_t reg;
        hobj_ref_t obj;
        hattr_ref_t attr;
    } ref;
    H5Q_QUEUE_ENTRY(H5Q_ref_entry_t) entry;
};

typedef H5Q_QUEUE_HEAD(H5Q_ref_head_t, H5Q_ref_entry_t) H5Q_ref_head_t;

typedef struct {
    H5Q_ref_head_t reg_refs;
    H5Q_ref_head_t obj_refs;
    H5Q_ref_head_t attr_refs;
} H5Q_view_t;

typedef struct {
    const H5Q_t *query;
    unsigned *result;
    H5Q_view_t *view;
} H5Q_apply_arg_t;

typedef struct {
    H5G_loc_t *loc;
    const char *loc_name;
    H5G_loc_t *obj_loc;
    H5Q_apply_arg_t *apply_args;
} H5Q_apply_attr_arg_t;

typedef struct {
    const H5Q_t *query;
    hbool_t *result;
} H5Q_apply_attr_elem_arg_t;

/********************/
/* Local Prototypes */
/********************/
static herr_t H5Q__apply_atom(const H5Q_t *query, hbool_t *result, va_list ap);
static herr_t H5Q__apply_data_elem(const H5Q_t *query, hbool_t *result,
    const H5T_t *type, const void *elem);
static H5T_t *H5Q__promote_type(const H5T_t *type1, const H5T_t *type2,
    H5Q_match_type_t *match_type);
static herr_t H5Q__apply_attr_name(const H5Q_t *query, hbool_t *result,
    const char *name);
static herr_t H5Q__apply_link_name(const H5Q_t *query, hbool_t *result,
    const char *name);

static herr_t H5Q__apply_iterate(hid_t oid, const char *name,
    const H5O_info_t *oinfo, void *udata);
static herr_t H5Q__apply_object(hid_t oid, const char *name,
    const H5O_info_t *oinfo, void *udata);
static herr_t H5Q__apply_object_link(H5G_loc_t *loc, const char *name,
    const H5O_info_t *oinfo, void *udata);
static herr_t H5Q__apply_object_data(H5G_loc_t *loc, const char *name,
    const H5O_info_t *oinfo, void *udata);
static herr_t H5Q__apply_object_attr(H5G_loc_t *loc, const char *name,
    const H5O_info_t *oinfo, void *udata);
static herr_t H5Q__apply_object_attr_iterate(H5A_t *attr, void *udata);
static herr_t H5Q__apply_object_attr_name(H5A_t *attr, void *udata);
static herr_t H5Q__apply_object_attr_value(H5A_t *attr, void *udata);
static herr_t H5Q__apply_object_attr_value_iterate(void *elem, const H5T_t *type,
    unsigned H5_ATTR_UNUSED ndim, const hsize_t H5_ATTR_UNUSED *point, void *udata);

static herr_t H5Q__view_append(H5Q_view_t *view, H5R_type_t ref_type, void *ref);
static herr_t H5Q__view_combine(H5Q_combine_op_t combine_op, H5Q_view_t *view1, H5Q_view_t *view2,
    unsigned result1, unsigned result2, H5Q_view_t *view, unsigned *result);
static herr_t H5Q__view_write(H5G_t *grp, H5Q_view_t *view);
static herr_t H5Q__view_free(H5Q_view_t *view);


static H5_INLINE void
H5Q__encode_memcpy(unsigned char **buf_ptr, size_t *nalloc, const void *data,
        size_t data_size)
{
    if (*buf_ptr != NULL) {
        HDmemcpy(*buf_ptr, data, data_size);
        *buf_ptr += data_size;
    }
    *nalloc += data_size;
}

static H5_INLINE void
H5Q__decode_memcpy(void *data, size_t data_size, const unsigned char **buf_ptr)
{
    if (*buf_ptr != NULL) {
        HDmemcpy(data, *buf_ptr, data_size);
        *buf_ptr += data_size;
    }
}

/*********************/
/* Package Variables */
/*********************/

/* Package initialization variable */
hbool_t H5_PKG_INIT_VAR = FALSE;

/*****************************/
/* Library Private Variables */
/*****************************/

/*******************/
/* Local Variables */
/*******************/

/* Declare a free list to manage the H5Q_t struct */
H5FL_DEFINE(H5Q_t);

/* Query ID class */
static const H5I_class_t H5I_QUERY_CLS[1] = {{
    H5I_QUERY,             /* Class ID for the type */
    0,                     /* Class behavior flags */
    0,                     /* Number of reserved IDs for this type */
    (H5I_free_t)H5Q_close  /* Free function for object's of this type */
}};

/* Flag indicating "top" of interface has been initialized */
static hbool_t H5Q_top_package_initialize_s = FALSE;

/*-------------------------------------------------------------------------
 * Function:    H5Q_init
 *
 * Purpose:	Initialize the interface from some other package.
 *
 * Return:  Success:    non-negative
 *          Failure:    negative
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5Q_init(void)
{
    herr_t ret_value = SUCCEED;   /* Return value */

    FUNC_ENTER_NOAPI(FAIL)
    /* FUNC_ENTER() does all the work */

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q_init() */

/*--------------------------------------------------------------------------
NAME
   H5Q__init_package -- Initialize interface-specific information
USAGE
    herr_t H5Q__init_package()

RETURNS
    Non-negative on success/Negative on failure
DESCRIPTION
    Initializes any interface-specific data or routines.

--------------------------------------------------------------------------*/
herr_t
H5Q__init_package(void)
{
    herr_t ret_value = SUCCEED;   /* Return value */

    FUNC_ENTER_PACKAGE

    /* Initialize the atom group for the QUERY IDs */
    if (FAIL == H5I_register_type(H5I_QUERY_CLS))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTINIT, FAIL, "unable to initialize interface");

    /* Mark "top" of interface as initialized, too */
    H5Q_top_package_initialize_s = TRUE;

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q__init_package() */

/*--------------------------------------------------------------------------
 NAME
    H5Q_top_term_package
 PURPOSE
    Terminate various H5Q objects
 USAGE
    void H5Q_top_term_package()
 RETURNS
 DESCRIPTION
    Release IDs for the atom group, deferring full interface shutdown
    until later (in H5Q_term_package).
 GLOBAL VARIABLES
 COMMENTS, BUGS, ASSUMPTIONS
     Can't report errors...
 EXAMPLES
 REVISION LOG
--------------------------------------------------------------------------*/
int
H5Q_top_term_package(void)
{
    int n = 0;

    FUNC_ENTER_NOAPI_NOINIT_NOERR

    if (H5Q_top_package_initialize_s) {
        if (H5I_nmembers(H5I_QUERY) > 0) {
            (void)H5I_clear_type(H5I_QUERY, FALSE, FALSE);
            n++; /*H5I*/
        } /* end if */

        /* Mark closed */
        if (0 == n)
            H5Q_top_package_initialize_s = FALSE;
    } /* end if */

    FUNC_LEAVE_NOAPI(n)
} /* H5Q_top_term_package() */

/*--------------------------------------------------------------------------
 NAME
    H5Q_term_package
 PURPOSE
    Terminate various H5Q objects
 USAGE
    void H5Q_term_package()
 RETURNS
    Non-negative on success/Negative on failure
 DESCRIPTION
    Release the atom group and any other resources allocated.
 GLOBAL VARIABLES
 COMMENTS, BUGS, ASSUMPTIONS
     Can't report errors...
 EXAMPLES
 REVISION LOG
--------------------------------------------------------------------------*/
int
H5Q_term_package(void)
{
    int n = 0;

    FUNC_ENTER_NOAPI_NOINIT_NOERR

    if (H5_PKG_INIT_VAR) {
        /* Sanity checks */
        HDassert(0 == H5I_nmembers(H5I_QUERY));

        /* Destroy the query object id group */
        n += (H5I_dec_type_ref(H5I_QUERY) > 0);

        /* Mark closed */
        if (0 == n)
            H5_PKG_INIT_VAR = FALSE;
    } /* end if */

    FUNC_LEAVE_NOAPI(n)
} /* end H5Q_term_package() */

/*-------------------------------------------------------------------------
 * Function:    H5Qcreate
 *
 * Purpose: Create a new query object of query_type type, with match_op
 * determining the query's match condition and additional parameters
 * determined by the type of the query.
 *
 * Return:  Success:    The ID for a new query.
 *          Failure:    FAIL
 *
 *-------------------------------------------------------------------------
 */
hid_t
H5Qcreate(H5Q_type_t query_type, H5Q_match_op_t match_op, ...)
{
    H5Q_t *query = NULL;
    va_list ap;
    hid_t ret_value;

    FUNC_ENTER_API(FAIL)
    H5TRACE2("i", "QtQm", query_type, match_op);

    va_start(ap, match_op);

    switch (query_type) {
        case H5Q_TYPE_DATA_ELEM:
        case H5Q_TYPE_ATTR_VALUE:
        {
            H5T_t *datatype = NULL;
            hid_t datatype_id;
            const void *value;

            /* Get arguments */
            datatype_id = va_arg(ap, hid_t);
            value = va_arg(ap, const void *);

            /* Get type */
            if (NULL == (datatype = (H5T_t *) H5I_object_verify(datatype_id, H5I_DATATYPE)))
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a data type");

            /* Create a new query object */
            if (NULL == (query = H5Q_create(query_type, match_op, datatype, value)))
                HGOTO_ERROR(H5E_QUERY, H5E_CANTCREATE, FAIL, "unable to create query");
        }
        break;
        case H5Q_TYPE_ATTR_NAME:
        {
            const char *attr_name;

            /* Get arguments */
            attr_name = va_arg(ap, const char *);

            /* Create a new query object */
            if (NULL == (query = H5Q_create(query_type, match_op, attr_name)))
                HGOTO_ERROR(H5E_QUERY, H5E_CANTCREATE, FAIL, "unable to create query");
        }
        break;
        case H5Q_TYPE_LINK_NAME:
        {
            const char *link_name;

            /* Get arguments */
            link_name = va_arg(ap, const char *);

            /* Create a new query object */
            if (NULL == (query = H5Q_create(query_type, match_op, link_name)))
                HGOTO_ERROR(H5E_QUERY, H5E_CANTCREATE, FAIL, "unable to create query");
        }
        break;
        case H5Q_TYPE_MISC:
        default:
        {
            HGOTO_ERROR(H5E_QUERY, H5E_BADTYPE, FAIL, "unsupported/unrecognized query type");
        }
        break;
    }

    /* Register the new query object to get an ID for it */
    if (FAIL == (query->query_id = H5I_register(H5I_QUERY, query, TRUE)))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTREGISTER, FAIL, "can't register query handle");
    ret_value = query->query_id;

done:
    va_end(ap);

    FUNC_LEAVE_API(ret_value)
} /* end H5Qcreate() */

/*-------------------------------------------------------------------------
 * Function:    H5Q_create
 *
 * Purpose: Private function for H5Qcreate
 *
 * Return:  Success:    Pointer to the new query
 *          Failure:    NULL
 *
 *-------------------------------------------------------------------------
 */
H5Q_t *
H5Q_create(H5Q_type_t query_type, H5Q_match_op_t match_op, ...)
{
    H5Q_t *query = NULL;
    va_list ap;
    H5Q_t *ret_value = NULL; /* Return value */

    FUNC_ENTER_NOAPI_NOINIT

    va_start(ap, match_op);

    /* Allocate query struct */
    if (NULL == (query = H5FL_CALLOC(H5Q_t)))
        HGOTO_ERROR(H5E_QUERY, H5E_NOSPACE, NULL, "can't allocate query structure");

    query->is_combined = FALSE;
    query->query_id = H5I_UNINIT;
    query->ref_count = 1;
    query->query.select.type = query_type;
    query->query.select.match_op = match_op;
    switch (query_type) {
        case H5Q_TYPE_DATA_ELEM:
        case H5Q_TYPE_ATTR_VALUE:
            {
                H5T_t *datatype = NULL;
                H5T_t *native_datatype = NULL;
                size_t datatype_size;
                const void *value;
                void *value_buf = NULL;

                datatype = va_arg(ap, H5T_t*);
                value = va_arg(ap, const void *);

                /* Only use native type */
                if (NULL == (native_datatype = H5T_get_native_type(datatype, H5T_DIR_DEFAULT, NULL, NULL, NULL)))
                    HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "cannot retrieve native type");
                query->query.select.elem.data_elem.type = native_datatype;
                if (0 == (datatype_size = H5T_get_size(native_datatype)))
                    HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a valid size");
                query->query.select.elem.data_elem.type_size = datatype_size;
                if (NULL == (value_buf = H5MM_malloc(datatype_size)))
                    HGOTO_ERROR(H5E_QUERY, H5E_CANTALLOC, NULL,
                            "can't allocate value buffer");
                HDmemcpy(value_buf, value, datatype_size);
                query->query.select.elem.data_elem.value = value_buf;
            }
            break;
        case H5Q_TYPE_ATTR_NAME:
            {
                const char *attr_name;

                attr_name = va_arg(ap, const char *);
                query->query.select.elem.attr_name.name = H5MM_strdup(attr_name);
            }
            break;
        case H5Q_TYPE_LINK_NAME:
            {
                const char *link_name;

                link_name = va_arg(ap, const char *);
                query->query.select.elem.link_name.name = H5MM_strdup(link_name);
            }
            break;
        case H5Q_TYPE_MISC:
        default:
            HGOTO_ERROR(H5E_QUERY, H5E_BADTYPE, NULL, "unsupported/unrecognized query type");
            break;
    } /* end switch */

    /* set return value */
    ret_value = query;

done:
    if (!ret_value && query)
        query = H5FL_FREE(H5Q_t, query);

    va_end(ap);

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q_create() */

/*-------------------------------------------------------------------------
 * Function:    H5Qclose
 *
 * Purpose: The H5Qclose routine terminates access to a query object,
 * given by query_id.
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5Qclose(hid_t query_id)
{
    herr_t ret_value = SUCCEED;         /* Return value */

    FUNC_ENTER_API(FAIL)
    H5TRACE1("e", "i", query_id);

    /* Check args */
    if (NULL == H5I_object_verify(query_id, H5I_QUERY))
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a query ID");

    if (FAIL == H5I_dec_app_ref(query_id))
    	HGOTO_ERROR(H5E_QUERY, H5E_CANTDEC, FAIL, "unable to decrement ref count on query");

done:
    FUNC_LEAVE_API(ret_value)
} /* end H5Qclose() */

/*-------------------------------------------------------------------------
 * Function:    H5Q_close
 *
 * Purpose: Private function for H5Qclose.
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5Q_close(H5Q_t *query)
{
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_NOAPI(FAIL)

    HDassert(query);

    if (--query->ref_count)
        HGOTO_DONE(SUCCEED)

    if (query->is_combined) {
        if (FAIL == H5Q_close(query->query.combine.l_query))
            HGOTO_ERROR(H5E_QUERY, H5E_CANTFREE, FAIL, "unable to free query");
            query->query.combine.l_query = NULL;
        if (FAIL == H5Q_close(query->query.combine.r_query))
            HGOTO_ERROR(H5E_QUERY, H5E_CANTFREE, FAIL, "unable to free query");
            query->query.combine.r_query = NULL;
    } else {
        switch (query->query.select.type) {
            case H5Q_TYPE_DATA_ELEM:
            case H5Q_TYPE_ATTR_VALUE:
                if (FAIL == H5T_close(query->query.select.elem.data_elem.type))
                    HGOTO_ERROR(H5E_QUERY, H5E_CANTFREE, FAIL, "unable to free datatype");
                query->query.select.elem.data_elem.type = NULL;
                H5MM_free(query->query.select.elem.data_elem.value);
                query->query.select.elem.data_elem.value = NULL;
                break;
            case H5Q_TYPE_ATTR_NAME:
                H5MM_free(query->query.select.elem.attr_name.name);
                query->query.select.elem.attr_name.name = NULL;
                break;
            case H5Q_TYPE_LINK_NAME:
                H5MM_free(query->query.select.elem.link_name.name);
                query->query.select.elem.link_name.name = NULL;
                break;
            case H5Q_TYPE_MISC:
            default:
                HGOTO_ERROR(H5E_QUERY, H5E_BADTYPE, FAIL, "unsupported/unrecognized query type");
                break;
        }
    }

    /* Free the query struct */
    query = H5FL_FREE(H5Q_t, query);

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q_close() */

/*-------------------------------------------------------------------------
 * Function:    H5Qcombine
 *
 * Purpose: The H5Qcombine routine creates a new compound query object by
 * combining two query objects (given by query1 and query2), using the
 * combination operator combine_op. Valid combination operators are:
 *      - H5Q_COMBINE_AND
 *      - H5Q_COMBINE_OR
 *
 * Return:  Success:    The ID for a new combined query.
 *          Failure:    FAIL
 *
 *-------------------------------------------------------------------------
 */
hid_t
H5Qcombine(hid_t query1_id, H5Q_combine_op_t combine_op, hid_t query2_id)
{
    H5Q_t *query = NULL, *query1 = NULL, *query2 = NULL;
    hid_t ret_value;

    FUNC_ENTER_API(FAIL)
    H5TRACE3("i", "iQci", query1_id, combine_op, query2_id);

    /* Check args and get the query objects */
    if (NULL == (query1 = (H5Q_t *) H5I_object_verify(query1_id, H5I_QUERY)))
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a query ID");
    if (NULL == (query2 = (H5Q_t *) H5I_object_verify(query2_id, H5I_QUERY)))
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a query ID");

    /* Combine query objects */
    if (NULL == (query = H5Q_combine(query1, combine_op, query2)))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTCREATE, FAIL, "unable to combine query objects");

    /* Register the new query object to get an ID for it */
    if (FAIL == (query->query_id = H5I_register(H5I_QUERY, query, TRUE)))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTREGISTER, FAIL, "can't register query handle");
    ret_value = query->query_id;

done:
    FUNC_LEAVE_API(ret_value)
} /* end H5Qcombine() */

/*-------------------------------------------------------------------------
 * Function:    H5Q_combine
 *
 * Purpose: Private function for H5Qcombine.
 *
 * Return:  Success:    Pointer to the new query
 *          Failure:    NULL
 *
 *-------------------------------------------------------------------------
 */
H5Q_t *
H5Q_combine(H5Q_t *query1, H5Q_combine_op_t combine_op, H5Q_t *query2)
{
    H5Q_t *query = NULL;
    H5Q_type_t type1, type2;
    H5Q_t *ret_value = NULL; /* Return value */

    FUNC_ENTER_NOAPI_NOINIT

    /* Allocate query struct */
    if (NULL == (query = H5FL_CALLOC(H5Q_t)))
        HGOTO_ERROR(H5E_QUERY, H5E_NOSPACE, NULL, "can't allocate query structure");

    switch (combine_op) {
        case H5Q_COMBINE_AND:
        case H5Q_COMBINE_OR:
            query->query.combine.op = combine_op;
            break;
        case H5Q_SINGLETON:
        default:
            HGOTO_ERROR(H5E_QUERY, H5E_BADTYPE, NULL, "unsupported/unrecognized combine op");
            break;
    }
    query->is_combined = TRUE;
    query->query_id = H5I_UNINIT;
    query->ref_count = 1;
    query->query.combine.l_query = query1;
    query1->ref_count++;
    query->query.combine.r_query = query2;
    query2->ref_count++;
    /* Work out query type of the combined query */
    type1 = (query1->is_combined) ? query1->query.combine.type : query1->query.select.type;
    type2 = (query2->is_combined) ? query2->query.combine.type : query2->query.select.type;
    query->query.combine.type = (type1 == type2) ? type1 : H5Q_TYPE_MISC;

    /* set return value */
    ret_value = query;

done:
    if (!ret_value && query)
        query = H5FL_FREE(H5Q_t, query);

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q_combine() */

/*-------------------------------------------------------------------------
 * Function:    H5Qget_type
 *
 * Purpose: The H5Qget_type routine queries a query object,
 * given by query_id, for its type information, originally provided to
 * H5Qcreate or created after a call to H5Qcombine. Information is returned
 * through the query_type parameter. See H5Qcreate for a table listing the
 * complete set of values that may be returned for query_type.
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5Qget_type(hid_t query_id, H5Q_type_t *query_type)
{
    H5Q_t *query = NULL;
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_API(FAIL)
    H5TRACE2("e", "i*Qt", query_id, query_type);

    /* Check args and get the query objects */
    if (NULL == (query = (H5Q_t *) H5I_object_verify(query_id, H5I_QUERY)))
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a query ID");
    if (!query_type)
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "NULL pointer to query type");

    /* Get match info */
    if (FAIL == H5Q_get_type(query, query_type))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTGET, FAIL, "unable to get query type");

done:
    FUNC_LEAVE_API(ret_value)
} /* end H5Qget_type() */

/*-------------------------------------------------------------------------
 * Function:    H5Q_get_type
 *
 * Purpose: Private function for H5Qget_type.
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5Q_get_type(const H5Q_t *query, H5Q_type_t *query_type)
{
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_NOAPI_NOINIT_NOERR

    HDassert(query);
    HDassert(query_type);

    *query_type = (query->is_combined) ? query->query.combine.type :
            query->query.select.type;

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q_get_type() */


/*-------------------------------------------------------------------------
 * Function:    H5Qget_match_op
 *
 * Purpose: The H5Qget_match_op routine queries a singleton query object,
 * given by query_id, for its op information, originally provided to
 * H5Qcreate. Match information is returned through the
 * match_op parameter. See H5Qcreate for a table listing the complete set of
 * values that may be returned for match_op.
 * It is an error to perform this call on a compound query object (one which
 * was created with H5Qcombine).
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5Qget_match_op(hid_t query_id, H5Q_match_op_t *match_op)
{
    H5Q_t *query = NULL;
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_API(FAIL)
    H5TRACE2("e", "i*Qm", query_id, match_op);

    /* Check args and get the query objects */
    if (NULL == (query = (H5Q_t *) H5I_object_verify(query_id, H5I_QUERY)))
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a query ID");
    if (!match_op)
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "NULL pointer to match op");

    /* Get match info */
    if (FAIL == H5Q_get_match_op(query, match_op))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTGET, FAIL, "unable to get match op");

done:
    FUNC_LEAVE_API(ret_value)
} /* end H5Qget_match_op() */

/*-------------------------------------------------------------------------
 * Function:    H5Q_get_match_op
 *
 * Purpose: Private function for H5Qget_match_op.
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5Q_get_match_op(const H5Q_t *query, H5Q_match_op_t *match_op)
{
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_NOAPI_NOINIT

    HDassert(query);
    HDassert(match_op);

    if (query->is_combined)
        HGOTO_ERROR(H5E_QUERY, H5E_CANTGET, FAIL, "cannot retrieve op from combined query");

    *match_op = query->query.select.match_op;

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q_get_match_op() */

/*-------------------------------------------------------------------------
 * Function:    H5Qget_components
 *
 * Purpose: The H5Qget_components routine queries a compound query object,
 * given by query_id, for its component queries.  The component queries are
 * returned in sub_query1_id and sub_query2_id, both of which must be closed
 * with H5Qclose.
 * It is an error to apply H5Qget_components to a singleton query object (one
 * which was created with H5Qcreate).
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5Qget_components(hid_t query_id, hid_t *sub_query1_id, hid_t *sub_query2_id)
{
    H5Q_t *query = NULL, *sub_query1 = NULL, *sub_query2 = NULL;
    hid_t _sub_query1_id, _sub_query2_id;
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_API(FAIL)
    H5TRACE3("e", "i*i*i", query_id, sub_query1_id, sub_query2_id);

    /* Check args and get the query objects */
    if (NULL == (query = (H5Q_t *) H5I_object_verify(query_id, H5I_QUERY)))
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a query ID");
    if (!sub_query1_id || !sub_query2_id)
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "NULL pointer to query_id");

    /* Get components */
    if (FAIL == H5Q_get_components(query, &sub_query1, &sub_query2))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTGET, FAIL, "unable to get components");

    /* Register the type and return the ID */
    if (FAIL == (_sub_query1_id = H5I_register(H5I_QUERY, sub_query1, TRUE)))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTREGISTER, FAIL, "unable to register query");
    if (FAIL == (_sub_query2_id = H5I_register(H5I_QUERY, sub_query2, TRUE)))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTREGISTER, FAIL, "unable to register query");

    *sub_query1_id = _sub_query1_id;
    *sub_query2_id = _sub_query2_id;

done:
    FUNC_LEAVE_API(ret_value)
} /* end H5Qget_components() */

/*-------------------------------------------------------------------------
 * Function:    H5Q_get_components
 *
 * Purpose: Private function for H5Qget_components.
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5Q_get_components(const H5Q_t *query, H5Q_t **sub_query1, H5Q_t **sub_query2)
{
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_NOAPI_NOINIT

    HDassert(query);
    HDassert(sub_query1);
    HDassert(sub_query2);

    if (!query->is_combined)
        HGOTO_ERROR(H5E_QUERY, H5E_CANTGET, FAIL, "not a combined query");

    *sub_query1 = query->query.combine.l_query;
    query->query.combine.l_query->ref_count++;
    *sub_query2 = query->query.combine.r_query;
    query->query.combine.r_query->ref_count++;

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q_get_components() */

/*-------------------------------------------------------------------------
 * Function:    H5Qget_combine_op
 *
 * Purpose: The H5Qget_combine_op routine queries a query object, given by
 * query_id, for its operator type. The possible operator types returned are:
 *      - H5Q_SINGLETON
 *      - H5Q_COMBINE_AND
 *      - H5Q_COMBINE_OR
 * H5Q_COMBINE_AND and H5Q_COMBINE_OR are only returned for query objects
 * produced with H5Qcombine and H5Q_SINGLETON is returned for query objects
 * produced with H5Qcreate.
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5Qget_combine_op(hid_t query_id, H5Q_combine_op_t *op_type)
{
    H5Q_t *query = NULL;
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_API(FAIL)
    H5TRACE2("e", "i*Qc", query_id, op_type);

    /* Check args and get the query objects */
    if (NULL == (query = (H5Q_t *) H5I_object_verify(query_id, H5I_QUERY)))
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a query ID");
    if (!op_type)
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "NULL pointer to op type");

    /* Get combine op */
    if (FAIL == H5Q_get_combine_op(query, op_type))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTGET, FAIL, "unable to get combine op");

done:
    FUNC_LEAVE_API(ret_value)
} /* end H5Qget_combine_op() */

/*-------------------------------------------------------------------------
 * Function:    H5Q_get_combine_op
 *
 * Purpose: Private function for H5Qget_combine_op.
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5Q_get_combine_op(const H5Q_t *query, H5Q_combine_op_t *op_type)
{
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_NOAPI_NOINIT_NOERR

    HDassert(query);
    HDassert(op_type);

    if (!query->is_combined)
        *op_type = H5Q_SINGLETON;
    else
        *op_type = query->query.combine.op;

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q_get_combine_op() */

/*-------------------------------------------------------------------------
 * Function:    H5Qencode
 *
 * Purpose: Given a query ID, serialize the query into buf.
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5Qencode(hid_t query_id, void *buf, size_t *nalloc)
{
    H5Q_t *query = NULL;
    herr_t ret_value;

    FUNC_ENTER_API(FAIL)
    H5TRACE3("e", "i*x*z", query_id, buf, nalloc);

    /* Check argument and retrieve object */
    if (NULL == (query = (H5Q_t *)H5I_object_verify(query_id, H5I_QUERY)))
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a query ID");
    if (nalloc == NULL)
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "NULL pointer for buffer size");

    /* Encode the query */
    if (FAIL == (ret_value = H5Q_encode(query, (unsigned char *)buf, nalloc)))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTENCODE, FAIL, "can't encode query");

done:
    FUNC_LEAVE_API(ret_value)
} /* end H5Qencode() */

/*-------------------------------------------------------------------------
 * Function:    H5Q_encode
 *
 * Purpose: Private function for H5Qencode.
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5Q_encode(const H5Q_t *query, unsigned char *buf, size_t *nalloc)
{
    size_t buf_size = 0;
    herr_t ret_value = SUCCEED;
    unsigned char *buf_ptr = buf;

    FUNC_ENTER_NOAPI_NOINIT

    HDassert(query);
    HDassert(nalloc);

    H5Q__encode_memcpy(&buf_ptr, &buf_size, &query->is_combined, sizeof(hbool_t));
    if (query->is_combined) {
        size_t l_buf_size = 0, r_buf_size = 0;

        H5Q__encode_memcpy(&buf_ptr, &buf_size, &query->query.combine.type, sizeof(H5Q_type_t));
        H5Q__encode_memcpy(&buf_ptr, &buf_size, &query->query.combine.op, sizeof(H5Q_combine_op_t));
        H5Q_encode(query->query.combine.l_query, buf_ptr, &l_buf_size);
        buf_size += l_buf_size;
        if (buf_ptr) buf_ptr += l_buf_size;
        H5Q_encode(query->query.combine.r_query, buf_ptr, &r_buf_size);
        buf_size += r_buf_size;
        if (buf_ptr) buf_ptr += r_buf_size;
    } else {
        H5Q__encode_memcpy(&buf_ptr, &buf_size, &query->query.select.type, sizeof(H5Q_type_t));
        H5Q__encode_memcpy(&buf_ptr, &buf_size, &query->query.select.match_op, sizeof(H5Q_match_op_t));
        switch (query->query.select.type) {
            case H5Q_TYPE_DATA_ELEM:
            case H5Q_TYPE_ATTR_VALUE:
            {
                size_t type_id_nalloc = 0;
                H5T_t *type = query->query.select.elem.data_elem.type;
                size_t type_size = query->query.select.elem.data_elem.type_size;
                void *value_buf = query->query.select.elem.data_elem.value;

                if (FAIL == H5T_encode(type, NULL, &type_id_nalloc))
                    HGOTO_ERROR(H5E_QUERY, H5E_CANTENCODE, FAIL, "can't get encoding size for datatype");
                H5Q__encode_memcpy(&buf_ptr, &buf_size, &type_id_nalloc, sizeof(size_t));
                if (FAIL == H5T_encode(type, buf_ptr, &type_id_nalloc))
                    HGOTO_ERROR(H5E_QUERY, H5E_CANTENCODE, FAIL, "can't encode datatype");
                buf_size += type_id_nalloc;
                if (buf_ptr) buf_ptr += type_id_nalloc;
                H5Q__encode_memcpy(&buf_ptr, &buf_size, &type_size, sizeof(size_t));
                H5Q__encode_memcpy(&buf_ptr, &buf_size, value_buf, type_size);
            }
                break;
            case H5Q_TYPE_ATTR_NAME:
            {
                size_t name_len = HDstrlen(query->query.select.elem.attr_name.name) + 1;
                char *name = query->query.select.elem.attr_name.name;

                H5Q__encode_memcpy(&buf_ptr, &buf_size, &name_len, sizeof(size_t));
                H5Q__encode_memcpy(&buf_ptr, &buf_size, name, name_len);
            }
                break;
            case H5Q_TYPE_LINK_NAME:
            {
                size_t name_len = HDstrlen(query->query.select.elem.link_name.name) + 1;
                char *name = query->query.select.elem.attr_name.name;

                H5Q__encode_memcpy(&buf_ptr, &buf_size, &name_len, sizeof(size_t));
                H5Q__encode_memcpy(&buf_ptr, &buf_size, name, name_len);
            }
                break;
            case H5Q_TYPE_MISC:
            default:
                HGOTO_ERROR(H5E_QUERY, H5E_BADTYPE, FAIL, "unsupported/unrecognized query type");
                break;
        }
    }

    *nalloc = buf_size;

done:

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q_encode() */

/*-------------------------------------------------------------------------
 * Function:    H5Qdecode
 *
 * Purpose: Deserialize the buffer and return a new query handle. The handle
 * must be closed using H5Qclose.
 *
 * Return:  Success:    query ID (non-negative)
 *
 *          Failure:    negative
 *-------------------------------------------------------------------------
 */
hid_t
H5Qdecode(const void *buf)
{
    H5Q_t *query;
    hid_t ret_value;      /* Return value */
    const unsigned char *buf_ptr = (const unsigned char *) buf;

    FUNC_ENTER_API(FAIL)
    H5TRACE1("i", "*x", buf);

    /* Check args */
    if (buf == NULL) HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "empty buffer");

    /* Create datatype by decoding buffer */
    if (NULL == (query = H5Q_decode(&buf_ptr)))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTDECODE, FAIL, "can't decode object");

    /* Register the type and return the ID */
    if (FAIL == (query->query_id = H5I_register(H5I_QUERY, query, TRUE)))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTREGISTER, FAIL, "unable to register query");
    ret_value = query->query_id;

done:
    FUNC_LEAVE_API(ret_value)
} /* end H5Qdecode() */

/*-------------------------------------------------------------------------
 * Function:    H5Q_decode
 *
 * Purpose: Private function for H5Qdecode.
 *
 * Return:  Success:    Pointer to the decoded query
 *          Failure:    NULL
 *
 *-------------------------------------------------------------------------
 */
H5Q_t *
H5Q_decode(const unsigned char **buf_ptr)
{
    H5Q_t *ret_value = NULL;
    H5Q_t *query = NULL;

    FUNC_ENTER_NOAPI_NOINIT

    HDassert(*buf_ptr);

    /* Allocate query struct */
    if (NULL == (query = H5FL_CALLOC(H5Q_t)))
        HGOTO_ERROR(H5E_QUERY, H5E_NOSPACE, NULL, "can't allocate query structure");

    /* Set ref count */
    query->query_id = H5I_UNINIT;
    query->ref_count = 1;

    H5Q__decode_memcpy(&query->is_combined, sizeof(hbool_t), buf_ptr);
    if (query->is_combined) {
        H5Q__decode_memcpy(&query->query.combine.type, sizeof(H5Q_type_t), buf_ptr);
        H5Q__decode_memcpy(&query->query.combine.op, sizeof(H5Q_combine_op_t), buf_ptr);
        query->query.combine.l_query = H5Q_decode(buf_ptr);
        query->query.combine.r_query = H5Q_decode(buf_ptr);
    } else {
        H5Q__decode_memcpy(&query->query.select.type, sizeof(H5Q_type_t), buf_ptr);
        H5Q__decode_memcpy(&query->query.select.match_op, sizeof(H5Q_match_op_t), buf_ptr);
        switch (query->query.select.type) {
            case H5Q_TYPE_DATA_ELEM:
            case H5Q_TYPE_ATTR_VALUE:
            {
                size_t type_id_nalloc = 0;
                H5T_t *type;
                size_t type_size = 0;
                void *value_buf;

                H5Q__decode_memcpy(&type_id_nalloc, sizeof(size_t), buf_ptr);
                if (NULL == (type = H5T_decode(*buf_ptr)))
                    HGOTO_ERROR(H5E_DATATYPE, H5E_CANTDECODE, NULL, "can't decode datatype");
                query->query.select.elem.data_elem.type = type;
                *buf_ptr += type_id_nalloc;

                H5Q__decode_memcpy(&type_size, sizeof(size_t), buf_ptr);
                if (!type_size)
                    HGOTO_ERROR(H5E_QUERY, H5E_BADVALUE, NULL, "NULL type size");
                query->query.select.elem.data_elem.type_size = type_size;
                if (NULL == (value_buf = H5MM_malloc(type_size)))
                    HGOTO_ERROR(H5E_QUERY, H5E_CANTALLOC, NULL, "can't allocate value buffer");
                H5Q__decode_memcpy(value_buf, type_size, buf_ptr);
                query->query.select.elem.data_elem.value = value_buf;
            }
            break;
            case H5Q_TYPE_ATTR_NAME:
            {
                size_t name_len = 0;
                char *name;
                
                H5Q__decode_memcpy(&name_len, sizeof(size_t), buf_ptr);
                if (!name_len)
                    HGOTO_ERROR(H5E_QUERY, H5E_BADVALUE, NULL, "NULL name len");
                if (NULL == (name = (char *)H5MM_malloc(name_len)))
                    HGOTO_ERROR(H5E_QUERY, H5E_CANTALLOC, NULL,
                            "can't allocate value buffer");
                H5Q__decode_memcpy(name, name_len, buf_ptr);
                query->query.select.elem.attr_name.name = name;
            }
            break;
            case H5Q_TYPE_LINK_NAME:
            {
                size_t name_len = 0;
                char *name;

                H5Q__decode_memcpy(&name_len, sizeof(size_t), buf_ptr);
                if (!name_len)
                    HGOTO_ERROR(H5E_QUERY, H5E_BADVALUE, NULL, "NULL name len");
                if (NULL == (name = (char *)H5MM_malloc(name_len)))
                    HGOTO_ERROR(H5E_QUERY, H5E_CANTALLOC, NULL,
                            "can't allocate value buffer");
                H5Q__decode_memcpy(name, name_len, buf_ptr);
                query->query.select.elem.link_name.name = name;
            }
            break;
            case H5Q_TYPE_MISC:
            default:
                HGOTO_ERROR(H5E_QUERY, H5E_BADTYPE, NULL, "unsupported/unrecognized query type");
                break;
        }
    }

    ret_value = query;
done:

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q_decode() */

/*-------------------------------------------------------------------------
 * Function:    H5Qapply_atom
 *
 * Purpose: Apply a query and return the result. Parameters, which the
 * query applies to, are determined by the type of the query.
 * It is an error to apply H5Qapply_atom to a combined query object of
 * different types.
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5Qapply_atom(hid_t query_id, hbool_t *result, ...)
{
    H5Q_t *query = NULL;
    H5T_t *native_type = NULL;
    herr_t ret_value;
    va_list ap;

    FUNC_ENTER_API(FAIL)
    H5TRACE2("e", "i*b", query_id, result);

    /* Check args and get the query objects */
    if (!result)
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "NULL pointer for result");
    if (NULL == (query = (H5Q_t *) H5I_object_verify(query_id, H5I_QUERY)))
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a query ID");

    if (query->is_combined && (query->query.combine.type == H5Q_TYPE_MISC))
        HGOTO_ERROR(H5E_QUERY, H5E_BADTYPE, FAIL, "cannot apply to misc combined query");

    va_start(ap, result);

    switch (query->query.select.type) {
        case H5Q_TYPE_DATA_ELEM:
        case H5Q_TYPE_ATTR_VALUE:
        {
            H5T_t *type = NULL;
            hid_t type_id;
            const void *value;

            /* Get arguments */
            type_id = va_arg(ap, hid_t);
            value = va_arg(ap, const void *);

            /* Get type */
            if (NULL == (type = (H5T_t *) H5I_object_verify(type_id, H5I_DATATYPE)))
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a data type");

            /* Only use native type */
            if (NULL == (native_type = H5T_get_native_type(type, H5T_DIR_DEFAULT, NULL, NULL, NULL)))
                HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "cannot retrieve native type");

            /* Apply query */
            if (FAIL == (ret_value = H5Q_apply_atom(query, result, native_type, value)))
                HGOTO_ERROR(H5E_QUERY, H5E_CANTCOMPARE, FAIL, "unable to apply query");
        }
        break;
        case H5Q_TYPE_ATTR_NAME:
        {
            const char *attr_name;

            /* Get arguments */
            attr_name = va_arg(ap, const char *);

            /* Apply query */
            if (FAIL == (ret_value = H5Q_apply_atom(query, result, attr_name)))
                HGOTO_ERROR(H5E_QUERY, H5E_CANTCOMPARE, FAIL, "unable to apply query");
        }
        break;
        case H5Q_TYPE_LINK_NAME:
        {
            const char *link_name;

            /* Get arguments */
            link_name = va_arg(ap, const char *);

            /* Apply query */
            if (FAIL == (ret_value = H5Q_apply_atom(query, result, link_name)))
                HGOTO_ERROR(H5E_QUERY, H5E_CANTCOMPARE, FAIL, "unable to apply query");
        }
        break;
        case H5Q_TYPE_MISC:
        default:
        {
            HGOTO_ERROR(H5E_QUERY, H5E_BADTYPE, FAIL, "unsupported/unrecognized query type");
        }
        break;
    }

done:
    if (native_type)
        H5T_close(native_type);

    va_end(ap);

    FUNC_LEAVE_API(ret_value)
} /* end H5Qapply_atom() */

/*-------------------------------------------------------------------------
 * Function:    H5Q_apply_atom
 *
 * Purpose: Private function for H5Qapply_atom.
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5Q_apply_atom(const H5Q_t *query, hbool_t *result, ...)
{
    herr_t ret_value = SUCCEED; /* Return value */
    va_list ap;

    FUNC_ENTER_NOAPI_NOINIT

    HDassert(query);
    HDassert(result);

    va_start(ap, result);

    if (FAIL == H5Q__apply_atom(query, result, ap))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTCOMPARE, FAIL, "unable to apply query")

done:
    va_end(ap);

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q_apply_atom() */

/*-------------------------------------------------------------------------
 * Function:    H5Q__apply_atom
 *
 * Purpose: Private function for H5Q_apply_atom.
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5Q__apply_atom(const H5Q_t *query, hbool_t *result, va_list ap)
{
    herr_t ret_value = SUCCEED; /* Return value */
    va_list aq;

    FUNC_ENTER_NOAPI_NOINIT

    HDassert(query);
    HDassert(result);
    HDassert(query->is_combined == FALSE);

    va_copy(aq, ap);

    if (query->is_combined) {
        hbool_t result1, result2;

        if (FAIL == H5Q__apply_atom(query->query.combine.l_query, &result1, ap))
            HGOTO_ERROR(H5E_QUERY, H5E_CANTCOMPARE, FAIL, "unable to apply query");
        if (FAIL == H5Q__apply_atom(query->query.combine.r_query, &result2, ap))
            HGOTO_ERROR(H5E_QUERY, H5E_CANTCOMPARE, FAIL, "unable to apply query");

        switch (query->query.combine.op) {
            case H5Q_COMBINE_AND:
                *result = result1 && result2;
                break;
            case H5Q_COMBINE_OR:
                *result = result1 || result2;
                break;
            case H5Q_SINGLETON:
            default:
                HGOTO_ERROR(H5E_QUERY, H5E_BADTYPE, FAIL, "unsupported/unrecognized combine op");
                break;
        }
    } else {
        switch (query->query.select.type) {
            case H5Q_TYPE_DATA_ELEM:
            case H5Q_TYPE_ATTR_VALUE:
            {
                H5T_t *type = va_arg(aq, H5T_t*);
                const void *elem = va_arg(aq, const void *);

                HDassert(type);
                HDassert(elem);

                if (FAIL == (ret_value = H5Q__apply_data_elem(query, result, type, elem)))
                    HGOTO_ERROR(H5E_QUERY, H5E_CANTCOMPARE, FAIL, "unable to apply data element query");
                break;
            }
            case H5Q_TYPE_ATTR_NAME:
            {
                const char *attr_name = va_arg(aq, const char *);

                HDassert(attr_name);

                if (FAIL == (ret_value = H5Q__apply_attr_name(query, result, attr_name)))
                    HGOTO_ERROR(H5E_QUERY, H5E_CANTCOMPARE, FAIL, "unable to apply attribute name query");
                break;
            }
            case H5Q_TYPE_LINK_NAME:
            {
                const char *link_name = va_arg(aq, const char *);

                HDassert(link_name);

                if (FAIL == (ret_value = H5Q__apply_link_name(query, result, link_name)))
                    HGOTO_ERROR(H5E_QUERY, H5E_CANTCOMPARE, FAIL, "unable to apply link name query");
                break;
            }
            case H5Q_TYPE_MISC:
            default:
                HGOTO_ERROR(H5E_QUERY, H5E_BADTYPE, FAIL, "unsupported/unrecognized query type");
                break;
        }
    }

done:
    va_end(aq);

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q__apply_atom() */

/*-------------------------------------------------------------------------
 * Function:    H5Q__apply_data_elem
 *
 * Purpose: Private function for H5Q__apply_atom (data element).
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5Q__apply_data_elem(const H5Q_t *query, hbool_t *result, const H5T_t *type, const void *value)
{
    herr_t ret_value = SUCCEED; /* Return value */
    void *value_buf = NULL, *query_value_buf = NULL;
    H5T_t *query_type, *promoted_type;
    H5Q_match_type_t match_type = H5Q_INVALID_MATCH_TYPE;
    size_t type_size, query_type_size, promoted_type_size;
    hid_t type_id = FAIL, query_type_id = FAIL, promoted_type_id = FAIL;
    H5T_path_t *tpath;
    H5Q_match_op_t query_op;
    hbool_t query_result = FALSE;

    FUNC_ENTER_NOAPI_NOINIT

    HDassert(query);
    HDassert((query->query.select.type == H5Q_TYPE_DATA_ELEM) ||
            (query->query.select.type == H5Q_TYPE_ATTR_VALUE));

    /* Keep a copy of elem to work on */
    if (0 == (type_size = H5T_get_size(type)))
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a valid size");

    /* Keep a copy of the query value */
    query_type = query->query.select.elem.data_elem.type;
    query_type_size = query->query.select.elem.data_elem.type_size;
    query_op = query->query.select.match_op;

    /* Promote type to compare elements with query */
    promoted_type = H5Q__promote_type(type, query_type, &match_type);
    if (0 == (promoted_type_size = H5T_get_size(promoted_type)))
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a valid size");

    /* Resize value and query value buf for convert
     * (promoted_type_size is always bigger) */
    if (NULL == (value_buf = H5MM_malloc(promoted_type_size)))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTALLOC, FAIL, "can't allocate value buffer");
    HDmemcpy(value_buf, value, type_size);

    if (NULL == (query_value_buf = H5MM_malloc(promoted_type_size)))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTALLOC, FAIL, "can't allocate value buffer");
    HDmemcpy(query_value_buf, query->query.select.elem.data_elem.value, query_type_size);

    /* Create temporary IDs for H5T_convert */
    if (FAIL == (type_id = H5I_register(H5I_DATATYPE, type, FALSE)))
        HGOTO_ERROR(H5E_ATOM, H5E_CANTREGISTER, FAIL, "unable to register datatype");
    if (FAIL == (query_type_id = H5I_register(H5I_DATATYPE, query_type, FALSE)))
        HGOTO_ERROR(H5E_ATOM, H5E_CANTREGISTER, FAIL, "unable to register datatype");
    if (FAIL == (promoted_type_id = H5I_register(H5I_DATATYPE, promoted_type, FALSE)))
        HGOTO_ERROR(H5E_ATOM, H5E_CANTREGISTER, FAIL, "unable to register datatype");

    /* Find the conversion function */
    if (NULL == (tpath = H5T_path_find(type, promoted_type, NULL, NULL, H5P_LST_DATASET_XFER_ID_g, FALSE)))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTINIT, FAIL, "unable to find type info");
    if (FAIL == (H5T_convert(tpath, type_id, promoted_type_id, 1, (size_t)0, (size_t)0, value_buf, NULL, H5P_LST_DATASET_XFER_ID_g)))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTCONVERT, FAIL, "can't convert value");

    if (NULL == (tpath = H5T_path_find(query_type, promoted_type, NULL, NULL, H5P_LST_DATASET_XFER_ID_g, FALSE)))
          HGOTO_ERROR(H5E_QUERY, H5E_CANTINIT, FAIL, "unable to find type info");
    if (FAIL == (H5T_convert(tpath, query_type_id, promoted_type_id, 1, (size_t)0, (size_t)0, query_value_buf, NULL, H5P_LST_DATASET_XFER_ID_g)))
         HGOTO_ERROR(H5E_QUERY, H5E_CANTCONVERT, FAIL, "can't convert query value");

    /* Could also use BOOST preprocessor for that but not really nice */
    switch (match_type) {
        case H5Q_NATIVE_INT_MATCH_CHAR:
            H5Q_CMP_DATA_ELEM(query_result, query_op, char, value_buf, query_value_buf);
            break;
        case H5Q_NATIVE_INT_MATCH_SHORT:
            H5Q_CMP_DATA_ELEM(query_result, query_op, short, value_buf, query_value_buf);
            break;
        case H5Q_NATIVE_INT_MATCH_INT:
            H5Q_CMP_DATA_ELEM(query_result, query_op, int, value_buf, query_value_buf);
            break;
        case H5Q_NATIVE_INT_MATCH_LONG:
            H5Q_CMP_DATA_ELEM(query_result, query_op, long, value_buf, query_value_buf);
            break;
        case H5Q_NATIVE_INT_MATCH_LLONG:
            H5Q_CMP_DATA_ELEM(query_result, query_op, long long, value_buf, query_value_buf);
            break;
        case H5Q_NATIVE_FLOAT_MATCH_FLOAT:
            H5Q_CMP_DATA_ELEM(query_result, query_op, float, value_buf, query_value_buf);
            break;
        case H5Q_NATIVE_FLOAT_MATCH_DOUBLE:
            H5Q_CMP_DATA_ELEM(query_result, query_op, double, value_buf, query_value_buf);
            break;
        case H5Q_INVALID_MATCH_TYPE:
        default:
            HGOTO_ERROR(H5E_DATATYPE, H5E_BADTYPE, FAIL, "unsupported/unrecognized datatype");
            break;
    }

    *result = query_result;

done:
    H5MM_free(value_buf);
    H5MM_free(query_value_buf);

    /* Free temporary IDs */
    if ((promoted_type_id != FAIL) && !H5I_remove(promoted_type_id))
        HGOTO_ERROR(H5E_ATOM, H5E_CANTFREE, FAIL, "problem freeing id");
    if ((query_type_id != FAIL) && !H5I_remove(query_type_id))
        HGOTO_ERROR(H5E_ATOM, H5E_CANTFREE, FAIL, "problem freeing id");
    if ((type_id != FAIL) && !H5I_remove(type_id))
        HGOTO_ERROR(H5E_ATOM, H5E_CANTFREE, FAIL, "problem freeing id");

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q__apply_data_elem() */

/*-------------------------------------------------------------------------
 * Function:    H5Q__promote_type
 *
 * Purpose: Private function for H5Q__apply_data_elem (promote data element type
 *          so that data element and query element can be compared).
 *
 * Return:  Success:    Pointer to promoted native type
 *          Failure:    NULL
 *
 *-------------------------------------------------------------------------
 */
static H5T_t *
H5Q__promote_type(const H5T_t *type1, const H5T_t *type2, H5Q_match_type_t *match_type)
{
    H5T_t *ret_value = NULL; /* Return value */
    H5T_class_t type1_class, type2_class, promoted_type_class;
    H5T_t *promoted_type;

    FUNC_ENTER_NOAPI_NOINIT

    /* Get class of types */
    if (H5T_NO_CLASS == (type1_class = H5T_get_class(type1, FALSE)))
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a valid class");
    if (H5T_NO_CLASS == (type2_class = H5T_get_class(type2, FALSE)))
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a valid class");

     if ((type1_class == H5T_FLOAT) || (type2_class == H5T_FLOAT)) {
         promoted_type_class = H5T_FLOAT;
     } else {
         promoted_type_class = H5T_INTEGER;
     }

    switch (promoted_type_class) {
         case H5T_INTEGER:
             if (H5Q_CMP_TYPE(type1, type2, (H5T_t *)H5I_object(H5T_NATIVE_LLONG_g))) {
                 *match_type = H5Q_NATIVE_INT_MATCH_LLONG;
                 promoted_type = (H5T_t *)H5I_object(H5T_NATIVE_LLONG_g);
             } else if (H5Q_CMP_TYPE(type1, type2, (H5T_t *)H5I_object(H5T_NATIVE_LONG_g))) {
                 *match_type = H5Q_NATIVE_INT_MATCH_LONG;
                 promoted_type = (H5T_t *)H5I_object(H5T_NATIVE_LONG_g);
             } else if (H5Q_CMP_TYPE(type1, type2, (H5T_t *)H5I_object(H5T_NATIVE_INT_g))) {
                 *match_type = H5Q_NATIVE_INT_MATCH_INT;
                 promoted_type = (H5T_t *)H5I_object(H5T_NATIVE_INT_g);
             } else if (H5Q_CMP_TYPE(type1, type2, (H5T_t *)H5I_object(H5T_NATIVE_SHORT_g))) {
                 *match_type = H5Q_NATIVE_INT_MATCH_SHORT;
                 promoted_type = (H5T_t *)H5I_object(H5T_NATIVE_SHORT_g);
             } else if (H5Q_CMP_TYPE(type1, type2, (H5T_t *)H5I_object(H5T_NATIVE_SCHAR_g))) {
                 *match_type = H5Q_NATIVE_INT_MATCH_CHAR;
                 promoted_type = (H5T_t *)H5I_object(H5T_NATIVE_SCHAR_g);
             } else {
                 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a valid type");
             }
             break;
         case H5T_FLOAT:
             if (H5Q_CMP_TYPE(type1, type2, (H5T_t *)H5I_object(H5T_NATIVE_DOUBLE_g))) {
                 *match_type = H5Q_NATIVE_FLOAT_MATCH_DOUBLE;
                 promoted_type = (H5T_t *)H5I_object(H5T_NATIVE_DOUBLE_g);
             } else if (H5Q_CMP_TYPE(type1, type2, (H5T_t *)H5I_object(H5T_NATIVE_FLOAT_g))) {
                 *match_type = H5Q_NATIVE_FLOAT_MATCH_FLOAT;
                 promoted_type = (H5T_t *)H5I_object(H5T_NATIVE_FLOAT_g);
             } else {
                 HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a valid type");
             }
             break;
         case H5T_NO_CLASS:
         case H5T_TIME:
         case H5T_STRING:
         case H5T_BITFIELD:
         case H5T_OPAQUE:
         case H5T_COMPOUND:
         case H5T_REFERENCE:
         case H5T_ENUM:
         case H5T_VLEN:
         case H5T_ARRAY:
         case H5T_NCLASSES:
         default:
             HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a valid class");
             break;
     }

    ret_value = promoted_type;

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q__promote_type() */

/*-------------------------------------------------------------------------
 * Function:    H5Q__apply_attr_name
 *
 * Purpose: Private function for H5Q__apply_atom (attribute name).
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5Q__apply_attr_name(const H5Q_t *query, hbool_t *result, const char *name)
{
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_NOAPI_NOINIT_NOERR

    HDassert(query);
    HDassert(query->query.select.type == H5Q_TYPE_ATTR_NAME);
    HDassert(result);
    HDassert(name);

    *result = (query->query.select.match_op == H5Q_MATCH_EQUAL) ?
            (0 == HDstrcmp(name, query->query.select.elem.attr_name.name)) :
            (0 != HDstrcmp(name, query->query.select.elem.attr_name.name));

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q__apply_attr_name() */

/*-------------------------------------------------------------------------
 * Function:    H5Q__apply_link_name
 *
 * Purpose: Private function for H5Q__apply_atom (link name).
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5Q__apply_link_name(const H5Q_t *query, hbool_t *result, const char *name)
{
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_NOAPI_NOINIT_NOERR

    HDassert(query);
    HDassert(query->query.select.type == H5Q_TYPE_LINK_NAME);
    HDassert(result);
    HDassert(name);

    *result = (query->query.select.match_op == H5Q_MATCH_EQUAL) ?
            (0 == HDstrcmp(name, query->query.select.elem.link_name.name)) :
            (0 != HDstrcmp(name, query->query.select.elem.link_name.name));

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q__apply_link_name() */

/*-------------------------------------------------------------------------
 * Function:    H5Qapply
 *
 * Purpose: Apply a query and return the result. Parameters, which the
 * query applies to, are determined by the type of the query.
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
hid_t
H5Qapply(hid_t loc_id, hid_t query_id, unsigned *result, hid_t vcpl_id)
{
    H5Q_t *query = NULL;
    H5G_loc_t loc;
    H5G_t *ret_grp;
    hid_t ret_value;

    FUNC_ENTER_API(FAIL)
    H5TRACE4("i", "ii*Iui", loc_id, query_id, result, vcpl_id);

    /* Check args and get the query objects */
    if (FAIL == H5G_loc(loc_id, &loc))
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a location");
    if (NULL == (query = (H5Q_t *) H5I_object_verify(query_id, H5I_QUERY)))
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a query ID");
    if (!result)
        HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "NULL pointer for result");

    /* Get the default view creation property list if the user didn't provide one */
    /* TODO fix that */
    if (H5P_DEFAULT == vcpl_id)
        vcpl_id = H5P_INDEX_ACCESS_DEFAULT;
    else
        if (TRUE != H5P_isa_class(vcpl_id, H5P_INDEX_ACCESS))
            HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not index access parms");

    /* Apply query */
    if (NULL == (ret_grp = H5Q_apply(&loc, loc_id, query, result, vcpl_id)))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTCOMPARE, FAIL, "unable to apply query");

    if (FAIL == (ret_value = H5I_register(H5I_GROUP, ret_grp, TRUE)))
        HGOTO_ERROR(H5E_ATOM, H5E_CANTREGISTER, FAIL, "unable to register group");

done:
    FUNC_LEAVE_API(ret_value)
} /* end H5Qapply() */

/*-------------------------------------------------------------------------
 * Function:    H5Q_apply
 *
 * Purpose: Private function for H5Qapply.
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
H5G_t *
H5Q_apply(const H5G_loc_t *loc, hid_t loc_id, const H5Q_t *query, unsigned *result,
        hid_t H5_ATTR_UNUSED vcpl_id)
{
    H5Q_apply_arg_t args;
    H5Q_view_t view = H5Q_VIEW_INITIALIZER(view); /* Resulting view */
    H5G_t *ret_grp = NULL; /* New group created */
    H5G_t *ret_value = NULL; /* Return value */
    H5P_genclass_t *pclass = NULL;
    unsigned flags;
    hid_t fapl_id = FAIL;
    H5F_t *new_file = NULL;
    H5G_loc_t file_loc;

    FUNC_ENTER_NOAPI_NOINIT

    HDassert(loc);
    HDassert(query);
    HDassert(result);

    /* First check and optimize query */
    /* TODO */

    /* Create new view and init args */
    args.query = query;
    args.result = result;
    args.view = &view;

    if (FAIL == H5O_visit(loc_id, ".", H5_INDEX_NAME, H5_ITER_NATIVE, H5Q__apply_iterate,
            &args, H5P_LINK_ACCESS_DEFAULT, H5AC_ind_dxpl_id))
        HGOTO_ERROR(H5E_SYM, H5E_BADITER, NULL, "object visitation failed");

    if (!H5Q_QUEUE_EMPTY(&view.reg_refs))
        H5Q_LOG_DEBUG("Number of reg refs: %zu\n", view.reg_refs.n_elem);
    if (!H5Q_QUEUE_EMPTY(&view.obj_refs))
        H5Q_LOG_DEBUG("Number of obj refs: %zu\n", view.obj_refs.n_elem);
    if (!H5Q_QUEUE_EMPTY(&view.attr_refs))
        H5Q_LOG_DEBUG("Number of attr refs: %zu\n", view.attr_refs.n_elem);

    /* Get property list class */
    if (NULL == (pclass = (H5P_genclass_t *)H5I_object_verify(H5P_FILE_ACCESS, H5I_GENPROP_CLS)))
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, NULL, "not a property list class");

    /* Create the new property list */
    if (FAIL == (fapl_id = H5P_create_id(pclass, TRUE)))
        HGOTO_ERROR(H5E_PLIST, H5E_CANTCREATE, NULL, "unable to create property list");

    /* Use the core VFD to store the view */
    if (FAIL == H5Pset_fapl_core(fapl_id, H5Q_VIEW_CORE_INCREMENT, FALSE))
        HGOTO_ERROR(H5E_PLIST, H5E_CANTSET, NULL, "unable to set property list to core VFD");

    /* Create a new file or truncate an existing file. */
    flags = H5F_ACC_EXCL | H5F_ACC_RDWR | H5F_ACC_CREAT;
    if (NULL == (new_file = H5F_open("view", flags, H5P_FILE_CREATE_DEFAULT, fapl_id, H5AC_dxpl_id)))
        HGOTO_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL, "unable to create file");

    /* Construct a group location for root group of the file */
    if (FAIL == H5G_root_loc(new_file, &file_loc))
        HGOTO_ERROR(H5E_SYM, H5E_BADVALUE, NULL, "unable to create location for file")

    /* Create the new group & get its ID */
    if (NULL == (ret_grp = H5G_create_anon(&file_loc, H5P_GROUP_CREATE_DEFAULT, H5P_GROUP_ACCESS_DEFAULT)))
        HGOTO_ERROR(H5E_SYM, H5E_CANTINIT, NULL, "unable to create group");

    /* Write view */
    if (FAIL == H5Q__view_write(ret_grp, &view))
        HGOTO_ERROR(H5E_QUERY, H5E_WRITEERROR, NULL, "can't write view");

    ret_value = ret_grp;

done:
    /* Release the group's object header, if it was created */
    if (ret_grp) {
        H5O_loc_t *grp_oloc;         /* Object location for group */

        /* Get the new group's object location */
        if (NULL == (grp_oloc = H5G_oloc(ret_grp)))
            HDONE_ERROR(H5E_SYM, H5E_CANTGET, NULL, "unable to get object location of group");

        /* Decrement refcount on group's object header in memory */
        if (FAIL == H5O_dec_rc_by_loc(grp_oloc, H5AC_dxpl_id))
            HDONE_ERROR(H5E_SYM, H5E_CANTDEC, NULL, "unable to decrement refcount on newly created object");
    } /* end if */

    /* Cleanup on failure */
    if (NULL == ret_value)
        if (ret_grp && (FAIL == H5G_close(ret_grp)))
            HDONE_ERROR(H5E_SYM, H5E_CLOSEERROR, NULL, "unable to release group");

    /* Close the property list */
    if ((fapl_id != FAIL) && (H5I_dec_app_ref(fapl_id) < 0))
        HDONE_ERROR(H5E_PLIST, H5E_CANTFREE, NULL, "can't close");

    /* Attempt to close the file/mount hierarchy */
    if (new_file && (FAIL == H5F_try_close(new_file)))
        HDONE_ERROR(H5E_FILE, H5E_CANTCLOSEFILE, NULL, "can't close file")

    /* Free the view */
    H5Q__view_free(&view);

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q_apply() */

/*-------------------------------------------------------------------------
 * Function:    H5Q__apply_iterate
 *
 * Purpose: Private function for H5Q_apply.
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5Q__apply_iterate(hid_t oid, const char *name, const H5O_info_t *oinfo, void *op_data)
{
    H5Q_apply_arg_t *args = (H5Q_apply_arg_t *) op_data;
    H5Q_type_t query_type;
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_NOAPI_NOINIT

    HDassert(args->query);
    HDassert(args->result);
    HDassert(args->view);

    if (FAIL == H5Q_get_type(args->query, &query_type))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTGET, FAIL, "unable to get query type");

    if (query_type != H5Q_TYPE_MISC) {
        if (FAIL == H5Q__apply_object(oid, name, oinfo, args))
            HGOTO_ERROR(H5E_QUERY, H5E_CANTCOMPARE, FAIL, "unable to compare query");
    } else {
        H5Q_combine_op_t op_type;
        H5Q_apply_arg_t args1, args2;
        H5Q_view_t view1 = H5Q_VIEW_INITIALIZER(view1), view2 = H5Q_VIEW_INITIALIZER(view2);
        unsigned result1 = 0, result2 = 0;

        if (FAIL == H5Q_get_combine_op(args->query, &op_type))
            HGOTO_ERROR(H5E_QUERY, H5E_CANTGET, FAIL, "unable to get combine op");

        args1.query = args->query->query.combine.l_query;
        args1.result = &result1;
        args1.view = &view1;

        args2.query = args->query->query.combine.r_query;
        args2.result = &result2;
        args2.view = &view2;

        if (FAIL == H5Q__apply_iterate(oid, name, oinfo, &args1))
            HGOTO_ERROR(H5E_QUERY, H5E_CANTCOMPARE, FAIL, "unable to apply query");
        if (FAIL == H5Q__apply_iterate(oid, name, oinfo, &args2))
            HGOTO_ERROR(H5E_QUERY, H5E_CANTCOMPARE, FAIL, "unable to apply query");

        if (FAIL == H5Q__view_combine(op_type, &view1, &view2, result1, result2,
                args->view, args->result))
            HGOTO_ERROR(H5E_QUERY, H5E_CANTMERGE, FAIL, "unable to merge results");

        if (result1) H5Q__view_free(&view1);
        if (result2) H5Q__view_free(&view2);
    }

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q__apply_iterate */

/*-------------------------------------------------------------------------
 * Function:    H5Q__apply_object
 *
 * Purpose: Private function for H5Q__apply_iterate.
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5Q__apply_object(hid_t oid, const char *name, const H5O_info_t *oinfo,
        void *udata)
{
    H5G_loc_t loc;
    H5Q_apply_arg_t *args = (H5Q_apply_arg_t *) udata;
    H5Q_type_t query_type;
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_NOAPI_NOINIT

    HDassert(name);
    HDassert(oinfo);
    HDassert(args);

    if (FAIL == H5G_loc(oid, &loc))
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a location");

    if (FAIL == H5Q_get_type(args->query, &query_type))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTGET, FAIL, "unable to get query type");

    switch (query_type) {
        /* If query on a link name, just compare the name of the object */
        case H5Q_TYPE_LINK_NAME:
            if (FAIL == H5Q__apply_object_link(&loc, name, oinfo, udata))
                HGOTO_ERROR(H5E_QUERY, H5E_CANTCOMPARE, FAIL, "can't apply link query to object");
            break;
        case H5Q_TYPE_DATA_ELEM:
            if (FAIL == H5Q__apply_object_data(&loc, name, oinfo, udata))
                HGOTO_ERROR(H5E_QUERY, H5E_CANTCOMPARE, FAIL, "can't apply data query to object");
            break;
        case H5Q_TYPE_ATTR_NAME:
        case H5Q_TYPE_ATTR_VALUE:
            if (FAIL == H5Q__apply_object_attr(&loc, name, oinfo, udata))
                HGOTO_ERROR(H5E_QUERY, H5E_CANTCOMPARE, FAIL, "can't apply data query to object");
            break;
        case H5Q_TYPE_MISC:
        default:
            HGOTO_ERROR(H5E_QUERY, H5E_BADTYPE, FAIL, "unsupported/unrecognized query type");
            break;
    }

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q__apply_object */

/*-------------------------------------------------------------------------
 * Function:    H5Q__apply_object_link
 *
 * Purpose: Private function for H5Q__apply_object.
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5Q__apply_object_link(H5G_loc_t *loc, const char *name, const H5O_info_t *oinfo,
        void *udata)
{
    hobj_ref_t ref_buf;
    hbool_t result;
    H5Q_apply_arg_t *args = (H5Q_apply_arg_t *) udata;
    const char *link_name = NULL;
    const char *trimmed_path = NULL;
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_NOAPI_NOINIT

    HDassert(loc);
    HDassert(name);
    HDassert(oinfo);
    HDassert(args);

    trimmed_path = HDstrrchr(name, '/');
    link_name = (trimmed_path) ? ++trimmed_path : name;

    if ((oinfo->type != H5O_TYPE_GROUP) && (oinfo->type != H5O_TYPE_DATASET))
        HGOTO_ERROR(H5E_QUERY, H5E_BADTYPE, FAIL, "unsupported/unrecognized object type");

    if (FAIL == H5Q_apply_atom(args->query, &result, link_name))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTCOMPARE, FAIL, "can't compare link name");

    if (!result) HGOTO_DONE(SUCCEED);

    *(args->result) = H5Q_REF_OBJ;

    H5Q_LOG_DEBUG("Match link name: %s (%s)\n", link_name, name);

    /* Keep object reference */
    if (FAIL == H5R_create(&ref_buf, H5R_OBJECT, loc, name, H5AC_dxpl_id))
        HGOTO_ERROR(H5E_DATASET, H5E_CANTCREATE, FAIL, "can't create region reference");
    if (FAIL == H5Q__view_append(args->view, H5R_OBJECT, &ref_buf))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTAPPEND, FAIL, "can't append region reference to view");

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q__apply_object_link */

/*-------------------------------------------------------------------------
 * Function:    H5Q__apply_object_data
 *
 * Purpose: Private function for H5Q__apply_object.
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5Q__apply_object_data(H5G_loc_t *loc, const char *name, const H5O_info_t *oinfo,
        void *udata)
{
    hreg_ref_t ref_buf = H5R_REG_REF_INITIALIZER;
    H5Q_apply_arg_t *args = (H5Q_apply_arg_t *) udata;
    hid_t obj_id = FAIL;
    H5S_t *dataspace = NULL;
    H5D_t *dataset = NULL;
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_NOAPI_NOINIT

    HDassert(loc);
    HDassert(name);
    HDassert(oinfo);
    HDassert(args);

    /* No data */
    if (oinfo->type == H5O_TYPE_GROUP)
        HGOTO_DONE(SUCCEED);

    if (oinfo->type != H5O_TYPE_DATASET)
        HGOTO_ERROR(H5E_QUERY, H5E_BADTYPE, FAIL, "unsupported/unrecognized object type");

    /* If query on a dataset, open the object and use H5D_query */
    if (FAIL == (obj_id = H5O_open_name(loc, name, H5P_LINK_ACCESS_DEFAULT, FALSE)))
        HGOTO_ERROR(H5E_DATASET, H5E_CANTOPENOBJ, FAIL, "can't open object");

    if (NULL == (dataset = (H5D_t *) H5I_object_verify(obj_id, H5I_DATASET)))
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a dataset");

    /* Query dataset */
    if (NULL == (dataspace = H5D_query(dataset, NULL, args->query, H5P_INDEX_ACCESS_DEFAULT, H5P_INDEX_XFER_DEFAULT)))
        HGOTO_ERROR(H5E_DATASET, H5E_CANTSELECT, FAIL, "can't query dataset");

    /* No element matched the query */
    if (H5S_SEL_NONE == H5S_get_select_type(dataspace))
        HGOTO_DONE(SUCCEED);

    *(args->result) = H5Q_REF_REG;

    /* Keep dataset region reference */
    if (FAIL == H5R_create(&ref_buf, H5R_REGION, loc, name, H5AC_dxpl_id, dataspace))
        HGOTO_ERROR(H5E_DATASET, H5E_CANTCREATE, FAIL, "can't get buffer size for region reference");
    if (NULL == (ref_buf.buf = H5MM_malloc(ref_buf.buf_size)))
        HGOTO_ERROR(H5E_QUERY, H5E_NOSPACE, FAIL, "can't allocate region reference buffer");
    if (FAIL == H5R_create(&ref_buf, H5R_REGION, loc, name, H5AC_dxpl_id, dataspace))
        HGOTO_ERROR(H5E_DATASET, H5E_CANTCREATE, FAIL, "can't create region reference");

    H5Q_LOG_DEBUG("Region reference size: %zu\n", ref_buf.buf_size);
    if (FAIL == H5Q__view_append(args->view, H5R_REGION, &ref_buf))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTAPPEND, FAIL, "can't append region reference to view");

done:
    if (dataspace) H5S_close(dataspace);
    if ((obj_id != FAIL) && (FAIL == H5I_dec_app_ref(obj_id)))
        HDONE_ERROR(H5E_OHDR, H5E_CANTRELEASE, FAIL, "unable to close object")
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q__apply_object_data */

/*-------------------------------------------------------------------------
 * Function:    H5Q__apply_object_attr
 *
 * Purpose: Private function for H5Q__apply_object.
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5Q__apply_object_attr(H5G_loc_t *loc, const char *name, const H5O_info_t *oinfo,
        void *udata)
{
    H5Q_apply_attr_arg_t attr_args;
    H5A_attr_iter_op_t attr_op; /* Attribute operator */
    H5Q_apply_arg_t *args = (H5Q_apply_arg_t *) udata;
    hid_t obj_id = FAIL;
    H5G_loc_t obj_loc;
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_NOAPI_NOINIT

    HDassert(loc);
    HDassert(name);
    HDassert(oinfo);
    HDassert(args);

    /* Build attribute operator info */
    attr_args.loc = loc;
    attr_args.loc_name = name;
    attr_args.apply_args = args;
    attr_op.op_type = H5A_ATTR_OP_LIB;
    attr_op.u.lib_op = (H5A_lib_iterate_t) H5Q__apply_object_attr_iterate;

    if (FAIL == (obj_id = H5O_open_name(loc, name, H5P_LINK_ACCESS_DEFAULT, FALSE)))
        HGOTO_ERROR(H5E_ATTR, H5E_CANTOPENOBJ, FAIL, "can't open object");

    /* Keep location of object */
    if (FAIL == H5G_loc(obj_id, &obj_loc))
        HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a location");
    attr_args.obj_loc = &obj_loc;

    /* Iterate over attributes */
    if (FAIL == (ret_value = H5O_attr_iterate(obj_id, H5AC_ind_dxpl_id,
            H5_INDEX_NAME, H5_ITER_NATIVE, 0, NULL, &attr_op, &attr_args)))
        HGOTO_ERROR(H5E_ATTR, H5E_BADITER, FAIL, "error iterating over attributes");

done:
    if ((obj_id != FAIL) && (FAIL == H5I_dec_app_ref(obj_id)))
        HDONE_ERROR(H5E_OHDR, H5E_CANTRELEASE, FAIL, "unable to close object")
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q__apply_object_attr */

/*-------------------------------------------------------------------------
 * Function:    H5Q__apply_object_attr_iterate
 *
 * Purpose: Private function for H5Q__apply_iterate.
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5Q__apply_object_attr_iterate(H5A_t *attr, void *udata)
{
    H5Q_apply_attr_arg_t *args = (H5Q_apply_attr_arg_t *) udata;
    H5Q_type_t query_type;
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_NOAPI_NOINIT

    HDassert(attr);
    HDassert(args);

    if (FAIL == H5Q_get_type(args->apply_args->query, &query_type))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTGET, FAIL, "unable to get query type");

    switch (query_type) {
        /* If query on an attribute name, just compare the name of the object */
        case H5Q_TYPE_ATTR_NAME:
            if (FAIL == H5Q__apply_object_attr_name(attr, udata))
                HGOTO_ERROR(H5E_QUERY, H5E_CANTCOMPARE, FAIL, "can't apply attr name query to object");
            break;
        case H5Q_TYPE_ATTR_VALUE:
            if (FAIL == H5Q__apply_object_attr_value(attr, udata))
                HGOTO_ERROR(H5E_QUERY, H5E_CANTCOMPARE, FAIL, "can't apply attr name query to object");
            break;
        case H5Q_TYPE_LINK_NAME:
        case H5Q_TYPE_DATA_ELEM:
        case H5Q_TYPE_MISC:
        default:
            HGOTO_ERROR(H5E_QUERY, H5E_BADTYPE, FAIL, "unsupported/unrecognized query type");
            break;
    }

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q__apply_object_attr_iterate */

/*-------------------------------------------------------------------------
 * Function:    H5Q__apply_object_attr_name
 *
 * Purpose: Private function for H5Q__apply_object_attr_iterate.
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5Q__apply_object_attr_name(H5A_t *attr, void *udata)
{
    H5Q_apply_attr_arg_t *args = (H5Q_apply_attr_arg_t *) udata;
    char *name = NULL;
    size_t name_len;
    hattr_ref_t ref_buf;
    hbool_t result = FALSE;
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_NOAPI_NOINIT

    HDassert(attr);
    HDassert(args);

    /* Get attribute name */
    if (0 == (name_len = (size_t) H5A_get_name(attr, 0, NULL)))
        HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, FAIL, "can't get size of attribute name");
    if (NULL == (name = (char *) H5MM_malloc(name_len + 1)))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTALLOC, FAIL, "can't allocate buffer for attribute name");
    if (0 == H5A_get_name(attr, name_len + 1, name))
        HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, FAIL, "can't get attribute name")

    if (FAIL == H5Q_apply_atom(args->apply_args->query, &result, (const char *) name))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTCOMPARE, FAIL, "can't compare attr name");

    if (!result) HGOTO_DONE(SUCCEED);

    *(args->apply_args->result) = H5Q_REF_ATTR;

    H5Q_LOG_DEBUG("Match attribute name: %s\n", (const char *) name);

    /* Keep object reference */
    if (FAIL == H5R_create(&ref_buf, H5R_ATTR, args->loc, args->loc_name, H5AC_dxpl_id, (const char *) name))
        HGOTO_ERROR(H5E_DATASET, H5E_CANTCREATE, FAIL, "can't create region reference");
    if (FAIL == H5Q__view_append(args->apply_args->view, H5R_ATTR, &ref_buf))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTAPPEND, FAIL, "can't append region reference to view");

done:
    H5MM_free(name);

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q__apply_object_attr_name */

/*-------------------------------------------------------------------------
 * Function:    H5Q__apply_object_attr_value
 *
 * Purpose: Private function for H5Q__apply_object_attr_iterate.
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5Q__apply_object_attr_value(H5A_t *iter_attr, void *udata)
{
    H5Q_apply_attr_arg_t *args = (H5Q_apply_attr_arg_t *) udata;
    char *name = NULL;
    size_t name_len;
    void *buf = NULL;
    size_t buf_size;
    H5A_t *attr = NULL;
    H5T_t *dt = NULL;
    H5S_t *space = NULL;
    size_t nelmts, elmt_size;
    H5S_sel_iter_op_t iter_op;  /* Operator for iteration */
    H5Q_apply_attr_elem_arg_t iter_args;
    hattr_ref_t ref_buf;
    hbool_t result = FALSE;
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_NOAPI_NOINIT

    HDassert(iter_attr);
    HDassert(args);

    /* TODO there may be another way of doing that but the attribute must be
     * opened cleanly and the attribute given is not open
     */

    /* Get attribute name */
    if (0 == (name_len = (size_t) H5A_get_name(iter_attr, 0, NULL)))
        HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, FAIL, "can't get size of attribute name");
    if (NULL == (name = (char *) H5MM_malloc(name_len + 1)))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTALLOC, FAIL, "can't allocate buffer for attribute name");
    if (0 == H5A_get_name(iter_attr, name_len + 1, (char *) name))
        HGOTO_ERROR(H5E_ATTR, H5E_CANTGET, FAIL, "can't get attribute name")

    /* Finish opening attribute */
    if (NULL == (attr = H5A_open(args->obj_loc, (const char *) name, H5AC_ind_dxpl_id)))
        HGOTO_ERROR(H5E_ATTR, H5E_CANTOPENOBJ, FAIL, "can't open attribute");

    /* Get attribute info */
    if (NULL == (dt = H5A_get_type(attr)))
        HGOTO_ERROR(H5E_OHDR, H5E_CANTGET, FAIL, "can't get attribute datatype");
    if (NULL == (space = H5A_get_space(attr)))
        HGOTO_ERROR(H5E_INDEX, H5E_CANTGET, FAIL, "can't get attribute dataspace");
    if (0 == (nelmts = (size_t) H5S_get_select_npoints(space)))
        HGOTO_ERROR(H5E_DATASPACE, H5E_BADVALUE, FAIL, "invalid number of elements");
    if (0 == (elmt_size = H5T_get_size(dt)))
        HGOTO_ERROR(H5E_DATATYPE, H5E_BADTYPE, FAIL, "invalid size of element");

    /* Allocate buffer to hold data */
    buf_size = nelmts * elmt_size;
    if (NULL == (buf = H5MM_malloc(buf_size)))
        HGOTO_ERROR(H5E_QUERY, H5E_NOSPACE, FAIL, "can't allocate read buffer");

    /* Read data */
    if (FAIL == H5A_read(attr, dt, buf, H5AC_ind_dxpl_id))
        HGOTO_ERROR(H5E_ATTR, H5E_READERROR, FAIL, "unable to read attribute");

    iter_args.query = args->apply_args->query;
    iter_args.result = &result;

    iter_op.op_type = H5S_SEL_ITER_OP_LIB;
    iter_op.u.lib_op = H5Q__apply_object_attr_value_iterate;

    /* Iterate over attribute elements to compare values */
    if (FAIL == H5S_select_iterate(buf, dt, space, &iter_op, &iter_args))
        HGOTO_ERROR(H5E_DATASPACE, H5E_CANTCOMPARE, FAIL, "unable to compare attribute elements");

    if (!result) HGOTO_DONE(SUCCEED);

    *(args->apply_args->result) = H5Q_REF_ATTR;

    H5Q_LOG_DEBUG("Match value of attribute name: %s\n", (const char *) name);

    /* Keep object reference */
    if (FAIL == H5R_create(&ref_buf, H5R_ATTR, args->loc, args->loc_name, H5AC_dxpl_id, (const char *) name))
        HGOTO_ERROR(H5E_DATASET, H5E_CANTCREATE, FAIL, "can't create region reference");
    if (FAIL == H5Q__view_append(args->apply_args->view, H5R_ATTR, &ref_buf))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTAPPEND, FAIL, "can't append region reference to view");

done:
    H5MM_free(name);
    H5MM_free(buf);
    if (attr) H5A_close(attr);
    if (dt) H5T_close(dt);
    if (space) H5S_close(space);

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q__apply_object_attr_value */

/*-------------------------------------------------------------------------
 * Function:    H5Q__apply_object_attr_value_iterate
 *
 * Purpose: Private function for H5Q__apply_iterate.
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5Q__apply_object_attr_value_iterate(void *elem, const H5T_t *type,
        unsigned H5_ATTR_UNUSED ndim, const hsize_t H5_ATTR_UNUSED *point, void *udata)
{
    H5Q_apply_attr_elem_arg_t *args = (H5Q_apply_attr_elem_arg_t *) udata;
    hbool_t result;
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_NOAPI_NOINIT

    HDassert(elem);
    HDassert(type);

    /* Apply the query */
    if (FAIL == H5Q_apply_atom(args->query, &result, type, elem))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTCOMPARE, FAIL, "unable to apply query to data element");

    *(args->result) |= result;

done:

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q__apply_object_attr_value_iterate */

/*-------------------------------------------------------------------------
 * Function:    H5Q__view_append
 *
 * Purpose: Private function for H5Q__apply_iterate.
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5Q__view_append(H5Q_view_t *view, H5R_type_t ref_type, void *ref)
{
    H5Q_ref_entry_t *ref_entry;
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_NOAPI_NOINIT

    HDassert(view);
    HDassert(ref);

    if (NULL == (ref_entry = (H5Q_ref_entry_t *) H5MM_calloc(sizeof(H5Q_ref_entry_t))))
        HGOTO_ERROR(H5E_QUERY, H5E_CANTALLOC, FAIL, "can't allocate ref entry");
    ref_entry->ref_type = ref_type;

    switch (ref_type) {
        case H5R_REGION:
            HDmemcpy(&ref_entry->ref.reg, ref, sizeof(hreg_ref_t));
            H5Q_QUEUE_INSERT_TAIL(&view->reg_refs, ref_entry, entry);
            break;
        case H5R_OBJECT:
            HDmemcpy(&ref_entry->ref.obj, ref, sizeof(hobj_ref_t));
            H5Q_QUEUE_INSERT_TAIL(&view->obj_refs, ref_entry, entry);
            break;
        case H5R_ATTR:
            HDmemcpy(&ref_entry->ref.attr, ref, sizeof(hattr_ref_t));
            H5Q_QUEUE_INSERT_TAIL(&view->attr_refs, ref_entry, entry);
            break;
        case H5R_DATASET_REGION:
        case H5R_BADTYPE:
        case H5R_MAXTYPE:
        default:
            HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "invalid ref type");
    }

done:
    if (FAIL == ret_value) H5MM_free(ref_entry);
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q__view_append */

/*-------------------------------------------------------------------------
 * Function:    H5Q__view_combine
 *
 * Purpose: Private function for H5Q__apply_iterate.
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5Q__view_combine(H5Q_combine_op_t combine_op, H5Q_view_t *view1, H5Q_view_t *view2,
        unsigned result1, unsigned result2, H5Q_view_t *view, unsigned *result)
{
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_NOAPI_NOINIT

    HDassert(view);
    HDassert(result);

    if ((combine_op == H5Q_COMBINE_AND) && ((result1 && result2))) {
        unsigned combine_result = (result1 > result2) ? result1 : result2;

        H5Q_LOG_DEBUG("Result 1 (%x), result 2 (%x), combined result (%x)\n",
                result1, result2, combine_result);

        *result = combine_result;

        switch (combine_result) {
            case H5Q_REF_REG:
            {
                /* Combined results are on the same object (here, dataset),
                 * therefore at this point only result1 or resul2 has a region
                 * reference */
                if (result1 & H5Q_REF_REG)
                    H5Q_QUEUE_CONCAT(&view->reg_refs, &view1->reg_refs);
                if (result2 & H5Q_REF_REG)
                    H5Q_QUEUE_CONCAT(&view->reg_refs, &view2->reg_refs);
            }
                break;
            case H5Q_REF_OBJ:
                break;
            case H5Q_REF_ATTR:
                /* TODO attribute AND attribute what does it mean ? */
                break;
            default:
                HGOTO_ERROR(H5E_QUERY, H5E_BADTYPE, FAIL, "unsupported/unrecognized query type");
                break;
        }
    } else if ((combine_op == H5Q_COMBINE_OR) && ((result1 || result2))) {
        unsigned combine_result = result1 | result2;

        H5Q_LOG_DEBUG("Result 1 (%x), result 2 (%x), combined result (%x)\n",
                result1, result2, combine_result);

        *result = combine_result;

        /* Combine reg references */
        H5Q_QUEUE_CONCAT(&view->reg_refs, &view1->reg_refs);
        H5Q_QUEUE_CONCAT(&view->reg_refs, &view2->reg_refs);

        /* Combine obj references */
        H5Q_QUEUE_CONCAT(&view->obj_refs, &view1->obj_refs);
        H5Q_QUEUE_CONCAT(&view->obj_refs, &view2->obj_refs);

        /* Combine attr references */
        H5Q_QUEUE_CONCAT(&view->attr_refs, &view1->attr_refs);
        H5Q_QUEUE_CONCAT(&view->attr_refs, &view2->attr_refs);
    }

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q__view_combine */

/*-------------------------------------------------------------------------
 * Function:    H5Q__view_write
 *
 * Purpose: Private function for H5Q_apply.
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5Q__view_write(H5G_t *grp, H5Q_view_t *view)
{
    H5G_loc_t loc;
    H5D_t *dset = NULL;
    H5S_t *mem_space = NULL;
    H5S_t *space = NULL;
    H5Q_ref_head_t *refs[H5Q_VIEW_REF_NTYPES] = { &view->reg_refs, &view->obj_refs,
            &view->attr_refs };
    const char *dset_name[H5Q_VIEW_REF_NTYPES] = { H5Q_VIEW_REF_REG_NAME,
            H5Q_VIEW_REF_OBJ_NAME, H5Q_VIEW_REF_ATTR_NAME };
    hid_t ref_types[H5Q_VIEW_REF_NTYPES] = { H5T_STD_REF_REG,
            H5T_STD_REF_OBJ, H5T_STD_REF_ATTR };
    herr_t ret_value = SUCCEED; /* Return value */
    int i;

    FUNC_ENTER_NOAPI_NOINIT

    HDassert(grp);

    /* Get location of group */
    loc.oloc = H5G_oloc(grp);
    loc.path = H5G_nameof(grp);

    /* Iterate over reference types and write references if any */
    for (i = 0; i < H5Q_VIEW_REF_NTYPES; i++) {
        H5Q_ref_entry_t *ref_entry = NULL;
        hsize_t n_elem = refs[i]->n_elem;
        hsize_t start = 0;

        if (!n_elem)
            break;

        /* Create dataspace */
        if (NULL == (space = H5S_create_simple(1, &n_elem, NULL)))
            HGOTO_ERROR(H5E_DATASPACE, H5E_CANTCREATE, FAIL, "unable to create dataspace");

        /* Create the new dataset & get its ID */
        if (NULL == (dset = H5D_create_named(&loc, dset_name[i], ref_types[i],
                space, H5P_LINK_CREATE_DEFAULT, H5P_DATASET_CREATE_DEFAULT,
                H5P_DATASET_ACCESS_DEFAULT, H5AC_dxpl_id)))
            HGOTO_ERROR(H5E_DATASET, H5E_CANTCREATE, FAIL, "unable to create dataset");

        /* Iterate over reference entries in view */
        H5Q_QUEUE_FOREACH(ref_entry, refs[i], entry) {
            hsize_t count = 1;

            if (NULL == (mem_space = H5S_create_simple(1, &count, NULL)))
                HGOTO_ERROR(H5E_DATASPACE, H5E_CANTCREATE, FAIL, "unable to create dataspace");
            if (FAIL == H5S_select_hyperslab(space, H5S_SELECT_SET, &start, NULL, &count, NULL))
                HGOTO_ERROR(H5E_DATASPACE, H5E_CANTINIT, FAIL, "unable to set hyperslab selection")
            if (FAIL == H5D_write(dset, FALSE, ref_types[i], mem_space, space,
                    H5P_DATASET_XFER_DEFAULT, &ref_entry->ref))
                HGOTO_ERROR(H5E_DATASET, H5E_WRITEERROR, FAIL, "unable to write dataset");
            if (FAIL == H5S_close(mem_space))
                HGOTO_ERROR(H5E_DATASPACE, H5E_CANTCLOSEOBJ, FAIL, "unable to close dataspace");
            mem_space = NULL;

            /* Increment reference position in file */
            start++;
        }

        if (FAIL == H5D_close(dset))
            HGOTO_ERROR(H5E_DATASET, H5E_CANTCLOSEOBJ, FAIL, "unable to close dataset");
        dset = NULL;
        if (FAIL == H5S_close(space))
            HGOTO_ERROR(H5E_DATASPACE, H5E_CANTCLOSEOBJ, FAIL, "unable to close dataspace");
        space = NULL;
    }

done:
    if (dset) H5D_close(dset);
    if (space) H5S_close(space);
    if (mem_space) H5S_close(mem_space);

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q__view_write */

/*-------------------------------------------------------------------------
 * Function:    H5Q__view_free
 *
 * Purpose: Private function for H5Q_apply.
 *
 * Return:  Non-negative on success/Negative on failure
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5Q__view_free(H5Q_view_t *view)
{
    H5Q_ref_head_t *refs[H5Q_VIEW_REF_NTYPES] = { &view->reg_refs, &view->obj_refs,
            &view->attr_refs };
    herr_t ret_value = SUCCEED; /* Return value */
    int i;

    FUNC_ENTER_NOAPI_NOINIT_NOERR

    for (i = 0; i < H5Q_VIEW_REF_NTYPES; i++) {
        while (!H5Q_QUEUE_EMPTY(refs[i])) {
            H5Q_ref_entry_t *ref_entry = H5Q_QUEUE_FIRST(refs[i]);
            H5Q_QUEUE_REMOVE_HEAD(refs[i], entry);
            H5MM_free(ref_entry);
        }
    }

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5Q__view_free */