Featured image of post Bag Collection Library C++

Bag Collection Library C++

Bag Collection Library in C++ as a Compile-Time Template Meta Program

Template Metaprogramming-Wikipedia

Whats a bag?

LanguageDescriptionSource
JavaJava Collections Framework does not include a bag interface by default. BUT! Apache Commons Collections provide a Bag interface with implementations like HashBag and TreeBagWikipedia - Set (abstract data type)
PythonPython’s collections.Counter class can be used as a bag, and the collections-extended library offers a bag class with multiset features.Collections Extended
C#.NET includes a Bag collection within System.Collections.Concurrent, which provides a thread-safe implementation for storing element counts. You can also use a List and just do type checking at runtime and use the visitor pattern. Which is not as efficient as this library, but it works.Wikipedia - Comparison of C# and Java
SmalltalkSmalltalk has a built-in Bag class that allows duplicates and provides methods for adding, removing, and counting elements.Wikipedia - Set (abstract data type)

Bag Explained

A bag, also known as a multiset, is a collection data structure that allows multiple occurrences of its elements without any particular order.

The bag my library was inspired by, was actually inspired by the runtime type design patterns in MFC.
https://es.wikipedia.org/wiki/Microsoft_Foundation_Classes

Which in turn was likely inspired by the multiset collection in Small talk , otherwise known as a bag.

Why a bag? because you can put anything in it you want!

With a normal dynamic language, you can do this with a List type of collection.
The disadvantage is you have to query the type at runtime.

With my bag collection- you have to tell me at compile time what is possible to be in the bag. The templates will generate the proper collection at compile time.

My bag library is more efficient, since you do not have to do any runtime type, but the disadvantage is you have to use a Visitor Pattern pattern to operate on the contents.

SourceTitleLink
WikipediaSet (abstract data type)Link
Collections ExtendedBags (Multisets) — collections_extended 2.0.2 documentationLink
WikipediaComparison of C# and JavaLink
AlexOmegaPyUnderstanding the Bag ADT in Java: A Flexible Data StructureLink
DZoneThe Bag Data Structure From Eclipse CollectionsLink
DEV CommunityThree Implementations of a Bag in PythonLink
Princeton University1.3 Bags, Queues, and StacksLink
Oregon State UniversityChapter 8: Bags and SetsLink
WikipediaCollection (abstract data type)Link
EducativeThe Data Collection BagLink
Oxford UniversityLECTURE 14: BAGS (MULTISETS)Link
TutorialsPointHibernate - Bag MappingsLink
ACM Digital LibraryTutorial: Languages for Collection TypesLink

Bag Source Code- Compile Time Meta Program

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949

#ifndef __BOOST_BAG_BAG_HPP
#define __BOOST_BAG_BAG_HPP

/*
 *
 *  Brian Braatz.
 *  Copyright 2003-2022 Brian C Braatz. All rights reserved.
 *
 */

#ifndef BOOST_MPL_MAP_HPP_INCLUDED
	#include <boost/mpl/map.hpp>
#endif

#include <boost/type_traits/is_reference.hpp>
#include <boost/type_traits/is_same.hpp>
#ifndef BOOST_UTILITY_ENABLE_IF_HPP
	#include <boost/utility/enable_if.hpp>
#endif
	#include <boost/mpl/has_xxx.hpp>
#ifndef BOOST_MPL_IF_HPP_INCLUDED
	#include <boost/mpl/if.hpp>
#endif

#include <boost/mpl/count_if.hpp>

#ifndef BOOST_TT_IS_BASE_AND_DERIVED_HPP_INCLUDED
	#include <boost/type_traits/is_base_and_derived.hpp>
#endif

// #include <boost/type_traits.hpp>
// #ifndef BOOST_TT_IS_SAME_HPP_INCLUDED
	#include <boost/type_traits/is_same.hpp>
// #endif


#include <boost/bag/detail/util.hpp>

// #ifndef BOOST_MPL_VECTOR_HPP_INCLUDED
	#include <boost/mpl/vector.hpp>
// #endif

#include <boost/mpl/transform.hpp>

#ifndef BOOST_UTILITY_ENABLE_IF_HPP
	#include <boost/utility/enable_if.hpp>
#endif

#ifndef BOOST_MPL_APPLY_WRAP_HPP_INCLUDED
	#include <boost/mpl/apply_wrap.hpp>
#endif

#ifndef BOOST_MPL_FIND_IF_HPP_INCLUDED
	#include <boost/mpl/find_if.hpp>
#endif
	
#ifndef BOOST_MPL_RANGE_C_HPP_INCLUDED
	#include <boost/mpl/range_c.hpp>
#endif

#ifndef BOOST_MPL_SIZE_HPP_INCLUDED
	#include <boost/mpl/size.hpp>
#endif
#include <boost/mpl/greater.hpp>

#ifndef BOOST_MPL_TRANSFORM_VIEW_HPP_INCLUDED
	#include <boost/mpl/transform_view.hpp>
#endif

#ifndef BOOST_MPL_FILTER_VIEW_HPP_INCLUDED
	#include <boost/mpl/filter_view.hpp>
#endif

#ifndef BOOST_MPL_ZIP_VIEW_HPP_INCLUDED
	#include <boost/mpl/zip_view.hpp>
#endif

#ifndef BOOST_MPL_INHERIT_HPP_INCLUDED
	#include <boost/mpl/inherit.hpp>
#endif

#ifndef BOOST_MPL_INHERIT_FRONT_TO_BACK_HPP_INCLUDED
	#include <boost/mpl/inherit_linearly.hpp>
#endif

#ifndef BOOST_MPL_UNPACK_ARGS_HPP_INCLUDED
	#include <boost/mpl/unpack_args.hpp>
#endif

#include <boost/mpl/less_equal.hpp>

#ifndef BOOST_MPL_CONTAINS_HPP_INCLUDED
	#include <boost/mpl/contains.hpp>
#endif

#ifndef BOOST_MPL_LAMBDA_HPP_INCLUDED
	#include <boost/mpl/lambda.hpp>
#endif
	#include <boost/mpl/filter_view.hpp>
	#include <boost/mpl/and.hpp>
	#include <boost/mpl/at.hpp>
#include <boost/mpl/assert.hpp>

#include <boost/mpl/joint_view.hpp>

#include <boost/mpl/greater.hpp>

#include <boost/bind.hpp>
#include <boost/iterator/transform_iterator.hpp>
#include <boost/shared_ptr.hpp>

#include <boost/ptr_container/ptr_vector.hpp>
#include <boost/mpl/set.hpp>
#include <boost/mpl/copy.hpp>
#include <boost/mpl/copy_if.hpp>

#include <vector>

#include <boost/mpl/vector_c.hpp>

#include <boost/mpl/equal_to.hpp>
#include <boost/mpl/not_equal_to.hpp>
#include <boost/mpl/less.hpp>
#include <boost/mpl/find_if.hpp>
#include <boost/mpl/count_if.hpp>
#include <boost/mpl/range_c.hpp>


namespace boost
{

namespace bag
{

namespace _mpl_ = boost::mpl;

// custom item filter for filtering pointer
template <class internal_t, class external_t>
struct ptr_filter
{	
	typedef ptr_filter  type;
	typedef external_t& result_type ;

	external_t & operator()( internal_t & v) const
	{	
		return (*v);
	}

	external_t & internal_to_external( internal_t & v) const
	{	
		return (*v);
	}
	internal_t external_to_internal( external_t &v) const
	{	
		return (&v);
	}
};

// Static item filter for custom items
template <class custom_item_t>
struct custom_item_filter
{
	typedef typename custom_item_t::impl_type	internal_t;
	typedef typename custom_item_t::arg_type	external_t;
	
	struct filter : custom_item_t
	{	
		typedef filter  type;
		typedef typename custom_item_t::return_type result_type;
		
		result_type & operator()( internal_t v) const
		{	
			return custom_item_t::impl_to_val(v);
		}

		external_t & internal_to_external( internal_t & v) const
		{	
			return custom_item_t::impl_to_val(v);
		}
		internal_t external_to_internal( external_t &v) const
		{	
			return custom_item_t::val_to_impl(v);
		}
	};
};
	
// vector style container
// holds pointers to objects of type T, but provides an interface as if by reference\value
// does notthing more than re-direct the interface
// i.e. items held by ptr- are NOT freed
//
template <class internal_t,class external_t, class arg_t,  class filter_t >
class filtered_vector : filter_t
{
	typedef std::vector<internal_t> vector_base_type;
	typedef filter_t filter_type;
	
	public:
		typedef vector_base_type		base_type;

		typedef boost::transform_iterator< filter_type ,typename vector_base_type::iterator > trans_iter_type;
		
		struct iterator : trans_iter_type
		{
			typedef iterator type;
			iterator()
			: trans_iter_type(typename vector_base_type::iterator() )
			{}
			iterator(typename vector_base_type::iterator it)
			: trans_iter_type(it )
			{}
			
		};
		typedef filtered_vector							type;
		typedef typename vector_base_type::allocator_type	allocator_type;
		typedef external_t								value_type;
		typedef typename vector_base_type::size_type			size_type;
	private:
		
		vector_base_type  m_vec;

	public:
		base_type & base()	// todo- implement base() function in multiple<by_val>
		{
			return m_vec;
		}
		void clear()
		{
			m_vec.clear();
		}
		void push_front(arg_t obj)
		{
			m_vec.push_front( filter_type::external_to_internal( obj ) );
		}
		void push_back(arg_t obj)
		{
			m_vec.push_back(filter_type::external_to_internal( obj )  );
		}
		void insert( iterator where, value_type& obj)
		{		
			m_vec.insert(where.base(),filter_type::external_to_internal( obj )  );
		}
		void insert( iterator where, size_type count, value_type& obj)
		{
			m_vec.insert(where.base(),count, filter_type::external_to_internal( obj )  );
		}
		value_type & at(size_type pos)
		{
			return filter_type::internal_to_external( m_vec.at(pos) );
		}
		value_type & operator[]( size_type pos)
		{
			return at(pos);
		}
		size_type capacity() const
		{
			return m_vec.capacity();
		}
		size_type size() 
		{
			return m_vec.size();
		}
		iterator erase( iterator where)
		{
			return iterator( m_vec.erase( where.base() ) );
		}	
		iterator begin()
		{
			return iterator(m_vec.begin());
		}                                                                   
	
		iterator end()
		{
			return iterator(m_vec.end());
		}                                                                   
};    

	template<typename T>
	struct shared_ptr_to_ref
	{	
		typedef shared_ptr_to_ref		type;
		typedef T&						result_type ;

		result_type operator()( boost::shared_ptr<T>& v) const
		{	
			return (*v);
		}
	};
  
// vector style container
// takes shared_ptr on push_front,push_back, & insert
// returns the dereferenced version of the shared_ptr on access (i.e. at() operator[] and via iterators)
template <class T>
class shared_ptr_vector
{
		typedef std::vector<boost::shared_ptr<T> > vector_ptrs;
	public:
		typedef shared_ptr_vector					 type;
		typedef typename vector_ptrs::allocator_type allocator_type;
		typedef boost::shared_ptr<T>				value_type;
		typedef T									return_type;
		
		typedef typename vector_ptrs::size_type size_type;

		struct iterator : boost::transform_iterator<shared_ptr_to_ref<T> ,typename vector_ptrs::iterator >
		{
			iterator()
			: boost::transform_iterator<shared_ptr_to_ref<T> ,typename vector_ptrs::iterator >(typename  vector_ptrs::iterator() )
			{}
			iterator(typename vector_ptrs::iterator it)
			: boost::transform_iterator<shared_ptr_to_ref<T> ,typename vector_ptrs::iterator >(it )
			{}
			
		};
	private:
		
		vector_ptrs  m_vec;

	public:
		void clear()
		{
			m_vec.clear();
		}
		void push_front(value_type  obj)
		{
			m_vec.push_back(obj);
		}
	
		void push_back(value_type  obj)
		{
			m_vec.push_back(obj);
		}
		void insert( iterator where, value_type  obj )
		{
			m_vec.insert(where.base(),obj);
		}
		void insert( iterator where, size_type count, value_type& obj)
		{
			m_vec.insert(where.base(),count, obj);
		}
		return_type & at(size_type pos)
		{
			return *(m_vec.at(pos));
		}
		return_type & operator[]( size_type pos)
		{
			return *(m_vec[pos]);
		}
		size_type capacity() const
		{
			return m_vec.capacity();
		}
		size_type size() 
		{
			return m_vec.size();
		}
		iterator erase( iterator where)
		{
			return iterator( m_vec.erase( where.base() ) );
		}	
		iterator begin()
		{
			return iterator(m_vec.begin());
		}                                                                   
	
		iterator end()
		{
			return iterator(m_vec.end());
		}                                                                   
};    

////////////////////////////////////////////////////////////////
// concepts
////////////////////////////////////////////////////////////////

// declarative concepts
// simliar in principle to aspects, but turned inside out
// instead of applying the aspect to an object and not touching the object
// you place metadata about the class into the class itself
// you can then filter on these concepts
// it is "aspects" without the weave

namespace detail
{
	BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(defines_concepts_impl, concepts, false)
};


// returns true if the passed type has a "concepts" defined as a nested name
// will return false is the passed type does not define a concepts typedef
template <  class T0  = _mpl_::void_,class XX  = _mpl_::void_>
struct defines_concepts;

template <>
struct defines_concepts<>
{
	template <class T, class Enable = void> 
	struct apply : _mpl_::false_
	{};

	template <class T>
	struct apply<T, typename boost::enable_if< detail::defines_concepts_impl<T>  >::type> 
	:	_mpl_::true_
	{};
};

template < class T> 
struct defines_concepts<T>
: _mpl_::apply1<defines_concepts <>, T>::type 
{
};

// returns true if the passed type "has" a given type in it's concept list
// will return false if either the type in question does NOT have the target concept in the concepts list
//		OR if the type in question does not have concepts at all
template < class C0  = _mpl_::void_, class T0  = _mpl_::void_,class XX  = _mpl_::void_>
struct has_concept;

template < class C0> 
struct has_concept<C0>
{
	template <class T, class Enable = void> 
	struct apply : _mpl_::false_
	{};

	template <class T>
	struct apply<T, typename boost::enable_if< defines_concepts<T>  >::type> 
	:	_mpl_::if_
		<
			_mpl_::contains
			< 
				typename T::concepts,
				C0
			>,
			_mpl_::true_,
			_mpl_::false_
		>
	{ };
	typedef has_concept type;

};

template < class C0, class T> 
struct has_concept<C0,T>
: _mpl_::apply1<has_concept <C0>, T>::type 
{
};

// returns false if the passed type "has" a given type in it's concept list
// will return true if either the type in question does NOT have the target concept in the concepts list
//		OR if the type in question does not have concepts at all
template < class C0  = _mpl_::void_, class T0  = _mpl_::void_,class XX  = _mpl_::void_>
struct not_has_concept;

template < class C0> 
struct not_has_concept<C0>
{
	template <class T, class Enable = void> 
	struct apply : _mpl_::true_
	{};

	template <class T>
	struct apply<T, typename boost::enable_if< defines_concepts<T>  >::type> 
	:	_mpl_::if_
		<
			_mpl_::contains
			< 
				typename T::concepts,
				C0
			>,
			_mpl_::false_,
			_mpl_::true_
		>
	{ };
	typedef not_has_concept type;

};

template < class C0, class T> 
struct not_has_concept<C0,T>
: _mpl_::apply1<not_has_concept <C0>, T>::type 
{
};

// has_any_concept<> which will give a match if any of the concepts match - will take a sequence
// returns true if the passed type "has" a given type in it's concept list
// will return false if either the type in question does NOT have the target concept in the concepts list
//		OR if the type in question does not have concepts at all
template < class SEQ0  = _mpl_::void_, class T0  = _mpl_::void_,class XX  = _mpl_::void_>
struct has_any_concept;

template < class SEQ0> 
struct has_any_concept<SEQ0>
{
	template <class T, class Enable = void> 
	struct apply : _mpl_::false_
	{};

	template <class T>
	struct apply<T, typename boost::enable_if< defines_concepts<T>  >::type> 
	:	_mpl_::if_
		<
			_mpl_::greater
			< 
				_mpl_::filter_view
				< 
					typename T::concepts ,
					_mpl_::contains<SEQ0, _mpl_::_1 >
				>, 
				_mpl_::int_<0> 
			>, 
			_mpl_::true_,
			_mpl_::false_
		>
	{ };
	typedef has_any_concept type;
};

template < class SEQ0, class T> 
struct has_any_concept<SEQ0,T>
: _mpl_::apply1<has_any_concept <SEQ0>, T>::type 
{
};

// has_any_concept<> which will give a match if any of the concepts match - will take a sequence
// returns false if the passed type "has" a given type in it's concept list
// will return true if either the type in question does NOT have the target concept in the concepts list
//		OR if the type in question does not have concepts at all
template < class SEQ0  = _mpl_::void_, class T0  = _mpl_::void_,class XX  = _mpl_::void_>
struct not_has_any_concept;

template < class SEQ0> 
struct not_has_any_concept<SEQ0>
{
	template <class T, class Enable = void> 
	struct apply : _mpl_::true_
	{};

	template <class T>
	struct apply<T, typename boost::enable_if< defines_concepts<T>  >::type> 
	:	_mpl_::if_
		<
			_mpl_::greater
			< 
				_mpl_::filter_view
				< 
					typename T::concepts ,
					_mpl_::contains<SEQ0, _mpl_::_1 >
				>, 
				_mpl_::int_<0> 
			>, 
			_mpl_::false_,
			_mpl_::true_
		>
	{ };
	typedef not_has_any_concept type;
};

template < class SEQ0, class T> 
struct not_has_any_concept<SEQ0,T>
: _mpl_::apply1<not_has_any_concept <SEQ0>, T>::type 
{
};


// returns the concepts associated with a given type
// will return an empty vector<> if the type does not have a concept
template < class C0  = _mpl_::void_, class T0  = _mpl_::void_,class XX  = _mpl_::void_>
struct get_concepts;

template < > 
struct get_concepts<>
{
	// T does not have an existing concepts
	template <class target_t, class Enable = void> 
	struct apply : _mpl_::vector0<>
	{};

