Blame view

src/util/kaldi-table-inl.h 97.9 KB
8dcb6dfcb   Yannick Estève   first commit
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
  // util/kaldi-table-inl.h
  
  // Copyright 2009-2011    Microsoft Corporation
  //                2013    Johns Hopkins University (author: Daniel Povey)
  //                2016    Xiaohui Zhang
  
  // See ../../COPYING for clarification regarding multiple authors
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //  http://www.apache.org/licenses/LICENSE-2.0
  //
  // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  // MERCHANTABLITY OR NON-INFRINGEMENT.
  // See the Apache 2 License for the specific language governing permissions and
  // limitations under the License.
  
  
  #ifndef KALDI_UTIL_KALDI_TABLE_INL_H_
  #define KALDI_UTIL_KALDI_TABLE_INL_H_
  
  #include <algorithm>
  #include <string>
  #include <thread>
  #include <utility>
  #include <vector>
  #include <errno.h>
  #include "util/kaldi-io.h"
  #include "util/kaldi-holder.h"
  #include "util/text-utils.h"
  #include "util/stl-utils.h"  // for StringHasher.
  #include "util/kaldi-semaphore.h"
  
  
  namespace kaldi {
  
  /// \addtogroup table_impl_types
  /// @{
  
  template<class Holder> class SequentialTableReaderImplBase {
   public:
    typedef typename Holder::T T;
    // note that Open takes rxfilename not rspecifier.  Open will only be
    // called on a just-allocated object.
    virtual bool Open(const std::string &rxfilename) = 0;
    // Done() should be called on a successfully opened, not-closed object.
    // only throws if called at the wrong time (i.e. code error).
    virtual bool Done() const = 0;
    // Returns true if the reader is open [i.e. Open() succeeded and
    // the user has not called Close()]
    virtual bool IsOpen() const = 0;
    // Returns the current key; it is valid to call this if Done() returned false.
    // Only throws on code error (i.e. called at the wrong time).
    virtual std::string Key() = 0;
    // Returns the value associated with the current key.  Valid to call it if
    // Done() returned false.  It throws if the value could not be read.  [However
    // if you use the ,p modifier it will never throw, unless you call it at the
    // wrong time, i.e. unless there is a code error.]
    virtual T &Value() = 0;
    virtual void FreeCurrent() = 0;
    // move to the next object.  This won't throw unless called wrongly (e.g. on
    // non-open archive.]
    virtual void Next() = 0;
    // Close the table.  Returns its status as bool so it won't throw, unless
    // called wrongly [i.e. on non-open archive.]
    virtual bool Close() = 0;
    // SwapHolder() is not part of the public interface of SequentialTableReader.
    // It should be called when it would be valid to call Value() or FreeCurrent()
    // (i.e. when a value is stored), and after this it's not valid to get the
    // value any more until you call Next().  It swaps the contents of
    // this->holder_ with those of 'other_holder'.  It's needed as part of how
    // we implement SequentialTableReaderBackgroundImpl.
    virtual void SwapHolder(Holder *other_holder) = 0;
    SequentialTableReaderImplBase() { }
    virtual ~SequentialTableReaderImplBase() { }  // throws.
   private:
    KALDI_DISALLOW_COPY_AND_ASSIGN(SequentialTableReaderImplBase);
  };
  
  // This is the implementation for SequentialTableReader
  // when it's actually a script file.
  template<class Holder>  class SequentialTableReaderScriptImpl:
        public SequentialTableReaderImplBase<Holder> {
   public:
    typedef typename Holder::T T;
  
    SequentialTableReaderScriptImpl(): state_(kUninitialized) { }
  
    // You may call Open from states kUninitialized and kError.
    // It may leave the object in any of the states.
    virtual bool Open(const std::string &rspecifier) {
      if (state_ != kUninitialized && state_ != kError)
        if (!Close())  // call Close() yourself to suppress this exception.
          KALDI_ERR << "Error closing previous input: "
                    << "rspecifier was " << rspecifier_;
      bool binary;
      rspecifier_ = rspecifier;
      RspecifierType rs = ClassifyRspecifier(rspecifier, &script_rxfilename_,
                                             &opts_);
      KALDI_ASSERT(rs == kScriptRspecifier);
      if (!script_input_.Open(script_rxfilename_, &binary)) {  // Failure on Open
        KALDI_WARN << "Failed to open script file "
                   << PrintableRxfilename(script_rxfilename_);
        state_ = kUninitialized;
        return false;
      } else {  // Open succeeded.
        if (binary) {
          KALDI_WARN << "Script file should not be binary file.";
          SetErrorState();
          return false;
        } else {
          state_ = kFileStart;
          Next();
          if (state_ == kError)
            return false;
          // any other status, including kEof, is OK from the point of view of
          // the 'open' function (empty scp file is not inherently an error).
          return true;
        }
      }
    }
  
    virtual bool IsOpen() const {
      switch (state_) {
        case kEof: case kHaveScpLine: case kHaveObject: case kHaveRange:
          return true;
        case kUninitialized: case kError:
          return false;
        default: KALDI_ERR << "IsOpen() called on invalid object.";
          // note: kFileStart is not a valid state for the user to call a member
          // function (we never return from a public function in this state).
          return false;
      }
    }
  
    virtual bool Done() const {
      switch (state_) {
        case kHaveScpLine: case kHaveObject: case kHaveRange: return false;
        case kEof: case kError: return true;  // Error condition, like Eof, counts
          // as Done(); the destructor/Close() will inform the user of the error.
        default: KALDI_ERR << "Done() called on TableReader object at the wrong"
            " time.";
          return false;
      }
    }
  
    virtual std::string Key() {
      // Valid to call this whenever Done() returns false.
      switch (state_) {
        case kHaveScpLine: case kHaveObject: case kHaveRange: break;
        default:
          // coding error.
          KALDI_ERR << "Key() called on TableReader object at the wrong time.";
      }
      return key_;
    }
  
    T &Value() {
      if (!EnsureObjectLoaded())
        KALDI_ERR << "Failed to load object from "
                  << PrintableRxfilename(data_rxfilename_)
                  << " (to suppress this error, add the permissive "
                  << "(p, ) option to the rspecifier.";
      // Because EnsureObjectLoaded() returned with success, we know
      // that if range_ is nonempty (i.e. a range was requested), the
      // state will be kHaveRange.
      if (state_ == kHaveRange) {
        return range_holder_.Value();
      } else {
        KALDI_ASSERT(state_ == kHaveObject);
        return holder_.Value();
      }
    }
  
    void FreeCurrent() {
      if (state_ == kHaveObject) {
        holder_.Clear();
        state_ = kHaveScpLine;
      } else if (state_ == kHaveRange) {
        range_holder_.Clear();
        state_ = kHaveObject;
      } else {
        KALDI_WARN << "FreeCurrent called at the wrong time.";
      }
    }
  
    void SwapHolder(Holder *other_holder) {
      // call Value() to ensure we have a value, and ignore its return value while
      // suppressing compiler warnings by casting to void.  It will cause the
      // program to die with KALDI_ERR if we couldn't get a value.
      (void) Value();
      // At this point we know that we successfully loaded an object,
      // and if there was a range specified, it's in range_holder_.
      if (state_ == kHaveObject) {
        holder_.Swap(other_holder);
        state_ = kHaveScpLine;
      } else if (state_ == kHaveRange) {
        range_holder_.Swap(other_holder);
        state_ = kHaveObject;
        // This indicates that we still have the base object (but no range).
      } else {
        KALDI_ERR << "Code error";
      }
      // Note: after this call there may be some junk left in range_holder_ or
      // holder_, but it won't matter.  We avoid calling Clear() on them, as this
      // function needs to be lightweight for the 'bg' feature to work well.
    }
  
    // Next goes to the next object.
    // It can leave the object in most of the statuses, but
    // the only circumstances under which it will return are:
    //  either:
    //  - if Done() returned true, i.e. kError or kEof.
    //  or:
    //  - in non-permissive mode, status kHaveScpLine or kHaveObjecct
    //  - in permissive mode, only when we successfully have an object,
    //    which means either (kHaveObject and range_.empty()), or
    //    kHaveRange.
    void Next() {
      while (1) {
        NextScpLine();
        if (Done()) return;
        if (opts_.permissive) {
          // Permissive mode means, when reading scp files, we treat keys whose
          // scp entry cannot be read as nonexistent.  This means trying to read.
          if (EnsureObjectLoaded()) return;  // Success.
          // else try the next scp line.
        } else {
          return;  // We go the next key; Value() will crash if we can't read the
                   // object on the scp line.
        }
      }
    }
  
    // This function may be entered at in any state.  At exit, the object will be
    // in state kUninitialized.  It only returns false in the situation where we
    // were at the end of the stream (kEof) and the script_input_ was a pipe and
    // it ended with error status; this is so that we can catch errors from
    // programs that we invoked via a pipe.
    virtual bool Close() {
      int32 status = 0;
      if (script_input_.IsOpen())
        status = script_input_.Close();
      if (data_input_.IsOpen())
        data_input_.Close();
      range_holder_.Clear();
      holder_.Clear();
      if (!this->IsOpen())
        KALDI_ERR << "Close() called on input that was not open.";
      StateType old_state = state_;
      state_ = kUninitialized;
      if (old_state == kError || (old_state == kEof && status != 0)) {
        if (opts_.permissive) {
          KALDI_WARN << "Close() called on scp file with read error, ignoring the"
              " error because permissive mode specified.";
          return true;
        } else {
          return false;  // User will do something with the error status.
        }
      } else {
        return true;
      }
      // Possible states                                          Return value
      // kLoadSucceeded/kRangeSucceeded/kRangeFailed              true
      // kError (if opts_.permissive)                             true
      // kError (if !opts_.permissive)                            false
      // kEof (if script_input_.Close() && !opts.permissive)      false
      // kEof (if !script_input_.Close() || opts.permissive)      true
      // kUninitialized/kFileStart/kHaveScpLine                   true
      // kUnitialized                                             true
    }
  
    virtual ~SequentialTableReaderScriptImpl() {
      if (this->IsOpen() && !Close())
        KALDI_ERR << "TableReader: reading script file failed: from scp "
                        << PrintableRxfilename(script_rxfilename_);
    }
   private:
  
    // Function EnsureObjectLoaded() ensures that we have fully loaded any object
    // (including object range) associated with the current key, and returns true
    // on success (i.e. we have the object) and false on failure.
    //
    // Possible entry states: kHaveScpLine, kLoadSucceeded, kRangeSucceeded
    //
    // Possible exit states: kHaveScpLine, kLoadSucceeded, kRangeSucceeded.
    //
    // Note: the return status has information that cannot be deduced from
    // just the exit state.  If the object could not be loaded we go to state
    // kHaveScpLine but return false; and if the range was requested but
    // could not be extracted, we go to state kLoadSucceeded but return false.
    bool EnsureObjectLoaded() {
      if (!(state_ == kHaveScpLine || state_ == kHaveObject ||
            state_ == kHaveRange))
        KALDI_ERR << "Invalid state (code error)";
  
      if (state_ == kHaveScpLine) {  // need to load the object into holder_.
        bool ans;
        // note, NULL means it doesn't read the binary-mode header
        if (Holder::IsReadInBinary()) {
          ans = data_input_.Open(data_rxfilename_, NULL);
        } else {
          ans = data_input_.OpenTextMode(data_rxfilename_);
        }
        if (!ans) {
          KALDI_WARN << "Failed to open file "
                     << PrintableRxfilename(data_rxfilename_);
          return false;
        } else {
          if (holder_.Read(data_input_.Stream())) {
            state_ = kHaveObject;
          } else {  // holder_ will not contain data.
            KALDI_WARN << "Failed to load object from "
                       << PrintableRxfilename(data_rxfilename_);
            return false;
          }
        }
      }
      // OK, at this point the state must be either
      // kHaveObject or kHaveRange.
      if (range_.empty()) {
        // if range_ is the empty string, we should not be in the state
        // kHaveRange.
        KALDI_ASSERT(state_ == kHaveObject);
        return true;
      }
      // range_ is nonempty.
      if (state_ == kHaveRange) {
        // range was already extracted, so there nothing to do.
        return true;
      }
      // OK, range_ is nonempty and state_ is kHaveObject.  We attempt to extract
      // the range object.  Note: ExtractRange() will throw with KALDI_ERR if the
      // object type doesn't support ranges.
      if (!range_holder_.ExtractRange(holder_, range_)) {
        KALDI_WARN  << "Failed to load object from "
                    << PrintableRxfilename(data_rxfilename_)
                    << "[" << range_ << "]";
        return false;
      } else {
        state_ = kHaveRange;
        return true;
      }
    }
  
    void SetErrorState() {
      state_ = kError;
      script_input_.Close();
      data_input_.Close();
      holder_.Clear();
      range_holder_.Clear();
    }
  
    // Reads the next line in the script file.
    // Possible entry states: kHaveObject, kHaveRange, kHaveScpLine, kFileStart.
    // Possible exit states: kEof, kError, kHaveScpLine, kHaveObject.
    void NextScpLine() {
      switch (state_) {  // Check and simplify the state.
        case kHaveRange:
          range_holder_.Clear();
          state_ = kHaveObject;
          break;
        case kHaveScpLine: case kHaveObject: case kFileStart: break;
        default:
          // No other states are valid to call Next() from.
          KALDI_ERR << "Reading script file: Next called wrongly.";
      }
      // at this point the state will be kHaveObject, kHaveScpLine, or kFileStart.
      std::string line;
      if (getline(script_input_.Stream(), line)) {
        // After extracting "key" from "line", we put the rest
        // of "line" into "rest", and then extract data_rxfilename_
        // (e.g. 1.ark:100) and possibly the range_ specifer
        // (e.g. [1:2,2:10]) from "rest".
        std::string data_rxfilename, rest;
        SplitStringOnFirstSpace(line, &key_, &rest);
        if (!key_.empty() && !rest.empty()) {
          // Got a valid line.
          if (rest[rest.size()-1] == ']') {
            if(!ExtractRangeSpecifier(rest, &data_rxfilename, &range_)) {
              KALDI_WARN << "Reading rspecifier '" << rspecifier_
                         << ", cannot make sense of scp line "
                         << line;
              SetErrorState();
              return;
            }
          } else {
            data_rxfilename = rest;
            range_ = "";
          }
          bool filenames_equal = (data_rxfilename_ == data_rxfilename);
          if (!filenames_equal)
            data_rxfilename_ = data_rxfilename;
          if (state_ == kHaveObject) {
            if (!filenames_equal) {
              holder_.Clear();
              state_ = kHaveScpLine;
            }
            // else leave state_ at kHaveObject and leave the object in the
            // holder.
          } else {
            state_ = kHaveScpLine;
          }
        } else {
          KALDI_WARN << "We got an invalid line in the scp file. "
                     << "It should look like: some_key 1.ark:10, got: "
                     << line;
          SetErrorState();
        }
      } else {
        state_ = kEof;  // there is nothing more in the scp file.  Might as well
                        // close input streams as we don't need them.
        script_input_.Close();
        if (data_input_.IsOpen())
          data_input_.Close();
        holder_.Clear();  // clear the holder if it was nonempty.
        range_holder_.Clear();  // clear the range holder if it was nonempty.
      }
    }
  
    std::string rspecifier_;  // the rspecifier that this class was opened with.
    RspecifierOptions opts_;  // options.
    std::string script_rxfilename_;  // rxfilename of the script file.
  
    Input script_input_;  // Input object for the .scp file
    Input data_input_;   // Input object for the entries in the script file;
                         // we make this a class member instead of a local variable,
                         // so that rspecifiers of the form filename:byte-offset,
                         // e.g. foo.ark:12345, can be handled using fseek().
  
    Holder holder_;       // Holds the object.
    Holder range_holder_; // Holds the partial object corresponding to the object
                          // range specifier 'range_'; this is only used when
                          // 'range_' is specified, i.e. when the .scp file
                          // contains lines of the form rspecifier[range], like
                          // foo.ark:242[0:9] (representing a row range of a
                          // matrix).
  
  
    std::string key_;  // the key of the current scp line we're processing
    std::string data_rxfilename_;  // the rxfilename corresponding to the current key
    std::string range_;  // the range of object corresponding to the current key, if an
                         // object range was specified in the script file, else "".
  
    enum StateType {
      //  Summary of the states this object can be in (state_).
      //
      //                (*) Does holder_ contain the object corresponding to
      //                    data_rxfilename_ ?
      //                    (*) Does range_holder_ contain a range object?
      //                         (*) is script_input_ open?
      //                             (*) are key_, data_rxfilename_ and range_ [if applicable] set?
      //
      kUninitialized, // no  no  no  no            Uninitialized or closed object.
      kFileStart,     // no  no  yes no            We just opened the .scp file (we'll never be in this
                      //                           state when a user-visible function is called.)
      kEof,           // no  no  no  no            We did Next() and found eof in script file.
      kError,         // no  no  no  no            Error reading or parsing script file.
      kHaveScpLine,   // no  no  yes yes           Have a line of the script file but nothing else.
      kHaveObject,    // yes no  yes yes           holder_ contains an object but range_holder_ does not.
      kHaveRange,     // yes yes yes yes           we have the range object in range_holder_ (implies
                      //                           range_ nonempty).
    } state_;
  
  
  };
  
  
  // This is the implementation for SequentialTableReader
  // when it's an archive.  Note that the archive format is:
  // key1 [space] object1 key2 [space]
  // object2 ... eof.
  // "object1" is the output of the Holder::Write function and will
  // typically contain a binary header (in binary mode) and then
  // the output of object.Write(os, binary).
  // The archive itself does not care whether it is in binary
  // or text mode, for reading purposes.
  
  template<class Holder>  class SequentialTableReaderArchiveImpl:
        public SequentialTableReaderImplBase<Holder> {
   public:
    typedef typename Holder::T T;
  
    SequentialTableReaderArchiveImpl(): state_(kUninitialized) { }
  
    virtual bool Open(const std::string &rspecifier) {
      if (state_ != kUninitialized) {
        if (!Close()) {  // call Close() yourself to suppress this exception.
          if (opts_.permissive)
            KALDI_WARN << "Error closing previous input "
                "(only warning, since permissive mode).";
          else
            KALDI_ERR << "Error closing previous input.";
        }
      }
      rspecifier_ = rspecifier;
      RspecifierType rs = ClassifyRspecifier(rspecifier,
                                             &archive_rxfilename_,
                                             &opts_);
      KALDI_ASSERT(rs == kArchiveRspecifier);
  
      bool ans;
      // NULL means don't expect binary-mode header
      if (Holder::IsReadInBinary())
        ans = input_.Open(archive_rxfilename_, NULL);
      else
        ans = input_.OpenTextMode(archive_rxfilename_);
      if (!ans) {  // header.
        KALDI_WARN << "Failed to open stream "
                   << PrintableRxfilename(archive_rxfilename_);
        state_ = kUninitialized;  // Failure on Open
        return false;  // User should print the error message.
      }
      state_ = kFileStart;
      Next();
      if (state_ == kError) {
        KALDI_WARN << "Error beginning to read archive file (wrong filename?): "
                   << PrintableRxfilename(archive_rxfilename_);
        input_.Close();
        state_ = kUninitialized;
        return false;
      }
      KALDI_ASSERT(state_ == kHaveObject || state_ == kEof);
      return true;
    }
  
    virtual void Next() {
      switch (state_) {
        case kHaveObject:
          holder_.Clear();
          break;
        case kFileStart: case kFreedObject:
          break;
        default:
          KALDI_ERR << "Next() called wrongly.";
      }
      std::istream &is = input_.Stream();
      is.clear();  // Clear any fail bits that may have been set... just in case
      // this happened in the Read function.
      is >> key_;  // This eats up any leading whitespace and gets the string.
      if (is.eof()) {
        state_ = kEof;
        return;
      }
      if (is.fail()) {  // This shouldn't really happen, barring file-system
                        // errors.
        KALDI_WARN << "Error reading archive "
                   << PrintableRxfilename(archive_rxfilename_);
        state_ = kError;
        return;
      }
      int c;
      if ((c = is.peek()) != ' ' && c != '\t' && c != '
  ') {  // We expect a
                                                       // space ' ' after the key.
        // We also allow tab [which is consumed] and newline [which is not], just
        // so we can read archives generated by scripts that may not be fully
        // aware of how this format works.
        KALDI_WARN << "Invalid archive file format: expected space after key "
                   << key_ << ", got character "
                   << CharToString(static_cast<char>(is.peek())) << ", reading "
                   << PrintableRxfilename(archive_rxfilename_);
        state_ = kError;
        return;
      }
      if (c != '
  ') is.get();  // Consume the space or tab.
      if (holder_.Read(is)) {
        state_ = kHaveObject;
        return;
      } else {
        KALDI_WARN << "Object read failed, reading archive "
                   << PrintableRxfilename(archive_rxfilename_);
        state_ = kError;
        return;
      }
    }
  
    virtual bool IsOpen() const {
      switch (state_) {
        case kEof: case kError: case kHaveObject: case kFreedObject: return true;
        case kUninitialized: return false;
        default: KALDI_ERR << "IsOpen() called on invalid object.";  // kFileStart
          // is not valid state for user to call something on.
          return false;
      }
    }
  
    virtual bool Done() const {
      switch (state_) {
        case kHaveObject:
          return false;
        case kEof: case kError:
          return true;  // Error-state counts as Done(), but destructor
          // will fail (unless you check the status with Close()).
        default:
          KALDI_ERR << "Done() called on TableReader object at the wrong time.";
          return false;
      }
    }
  
    virtual std::string Key() {
      // Valid to call this whenever Done() returns false
      switch (state_) {
        case kHaveObject: break;  // only valid case.
        default:
          // coding error.
          KALDI_ERR << "Key() called on TableReader object at the wrong time.";
      }
      return key_;
    }
  
    T &Value() {
      switch (state_) {
        case kHaveObject:
          break;  // only valid case.
        default:
          // coding error.
          KALDI_ERR << "Value() called on TableReader object at the wrong time.";
      }
      return holder_.Value();
    }
  
    virtual void FreeCurrent() {
      if (state_ == kHaveObject) {
        holder_.Clear();
        state_ = kFreedObject;
      } else {
        KALDI_WARN << "FreeCurrent called at the wrong time.";
      }
    }
  
    void SwapHolder(Holder *other_holder) {
      // call Value() to ensure we have a value, and ignore its return value while
      // suppressing compiler warnings by casting to void.
      (void) Value();
      if (state_ == kHaveObject) {
        holder_.Swap(other_holder);
        state_ = kFreedObject;
      } else {
        KALDI_ERR << "SwapHolder called at the wrong time "
                     "(error related to ',bg' modifier).";
      }
    }
  
    virtual bool Close() {
      // To clean up, Close() also closes the Input object if
      // it's open.  It will succeed if the stream was not in an error state,
      // and the Input object isn't in an error state  we've found eof in the archive.
      if (!this->IsOpen())
        KALDI_ERR << "Close() called on TableReader twice or otherwise wrongly.";
      int32 status = 0;
      if (input_.IsOpen())
        status = input_.Close();
      if (state_ == kHaveObject)
        holder_.Clear();
      StateType old_state = state_;
      state_ = kUninitialized;
      if (old_state == kError || (old_state == kEof && status != 0)) {
        if (opts_.permissive) {
          KALDI_WARN << "Error detected closing TableReader for archive "
                     << PrintableRxfilename(archive_rxfilename_)
                     << " but ignoring "
                     << "it as permissive mode specified.";
          return true;
        } else {
          return false;
        }
      } else {
        return true;
      }
    }
  
    virtual ~SequentialTableReaderArchiveImpl() {
      if (this->IsOpen() && !Close())
        KALDI_ERR << "TableReader: error detected closing archive "
                  << PrintableRxfilename(archive_rxfilename_);
    }
   private:
    Input input_;  // Input object for the archive
    Holder holder_;     // Holds the object.
    std::string key_;
    std::string rspecifier_;
    std::string archive_rxfilename_;
    RspecifierOptions opts_;
    enum StateType {  //  [The state of the reading process]        [does holder_ [is input_
      //                                                     have object]   open]
      kUninitialized,  // Uninitialized or closed.                  no         no
      kFileStart,      // [state we use internally: just opened.]   no         yes
      kEof,     // We did Next() and found eof in archive           no         no
      kError,   // Some other error                                 no         no
      kHaveObject,  // We read the key and the object after it.     yes        yes
      kFreedObject,  // The user called FreeCurrent().              no         yes
    } state_;
  };
  
  // this is for when someone adds the 'th' modifier; it wraps around the basic
  // implementation and allows it to do the reading in a background thread.
  template<class Holder>
  class SequentialTableReaderBackgroundImpl:
        public SequentialTableReaderImplBase<Holder> {
   public:
    typedef typename Holder::T T;
  
    SequentialTableReaderBackgroundImpl(
        SequentialTableReaderImplBase<Holder> *base_reader):
        base_reader_(base_reader) {}
  
    // This function ignores the rxfilename argument.
    // We use the same function signature as the regular Open(),
    // for convenience.
    virtual bool Open(const std::string &rxfilename) {
      KALDI_ASSERT(base_reader_ != NULL &&
                   base_reader_->IsOpen());  // or code error.
      {
        thread_ = std::thread(SequentialTableReaderBackgroundImpl<Holder>::run,
                              this);
      }
  
      if (!base_reader_->Done())
        Next();
      return true;
    }
  
    virtual bool IsOpen() const {
      // Close() sets base_reader_ to NULL, and we never initialize this object
      // with a non-open base_reader_, so no need to check if it's open.
      return base_reader_ != NULL;
    }
  
    void RunInBackground() {
      try {
        // This function is called in the background thread.  The whole point of
        // the background thread is that we don't want to do the actual reading
        // (inside Next()) in the foreground.
        while (base_reader_ != NULL && !base_reader_->Done()) {
          consumer_sem_.Signal();
          // Here is where the consumer process (parent thread) gets to do its
          // stuff.  Principally it calls SwapHolder()-- a shallow swap that is
          // cheap.
          producer_sem_.Wait();
          // we check that base_reader_ is not NULL in case Close() was
          // called in the main thread.
          if (base_reader_ != NULL)
            base_reader_->Next();   //  here is where the work happens.
        }
        // this signal will be waited on in the Next() function of the foreground
        // thread if it is still running, or Close() otherwise.
        consumer_sem_.Signal();
        // this signal may be waited on in Close().
        consumer_sem_.Signal();
      } catch (...) {
        // There is nothing we called above that could potentially throw due to
        // user data.  So we treat reaching this point as a code-error condition.
        // Closing base_reader_ will trigger an exception in Next() in the main
        // thread when it checks that base_reader_->IsOpen().
        if (base_reader_->IsOpen()) {
          base_reader_->Close();
          delete base_reader_;
          base_reader_ = NULL;
        }
        consumer_sem_.Signal();
        return;
      }
    }
    static void run(SequentialTableReaderBackgroundImpl<Holder> *object) {
      object->RunInBackground();
    }
    virtual bool Done() const {
      return key_.empty();
    }
    virtual std::string Key() {
      if (key_.empty())
        KALDI_ERR << "Calling Key() at the wrong time.";
      return key_;
    }
    virtual T &Value() {
      if (key_.empty())
        KALDI_ERR << "Calling Value() at the wrong time.";
      return holder_.Value();
    }
    void SwapHolder(Holder *other_holder) {
      KALDI_ERR << "SwapHolder() should not be called on this class.";
    }
    virtual void FreeCurrent() {
      if (key_.empty())
        KALDI_ERR << "Calling FreeCurrent() at the wrong time.";
      // note: ideally a call to Value() should crash if you have just called
      // FreeCurrent().  For typical holders such as KaldiObjectHolder this will
      // happen inside the holder_.Value() call.  This won't be the case for all
      // holders, but it's not a great loss (just a missed opportunity to spot a
      // code error).
      holder_.Clear();
    }
    virtual void Next() {
      consumer_sem_.Wait();
      if (base_reader_ == NULL || !base_reader_->IsOpen())
        KALDI_ERR << "Error detected (likely code error) in background "
                  << "reader (',bg' option)";
      if (base_reader_->Done()) {
        // there is nothing else to read.
        key_ = "";
      } else {
        key_ = base_reader_->Key();
        base_reader_->SwapHolder(&holder_);
      }
      // this Signal() tells the producer thread, in the background,
      // that it's now safe to read the next value.
      producer_sem_.Signal();
    }
  
    // note: we can be sure that Close() won't be called twice, as the TableReader
    // object will delete this object after calling Close.
    virtual bool Close() {
      KALDI_ASSERT(base_reader_ != NULL && thread_.joinable());
      // wait until the producer thread is idle.
      consumer_sem_.Wait();
      bool ans = true;
      try {
        ans = base_reader_->Close();
      } catch (...) {
        ans = false;
      }
      delete base_reader_;
      // setting base_reader_ to NULL will cause the loop in the producer thread
      // to exit.
      base_reader_ = NULL;
      producer_sem_.Signal();
  
      thread_.join();
      return ans;
    }
    ~SequentialTableReaderBackgroundImpl() {
      if (base_reader_) {
        if (!Close()) {
          KALDI_ERR << "Error detected closing background reader "
                    << "(relates to ',bg' modifier)";
        }
      }
    }
   private:
    std::string key_;
    Holder holder_;
    // I couldn't figure out what to call these semaphores.  consumer_sem_ is the
    // one that the consumer (main thread) waits on; producer_sem_ is the one
    // that the producer (background thread) waits on.
    Semaphore consumer_sem_;
    Semaphore producer_sem_;
    std::thread thread_;
    SequentialTableReaderImplBase<Holder> *base_reader_;
  
  };
  
  template<class Holder>
  SequentialTableReader<Holder>::SequentialTableReader(const std::string
                                                       &rspecifier): impl_(NULL) {
    if (rspecifier != "" && !Open(rspecifier))
      KALDI_ERR << "Error constructing TableReader: rspecifier is " << rspecifier;
  }
  
  template<class Holder>
  bool SequentialTableReader<Holder>::Open(const std::string &rspecifier) {
    if (IsOpen())
      if (!Close())
        KALDI_ERR << "Could not close previously open object.";
    // now impl_ will be NULL.
  
    RspecifierOptions opts;
    RspecifierType wt = ClassifyRspecifier(rspecifier, NULL, &opts);
    switch (wt) {
      case kArchiveRspecifier:
        impl_ = new SequentialTableReaderArchiveImpl<Holder>();
        break;
      case kScriptRspecifier:
        impl_ = new SequentialTableReaderScriptImpl<Holder>();
        break;
      case kNoRspecifier: default:
        KALDI_WARN << "Invalid rspecifier " << rspecifier;
        return false;
    }
    if (!impl_->Open(rspecifier)) {
      delete impl_;
      impl_ = NULL;
      return false;  // sub-object will have printed warnings.
    }
    if (opts.background) {
      impl_ = new SequentialTableReaderBackgroundImpl<Holder>(
          impl_);
      if (!impl_->Open("")) {
        // the rxfilename is ignored in that Open() call.
        // It should only return false on code error.
        return false;
      }
    }
    return true;
  }
  
  template<class Holder>
  bool SequentialTableReader<Holder>::Close() {
    CheckImpl();
    bool ans = impl_->Close();
    delete impl_;  // We don't keep around empty impl_ objects.
    impl_ = NULL;
    return ans;
  }
  
  
  template<class Holder>
  bool SequentialTableReader<Holder>::IsOpen() const {
    return (impl_ != NULL);  // Because we delete the object whenever
    // that object is not open.  Thus, the IsOpen functions of the
    // Impl objects are not really needed.
  }
  
  template<class Holder>
  std::string SequentialTableReader<Holder>::Key() {
    CheckImpl();
    return impl_->Key();  // this call may throw if called wrongly in other ways,
    // e.g. eof.
  }
  
  
  template<class Holder>
  void SequentialTableReader<Holder>::FreeCurrent() {
    CheckImpl();
    impl_->FreeCurrent();
  }
  
  
  template<class Holder>
  typename SequentialTableReader<Holder>::T &
  SequentialTableReader<Holder>::Value() {
    CheckImpl();
    return impl_->Value();  // This may throw (if EnsureObjectLoaded() returned false you
                            // are safe.).
  }
  
  
  template<class Holder>
  void SequentialTableReader<Holder>::Next() {
    CheckImpl();
    impl_->Next();
  }
  
  template<class Holder>
  bool SequentialTableReader<Holder>::Done() {
    CheckImpl();
    return impl_->Done();
  }
  
  
  template<class Holder>
  SequentialTableReader<Holder>::~SequentialTableReader() {
    delete impl_;
    // Destructor of impl_ may throw.
  }
  
  
  
  template<class Holder> class TableWriterImplBase {
   public:
    typedef typename Holder::T T;
  
    virtual bool Open(const std::string &wspecifier) = 0;
  
    // Write returns true on success, false on failure, but
    // some errors may not be detected until we call Close().
    // It throws (via KALDI_ERR) if called wrongly.  We could
    // have just thrown on all errors, since this is what
    // TableWriter does; it was designed this way because originally
    // TableWriter::Write returned an exit status.
    virtual bool Write(const std::string &key, const T &value) = 0;
  
    // Flush will flush any archive; it does not return error status,
    //  any errors will be reported on the next Write or Close.
    virtual void Flush() = 0;
  
    virtual bool Close() = 0;
  
    virtual bool IsOpen() const = 0;
  
    // May throw on write error if Close was not called.
    virtual ~TableWriterImplBase() { }
  
    TableWriterImplBase() { }
   private:
    KALDI_DISALLOW_COPY_AND_ASSIGN(TableWriterImplBase);
  };
  
  
  // The implementation of TableWriter we use when writing directly
  // to an archive with no associated scp.
  template<class Holder>
  class TableWriterArchiveImpl: public TableWriterImplBase<Holder> {
   public:
    typedef typename Holder::T T;
  
    virtual bool Open(const std::string &wspecifier) {
      switch (state_) {
        case kUninitialized:
          break;
        case kWriteError:
          KALDI_ERR << "Opening stream, already open with write error.";
        case kOpen: default:
          if (!Close())  // throw because this error may not have been previously
            // detected by the user.
            KALDI_ERR << "Opening stream, error closing previously open stream.";
      }
      wspecifier_ = wspecifier;
      WspecifierType ws = ClassifyWspecifier(wspecifier,
                                             &archive_wxfilename_,
                                             NULL,
                                             &opts_);
      KALDI_ASSERT(ws == kArchiveWspecifier);  // or wrongly called.
  
      if (output_.Open(archive_wxfilename_, opts_.binary, false)) {  // false
                                                        // means no binary header.
        state_ = kOpen;
        return true;
      } else {
        // stream will not be open.  User will report this error
        // (we return bool), so don't bother printing anything.
        state_ = kUninitialized;
        return false;
      }
    }
  
    virtual bool IsOpen() const {
      switch (state_) {
        case kUninitialized: return false;
        case kOpen: case kWriteError: return true;
        default: KALDI_ERR << "IsOpen() called on TableWriter in invalid state.";
      }
      return false;
    }
  
    // Write returns true on success, false on failure, but
    // some errors may not be detected till we call Close().
    virtual bool Write(const std::string &key, const T &value) {
      switch (state_) {
        case kOpen: break;
        case kWriteError:
          // user should have known from the last
          // call to Write that there was a problem.
          KALDI_WARN << "Attempting to write to invalid stream.";
          return false;
        case kUninitialized: default:
          KALDI_ERR << "Write called on invalid stream";
      }
      // state is now kOpen or kWriteError.
      if (!IsToken(key))  // e.g. empty string or has spaces...
        KALDI_ERR << "Using invalid key " << key;
      output_.Stream() << key << ' ';
      if (!Holder::Write(output_.Stream(), opts_.binary, value)) {
        KALDI_WARN << "Write failure to "
                   << PrintableWxfilename(archive_wxfilename_);
        state_ = kWriteError;
        return false;
      }
      if (state_ == kWriteError) return false;  // Even if this Write seems to
      // have succeeded, we fail because a previous Write failed and the archive
      // may be corrupted and unreadable.
  
      if (opts_.flush)
        Flush();
      return true;
    }
  
    // Flush will flush any archive; it does not return error status,
    //  any errors will be reported on the next Write or Close.
    virtual void Flush() {
      switch (state_) {
        case kWriteError: case kOpen:
          output_.Stream().flush();  // Don't check error status.
          return;
        default:
          KALDI_WARN << "Flush called on not-open writer.";
      }
    }
  
    virtual bool Close() {
      if (!this->IsOpen() || !output_.IsOpen())
        KALDI_ERR << "Close called on a stream that was not open."
                  << this->IsOpen() << ", " << output_.IsOpen();
      bool close_success = output_.Close();
      if (!close_success) {
        KALDI_WARN << "Error closing stream: wspecifier is " << wspecifier_;
        state_ = kUninitialized;
        return false;
      }
      if (state_ == kWriteError) {
        KALDI_WARN << "Closing writer in error state: wspecifier is "
                   << wspecifier_;
        state_ = kUninitialized;
        return false;
      }
      state_ = kUninitialized;
      return true;
    }
  
    TableWriterArchiveImpl(): state_(kUninitialized) {}
  
    // May throw on write error if Close was not called.
    virtual ~TableWriterArchiveImpl() {
      if (!IsOpen()) return;
      else if (!Close())
        KALDI_ERR << "At TableWriter destructor: Write failed or stream close "
                  << "failed: wspecifier is "<<  wspecifier_;
    }
  
   private:
    Output output_;
    WspecifierOptions opts_;
    std::string wspecifier_;
    std::string archive_wxfilename_;
    enum {               // is stream open?
      kUninitialized,    // no
      kOpen,             // yes
      kWriteError,       // yes
    } state_;
  };
  
  
  
  
  // The implementation of TableWriter we use when writing to
  // individual files (more generally, wxfilenames) specified
  // in an scp file that we read.
  
  // Note: the code for this class is similar to
  // RandomAccessTableReaderScriptImpl; try to keep them in sync.
  
  template<class Holder>
  class TableWriterScriptImpl: public TableWriterImplBase<Holder> {
   public:
    typedef typename Holder::T T;
  
    TableWriterScriptImpl(): last_found_(0), state_(kUninitialized) {}
  
    virtual bool Open(const std::string &wspecifier) {
      switch (state_) {
        case kReadScript:
          KALDI_ERR << " Opening already open TableWriter: call Close first.";
        case kUninitialized: case kNotReadScript:
          break;
      }
      wspecifier_ = wspecifier;
      WspecifierType ws = ClassifyWspecifier(wspecifier,
                                             NULL,
                                             &script_rxfilename_,
                                             &opts_);
      KALDI_ASSERT(ws == kScriptWspecifier);  // or wrongly called.
      KALDI_ASSERT(script_.empty());  // no way it could be nonempty at this point.
  
      if (!ReadScriptFile(script_rxfilename_,
                           true,  // print any warnings
                           &script_)) {  // error reading script file or invalid
                                         // format
        state_ = kNotReadScript;
        return false;  // no need to print further warnings.  user gets the error.
      }
      std::sort(script_.begin(), script_.end());
      for (size_t i = 0; i+1 < script_.size(); i++) {
        if (script_[i].first.compare(script_[i+1].first) >= 0) {
          // script[i] not < script[i+1] in lexical order...
          KALDI_WARN << "Script file " << PrintableRxfilename(script_rxfilename_)
                     << " contains duplicate key " << script_[i].first;
          state_ = kNotReadScript;
          return false;
        }
      }
      state_ = kReadScript;
      return true;
    }
  
    virtual bool IsOpen() const {  return (state_ == kReadScript);  }
  
    virtual bool Close() {
      if (!IsOpen())
        KALDI_ERR << "Close() called on TableWriter that was not open.";
      state_ = kUninitialized;
      last_found_ = 0;
      script_.clear();
      return true;
    }
  
    // Write returns true on success, false on failure, but
    // some errors may not be detected till we call Close().
    virtual bool Write(const std::string &key, const T &value) {
      if (!IsOpen())
        KALDI_ERR << "Write called on invalid stream";
  
      if (!IsToken(key))  // e.g. empty string or has spaces...
        KALDI_ERR << "Using invalid key " << key;
  
      std::string wxfilename;
      if (!LookupFilename(key, &wxfilename)) {
        if (opts_.permissive) {
          return true;  // In permissive mode, it's as if we're writing to
                       // /dev/null for missing keys.
        } else {
          KALDI_WARN << "Script file "
                     << PrintableRxfilename(script_rxfilename_)
                     << " has no entry for key " <<key;
          return false;
        }
      }
      Output output;
      if (!output.Open(wxfilename, opts_.binary, false)) {
        // Open in the text/binary mode (on Windows) given by member var. "binary"
        // (obtained from wspecifier), but do not put the binary-mode header (it
        // will be written, if needed, by the Holder::Write function.)
        KALDI_WARN << "Failed to open stream: "
                   << PrintableWxfilename(wxfilename);
        return false;
      }
      if (!Holder::Write(output.Stream(), opts_.binary, value)
          || !output.Close()) {
        KALDI_WARN << "Failed to write data to "
                   << PrintableWxfilename(wxfilename);
  
        return false;
      }
      return true;
    }
  
    // Flush does nothing in this implementation, there is nothing to flush.
    virtual void Flush() { }
  
  
    virtual ~TableWriterScriptImpl() {
      // Nothing to do in destructor.
    }
  
   private:
    // Note: this function is almost the same as in
    // RandomAccessTableReaderScriptImpl.
    bool LookupFilename(const std::string &key, std::string *wxfilename) {
      // First, an optimization: if we're going consecutively, this will
      // make the lookup very fast.
      last_found_++;
      if (last_found_ < script_.size() && script_[last_found_].first == key) {
        *wxfilename = script_[last_found_].second;
        return true;
      }
      std::pair<std::string, std::string> pr(key, "");  // Important that ""
      // compares less than or equal to any string, so lower_bound points to the
      // element that has the same key.
      typedef typename std::vector<std::pair<std::string, std::string> >
                       ::const_iterator IterType;
      IterType iter = std::lower_bound(script_.begin(), script_.end(), pr);
      if (iter != script_.end() && iter->first == key) {
        last_found_ = iter - script_.begin();
        *wxfilename = iter->second;
        return true;
      } else {
        return false;
      }
    }
  
  
    WspecifierOptions opts_;
    std::string wspecifier_;
    std::string script_rxfilename_;
  
    // the script_ variable contains pairs of (key, filename), sorted using
    // std::sort.  This can be used with binary_search to look up filenames for
    // writing.  If this becomes inefficient we can use std::unordered_map (but I
    // suspect this wouldn't be significantly faster & would use more memory).
    // If memory becomes a problem here, the user should probably be passing
    // only the relevant part of the scp file rather than expecting us to get too
    // clever in the code.
    std::vector<std::pair<std::string, std::string> > script_;
    size_t last_found_;  // This is for an optimization used in LookupFilename.
  
    enum {
      kUninitialized,
      kReadScript,
      kNotReadScript,  // read of script failed.
    } state_;
  };
  
  
  // The implementation of TableWriter we use when writing directly
  // to an archive plus an associated scp.
  template<class Holder>
  class TableWriterBothImpl: public TableWriterImplBase<Holder> {
   public:
    typedef typename Holder::T T;
  
    virtual bool Open(const std::string &wspecifier) {
      switch (state_) {
        case kUninitialized:
          break;
        case kWriteError:
          KALDI_ERR << "Opening stream, already open with write error.";
        case kOpen: default:
          if (!Close())  // throw because this error may not have been previously
                         // detected by user.
            KALDI_ERR << "Opening stream, error closing previously open stream.";
      }
      wspecifier_ = wspecifier;
      WspecifierType ws = ClassifyWspecifier(wspecifier,
                                             &archive_wxfilename_,
                                             &script_wxfilename_,
                                             &opts_);
      KALDI_ASSERT(ws == kBothWspecifier);  // or wrongly called.
      if (ClassifyWxfilename(archive_wxfilename_) != kFileOutput)
        KALDI_WARN << "When writing to both archive and script, the script file "
            "will generally not be interpreted correctly unless the archive is "
            "an actual file: wspecifier = " << wspecifier;
  
      if (!archive_output_.Open(archive_wxfilename_, opts_.binary, false)) {
        // false means no binary header.
        state_ = kUninitialized;
        return false;
      }
      if (!script_output_.Open(script_wxfilename_, false, false)) {  // first
        // false means text mode: script files always text-mode.   second false
        //  means don't write header (doesn't matter for text mode).
        archive_output_.Close();  // Don't care about status: error anyway.
        state_ = kUninitialized;
        return false;
      }
      state_ = kOpen;
      return true;
    }
  
    virtual bool IsOpen() const {
      switch (state_) {
        case kUninitialized: return false;
        case kOpen: case kWriteError: return true;
        default: KALDI_ERR << "IsOpen() called on TableWriter in invalid state.";
      }
      return false;
    }
  
    void MakeFilename(typename std::ostream::pos_type streampos,
                      std::string *output) const {
      std::ostringstream ss;
      ss << ':' << streampos;
      KALDI_ASSERT(ss.str() != ":-1");
      *output = archive_wxfilename_ + ss.str();
  
      // e.g. /some/file:12302.
      // Note that we warned if archive_wxfilename_ is not an actual filename;
      // the philosophy is we give the user rope and if they want to hang
      // themselves, with it, fine.
    }
  
    // Write returns true on success, false on failure, but
    // some errors may not be detected till we call Close().
    virtual bool Write(const std::string &key, const T &value) {
      switch (state_) {
        case kOpen: break;
        case kWriteError:
          // user should have known from the last
          // call to Write that there was a problem.  Warn about it.
          KALDI_WARN << "Writing to non-open TableWriter object.";
          return false;
        case kUninitialized: default:
          KALDI_ERR << "Write called on invalid stream";
      }
      // state is now kOpen or kWriteError.
      if (!IsToken(key))  // e.g. empty string or has spaces...
        KALDI_ERR << "Using invalid key " << key;
      std::ostream &archive_os = archive_output_.Stream();
      archive_os << key << ' ';
      typename std::ostream::pos_type archive_os_pos = archive_os.tellp();
      // position at start of Write() to archive.  We will record this in the
      // script file.
      std::string offset_rxfilename;  // rxfilename with offset into the archive,
      // e.g. some_archive_name.ark:431541423
      MakeFilename(archive_os_pos, &offset_rxfilename);
  
      // Write to the script file first.
      // The idea is that we want to get all the information possible into the
      // script file, to make it easier to unwind errors later.
      std::ostream &script_os = script_output_.Stream();
      script_output_.Stream() << key << ' ' << offset_rxfilename << '
  ';
  
      if (!Holder::Write(archive_output_.Stream(), opts_.binary, value)) {
        KALDI_WARN << "Write failure to"
                   << PrintableWxfilename(archive_wxfilename_);
        state_ = kWriteError;
        return false;
      }
  
      if (script_os.fail()) {
        KALDI_WARN << "Write failure to script file detected: "
                   << PrintableWxfilename(script_wxfilename_);
        state_ = kWriteError;
        return false;
      }
  
      if (archive_os.fail()) {
        KALDI_WARN << "Write failure to archive file detected: "
                   << PrintableWxfilename(archive_wxfilename_);
        state_ = kWriteError;
        return false;
      }
  
      if (state_ == kWriteError) return false;  // Even if this Write seems to
      // have succeeded, we fail because a previous Write failed and the archive
      // may be corrupted and unreadable.
  
      if (opts_.flush)
        Flush();
      return true;
    }
  
    // Flush will flush any archive; it does not return error status,
    //  any errors will be reported on the next Write or Close.
    virtual void Flush() {
      switch (state_) {
        case kWriteError: case kOpen:
          archive_output_.Stream().flush();  // Don't check error status.
          script_output_.Stream().flush();  // Don't check error status.
          return;
        default:
          KALDI_WARN << "Flush called on not-open writer.";
      }
    }
  
    virtual bool Close() {
      if (!this->IsOpen())
        KALDI_ERR << "Close called on a stream that was not open.";
      bool close_success = true;
      if (archive_output_.IsOpen())
        if (!archive_output_.Close()) close_success = false;
      if (script_output_.IsOpen())
        if (!script_output_.Close()) close_success = false;
      bool ans = close_success && (state_ != kWriteError);
      state_ = kUninitialized;
      return ans;
    }
  
    TableWriterBothImpl(): state_(kUninitialized) {}
  
    // May throw on write error if Close() was not called.
    // User can get the error status by calling Close().
    virtual ~TableWriterBothImpl() {
      if (!IsOpen()) return;
      else if (!Close())
        KALDI_ERR << "Write failed or stream close failed: "
                  << wspecifier_;
    }
  
   private:
    Output archive_output_;
    Output script_output_;
    WspecifierOptions opts_;
    std::string archive_wxfilename_;
    std::string script_wxfilename_;
    std::string wspecifier_;
    enum {               // is stream open?
      kUninitialized,    // no
      kOpen,             // yes
      kWriteError,       // yes
    } state_;
  };
  
  
  template<class Holder>
  TableWriter<Holder>::TableWriter(const std::string &wspecifier): impl_(NULL) {
    if (wspecifier != "" && !Open(wspecifier))
      KALDI_ERR << "Failed to open table for writing with wspecifier: " << wspecifier
                << ": errno (in case it's relevant) is: " << strerror(errno);
  }
  
  template<class Holder>
  bool TableWriter<Holder>::IsOpen() const {
    return (impl_ != NULL);
  }
  
  
  template<class Holder>
  bool TableWriter<Holder>::Open(const std::string &wspecifier) {
    if (IsOpen()) {
      if (!Close())  // call Close() yourself to suppress this exception.
        KALDI_ERR << "Failed to close previously open writer.";
    }
    KALDI_ASSERT(impl_ == NULL);
    WspecifierType wtype = ClassifyWspecifier(wspecifier, NULL, NULL, NULL);
    switch (wtype) {
      case kBothWspecifier:
        impl_ = new TableWriterBothImpl<Holder>();
        break;
      case kArchiveWspecifier:
        impl_ = new TableWriterArchiveImpl<Holder>();
        break;
      case kScriptWspecifier:
        impl_ = new TableWriterScriptImpl<Holder>();
        break;
      case kNoWspecifier: default:
        KALDI_WARN << "ClassifyWspecifier: invalid wspecifier " << wspecifier;
        return false;
    }
    if (impl_->Open(wspecifier)) {
      return true;
    } else {  // The class will have printed a more specific warning.
      delete impl_;
      impl_ = NULL;
      return false;
    }
  }
  
  template<class Holder>
  void TableWriter<Holder>::Write(const std::string &key,
                                  const T &value) const {
    CheckImpl();
    if (!impl_->Write(key, value))
      KALDI_ERR << "Error in TableWriter::Write";
    // More specific warning will have
    // been printed in the Write function.
  }
  
  template<class Holder>
  void TableWriter<Holder>::Flush() {
    CheckImpl();
    impl_->Flush();
  }
  
  template<class Holder>
  bool TableWriter<Holder>::Close() {
    CheckImpl();
    bool ans = impl_->Close();
    delete impl_;  // We don't keep around non-open impl_ objects
                   // [c.f. definition of IsOpen()]
    impl_ = NULL;
    return ans;
  }
  
  template<class Holder>
  TableWriter<Holder>::~TableWriter() {
    if (IsOpen() && !Close()) {
      KALDI_ERR << "Error closing TableWriter [in destructor].";
    }
  }
  
  
  // Types of RandomAccessTableReader:
  // In principle, we would like to have four types of RandomAccessTableReader:
  //  the 4 combinations  [scp, archive], [seekable, not-seekable],
  // where if something is seekable we only store a file offset.  However,
  // it seems sufficient for now to only implement two of these, in both
  // cases assuming it's not seekable so we never store file offsets and always
  // store either the scp line or the data in the archive.  The reasons are:
  // (1)
  // For scp files, storing the actual entry is not that much more expensive
  // than storing the file offsets (since the entries are just filenames), and
  // avoids a lot of fseek operations that might be expensive.
  // (2)
  // For archive files, there is no real reason, if you have the archive file
  // on disk somewhere, why you wouldn't access it via its associated scp.
  // [i.e. write it as ark, scp].  The main reason to read archives directly
  // is if they are part of a pipe, and in this case it's not seekable, so
  // we implement only this case.
  //
  // Note that we will rarely in practice have to keep in memory everything in
  // the archive, as long as things are only read once from the archive (the
  // "o, " or "once" option) and as long as we keep our keys in sorted order;
  // to take advantage of this we need the "s, " (sorted) option, so we would
  // read archives as e.g. "s, o, ark:-" (this is the rspecifier we would use if
  // it was the standard input and these conditions held).
  
  template<class Holder> class RandomAccessTableReaderImplBase {
   public:
    typedef typename Holder::T T;
  
    virtual bool Open(const std::string &rspecifier) = 0;
  
    virtual bool HasKey(const std::string &key) = 0;
  
    virtual const T &Value(const std::string &key) = 0;
  
    virtual bool Close() = 0;
  
    virtual ~RandomAccessTableReaderImplBase() {}
  };
  
  
  // Implementation of RandomAccessTableReader for a script file; for simplicity
  // we just read it in all in one go, as it's unlikely someone would generate
  // this from a pipe.  In principle we could read it on-demand as for the
  // archives, but this would probably be overkill.
  
  // Note: the code for this this class is similar to TableWriterScriptImpl:
  // try to keep them in sync.
  template<class Holder>
  class RandomAccessTableReaderScriptImpl:
        public RandomAccessTableReaderImplBase<Holder> {
   public:
    typedef typename Holder::T T;
  
    RandomAccessTableReaderScriptImpl(): last_found_(0), state_(kUninitialized) {}
  
    virtual bool Open(const std::string &rspecifier) {
      switch (state_) {
        case kNotHaveObject: case kHaveObject: case kHaveRange:
          KALDI_ERR << " Opening already open RandomAccessTableReader:"
                       " call Close first.";
        case kUninitialized: case kNotReadScript:
          break;
      }
      rspecifier_ = rspecifier;
      RspecifierType rs = ClassifyRspecifier(rspecifier,
                                             &script_rxfilename_,
                                             &opts_);
      KALDI_ASSERT(rs == kScriptRspecifier);  // or wrongly called.
      KALDI_ASSERT(script_.empty());  // no way it could be nonempty at this point
  
      if (!ReadScriptFile(script_rxfilename_,
                          true,  // print any warnings
                          &script_)) {  // error reading script file or invalid
                                        // format
        state_ = kNotReadScript;
        return false;  // no need to print further warnings.  user gets the error.
      }
  
      rspecifier_ = rspecifier;
      // If opts_.sorted, the user has asserted that the keys are already sorted.
      // Although we could easily sort them, we want to let the user know of this
      // mistake.  This same mistake could have serious effects if used with an
      // archive rather than a script.
      if (!opts_.sorted)
        std::sort(script_.begin(), script_.end());
      for (size_t i = 0; i + 1 < script_.size(); i++) {
        if (script_[i].first.compare(script_[i+1].first) >= 0) {
          // script[i] not < script[i+1] in lexical order...
          bool same = (script_[i].first == script_[i+1].first);
          KALDI_WARN << "Script file " << PrintableRxfilename(script_rxfilename_)
                     << (same ? " contains duplicate key: " :
                         " is not sorted (remove s, option or add ns, option):"
                         " key is ") << script_[i].first;
          state_ = kNotReadScript;
          return false;
        }
      }
      state_ = kNotHaveObject;
      key_ = "";  // make sure we don't have a key set
      return true;
    }
  
    virtual bool IsOpen() const {
      return  (state_ == kNotHaveObject || state_ == kHaveObject ||
               state_ == kHaveRange);
    }
  
    virtual bool Close() {
      if (!IsOpen())
        KALDI_ERR << "Close() called on RandomAccessTableReader that was not"
                     " open.";
      holder_.Clear();
      range_holder_.Clear();
      state_ = kUninitialized;
      last_found_ = 0;
      script_.clear();
      key_ = "";
      range_ = "";
      data_rxfilename_ = "";
      // This cannot fail because any errors of a "global" nature would have been
      // detected when we did Open().  With archives it's different.
      return true;
    }
  
    virtual bool HasKey(const std::string &key) {
      bool preload = opts_.permissive;
      // In permissive mode, we have to check that we can read
      // the scp entry before we assert that the key is there.
      return HasKeyInternal(key, preload);
    }
  
  
    // Write returns true on success, false on failure, but
    // some errors may not be detected till we call Close().
    virtual const T&  Value(const std::string &key) {
      if (!HasKeyInternal(key, true)) // true == preload.
        KALDI_ERR << "Could not get item for key " << key
                  << ", rspecifier is " << rspecifier_ << " [to ignore this, "
                   << "add the p, (permissive) option to the rspecifier.";
      KALDI_ASSERT(key_ == key);
      if (state_ == kHaveObject) {
        return holder_.Value();
      } else {
        KALDI_ASSERT(state_ == kHaveRange);
        return range_holder_.Value();
      }
    }
  
    virtual ~RandomAccessTableReaderScriptImpl() { }
  
   private:
  
    // HasKeyInternal when called with preload == false just tells us whether the
    // key is in the scp.  With preload == true, which happens when the ,p
    // (permissive) option is given in the rspecifier (or when called from
    // Value()), it will also check that we can preload the object from disk
    // (loading from the rxfilename in the scp), and only return true if we can.
    // This function is called both from HasKey and from Value().
    virtual bool HasKeyInternal(const std::string &key, bool preload) {
      switch (state_) {
        case kUninitialized: case kNotReadScript:
          KALDI_ERR << "HasKey called on RandomAccessTableReader object that is"
                       " not open.";
        case kHaveObject:
          if (key == key_ && range_.empty())
            return true;
          break;
        case kHaveRange:
          if (key == key_)
            return true;
          break;
        case kNotHaveObject: default: break;
      }
      KALDI_ASSERT(IsToken(key));
      size_t key_pos = 0;
      if (!LookupKey(key, &key_pos)) {
        return false;
      } else {
        if (!preload) {
          return true;  // we have the key, and were not asked to verify that the
                        // object could be read.
        } else {  // preload specified, so we have to attempt to pre-load the
                  // object before returning.
          std::string data_rxfilename, range; // We will split
          // script_[key_pos].second (e.g. "1.ark:100[0:2]" into data_rxfilename
          // (e.g. "1.ark:100") and range (if any), e.g. "0:2".
          if (script_[key_pos].second[script_[key_pos].second.size()-1] == ']') {
            if(!ExtractRangeSpecifier(script_[key_pos].second,
                                      &data_rxfilename,
                                      &range)) {
              KALDI_ERR << "TableReader: failed to parse range in '"
                        << script_[key_pos].second << "'";
            }
          } else {
            data_rxfilename = script_[key_pos].second;
          }
          if (state_ == kHaveRange) {
            if (data_rxfilename_ == data_rxfilename && range_ == range) {
              // the odd situation where two keys had the same rxfilename and range:
              // just change the key and keep the object.
              key_ = key;
              return true;
            } else {
              range_holder_.Clear();
              state_ = kHaveObject;
            }
          }
          // OK, at this point the state will be kHaveObject or kNotHaveObject.
          if (state_ == kHaveObject) {
            if (data_rxfilename_ != data_rxfilename) {
              // clear out the object.
              state_ = kNotHaveObject;
              holder_.Clear();
            }
          }
          // At this point we can safely switch to the new key, data_rxfilename
          // and range, and we know that if we have an object, it will already be
          // the correct one.  The state is now kHaveObject or kNotHaveObject.
          key_ = key;
          data_rxfilename_ = data_rxfilename;
          range_ = range;
          if (state_ == kNotHaveObject) {
            // we need to read the object.
            if (!input_.Open(data_rxfilename)) {
              KALDI_WARN << "Error opening stream "
                         << PrintableRxfilename(data_rxfilename);
              return false;
            } else {
              if (holder_.Read(input_.Stream())) {
                state_ = kHaveObject;
              } else {
                KALDI_WARN << "Error reading object from "
                    "stream " << PrintableRxfilename(data_rxfilename);
                return false;
              }
            }
          }
          // At this point the state is kHaveObject.
          if (range.empty())
            return true;  // we're done: no range was requested.
          if (range_holder_.ExtractRange(holder_, range)) {
            state_ = kHaveRange;
            return true;
          } else {
            KALDI_WARN  << "Failed to load object from "
                        << PrintableRxfilename(data_rxfilename)
                        << "[" << range << "]";
            // leave state at kHaveObject.
            return false;
          }
        }
      }
    }
  
    // This function attempts to look up the key "key" in the sorted array
    // script_.  If it was found it returns true and puts the array offset into
    // 'script_offset'; otherwise it returns false.
    bool LookupKey(const std::string &key, size_t *script_offset) {
      // First, an optimization: if we're going consecutively, this will
      // make the lookup very fast.  Since we may call HasKey and then
      // Value(), which both may look up the key, we test if either the
      // current or next position are correct.
      if (last_found_ < script_.size() && script_[last_found_].first == key) {
        *script_offset = last_found_;
        return true;
      }
      last_found_++;
      if (last_found_ < script_.size() && script_[last_found_].first == key) {
        *script_offset = last_found_;
        return true;
      }
      std::pair<std::string, std::string> pr(key, "");  // Important that ""
      // compares less than or equal to any string, so lower_bound points to the
      // element that has the same key.
      typedef typename std::vector<std::pair<std::string, std::string> >
                       ::const_iterator IterType;
      IterType iter = std::lower_bound(script_.begin(), script_.end(), pr);
      if (iter != script_.end() && iter->first == key) {
        last_found_ = *script_offset = iter - script_.begin();
        return true;
      } else {
        return false;
      }
    }
  
  
    Input input_;  // Use the same input_ object for reading each file, in case
                   // the scp specifies offsets in an archive so we can keep the
                   // same file open.
    RspecifierOptions opts_;
    std::string rspecifier_;  // rspecifier used to open this object; used in
                              // debug messages
    std::string script_rxfilename_;  // rxfilename of script file that we read.
  
    std::string key_;  // The current key of the object that we have, but see the
                       // notes regarding states_ for more explanation of the
                       // semantics.
  
    Holder holder_;
    Holder range_holder_; // Holds the partial object corresponding to the object
                          // range specifier 'range_'. this is only used when
                          // 'range_' is specified.
    std::string range_; // range within which we read the object from holder_.
                        // If key_ is set, always correspond to the key.
    std::string data_rxfilename_;  // the rxfilename corresponding to key_,
                                   // always set when key_ is set.
  
  
    // the script_ variable contains pairs of (key, filename), sorted using
    // std::sort.  This can be used with binary_search to look up filenames for
    // writing.  If this becomes inefficient we can use std::unordered_map (but I
    // suspect this wouldn't be significantly faster & would use more memory).
    // If memory becomes a problem here, the user should probably be passing
    // only the relevant part of the scp file rather than expecting us to get too
    // clever in the code.
    std::vector<std::pair<std::string, std::string> > script_;
    size_t last_found_;  // This is for an optimization used in FindFilename.
  
    enum {
      //                   (*) is script_ set up?
      //                          (*) does holder_ contain an object?
      //                               (*) does range_holder_ contain and object?
      //
      //
      kUninitialized,  //    no    no    no
      kNotReadScript,  //    no    no    no
      kNotHaveObject,  //    yes   no    no
      kHaveObject,     //    yes   yes   no
      kHaveRange,      //    yes   yes   yes
  
      // If we are in a state where holder_ contains an object, it always contains
      // the object from 'key_', and the corresponding rxfilename is always
      // 'data_rxfilename_'.  If range_holder_ contains an object, it always
      // corresponds to the range 'range_' of the object in 'holder_', and always
      // corresponds to the current key.
    } state_;
  };
  
  
  
  
  // This is the base-class (with some implemented functions) for the
  // implementations of RandomAccessTableReader when it's an archive.  This
  // base-class handles opening the files, storing the state of the reading
  // process, and loading objects.  This is the only case in which we have
  // an intermediate class in the hierarchy between the virtual ImplBase
  // class and the actual Impl classes.
  // The child classes vary in the assumptions regarding sorting, etc.
  
  template<class Holder>
  class RandomAccessTableReaderArchiveImplBase:
        public RandomAccessTableReaderImplBase<Holder> {
   public:
    typedef typename Holder::T T;
  
    RandomAccessTableReaderArchiveImplBase(): holder_(NULL),
                                              state_(kUninitialized) { }
  
    virtual bool Open(const std::string &rspecifier) {
      if (state_ != kUninitialized) {
        if (!this->Close())  // call Close() yourself to suppress this exception.
          KALDI_ERR << "Error closing previous input.";
      }
      rspecifier_ = rspecifier;
      RspecifierType rs = ClassifyRspecifier(rspecifier, &archive_rxfilename_,
                                             &opts_);
      KALDI_ASSERT(rs == kArchiveRspecifier);
  
      // NULL means don't expect binary-mode header
      bool ans;
      if (Holder::IsReadInBinary())
        ans = input_.Open(archive_rxfilename_, NULL);
      else
        ans = input_.OpenTextMode(archive_rxfilename_);
      if (!ans) {  // header.
        KALDI_WARN << "Failed to open stream "
                   << PrintableRxfilename(archive_rxfilename_);
        state_ = kUninitialized;  // Failure on Open
        return false;  // User should print the error message.
      } else {
        state_ = kNoObject;
      }
      return true;
    }
  
    // ReadNextObject() requires that the state be kNoObject,
    // and it will try read the next object.  If it succeeds,
    // it sets the state to kHaveObject, and
    // cur_key_ and holder_ have the key and value.  If it fails,
    // it sets the state to kError or kEof.
    void ReadNextObject() {
      if (state_ != kNoObject)
        KALDI_ERR << "ReadNextObject() called from wrong state.";
      // Code error somewhere in this class or a child class.
      std::istream &is = input_.Stream();
      is.clear();  // Clear any fail bits that may have been set... just in case
      // this happened in the Read function.
      is >> cur_key_;  // This eats up any leading whitespace and gets the string.
      if (is.eof()) {
        state_ = kEof;
        return;
      }
      if (is.fail()) {  // This shouldn't really happen, barring file-system
                        // errors.
        KALDI_WARN << "Error reading archive: rspecifier is " << rspecifier_;
        state_ = kError;
        return;
      }
      int c;
      if ((c = is.peek()) != ' ' && c != '\t' && c != '
  ') {  // We expect a
        // space ' ' after the key.
        // We also allow tab, just so we can read archives generated by scripts
        // that may not be fully aware of how this format works.
        KALDI_WARN << "Invalid archive file format: expected space after key "
                   <<cur_key_
                   <<", got character "
                   << CharToString(static_cast<char>(is.peek()))
                   << ", reading archive "
                   << PrintableRxfilename(archive_rxfilename_);
        state_ = kError;
        return;
      }
      if (c != '
  ') is.get();  // Consume the space or tab.
      holder_ = new Holder;
      if (holder_->Read(is)) {
        state_ = kHaveObject;
        return;
      } else {
        KALDI_WARN << "Object read failed, reading archive "
                   << PrintableRxfilename(archive_rxfilename_);
        state_ = kError;
        delete holder_;
        holder_ = NULL;
        return;
      }
    }
  
    virtual bool IsOpen() const {
      switch (state_) {
        case kEof: case kError: case kHaveObject: case kNoObject: return true;
        case kUninitialized: return false;
        default: KALDI_ERR << "IsOpen() called on invalid object.";
          return false;
      }
    }
  
    // Called by the child-class virutal Close() functions; does the
    // shared parts of the cleanup.
    bool CloseInternal() {
      if (!this->IsOpen())
        KALDI_ERR << "Close() called on TableReader twice or otherwise wrongly.";
      if (input_.IsOpen())
        input_.Close();
      if (state_ == kHaveObject) {
        KALDI_ASSERT(holder_ != NULL);
        delete holder_;
        holder_ = NULL;
      } else {
        KALDI_ASSERT(holder_ == NULL);
      }
      bool ans = (state_ != kError);
      state_ = kUninitialized;
      if (!ans && opts_.permissive) {
        KALDI_WARN << "Error state detected closing reader.  "
                   << "Ignoring it because you specified permissive mode.";
        return true;
      }
      return ans;
    }
  
    ~RandomAccessTableReaderArchiveImplBase() {
      // The child class has the responsibility to call CloseInternal().
      KALDI_ASSERT(state_ == kUninitialized && holder_ == NULL);
    }
   private:
    Input input_;       // Input object for the archive
   protected:
    // The variables below are accessed by child classes.
  
    std::string cur_key_;   // current key (if state == kHaveObject).
    Holder *holder_;    // Holds the object we just read (if state == kHaveObject)
  
    std::string rspecifier_;
    std::string archive_rxfilename_;
    RspecifierOptions opts_;
  
    enum {  //  [The state of the reading process]        [does holder_ [is input_
      //                                                      have object]   open]
      kUninitialized,  // Uninitialized or closed                   no         no
      kNoObject,      // Do not have object in holder_              no         yes
      kHaveObject,    // Have object in holder_                     yes        yes
      kEof,           // End of file                                no         yes
      kError,         // Some kind of error-state in the reading.   no         yes
    } state_;
  };
  
  
  // RandomAccessTableReaderDSortedArchiveImpl (DSorted for "doubly sorted") is
  // the implementation for random-access reading of archives when both the
  // archive, and the calling code, are in sorted order (i.e. we ask for the keys
  // in sorted order).  This is when the s and cs options are both given.  It only
  // ever has to keep one object in memory.  It inherits from
  // RandomAccessTableReaderArchiveImplBase which implements the common parts of
  // RandomAccessTableReader that are used when it's an archive we're reading from
  
  template<class Holder>
  class RandomAccessTableReaderDSortedArchiveImpl:
        public RandomAccessTableReaderArchiveImplBase<Holder> {
    using RandomAccessTableReaderArchiveImplBase<Holder>::kUninitialized;
    using RandomAccessTableReaderArchiveImplBase<Holder>::kHaveObject;
    using RandomAccessTableReaderArchiveImplBase<Holder>::kNoObject;
    using RandomAccessTableReaderArchiveImplBase<Holder>::kEof;
    using RandomAccessTableReaderArchiveImplBase<Holder>::kError;
    using RandomAccessTableReaderArchiveImplBase<Holder>::state_;
    using RandomAccessTableReaderArchiveImplBase<Holder>::opts_;
    using RandomAccessTableReaderArchiveImplBase<Holder>::cur_key_;
    using RandomAccessTableReaderArchiveImplBase<Holder>::holder_;
    using RandomAccessTableReaderArchiveImplBase<Holder>::rspecifier_;
    using RandomAccessTableReaderArchiveImplBase<Holder>::archive_rxfilename_;
    using RandomAccessTableReaderArchiveImplBase<Holder>::ReadNextObject;
   public:
    typedef typename Holder::T T;
  
    RandomAccessTableReaderDSortedArchiveImpl() { }
  
    virtual bool Close() {
      // We don't have anything additional to clean up, so just
      // call generic base-class one.
      return this->CloseInternal();
    }
  
    virtual bool HasKey(const std::string &key) {
      return FindKeyInternal(key);
    }
    virtual const T & Value(const std::string &key) {
      if (!FindKeyInternal(key)) {
        KALDI_ERR << "Value() called but no such key " << key
                  << " in archive " << PrintableRxfilename(archive_rxfilename_);
      }
      KALDI_ASSERT(this->state_ == kHaveObject && key == this->cur_key_
                   && holder_ != NULL);
      return this->holder_->Value();
    }
  
    virtual ~RandomAccessTableReaderDSortedArchiveImpl() {
      if (this->IsOpen())
        if (!Close())  // more specific warning will already have been printed.
          // we are in some kind of error state & user did not find out by
          // calling Close().
          KALDI_ERR << "Error closing RandomAccessTableReader: rspecifier is "
                    << rspecifier_;
    }
   private:
    // FindKeyInternal tries to find the key by calling "ReadNextObject()"
    // as many times as necessary till we get to it.  It is called from
    // both FindKey and Value().
    bool FindKeyInternal(const std::string &key) {
      // First check that the user is calling us right: should be
      // in sorted order.  If not, error.
      if (!last_requested_key_.empty()) {
        if (key.compare(last_requested_key_) < 0) {  // key < last_requested_key_
          KALDI_ERR << "You provided the \"cs\" option "
                    << "but are not calling with keys in sorted order: "
                    << key << " < " << last_requested_key_ << ": rspecifier is "
                    << rspecifier_;
        }
      }
      // last_requested_key_ is just for debugging of order of calling.
      last_requested_key_ = key;
  
      if (state_ == kNoObject)
        ReadNextObject();  // This can only happen
        // once, the first time someone calls HasKey() or Value().  We don't
        // do it in the initializer to stop the program hanging too soon,
        // if reading from a pipe.
  
      if (state_ == kEof || state_ == kError) return false;
  
      if (state_ == kUninitialized)
        KALDI_ERR << "Trying to access a RandomAccessTableReader object that is"
                     " not open.";
  
      std::string last_key_;  // To check that
      // the archive we're reading is in sorted order.
      while (1) {
        KALDI_ASSERT(state_ == kHaveObject);
        int compare = key.compare(cur_key_);
        if (compare == 0) {  // key == key_
          return true;  // we got it..
        } else if (compare < 0) {  // key < cur_key_, so we already read past the
          // place where we want to be.  This implies that we will never find it
          // [due to the sorting etc., this means it just isn't in the archive].
          return false;
        } else {  // compare > 0, key > cur_key_.  We need to read further ahead.
          last_key_ = cur_key_;
          // read next object.. we have to set state to kNoObject first.
          KALDI_ASSERT(holder_ != NULL);
          delete holder_;
          holder_ = NULL;
          state_ = kNoObject;
          ReadNextObject();
          if (state_ != kHaveObject)
            return false;  // eof or read error.
          if (cur_key_.compare(last_key_) <= 0) {
            KALDI_ERR << "You provided the \"s\" option "
                      << " (sorted order), but keys are out of order or"
                         " duplicated: "
                      << last_key_ << " is followed by " << cur_key_
                      << ": rspecifier is " << rspecifier_;
          }
        }
      }
    }
  
    /// Last string provided to HasKey() or Value();
    std::string last_requested_key_;
  };
  
  // RandomAccessTableReaderSortedArchiveImpl is for random-access reading of
  // archives when the user specified the sorted (s) option but not the
  // called-sorted (cs) options.
  template<class Holder>
  class RandomAccessTableReaderSortedArchiveImpl:
        public RandomAccessTableReaderArchiveImplBase<Holder> {
    using RandomAccessTableReaderArchiveImplBase<Holder>::kUninitialized;
    using RandomAccessTableReaderArchiveImplBase<Holder>::kHaveObject;
    using RandomAccessTableReaderArchiveImplBase<Holder>::kNoObject;
    using RandomAccessTableReaderArchiveImplBase<Holder>::kEof;
    using RandomAccessTableReaderArchiveImplBase<Holder>::kError;
    using RandomAccessTableReaderArchiveImplBase<Holder>::state_;
    using RandomAccessTableReaderArchiveImplBase<Holder>::opts_;
    using RandomAccessTableReaderArchiveImplBase<Holder>::cur_key_;
    using RandomAccessTableReaderArchiveImplBase<Holder>::holder_;
    using RandomAccessTableReaderArchiveImplBase<Holder>::rspecifier_;
    using RandomAccessTableReaderArchiveImplBase<Holder>::archive_rxfilename_;
    using RandomAccessTableReaderArchiveImplBase<Holder>::ReadNextObject;
  
   public:
    typedef typename Holder::T T;
  
    RandomAccessTableReaderSortedArchiveImpl():
        last_found_index_(static_cast<size_t>(-1)),
        pending_delete_(static_cast<size_t>(-1)) { }
  
    virtual bool Close() {
      for (size_t i = 0; i < seen_pairs_.size(); i++)
        delete seen_pairs_[i].second;
      seen_pairs_.clear();
  
      pending_delete_ = static_cast<size_t>(-1);
      last_found_index_ = static_cast<size_t>(-1);
  
      return this->CloseInternal();
    }
    virtual bool HasKey(const std::string &key) {
      HandlePendingDelete();
      size_t index;
      bool ans = FindKeyInternal(key, &index);
      if (ans && opts_.once && seen_pairs_[index].second == NULL) {
        // Just do a check RE the once option. "&&opts_.once" is for
        // efficiency since this can only happen in that case.
        KALDI_ERR << "Error: HasKey called after Value() already called for "
                  << " that key, and once (o) option specified: rspecifier is "
                  << rspecifier_;
      }
      return ans;
    }
    virtual const T & Value(const std::string &key) {
      HandlePendingDelete();
      size_t index;
      if (!FindKeyInternal(key, &index)) {
        KALDI_ERR << "Value() called but no such key " << key
                  << " in archive " << PrintableRxfilename(archive_rxfilename_);
      }
      if (seen_pairs_[index].second == NULL) {  // can happen if opts.once_
        KALDI_ERR << "Error: Value() called more than once for key "
                  << key << " and once (o) option specified: rspecifier is "
                  << rspecifier_;
      }
      if (opts_.once)
        pending_delete_ = index;  // mark this index to be deleted on next call.
      return seen_pairs_[index].second->Value();
    }
    virtual ~RandomAccessTableReaderSortedArchiveImpl() {
      if (this->IsOpen())
        if (!Close())  // more specific warning will already have been printed.
          // we are in some kind of error state & user did not find out by
          // calling Close().
          KALDI_ERR << "Error closing RandomAccessTableReader: rspecifier is "
                    << rspecifier_;
    }
   private:
    void HandlePendingDelete() {
      const size_t npos = static_cast<size_t>(-1);
      if (pending_delete_ != npos) {
        KALDI_ASSERT(pending_delete_ < seen_pairs_.size());
        KALDI_ASSERT(seen_pairs_[pending_delete_].second != NULL);
        delete seen_pairs_[pending_delete_].second;
        seen_pairs_[pending_delete_].second = NULL;
        pending_delete_ = npos;
      }
    }
  
    // FindKeyInternal tries to find the key in the array "seen_pairs_".
    // If it is not already there, it reads ahead as far as necessary
    // to determine whether we have the key or not.  On success it returns
    // true and puts the index into the array seen_pairs_, into "index";
    // on failure it returns false.
    // It will leave the state as either kNoObject, kEof or kError.
    // FindKeyInternal does not do any checking about whether you are asking
    // about a key that has been already given (with the "once" option).
    // That is the user's responsibility.
  
    bool FindKeyInternal(const std::string &key, size_t *index) {
      // First, an optimization in case the previous call was for the
      // same key, and we found it.
      if (last_found_index_ < seen_pairs_.size()
         && seen_pairs_[last_found_index_].first == key) {
        *index = last_found_index_;
        return true;
      }
  
      if (state_ == kUninitialized)
        KALDI_ERR << "Trying to access a RandomAccessTableReader object that is"
                     " not open.";
  
      // Step one is to see whether we have to read ahead for the object..
      // Note, the possible states right now are kNoObject, kEof or kError.
      // We are never in the state kHaveObject except just after calling
      // ReadNextObject().
      bool looped = false;
      while (state_ == kNoObject &&
            (seen_pairs_.empty() || key.compare(seen_pairs_.back().first) > 0)) {
        looped = true;
        // Read this as:
        //  while ( the stream is potentially good for reading &&
        //        ([got no keys] || key > most_recent_key) ) { ...
        //     Try to read a new object.
        // Note that the keys in seen_pairs_ are ordered from least to greatest.
        ReadNextObject();
        if (state_ == kHaveObject) {  // Successfully read object.
          if (!seen_pairs_.empty() &&  // This is just a check.
             cur_key_.compare(seen_pairs_.back().first) <= 0) {
            // read the expression above as: !( cur_key_ > previous_key).
            // it means we are not in sorted order [the user specified that we
            // are, or we would not be using this implementation].
            KALDI_ERR << "You provided the sorted (s) option but keys in archive "
                      << PrintableRxfilename(archive_rxfilename_) << " are not "
                      << "in sorted order: " << seen_pairs_.back().first
                      << " is followed by " << cur_key_;
          }
          KALDI_ASSERT(holder_ != NULL);
          seen_pairs_.push_back(std::make_pair(cur_key_, holder_));
          holder_ = NULL;
          state_ = kNoObject;
        }
      }
      if (looped) {  // We only need to check the last element of the seen_pairs_
        // array, since we would not have read more after getting "key".
        if (!seen_pairs_.empty() && seen_pairs_.back().first == key) {
          last_found_index_ = *index = seen_pairs_.size() - 1;
          return true;
        } else {
          return false;
        }
      }
      // Now we have do an actual binary search in the seen_pairs_ array.
      std::pair<std::string, Holder*> pr(key, static_cast<Holder*>(NULL));
      typename std::vector<std::pair<std::string, Holder*> >::iterator
          iter = std::lower_bound(seen_pairs_.begin(), seen_pairs_.end(),
                                  pr, PairCompare());
      if (iter != seen_pairs_.end() &&
         key == iter->first) {
        last_found_index_ = *index = (iter - seen_pairs_.begin());
        return true;
      } else {
        return false;
      }
    }
  
    // These are the pairs of (key, object) we have read.  We keep all the keys we
    // have read but the actual objects (if they are stored with pointers inside
    // the Holder object) may be deallocated if once == true, and the Holder
    // pointer set to NULL.
    std::vector<std::pair<std::string, Holder*> > seen_pairs_;
    size_t last_found_index_;  // An optimization s.t. if FindKeyInternal called
    // twice with same key (as it often will), it doesn't have to do the key
    // search twice.
    size_t pending_delete_;  // If opts_.once == true, this is the index of
    // element of seen_pairs_ that is pending deletion.
    struct PairCompare {
      // PairCompare is the Less-than operator for the pairs of(key, Holder).
      // compares the keys.
      inline bool operator() (const std::pair<std::string, Holder*> &pr1,
                              const std::pair<std::string, Holder*> &pr2) {
        return  (pr1.first.compare(pr2.first) < 0);
      }
    };
  };
  
  
  
  // RandomAccessTableReaderUnsortedArchiveImpl is for random-access reading of
  // archives when the user does not specify the sorted (s) option (in this case
  // the called-sorted, or "cs" option, is ignored).  This is the least efficient
  // of the random access archive readers, in general, but it can be as efficient
  // as the others, in speed, memory and latency, if the "once" option is
  // specified and it happens that the keys of the archive are the same as the
  // keys the code is called with (to HasKey() and Value()), and in the same
  // order.  However, if you ask it for a key that's not present it will have to
  // read the archive till the end and store it all in memory.
  
  template<class Holder>
  class RandomAccessTableReaderUnsortedArchiveImpl:
        public RandomAccessTableReaderArchiveImplBase<Holder> {
    using RandomAccessTableReaderArchiveImplBase<Holder>::kUninitialized;
    using RandomAccessTableReaderArchiveImplBase<Holder>::kHaveObject;
    using RandomAccessTableReaderArchiveImplBase<Holder>::kNoObject;
    using RandomAccessTableReaderArchiveImplBase<Holder>::kEof;
    using RandomAccessTableReaderArchiveImplBase<Holder>::kError;
    using RandomAccessTableReaderArchiveImplBase<Holder>::state_;
    using RandomAccessTableReaderArchiveImplBase<Holder>::opts_;
    using RandomAccessTableReaderArchiveImplBase<Holder>::cur_key_;
    using RandomAccessTableReaderArchiveImplBase<Holder>::holder_;
    using RandomAccessTableReaderArchiveImplBase<Holder>::rspecifier_;
    using RandomAccessTableReaderArchiveImplBase<Holder>::archive_rxfilename_;
    using RandomAccessTableReaderArchiveImplBase<Holder>::ReadNextObject;
  
    typedef typename Holder::T T;
  
   public:
    RandomAccessTableReaderUnsortedArchiveImpl(): to_delete_iter_(map_.end()),
                                                  to_delete_iter_valid_(false) {
      map_.max_load_factor(0.5);  // make it quite empty -> quite efficient.
      // default seems to be 1.
    }
  
    virtual bool Close() {
      for (typename MapType::iterator iter = map_.begin();
          iter != map_.end();
          ++iter) {
        delete iter->second;
      }
      map_.clear();
      first_deleted_string_ = "";
      to_delete_iter_valid_ = false;
      return this->CloseInternal();
    }
  
    virtual bool HasKey(const std::string &key) {
      HandlePendingDelete();
      return FindKeyInternal(key, NULL);
    }
    virtual const T & Value(const std::string &key) {
      HandlePendingDelete();
      const T *ans_ptr = NULL;
      if (!FindKeyInternal(key, &ans_ptr))
        KALDI_ERR << "Value() called but no such key " << key
                  << " in archive " << PrintableRxfilename(archive_rxfilename_);
      return *ans_ptr;
    }
    virtual ~RandomAccessTableReaderUnsortedArchiveImpl() {
      if (this->IsOpen())
        if (!Close())  // more specific warning will already have been printed.
          // we are in some kind of error state & user did not find out by
          // calling Close().
          KALDI_ERR << "Error closing RandomAccessTableReader: rspecifier is "
                    << rspecifier_;
    }
   private:
    void HandlePendingDelete() {
      if (to_delete_iter_valid_) {
        to_delete_iter_valid_ = false;
        delete to_delete_iter_->second;  // Delete Holder object.
        if (first_deleted_string_.length() == 0)
          first_deleted_string_ = to_delete_iter_->first;
        map_.erase(to_delete_iter_);  // delete that element.
      }
    }
  
    // FindKeyInternal tries to find the key in the map "map_"
    // If it is not already there, it reads ahead either until it finds the
    // key, or until end of file.  If called with value_ptr == NULL,
    // it assumes it's called from HasKey() and just returns true or false
    // and doesn't otherwise have side effects.  If called with value_ptr !=
    // NULL, it assumes it's called from Value().  Thus, it will crash
    // if it cannot find the key.  If it can find it it puts its address in
    // *value_ptr, and if opts_once == true it will mark that element of the
    // map to be deleted.
  
    bool FindKeyInternal(const std::string &key, const T **value_ptr = NULL) {
      typename MapType::iterator iter = map_.find(key);
      if (iter != map_.end()) {  // Found in the map...
        if (value_ptr == NULL) {  // called from HasKey
          return true;  // this is all we have to do.
        } else {
          *value_ptr = &(iter->second->Value());
          if (opts_.once) {  // value won't be needed again, so mark
            // for deletion.
            to_delete_iter_ = iter;  // pending delete.
            KALDI_ASSERT(!to_delete_iter_valid_);
            to_delete_iter_valid_ = true;
          }
          return true;
        }
      }
      while (state_ == kNoObject) {
        ReadNextObject();
        if (state_ == kHaveObject) {  // Successfully read object.
          state_ = kNoObject;  // we are about to transfer ownership
          // of the object in holder_ to map_.
          // Insert it into map_.
          std::pair<typename MapType::iterator, bool> pr =
              map_.insert(typename MapType::value_type(cur_key_, holder_));
  
          if (!pr.second) {  // Was not inserted-- previous element w/ same key
            delete holder_;  // map was not changed, no ownership transferred.
            holder_ = NULL;
            KALDI_ERR << "Error in RandomAccessTableReader: duplicate key "
                      << cur_key_ << " in archive " << archive_rxfilename_;
          }
          holder_ = NULL;  // ownership transferred to map_.
          if (cur_key_ == key) {  // the one we wanted..
            if (value_ptr == NULL) {  // called from HasKey
              return true;
            } else {  // called from Value()
              *value_ptr = &(pr.first->second->Value());  // this gives us the
              // Value() from the Holder in the map.
              if (opts_.once) {  // mark for deletion, as won't be needed again.
                to_delete_iter_ = pr.first;
                KALDI_ASSERT(!to_delete_iter_valid_);
                to_delete_iter_valid_ = true;
              }
              return true;
            }
          }
        }
      }
      if (opts_.once && key == first_deleted_string_) {
        KALDI_ERR << "You specified the once (o) option but "
                  << "you are calling using key " << key
                  << " more than once: rspecifier is " << rspecifier_;
      }
      return false;  // We read the entire archive (or got to error state) and
      // didn't find it.
    }
  
    typedef unordered_map<std::string, Holder*, StringHasher>  MapType;
    MapType map_;
  
    typename MapType::iterator to_delete_iter_;
    bool to_delete_iter_valid_;
  
    std::string first_deleted_string_;  // keep the first string we deleted
    // from map_ (if opts_.once == true).  It's for an inexact spot-check that the
    // "once" option isn't being used incorrectly.
  };
  
  
  
  
  
  template<class Holder>
  RandomAccessTableReader<Holder>::RandomAccessTableReader(const
                                                         std::string &rspecifier):
      impl_(NULL) {
    if (rspecifier != "" && !Open(rspecifier))
      KALDI_ERR << "Error opening RandomAccessTableReader object "
          " (rspecifier is: " << rspecifier << ")";
  }
  
  template<class Holder>
  bool RandomAccessTableReader<Holder>::Open(const std::string &rspecifier) {
    if (IsOpen())
      KALDI_ERR << "Already open.";
    RspecifierOptions opts;
    RspecifierType rs = ClassifyRspecifier(rspecifier, NULL, &opts);
    switch (rs) {
      case kScriptRspecifier:
        impl_ = new RandomAccessTableReaderScriptImpl<Holder>();
        break;
      case kArchiveRspecifier:
        if (opts.sorted) {
          if (opts.called_sorted)  // "doubly" sorted case.
            impl_ = new RandomAccessTableReaderDSortedArchiveImpl<Holder>();
          else
            impl_ = new RandomAccessTableReaderSortedArchiveImpl<Holder>();
        } else {
          impl_ = new RandomAccessTableReaderUnsortedArchiveImpl<Holder>();
        }
        break;
      case kNoRspecifier: default:
        KALDI_WARN << "Invalid rspecifier: "
                   << rspecifier;
        return false;
    }
    if (!impl_->Open(rspecifier)) {
      // A warning will already have been printed.
      delete impl_;
      impl_ = NULL;
      return false;
    }
    return true;
  }
  
  template<class Holder>
  bool RandomAccessTableReader<Holder>::HasKey(const std::string &key) {
    CheckImpl();
    if (!IsToken(key))
      KALDI_ERR << "Invalid key \"" << key << '"';
    return impl_->HasKey(key);
  }
  
  
  template<class Holder>
  const typename RandomAccessTableReader<Holder>::T&
  RandomAccessTableReader<Holder>::Value(const std::string &key) {
    CheckImpl();
    return impl_->Value(key);
  }
  
  template<class Holder>
  bool RandomAccessTableReader<Holder>::Close() {
    CheckImpl();
    bool ans =impl_->Close();
    delete impl_;
    impl_ = NULL;
    return ans;
  }
  
  template<class Holder>
  RandomAccessTableReader<Holder>::~RandomAccessTableReader() {
    if (IsOpen() && !Close())  // call Close() yourself to stop this being thrown.
      KALDI_ERR << "failure detected in destructor.";
  }
  
  template<class Holder>
  void SequentialTableReader<Holder>::CheckImpl() const {
    if (!impl_) {
      KALDI_ERR << "Trying to use empty SequentialTableReader (perhaps you "
                << "passed the empty string as an argument to a program?)";
    }
  }
  
  template<class Holder>
  void RandomAccessTableReader<Holder>::CheckImpl() const {
    if (!impl_) {
      KALDI_ERR << "Trying to use empty RandomAccessTableReader (perhaps you "
                << "passed the empty string as an argument to a program?)";
    }
  }
  
  template<class Holder>
  void TableWriter<Holder>::CheckImpl() const {
    if (!impl_) {
      KALDI_ERR << "Trying to use empty TableWriter (perhaps you "
                << "passed the empty string as an argument to a program?)";
    }
  }
  
  template<class Holder>
  RandomAccessTableReaderMapped<Holder>::RandomAccessTableReaderMapped(
      const std::string &table_rxfilename,
      const std::string &utt2spk_rxfilename):
      reader_(table_rxfilename), token_reader_(table_rxfilename.empty() ? "" :
                                               utt2spk_rxfilename),
      utt2spk_rxfilename_(utt2spk_rxfilename) { }
  
  template<class Holder>
  bool RandomAccessTableReaderMapped<Holder>::Open(
      const std::string &table_rxfilename,
      const std::string &utt2spk_rxfilename) {
    if (reader_.IsOpen()) reader_.Close();
    if (token_reader_.IsOpen()) token_reader_.Close();
    KALDI_ASSERT(!table_rxfilename.empty());
    if (!reader_.Open(table_rxfilename)) return false;  // will have printed
    // warning internally, probably.
    if (!utt2spk_rxfilename.empty()) {
      if (!token_reader_.Open(utt2spk_rxfilename)) {
        reader_.Close();
        return false;
      }
    }
    return true;
  }
  
  
  template<class Holder>
  bool RandomAccessTableReaderMapped<Holder>::HasKey(const std::string &utt) {
    // We don't check IsOpen, we let the call go through to the member variable
    // (reader_), which will crash with a more informative error message than
    // we can give here, as we don't any longer know the rxfilename.
    if (token_reader_.IsOpen()) {  // We need to map the key from utt to spk.
      if (!token_reader_.HasKey(utt))
        KALDI_ERR << "Attempting to read key " << utt << ", which is not present "
                  << "in utt2spk map or similar map being read from "
                  << PrintableRxfilename(utt2spk_rxfilename_);
      const std::string &spk = token_reader_.Value(utt);
      return reader_.HasKey(spk);
    } else {
      return reader_.HasKey(utt);
    }
  }
  
  template<class Holder>
  const typename Holder::T& RandomAccessTableReaderMapped<Holder>::Value(
      const std::string &utt) {
    if (token_reader_.IsOpen()) {  // We need to map the key from utt to spk.
      if (!token_reader_.HasKey(utt))
        KALDI_ERR << "Attempting to read key " << utt << ", which is not present "
                  << "in utt2spk map or similar map being read from "
                  << PrintableRxfilename(utt2spk_rxfilename_);
      const std::string &spk = token_reader_.Value(utt);
      return reader_.Value(spk);
    } else {
      return reader_.Value(utt);
    }
  }
  
  
  
  /// @}
  
  }  // end namespace kaldi
  
  
  
  #endif  // KALDI_UTIL_KALDI_TABLE_INL_H_