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
#ifndef BMSTRSPARSEVEC__H__INCLUDED__
#define BMSTRSPARSEVEC__H__INCLUDED__
/*
Copyright(c) 2002-2017 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
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
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
For more information please visit: http://bitmagic.io
*/
/*! \file bmstrsparsevec.h
\brief string sparse vector based on bit-transposed matrix
*/
#include <stddef.h>
#include "bmconst.h"
#ifndef BM_NO_STL
#include <stdexcept>
#endif
#ifndef BM__H__INCLUDED__
// BitMagic utility headers do not include main "bm.h" declaration
// #include "bm.h" or "bm64.h" explicitly
# error missing include (bm.h or bm64.h)
#endif
#include "bmtrans.h"
#include "bmalgo.h"
#include "bmbuffer.h"
#include "bmbmatrix.h"
#include "bmdef.h"
namespace bm
{
/*!
\brief succinct sparse vector for strings with compression using bit-slicing ( transposition) method
Initial string is bit-transposed into bit-slices so collection may use less
memory due to prefix sum (GAP) compression in bit-slices. In addition, the container
can use chracter re-mapping using char freaquencies to compute the minimal codes.
Re-mapping can reduce memory footprint, get better search performance and improve storage
compression.
Template parameters:
CharType - type of character (char or unsigned char) (wchar not tested)
BV - bit-vector for bit-slicing
STR_SIZE - initial string size (can dynamically increase on usage)
@ingroup sv
*/
template<typename CharType, typename BV, unsigned STR_SIZE>
class str_sparse_vector : public base_sparse_vector<CharType, BV, STR_SIZE>
{
public:
typedef BV bvector_type;
typedef bvector_type* bvector_type_ptr;
typedef const bvector_type* bvector_type_const_ptr;
typedef CharType value_type;
typedef CharType* value_type_prt;
typedef typename bvector_type::size_type size_type;
typedef typename BV::allocator_type allocator_type;
typedef typename bvector_type::allocation_policy allocation_policy_type;
typedef typename bvector_type::enumerator bvector_enumerator_type;
typedef typename allocator_type::allocator_pool_type allocator_pool_type;
typedef bm::basic_bmatrix<BV> bmatrix_type;
typedef base_sparse_vector<CharType, BV, STR_SIZE> parent_type;
typedef typename parent_type::unsigned_value_type unsigned_value_type;
/*! Statistical information about memory allocation details. */
struct statistics : public bv_statistics
{};
enum octet_slices
{
sv_octet_slices = STR_SIZE
};
/** Matrix of character remappings
@internal
*/
typedef bm::dynamic_heap_matrix<unsigned char, allocator_type>
slice_octet_matrix_type;
typedef slice_octet_matrix_type remap_matrix_type;
/** Matrix of character frequencies (for optimal code remap)
@internal
*/
typedef
bm::dynamic_heap_matrix<size_t, allocator_type> octet_freq_matrix_type;
struct is_remap_support { enum trait { value = true }; };
struct is_rsc_support { enum trait { value = false }; };
struct is_dynamic_splices { enum trait { value = true }; };
class reference_base
{
public:
typedef
bm::heap_vector<CharType, typename bvector_type::allocator_type, true>
bufffer_type;
protected:
mutable bufffer_type buf_;
};
/**
Reference class to access elements via common [] operator
@ingroup sv
*/
class const_reference : protected reference_base
{
public:
const_reference(
const str_sparse_vector<CharType, BV, STR_SIZE>& str_sv,
size_type idx)
: str_sv_(str_sv), idx_(idx)
{
this->buf_.resize(str_sv.effective_max_str());
}
operator const value_type*() const BMNOEXCEPT
{
return get();
}
const value_type* get() const BMNOEXCEPT
{
str_sv_.get(idx_, this->buf_.data(), str_sv_.effective_max_str());
return this->buf_.data();
}
bool operator==(const const_reference& ref) const BMNOEXCEPT
{ return bool(*this) == bool(ref); }
bool is_null() const BMNOEXCEPT { return str_sv_.is_null(idx_); }
private:
const str_sparse_vector<CharType, BV, STR_SIZE>& str_sv_;
size_type idx_;
};
/**
Reference class to access elements via common [] operator
@ingroup sv
*/
class reference : protected reference_base
{
public:
reference(str_sparse_vector<CharType, BV, STR_SIZE>& str_sv,
size_type idx)
: str_sv_(str_sv), idx_(idx)
{
this->buf_.resize(str_sv.effective_max_str());
}
operator const value_type*() const BMNOEXCEPT
{
return get();
}
const value_type* get() const BMNOEXCEPT
{
str_sv_.get(idx_, this->buf_.data(), str_sv_.effective_max_str());
return this->buf_.data();
}
reference& operator=(const reference& ref)
{
// TO DO: implement element copy bit by bit
str_sv_.set(idx_, (const value_type*)ref);
return *this;
}
reference& operator=(const value_type* str)
{
str_sv_.set(idx_, str);
return *this;
}
bool operator==(const reference& ref) const BMNOEXCEPT
{ return bool(*this) == bool(ref); }
bool is_null() const BMNOEXCEPT { return str_sv_.is_null(idx_); }
private:
str_sparse_vector<CharType, BV, STR_SIZE>& str_sv_;
size_type idx_;
};
/**
Const iterator to do quick traverse of the sparse vector.
Implementation uses buffer for decoding so, competing changes
to the original vector may not match the iterator returned values.
This iterator keeps an operational buffer of transposed elements,
so memory footprint is not negligable.
@ingroup sv
*/
class const_iterator
{
public:
friend class str_sparse_vector;
#ifndef BM_NO_STL
typedef std::input_iterator_tag iterator_category;
#endif
typedef str_sparse_vector<CharType, BV, STR_SIZE> str_sparse_vector_type;
typedef str_sparse_vector_type* str_sparse_vector_type_ptr;
typedef typename str_sparse_vector_type::value_type value_type;
typedef typename str_sparse_vector_type::size_type size_type;
typedef typename str_sparse_vector_type::bvector_type bvector_type;
typedef typename bvector_type::allocator_type allocator_type;
typedef typename allocator_type::allocator_pool_type allocator_pool_type;
typedef long long difference_type;
typedef CharType* pointer;
typedef CharType*& reference;
public:
/**
Construct iterator (not attached to any particular vector)
*/
const_iterator() BMNOEXCEPT;
/**
Construct iterator (attached to sparse vector)
@param sv - pointer to sparse vector
*/
const_iterator(const str_sparse_vector_type* sv) BMNOEXCEPT;
/**
Construct iterator (attached to sparse vector) and positioned
@param sv - reference to sparse vector
@param pos - position in the vector to start
*/
const_iterator(const str_sparse_vector_type* sv, size_type pos) BMNOEXCEPT;
const_iterator(const const_iterator& it) BMNOEXCEPT;
/**
setup iterator to retrieve a sub-string of a string
@param from - Position of the first character to be copied
@param len - length of a substring (defult: 0 read to the available end)
*/
void set_substr(unsigned from, unsigned len = 0) BMNOEXCEPT;
bool operator==(const const_iterator& it) const BMNOEXCEPT
{ return (pos_ == it.pos_) && (sv_ == it.sv_); }
bool operator!=(const const_iterator& it) const BMNOEXCEPT
{ return ! operator==(it); }
bool operator < (const const_iterator& it) const BMNOEXCEPT
{ return pos_ < it.pos_; }
bool operator <= (const const_iterator& it) const BMNOEXCEPT
{ return pos_ <= it.pos_; }
bool operator > (const const_iterator& it) const BMNOEXCEPT
{ return pos_ > it.pos_; }
bool operator >= (const const_iterator& it) const BMNOEXCEPT
{ return pos_ >= it.pos_; }
/// \brief Get current position (value)
const value_type* operator*() const BMNOEXCEPT { return this->value(); }
/// \brief Advance to the next available value
const_iterator& operator++() BMNOEXCEPT
{ this->advance(); return *this; }
/// \brief Advance to the next available value
const_iterator& operator++(int) BMNOEXCEPT
{ const_iterator tmp(*this);this->advance(); return tmp; }
/// \brief Get current position (value)
const value_type* value() const;
/// \brief Get NULL status
bool is_null() const BMNOEXCEPT { return sv_->is_null(this->pos_); }
/// Returns true if iterator is at a valid position
bool valid() const BMNOEXCEPT { return pos_ != bm::id_max; }
/// Invalidate current iterator
void invalidate() BMNOEXCEPT { pos_ = bm::id_max; }
/// Current position (index) in the vector
size_type pos() const BMNOEXCEPT { return pos_; }
/// re-position to a specified position
void go_to(size_type pos) BMNOEXCEPT;
/// advance iterator forward by one
void advance() BMNOEXCEPT;
protected:
enum buf_size_e
{
n_rows = 1024
};
typedef dynamic_heap_matrix<CharType, allocator_type> buffer_matrix_type;
private:
const str_sparse_vector_type* sv_; ///!< ptr to parent
unsigned substr_from_; ///!< substring from
unsigned substr_to_; ///!< substring to
mutable size_type pos_; ///!< Position
mutable buffer_matrix_type buf_matrix_; ///!< decode value buffer
mutable size_type pos_in_buf_; ///!< buffer position
};
/**
Back insert iterator implements buffered insert, faster than generic
access assignment.
Limitations for buffered inserter:
1. Do not use more than one inserter (into one vector) at the same time
2. Use method flush() at the end to send the rest of accumulated buffer
flush is happening automatically on destruction, but if flush produces an
exception (for whatever reason) it will be an exception in destructor.
As such, explicit flush() is safer way to finilize the sparse vector load.
@ingroup sv
*/
class back_insert_iterator
{
public:
#ifndef BM_NO_STL
typedef std::output_iterator_tag iterator_category;
#endif
typedef str_sparse_vector<CharType, BV, STR_SIZE> str_sparse_vector_type;
typedef str_sparse_vector_type* str_sparse_vector_type_ptr;
typedef typename str_sparse_vector_type::value_type value_type;
typedef typename str_sparse_vector_type::size_type size_type;
typedef typename str_sparse_vector_type::bvector_type bvector_type;
typedef typename bvector_type::allocator_type allocator_type;
typedef typename allocator_type::allocator_pool_type allocator_pool_type;
typedef void difference_type;
typedef void pointer;
typedef void reference;
public:
back_insert_iterator() BMNOEXCEPT;
back_insert_iterator(str_sparse_vector_type* sv) BMNOEXCEPT;
back_insert_iterator(const back_insert_iterator& bi) BMNOEXCEPT;
back_insert_iterator& operator=(const back_insert_iterator& bi)
{
BM_ASSERT(bi.empty());
buf_matrix_.init_resize(
bi.buf_matrix_.rows(), bi.buf_matrix_.cols());
this->flush(); sv_ = bi.sv_;
return *this;
}
~back_insert_iterator();
/** push value to the vector */
back_insert_iterator& operator=(const value_type* v)
{ this->add(v); return *this; }
/** push value to the vector */
template<typename StrType>
back_insert_iterator& operator=(const StrType& v)
{
this->add(v.c_str()); return *this; // TODO: avoid c_str()
}
/** noop */
back_insert_iterator& operator*() { return *this; }
/** noop */
back_insert_iterator& operator++() { return *this; }
/** noop */
back_insert_iterator& operator++( int ) { return *this; }
/** add value to the container*/
void add(const value_type* v);
/** add NULL (no-value) to the container */
void add_null();
/** add a series of consequitve NULLs (no-value) to the container */
void add_null(size_type count);
/** flush the accumulated buffer */
void flush();
protected:
/** return true if insertion buffer is empty */
bool empty() const BMNOEXCEPT;
typedef typename bvector_type::block_idx_type block_idx_type;
/** add value to the buffer without changing the NULL vector
@param v - value to push back
@internal
*/
void add_value(const value_type* v);
private:
enum buf_size_e
{
n_buf_size = str_sparse_vector_type::ins_buf_size // 1024 * 8
};
typedef bm::dynamic_heap_matrix<CharType, allocator_type> buffer_matrix_type;
private:
str_sparse_vector_type* sv_; ///!< pointer on the parent vector
bvector_type* bv_null_; ///!< not NULL vector pointer
buffer_matrix_type buf_matrix_; ///!< value buffer
size_type pos_in_buf_; ///!< buffer position
block_idx_type prev_nb_; ///!< previous block added
};
public:
/*!
\brief Sparse vector constructor
\param null_able - defines if vector supports NULL values flag
by default it is OFF, use bm::use_null to enable it
\param ap - allocation strategy for underlying bit-vectors
Default allocation policy uses BM_BIT setting (fastest access)
\param bv_max_size - maximum possible size of underlying bit-vectors
Please note, this is NOT size of svector itself, it is dynamic upper limit
which should be used very carefully if we surely know the ultimate size
\param alloc - allocator for bit-vectors
\sa bvector<>
\sa bm::bvector<>::allocation_policy
\sa bm::startegy
*/
str_sparse_vector(bm::null_support null_able = bm::no_null,
allocation_policy_type ap = allocation_policy_type(),
size_type bv_max_size = bm::id_max,
const allocator_type& alloc = allocator_type());
/*! copy-ctor */
str_sparse_vector(const str_sparse_vector& str_sv);
/*! copy assignmment operator */
str_sparse_vector<CharType, BV, STR_SIZE>& operator = (
const str_sparse_vector<CharType, BV, STR_SIZE>& str_sv)
{
if (this != &str_sv)
parent_type::copy_from(str_sv);
remap_flags_ = str_sv.remap_flags_;
remap_matrix1_ = str_sv.remap_matrix1_;
remap_matrix2_ = str_sv.remap_matrix2_;
return *this;
}
#ifndef BM_NO_CXX11
/*! move-ctor */
str_sparse_vector(str_sparse_vector<CharType, BV, STR_SIZE>&& str_sv) BMNOEXCEPT
{
parent_type::swap(str_sv);
remap_flags_ = str_sv.remap_flags_;
remap_matrix1_.swap(str_sv.remap_matrix1_);
remap_matrix2_.swap(str_sv.remap_matrix2_);
}
/*! move assignmment operator */
str_sparse_vector<CharType, BV, STR_SIZE>& operator =
(str_sparse_vector<CharType, BV, STR_SIZE>&& str_sv) BMNOEXCEPT
{
if (this != &str_sv)
{
this->swap(str_sv);
}
return *this;
}
#endif
public:
// ------------------------------------------------------------
/*! @name String element access */
///@{
/** \brief Operator to get read access to an element */
const const_reference operator[](size_type idx) const
{ return const_reference(*this, idx); }
/** \brief Operator to get write access to an element */
reference operator[](size_type idx) { return reference(*this, idx); }
/*!
\brief set specified element with bounds checking and automatic resize
\param idx - element index (vector auto-resized if needs to)
\param str - string to set (zero terminated)
*/
void set(size_type idx, const value_type* str);
/*!
\brief set NULL status for the specified element
Vector is resized automatically
\param idx - element index (vector auto-resized if needs to)
*/
void set_null(size_type idx);
/*!
\brief insert the specified element
\param idx - element index (vector auto-resized if needs to)
\param str - string to set (zero terminated)
*/
void insert(size_type idx, const value_type* str);
/*!
\brief insert STL string
\param idx - element index (vector auto-resized if needs to)
\param str - STL string to set
*/
template<typename StrType>
void insert(size_type idx, const StrType& str)
{
this->insert(idx, str.c_str()); // TODO: avoid c_str()
}
/*!
\brief erase the specified element
\param idx - element index
*/
void erase(size_type idx);
/*!
\brief get specified element
\param idx - element index
\param str - string buffer
\param buf_size - string buffer size
@return string length
*/
size_type get(size_type idx,
value_type* str, size_type buf_size) const BMNOEXCEPT;
/*!
\brief set specified element with bounds checking and automatic resize
This is an equivalent of set() method, but templetized to be
more compatible with the STL std::string and the likes
\param idx - element index (vector auto-resized if needs to)
\param str - input string
expected an STL class with size() support,
like basic_string<> or vector<char>
*/
template<typename StrType>
void assign(size_type idx, const StrType& str)
{
if (idx >= this->size())
this->size_ = idx+1;
size_type str_size = size_type(str.size());
if (!str_size)
{
this->clear_value_planes_from(0, idx);
return;
}
for (size_type i=0; i < str_size; ++i)
{
CharType ch = str[i];
if (remap_flags_) // compressional re-mapping is in effect
{
unsigned char remap_value = remap_matrix2_.get(i, unsigned(ch));
BM_ASSERT(remap_value);
ch = CharType(remap_value);
}
this->bmatr_.set_octet(idx, i, (unsigned char)ch);
if (!ch)
break;
} // for i
this->clear_value_planes_from(unsigned(str_size*8), idx);
if (bvector_type* bv_null = this->get_null_bvect())
bv_null->set_bit_no_check(idx);
}
/*!
\brief push back a string
\param str - string to set
(STL class with size() support, like basic_string)
*/
template<typename StrType>
void push_back(const StrType& str) { assign(this->size_, str); }
/*!
\brief push back a string (zero terminated)
\param str - string to set
*/
void push_back(const value_type* str) { set(this->size_, str); }
/*!
\brief get specified string element
Template method expects an STL-compatible type basic_string<>
\param idx - element index (vector auto-resized if needs to)
\param str - string to get [out]
*/
template<typename StrType>
void get(size_type idx, StrType& str) const
{
str.clear();
for (unsigned i = 0; true; ++i)
{
CharType ch = CharType(this->bmatr_.get_octet(idx, i));
if (!ch)
break;
if (remap_flags_)
{
const unsigned char* remap_row = remap_matrix1_.row(i);
unsigned char remap_value = remap_row[unsigned(ch)];
BM_ASSERT(remap_value);
if (!remap_value) // unknown dictionary element
{
throw_bad_value(0);
break;
}
ch = CharType(remap_value);
}
str.push_back(ch);
} // for i
}
/*! Swap content */
void swap(str_sparse_vector& str_sv) BMNOEXCEPT;
///@}
// ------------------------------------------------------------
/*! @name Element comparison functions */
///@{
/**
\brief Compare vector element with argument lexicographically
NOTE: for a re-mapped container, input string may have no correct
remapping, in this case we have an ambiguity
(we know it is not equal (0) but LT or GT?).
Behavior is undefined.
\param idx - vactor element index
\param str - argument to compare with
\return 0 - equal, < 0 - vect[i] < str, >0 otherwise
*/
int compare(size_type idx, const value_type* str) const BMNOEXCEPT;
/**
\brief Find size of common prefix between two vector elements in octets
\return size of common prefix
*/
unsigned common_prefix_length(size_type idx1, size_type idx2) const BMNOEXCEPT;
///@}
// ------------------------------------------------------------
/*! @name Clear */
///@{
/*! \brief resize to zero, free memory */
void clear_all(bool free_mem) BMNOEXCEPT;
/*! \brief resize to zero, free memory */
void clear() BMNOEXCEPT { clear_all(true); }
/*!
\brief clear range (assign bit 0 for all planes)
\param left - interval start
\param right - interval end (closed interval)
\param set_null - set cleared values to unassigned (NULL)
*/
str_sparse_vector<CharType, BV, STR_SIZE>&
clear_range(size_type left, size_type right, bool set_null = false)
{
parent_type::clear_range(left, right, set_null);
return *this;
}
///@}
// ------------------------------------------------------------
/*! @name Size, etc */
///@{
/*! \brief return size of the vector
\return size of sparse vector
*/
size_type size() const { return this->size_; }
/*! \brief return true if vector is empty
\return true if empty
*/
bool empty() const { return (size() == 0); }
/*! \brief resize vector
\param sz - new size
*/
void resize(size_type sz) { parent_type::resize(sz); }
/*! \brief get maximum string length capacity
\return maximum string length sparse vector can take
*/
static size_type max_str() { return sv_octet_slices; }
/*! \brief get effective string length used in vector
Calculate and returns efficiency, how close are we
to the reserved maximum.
\return current string length maximum
*/
size_type effective_max_str() const BMNOEXCEPT;
/*! \brief get effective string length used in vector
\return current string length maximum
*/
size_type effective_vector_max() const { return effective_max_str(); }
/**
\brief recalculate size to exclude tail NULL elements
After this call size() will return the true size of the vector
*/
void sync_size() BMNOEXCEPT;
///@}
// ------------------------------------------------------------
/*! @name Memory optimization/compression */
///@{
/*!
\brief run memory optimization for all vector planes
\param temp_block - pre-allocated memory block to avoid unnecessary re-allocs
\param opt_mode - requested compression depth
\param stat - memory allocation statistics after optimization
*/
void optimize(
bm::word_t* temp_block = 0,
typename bvector_type::optmode opt_mode = bvector_type::opt_compress,
typename str_sparse_vector<CharType, BV, STR_SIZE>::statistics* stat = 0);
/*!
@brief Calculates memory statistics.
Function fills statistics structure containing information about how
this vector uses memory and estimation of max. amount of memory
bvector needs to serialize itself.
@param st - pointer on statistics structure to be filled in.
@sa statistics
*/
void calc_stat(
struct str_sparse_vector<CharType, BV, STR_SIZE>::statistics* st
) const BMNOEXCEPT;
///@}
// ------------------------------------------------------------
/*! @name Iterator access */
///@{
/** Provide const iterator access to container content */
const_iterator begin() const BMNOEXCEPT;
/** Provide const iterator access to the end */
const_iterator end() const BMNOEXCEPT { return const_iterator(this, bm::id_max); }
/** Get const_itertor re-positioned to specific element
@param idx - position in the sparse vector
*/
const_iterator get_const_iterator(size_type idx) const BMNOEXCEPT
{ return const_iterator(this, idx); }
/** Provide back insert iterator
Back insert iterator implements buffered insertion, which is faster, than random access
or push_back
*/
back_insert_iterator get_back_inserter()
{ return back_insert_iterator(this); }
///@}
// ------------------------------------------------------------
/*! @name Various traits */
///@{
/** \brief various type traits
*/
static constexpr
bool is_compressed() BMNOEXCEPT { return false; }
static constexpr
bool is_str() BMNOEXCEPT { return true; }
///@}
// ------------------------------------------------------------
/*! @name Char remapping, succinct utilities
Remapping runs character usage analysis (frequency analysis)
based on that implements reduction of dit-depth thus improves
search performance and memory usage (both RAM and serialized).
Remapping limits farther modifications of sparse vector.
(Use remapped vector as read-only).
*/
///@{
/**
Get character remapping status (true|false)
*/
bool is_remap() const BMNOEXCEPT { return remap_flags_ != 0; }
/**
Build remapping profile and load content from another sparse vector
Remapped vector likely saves memory (both RAM and disk) but
should not be modified (should be read-only).
\param str_sv - source sparse vector (assumed it is not remapped)
*/
void remap_from(const str_sparse_vector& str_sv);
/**
Build remapping profile and re-load content to save memory
*/
void remap();
/*!
Calculate flags which octets are present on each byte-plane.
@internal
*/
void calc_octet_stat(octet_freq_matrix_type& octet_matrix) const;
/*!
Compute optimal remap codes
@internal
*/
void build_octet_remap(
slice_octet_matrix_type& octet_remap_matrix1,
slice_octet_matrix_type& octet_remap_matrix2,
octet_freq_matrix_type& octet_occupancy_matrix) const;
/*!
remap string from external (ASCII) system to matrix internal code
@return true if remapping was ok, false if found incorrect value
for the plane
@internal
*/
static
bool remap_tosv(value_type* BMRESTRICT sv_str,
size_type buf_size,
const value_type* BMRESTRICT str,
const slice_octet_matrix_type& BMRESTRICT octet_remap_matrix2
) BMNOEXCEPT;
/*!
remap string from external (ASCII) system to matrix internal code
@internal
*/
bool remap_tosv(value_type* sv_str,
size_type buf_size,
const value_type* str) const BMNOEXCEPT
{
return remap_tosv(sv_str, buf_size, str, remap_matrix2_);
}
/*!
remap string from internal code to external (ASCII) system
@return true if remapping was ok, false if found incorrect value
for the plane
@internal
*/
static
bool remap_fromsv(
value_type* BMRESTRICT str,
size_type buf_size,
const value_type* BMRESTRICT sv_str,
const slice_octet_matrix_type& BMRESTRICT octet_remap_matrix1
) BMNOEXCEPT;
/*!
re-calculate remap matrix2 based on matrix1
@internal
*/
void recalc_remap_matrix2();
///@}
// ------------------------------------------------------------
/*! @name Export content to C-style */
///@{
/**
\brief Bulk export strings to a C-style matrix of chars
\param cmatr - dest matrix (bm::heap_matrix)
\param idx_from - index in the sparse vector to export from
\param dec_size - decoding size (matrix column allocation should match)
\param zero_mem - set to false if target array is pre-initialized
with 0s to avoid performance penalty
\return number of actually exported elements (can be less than requested)
*/
template<typename CharMatrix>
size_type decode(CharMatrix& cmatr,
size_type idx_from,
size_type dec_size,
bool zero_mem = true) const
{
size_type str_len = effective_max_str();
return decode_substr(cmatr, idx_from, dec_size,
0, unsigned(str_len-1), zero_mem);
}
/**
\brief Bulk export strings to a C-style matrix of chars
\param cmatr - dest matrix (bm::heap_matrix)
\param idx_from - index in the sparse vector to export from
\param dec_size - decoding size (matrix column allocation should match)
\param substr_from - sub-string position from
\param substr_to - sub-string position to
\param zero_mem - set to false if target array is pre-initialized
with 0s to avoid performance penalty
\return number of actually exported elements (can be less than requested)
*/
template<typename CharMatrix>
size_type decode_substr(CharMatrix& cmatr,
size_type idx_from,
size_type dec_size,
unsigned substr_from,
unsigned substr_to,
bool zero_mem = true) const
{
/// Decoder functor
/// @internal
///
struct sv_decode_visitor_func
{
sv_decode_visitor_func(CharMatrix& cmatr) BMNOEXCEPT2
: cmatr_(cmatr), mask_(0), sv_off_(0)
{}
void add_bits(size_type bv_offset,
const unsigned char* BMRESTRICT bits, unsigned bits_size) BMNOEXCEPT
{
// can be negative (-1) when bv base offset = 0 and sv = 1,2..
size_type base = bv_offset - sv_off_;
unsigned_value_type m = mask_;
const unsigned i = substr_i_;
for (unsigned j = 0; j < bits_size; ++j)
{
size_type idx = bits[j] + base;
value_type* BMRESTRICT str = cmatr_.row(idx);
str[i] |= m;
} // for i
}
void add_range(size_type bv_offset, size_type sz) BMNOEXCEPT
{
auto base = bv_offset - sv_off_;
unsigned_value_type m = mask_;
const unsigned i = substr_i_;
for (size_type j = 0; j < sz; ++j)
{
size_type idx = j + base;
value_type* BMRESTRICT str = cmatr_.row(idx);
str[i] |= m;
} // for i
}
CharMatrix& cmatr_; ///< target array for reverse transpose
unsigned_value_type mask_; ///< bit-plane mask
unsigned substr_i_; ///< i
size_type sv_off_; ///< SV read offset
};
BM_ASSERT(substr_from <= substr_to);
BM_ASSERT(cmatr.is_init());
if (zero_mem)
cmatr.set_zero(); // TODO: set zero based on requested capacity
size_type rows = size_type(cmatr.rows());
size_type max_sz = this->size() - idx_from;
if (max_sz < dec_size)
dec_size = max_sz;
if (rows < dec_size)
dec_size = rows;
if (!dec_size)
return dec_size;
sv_decode_visitor_func func(cmatr);
for (unsigned i = substr_from; i <= substr_to; ++i)
{
unsigned bi = 0;
func.substr_i_ = i - substr_from; // to put substr at the str[0]
auto rsize = this->bmatr_.rows();
for (unsigned k = i * 8; k < (i * 8) + 8; ++k, ++bi)
{
if (k >= rsize)
continue;
const bvector_type* bv = this->bmatr_.get_row(k);
if (!bv)
continue;
func.mask_ = unsigned_value_type(1u << bi);
func.sv_off_ = idx_from;
size_type end = idx_from + dec_size;
bm::for_each_bit_range_no_check(*bv, idx_from, end-1, func);
} // for k
} // for i
if (remap_flags_)
{
for (unsigned i = 0; i < dec_size; ++i)
{
typename CharMatrix::value_type* str = cmatr.row(i);
remap_matrix1_.remapz(str);
} // for i
}
return dec_size;
}
/**
\brief Bulk import of strings from a C-style matrix of chars
\param cmatr - source matrix (bm::heap_matrix)
[in/out] parameter gets modified(corrupted)
in the process
\param idx_from - destination index in the sparse vector
\param imp_size - import size (number or rows to import)
*/
template<typename CharMatrix>
void import(CharMatrix& cmatr, size_type idx_from, size_type imp_size)
{
if (!imp_size)
return;
if (idx_from < this->size_) // in case it touches existing elements
{
// clear all planes in the range to provide corrrect import of 0 values
this->clear_range(idx_from, idx_from + imp_size - 1);
}
import_no_check(cmatr, idx_from, imp_size);
}
/**
\brief Bulk push-back import of strings from a C-style matrix of chars
\param cmatr - source matrix (bm::heap_matrix)
[in/out] parameter gets modified(corrupted)
in the process
\param imp_size - import size (number or rows to import)
*/
template<typename CharMatrix>
void import_back(CharMatrix& cmatr, size_type imp_size)
{
if (!imp_size)
return;
import_no_check(cmatr, this->size(), imp_size);
}
///@}
// ------------------------------------------------------------
/*! @name Merge, split, partition data */
///@{
/**
@brief copy range of values from another sparse vector
Copy [left..right] values from the source vector,
clear everything outside the range.
\param sv - source vector
\param left - index from in losed diapason of [left..right]
\param right - index to in losed diapason of [left..right]
\param slice_null - "use_null" copy range for NULL vector or
do not copy it
*/
void copy_range(const str_sparse_vector<CharType, BV, STR_SIZE>& sv,
size_type left, size_type right,
bm::null_support slice_null = bm::use_null);
/**
\brief merge with another sparse vector using OR operation
Merge is different from join(), because it borrows data from the source
vector, so it gets modified (destructive join)
\param tr_sv - [in, out]argument vector to join with (vector mutates)
\return self reference
*/
str_sparse_vector<CharType, BV, STR_SIZE>&
merge(str_sparse_vector<CharType, BV, STR_SIZE>& str_sv);
/**
Keep only specified interval in the sparse vector, clear all other
elements.
\param left - interval start
\param right - interval end (closed interval)
\param slice_null - "use_null" copy range for NULL vector or not
*/
void keep_range(size_type left, size_type right,
bm::null_support slice_null = bm::use_null);
///@}
// ------------------------------------------------------------
/*! \brief syncronize internal structures */
void sync(bool force);
/*!
\brief check if another sparse vector has the same content and size
\param sv - sparse vector for comparison
\param null_able - flag to consider NULL vector in comparison (default)
or compare only value content planes
\return true, if it is the same
*/
bool equal(const str_sparse_vector<CharType, BV, STR_SIZE>& sv,
bm::null_support null_able = bm::use_null) const BMNOEXCEPT;
/**
\brief find position of compressed element by its rank
*/
static
bool find_rank(size_type rank, size_type& pos) BMNOEXCEPT;
/**
\brief size of sparse vector (may be different for RSC)
*/
size_type effective_size() const BMNOEXCEPT { return size(); }
protected:
enum insert_buf_size_e
{
ins_buf_size = 1024 * 8
};
/// @internal
template<typename CharMatrix, size_t BufSize = ins_buf_size>
void import_no_check(CharMatrix& cmatr,
size_type idx_from, size_type imp_size,
bool set_not_null = true)
{
BM_ASSERT (cmatr.is_init());
unsigned max_str_size = 0;
{
for (unsigned j = 0; j < imp_size; ++j)
{
typename CharMatrix::value_type* str = cmatr.row(j);
typename CharMatrix::size_type i;
typename CharMatrix::size_type cols = cmatr.cols();
for (i = 0; i < cols; ++i)
{
value_type ch = str[i];
if (!ch)
{
max_str_size =
(unsigned)((i > max_str_size) ? i : max_str_size);
break;
}
if (remap_flags_) // re-mapping is in effect
{
unsigned char remap_value =
remap_matrix2_.get(i, (unsigned char)(ch));
BM_ASSERT(remap_value); // unknown ?!
/*
if (!remap_value) // unknown dictionary element
throw_bad_value(0); */
str[i] = CharType(remap_value);
}
} // for i
} // for j
}
this->bmatr_.allocate_rows((1+max_str_size) * 8 + this->is_nullable());
unsigned_value_type ch_slice[BufSize];
for (unsigned i = 0; i < max_str_size; ++i)
{
unsigned ch_acc = 0;
#if defined(BMVECTOPT) || defined(BM_USE_GCC_BUILD)
if (imp_size == ins_buf_size) /// full buffer import can use loop unrolling
{
for (size_type j = 0; j < imp_size; j+=4)
{
unsigned_value_type ch0 = (unsigned_value_type)cmatr.row(j)[i];
unsigned_value_type ch1 = (unsigned_value_type)cmatr.row(j+1)[i];
unsigned_value_type ch2 = (unsigned_value_type)cmatr.row(j+2)[i];
unsigned_value_type ch3 = (unsigned_value_type)cmatr.row(j+3)[i];
ch_acc |= ch0 | ch1 | ch2 | ch3;
ch_slice[j] = ch0; ch_slice[j+1] = ch1;
ch_slice[j+2] = ch2; ch_slice[j+3] = ch3;
}
}
else
#endif
{
for (size_type j = 0; j < imp_size; ++j)
{
unsigned_value_type ch = (unsigned_value_type)cmatr.row(j)[i];
ch_acc |= ch;
ch_slice[j] = ch;
}
}
import_char_slice(ch_slice, ch_acc, i, idx_from, imp_size);
}
size_type idx_to = idx_from + imp_size - 1;
if (set_not_null)
{
if (bvector_type* bv_null = this->get_null_bvect())
bv_null->set_range(idx_from, idx_to);
}
if (idx_to >= this->size())
this->size_ = idx_to+1;
}
#ifdef _MSC_VER
#pragma warning( push )
#pragma warning( disable : 4146 )
#endif
/// @internal
template<size_t BufSize = ins_buf_size>
void import_char_slice(const unsigned_value_type* ch_slice,
unsigned ch_acc,
size_type char_slice_idx,
size_type idx_from, size_type imp_size)
{
size_type bit_list[BufSize];
for ( ;ch_acc; ch_acc &= ch_acc - 1) // bit-scan
{
unsigned n_bits = 0;
const unsigned bi = (bm::word_bitcount((ch_acc & -ch_acc) - 1));
unsigned mask = 1u << bi;
#if defined(BMVECTOPT) || defined(BM_USE_GCC_BUILD)
if (imp_size == ins_buf_size) /// full buffer import can use loop unrolling
{
mask |= (mask << 8) | (mask << 16) | (mask << 24);
for (size_type j = 0; j < imp_size; j+=4)
{
unsigned ch0 = ((unsigned)ch_slice[j+0]) |
((unsigned)ch_slice[j+1] << 8) |
((unsigned)ch_slice[j+2] << 16) |
((unsigned)ch_slice[j+3] << 24);
ch0 &= mask;
ch0 = (ch0 >> bi) | (ch0 >> (bi+7)) |
(ch0 >> (bi+14)) | (ch0 >> (bi+21));
ch0 &= 15u;
BM_ASSERT(bm::word_bitcount(ch0) <= 4);
for (size_type base_idx = idx_from + j ;ch0; ch0 &= ch0 - 1) // bit-scan
{
const unsigned bit_idx =
(bm::word_bitcount((ch0 & -ch0) - 1));
bit_list[n_bits++] = base_idx + bit_idx;
} // for ch0
} // for j
}
else
#endif
{
for (size_type j = 0; j < imp_size; ++j)
{
unsigned ch = unsigned(ch_slice[j]);
if (ch & mask)
bit_list[n_bits++] = idx_from + j;
} // for j
}
if (n_bits) // set transposed bits to the target plane
{
bvector_type* bv =
this->get_create_slice((unsigned)(char_slice_idx * 8) + bi);
bv->import_sorted(&bit_list[0], n_bits);
}
} // for ch_acc
}
#ifdef _MSC_VER
#pragma warning( pop )
#endif
// ------------------------------------------------------------
/*! @name Errors and exceptions */
///@{
/**
\brief throw range error
\internal
*/
static
void throw_range_error(const char* err_msg);
/**
\brief throw domain error
\internal
*/
static
void throw_bad_value(const char* err_msg);
///@}
/*! \brief set value without checking boundaries */
void set_value(size_type idx, const value_type* str);
/*! \brief set value without checking boundaries or support of NULL */
void set_value_no_null(size_type idx, const value_type* str);
/*! \brief insert value without checking boundaries */
void insert_value(size_type idx, const value_type* str);
/*! \brief insert value without checking boundaries or support of NULL */
void insert_value_no_null(size_type idx, const value_type* str);
size_type size_internal() const { return size(); }
void resize_internal(size_type sz) { resize(sz); }
size_t remap_size() const { return remap_matrix1_.get_buffer().size(); }
const unsigned char* get_remap_buffer() const
{ return remap_matrix1_.get_buffer().buf(); }
unsigned char* init_remap_buffer()
{
remap_matrix1_.init(true);
return remap_matrix1_.get_buffer().data();
}
void set_remap() { remap_flags_ = 1; }
protected:
bool resolve_range(size_type from, size_type to,
size_type* idx_from, size_type* idx_to) const
{
*idx_from = from; *idx_to = to; return true;
}
const remap_matrix_type* get_remap_matrix() const
{ return &remap_matrix1_; }
remap_matrix_type* get_remap_matrix()
{ return &remap_matrix1_; }
protected:
template<class SVect> friend class sparse_vector_serializer;
template<class SVect> friend class sparse_vector_deserializer;
protected:
unsigned remap_flags_; ///< remapping status
slice_octet_matrix_type remap_matrix1_; ///< octet remap table 1
slice_octet_matrix_type remap_matrix2_; ///< octet remap table 2
};
//---------------------------------------------------------------------
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
str_sparse_vector<CharType, BV, STR_SIZE>::str_sparse_vector(
bm::null_support null_able,
allocation_policy_type ap,
size_type bv_max_size,
const allocator_type& alloc)
: parent_type(null_able, ap, bv_max_size, alloc),
remap_flags_(0)
{
static_assert(STR_SIZE > 1,
"BM:: String vector size must be > 1 (to accomodate 0 terminator)");
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
str_sparse_vector<CharType, BV, STR_SIZE>::str_sparse_vector(
const str_sparse_vector& str_sv)
: parent_type(str_sv),
remap_flags_(str_sv.remap_flags_),
remap_matrix1_(str_sv.remap_matrix1_),
remap_matrix2_(str_sv.remap_matrix2_)
{
static_assert(STR_SIZE > 1,
"BM:: String vector size must be > 1 (to accomodate 0 terminator)");
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void str_sparse_vector<CharType, BV, STR_SIZE>::swap(
str_sparse_vector& str_sv) BMNOEXCEPT
{
parent_type::swap(str_sv);
bm::xor_swap(remap_flags_, str_sv.remap_flags_);
remap_matrix1_.swap(str_sv.remap_matrix1_);
remap_matrix2_.swap(str_sv.remap_matrix2_);
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void str_sparse_vector<CharType, BV, STR_SIZE>::set(
size_type idx, const value_type* str)
{
if (idx >= this->size())
this->size_ = idx+1;
set_value(idx, str);
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void str_sparse_vector<CharType, BV, STR_SIZE>::insert(
size_type idx, const value_type* str)
{
if (idx >= this->size())
{
this->size_ = idx+1;
set_value(idx, str);
return;
}
insert_value(idx, str);
this->size_++;
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void str_sparse_vector<CharType, BV, STR_SIZE>::erase(size_type idx)
{
BM_ASSERT(idx < this->size_);
if (idx >= this->size_)
return;
this->erase_column(idx, true);
this->size_--;
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void str_sparse_vector<CharType, BV, STR_SIZE>::set_null(size_type idx)
{
if (bvector_type* bv_null = this->get_null_bvect())
bv_null->clear_bit_no_check(idx);
if (idx >= this->size_)
this->size_ = idx + 1;
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void str_sparse_vector<CharType, BV, STR_SIZE>::set_value(
size_type idx, const value_type* str)
{
set_value_no_null(idx, str);
if (bvector_type* bv_null = this->get_null_bvect())
bv_null->set_bit_no_check(idx);
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void str_sparse_vector<CharType, BV, STR_SIZE>::set_value_no_null(
size_type idx, const value_type* str)
{
for (unsigned i = 0; true; ++i)
{
CharType ch = str[i];
if (!ch)
{
this->clear_value_planes_from(i*8, idx);
return;
}
if (remap_flags_) // compressional re-mapping is in effect
{
auto r = remap_matrix2_.rows();
if (i >= r)
{
remap_matrix1_.resize(i + 1, remap_matrix1_.cols(), true);
remap_matrix2_.resize(i + 1, remap_matrix2_.cols(), true);
}
unsigned char remap_value = remap_matrix2_.get(i, unsigned(ch));
BM_ASSERT(remap_value);
if (!remap_value) // unknown dictionary element
{
this->clear_value_planes_from(i*8, idx);
return;
}
ch = CharType(remap_value);
}
this->bmatr_.set_octet(idx, i, (unsigned char)ch);
} // for i
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void str_sparse_vector<CharType, BV, STR_SIZE>::insert_value(
size_type idx, const value_type* str)
{
insert_value_no_null(idx, str);
this->insert_null(idx, true);
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void str_sparse_vector<CharType, BV, STR_SIZE>::insert_value_no_null(
size_type idx, const value_type* str)
{
for (unsigned i = 0; true; ++i)
{
CharType ch = str[i];
if (!ch)
{
this->insert_clear_value_planes_from(i*8, idx);
return;
}
if (remap_flags_) // compressional re-mapping is in effect
{
unsigned char remap_value = remap_matrix2_.get(i, unsigned(ch));
BM_ASSERT(remap_value);
if (!remap_value) // unknown dictionary element
{
this->insert_clear_value_planes_from(i*8, idx);
return;
}
ch = CharType(remap_value);
}
this->bmatr_.insert_octet(idx, i, (unsigned char)ch);
} // for i
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
typename str_sparse_vector<CharType, BV, STR_SIZE>::size_type
str_sparse_vector<CharType, BV, STR_SIZE>::get(
size_type idx, value_type* str, size_type buf_size) const BMNOEXCEPT
{
size_type i = 0;
for (; true; ++i)
{
if (i >= buf_size)
break;
CharType ch = CharType(this->bmatr_.get_octet(idx, i));
str[i] = ch;
if (!ch)
break;
}
if (remap_flags_)
remap_matrix1_.remap(str, i);
return i;
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void str_sparse_vector<CharType, BV, STR_SIZE>::optimize(
bm::word_t* temp_block,
typename bvector_type::optmode opt_mode,
typename str_sparse_vector<CharType, BV, STR_SIZE>::statistics* st)
{
typename bvector_type::statistics stbv;
parent_type::optimize(temp_block, opt_mode, &stbv);
if (st)
st->add(stbv);
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void str_sparse_vector<CharType, BV, STR_SIZE>::calc_stat(
struct str_sparse_vector<CharType, BV, STR_SIZE>::statistics* st
) const BMNOEXCEPT
{
BM_ASSERT(st);
typename bvector_type::statistics stbv;
parent_type::calc_stat(&stbv);
st->reset();
st->bit_blocks += stbv.bit_blocks;
st->gap_blocks += stbv.gap_blocks;
st->ptr_sub_blocks += stbv.ptr_sub_blocks;
st->bv_count += stbv.bv_count;
st->max_serialize_mem += stbv.max_serialize_mem + 8;
st->memory_used += stbv.memory_used;
st->gap_cap_overhead += stbv.gap_cap_overhead;
size_t remap_mem_usage = sizeof(remap_flags_);
remap_mem_usage += remap_matrix1_.get_buffer().mem_usage();
remap_mem_usage += remap_matrix2_.get_buffer().mem_usage();
st->memory_used += remap_mem_usage;
if (remap_flags_) // use of remapping requires some extra storage
{
st->max_serialize_mem += (remap_mem_usage * 2);
}
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
int str_sparse_vector<CharType, BV, STR_SIZE>::compare(
size_type idx,
const value_type* str) const BMNOEXCEPT
{
BM_ASSERT(str);
int res = 0;
if (remap_flags_)
{
for (unsigned i = 0; true; ++i)
{
CharType octet = str[i];
CharType sv_octet = (CharType)this->bmatr_.get_octet(idx, i);
if (!sv_octet)
{
res = -octet; // -1 || 0
break;
}
const unsigned char* remap_row = remap_matrix1_.row(i);
unsigned char remap_value = remap_row[unsigned(sv_octet)];
BM_ASSERT(remap_value);
res = (remap_value > octet) - (remap_value < octet);
if (res || !octet)
break;
} // for i
}
else
{
for (unsigned i = 0; true; ++i)
{
CharType octet = str[i];
CharType sv_octet = (CharType)this->bmatr_.get_octet(idx, i);
if (!sv_octet)
{
res = -octet; // -1 || 0
break;
}
res = (sv_octet > octet) - (sv_octet < octet);
if (res || !octet)
break;
} // for i
}
return res;
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
unsigned str_sparse_vector<CharType, BV, STR_SIZE>::common_prefix_length(
size_type idx1, size_type idx2) const BMNOEXCEPT
{
unsigned i = 0;
for (; true; ++i)
{
CharType ch1 = CharType(this->bmatr_.get_octet(idx1, i));
CharType ch2 = CharType(this->bmatr_.get_octet(idx2, i));
if (!ch1 || !ch2)
{
if (i)
--i;
break;
}
if (ch1 != ch2)
break;
} // for
return i;
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
bool
str_sparse_vector<CharType, BV, STR_SIZE>::find_rank(
size_type rank,
size_type& pos) BMNOEXCEPT
{
BM_ASSERT(rank);
pos = rank - 1;
return true;
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
typename str_sparse_vector<CharType, BV, STR_SIZE>::size_type
str_sparse_vector<CharType, BV, STR_SIZE>::effective_max_str()
const BMNOEXCEPT
{
return this->bmatr_.octet_size();
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void str_sparse_vector<CharType, BV, STR_SIZE>::calc_octet_stat(
octet_freq_matrix_type& octet_matrix) const
{
size_type max_str_len = effective_max_str();
octet_matrix.resize(max_str_len, 256, false);
octet_matrix.set_zero(); //init(true);
const_iterator it(this);
for(; it.valid(); ++it)
{
const value_type* s = *it; // get asciiz char*
for (unsigned i = 0; true; ++i) // for each char in str
{
value_type ch = s[i];
if (!ch)
break;
typename
octet_freq_matrix_type::value_type* row = octet_matrix.row(i);
unsigned ch_idx = (unsigned char)ch;
row[ch_idx] += 1;
} // for i
} // for it
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void str_sparse_vector<CharType, BV, STR_SIZE>::build_octet_remap(
slice_octet_matrix_type& octet_remap_matrix1,
slice_octet_matrix_type& octet_remap_matrix2,
octet_freq_matrix_type& octet_occupancy_matrix) const
{
size_type max_str_len = effective_max_str();
octet_remap_matrix1.resize(max_str_len, 256, false);
octet_remap_matrix1.set_zero();
octet_remap_matrix2.resize(max_str_len, 256, false);
octet_remap_matrix2.set_zero();
for (unsigned i = 0; i < octet_occupancy_matrix.rows(); ++i)
{
typename octet_freq_matrix_type::value_type* frq_row =
octet_occupancy_matrix.row(i);
unsigned char* remap_row1 = octet_remap_matrix1.row(i);
unsigned char* remap_row2 = octet_remap_matrix2.row(i);
const typename slice_octet_matrix_type::size_type row_size =
octet_occupancy_matrix.cols();
for (unsigned remap_code = 1; true; ++remap_code)
{
typename octet_freq_matrix_type::size_type char_idx;
bool found = bm::find_max_nz(frq_row, row_size, &char_idx);
#if 0
bool found = bm::find_first_nz(frq_row, row_size, &char_idx);
#endif
if (!found)
break;
BM_ASSERT(char_idx);
unsigned char ch = (unsigned char)char_idx;
remap_row1[remap_code] = ch;
remap_row2[ch] = (unsigned char)remap_code;
frq_row[char_idx] = 0; // clear the picked char
} // for
} // for i
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void str_sparse_vector<CharType, BV, STR_SIZE>::recalc_remap_matrix2()
{
BM_ASSERT(remap_flags_);
remap_matrix2_.resize(remap_matrix1_.rows(), remap_matrix1_.cols(), false);
remap_matrix2_.set_zero();
//remap_matrix2_.init(true);
for (unsigned i = 0; i < remap_matrix1_.rows(); ++i)
{
const unsigned char* remap_row1 = remap_matrix1_.row(i);
unsigned char* remap_row2 = remap_matrix2_.row(i);
for (unsigned j = 1; j < remap_matrix1_.cols(); ++j)
{
if (remap_row1[j])
{
unsigned ch_code = remap_row1[j];
remap_row2[ch_code] = (unsigned char)j;
BM_ASSERT(ch_code < 256);
}
} // for j
} // for i
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
bool str_sparse_vector<CharType, BV, STR_SIZE>::remap_tosv(
value_type* BMRESTRICT sv_str,
size_type buf_size,
const value_type* BMRESTRICT str,
const slice_octet_matrix_type& BMRESTRICT octet_remap_matrix2) BMNOEXCEPT
{
for (unsigned i = 0; i < buf_size; ++i)
{
CharType ch = str[i];
if (!ch)
{
sv_str[i] = ch;
break;
}
const unsigned char* remap_row = octet_remap_matrix2.row(i);
unsigned char remap_value = remap_row[unsigned(ch)];
if (!remap_value) // unknown dictionary element
return false;
sv_str[i] = CharType(remap_value);
} // for i
return true;
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned MAX_STR_SIZE>
bool str_sparse_vector<CharType, BV, MAX_STR_SIZE>::remap_fromsv(
value_type* BMRESTRICT str,
size_type buf_size,
const value_type* BMRESTRICT sv_str,
const slice_octet_matrix_type& BMRESTRICT octet_remap_matrix1
) BMNOEXCEPT
{
for (unsigned i = 0; i < buf_size; ++i)
{
CharType ch = sv_str[i];
if (!ch)
{
str[i] = ch;
break;
}
const unsigned char* remap_row = octet_remap_matrix1.row(i);
unsigned char remap_value = remap_row[unsigned(ch)];
if (!remap_value) // unknown dictionary element
return false;
str[i] = CharType(remap_value);
} // for i
return true;
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned MAX_STR_SIZE>
void str_sparse_vector<CharType, BV, MAX_STR_SIZE>::remap()
{
// TODO: get rid of tmp, implement a move-remapping
str_sparse_vector<CharType, BV, MAX_STR_SIZE>
sv_tmp(this->get_null_support());
sv_tmp.remap_from(*this);
sv_tmp.swap(*this);
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void
str_sparse_vector<CharType, BV, STR_SIZE>::remap_from(
const str_sparse_vector& str_sv)
{
if (str_sv.is_remap())
{
*this = str_sv;
return;
}
this->clear_all(true);
if (str_sv.empty()) // no content to remap
return;
octet_freq_matrix_type omatrix; // occupancy map
str_sv.calc_octet_stat(omatrix);
str_sv.build_octet_remap(remap_matrix1_, remap_matrix2_, omatrix);
remap_flags_ = 1; // turn ON remapped mode
const unsigned buffer_size = ins_buf_size; // 1024 * 8;
typedef bm::dynamic_heap_matrix<CharType, allocator_type> buffer_matrix_type;
size_type str_len = str_sv.effective_max_str();
//remap_buffer_type cmatr(true);
buffer_matrix_type cmatr(buffer_size, str_len);
cmatr.init(true); // init and set zero
for (size_type i = 0; true; )
{
size_type dsize = str_sv.decode(cmatr, i, buffer_size, true);
if (!dsize)
break;
this->import(cmatr, i, dsize);
i += dsize;
} // for i
if (bvector_type* bv_null = this->get_null_bvect())
{
const bvector_type* bv_null_arg = str_sv.get_null_bvector();
if (bv_null_arg)
*bv_null = *bv_null_arg;
else
{
// TODO: exception? assert? maybe it is OK...
}
}
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void str_sparse_vector<CharType, BV, STR_SIZE>::sync(bool /*force*/)
{
if (remap_flags_)
{
recalc_remap_matrix2();
}
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
bool str_sparse_vector<CharType, BV, STR_SIZE>::equal(
const str_sparse_vector<CharType, BV, STR_SIZE>& sv,
bm::null_support null_able) const BMNOEXCEPT
{
// at this point both vectors should have the same remap settings
// to be considered "equal".
// Strictly speaking this is incorrect, because re-map and non-remap
// vectors may have the same content
if (remap_flags_ != sv.remap_flags_)
return false;
if (remap_flags_)
{
// TODO: equal matrix dimention overlap may be not enough
// (check the non-overlap to be zero)
// dimentionality shrink is a result of de-serialization
bool b;
b = remap_matrix1_.equal_overlap(sv.remap_matrix1_);
if (!b)
return b;
b = remap_matrix2_.equal_overlap(sv.remap_matrix2_);
if (!b)
return b;
}
return parent_type::equal(sv, null_able);
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void str_sparse_vector<CharType, BV, STR_SIZE>::copy_range(
const str_sparse_vector<CharType, BV, STR_SIZE>& sv,
size_type left, size_type right,
bm::null_support slice_null)
{
if (left > right)
bm::xor_swap(left, right);
this->clear_all(true);
remap_flags_ = sv.remap_flags_;
remap_matrix1_ = sv.remap_matrix1_;
remap_matrix2_ = sv.remap_matrix2_;
this->copy_range_slices(sv, left, right, slice_null);
this->resize(sv.size());
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
str_sparse_vector<CharType, BV, STR_SIZE>&
str_sparse_vector<CharType, BV, STR_SIZE>::merge(
str_sparse_vector<CharType, BV, STR_SIZE>& str_sv)
{
size_type arg_size = str_sv.size();
if (this->size_ < arg_size)
resize(arg_size);
// there is an assumption here that we only need to copy remap flags once
// because we merge matrices with the same remaps
// otherwise - undefined behavior
//
if (remap_flags_ != str_sv.remap_flags_)
{
remap_flags_ = str_sv.remap_flags_;
remap_matrix1_ = str_sv.remap_matrix1_;
remap_matrix2_ = str_sv.remap_matrix2_;
}
bvector_type* bv_null = this->get_null_bvect();
this->merge_matr(str_sv.bmatr_);
// our vector is NULL-able but argument is not (assumed all values are real)
if (bv_null && !str_sv.is_nullable())
bv_null->set_range(0, arg_size-1);
return *this;
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void str_sparse_vector<CharType, BV, STR_SIZE>::keep_range(
size_type left, size_type right,
bm::null_support slice_null)
{
if (right < left)
bm::xor_swap(left, right);
this->keep_range_no_check(left, right, slice_null);
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
typename str_sparse_vector<CharType, BV, STR_SIZE>::const_iterator
str_sparse_vector<CharType, BV, STR_SIZE>::begin() const BMNOEXCEPT
{
typedef typename
str_sparse_vector<CharType, BV, STR_SIZE>::const_iterator it_type;
return it_type(this);
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void str_sparse_vector<CharType, BV, STR_SIZE>::clear_all(
bool free_mem) BMNOEXCEPT
{
parent_type::clear_all(free_mem);
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void str_sparse_vector<CharType, BV, STR_SIZE>::throw_range_error(
const char* err_msg)
{
#ifndef BM_NO_STL
throw std::range_error(err_msg);
#else
BM_ASSERT_THROW(false, BM_ERR_RANGE);
#endif
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void str_sparse_vector<CharType, BV, STR_SIZE>::throw_bad_value(
const char* err_msg)
{
#ifndef BM_NO_STL
if (!err_msg)
err_msg = "Unknown/incomparable dictionary character";
throw std::domain_error(err_msg);
#else
BM_ASSERT_THROW(false, BM_BAD_VALUE);
#endif
}
//---------------------------------------------------------------------
//
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
str_sparse_vector<CharType, BV, STR_SIZE>::const_iterator::const_iterator() BMNOEXCEPT
: sv_(0), substr_from_(0), substr_to_(STR_SIZE),
pos_(bm::id_max), pos_in_buf_(~size_type(0))
{
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
str_sparse_vector<CharType, BV, STR_SIZE>::const_iterator::const_iterator(
const str_sparse_vector<CharType, BV, STR_SIZE>::const_iterator& it) BMNOEXCEPT
: sv_(it.sv_),
substr_from_(it.substr_from_), substr_to_(it.substr_to_),
pos_(it.pos_), pos_in_buf_(~size_type(0))
{
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
str_sparse_vector<CharType, BV, STR_SIZE>::const_iterator::const_iterator(
const str_sparse_vector<CharType, BV, STR_SIZE>* sv) BMNOEXCEPT
: sv_(sv), pos_(sv->empty() ? bm::id_max : 0), pos_in_buf_(~size_type(0))
{
substr_from_ = 0;
substr_to_ = (unsigned) sv_->effective_max_str();
buf_matrix_.resize(n_rows, substr_to_+1);
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
str_sparse_vector<CharType, BV, STR_SIZE>::const_iterator::const_iterator(
const str_sparse_vector<CharType, BV, STR_SIZE>* sv,
typename str_sparse_vector<CharType, BV, STR_SIZE>::size_type pos) BMNOEXCEPT
: sv_(sv), pos_(pos >= sv->size() ? bm::id_max : pos), pos_in_buf_(~size_type(0))
{
substr_from_ = 0;
substr_to_ = (unsigned) sv_->effective_max_str();
buf_matrix_.resize(n_rows, substr_to_+1);
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void str_sparse_vector<CharType, BV, STR_SIZE>::const_iterator::set_substr(
unsigned from, unsigned len) BMNOEXCEPT
{
unsigned max_str = sv_->effective_max_str();
substr_from_ = from;
if (!len)
{
len = 1 + max_str - from;
substr_to_ = from + len;
}
else
{
// TODO: check for overflow
substr_to_ = substr_from_ + (len - 1);
}
if (max_str < substr_to_)
substr_to_ = max_str;
buf_matrix_.resize(n_rows, len+1, false/*no content copy*/);
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
const typename str_sparse_vector<CharType, BV, STR_SIZE>::value_type*
str_sparse_vector<CharType, BV, STR_SIZE>::const_iterator::value() const
{
BM_ASSERT(sv_);
BM_ASSERT(this->valid());
if (pos_in_buf_ == ~size_type(0))
{
if (!buf_matrix_.is_init())
buf_matrix_.init();
pos_in_buf_ = 0;
size_type d = sv_->decode_substr(buf_matrix_, pos_, n_rows,
substr_from_, substr_to_);
if (!d)
{
pos_ = bm::id_max;
return 0;
}
}
return buf_matrix_.row(pos_in_buf_);
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void
str_sparse_vector<CharType, BV, STR_SIZE>::const_iterator::go_to(
typename str_sparse_vector<CharType, BV, STR_SIZE>::size_type pos
) BMNOEXCEPT
{
pos_ = (!sv_ || pos >= sv_->size()) ? bm::id_max : pos;
pos_in_buf_ = ~size_type(0);
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void
str_sparse_vector<CharType, BV, STR_SIZE>::const_iterator::advance() BMNOEXCEPT
{
if (pos_ == bm::id_max) // nothing to do, we are at the end
return;
++pos_;
if (pos_ >= sv_->size())
this->invalidate();
else
{
if (pos_in_buf_ != ~size_type(0))
{
++pos_in_buf_;
if (pos_in_buf_ >= n_rows)
pos_in_buf_ = ~size_type(0);
}
}
}
//---------------------------------------------------------------------
//
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
str_sparse_vector<CharType, BV, STR_SIZE>::back_insert_iterator::back_insert_iterator() BMNOEXCEPT
: sv_(0), bv_null_(0), pos_in_buf_(~size_type(0)), prev_nb_(0)
{}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
str_sparse_vector<CharType, BV, STR_SIZE>::back_insert_iterator::back_insert_iterator(
str_sparse_vector<CharType, BV, STR_SIZE>* sv) BMNOEXCEPT
: sv_(sv), pos_in_buf_(~size_type(0))
{
if (sv)
{
prev_nb_ = sv_->size() >> bm::set_block_shift;
bv_null_ = sv_->get_null_bvect();
unsigned esize = (unsigned) sv_->effective_max_str();
if (esize < STR_SIZE)
esize = STR_SIZE;
buf_matrix_.init_resize(n_buf_size, esize);
}
else
{
bv_null_ = 0; prev_nb_ = 0;
}
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
str_sparse_vector<CharType, BV, STR_SIZE>::back_insert_iterator::back_insert_iterator(
const str_sparse_vector<CharType, BV, STR_SIZE>::back_insert_iterator& bi) BMNOEXCEPT
: sv_(bi.sv_), bv_null_(bi.bv_null_), buf_matrix_(bi.buf_matrix_.rows(), bi.buf_matrix_.cols()),
pos_in_buf_(~size_type(0)), prev_nb_(bi.prev_nb_)
{
BM_ASSERT(bi.empty());
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
str_sparse_vector<CharType, BV, STR_SIZE>::back_insert_iterator::~back_insert_iterator()
{
this->flush();
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
bool
str_sparse_vector<CharType, BV, STR_SIZE>::back_insert_iterator::empty()
const BMNOEXCEPT
{
return (pos_in_buf_ == ~size_type(0) || !sv_);
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void str_sparse_vector<CharType, BV, STR_SIZE>::back_insert_iterator::flush()
{
if (this->empty())
return;
size_type imp_idx = sv_->size();
sv_->import_no_check(buf_matrix_, imp_idx, pos_in_buf_+1, false);
pos_in_buf_ = ~size_type(0);
block_idx_type nb = sv_->size() >> bm::set_block_shift;
if (nb != prev_nb_)
{
// optimize all previous blocks in all planes
sv_->optimize_block(prev_nb_);
prev_nb_ = nb;
}
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void str_sparse_vector<CharType, BV, STR_SIZE>::back_insert_iterator::add(
const typename str_sparse_vector<CharType, BV, STR_SIZE>::back_insert_iterator::value_type* v)
{
if (!v)
{
this->add_null();
return;
}
size_type buf_idx = this->pos_in_buf_; // offset in
size_type sz = sv_->size();
this->add_value(v);
if (bv_null_)
bv_null_->set_bit_no_check(sz + buf_idx + 1);
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void str_sparse_vector<CharType, BV, STR_SIZE>::back_insert_iterator::add_null()
{
/*size_type buf_idx = */this->add_value("");
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void str_sparse_vector<CharType, BV, STR_SIZE>::back_insert_iterator::add_null(
typename str_sparse_vector<CharType, BV, STR_SIZE>::back_insert_iterator::size_type count)
{
for (size_type i = 0; i < count; ++i) // TODO: optimization
this->add_value("");
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void
str_sparse_vector<CharType, BV, STR_SIZE>::back_insert_iterator::add_value(
const str_sparse_vector<CharType, BV, STR_SIZE>::back_insert_iterator::value_type* v)
{
BM_ASSERT(sv_);
BM_ASSERT(v);
BM_ASSERT(buf_matrix_.rows()>0);
if (pos_in_buf_ >= buf_matrix_.rows()-1)
{
if (pos_in_buf_ == ~size_type(0) && (!buf_matrix_.is_init()))
buf_matrix_.init();
else
this->flush();
pos_in_buf_ = 0; buf_matrix_.set_zero();
}
else
{
++pos_in_buf_;
}
value_type* r = buf_matrix_.row(pos_in_buf_);
typename buffer_matrix_type::size_type i;
typename buffer_matrix_type::size_type cols = buf_matrix_.cols();
for (i = 0; i < cols; ++i)
{
r[i] = v[i];
if (!r[i])
return;
} // for i
// string is longer than the initial size, matrix resize is needed
for (cols = i; true; ++cols) // find the new length
{
if (!v[cols])
break;
} // for cols
// cols is now string length and the new mattrix size parameter
buf_matrix_.resize(buf_matrix_.rows(), cols + 1);
r = buf_matrix_.row(pos_in_buf_);
cols = buf_matrix_.cols();
for (; i < cols; ++i)
{
r[i] = v[i];
if (!r[i])
return;
} // for i
BM_ASSERT(0);
}
//---------------------------------------------------------------------
template<class CharType, class BV, unsigned STR_SIZE>
void str_sparse_vector<CharType, BV, STR_SIZE>::sync_size() BMNOEXCEPT
{
const bvector_type* bv_null = this->get_null_bvector();
if (!bv_null)
return;
bool found = bv_null->find_reverse(this->size_);
this->size_ += found;
}
//---------------------------------------------------------------------
} // namespace
#endif