	template <class target_t>
	struct apply<target_t, typename boost::enable_if< defines_concepts<target_t>  >::type> 
	:   target_t::concepts
	{ 
		
	};
};

template < class target_t> 
struct get_concepts<target_t>
: _mpl_::apply1<get_concepts<>, target_t>::type 
{
};

////////////////////////////////////////////////////////////////
// concepts defined
////////////////////////////////////////////////////////////////
// concepts
struct modifier {};
struct local_modifier {};
struct global_modifier {};

////////////////////////////////////////////////////////////////
// target
////////////////////////////////////////////////////////////////
namespace detail
{
	BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(has_target_type_impl, target_type, false)
};
// default case returns the type
template <class data_t, class Enable = void> 
struct target
{
	typedef data_t	type;
};	

// if the type HAS a target type- then return it
template <class data_t>
struct target<data_t, typename boost::enable_if< detail::has_target_type_impl< data_t >  >::type> 
{  
	typedef typename data_t::target_type	type;
};

////////////////////////////////////////////////////////////////
// key
////////////////////////////////////////////////////////////////

namespace detail
{
	// returns true if the target defines a "key_type" typedef
	BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(has_key_type_impl, key_type, false)
};


// returns either the "key_type" typedef in the passed type
// or returns the type itself if the key_type is not present
// will always return a non-reference type
template <class data_t, class Enable = void> 
struct key
{
	typedef typename boost::remove_reference<data_t>::type	type;
};	
template <class data_t>
struct key<data_t, typename boost::enable_if< detail::has_key_type_impl< data_t >  >::type> 
{  
	typedef typename boost::remove_reference<typename data_t::key_type>::type	type;
};

// mfc for returning the key of a type using the key<> template
struct key_of
{
	template <class data_t>
	struct apply
	{
		typedef typename key<data_t>::type	type;
	};
};


// returns the index of the passed type
template <class data_t>
struct index_
{
	typedef typename data_t::type::idx_type	type;
};	
// returns the instance of the passed type
template <class data_t>
struct instance
{
	typedef typename data_t::instance_type	type;
};	

// returns the implementation of the passed type
template <class data_t>
struct impl
{
	typedef typename data_t::impl_type	type;
};	

// returns the storage of the passed type
template <class data_t>
struct storage
{
	typedef typename data_t::impl_type::storage_type	type;
};	

// mfc returns the arg type from a data<> class
struct arg_of
{
	template <class data_t>
	struct apply
	{
		typedef typename impl<data_t>::type::arg_type	type;
	};
};

// mfc for returning the index
struct index_of
{
	template <class data_t>
	struct apply
	{
		typedef typename index_<data_t>::type	type;
	};
};
// mfc for returning the implementation
struct impl_of
{
	template <class data_t>
	struct apply
	{
		typedef typename impl<data_t>::type	type;
	};
};
// mfc for returning the instance
struct instance_of
{
	template <class data_t>
	struct apply
	{
		typedef typename instance<data_t>::type	type;
	};
};
// mfc for returning the storage of a type
struct storage_of
{
	template <class data_t>
	struct apply
	{
		typedef typename storage<data_t>::type	type;
	};
};
// where how_t is a unary metafunction class and arg_t is a param to that mfc
// returns true if the value_t passed type passes the evaulation of how_t
// the internal apply allows for passing does_match to a algorithm intended to iterate or filter
// a compile time sequence
template <class how_t, class value_t>
struct does_match
{
	template <class arg_t>
	struct apply
	: boost::is_same< typename _mpl_::apply1< how_t,arg_t>::type , value_t> 
	{	};

	typedef does_match type;
};

// locates a class in a sequence
// seq_t is the sequence to search
// how_t is a metafunction class for checking
// value_t is the value that must be equal to the results of the applied how_t
template <class seq_t, class how_t, class value_t>
struct locate
:	_mpl_::identity
	<
		typename _mpl_::deref
		<
			typename _mpl_::find_if
			<
				seq_t, 
				does_match<how_t,value_t>
			>::type 
		>::type
	>
{};	
// mfc
// given data_arg_t, will pull the key from data_arg_t
// and will pass the resulting key to the mfc supplied as exp_t
template <class exp_t>
struct eval_expression_on_key 
{
	template <class data_arg_t>
	struct apply
	: _mpl_::apply1<exp_t, typename _mpl_::apply1< key_of,data_arg_t>::type >::type
	{
	};
	typedef eval_expression_on_key  type;
};

// will return true if the how_t and value_t result in a match 
// against the passed type data_arg_t in the internal apply nested template
template <class how_t, class value_t>
struct does_contain
{
	template <class data_arg_t>	// element in sequence
	struct apply
	: _mpl_::contains< data_arg_t, typename _mpl_::apply1< how_t,value_t>::type > 
	{	};
};
////////////////////////////////////////////////////////////////
// formal_item
////////////////////////////////////////////////////////////////

// base class for formal items
struct formal_item	
{
	typedef _mpl_::vector1< formal_item> concepts;
};

// returns the nested item<> template with the passed base_t class applied from target_t
template <class base_t, class target_t>
struct apply_item_base
{
	typedef typename target_t:: template item<base_t> type;
};

// returns true if T is a formal_item derived class
template <class T>
struct is_formal_item : boost::is_base_and_derived<formal_item, typename key< typename boost::remove_reference<T>::type >::type > {};

struct sample_base {};

// if target_t is a formal item, will apply base_t to the nested item<> template and return the resulting type
// if target_t is NOT a formal item will return the target<> of target_t
// if the target_t IS a formal item and is passed as a reference, 
//		the reference is removed, 
//		the nested item<> has base_t applied
//		the return is then the nested item<> with base applied returned as a reference
template <class base_t, class target_t, class Enable = void> 
struct if_formal_apply_base  {  };

template <class base_t, class target_t>
struct if_formal_apply_base<base_t, target_t, typename boost::enable_if<is_formal_item<target_t>  >::type> 
{ 
	typedef typename boost::remove_reference<target_t>::type target_type;
	typedef typename target_type:: template item<base_t > item_type;
	// if is reference on the original target- re-add it back to the output
	typedef typename _mpl_::if_< boost::is_reference<target_t>, item_type&, item_type>::type type;
};

template <class base_t, class target_t>
struct if_formal_apply_base<base_t, target_t, typename boost::enable_if<_mpl_::not_< is_formal_item<target_t> >  >::type> 
{ 
		typedef typename target<target_t>::type type;
};

//////////////////////////////////////////
// Global Traits
namespace detail
{
	struct any_instance	{};	// concept for "any" instance
};

// defines "by value" storage
struct by_val
{
	typedef detail::any_instance		instance_concept;
	typedef by_val						storage_concept;
	typedef by_val						type;
};

// defines "by reference" storage
struct by_ref
{
	typedef detail::any_instance		instance_concept;
	typedef by_ref						storage_concept;
	typedef by_ref						type;
};

// defines "by any" storage
struct by_any
{
	typedef detail::any_instance		instance_concept;
	typedef by_any						storage_concept;
	typedef by_any						type;
};

// defines "by auto ref " storage
struct by_auto_ref
{
	typedef detail::any_instance		instance_concept;
	typedef by_auto_ref					storage_concept;
	typedef by_auto_ref					type;
};
// defines "by auto ref " storage
struct by_shared_ref
{
	typedef detail::any_instance			instance_concept;
	typedef by_shared_ref					storage_concept;
	typedef by_shared_ref					type;
};

struct removable_items	{	typedef removable_items type; }; // concept that specifies the target has removable items
//////////////////////////////////////////
// Custom item support
struct custom_item
{
	typedef custom_item type;
};

template <class T>
struct is_custom_item : boost::is_base_and_derived<custom_item, T> {};

	// simple base exists to support is_shared_ref<>
	struct shared_ref_base	{};

template <class T>
struct is_shared_ref : boost::is_base_and_derived<shared_ref_base, T> {};

//////////////////////////////////////////
// custom item implementation
template <class T>
struct shared_ref : custom_item , shared_ref_base
{
	BOOST_MPL_ASSERT(( _mpl_::not_< boost::is_reference<T> > )); // reference type keys are NOT allowed with auto_ref!!!!!!

	typedef shared_ref				type;		// our type - defined for _mpl_ compatiblity
	typedef T						key_type;	// type used for item lookup
	typedef boost::shared_ptr<T>	impl_type;	// type used for implementing storage of the item
	typedef boost::shared_ptr<T> 	arg_type;	// type used for receiving a value to store in the item
	typedef T						return_type;// type used to return a dereferenece impl_type  
	typedef T						value_type; // type used to return a dereferenece impl_type  

	typedef by_shared_ref			instance_concept;
	typedef _mpl_::vector< custom_item, by_shared_ref> concepts;

	// policy function for taking the impl object and returning the value
	// bag will never call this function if is_val_empty() returns true
	return_type & impl_to_val(impl_type & obj) const
	{
		assert(obj.get() != 0); // todo throw 
		return *(obj.get());
	}
	// policy function for converting a value to an implementation
	impl_type val_to_impl(arg_type & obj) const
	{
		return impl_type(obj);
	}
	// policy function for checking the impl object for being empty
	bool is_val_empty(impl_type & obj)
	{
		return (obj.get() == 0);
	}
};

	// simple base exists to support is_auto_ref<>
	struct auto_ref_base	{};

// returns true if T is an auto_ref<>
template <class T>
struct is_auto_ref : boost::is_base_and_derived<auto_ref_base, T> {};

template <class T>
struct auto_ref : custom_item, auto_ref_base
{
	BOOST_MPL_ASSERT(( _mpl_::not_< boost::is_reference<T> > )); // reference type keys are NOT allowed with auto_ref!!!!!!

	typedef auto_ref				type;		// our type - defined for mpl compatiblity
	typedef T						key_type;	// type used for item lookup
	typedef std::auto_ptr<T>		impl_type;	// type used for implementing storage of the item
	typedef T *						arg_type;	// type used for receiving a value to store in the item
	typedef T						return_type;// type used to return a dereferenece impl_type  
	typedef T						value_type; // type used to return a dereferenece impl_type  

	typedef by_auto_ref			instance_concept;
	typedef _mpl_::vector< custom_item, by_auto_ref> concepts;
	
	typedef std::auto_ptr<T>		single_storage_type;
	typedef boost::ptr_vector<T>	multiple_storage_type;
	
	// todo- do we need to define set and insert methods?

	// policy function for taking the impl object and returning the value
	// bag will never call this function if is_val_empty() returns true
	return_type & impl_to_val(impl_type & obj)
	{
		assert(obj.get() != 0);
		return *(obj.get());
	}
	// policy function for converting a value to an implementation
	impl_type val_to_impl(arg_type & obj) // const
	{
		return impl_type(obj);
	}
	// policy function for converting a value to an implementation (for multiple)
	arg_type & multiple_val_to_impl(arg_type & obj) // const
	{
		return obj;
	}
	// policy function for checking the impl object for being empty
	bool is_val_empty(impl_type & obj)
	{
		return (obj.get() == 0);
	}
};

	


///////////////////////////////////////
// SINGLE
///////////////////////////////////////

namespace detail
{
	struct bag_impl_base
	{};
};

template <class T>
struct is_bag : boost::is_base_and_derived<detail::bag_impl_base, typename boost::remove_reference<T>::type > {};

//////////////////////////////////////////////
// Internal functors for visiting our target
// special handling for embedded bags and re-passing the original
// meta filter to the embedded bag 

// visit a non-bag
template <class orig_arg_t,class Tar, class visitEnable = void> 
struct visit_impl 
{ 
	template <class functor_t, class value_t >
	visit_impl(functor_t & functor, value_t & val)
	{
		// value is not a bag- so just pass it to the functor
		functor(val);
	}
};
// todo- orig_arg is not a good name- it NOW is the orig filter_exp
// visit a bag
template <class orig_arg_t,class Tar>
struct visit_impl<orig_arg_t, Tar, typename boost::enable_if< is_bag<Tar> >::type> 
{  
	// visit with filter
	template <class orig_arg_t2, class visitEnable2 = void> 
	struct visit_bag_impl
	{
		template <class functor_t, class value_t >
		visit_bag_impl(functor_t & functor, value_t & val)
		{
			// value is a bag- so call for_each
			val.template for_each_raw_filtered_visit_each<orig_arg_t2,functor_t>(functor);
		}
	};
	// visit without filter
	template <class orig_arg_t2 > 
	struct visit_bag_impl<orig_arg_t2, typename boost::enable_if<boost::is_same<orig_arg_t2,_mpl_::void_> >::type > 
	{
		template <class functor_t, class value_t >
		visit_bag_impl(functor_t & functor, value_t & val)
		{
			// value is a bag- so call for_each
			val.for_each(functor);
		}
	};
	template <class functor_t, class value_t >
	visit_impl(functor_t & functor, value_t & val)
	{
		// pick the right way to visit the target
		visit_bag_impl<orig_arg_t>(functor, val);
	}
};

// functor for visiting a target
// will correctly visit the elements based on their
// type and strategy
template <class orig_arg_t, class key_t, class functor_t2>
struct visit_target
{
	functor_t2 & mFunc;
	visit_target(functor_t2 & func)
	: mFunc(func) {};

	template <class value_t>
	void operator()( value_t & val)
	{
		// select correct way to visit based on key type
		visit_impl<orig_arg_t, key_t>(mFunc,val);
	}
};

// exception for uninitialized item access
// this is generally only thrown when 
// an attempt has been made to access a non by_val item
// and the item is uninitialized
struct uninitialized_item_access : std::exception
{
	virtual char const * what() const throw()
	{
		return "boost::bag::uninitialized_item_access";
	}
};


// todo- a way to check for empty on the item

/////////////////////////////////////
// local modifiers

// placeholder so bag::items::strategy::dimension has a simple class to return
struct single_
{	
	typedef single_ type;
};

	
// Single - by_val
template <class target_t, class Enable = void> 
struct single
{
	typedef _mpl_::vector< detail::any_instance, single_, single<by_val>, single<by_any>, by_any, by_val, modifier, local_modifier> concepts;
	
	typedef single		type;
	typedef target_t	key_type;
	typedef target_t	value_type;
	typedef target_t	arg_type;
	typedef target_t	target_type; // original value passed in 
	
	private:
		key_type			m_rawvalue;
	public:
		single()
		{}
		void set(arg_type & obj)
		{
			// by_val so we set by val
			// todo test for this
			m_rawvalue = obj;
		}
		void insert(arg_type & obj)
		{
			set(obj);
		}
		// returns the value as a ref
		value_type & val_as_ref()
		{
			return m_rawvalue;
		}
		// single-by_val tests for equal by value
		bool is_equal(arg_type & obj)
		{
			return m_rawvalue == obj;
		}
	
	public:

		template <class orig_arg_t, class fctor_t>
		void visit_each(fctor_t & f)
		{									  
			// select correct way to visit based on key type
			visit_impl<orig_arg_t, key_type>(f,val_as_ref());
		}

		// apply functions
		// techincally these are "re-apply" functions
		// they allow the caller- to re-apply the target from this type
		// if the target is a &, will sense this and
		//		strip the reference from the key type
		//		force the storage type to be "by_ref"
		template <class new_target_t, class _Enable = void> 
		struct apply
		{
			typedef single<new_target_t > type;
		};

		template< class new_target_t>
		struct apply< new_target_t, typename boost::enable_if< has_concept<by_ref,new_target_t>  >::type> 
		{
			typedef single<new_target_t& > type;
		};
};

// Single - by_ref
template< class target_t>
struct single< target_t, typename boost::enable_if<boost::is_reference<target_t>  >::type> 
{
	typedef _mpl_::vector< detail::any_instance, single_, single<by_ref>, single<by_any>, by_any, by_ref, modifier, local_modifier, removable_items> concepts;

	typedef single		type;
	typedef typename	boost::remove_reference<target_t>::type	key_type;
	typedef key_type	value_type;
	typedef target_t	arg_type;
	typedef target_t	target_type; // original value passed in 

	private:
		value_type			* m_rawvalue;
	public:
		single()
		: m_rawvalue(0)
		{}
		void set(arg_type  obj)
		{
			// by_ref so we take the address
			m_rawvalue = &obj;
		}
		void insert(arg_type obj)
		{
			set(obj);
		}
		// single-by_val tests for equal by ptr address
		bool is_equal(arg_type  obj)
		{
			return m_rawvalue == &obj;
		}
		// returns the value as a ref
		value_type & val_as_ref()
		{
			if (m_rawvalue == 0)
				throw uninitialized_item_access();

			return *m_rawvalue;
		}
		
		template <class orig_arg_t, class functor_t>
		void visit_each(functor_t & functor)
		{									  
			if (m_rawvalue != 0)
			{
				visit_impl<orig_arg_t, key_type>(functor,val_as_ref());
			}
		}
		template <class T>
		void remove(T&)
		{
			m_rawvalue = 0;	
		}
		template <class new_target_t, class _Enable = void> 
		struct apply
		{
			typedef single<new_target_t > type;
		};
		template< class new_target_t>
		struct apply< new_target_t, typename boost::enable_if< has_concept<by_ref,new_target_t>  >::type> 
		{
			typedef single<new_target_t& > type;
		};
};

// single - custom_item
template< class target_t>
struct single< target_t, typename boost::enable_if<is_custom_item<target_t>  >::type> : target_t
{
	typedef typename target_t::instance_concept::type instance_concept_t; // instance concept pulled from the target
	
	typedef _mpl_::vector< detail::any_instance, single_, single<instance_concept_t>, single<by_any>, by_any, instance_concept_t,  modifier, local_modifier> concepts;
	
	typedef single							type;		// type defined for mpl compatiblity
	typedef typename target_t::key_type		key_type;	// type used for item lookup
	typedef typename target_t::impl_type	impl_type;	// type used for implementing storage of the item
	typedef typename target_t::arg_type		arg_type;	// type used for receiving a value to store in the item
	typedef typename target_t::return_type	return_type;// type used to return a dereferenece impl_type  

	private:
		impl_type			m_rawvalue;
	public:
		single()
		{}
		void set(arg_type & obj)
		{
			// by_val so we set by val
			// todo test for this
			m_rawvalue = target_t::val_to_impl(obj);
			assert(!is_empty());
		}
		void insert(arg_type & obj)
		{
			set(obj);
		}
		// returns the value as a ref
		return_type & val_as_ref()
		{
			if (is_empty())
				throw uninitialized_item_access();

			return target_t::impl_to_val(m_rawvalue);
		}
	
		bool is_empty()
		{
			return target_t::is_val_empty(m_rawvalue);
		}
		// single-by_val tests for equal by value
		bool is_equal(arg_type & obj)
		{
			assert(!is_empty());
			return m_rawvalue == obj;
		}
	
	public:

		template <class orig_arg_t, class fctor_t>
		void visit_each(fctor_t & f)
		{	
			// if we are not empty, then visitation is allowed								
			if (!is_empty())
			{
				// select correct way to visit based on key type
				visit_impl<orig_arg_t, key_type>(f,val_as_ref());
			}
		}

		// apply functions
		// techincally these are "re-apply" functions
		// they allow the caller- to re-apply the target from this type
		// if the target is a &, will sense this and
		//		strip the reference from the key type
		//		force the storage type to be "by_ref"
		template <class new_target_t, class _Enable = void> 
		struct apply
		{
			typedef single<new_target_t > type;
		};

		template< class new_target_t>
		struct apply< new_target_t, typename boost::enable_if< has_concept<by_ref,new_target_t>  >::type> 
		{
			typedef single<new_target_t& > type;
		};
};

// global modifier - by_val
template <>
struct single<by_val>	
{
	typedef single type;
	typedef by_val			storage_concept;

	template <class new_target_t, class _Enable = void> 
	struct apply
	{
		typedef single<new_target_t > type;
	};
	template< class new_target_t>
	struct apply< new_target_t, typename boost::enable_if< has_concept<by_ref,new_target_t>  >::type> 
	{
		typedef single<new_target_t& > type;
	};
};

// global modifier - by_ref
template <>
struct single<by_ref>
{
	typedef single type;
	typedef by_ref			storage_concept;
    
