Ticket #816: assoc_by_insert.cpp

File assoc_by_insert.cpp, 3.7 KB (added by cppljevans@…, 13 years ago)

solution using superclass which is fold over insert op

Line 
1//prototype whether initial mpl::set can be done by using repeated inserts.
2//
3#include <boost/mpl/set.hpp>
4#include <boost/mpl/insert.hpp>
5#include <boost/mpl/erase_key.hpp>
6#include <boost/mpl/equal.hpp>
7#include <boost/mpl/fold.hpp>
8#include <boost/mpl/and.hpp>
9#include <boost/mpl/empty.hpp>
10#include <boost/mpl/size.hpp>
11
12#include <boost/mpl/assert.hpp>
13
14namespace boost{namespace mpl{
15namespace assoc_impl
16//prototype namespace:
17{
18#define USE_SET_INSERT
19#ifdef USE_SET_INSERT
20//{set
21 template
22 < typename... Elements
23 >
24 struct
25set
26;
27 template
28 <
29 >
30 struct
31set
32 <
33 >
34 : mpl::set<>
35{
36};
37
38 template
39 < typename HeadElement
40 , typename... TailElements
41 >
42 struct
43set
44 < HeadElement
45 , TailElements...
46 >
47 : mpl::insert
48 < assoc_impl::set<TailElements...>
49 , HeadElement
50 >::type
51{
52};
53//}set
54 template
55 < typename Set0
56 , typename Set1
57 >
58 struct
59erase_sets
60/**@brief
61 * erases Set1 from Set0
62 */
63: fold
64 < Set1
65 , Set0
66 , erase_key<arg<1>,arg<2> >
67 >
68{
69};
70 template
71 < typename Set0
72 , typename Set1
73 >
74 struct
75equal_sets
76/**@brief
77 * Is Set1 equal to Set0?
78 */
79: and_
80 < empty<typename erase_sets<Set0,Set1>::type>
81 , empty<typename erase_sets<Set1,Set0>::type>
82 >
83{
84};
85
86#endif
87
88void set_insert_erase_test(void)
89{
90 typedef set<> empty_set;
91 typedef insert<empty_set, int>::type set_int;
92 BOOST_MPL_ASSERT((has_key<set_int,int>));
93 typedef insert<set_int, float>::type set_int_float;
94 BOOST_MPL_ASSERT((has_key<set_int_float,int>));
95 BOOST_MPL_ASSERT((has_key<set_int_float,float>));
96 typedef insert<set_int_float, int>::type set_int_float_int;
97 BOOST_MPL_ASSERT((boost::is_same<set_int_float,set_int_float_int>));
98 typedef erase_key<set_int_float, float>::type set_int_float_erase_float;
99 BOOST_MPL_ASSERT((boost::is_same<set_int,set_int_float_erase_float>));
100 typedef insert<empty_set, float>::type set_float;
101 typedef erase_key<set_int_float, int>::type set_int_float_erase_int;
102 BOOST_MPL_ASSERT_NOT((boost::is_same<set_float,set_int_float_erase_int>));
103 BOOST_MPL_ASSERT((equal<set_float,set_int_float_erase_int>));
104 typedef insert<set_float, int>::type set_float_int;
105 BOOST_MPL_ASSERT_NOT((boost::is_same<set_float_int,set_int_float>));
106 #ifdef USE_SET_INSERT
107 #if 0
108 typedef erase_sets<set_int_float,set_float_int>::type erase_sets_result;
109 BOOST_MPL_ASSERT((is_same<erase_sets_result,void>));
110 #endif
111 BOOST_MPL_ASSERT((equal_sets<set_int_float,set_float_int>));
112 BOOST_MPL_ASSERT_NOT((equal_sets<set_int,set_float_int>));
113 BOOST_MPL_ASSERT_NOT((equal_sets<set_int_float,set_float>));
114 #endif
115}
116
117void set_dup_key_con_test(void)
118{
119 typedef
120 ::boost::mpl::
121 #ifdef USE_SET_INSERT
122 assoc_impl::
123 #endif
124 set
125 < int
126 , int
127 >
128 int2_set;
129 BOOST_MPL_ASSERT((has_key<int2_set,int>));
130 unsigned const size_int2_set=size<int2_set>::type::value;
131 BOOST_MPL_ASSERT_RELATION(size_int2_set,==,1);
132 //^1)according to "normal: interpretation of "set".
133 typedef erase_key<int2_set,int>::type int2_erase_int;
134 unsigned const size_int2_erase_int=size<int2_erase_int>::type::value;
135 BOOST_MPL_ASSERT_RELATION(size_int2_erase_int,==,0);
136 BOOST_MPL_ASSERT_NOT((has_key<int2_erase_int,int>));
137 //^2)according to "Semantics:" on:
138 // http://www.boost.org/libs/mpl/doc/refmanual/erase-key.html
139 typedef erase_key<int2_set,float>::type int2_erase_float;
140 unsigned const size_int2_erase_float=size<int2_erase_float>::type::value;
141 BOOST_MPL_ASSERT_RELATION(size_int2_erase_float,==,1);
142 //^3)according to "Postcondition:" on:
143 // http://www.boost.org/libs/mpl/doc/refmanual/erase-key.html
144}
145
146}//exit assoc_impl namespace
147}}//exit boost::mpl