	template <class new_target_t, class _Enable = void> 
	struct apply
	{
		typedef single<typename boost::remove_reference<new_target_t>::type & > type;
	};
	template< class new_target_t>
	struct apply< new_target_t, typename boost::enable_if< has_concept<by_ref,new_target_t>  >::type> 
	{
		typedef single<typename boost::remove_reference<new_target_t>::type & > type;
	};
	template< class new_target_t>
	struct apply< new_target_t, typename boost::enable_if< has_concept<custom_item,new_target_t>  >::type> 
	{
		typedef single<new_target_t > type;
	};

};
// global modifier - by_ref
template <>
struct single<by_auto_ref>
{
	typedef single type;
	typedef by_auto_ref			storage_concept;

	template <class new_target_t, class _Enable = void> 
	struct apply
	{
		typedef single< auto_ref<new_target_t> > type;
	};

};
// global modifier - by_shared_ref
template <>
struct single<by_shared_ref>
{
	typedef single type;
	typedef by_shared_ref			storage_concept;

	template <class new_target_t, class _Enable = void> 
	struct apply
	{
		typedef single< shared_ref<new_target_t> > type;
	};
};

// filter expression ONLY
template <>
struct single<by_any>
{
	typedef single type;
	typedef by_any			storage_concept;
};


///////////////////////////////////////
// multiple
///////////////////////////////////////

// placeholder so bag::items::strategy::dimension has a simple class to return
struct multiple_
{	
	typedef multiple_ type;
};

// multiple- by_val
template <class target_t, class Enable = void> 
struct multiple
{
	typedef multiple				type;
	typedef target_t				key_type;
	typedef std::vector<target_t>	value_type;
	typedef target_t				arg_type;
	typedef target_t				target_type; // original value passed in 

	typedef _mpl_::vector< detail::any_instance, multiple_, multiple<by_val>, multiple<by_any>, by_any, by_val, modifier, local_modifier, removable_items> concepts;
		
	private:
		std::vector<key_type>			m_rawvalue;
	public:
		multiple()
		{}
		void set(arg_type & obj)
		{
			// by_val so we set by val
			m_rawvalue.clear();
			m_rawvalue.push_back(obj);
		}
		void insert(arg_type & obj)
		{
			m_rawvalue.push_back(obj);
		}

		template <class T>
		void push_back(T 	val)
		{
			m_rawvalue.push_back(val);
		}

		// multiple-by_val tests for equal by value
		bool is_equal(arg_type & obj)
		{
			using namespace boost;
			typename std::vector<key_type >::iterator  itFound;
			itFound = std::find_if(m_rawvalue.begin(),m_rawvalue.end(),
								bind(std::equal_to<key_type>(), _1, obj ) );	
			if (*itFound == obj)
				return true;
			return false;
		}
		
		void remove(key_type 	pvalue)
		{
			using namespace boost;
			typename std::vector<key_type >::iterator  itFound;
			itFound = std::find_if(m_rawvalue.begin(),m_rawvalue.end(),
								bind(std::equal_to<key_type>(), _1, pvalue ) );	
			// if we found m_pCurrent in the children of m_pParent, then we are valid
			if (*itFound == pvalue)
				m_rawvalue.erase(itFound);
		}	

		// returns the value as a ref
		value_type & val_as_ref()
		{
			return m_rawvalue;
		}
		template <class orig_arg_t, class fctor_t>
		void visit_each(fctor_t & functor)
		{									  
			std::for_each(m_rawvalue.begin(), m_rawvalue.end(), visit_target<orig_arg_t,key_type,fctor_t>(functor) );
		}

        // apply functions
        // techincally these are "re-apply" functions
        // they allow the caller- to re-apply the target from this type
        // if the target is a &, will sense this and
        //		strip the reference from the key type
        //		force the storage type to be "by_ref"
        template <class new_target_t, class _Enable = void> 
        struct apply
        {
            typedef multiple<new_target_t > type;
        };

        template< class new_target_t>
        struct apply< new_target_t, typename boost::enable_if< has_concept<by_ref,new_target_t>  >::type> 
        {
            typedef multiple<new_target_t& > type;
        };
};

// multiple- by_ref
template< class target_t>
struct multiple< target_t, typename boost::enable_if<boost::is_reference<target_t>  >::type> 
{
	typedef multiple											type;
	typedef typename boost::remove_reference<target_t>::type	key_type;
	typedef filtered_vector
			<
				key_type*, 
				key_type, 
				key_type&, 
				ptr_filter<key_type *, key_type> 
			>													container_type;

	typedef container_type										value_type;
	typedef target_t											arg_type;
	typedef target_t											target_type; // original value passed in 
	
	typedef _mpl_::vector< detail::any_instance, multiple_, multiple<by_ref>, multiple<by_any>, by_any, by_ref, modifier, local_modifier, removable_items> concepts;
		
	private:
		container_type					m_rawvalue;
	public:
		multiple()
		{}
		void set(arg_type obj)
		{
			m_rawvalue.clear();
			m_rawvalue.push_back(obj);
		}
		void insert(arg_type obj)
		{
			m_rawvalue.push_back(obj);
		}

		template <class T>
		void push_back(T 	val)
		{
			m_rawvalue.push_back(val);
		}
		
		// multiple-by_ref tests for equal by ptr
		bool is_equal(target_t  obj)
		{
			using namespace boost;
			typename container_type::base_type::iterator itFound;
			
			itFound = std::find_if(m_rawvalue.base().begin(),m_rawvalue.base().end(),
								bind(std::equal_to<key_type*>(), _1, &obj ) );	
			if (*itFound == &obj)
				return true;
			return false;
		}

		void remove(target_t value)
		{
			using namespace boost;
			typename container_type::base_type::iterator itFound;
			itFound = std::find_if(m_rawvalue.base().begin(),m_rawvalue.base().end(),
								bind(std::equal_to<key_type*>(), _1, &value ) );	
			// if we found m_pCurrent in the children of m_pParent, then we are valid
			if (*itFound == &value)
				m_rawvalue.base().erase(itFound);
		}	

		// returns the value as a ref
		value_type & val_as_ref()
		{
			return m_rawvalue;
		}
		template <class orig_arg_t, class fctor_t>
		void visit_each(fctor_t & functor)
		{									  
			std::for_each(m_rawvalue.begin(), m_rawvalue.end(), visit_target<orig_arg_t,key_type,fctor_t>(functor) );
		}
        // apply functions
        // techincally these are "re-apply" functions
        // they allow the caller- to re-apply the target from this type
        // if the target is a &, will sense this and
        //		strip the reference from the key type
        //		force the storage type to be "by_ref"
        template <class new_target_t, class _Enable = void> 
        struct apply
        {
            typedef multiple<new_target_t > type;
        };

        template< class new_target_t>
        struct apply< new_target_t, typename boost::enable_if< has_concept<by_ref,new_target_t>  >::type> 
        {
            typedef multiple<new_target_t& > type;
        };
};



// multiple - auto_ref
template< class target_t>
struct multiple< target_t, typename boost::enable_if<is_auto_ref<target_t>  >::type> : target_t
{
	typedef _mpl_::vector< detail::any_instance, multiple_, multiple<by_auto_ref>, multiple<by_any>, by_any, by_auto_ref, modifier, local_modifier> concepts;
	
	typedef multiple									type;			// type defined for mpl compatiblity
	typedef typename target_t::key_type					key_type;		// type used for item lookup
	typedef typename target_t::impl_type				impl_type;		// type used for implementing storage of the item
	typedef typename target_t::arg_type					arg_type;		// type used for receiving a value to store in the item
	typedef typename target_t::return_type				return_type;	// type used to return a dereferenece impl_type  
	typedef typename target_t::multiple_storage_type	storage_type;	// type for how to store multiples
	typedef typename target_t::multiple_storage_type	value_type;		// type for how to store multiples

	private:
		storage_type	m_rawvalue;
	public:
		multiple()
		{}
		void set(arg_type & obj)
		{
			m_rawvalue.clear();
			m_rawvalue.push_back( obj ); 
		}
		void insert(arg_type & obj)
		{
			m_rawvalue.push_back( obj );
		}
		
		template <class T>
		void push_back(T 	val)
		{
			insert(val);
		}

		// returns the value as a ref
		storage_type & val_as_ref()
		{
			return m_rawvalue;
		}
	
		bool is_empty()
		{
			return (m_rawvalue.size() ==0);
		}
		// single-by_val tests for equal by value
		bool is_equal(arg_type & obj)
		{
			assert(!is_empty());
			// todo- fix
			return false; // m_rawvalue == obj;
		}
	
	public:

		template <class orig_arg_t, class fctor_t>
		void visit_each(fctor_t & functor)
		{	
			// if we are not empty, then allow visitation								
			if (!is_empty())
			{
				std::for_each(m_rawvalue.begin(), m_rawvalue.end(), visit_target<orig_arg_t,key_type,fctor_t>(functor) );
			}
		}

		// apply functions
		// techincally these are "re-apply" functions
		// they allow the caller- to re-apply the target from this type
		// if the target is a &, will sense this and
		//		strip the reference from the key type
		//		force the storage type to be "by_ref"
		template <class new_target_t, class _Enable = void> 
		struct apply
		{
			typedef multiple<new_target_t > type;
		};

		template< class new_target_t>
		struct apply< new_target_t, typename boost::enable_if< has_concept<by_ref,new_target_t>  >::type> 
		{
			typedef multiple<new_target_t& > type;
		};
};


// multiple - shared_ref
template< class target_t>
struct multiple< target_t, typename boost::enable_if<is_shared_ref<target_t>  >::type> : target_t
{
	typedef _mpl_::vector< detail::any_instance, multiple_, multiple<by_shared_ref>, multiple<by_any>, by_any, by_shared_ref, modifier, local_modifier> concepts;
	
	typedef multiple						type;			// type defined for mpl compatiblity
	typedef typename target_t::key_type		key_type;		// type used for item lookup
	typedef typename target_t::arg_type		arg_type;		// type used for receiving a value to store in the item
	typedef shared_ptr_vector<key_type	>	storage_type;	// type for how to store multiples
	typedef shared_ptr_vector<key_type	>	impl_type;		// type for how to store multiples
	typedef storage_type	value_type;
	
	private:
		storage_type	m_rawvalue;
	public:
		multiple()
		{}
		
		void set(arg_type obj)
		{
			m_rawvalue.clear();
			m_rawvalue.push_back( obj ); 
		}
		
		void insert(arg_type  obj)
		{
			m_rawvalue.push_back( obj );
		}
		
		template <class T>
		void push_back(T 	val)
		{
			insert(val);
		}

		// returns the value as a ref
		storage_type & val_as_ref()
		{
			return m_rawvalue;
		}
	
		bool is_empty()
		{
			return (m_rawvalue.size() ==0);
		}
		
		// single-by_val tests for equal by value
		bool is_equal(arg_type & obj)
		{
			assert(!is_empty());
			// todo- fix
			return false; // m_rawvalue == obj;
		}

	public:

		template <class orig_arg_t, class fctor_t>
		void visit_each(fctor_t & functor)
		{	
			// if we are not empty, then visitation is allowed								
			if (!is_empty())
			{
				typename storage_type::iterator it; 
				for( it =m_rawvalue.begin(); it !=m_rawvalue.end(); it++)
				{
					// select correct way to visit based on key type
					visit_impl<orig_arg_t, key_type>(functor,(*it)); 
				}
			}
		}

		// apply functions
		// techincally these are "re-apply" functions
		// they allow the caller- to re-apply the target from this type
		// if the target is a &, will sense this and
		//		strip the reference from the key type
		//		force the storage type to be "by_ref"
		template <class new_target_t, class _Enable = void> 
		struct apply
		{
			typedef multiple<new_target_t > type;
		};

		template< class new_target_t>
		struct apply< new_target_t, typename boost::enable_if< has_concept<by_ref,new_target_t>  >::type> 
		{
			typedef multiple<new_target_t& > type;
		};
};


// global modifier - by_val
template <>
struct multiple<by_val>	
{
	typedef multiple type;
	typedef by_val			storage_concept;

	template <class new_target_t, class _Enable = void> 
	struct apply
	{
		typedef multiple<new_target_t > type;
	};
	template< class new_target_t>
	struct apply< new_target_t, typename boost::enable_if< has_concept<by_ref,new_target_t>  >::type> 
	{
		typedef multiple<new_target_t& > type;
	};
};

// global modifier - by_ref
template <>
struct multiple<by_ref>
{
	typedef multiple type;

	typedef by_ref			storage_concept;

	template <class new_target_t, class _Enable = void> 
	struct apply
	{
		typedef multiple<typename boost::remove_reference<new_target_t>::type & > type;
	};

	template< class new_target_t>
	struct apply< new_target_t, typename boost::enable_if< has_concept<by_ref,new_target_t>  >::type> 
	{
		typedef multiple<typename boost::remove_reference<new_target_t>::type & > type;
	};
	template< class new_target_t>
	struct apply< new_target_t, typename boost::enable_if< has_concept<custom_item,new_target_t>  >::type> 
	{
		typedef multiple<new_target_t > type;
	};


};
// global modifier - by_ref
template <>
struct multiple<by_auto_ref>
{
	typedef multiple type;
	typedef by_auto_ref			storage_concept;

	template <class new_target_t, class _Enable = void> 
	struct apply
	{
		typedef multiple< auto_ref<new_target_t> > type;
	};
};

// global modifier - by_shared_ref
template <>
struct multiple<by_shared_ref>
{
	typedef multiple type;
	typedef by_shared_ref			storage_concept;

	template <class new_target_t, class _Enable = void> 
	struct apply
	{
		typedef multiple< shared_ref<new_target_t> > type;
	};
};

// global modifier - by_ref
template <>
struct multiple<by_any>
{
	typedef multiple type;
	typedef by_any			storage_concept;
};

// ending element
struct end
{	
	typedef end type;
	
	// hack
	template <int irt_arg_count>
	struct get_arg_item_map
	{
		typedef typename mpl::vector0<>::type type;

		// repass the count
		enum { new_count = 0 };
	};

};

// namespace detail {

// takes passed sequence and turns it into a vector
// useful for getting around mpl views not being excactly the same as a vector<>
template <class seq_t>
struct make_vector 
{
	typedef typename 
	_mpl_::copy<	typename seq_t::type , _mpl_::back_inserter< _mpl_::vector0<> >	>::type 
	type;
	
//	BOOST_MPL_ASSERT((_mpl_::is_sequence<typename seq_t::type >));
};

template <>
struct make_vector<end>
{
	typedef _mpl_::vector0<> type;
};

template <>
struct make_vector<_mpl_::void_>
{
	typedef mpl::vector0<> type;
};

// implementation for unique_all
template <class curr_iter_t, class end_pos , class prev_seq_t >
struct unique_all_impl
{
	// current iterator - dereferenced
	typedef typename _mpl_::deref<curr_iter_t>::type curr_deref_t;
	
	typedef typename _mpl_::eval_if
	< 
		// if the current pos is less than the end pos
		_mpl_::less< typename curr_iter_t::pos, end_pos >,
		unique_all_impl	// recurse into ourselves - with incremeneted iter
		< 
			typename _mpl_::next< curr_iter_t >::type,	// pass next iter
			end_pos,									// repass- endpos	
			typename _mpl_::eval_if	// if previous sequence contains current item,
			<
				_mpl_::contains<prev_seq_t, curr_deref_t >,
				prev_seq_t,		/// do not add
				// ELSE
				// add the current type to the previous sequence
				typename _mpl_::push_back< prev_seq_t, curr_deref_t >::type  // otherwise add
			>::type
		>,
		// all done recursing, so return the previous sequence
		prev_seq_t	
	>::type type;
};

// }; // namespace detail

#ifdef WORKS
// returns a form of passed sequence with ALL duplicate types removed
template <class seq_t >
struct unique_all
// : unique_all_impl< typename _mpl_::begin<seq_t>::type, _mpl_::long_<_mpl_::size<seq_t>::type::value> , typename _mpl_::clear<seq_t>::type >
: unique_all_impl< typename _mpl_::begin<typename make_vector< typename seq_t::type>::type >::type, _mpl_::long_<_mpl_::size<typename seq_t::type >::type::value> , _mpl_::vector0<> >
{
	BOOST_MPL_ASSERT((_mpl_::is_sequence<typename seq_t::type >));
};
#endif

namespace detail
{
	BOOST_MPL_HAS_XXX_TRAIT_NAMED_DEF(defines_pos_impl, pos, false)
};

	// returns a form of passed sequence with ALL duplicate types removed
	// will work directly with iterators which support ::pos, if the iter 
	// does not support pos, will first copy to a vector (which has an iter which supports pos)
	//	(note this copy into a vector is what enables unique_all to work with views)

	// unique_all for views
	template <class seq_t, class Enable = void> 
	struct unique_all
	: unique_all_impl< typename _mpl_::begin<typename make_vector< typename seq_t::type>::type >::type, _mpl_::long_<_mpl_::size<typename seq_t::type >::type::value> , _mpl_::vector0<> >
	{
		BOOST_MPL_ASSERT((_mpl_::is_sequence<typename seq_t::type >));
	};

	// unique_all for sequences which have iters which support ::pos (i.e. vector)
	template <class seq_t>
	struct unique_all<seq_t, typename boost::enable_if< detail::defines_pos_impl< typename _mpl_::begin<seq_t>::type >  >::type> 
	: unique_all_impl< typename _mpl_::begin< typename seq_t::type >::type, _mpl_::long_<_mpl_::size<typename seq_t::type >::type::value> , _mpl_::vector0<> >
	{
		BOOST_MPL_ASSERT((_mpl_::is_sequence<typename seq_t::type >));
	};

	// implementation for at_pos
	// will recurse itself until it finds the position needed
	// will return the type found
	template <class curr_iter_t,  class find_pos_t >
	struct at_pos_impl
	{
		// current iterator - dereferenced
		typedef typename _mpl_::deref<curr_iter_t>::type curr_deref_t;
		
		typedef typename _mpl_::eval_if
		< 
			// if the current pos is less than the end pos
			_mpl_::less< typename curr_iter_t::pos, find_pos_t >,
			at_pos_impl	// recurse into ourselves - with incremeneted iter
			< 
				typename _mpl_::next< curr_iter_t >::type,	// pass next iter
				find_pos_t									// repass- find_pos	
			>,
			// all done recursing, so return type
			_mpl_::identity<curr_deref_t>
		>::type type;
	};

	// }; // namespace detail

	// returns the type at the passed position
	// will compile time ASSERT if the position passed is beyond the size 
	// of seq_t
	template <class seq_t, class request_pos_t >
	struct at_pos 
	 :	at_pos_impl 
		<
			typename _mpl_::begin<seq_t>::type,				// begin iter
			_mpl_::long_<request_pos_t::type::value>			// requested pos
		>
	{
		// ensure type is a sequence
		BOOST_MPL_ASSERT((_mpl_::is_sequence<typename seq_t::type >));
		
		// ordinal passed must be within range of values in sequence
		BOOST_MPL_ASSERT((_mpl_::less<_mpl_::long_<request_pos_t::type::value>, _mpl_::long_<_mpl_::size<seq_t>::type::value>   > ));
	};

	template <class seq_t, int irequest_pos_t >
	struct at_pos_c 
	 :	at_pos_impl 
		<
			typename _mpl_::begin<seq_t>::type,				// begin iter
			_mpl_::long_<irequest_pos_t>					// requested pos
		>
	{
		// ensure type is a sequence
		BOOST_MPL_ASSERT((_mpl_::is_sequence<typename seq_t::type >));
		
		// ordinal passed must be within range of values in sequence
		BOOST_MPL_ASSERT((_mpl_::less<_mpl_::long_<irequest_pos_t>, _mpl_::long_<_mpl_::size<seq_t>::type::value>   > ));
	};

//////////////////////////////////////////
// is type an mpl integral int?
// 
// returns true if the passed type is an 
// integral int
// will correctly sense if the type is a mpl::int_<T> or an mpl::integral_c<int,T> 
template <class T, class dummy = void>
struct is_mpl_integral_int
: _mpl_::false_ {};

template <int T>
struct is_mpl_integral_int< _mpl_::int_<T> >
: _mpl_::true_ 
{};

template <int T>
struct is_mpl_integral_int< _mpl_::integral_c<int,T> >
: _mpl_::true_ 
{};

// not form (purposely copy-pasted- to reduce instantiations)
template <class T, class dummy = void>
struct not_is_mpl_integral_int
: _mpl_::true_ {};

template <int T>
struct not_is_mpl_integral_int< _mpl_::int_<T> >
: _mpl_::false_ 
{};

template <int T>
struct not_is_mpl_integral_int< _mpl_::integral_c<int,T> >
: _mpl_::false_ 
{};

// promotes passed type to integral<int,>
template <class T, class dummy = void>
struct promote_to_mpl_integral_int
{
	// not an integral int- just repass it
	typedef typename _mpl_::identity<T>::type type;
};

template <int T>
struct promote_to_mpl_integral_int< _mpl_::int_<T> >
: _mpl_::integral_c<int, T>
{};

template <int T>
struct promote_to_mpl_integral_int< _mpl_::integral_c<int,T> >
: _mpl_::integral_c<int,T> 
{};

// returns true if a sequence contains an mpl int<> or an mpl integral_c<int,>
template <class seq_t>
struct seq_contains_mpl_integral_int
: _mpl_::not_< boost::is_same< typename _mpl_::deref< typename _mpl_::find_if<seq_t, is_mpl_integral_int<_mpl_::_1> >::type >::type, _mpl_::void_> >
{}; 

// returns false if a sequence contains an mpl int<> or an mpl integral_c<int,>
template <class seq_t>
struct not_seq_contains_mpl_integral_int
: boost::is_same< typename _mpl_::deref< typename _mpl_::find_if<seq_t, is_mpl_integral_int<_mpl_::_1> >::type >::type, _mpl_::void_>
{}; 

// returns true if a sequence is completely filled with either int<> or integral_c<int,>
template <class seq_t>
struct seq_is_all_mpl_integral_int
:	_mpl_::equal_to
	< 
		_mpl_::count_if<seq_t, is_mpl_integral_int<_mpl_::_1> >,
		_mpl_::size<seq_t>
	>
{}; 

// returns false if a sequence is completely filled with either int<> or integral_c<int,>
template <class seq_t>
struct not_seq_is_all_mpl_integral_int
:	_mpl_::not_equal_to
	< 
		_mpl_::count_if<seq_t, is_mpl_integral_int<_mpl_::_1> >,
		_mpl_::size<seq_t>
	>
{}; 

// turns int<0> into integral<int,>
template <class seq_t>
struct normalized_ordinal_vector
{
	typedef typename make_vector< _mpl_::transform_view< _mpl_::filter_view< seq_t, is_mpl_integral_int<_mpl_::_1> >, promote_to_mpl_integral_int<_mpl_::_1> > >::type type;
};

// takes a sequence and splits it into one sequence of keys and another of ordinals
// ordinals may be expressed as mpl::int_<> or mpl::integral_c<int,>
template <class seq_t>
struct split_keys_and_orginals
{
	typedef split_keys_and_orginals type;
	
	BOOST_MPL_ASSERT((_mpl_::is_sequence<seq_t> ));

	// just the keys in seq_t
	typedef typename make_vector< _mpl_::filter_view< seq_t, not_is_mpl_integral_int<_mpl_::_1>  > >::type keys;

	// just the ordinals in seq_t
	typedef typename normalized_ordinal_vector<seq_t>::type ordinals;
};

// default case
template <class arg_t, class Enable = void>
struct generate_mfc_filter_from_sequence
{
	typedef _mpl_::void_ type;
};

//	only containing ordinals
template <class arg_t>
struct generate_mfc_filter_from_sequence
<
	arg_t, 
	typename boost::enable_if	// if ALL the items in the sequence are ints
	< 
		seq_is_all_mpl_integral_int<arg_t>  
	>::type 
>
{
	// normallize the ordinals
	typedef typename normalized_ordinal_vector<arg_t>::type normalized_ordinals;

	// return a filter expression which will check for a given type to be within one of the ordinals
	typedef  typename _mpl_::lambda< _mpl_::contains< normalized_ordinals  , index_<_mpl_::_1>   > >::type type;
};

// only containing keys - no mpl::ints<>
template <class arg_t>
struct generate_mfc_filter_from_sequence
<
	arg_t, 
	typename boost::enable_if	// if there are no ints or integral<int,>'s in the sequence
	< 
		not_seq_contains_mpl_integral_int<arg_t>  
	>::type 
>
{
	// return a filter expression which will check for a given type to be one of the keys in arg_t
	typedef typename _mpl_::lambda< _mpl_::contains< arg_t  , key<_mpl_::_1>   > >::type type;
};

//  containing both ordinals and keys
template <class arg_t>
struct generate_mfc_filter_from_sequence
<
	arg_t, 
	typename boost::enable_if	// // if arg_t contains ints, and not all of them are ints
	< 
		_mpl_::and_
		< 
			seq_contains_mpl_integral_int<arg_t>, 
			not_seq_is_all_mpl_integral_int<arg_t> 
		>  
	>::type 
>
{
	// split the int<> & integral_c<int,> types into one sequence
	// and put the keys into another one (seperate)
	typedef typename split_keys_and_orginals<arg_t>::type split;

	// return a filter expression which will check for the key of a given arg to be within the key sequence
	// OR a ordinal of a given arg to be within the ordinal sequence
	typedef typename _mpl_::lambda
	 < 
		_mpl_::or_
		< 
			_mpl_::contains		// if the key of arg is in our key sequence
			< 
				typename split::keys, 
				key<_mpl_::_1> 
			> , 
			_mpl_::contains		// (or) if the ordinal of the arg is in our arg sequence
			<  
				typename split::ordinals  , 
				index_<_mpl_::_1>   
			> 
		> 
	>::type type;
};


///////////////////////////////////////
// data
///////////////////////////////////////

template <class idx_t, class key_t, class concepts_t, class impl_t>
struct data
{
	typedef data		type;
	typedef idx_t		idx_type;
	typedef key_t		key_type;
	typedef concepts_t	concepts;
	typedef impl_t		impl_type;
};


namespace detail
{ 
	// helper class for determining the argument type for a given 
	// element
	template <class data_view, class seq_t>
	class arg_type
	{
		template <int idx2>
		struct get_arg_type
		{
			typedef typename _mpl_::at<data_view , _mpl_::int_<idx2> >::type::impl_type::arg_type type;
			
		};
	public:
		template <int idx>
		class get
		{
			public:
			typedef typename _mpl_::eval_if
			<
				_mpl_::less< _mpl_::int_<idx>, _mpl_::size<seq_t> >,  
				get_arg_type<idx>,
				_mpl_::void_
			>::type type;
		};
	};
	
	template <class T>
	struct get_seq
	{
		typedef typename _mpl_::lambda<T>::type::seq_type type;
	};
	template <class T>
	struct get_all_bag_keys
	{
		typedef typename T::all_bag_keys type; 
	};
	
	template <class T>
	struct get_data_view_all
	{
		typedef typename T::data_view_all type; 
	};
	
	template <class T>
	struct get_value_type
	{
		typedef typename T::value_type type;

	};

typedef _mpl_::vector10<by_any,by_val,by_ref,single<by_ref>,single<by_val>,single<by_any>, multiple<by_ref>,multiple<by_val>,multiple<by_any>,formal_item > arg_filters;

template <class arg0_t>
struct test
{
	typedef typename _mpl_::lambda< _mpl_::contains< arg0_t  , key<_mpl_::_1>   > >::type type;
};

			/* 
				can take:
					A where A is a type in seq_t
					by_ref,
					by_val, (anything in arg_filters)
					some _mpl_ sequence i.e. vector<A,B,C>
					a compile time lambda expression
				will generate a unary metafunction class which will 
				return true if the _1 arg passes the generated filter
			*/ 
			template <class arg0_t, class all_bag_keys_t> // todo remove arg_seq_t class arg_seq_t, 
			struct arg_to_filter_impl
			{
				typedef typename _mpl_::lambda< arg0_t >::type arg0_t_lambda;
				typedef typename _mpl_::eval_if
				< 
					// is the arg a specific filter expression? (i.e. by_val, multiple<by_val> etc...)
					_mpl_::contains<arg_filters,arg0_t_lambda >,
					_mpl_::eval_if
					<
						// is the filter speifically a formal_item filter?	
						boost::is_same<arg0_t,formal_item>,

						// return mfc which will filter on key types which are formal items
						_mpl_::lambda< is_formal_item< key<_mpl_::_1>   > >,

						// else return a filter expression to look for the arg as a concept
						has_concept< arg0_t>
					>,
					// ELSE
					_mpl_::eval_if
					< 
						// is the arg a sequence?		
						_mpl_::is_sequence< arg0_t_lambda>, 

						generate_mfc_filter_from_sequence< arg0_t >,

                        // if the type is IN the list of keys for all items (deep)
						_mpl_::eval_if	
						<
							_mpl_::contains		// if the arg is in the sequence
							< 
								all_bag_keys_t, // all the deep types for this and contained bags 
								arg0_t_lambda
							>,  
							does_match<key_of,arg0_t >, // return a mfc for checking on a match of key with the passed arg
							// otherwise assume the arg is a lambda expression and return an MFC which will apply it to the key type
							eval_expression_on_key< arg0_t_lambda  >
						>
					>
				>::type type;
			};


	// actual implementation of bag
	template < class default_wrapper_t, class seq_t>
	struct bag_impl : bag_impl_base
	{
			typedef seq_t seq_type;
			
			typedef mpl::vector1<bag_impl> rt_args; // runtime args for use with adapters

			// base class for formal items
			struct formal_item_base : formal_item
			{
				typedef bag_impl	parent_type;
						
				parent_type * pParent;
				typedef _mpl_::vector1< formal_item> concepts;
			};		
			
			// will re-wrap an item if if is formal
			// result will be formal_item_base is applied 
			// as the base class to the nested 'item<>' template in 
			// passed class. 
			template <class T, class Enable = void> 
			struct rewrap_wrapped_modifier_if_formal  
			{  
				typedef _mpl_::void_ type;
			};

			template <class T>
			struct rewrap_wrapped_modifier_if_formal<T, typename boost::enable_if< has_concept<local_modifier, T >  >::type> 
			:	_mpl_::apply1
				<	T,
					typename if_formal_apply_base
					<	
						formal_item_base, 
						T
					>::type
				>
			{};
			
			// calculates the implementation type for an item
			struct calculate_impl_type
			{
				template <class T> 
				struct apply 
				: _mpl_::if_
				<
					has_concept<local_modifier, T >,		// local modifier in type?
					typename rewrap_wrapped_modifier_if_formal<T>::type, // rewrap it from modifier - else apply default
					typename _mpl_::apply1< default_wrapper_t, typename if_formal_apply_base< formal_item_base, T>::type > ::type
				>
				{};
			};	
			
			// will return a class of type 'data<>' which will have 
			// internal type information for the bag on how to interact 
			// with the item
			struct build_data_row
			{
				template <class idx_element_t, class seq_element_t > 
				struct apply 
				{
					typedef typename _mpl_::apply1<calculate_impl_type,seq_element_t>::type impl_type;
					typedef data
					<
						idx_element_t,
						typename key<seq_element_t>::type,
						typename impl_type::concepts,
						impl_type
					> type;
				};
			};	
			
			// sequential list of numbers for each item in passed sequence
			typedef _mpl_::range_c<int,0, _mpl_::size<seq_t>::value >		 seq_indexes;
	  
			// sequence with indices applied
			// takes the seq_indexes and builds a sequence of data<> elements
			// the resulting sequence will each have a unique index # in each data<> element
			typedef typename 
			_mpl_::transform_view
			<
				_mpl_::zip_view<_mpl_::vector<seq_indexes,seq_t> >
				, _mpl_::unpack_args< _mpl_::apply2<build_data_row,_mpl_::_1, _mpl_::_2 > > 
			>  ::type data_view; 
			
			// the inlined strategies- i.e. single<int,by_val> in a sequence form
			typedef typename _mpl_::transform_view< data_view, impl<_mpl_::_1> >::type data_view_inlined_strategies;

			// make a view of just the keys
			typedef typename _mpl_::transform_view<data_view, key_of>::type data_view__keys;

			// make a view of just the args
			typedef typename _mpl_::transform_view<data_view, arg_of>::type data_view__args;

			// view of just the contained bag types
			typedef typename _mpl_::filter_view
			< 
				data_view__keys , 
				_mpl_::lambda< is_bag< _mpl_::_1 > > 
			>::type bag_types_contained;


			// sequence of this bag's keys combined with any contained bags
			// note this will recursively dig into contained bags and pull out the keys 
			// will recurse until the leaf nodes are hit
			typedef typename  _mpl_::fold
			 <
				bag_types_contained // just the keys of any child bags
				, data_view__keys // seed it with this bag's keys
				, _mpl_::joint_view< _mpl_::_1, _mpl_::lambda< get_all_bag_keys< _mpl_::_2> >  >::type 
			>::type all_bag_keys; 

			// view of just the contained bag types - for all contained bags
			typedef typename _mpl_::filter_view
			< 
				all_bag_keys , 
				_mpl_::lambda< is_bag< _mpl_::_1 > > 
			>::type all_bag_types_contained;
			
			// data view of all items- including embedded bags
			typedef typename  _mpl_::fold
			 <
				bag_types_contained // just the keys of any child bags
				, data_view // seed it with this bag's dataview
				, _mpl_::joint_view< _mpl_::_1, _mpl_::lambda< get_data_view_all< _mpl_::_2> >  >::type 
			>::type data_view_all; 

			// helper template for calling arg_type<>::get-
			// this helper passes in the appropriate data_view and seq_t 
			// allowing the caller to only worry about the index of the item
			template <int idx>	struct argt : arg_type<data_view,seq_t>:: template get<idx> {};
			
			template <class arg_seq_t, class arg0_t> // todo remove arg_seq_t
			struct arg_to_filter_old
			{
				typedef typename _mpl_::lambda< arg0_t >::type arg0_t_lambda;
				typedef typename _mpl_::eval_if
				< 
					// is the arg a specific filter expression? (i.e. by_val, multiple<by_val> etc...)
					_mpl_::contains<arg_filters,arg0_t_lambda >,
					_mpl_::eval_if
					<
						// is the filter speifically a formal_item filter?	
						boost::is_same<arg0_t,formal_item>,

						// return mfc which will filter on key types which are formal items
						typename _mpl_::lambda< is_formal_item< key<_mpl_::_1>   > >::type,

						// else return a filter expression to look for the arg as a concept
						has_concept< arg0_t>
					>,
					// ELSE
					_mpl_::eval_if
					< 
						// is the arg a sequence?		
						typename _mpl_::is_sequence< arg0_t_lambda>::type, 
						// return mfc which will filter on key types which match the raw types in seq_t
						typename _mpl_::lambda< _mpl_::contains< arg0_t_lambda  , key<_mpl_::_1>   > >::type,
						// ELSE 
						// if the type is IN the list of keys for all items (deep)
						_mpl_::eval_if	
						<
							typename _mpl_::contains		// if the arg is in the sequence
							< 
								all_bag_keys, // all the deep types for this and contained bags 
								arg0_t_lambda
							>::type, 
							does_match<key_of,arg0_t >, // return a mfc for checking on a match of key with the passed arg
							// otherwise assume the arg is a lambda expression and return an MFC which will apply it to the key type
							eval_expression_on_key< arg0_t_lambda  >
						>
					>
				>::type type;
			};

			template <class arg0_t > // todo remove arg_seq_t class arg_seq_t, 
			struct arg_to_filter : arg_to_filter_impl <  arg0_t, all_bag_keys> {};

			// wrap for holding the internal implementation of the items
			template <class data_t>
			struct data_to_object_wrap
			{
				typedef data_to_object_wrap type;
				typedef typename impl<data_t>::type value_type;
				value_type value;						
			};
			
			// generate the class holding the objects used to store the item data
			typedef typename _mpl_::inherit_linearly
			<
				data_view, _mpl_::inherit<data_to_object_wrap<_mpl_::_2>,_mpl_::_1>
			>::type generated_objects_t;
			
			// instatiate the class holding all the objects in the bag
			generated_objects_t generated_objs;

			// inserts an item into the bag based on ordinal
			template <int int_idx_t>
			void insert(typename argt<int_idx_t>::type a0) 
			{
				elem<int_idx_t>().insert(a0);
			}

			// inserts an item into the bag based on type
			// will insert into the FIRST KEY FOUND that matches
			template <class T >
			void insert(typename locate<data_view,key_of,T>::type::impl_type::arg_type a0) 
			{
				typedef typename locate<data_view,key_of,T>::type::idx_type found_idx;
				elem< found_idx::value >().insert(a0);
			}
			// set an element based on ordinal
			// if the stategy is single, then the item gets the new value
			// otherwise - the item is cleared and set with a0
			template <int int_idx_t>
			typename get_value_type
			<
				typename data_to_object_wrap 
				< 
					typename locate<data_view,index_of,_mpl_::integral_c<int, int_idx_t> >::type 
				>::value_type   
			>::type &
			set(typename argt<int_idx_t>::type a0) 
			{
				elem<int_idx_t>().set(a0);
				return elem< int_idx_t >().val_as_ref();
			}

			// set an element based on type
			// if the stategy is single, then the item gets the new value
			// otherwise - the item is cleared and set with a0
			// first item with type found is used
			template <class key_t >
			typename get_value_type
			<
				typename data_to_object_wrap 
				< 
					typename locate<data_view,key_of,key_t >::type 
				>::value_type   
			>::type &
			set(typename locate<data_view,key_of,key_t>::type::impl_type::arg_type a0) 
			{
				typedef typename locate<data_view,key_of,key_t>::type::idx_type found_idx;
				elem< found_idx::value >().set(a0);
				return elem< found_idx::value >().val_as_ref();
			}
			
			// helper for getting the object via the data<> type
			template<class data_t>
			typename data_to_object_wrap < data_t>::value_type  & 
			get_object_by_data_type()
			{
				return static_cast< data_to_object_wrap <data_t> &>(generated_objs).value; 
			}
			
			// helper for getting an object via an mfc used 
			// to locate the data type
			template<class mfc_t, class value_t>
			typename data_to_object_wrap 
			< 
				typename locate<data_view,mfc_t,value_t>::type 
			>::value_type  & 
			get_object_by()
			{
				typedef typename locate<data_view,mfc_t,value_t>::type found_data_type;
				return 	get_object_by_data_type<found_data_type>();		
			}

			// get the raw internal element via ordinal
			template <int int_idx_t>
			typename data_to_object_wrap 
			< 
				typename locate<data_view,index_of,_mpl_::integral_c<int, int_idx_t> >::type 
			>::value_type  & 
			elem()
			{
				typedef typename locate<data_view,index_of,_mpl_::integral_c<int, int_idx_t> >::type  found_data_type;
				return 	get_object_by_data_type<found_data_type>();		
			}

			// get the item via key
			template <class key_t>
			typename get_value_type
			<
				typename data_to_object_wrap 
				< 
					typename locate<data_view,key_of,key_t >::type 
				>::value_type   
			>::type &
			item()
			{
				typedef typename locate<data_view,key_of,key_t >::type  found_data_type;
				return 	get_object_by_data_type<found_data_type>().val_as_ref();		
			}

			// get the item via ordinal
			template <int int_idx_t>
			typename get_value_type
			<
				typename data_to_object_wrap 
				< 
					typename locate<data_view,index_of,_mpl_::integral_c<int, int_idx_t> >::type 
				>::value_type   
			>::type &
			item()
			{
				typedef typename locate<data_view,index_of,_mpl_::integral_c<int, int_idx_t> >::type  found_data_type;
				return 	get_object_by_data_type<found_data_type>().val_as_ref();		
			}

			// speficically get the item via ordinal
			template <int int_idx_t>
			typename get_value_type
			<
				typename data_to_object_wrap 
				< 
					typename locate<data_view,index_of,_mpl_::integral_c<int, int_idx_t> >::type 
				>::value_type   
			>::type &
			item_at()
			{
				typedef typename locate<data_view,index_of,_mpl_::integral_c<int, int_idx_t> >::type  found_data_type;
				return 	get_object_by_data_type<found_data_type>().val_as_ref();		
			}

			// return the type of the item
			template <int int_idx_t>
			struct item_type
				: get_value_type
				<
					typename data_to_object_wrap 
					< 
						typename locate<data_view,index_of,_mpl_::integral_c<int, int_idx_t> >::type 
					>::value_type   
				>
			{};

			// visit the generated object
			// takes a ref to the bag, and a functor
			// the functor needs to visit the implementation of the item
			// where visit_gen_object needs to look up the item via
			// the internal data<> type
			// visit_gen_object isolates the passed functor from this 
			// internal mechanism and allows the functor to end up visiting
			// the implementation of the generated object
			template <class func_t>
			struct visit_gen_object
			{
				bag_impl		&			mbag;
				func_t	&			mfunc;
				
				visit_gen_object(bag_impl & bag_, func_t & func)	
				: mbag(bag_) , mfunc(func) {};
				
				template <class data_type_to_lookup_t>
				void operator()(data_type_to_lookup_t &)
				{
					mfunc(mbag.get_object_by_data_type<data_type_to_lookup_t>());
				}			
			};

			// iterates all of the internally stored items
			template <class func_t>
			void for_each_raw(func_t  func)
			{
				_mpl_::for_each<data_view>(visit_gen_object<func_t>(*this,func) );
			}

			// takes the received target- and hands the passed functor to the target's visit_each() member
			template <class orig_arg_t, class functor_t> 
			struct call_visit_each_on_target
			{
				typedef call_visit_each_on_target type;
				typedef orig_arg_t	orig_arg_type;
				functor_t & m_functor;

				call_visit_each_on_target(functor_t & functor)
				: m_functor(functor)	{};
				
				template <class target_t>
				void operator()(target_t &t )
				{			
					t.template visit_each< orig_arg_type, functor_t>(m_functor);				
				}
			};

			// will vist each gen object which has an exact raw type match 
			// with the passed sequence
			template <class vec_t, class func_t>
			void for_each_raw_filtered_new(func_t  func)
			{
			
				typedef _mpl_::filter_view< data_view , _mpl_::contains<vec_t, index_< _mpl_::_1> >   >  filtered_sequence;				
				_mpl_::for_each< filtered_sequence >(visit_gen_object<func_t>(*this,func) );
			}

			template <class exp_t>
			void print_storage()
			{
	
				// make a view of all things with match the expression
				typedef _mpl_::filter_view< data_view , _mpl_::or_<_mpl_::apply1<typename exp_t::type::type ,_mpl_::_1>,is_bag< key<_mpl_::_1> > >   >  filtered_sequence;
				print_type pt;
				_mpl_::for_each< filtered_sequence >(visit_gen_object<print_type>(*this,pt) );
			}

			// will vist each gen object which has an exact raw type match 
			// with the passed sequence
			template <class exp_t, class func_t>
			void for_each_raw_filtered(func_t  func)
			{
				// make a view of all things with match the expression
				typedef _mpl_::filter_view< data_view , _mpl_::or_<_mpl_::apply1<typename exp_t::type,_mpl_::_1>,is_bag< key<_mpl_::_1> > >   >  filtered_sequence;
				_mpl_::for_each< filtered_sequence >(visit_gen_object<func_t>(*this,func) );
			}
			// visit the gen objects- applying the filter to the internal objects
			// will then proxy out the functor to the actual implementation of the item
			template <class exp_t, class func_t>
			void for_each_raw_filtered_visit_each(func_t  func)
			{
				call_visit_each_on_target<exp_t,func_t> cvet(func) ;
				typedef _mpl_::filter_view< data_view , _mpl_::or_<_mpl_::apply1<typename exp_t::type,_mpl_::_1>,is_bag< key<_mpl_::_1> > >   >  filtered_sequence;
				_mpl_::for_each< filtered_sequence >(visit_gen_object<call_visit_each_on_target<exp_t,func_t> >(*this,cvet) );
			}

			// visit each item
			template <class func_t>
			void for_each(func_t  func)
			{
				call_visit_each_on_target<_mpl_::void_,func_t> cvet(func) ;
				for_each_raw(cvet );
			}
			// visit each item with a valid argument
			template <class arg_t, class func_t> 
			void for_each(func_t  func)
			{
				for_each_raw_filtered_visit_each<arg_to_filter< arg_t > >(func );
			}
			// todo- go away?
			template <class exp_t, class func_t> 
			void for_each_if(func_t  func)
			{
				call_visit_each_on_target<_mpl_::void_,func_t> cvet(func) ;
				for_each_raw_filtered< eval_expression_on_key< typename _mpl_::lambda< exp_t >::type >  >(cvet );
			}

			// types is any filter param
			// exp_t is a unary metafunction class
			template<class arg_t, class exp_t, class func_t>											 
			void for_each_if(func_t  func)
			{
				typedef typename arg_to_filter<arg_t>::type filter_mfc;
				typedef pass_functor_to_target_matching_exp<   exp_t, func_t > func_proxy_type;
				func_proxy_type func_proxy(func);
				call_visit_each_on_target< _mpl_::void_,func_proxy_type > cvet( func_proxy ) ;
				for_each_raw_filtered< filter_mfc  >(cvet );
			}
		private:
			// passes a functor to a target if the target matches the compile time 
			// expression
			template<class exp_t, class functor_t>											 
			struct pass_functor_to_target_matching_exp
			{	
				public:
				typedef pass_functor_to_target_matching_exp type;
				functor_t & m_functor;

				typedef typename _mpl_::lambda< exp_t >::type exp_func_t;

				pass_functor_to_target_matching_exp(functor_t & functor)
				: m_functor(functor)	{};

				private:
					struct do_visit
					{
						template <class target_t>
						do_visit(target_t &t, functor_t & functor )
						{
							functor(t);
						}
					};
					struct do_not_visit
					{
						template <class target_t>
						do_not_visit(target_t &, functor_t &  )
						{
							// this space for rent
						}
					};
				public:			
				template <class target_t>
				void operator()(target_t &t )
				{			
					typename _mpl_::if_
					<	typename exp_func_t::template apply
						<
							target_t 
						>::type, 
						do_visit, 
						do_not_visit
					>::type (t, m_functor);
				}
			};
			
			public:
			
			template <class arg_t>
			struct remove_first_if_equal
			{
				arg_t		m_arg;
				bool		m_bfound;
				remove_first_if_equal(arg_t  arg)
				: m_arg(arg), m_bfound(false) 
				{ }
				template <class T>
				void operator()(T & obj)
				{
					if (m_bfound)
						return;
					if (obj.is_equal(m_arg) == true)
					{
						obj.remove(m_arg);
						m_bfound = true;
					}
				}
			};
			public:
			 
			template <class by_t, class T>
			bool remove(T & obj, typename enable_if<boost::is_same<by_ref, by_t> >::type* dummy = 0)
			{
				remove_first_if_equal<T&> rm_functor(obj); 
				for_each_raw_filtered< has_concept<by_t> >(rm_functor );
				return rm_functor.m_bfound;
			}
			template <class by_t, class T>
			bool remove(T obj, typename enable_if<boost::is_same<by_val, by_t> >::type* dummy = 0)
			{
				remove_first_if_equal<T> rm_functor(obj); 
				for_each_raw_filtered< has_concept<by_t> >(rm_functor );
				return rm_functor.m_bfound;
			}
			public:
				typedef mpl::false_ is_filter ;	// this is a bag not a filter
				
			////////////////////////////////////////////
			// BAG Metaprogramming interface
			
			// structure for presenting some useful meta interfaces from this bag
			// 
			struct config
			{
				struct error{ typedef error type; };
				
				/////////////////////
				// internal helpers
				private:
					// takes the passed concept list- and returns the matching instance type
					// todo- for the basic and code stuff we DONT need concepts (!)
					template <class target_concepts>
					struct instance_concept_to_type
					:	_mpl_::if_
						< 
							_mpl_::contains< target_concepts, by_val>, 
							by_val,
							// ELSE
							typename _mpl_::if_
							< 
								_mpl_::contains< target_concepts, by_ref>, 
								by_ref,
								// ELSE
								typename _mpl_::if_
								< 
									_mpl_::contains< target_concepts, by_auto_ref>, 
									by_auto_ref,
									// ELSE
									typename _mpl_::if_
									< 
										_mpl_::contains< target_concepts, by_shared_ref>, 
										by_shared_ref,
										// ELSE
										error
									>::type
								>::type
							>::type
						>::type
					{};
					// takes a concept list, finds the dimension and returns it
					// returns multiple_ in place of multiple<> and single_ in place of single<>
					template <class target_concepts>
					struct dimension_concept_to_type
					:	_mpl_::if_
						< 
							_mpl_::contains< target_concepts, multiple_>, 
							multiple_,
							// ELSE
							typename _mpl_::if_
							< 
								_mpl_::contains< target_concepts, single_>, 
								single_,
								// ELSE
								error
							>::type
						>::type
					{};

					// given an internal storage type- will return a string 
					// denoting the dimension 
					template< class T>
					struct impl_to_dimension_string
					{
						typedef impl_to_dimension_string type;

						struct _single	{	std::string name()		{	return "single";	}	};
						struct _multiple{	std::string name()		{	return "multiple";	}	};
						struct _error	{	std::string name()		{	return "*ERROR*";	}	};
						
						std::string operator()()
						{
							return
							typename _mpl_::if_
							<
								has_concept<single<by_any>,T>,
								_single,
								typename _mpl_::if_
								<	
									has_concept<multiple<by_any>,T>, 
									_multiple,
									_error
								>::type 
							>::type().name(); 
						}
					};

					// given an internal data<> class - will return string form of storage
					template< class T>
					struct impl_to_instance_string
					{
						typedef impl_to_instance_string type;
						 
						struct _by_val			{	std::string name()	{	return "by_val";			}	};
						struct _by_ref			{	std::string name()	{	return "by_ref";			}	};
						struct _by_auto_ref		{	std::string name()	{	return "by_auto_ref";		}	};
						struct _by_shared_ref	{	std::string name()	{	return "by_shared_ref";	}	};
						struct _error			{	std::string name()	{	return "*ERROR*";			}	};
						
						std::string operator()()
						{
							return				// this could be replaced by a map, once we have typedef's for these things
							typename _mpl_::if_
							<
								has_concept<by_val,T>,
								_by_val,
								typename _mpl_::if_
								<	
									has_concept<by_ref,T>, 
									_by_ref,
									typename _mpl_::if_
									<
										has_concept<by_auto_ref,T>, 
										_by_auto_ref,
										typename _mpl_::if_
										<
											has_concept<by_shared_ref,T>, 
											_by_shared_ref,
											_error
										>::type
									>::type
								>::type 
							>::type().name() ; 
						}
					};
					
					// given the data<> class will return
					// a strategy for that data class
					// i.e. single<by_val> etc..
					template <class data_t>
					struct impl_to_strategy
					{
						struct error{ typedef error type; };
				
						// lookup the instance type
						typedef typename instance_concept_to_type< typename  data_t::concepts>::type instance_type;
						typedef typename _mpl_::if_
						< 
							// if it is a single
							has_concept< single_,data_t >,
							typename single<instance_type>::type,
							// ELSE if it is multiple
							typename _mpl_::if_
							< 
								has_concept< multiple_,data_t >,
								typename multiple<instance_type>::type,
								// ELSE
								error
							>::type
						>::type type;
					};
				
				public:
					// slot size of bag (i.e. number of slots)
					typedef _mpl_::size<seq_t> slot_size;
					
					// valid strategies
					typedef _mpl_::vector8< single<by_val>, single<by_ref>, single<by_auto_ref>, single<by_shared_ref>, multiple<by_val>, multiple<by_ref>, multiple<by_auto_ref>, multiple<by_shared_ref> > valid_strategies;
					
					// returns true if T is a valid strategy
					template <class T> struct is_valid_strategy : _mpl_::contains< valid_strategies, T> {};

					// valid instances
					typedef _mpl_::vector4< by_val, by_ref, by_auto_ref, by_shared_ref > valid_instances;
					
					// returns true if T is a valid instance
					template <class T> struct is_valid_instance : _mpl_::contains< valid_instances, T> {};

					// valid dimensions
					typedef _mpl_::vector2< single_,multiple_ > valid_dimensions;
					
					// returns true if T is a valid instance
					template <class T> struct is_valid_dimension : _mpl_::contains< valid_dimensions, T> {};
					
					// sequence of all the keys
					typedef data_view__keys	keys ;
					
					// the inlined strategies- i.e. single<int,by_val> in a sequence form
					typedef data_view_inlined_strategies inlined_strategies;
					
					// todo 	* make a all_inlined_strategies view	
					
					// sequence of all the keys - includes items in contained bags
					typedef all_bag_keys	all_keys;
					
					// list of all bag types contained in the bag
					typedef bag_types_contained contained_bags;

					// list of all bag types contained in the bag
					typedef all_bag_types_contained all_contained_bags;
					
					// formal item keys in this bag
					typedef typename _mpl_::filter_view
					< 
						keys , 
						_mpl_::lambda< is_formal_item< _mpl_::_1 > > 
					>::type formal_items;

					// filtered keys
					// will take any valid filter and apply to keys
					template <class filter_arg_t>
					struct filtered_keys
					:	_mpl_::transform_view
						<
							_mpl_::filter_view
							< 
								data_view , 
								_mpl_::apply1
								<
									typename arg_to_filter<  filter_arg_t  >::type 
									,_mpl_::_1
								>
							>,
							key_of
						>
					{};
					// filtered ordinals
					// will take any valid filter and return a list of ordinals
					template <class filter_arg_t>
					struct filtered_ordinals
					:	make_vector
						<
						_mpl_::transform_view
						<
							_mpl_::filter_view
							< 
								data_view , 
								_mpl_::apply1
								<
									typename arg_to_filter<  filter_arg_t  >::type 
									,_mpl_::_1
								>
							>,
							index_of
						>
						>
					{};

					// filtered all keys
					// will take any valid filter and apply to keys
					// like filtered_keys- except includes on keys list from embedded bags
					template <class filter_arg_t>
					struct filtered_all_keys
					:	_mpl_::transform_view
						<
							_mpl_::filter_view
							< 
								data_view_all , 
								_mpl_::apply1
								<
									typename arg_to_filter< filter_arg_t  >::type 
									,_mpl_::_1
								>
							>,
							key_of
						>
					{};

					// all formal item keys in this bag and contained
					typedef typename _mpl_::filter_view
					< 
						all_keys , 
						_mpl_::lambda< is_formal_item< _mpl_::_1 > > 
					>::type all_formal_items;
					
					// list of all the slot ordinals in the bag
					typedef seq_indexes slot_ordinals;
					
					
				/////////////////////
				// DIMENSION 
				public:
					template <class key_t>
					struct dimension_of // returns either single_ or multiple_ to represent single<> and multiple<>
					: dimension_concept_to_type< typename locate<data_view,key_of, key_t >::type::concepts > {};

					template <int int_idx_t>
					struct dimension_at // returns either single_ or multiple_ to represent single<> and multiple<>
					: dimension_concept_to_type< typename locate<data_view,index_of,_mpl_::integral_c<int, int_idx_t> >::type::concepts > {};

					template <class key_t>
					struct dimension_of_string 
					: impl_to_dimension_string< typename locate<data_view,key_of, key_t >::type > {};

					template <int int_idx_t>
					struct dimension_at_string 
					: impl_to_dimension_string< typename locate<data_view,index_of,_mpl_::integral_c<int, int_idx_t> >::type > {};

					
					// true if item at key is of passed dimension
					template <class key_t, class dimension_t>
					struct is_dimension_of 
					: has_concept<dimension_t, typename locate<data_view,key_of, key_t >::type > 
					{	
						BOOST_MPL_ASSERT(( is_valid_dimension<dimension_t > ));
					};

					// true if item at ordinal is of passed dimension
					template <int int_idx_t, class dimension_t>
					struct is_dimension_at 
					: has_concept<dimension_t, typename locate<data_view,index_of,_mpl_::integral_c<int, int_idx_t> >::type > 
					{	
						BOOST_MPL_ASSERT(( is_valid_dimension<dimension_t > ));
					};

				/////////////////////
				// INSTANCE 
				public:
					template <class key_t>
					struct instance_of // returns either single_ or multiple_ to represent single<> and multiple<>
					: instance_concept_to_type< typename locate<data_view,key_of, key_t >::type::concepts > {};

					template <int int_idx_t>
					struct instance_at // returns either single_ or multiple_ to represent single<> and multiple<>
					: instance_concept_to_type< typename locate<data_view,index_of,_mpl_::integral_c<int, int_idx_t> >::type::concepts > {};

					template <class key_t>
					struct instance_of_string 
					: impl_to_instance_string< typename locate<data_view,key_of, key_t >::type > {};

					template <int int_idx_t>
					struct instance_at_string 
					: impl_to_instance_string< typename locate<data_view,index_of,_mpl_::integral_c<int, int_idx_t> >::type > {};

					// true if item at key is of passed instance
					template <class key_t, class instance_t>
					struct is_instance_of 
					: has_concept<instance_t, typename locate<data_view,key_of, key_t >::type > 
					{	
						BOOST_MPL_ASSERT(( is_valid_instance<instance_t > ));
					};

					// true if item at ordinal is of passed dimension
					template <int int_idx_t, class instance_t>
					struct is_instance_at 
					: has_concept<instance_t, typename locate<data_view,index_of,_mpl_::integral_c<int, int_idx_t> >::type > 
					{	
						BOOST_MPL_ASSERT(( is_valid_instance<instance_t > ));
					};
				/////////////////////
				// KEY 
				public:
					template <int int_idx_t>
					struct key_at	// returns the key at the specified orginal
					{
						typedef typename locate<data_view,index_of,_mpl_::integral_c<int, int_idx_t> >::type::key_type type;
					};
					
					// returns true if this bag has passed key
					template< class key_t>	struct has_key : _mpl_::contains< data_view__keys,key_t>	{};

				/////////////////////
				// arg 
				public:
					template <class key_t>
					struct arg_of	// returns the key at the specified orginal
					{
						typedef typename locate<data_view,key_of,key_t >::type::impl_type::arg_type type;
					};
					template <int int_idx_t>
					struct arg_at	// returns the key at the specified orginal
					{
						typedef typename locate<data_view,index_of,_mpl_::integral_c<int, int_idx_t> >::type::impl_type::arg_type type;
					};
					
					// returns true if this bag has passed key
					template< class arg_t>	struct has_arg : _mpl_::contains< data_view__keys,key_t>	{};
					
					typedef typename make_vector< data_view__args >::type all_args;

				/////////////////////
				// return_type_at 
				public:
				
				template <int int_idx_t>
				struct return_type_at
				:	get_value_type
					<
						typename data_to_object_wrap 
						< 
							typename locate<data_view,index_of,_mpl_::integral_c<int, int_idx_t> >::type 
						>::value_type   
					> 
				{};
					
				/////////////////////
				// Item Strategy 
				public:
					template <class key_t>
					struct strategy_of 
					{	
						typedef typename impl_to_strategy<  typename locate<data_view,key_of, key_t >::type  >::type type;
					};
					template <int int_idx_t>
					struct strategy_at	
					{
						typedef typename impl_to_strategy< typename locate<data_view,index_of,_mpl_::integral_c<int, int_idx_t> >::type >::type type;
					};
					template <class key_t>
					struct strategy_of_string 
					{	
						struct type
						{
							std::string operator()() { return typename dimension_of_string<key_t>::type()() + "<" + typename instance_of_string<key_t>::type()() + ">";	}
						};
					};
					template <int int_idx_t>
					struct strategy_at_string 
					{	
						struct type
						{
							std::string operator()() { return typename dimension_at_string<int_idx_t>::type()() + "<" + typename instance_at_string<int_idx_t>::type()() + ">";	}
						};
					};

				/////////////////////
				// Embedded Bags 
				public:
					struct has_embedded_bag : _mpl_::greater< _mpl_::size<contained_bags>, _mpl_::int_<0> > {};
				
			};
			
			// constructor implemenation
			// NOTE: argt<> ends up getting the argument type for the item from the strategy
			bag_impl()
			{
			}
			bag_impl(typename argt<0>::type a0)
			{
				elem<0>().set(a0);
			}
			bag_impl(typename argt<0>::type a0, typename argt<1>::type a1)
			{
				elem<0>().set(a0);
				elem<1>().set(a1);
			}
			bag_impl(typename argt<0>::type a0, typename argt<1>::type a1, typename argt<2>::type a2)
			{
				elem<0>().set(a0);
				elem<1>().set(a1);
				elem<2>().set(a2);
			}
			bag_impl(typename argt<0>::type a0, typename argt<1>::type a1, typename argt<2>::type a2, typename argt<3>::type a3)
			{
				elem<0>().set(a0);
				elem<1>().set(a1);
				elem<2>().set(a2);
				elem<3>().set(a3);
			}
			bag_impl(typename argt<0>::type a0, typename argt<1>::type a1, typename argt<2>::type a2, typename argt<3>::type a3, typename argt<4>::type a4)
			{
				elem<0>().set(a0);
				elem<1>().set(a1);
				elem<2>().set(a2);
				elem<3>().set(a3);
				elem<4>().set(a4);
			}
			bag_impl(typename argt<0>::type a0, typename argt<1>::type a1, typename argt<2>::type a2, typename argt<3>::type a3, typename argt<4>::type a4, typename argt<5>::type a5)
			{
				elem<0>().set(a0);
				elem<1>().set(a1);
				elem<2>().set(a2);
				elem<3>().set(a3);
				elem<4>().set(a4);
				elem<5>().set(a5);
			}
			bag_impl(typename argt<0>::type a0, typename argt<1>::type a1, typename argt<2>::type a2, typename argt<3>::type a3, typename argt<4>::type a4, typename argt<5>::type a5, typename argt<6>::type a6)
			{
				elem<0>().set(a0);
				elem<1>().set(a1);
				elem<2>().set(a2);
				elem<3>().set(a3);
				elem<4>().set(a4);
				elem<5>().set(a5);
				elem<6>().set(a6);
			}
			bag_impl(typename argt<0>::type a0, typename argt<1>::type a1, typename argt<2>::type a2, typename argt<3>::type a3, typename argt<4>::type a4, typename argt<5>::type a5, typename argt<6>::type a6, typename argt<7>::type a7)
			{
				elem<0>().set(a0);
				elem<1>().set(a1);
				elem<2>().set(a2);
				elem<3>().set(a3);
				elem<4>().set(a4);
				elem<5>().set(a5);
				elem<6>().set(a6);
				elem<7>().set(a7);
			}
			bag_impl(typename argt<0>::type a0, typename argt<1>::type a1, typename argt<2>::type a2, typename argt<3>::type a3, typename argt<4>::type a4, typename argt<5>::type a5, typename argt<6>::type a6, typename argt<7>::type a7, typename argt<8>::type a8)
			{
				elem<0>().set(a0);
				elem<1>().set(a1);
				elem<2>().set(a2);
				elem<3>().set(a3);
				elem<4>().set(a4);
				elem<5>().set(a5);
				elem<6>().set(a6);
				elem<7>().set(a7);
				elem<8>().set(a8);
			}
			bag_impl(typename argt<0>::type a0, typename argt<1>::type a1, typename argt<2>::type a2, typename argt<3>::type a3, typename argt<4>::type a4, typename argt<5>::type a5, typename argt<6>::type a6, typename argt<7>::type a7, typename argt<8>::type a8, typename argt<9>::type a9)
			{
				elem<0>().set(a0);
				elem<1>().set(a1);
				elem<2>().set(a2);
				elem<3>().set(a3);
				elem<4>().set(a4);
				elem<5>().set(a5);
				elem<6>().set(a6);
				elem<7>().set(a7);
				elem<8>().set(a8);
				elem<9>().set(a9);
			}
			bag_impl(typename argt<0>::type a0, typename argt<1>::type a1, typename argt<2>::type a2, typename argt<3>::type a3, typename argt<4>::type a4, typename argt<5>::type a5, typename argt<6>::type a6, typename argt<7>::type a7, typename argt<8>::type a8, typename argt<9>::type a9, typename argt<10>::type a10)
			{
				elem<0>().set(a0);
				elem<1>().set(a1);
				elem<2>().set(a2);
				elem<3>().set(a3);
				elem<4>().set(a4);
				elem<5>().set(a5);
				elem<6>().set(a6);
				elem<7>().set(a7);
				elem<8>().set(a8);
				elem<9>().set(a9);
				elem<10>().set(a10);
			}
			bag_impl(typename argt<0>::type a0, typename argt<1>::type a1, typename argt<2>::type a2, typename argt<3>::type a3, typename argt<4>::type a4, typename argt<5>::type a5, typename argt<6>::type a6, typename argt<7>::type a7, typename argt<8>::type a8, typename argt<9>::type a9, typename argt<10>::type a10, typename argt<11>::type a11)
			{
				elem<0>().set(a0);
				elem<1>().set(a1);
				elem<2>().set(a2);
				elem<3>().set(a3);
				elem<4>().set(a4);
				elem<5>().set(a5);
				elem<6>().set(a6);
				elem<7>().set(a7);
				elem<8>().set(a8);
				elem<9>().set(a9);
				elem<10>().set(a10);
				elem<11>().set(a11);
			}
			bag_impl(typename argt<0>::type a0, typename argt<1>::type a1, typename argt<2>::type a2, typename argt<3>::type a3, typename argt<4>::type a4, typename argt<5>::type a5, typename argt<6>::type a6, typename argt<7>::type a7, typename argt<8>::type a8, typename argt<9>::type a9, typename argt<10>::type a10, typename argt<11>::type a11, typename argt<12>::type a12)
			{
				elem<0>().set(a0);
				elem<1>().set(a1);
				elem<2>().set(a2);
				elem<3>().set(a3);
				elem<4>().set(a4);
				elem<5>().set(a5);
				elem<6>().set(a6);
				elem<7>().set(a7);
				elem<8>().set(a8);
				elem<9>().set(a9);
				elem<10>().set(a10);
				elem<11>().set(a11);
				elem<12>().set(a12);
			}
			bag_impl(typename argt<0>::type a0, typename argt<1>::type a1, typename argt<2>::type a2, typename argt<3>::type a3, typename argt<4>::type a4, typename argt<5>::type a5, typename argt<6>::type a6, typename argt<7>::type a7, typename argt<8>::type a8, typename argt<9>::type a9, typename argt<10>::type a10, typename argt<11>::type a11, typename argt<12>::type a12, typename argt<13>::type a13)
			{
				elem<0>().set(a0);
				elem<1>().set(a1);
				elem<2>().set(a2);
				elem<3>().set(a3);
				elem<4>().set(a4);
				elem<5>().set(a5);
				elem<6>().set(a6);
				elem<7>().set(a7);
				elem<8>().set(a8);
				elem<9>().set(a9);
				elem<10>().set(a10);
				elem<11>().set(a11);
				elem<12>().set(a12);
				elem<13>().set(a13);
			}
			bag_impl(typename argt<0>::type a0, typename argt<1>::type a1, typename argt<2>::type a2, typename argt<3>::type a3, typename argt<4>::type a4, typename argt<5>::type a5, typename argt<6>::type a6, typename argt<7>::type a7, typename argt<8>::type a8, typename argt<9>::type a9, typename argt<10>::type a10, typename argt<11>::type a11, typename argt<12>::type a12, typename argt<13>::type a13, typename argt<14>::type a14)
			{
				elem<0>().set(a0);
				elem<1>().set(a1);
				elem<2>().set(a2);
				elem<3>().set(a3);
				elem<4>().set(a4);
				elem<5>().set(a5);
				elem<6>().set(a6);
				elem<7>().set(a7);
				elem<8>().set(a8);
				elem<9>().set(a9);
				elem<10>().set(a10);
				elem<11>().set(a11);
				elem<12>().set(a12);
				elem<13>().set(a13);
				elem<14>().set(a13);
			}
	};

}; // namespace detail

// Public interface to the bag

template < class T0  = _mpl_::void_, class T1  = _mpl_::void_,class T2  = _mpl_::void_,class T3  = _mpl_::void_>
struct bag;

template < class seq_t>
struct bag<seq_t> : public detail::bag_impl<single<by_val>, seq_t>
{	
	typedef single<by_val> wrapper_t;
	typedef typename detail::bag_impl<wrapper_t, seq_t>::data_view data_view;
	template <int idx>	struct argt : detail::arg_type<data_view,seq_t>:: template get<idx> {};
		
	bag()	{	}
	bag(typename argt<0>::type a0)	: detail::bag_impl<wrapper_t, seq_t>(a0)	{	}
	bag(typename argt<0>::type a0, typename argt<1>::type  a1)	: detail::bag_impl<wrapper_t, seq_t>(a0, a1)	{	}
	bag(typename argt<0>::type a0, typename argt<1>::type  a1, typename argt<2>::type  a2)	: detail::bag_impl<wrapper_t, seq_t>(a0, a1, a2)	{	}
	bag(typename argt<0>::type a0, typename argt<1>::type  a1, typename argt<2>::type  a2, typename argt<3>::type  a3)	: detail::bag_impl<wrapper_t, seq_t>(a0, a1, a2, a3)	{	}
	bag(typename argt<0>::type a0, typename argt<1>::type  a1, typename argt<2>::type  a2, typename argt<3>::type  a3, typename argt<4>::type  a4)	: detail::bag_impl<wrapper_t, seq_t>(a0, a1, a2, a3, a4)	{	}
	bag(typename argt<0>::type a0, typename argt<1>::type  a1, typename argt<2>::type  a2, typename argt<3>::type  a3, typename argt<4>::type a4, typename argt<5>::type a5)	: detail::bag_impl<wrapper_t, seq_t>(a0, a1, a2, a3, a4, a5)	{	}
	bag(typename argt<0>::type a0, typename argt<1>::type  a1, typename argt<2>::type  a2, typename argt<3>::type  a3, typename argt<4>::type a4, typename argt<5>::type a5, typename argt<6>::type a6)	: detail::bag_impl<wrapper_t, seq_t>(a0, a1, a2, a3, a4, a5, a6)	{	}
	bag(typename argt<0>::type a0, typename argt<1>::type  a1, typename argt<2>::type  a2, typename argt<3>::type  a3, typename argt<4>::type a4, typename argt<5>::type a5, typename argt<6>::type a6, typename argt<7>::type a7)	: detail::bag_impl<wrapper_t, seq_t>(a0, a1, a2, a3, a4, a5, a6, a7)	{	}
	bag(typename argt<0>::type a0, typename argt<1>::type  a1, typename argt<2>::type  a2, typename argt<3>::type  a3, typename argt<4>::type a4, typename argt<5>::type a5, typename argt<6>::type a6, typename argt<7>::type a7, typename argt<8>::type a8)	: detail::bag_impl<wrapper_t, seq_t>(a0, a1, a2, a3, a4, a5, a6, a7, a8)	{	}
	bag(typename argt<0>::type a0, typename argt<1>::type  a1, typename argt<2>::type  a2, typename argt<3>::type  a3, typename argt<4>::type a4, typename argt<5>::type a5, typename argt<6>::type a6, typename argt<7>::type a7, typename argt<8>::type a8, typename argt<9>::type a9)	: detail::bag_impl<wrapper_t, seq_t>(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9)	{	}
	bag(typename argt<0>::type a0, typename argt<1>::type  a1, typename argt<2>::type  a2, typename argt<3>::type  a3, typename argt<4>::type a4, typename argt<5>::type a5, typename argt<6>::type a6, typename argt<7>::type a7, typename argt<8>::type a8, typename argt<9>::type a9, typename argt<10>::type a10)	: detail::bag_impl<wrapper_t, seq_t>(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)	{	}
	bag(typename argt<0>::type a0, typename argt<1>::type  a1, typename argt<2>::type  a2, typename argt<3>::type  a3, typename argt<4>::type a4, typename argt<5>::type a5, typename argt<6>::type a6, typename argt<7>::type a7, typename argt<8>::type a8, typename argt<9>::type a9, typename argt<10>::type a10, typename argt<11>::type a11)	: detail::bag_impl<wrapper_t, seq_t>(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11)  { }	bag(typename argt<0>::type a0, typename argt<1>::type  a1, typename argt<2>::type  a2, typename argt<3>::type  a3, typename argt<4>::type a4, typename argt<5>::type a5, typename argt<6>::type a6, typename argt<7>::type a7, typename argt<8>::type a8, typename argt<9>::type a9, typename argt<10>::type a10, typename argt<11>::type a11, typename argt<12>::type a12)	: detail::bag_impl<wrapper_t, seq_t>(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) {}	bag(typename argt<0>::type a0, typename argt<1>::type  a1, typename argt<2>::type  a2, typename argt<3>::type  a3, typename argt<4>::type a4, typename argt<5>::type a5, typename argt<6>::type a6, typename argt<7>::type a7, typename argt<8>::type a8, typename argt<9>::type a9, typename argt<10>::type a10, typename argt<11>::type a11, typename argt<12>::type a12, typename argt<13>::type a13)	: detail::bag_impl<wrapper_t, seq_t>(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12,a13) {}
	bag(typename argt<0>::type a0, typename argt<1>::type  a1, typename argt<2>::type  a2, typename argt<3>::type  a3, typename argt<4>::type a4, typename argt<5>::type a5, typename argt<6>::type a6, typename argt<7>::type a7, typename argt<8>::type a8, typename argt<9>::type a9, typename argt<10>::type a10, typename argt<11>::type a11, typename argt<12>::type a12, typename argt<13>::type a13, typename argt<14>::type a14)		: detail::bag_impl<wrapper_t, seq_t>(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14) {}
};


template < class default_wrapper_t, class seq_t>
struct bag<default_wrapper_t,seq_t>  : detail::bag_impl<default_wrapper_t, seq_t>
{
	typedef default_wrapper_t wrapper_t;
	typedef typename detail::bag_impl<wrapper_t, seq_t>::data_view data_view;
	template <int idx>	struct argt : detail::arg_type<data_view,seq_t>:: template get<idx> {};
		
	bag()	{	}
	bag(typename argt<0>::type a0)	: detail::bag_impl<wrapper_t, seq_t>(a0)	{	}
	bag(typename argt<0>::type a0, typename argt<1>::type  a1)	: detail::bag_impl<wrapper_t, seq_t>(a0, a1)	{	}
	bag(typename argt<0>::type a0, typename argt<1>::type  a1, typename argt<2>::type  a2)	: detail::bag_impl<wrapper_t, seq_t>(a0, a1, a2)	{	}
	bag(typename argt<0>::type a0, typename argt<1>::type  a1, typename argt<2>::type  a2, typename argt<3>::type  a3)	: detail::bag_impl<wrapper_t, seq_t>(a0, a1, a2, a3)	{	}
	bag(typename argt<0>::type a0, typename argt<1>::type  a1, typename argt<2>::type  a2, typename argt<3>::type  a3, typename argt<4>::type  a4)	: detail::bag_impl<wrapper_t, seq_t>(a0, a1, a2, a3, a4)	{	}
	bag(typename argt<0>::type a0, typename argt<1>::type  a1, typename argt<2>::type  a2, typename argt<3>::type  a3, typename argt<4>::type a4, typename argt<5>::type a5)	: detail::bag_impl<wrapper_t, seq_t>(a0, a1, a2, a3, a4, a5)	{	}
	bag(typename argt<0>::type a0, typename argt<1>::type  a1, typename argt<2>::type  a2, typename argt<3>::type  a3, typename argt<4>::type a4, typename argt<5>::type a5, typename argt<6>::type a6)	: detail::bag_impl<wrapper_t, seq_t>(a0, a1, a2, a3, a4, a5, a6)	{	}
	bag(typename argt<0>::type a0, typename argt<1>::type  a1, typename argt<2>::type  a2, typename argt<3>::type  a3, typename argt<4>::type a4, typename argt<5>::type a5, typename argt<6>::type a6, typename argt<7>::type a7)	: detail::bag_impl<wrapper_t, seq_t>(a0, a1, a2, a3, a4, a5, a6, a7)	{	}
	bag(typename argt<0>::type a0, typename argt<1>::type  a1, typename argt<2>::type  a2, typename argt<3>::type  a3, typename argt<4>::type a4, typename argt<5>::type a5, typename argt<6>::type a6, typename argt<7>::type a7, typename argt<8>::type a8)	: detail::bag_impl<wrapper_t, seq_t>(a0, a1, a2, a3, a4, a5, a6, a7, a8)	{	}
	bag(typename argt<0>::type a0, typename argt<1>::type  a1, typename argt<2>::type  a2, typename argt<3>::type  a3, typename argt<4>::type a4, typename argt<5>::type a5, typename argt<6>::type a6, typename argt<7>::type a7, typename argt<8>::type a8, typename argt<9>::type a9)	: detail::bag_impl<wrapper_t, seq_t>(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9)	{	}
	bag(typename argt<0>::type a0, typename argt<1>::type  a1, typename argt<2>::type  a2, typename argt<3>::type  a3, typename argt<4>::type a4, typename argt<5>::type a5, typename argt<6>::type a6, typename argt<7>::type a7, typename argt<8>::type a8, typename argt<9>::type a9, typename argt<10>::type a10)	: detail::bag_impl<wrapper_t, seq_t>(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10)	{	}
	bag(typename argt<0>::type a0, typename argt<1>::type  a1, typename argt<2>::type  a2, typename argt<3>::type  a3, typename argt<4>::type a4, typename argt<5>::type a5, typename argt<6>::type a6, typename argt<7>::type a7, typename argt<8>::type a8, typename argt<9>::type a9, typename argt<10>::type a10, typename argt<11>::type a11)	: detail::bag_impl<wrapper_t, seq_t>(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11)  { }	bag(typename argt<0>::type a0, typename argt<1>::type  a1, typename argt<2>::type  a2, typename argt<3>::type  a3, typename argt<4>::type a4, typename argt<5>::type a5, typename argt<6>::type a6, typename argt<7>::type a7, typename argt<8>::type a8, typename argt<9>::type a9, typename argt<10>::type a10, typename argt<11>::type a11, typename argt<12>::type a12)	: detail::bag_impl<wrapper_t, seq_t>(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12) {}	bag(typename argt<0>::type a0, typename argt<1>::type  a1, typename argt<2>::type  a2, typename argt<3>::type  a3, typename argt<4>::type a4, typename argt<5>::type a5, typename argt<6>::type a6, typename argt<7>::type a7, typename argt<8>::type a8, typename argt<9>::type a9, typename argt<10>::type a10, typename argt<11>::type a11, typename argt<12>::type a12, typename argt<13>::type a13)	: detail::bag_impl<wrapper_t, seq_t>(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12,a13) {}
	bag(typename argt<0>::type a0, typename argt<1>::type  a1, typename argt<2>::type  a2, typename argt<3>::type  a3, typename argt<4>::type a4, typename argt<5>::type a5, typename argt<6>::type a6, typename argt<7>::type a7, typename argt<8>::type a8, typename argt<9>::type a9, typename argt<10>::type a10, typename argt<11>::type a11, typename argt<12>::type a12, typename argt<13>::type a13, typename argt<14>::type a14)		: detail::bag_impl<wrapper_t, seq_t>(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14) {}
};

////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// HELPERS

template <class T>
struct get_value_c
{
	typedef get_value_c type;
	enum { value=T::value};
};

//////////////////////////////////////////////
// helpers

/*
	join_nested_seq
		takes a sequence of types- which themselves have nested sequences
		uses the passed "nested_resolver" to locate the nested sequences
		joins together the nested sequences
*/
template 
<
	class seq_t,
	class nested_retriever
>
struct join_nested_seq
: mpl::fold
	<
		typename mpl::transform
		<
			typename seq_t::type,
			nested_retriever
		>::type ,
		mpl::vector0<>,
		mpl::joint_view< mpl::_1,mpl::_2> 	
    >
{ };

// retrieves the keys from type as an mpl sequence
template <class T>
struct get_keys
{	
	typedef typename T::iv_keys::sequence type;
};

// retrieves the return types from type as an mpl sequence
template <class T>
struct get_rets
{	
	typedef typename T::iv_rets::sequence type;
};

// retrieves the arg types from type as an mpl sequence
template <class T>
struct get_args
{	
	typedef typename T::iv_args::sequence type;
};

// retrieves an mpl sequence of ordinals from the passed type
template <class T>
struct get_ordinals
{	
	typedef typename make_vector< typename T::iv_ordinals::sequence::type >::type type;
};

// retrieves an mpl sequence of ordinals from the passed type (end case)
// (having an end specialization simplifies recursive usage)
template <>
struct get_ordinals<end>
{	
	typedef mpl::vector0<> type;
};

// retrieves the deep ordinals from a given type as an mpl sequence
template <class T>
struct get_deep_ordinals
{	
	typedef typename make_vector< typename T::iv_deep_ordinals::sequence::type >::type type;
};

// retrieves the deep ordinals from a given type as an mpl sequence (end case)
// (having an end specialization simplifies recursive usage)
template <>
struct get_deep_ordinals<end>
{	
	typedef mpl::vector0<> type;
};

// checks if passed object is of the type passed
template<class looking_for_t, class obj_t>
bool is_obj_of_type(obj_t const & )
{
	if (boost::is_same<looking_for_t,obj_t>::value)
			return true;
	return false;
}

// retrieves the deep target type from passed
template <class T>
struct get_deep_target_type
{
	typedef typename T::deep_target_type type;
};

// retrieves the deep target type (as a pointer) from passed
template <class T>
struct get_deep_target_type_as_ptr
{
	typedef typename T::deep_target_type * type;
};

/*
	make_join_map
	takes a iv sequence of bags\adapaters
	uses exp_t to reteieve the nested sequence from item in iv_scan_t
	
	results in a an ordered sequence of mpl::pair<>s where
		first == the ordinal of the item
		second the ordinal of the item in the first item
	
	for each ordinal in the second item, the ordinal of the first is matched
	
*/
template 
<
	class exp_t,		// the resolver expression
	class iv_scan_t		// the sequence to scan	- a iv sequence of bags
>
struct make_join_map
{
	// implementation for make_join_map
	template <int curr_pos, int end_pos , class prev_seq_t >
	struct make_join_map_impl
	:	_mpl_::if_
		< 
			// if the current pos is less than the end pos
			_mpl_::less< mpl::int_<curr_pos>, mpl::int_<end_pos> >,
			make_join_map_impl	// recurse into ourselves - with incremeneted iter
			< 
				curr_pos +1,
				end_pos,						// repass- endpos					 
				mpl::joint_view	
				< 
					typename prev_seq_t::type,	// join previous sequence
					typename mpl::transform		// with a new sequence made up of pair<> where first is current pos, and second is the element in the range
					<
						typename mpl::apply1	// apply the resolver to the current item (to give us the seq to transform)
						<
							exp_t,	// resolver
							typename iv_scan_t:: template type_at<curr_pos>::type	// dereferenced type
						>::type,
						mpl::pair< mpl::int_<curr_pos>, mpl::_1>
					>::type
				> 
			>  ,
			// all done recursing, so return the previous sequence
			typename prev_seq_t::type
		>::type 
	{};
	
	// instantiation of the recursive zip_filter_impl struct
	typedef typename
	make_join_map_impl
	< 
		0,							// start at element 0
		iv_scan_t::size::value ,	// until the end of the sequence
		_mpl_::vector0<>			// start out with an empty vector
	>::type type;
};

////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// INDEXED STORAGE

/*
	purposely flattened out indexed storage
	allows a passed indexed_vector<> to be made into objects 
	which are then retrieveable via integral constant
	
	note: this code is a re-write from a version that utilzed mpl::inherit_linearly<>
			this more "concrete" version of this idiom compiles **much** faster for usage in bag
			(bag and supporting classes require heavy use of lookup via ordinal, which was killing compile times
			with the normal mpl way of doing things in regards to algorithms needed to support bag)
*/

template <int size_c, class seq_t>
struct indexed_storage
{	};

template < class iv_seq_t>
struct indexed_storage<1, iv_seq_t> 
{
	typedef typename iv_seq_t:: template type_at<0>::type T0;

	typedef indexed_storage	type; 
	typedef iv_seq_t		iv_seq;	// publish our iv seq
		
	T0 obj0;
	
	T0 & get_item_at(mpl::int_<0>)
	{
		return obj0;
	}
};

template < class iv_seq_t>
struct indexed_storage<2, iv_seq_t> 
{
	typedef typename iv_seq_t:: template type_at<0>::type T0;
	typedef typename iv_seq_t:: template type_at<1>::type T1;

	typedef indexed_storage	type; 
	typedef iv_seq_t		iv_seq;	// publish our iv seq
	
	T0 obj0;
	T1 obj1;
	
	T0 & get_item_at(mpl::int_<0>)
	{
		return obj0;
	}
	T1 & get_item_at(mpl::int_<1>)
	{
		return obj1;
	}
};

template < class iv_seq_t>
struct indexed_storage<3, iv_seq_t> 
{
	typedef typename iv_seq_t:: template type_at<0>::type T0;
	typedef typename iv_seq_t:: template type_at<1>::type T1;
	typedef typename iv_seq_t:: template type_at<2>::type T2;

	typedef indexed_storage	type; 
	typedef iv_seq_t		iv_seq;	// publish our iv seq
	
	T0 obj0;
	T1 obj1;
	T2 obj2;
	
	T0 & get_item_at(mpl::int_<0>)
	{
		return obj0;
	}
	T1 & get_item_at(mpl::int_<1>)
	{
		return obj1;
	}
	T2 & get_item_at(mpl::int_<2>)
	{
		return obj2;
	}
};

template < class iv_seq_t>
struct indexed_storage<4, iv_seq_t> 
{
	typedef typename iv_seq_t:: template type_at<0>::type T0;
	typedef typename iv_seq_t:: template type_at<1>::type T1;
	typedef typename iv_seq_t:: template type_at<2>::type T2;
	typedef typename iv_seq_t:: template type_at<3>::type T3;

	typedef indexed_storage	type; 
	typedef iv_seq_t		iv_seq;	// publish our iv seq
	
	T0 obj0;
	T1 obj1;
	T2 obj2;
	T3 obj3;
	
	T0 & get_item_at(mpl::int_<0>)
	{
		return obj0;
	}
	T1 & get_item_at(mpl::int_<1>)
	{
		return obj1;
	}
	T2 & get_item_at(mpl::int_<2>)
	{
		return obj2;
	}
	T3 & get_item_at(mpl::int_<3>)
	{
		return obj3;
	}
};

////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// FOR EACH ITEM

/* 
	visits each item in passed container
	container must support item_at<> and ::size
	
	as bag makes heavy use of visitation, this code was 
	re-written from a version which used mpl::for_each<> for 
	compile time improvement
	
*/
template <class container_t, class functor_t>
void for_each_impl(mpl::int_<1>, container_t & cont, functor_t & func)
{
	func(cont. template item_at<0>());
}

template <class container_t, class functor_t>
void for_each_impl(mpl::int_<2>, container_t & cont, functor_t &  func)
{
	func(cont. template item_at<0>());
	func(cont. template item_at<1>());
}

template <class container_t, class functor_t>
void for_each_impl(mpl::int_<3>, container_t & cont, functor_t & func)
{
	func(cont. template item_at<0>());
	func(cont. template item_at<1>());
	func(cont. template item_at<2>());
}

template <class container_t, class functor_t>
void for_each_impl(mpl::int_<4>, container_t & cont, functor_t & func)
{
	func(cont. template item_at<0>());
	func(cont. template item_at<1>());
	func(cont. template item_at<2>());
	func(cont. template item_at<3>());
}

template <class container_t, class functor_t>
void for_each_impl(mpl::int_<5>, container_t & cont, functor_t & func)
{
	func(cont. template item_at<0>());
	func(cont. template item_at<1>());
	func(cont. template item_at<2>());
	func(cont. template item_at<3>());
	func(cont. template item_at<4>());
}

template <class container_t, class functor_t>
void for_each_item( container_t & cont, functor_t func)
{
	for_each_impl(typename container_t::size(), cont, func);
}

////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// FOR EACH ORDINAL
/* 
	visits each ordinal in passed container
		in the order of the ordinals
	container must support item_at<> and ::size
	
	as bag makes heavy use of visitation, this code was 
	re-written from a version which used mpl::for_each<> for 
	compile time improvement
	
*/

template <class iv_ordinals_t, class container_t, class functor_t>
void for_each_ordinal_impl(mpl::int_<1>, container_t & cont, functor_t func)
{
	// access the iv_ordinals_t to retrieve the actual index into the container
	func(cont. template item_at< iv_ordinals_t::template type_at< 0>::type::value >());
}

template <class iv_ordinals_t, class container_t, class functor_t>
void for_each_ordinal_impl(mpl::int_<2>, container_t & cont, functor_t &  func)
{
	// access the iv_ordinals_t to retrieve the actual index into the container
	func(cont. template item_at< iv_ordinals_t::template type_at< 0>::type::value >());
	func(cont. template item_at< iv_ordinals_t::template type_at< 1>::type::value >());
}

template <class iv_ordinals_t, class container_t, class functor_t>
void for_each_ordinal_impl(mpl::int_<3>, container_t & cont, functor_t & func)
{
	// access the iv_ordinals_t to retrieve the actual index into the container
	func(cont. template item_at< iv_ordinals_t::template type_at< 0>::type::value >());
	func(cont. template item_at< iv_ordinals_t::template type_at< 1>::type::value >());
	func(cont. template item_at< iv_ordinals_t::template type_at< 2>::type::value >());
}

template <class iv_ordinals_t, class container_t, class functor_t>
void for_each_ordinal_impl(mpl::int_<4>, container_t & cont, functor_t & func)
{
	// access the iv_ordinals_t to retrieve the actual index into the container
	func(cont. template item_at< iv_ordinals_t::template type_at< 0>::type::value >());
	func(cont. template item_at< iv_ordinals_t::template type_at< 1>::type::value >());
	func(cont. template item_at< iv_ordinals_t::template type_at< 2>::type::value >());
	func(cont. template item_at< iv_ordinals_t::template type_at< 3>::type::value >());
}

template <class iv_ordinals_t, class container_t, class functor_t>
void for_each_ordinal_impl(mpl::int_<5>, container_t & cont, functor_t & func)
{
	// access the iv_ordinals_t to retrieve the actual index into the container
	func(cont. template item_at< iv_ordinals_t::template type_at< 0>::type::value >());
	func(cont. template item_at< iv_ordinals_t::template type_at< 1>::type::value >());
	func(cont. template item_at< iv_ordinals_t::template type_at< 2>::type::value >());
	func(cont. template item_at< iv_ordinals_t::template type_at< 3>::type::value >());
	func(cont. template item_at< iv_ordinals_t::template type_at< 4>::type::value >());
}

// accesses the iv_ordinals_t to retrieve the actual index into the container
template <class iv_ordinals_t, class container_t, class functor_t>
void for_each_ordinal( container_t & cont, functor_t func)
{
	for_each_ordinal_impl<iv_ordinals_t>(typename iv_ordinals_t::size(), cont, func);
}

}; // namespace bag
}; // namespace boost

#endif

Bag Visitation Library- Compile Time Meta Program

  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
/*  
 *   Brian Braatz.
 *   Copyright 2003-2012 Brian C Braatz. All rights reserved.
 *
 */

#ifndef __BOOST_BAG_FOR_ZIP_HPP
#define __BOOST_BAG_FOR_ZIP_HPP

#include <boost/mpl/not_equal_to.hpp>
#include <boost/mpl/comparison.hpp>
#include <boost/mpl/and.hpp>
#include <boost/mpl/logical.hpp>

namespace boost { namespace bag {

namespace _mpl_ = boost::mpl; // note: it is a common practice to use a "mpl" as an alias- though using it inside THIS namespace
							  // creates issues- so inside the bag namespace we use "_mpl_"

// tags for traits

struct static_model		{ typedef static_model type; };		// static model requires compile time recursion to iterate
struct dynamic_model	{ typedef dynamic_model type; };	// dynamic model changes at runtime

// returns true if the passed type has a nested model typedef which == static_model
template <class model_t>
struct is_model_static_impl
: _mpl_::false_	{};

template <>
struct is_model_static_impl<static_model>
: _mpl_::true_	{};

template <class T>
struct is_model_static : is_model_static_impl<typename T::model>	{};

template <class T>
struct not_is_model_static : _mpl_::not_< is_model_static_impl<typename T::model> >	{};

// trait for static_iterator
// provides functionality for iteration over any forward capable mpl sequence
struct mpl_seq
{
	typedef mpl_seq type;

	// expose iteration constructs
	template <class T>	struct begin	: _mpl_::begin<T> {};	
	template <class T>	struct end		: _mpl_::end<T> {};
	template <class T>	struct next		: _mpl_::next<T> {};
	template <class T>	struct deref	: _mpl_::deref<T> {};

	enum { arg_ordinal = -1}; // -1 means does not want an runtime argument

	// deref function for retrieving the current "value" in the sequence
	template <class static_iterator_t>   
	typename static_iterator_t::deref_current get_value()
	{
		return typename static_iterator_t::deref_current();
	}
	
	// returns mpl::true_ if passed iter is at the last position
	template <class iter_t, class last_iter_t>
	struct is_at_last
	: boost::is_same<iter_t,last_iter_t>
	{};
	
};

// internal trait for a sequence of bag slots
template <int ARG_NUM, class bag_t>
struct bag_slot_seq : mpl_seq 
{
	typedef bag_slot_seq type;

	typedef bag_t bag_type;			// type of bag
	typedef bag_type & arg_type;	// runtime argument type
	typedef bag_type  value_type;   // value type of argument
	
	// returns the return type of the passed iterator applied to the bag
	template <class static_iterator_t>   
	struct return_type
	: bag_type::config::template return_type_at
		<
			static_iterator_t::deref_current::value 
		>
	{};
	
	enum { arg_ordinal = ARG_NUM}; // initial argument ordinal desired
};

// static iterator and static_iterator_next are helpers for compile time iteration
// having these classes allows the (further) rather complicated code 
// which determines which argument to send to a function to be considerably simpler
template <class iter_traits_t, class seq_t, class current_iter_t  >
struct static_iterator_ : iter_traits_t
{
	typedef static_iterator_		type;			// type for this iter
	typedef iter_traits_t			iter_traits;	// traits for this iter
	typedef static_model			model;			// model for this iter
	typedef current_iter_t			current;		// current iter
	typedef typename seq_t::type 	seq_type;	

	typedef typename iter_traits:: template end<seq_type>::type				last;			// iter for last position
	typedef typename iter_traits:: template deref<current_iter_t>::type		deref_current;	// current iter - dereferenced
	
	enum { arg_ordinal = iter_traits_t::arg_ordinal}; // argument desried

	// is last ?
	typedef typename iter_traits:: template is_at_last<current, last>::type is_past_last_type;

	// is last - constant
	enum { is_past_last =   is_past_last_type::value };
};

template <class iter_traits_t, class seq_t >
struct static_iterator : static_iterator_<iter_traits_t, seq_t, typename iter_traits_t::template begin<seq_t>::type>
{};

template <class static_iterator_t>
struct static_iterator_next : static_iterator_t::iter_traits
{
	typedef static_iterator_next						type;			// type for this iter
	typedef typename static_iterator_t::iter_traits		iter_traits;	// traits for this iter
	typedef typename static_iterator_t::last			last;			// iter for last position
	
	typedef typename iter_traits::template next< typename static_iterator_t::current >::type		current;	// current iter
	typedef typename iter_traits::template deref<current>::type									deref_current;	// current iter - dereferenced

	// is last ?
	typedef typename iter_traits:: template is_at_last<current, last>::type is_past_last_type;

	enum { arg_ordinal = iter_traits::arg_ordinal}; // argument desried

	// is last
	enum { is_past_last =   is_past_last_type::value };
};

// template for determining if a iter is past the last value
template  <class static_iterator_t>
struct is_static_iterator_past_last
: static_iterator_t::is_past_last_type {};

// template for dereferencing the iter
template <class static_iter_t>
struct get_deref_current
{
	typedef typename static_iter_t::deref_current type;
};

// returns int_<0> if not at last otherwise returns int_<1>
template <class static_iter_t>
struct is_past_last_to_int
{
	typedef typename _mpl_::if_< _mpl_::bool_< static_iter_t::is_past_last > , _mpl_::int_<1>, _mpl_::int_<0> >::type type;
};

// holds multiple static iterators and allows them to simultanesouly iterate
template <class seq_static_iterators_t>
struct static_iterator_collection
{
	typedef static_iterator_collection type;						// type of this class
	typedef typename seq_static_iterators_t::type static_iterators;	// sequence of iterators we manage
	
	// returns a new static_iterator_collection, with all the iter elements incremented
	typedef static_iterator_collection
	<
		typename _mpl_::transform
		<
			static_iterators,
			static_iterator_next< _mpl_::_1>  
		>::type 
	> next;
	
	private:
		// list of just the values of the iterators past_last values
		typedef typename _mpl_::transform
		<
			static_iterators,
			is_past_last_to_int<_mpl_::_1>
		>::type vec_is_past_lasts;

	public:
	// number of contained iters which are past last
	typedef typename _mpl_::count_if
	<
		static_iterators,
		is_static_iterator_past_last<_mpl_::_1>
	>::type num_past_last;
	
	// if the number of iters which report past_last is greater than 0
	typedef typename _mpl_::if_
	< 
		_mpl_::greater< num_past_last, _mpl_::int_<0> >,
		_mpl_::true_, 
		_mpl_::false_ 

	>::type is_past_last_type; 

	// true if any contined iters are past the last
	enum 
	{ is_past_last =  is_past_last_type::value };
};


// zip trait for forward iteration over a std container
template <int ARG_NUM, class T>
struct std_each
{
	typedef T &							arg_type;			// argument type
	typedef dynamic_model				model;				// model for trait
	typedef _mpl_::true_					argument_wanted;	// defines if this trait receieves a runtime argument
	typedef typename T::value_type &	return_type;		// return type (passed to functor)
	
	enum { arg_ordinal = ARG_NUM};			// ordinal of argument we wish to receieve
	
	typename T::iterator it_current;		// iterator for current element
	typename T::iterator it_end;			// iterator for ending element
	
	// receieves the arg - expecting the arg denoted via arg_ordinal
	void receieve_arg( arg_type arg)
	{
		it_current	= arg.begin();
		it_end		= arg.end();
	}
	
	// returns the current value pointed to
	return_type get_value()
	{
		return*it_current;
	};

	// increments the runtime iterator
	void increment()
	{
		it_current++;
	}

	// returns true if runtime iterator is at end
	bool at_end()
	{
		return (it_current == it_end);
	}
};

// zip trait for passing a reference argument to a functor
template <int ARG_NUM, class T>
struct ref_argument
{
	typedef T &					arg_type;			// argument type
	typedef T &					return_type;		// return type
	typedef dynamic_model		model;				// model for trait
	typedef _mpl_::true_			argument_wanted;	// defines if this trait receieves a runtime argument

	enum { arg_ordinal = ARG_NUM};					// ordinal of argument we wish to receieve

	T * m_pVal;										// storage of argument
	
	// receieves the arg - expecting the arg denoted via arg_ordinal
	void receieve_arg( arg_type arg)
	{
		m_pVal = &arg; 
	}

	// increments the runtime iterator
	void increment()
	{
		// NOP
	}

	// returns the current value pointed to
	arg_type get_value()
	{
		return *m_pVal;
	};

	// returns true if runtime iterator is at end
	bool at_end()
	{
		return false;
	}
};

// public class for wrappering call to static_iterator
template <class seq_t>
struct mpl_each
: static_iterator<mpl_seq, seq_t >
{

};

template <int ARG_NUM = -1, class T0= _mpl_::void_, class T1= _mpl_::void_, class T2= _mpl_::void_>
struct bag_each_slot;
		
template <int ARG_NUM, class bag_t>
struct bag_each_slot<ARG_NUM, bag_t>
: static_iterator< bag_slot_seq<ARG_NUM,bag_t> , typename bag_t::config::slot_ordinals >
{ };


template <int ARG_NUM, class bag_t, class filter_t>
struct bag_each_slot<ARG_NUM, bag_t, filter_t>
: static_iterator< bag_slot_seq<ARG_NUM,bag_t> , typename bag_t::config::template filtered_ordinals<filter_t >::type >
{ };

template <class T0= _mpl_::void_, class T1= _mpl_::void_, class T2= _mpl_::void_>
struct bag_each_slot_ordinal;
		
template <class bag_t>
struct bag_each_slot_ordinal< bag_t>
: static_iterator<mpl_seq, typename bag_t::config::slot_ordinals >
{ };


template < class bag_t, class filter_t>
struct bag_each_slot_ordinal<bag_t, filter_t>
: static_iterator<mpl_seq, typename bag_t::config::template filtered_ordinals<filter_t >::type  >
{ };



// functor for calling the increment() method
struct increment_all
{
	template <class T>
	void operator()(T& obj)
	{
		obj.increment();
	}
};

// bag of functor arguments
template <class seq_t>
struct bag_functor_arguments : bag< seq_t >
{
	private:
		struct any_at_end_fctor	// todo replace this with for_until
		{
			bool & bRet;
			any_at_end_fctor(bool & b)
			: bRet(b)	{};
			
			template <class TT>
			void operator()(TT & v)
			{
				if (v.at_end())
					bRet = true;
			}
		};
		
	public:
		// returns true if any arguments are "at the end"
		bool any_at_end()
		{
			bool bRet = false;
			any_at_end_fctor f(bRet);
			for_each(f);
			
			return bRet;
		}
		// type of this class
		typedef  bag_functor_arguments type;
};

// holds a ordinal which reference the location of a type in a static sequence
// (static_ord_ref  implementation for a static trait which DOES NOT WANT a runtime argument)
template <class iter_t, class Enable = void>
struct static_ord_ref 
{
	typedef static_ord_ref				type;
	typedef static_model				model;
	typedef typename iter_t::pos		pos;
	enum { value = pos::value	};		// the position in the static iter collection we point to
	
	typedef _mpl_::false_ argument_wanted;

	void increment()
	{
		// NOP
	}
};

// (static_ord_ref implementation for a static trait which wants a runtime argument)
template 
<
	class iter_t
>
struct static_ord_ref
<
	iter_t, 
	typename boost::enable_if 
	< 
		_mpl_::not_equal_to	// arg ordinal of dereferenced iter is not equal to -1
		< 
			_mpl_::int_< _mpl_::deref<iter_t >::type::arg_ordinal>, 
			_mpl_::int_<-1> 
		> 
	>::type 
> 
{
	typedef static_ord_ref				type;
	typedef static_model				model;
	typedef typename iter_t::pos		pos;
	enum { value = pos::value	};		// the position in the static iter collection we point to
	typedef typename _mpl_::deref<iter_t >::type static_trait;
	
	enum { arg_ordinal = static_trait::arg_ordinal };	// arg ordinal desired
	
	typedef _mpl_::true_ argument_wanted;					// this class DOES want a runtime arg
							
	void increment()
	{
		// NOP
	}
	typename static_trait::value_type * pValue;			// member for holding onto the argument 

	typedef typename static_trait::value_type &	return_type;
	
	static_ord_ref()									// default ctor
	: pValue(0) {};
	
	void receieve_arg( typename static_trait::arg_type arg)	// receieve an argument
	{
		pValue = &arg;
	}
	
	return_type get_value()								// return the argument
	{
		assert(pValue);
		return *pValue;
	}
};

// returns true of the passed trait wants an argument
template <class T>
struct wants_argument
: T::argument_wanted::type
{};

template <class T>
struct not_wants_argument
: _mpl_::not_< typename T::argument_wanted::type>
{};

template <class T>
struct is_static_ord_ref
: _mpl_::false_	{};

template <class iter_t, class enable_t>
struct is_static_ord_ref< static_ord_ref<iter_t, enable_t> >
: _mpl_::true_	{};


// given a iter, returns a static ordinal ref
template <class iter_t>
struct ordinal_ref
 : static_ord_ref< iter_t> 
{};

// functor for dispatching arguments
// takes a bag of arguments
/// on the operator()(), will ask the type of the passed
// operand which ordinal in the arg bag it wishes to receieve
// will then pass that item to the object
template <class bag_zip_args_t>
struct assign_arguments
{
	bag_zip_args_t & m_bag_zip_args;
	
	assign_arguments(bag_zip_args_t & bag_zip_args)
	: m_bag_zip_args(bag_zip_args)	{}
	
	template <class T>
	void operator()(T& obj)
	{	
		obj.template receieve_arg(	m_bag_zip_args.template item_at<T::arg_ordinal>() );
	}
};
/////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////

/*	
	takes item ordinal & static iter collection
	
	grab the item type from the bag
	
	ask the item type if it is a static model or not
	
	if it is dynamic
	
		grab the arg from the bag and return that
	if it IS static- then ask for the ordinal in the collection 
		return the value that that iter returns
*/

template <class static_iter_col, class bag_t>
struct get_dispatch_args
{
	typedef get_dispatch_args type;

	template <int iOrdinal, class Enable = void>
	struct arg
	{
		bag_t & m_bg;
		arg(bag_t & bg)
		: m_bg(bg) {};
		
		// ask the bag for the return type trait of current ordinal
		typedef typename bag_t::config::template return_type_at<iOrdinal>::type return_type_trait;
		
		// pull the return type from the trait
		typedef typename return_type_trait::return_type return_type;
		
		// member for pulling the value out of the bag
		return_type get_value()
		{
			// from the arg bag, look up the item at passed ordinal and return value
			return m_bg.template item_at<iOrdinal>().get_value();
		}
	};
	
	// arg retreival for static ord ref items which do not take an argument
	template <int iOrdinal>
	struct arg
	< 
		iOrdinal, 
		typename boost::enable_if
		< 
			_mpl_::and_
			<
				// is it a static ordinal reference?
				is_static_ord_ref < typename bag_t::config:: template key_at< iOrdinal>::type >, 
				// does it NOT want a runtime argument?
				not_wants_argument < typename bag_t::config:: template key_at< iOrdinal>::type >
			>
		>::type 
	>
	{
		arg(bag_t & ) 
		{};
		
		// bag item (static_ord_ref)
		typedef typename bag_t::config:: template key_at< iOrdinal>::type bag_item;
		
		// find the iter for the current iteration
		typedef typename at_pos<typename static_iter_col::static_iterators , typename bag_item::pos>::type static_iter;

		// deref the type the iter is pointing to
		typedef typename static_iter::deref_current return_type;
		
		// go to the iter and ask for the value
		return_type get_value()
		{
			// get the value from the static iter
			return static_iter().template get_value<static_iter>();			
		}
	};
	// arg retreival for static ord ref items which DO take an argument
	template <int iOrdinal>
	struct arg
	< 
		iOrdinal, 
		typename boost::enable_if
		< 
			_mpl_::and_
			<
				// is it a static ordinal reference?
				is_static_ord_ref < typename bag_t::config:: template key_at< iOrdinal>::type >, 
				// DOES it want a runtime argument?
				wants_argument < typename bag_t::config:: template key_at< iOrdinal>::type >
			>
		>::type 
	>
	{
		bag_t & m_bg;
		arg(bag_t & bg) 
		: m_bg(bg) {};
		
		// bag item (static_ord_ref)
		typedef typename bag_t::config:: template key_at< iOrdinal>::type bag_item;
		
		// find the iter for the current iteration
		typedef typename at_pos<typename static_iter_col::static_iterators , typename bag_item::pos>::type static_iter;

		// goto the current iter and ask it for the return type
		typedef typename static_iter::template return_type<static_iter>::type return_type;

		// go to the iter and ask for the value
		return_type & get_value()
		{
			/* 
				go to the bag of functor args,
					get the value of the bag being held there (originally passed into for_zip)
						retrieve the item of that bag at the position denoted by value pointed to by static_iter
			*/
			return 
				m_bg.template item_at<iOrdinal>(). 
					template get_value(). 
						template item_at< static_iter::deref_current::value >();
		}
	};
};

template <class static_iter_col, class bag_t, class func_t>
inline void dispatch_args
( 
	bag_t & bg_args,				// bag of arguments
	func_t & func,					// functor to visit with
	_mpl_::long_<1> bag_slot_size		// bag slot size
)
{
	typedef get_dispatch_args<static_iter_col, bag_t> gda;
	func( 
		typename gda:: template arg<0>(bg_args).get_value()
		);
}

template <class static_iter_col, class bag_t, class func_t>
inline void dispatch_args
( 
	bag_t & bg_args,				// bag of arguments
	func_t & func,					// functor to visit with
	_mpl_::long_<2> bag_slot_size		// bag slot size
)
{
	typedef get_dispatch_args<static_iter_col, bag_t> gda;
	func( 
		typename gda:: template arg<0>(bg_args).get_value(),
		typename gda:: template arg<1>(bg_args).get_value()
		);
}

template <class static_iter_col, class bag_t, class func_t>
inline void dispatch_args
( 
	bag_t & bg_args,				// bag of arguments
	func_t & func,					// functor to visit with
	_mpl_::long_<3> bag_slot_size		// bag slot size
)
{
	typedef get_dispatch_args<static_iter_col, bag_t> gda;
	func( 
		typename gda:: template arg<0>(bg_args).get_value(),
		typename gda:: template arg<1>(bg_args).get_value(), 
		typename gda:: template arg<2>(bg_args).get_value()
		);
}
template <class static_iter_col, class bag_t, class func_t>
inline void dispatch_args
( 
	bag_t & bg_args,				// bag of arguments
	func_t & func,					// functor to visit with
	_mpl_::long_<4> bag_slot_size		// bag slot size
)
{
	typedef get_dispatch_args<static_iter_col, bag_t> gda;
	func( 
		typename gda:: template arg<0>(bg_args).get_value(),
		typename gda:: template arg<1>(bg_args).get_value(), 
		typename gda:: template arg<2>(bg_args).get_value(),
		typename gda:: template arg<3>(bg_args).get_value()
		);
}
template <class static_iter_col, class bag_t, class func_t>
inline void dispatch_args
( 
	bag_t & bg_args,				// bag of arguments
	func_t & func,					// functor to visit with
	_mpl_::long_<5> bag_slot_size		// bag slot size
)
{
	typedef get_dispatch_args<static_iter_col, bag_t> gda;
	func( 
		typename gda:: template arg<0>(bg_args).get_value(),
		typename gda:: template arg<1>(bg_args).get_value(), 
		typename gda:: template arg<2>(bg_args).get_value(),
		typename gda:: template arg<3>(bg_args).get_value(),
		typename gda:: template arg<4>(bg_args).get_value()
		);
}

template< bool done = true> // true == recursion done
struct zip_each_impl
{
	template <class curr_static_iter_t, class arg_t, class func_t>
	inline void call_each(arg_t & args, func_t & func )
	{
		// do nothing
	}
};

// zip each static iter and runtime iter 
template <>
struct zip_each_impl<false> // false == not done
{
	template <class static_iter_col, class arg_t, class func_t>
	inline void call_each(arg_t & args, func_t & func )
	{
//		if(! args.any_at_end())
		{
			// call functor with args:
			// pass the static iterator collection , bag of args and functor
			// to a function which will look up the appropriate arg for each functor param
			// and pass accordingly
			dispatch_args<static_iter_col>( args, func, typename arg_t::config::slot_size::type () );

			// increment all rt iters
			args.for_each(increment_all());
			
			// incremenet all static iters
			typedef typename static_iter_col::next next_static_iter_col;
			
			//initiate recursion \ visitation
			zip_each_impl
			< 
				 next_static_iter_col::is_past_last

			>(). template call_each<next_static_iter_col>(args,func);
		} 
	}
};

// receieves a variable number of arguments and boot-straps up the recursion \ visiation
template <class seq_zip_traits_t, class seq_zip_args_t, class functor_t >
struct zip_visitation_impl
{
	// define a bag of the zip_arguments
	typedef bag
	< 
		single<by_ref>, 
		seq_zip_args_t			// argument types
	
	> bag_zip_args_type;

	// build a bag of the zip_arguments
	bag_zip_args_type bag_zip_args;

	// ctors for arguments
	template <class A0 > zip_visitation_impl(A0 & a0)	
		: bag_zip_args(a0) {};
	template <class A0, class AA1> zip_visitation_impl(A0 & a0, AA1 & a1) 
		: bag_zip_args(a0, a1) {};
	template <class A0, class A1, class A2> zip_visitation_impl(A0 & a0, A1 & a1, A2 & a2) 
		: bag_zip_args(a0, a1, a2) {};
	template <class A0, class A1, class A2, class A3> zip_visitation_impl(A0 & a0, A1 & a1, A2 & a2, A3 & a3) 
		: bag_zip_args(a0, a1, a2, a3) {};
	template <class A0, class A1, class A2, class A3, class A4> zip_visitation_impl(A0 & a0, A1 & a1, A2 & a2, A3 & a3, A4 & a4) 
		: bag_zip_args(a0, a1, a2, a3, a4) {};

	void operator()(functor_t & func)
	{
		// make a static collection of just the static_model traits
		typedef  typename _mpl_::copy_if
		<
			seq_zip_traits_t,
			is_model_static < _mpl_::_1 >,
			_mpl_::back_inserter< _mpl_::vector<> >

		>::type vec_static_traits;
		
		// transform vector of passed traits so that static_iterators are changed 
		// to an reference class instead of the actual class
		// reference points to ordinal in vec_static_traits
		typedef  typename _mpl_::transform
		<
			seq_zip_traits_t
			,	_mpl_::if_
				< 
					is_model_static < _mpl_::_1 >,	// if the model of the trait is static,
					ordinal_ref						
					<
						_mpl_::find					// look up the class in the static only list of traits
						<
							vec_static_traits, 
							_mpl_::_1
						>
					>,
					_mpl_::_1							// otherwise, return the existing type
				>
		>::type vec_arg_bag_slots;
		
		// build a bag of the final functor arguments (from the transformed traits)
		// this bag is now in the ORDER of the args that need to go to the functor
		//		and has static_ord_ref() objects in the places where the static iterator(s) need to go
		bag_functor_arguments< vec_arg_bag_slots > bag_fctor_args;

		// give the trait instantiated objects their arguments
		// scan the vector of passed traits- filter out ones which want a runtime argument
		//		look up the object in the functor arguments (via ordinal)
		//		pass it the object from zip_arguments (via trait.receieve_arg() )
		bag_fctor_args.template 
		for_each
		< 
			_mpl_::lambda< wants_argument<_mpl_::_1> > 

		> (assign_arguments<bag_zip_args_type>(bag_zip_args));

		// define collection of static iters
		typedef static_iterator_collection<vec_static_traits> static_iter_col;
		
		//initiate recursion \ visitation
		zip_each_impl
		< 
			 static_iter_col::is_past_last

		>(). template call_each<static_iter_col>(bag_fctor_args,func);
	}
};

// bring in the detailed impl of for_zip
#include <boost/bag/detail/for_zip_impl.hpp>

} } // end namespace boost { end namespace bag {

#endif // #ifndef __BOOST_BAG_FOR_ZIP_HPP