diff options
Diffstat (limited to 'src/boost/libs/bimap/example/bimap_and_boost')
8 files changed, 646 insertions, 0 deletions
diff --git a/src/boost/libs/bimap/example/bimap_and_boost/assign.cpp b/src/boost/libs/bimap/example/bimap_and_boost/assign.cpp new file mode 100644 index 000000000..841c63532 --- /dev/null +++ b/src/boost/libs/bimap/example/bimap_and_boost/assign.cpp @@ -0,0 +1,79 @@ +// Boost.Bimap +// +// Copyright (c) 2006-2007 Matias Capeletto +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// VC++ 8.0 warns on usage of certain Standard Library and API functions that +// can be cause buffer overruns or other possible security issues if misused. +// See https://web.archive.org/web/20071014014301/http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx +// But the wording of the warning is misleading and unsettling, there are no +// portable alternative functions, and VC++ 8.0's own libraries use the +// functions in question. So turn off the warnings. +#define _CRT_SECURE_NO_DEPRECATE +#define _SCL_SECURE_NO_DEPRECATE + +// Boost.Bimap Example +//----------------------------------------------------------------------------- + +#include <boost/config.hpp> + +#include <string> + +#include <boost/assign/list_of.hpp> +#include <boost/assign/list_inserter.hpp> + +#include <boost/bimap/bimap.hpp> +#include <boost/bimap/multiset_of.hpp> +#include <boost/bimap/list_of.hpp> + +using namespace boost::bimaps; +using namespace boost; + + +int main() +{ + //[ code_bimap_and_boost_assign + + typedef bimap< multiset_of< int >, list_of< std::string > > bm_type; + + // We can use assign::list_of to initialize the container. + + bm_type bm = assign::list_of< bm_type::relation > /*< + Note that `bm_type::relation` has to be used instead of `bm_type::value_type`. + Contrary to `value_type`, `relation` type stores the elements as non const, a + requirement of `assign::list_of` >*/ + ( 1, "one" ) + ( 2, "two" ) + ( 3, "three" ); + + // The left map view is a multiset, again we use insert + + assign::insert( bm.left ) + ( 4, "four" ) + ( 5, "five" ) + ( 6, "six" ); + + // The right map view is a list so we use push_back here + // Note the order of the elements in the list! + + assign::push_back( bm.right ) + ( "seven" , 7 ) + ( "eight" , 8 ); + + assign::push_front( bm.right ) + ( "nine" , 9 ) + ( "ten" , 10 ) + ( "eleven", 11 ); + + // Since it is left_based the main view is a multiset, so we use insert + + assign::insert( bm ) + ( 12, "twelve" ) + ( 13, "thirteen" ); + //] + + return 0; +} diff --git a/src/boost/libs/bimap/example/bimap_and_boost/foreach.cpp b/src/boost/libs/bimap/example/bimap_and_boost/foreach.cpp new file mode 100644 index 000000000..a1d344112 --- /dev/null +++ b/src/boost/libs/bimap/example/bimap_and_boost/foreach.cpp @@ -0,0 +1,106 @@ +// Boost.Bimap +// +// Copyright (c) 2006-2007 Matias Capeletto +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// VC++ 8.0 warns on usage of certain Standard Library and API functions that +// can be cause buffer overruns or other possible security issues if misused. +// See https://web.archive.org/web/20071014014301/http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx +// But the wording of the warning is misleading and unsettling, there are no +// portable alternative functions, and VC++ 8.0's own libraries use the +// functions in question. So turn off the warnings. +#define _CRT_SECURE_NO_DEPRECATE +#define _SCL_SECURE_NO_DEPRECATE + +// Boost.Bimap Example +//----------------------------------------------------------------------------- + +#include <boost/config.hpp> + +#include <string> +#include <iostream> + +#include <boost/foreach.hpp> + +#include <boost/bimap/bimap.hpp> +#include <boost/bimap/list_of.hpp> +#include <boost/bimap/support/lambda.hpp> + +using namespace boost::bimaps; + + +int main() +{ + //[ code_bimap_and_boost_foreach + + typedef bimap< std::string, list_of<int> > bm_type; + + bm_type bm; + bm.insert( bm_type::value_type("1", 1) ); + bm.insert( bm_type::value_type("2", 2) ); + bm.insert( bm_type::value_type("3", 4) ); + bm.insert( bm_type::value_type("4", 2) ); + + BOOST_FOREACH( bm_type::left_reference p, bm.left ) + { + ++p.second; /*< We can modify the right element because we have + use a mutable collection type in the right side. >*/ + } + + BOOST_FOREACH( bm_type::right_const_reference p, bm.right ) + { + std::cout << p.first << "-->" << p.second << std::endl; + } + + //] + + // More examples + + BOOST_FOREACH( bm_type::right_reference p, bm.right ) + { + ++p.first; + } + + BOOST_FOREACH( bm_type::left_const_reference p, bm.left ) + { + std::cout << p.first << "-->" << p.second << std::endl; + } + + BOOST_FOREACH( bm_type::reference p, bm ) + { + ++p.right; + } + + const bm_type & cbm = bm; + BOOST_FOREACH( bm_type::const_reference p, cbm ) + { + std::cout << p.left << "-->" << p.right << std::endl; + } + + BOOST_FOREACH( bm_type::const_reference p, bm ) + { + std::cout << p.left << "-->" << p.right << std::endl; + } + + //[ code_bimap_and_boost_foreach_using_range + + BOOST_FOREACH( bm_type::left_reference p, + ( bm.left.range( std::string("1") <= _key, _key < std::string("3") ) )) + { + ++p.second; + } + + BOOST_FOREACH( bm_type::left_const_reference p, + ( bm.left.range( std::string("1") <= _key, _key < std::string("3") ) )) + { + std::cout << p.first << "-->" << p.second << std::endl; + } + //] + + return 0; +} + + diff --git a/src/boost/libs/bimap/example/bimap_and_boost/lambda.cpp b/src/boost/libs/bimap/example/bimap_and_boost/lambda.cpp new file mode 100644 index 000000000..cd7ce8519 --- /dev/null +++ b/src/boost/libs/bimap/example/bimap_and_boost/lambda.cpp @@ -0,0 +1,49 @@ +// Boost.Bimap +// +// Copyright (c) 2006-2007 Matias Capeletto +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// VC++ 8.0 warns on usage of certain Standard Library and API functions that +// can be cause buffer overruns or other possible security issues if misused. +// See https://web.archive.org/web/20071014014301/http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx +// But the wording of the warning is misleading and unsettling, there are no +// portable alternative functions, and VC++ 8.0's own libraries use the +// functions in question. So turn off the warnings. +#define _CRT_SECURE_NO_DEPRECATE +#define _SCL_SECURE_NO_DEPRECATE + +// Boost.Bimap Example +//----------------------------------------------------------------------------- + +#include <boost/config.hpp> + +#include <string> + +#include <boost/bimap/bimap.hpp> +#include <boost/bimap/support/lambda.hpp> + +using namespace boost::bimaps; + +int main() +{ + //[ code_bimap_and_boost_lambda + + typedef bimap< std::string, int > bm_type; + + bm_type bm; + bm.insert( bm_type::value_type("one",1) ); + bm.insert( bm_type::value_type("two",2) ); + + bm.right.range( 5 < _key, _key < 10 ); + + bm.left.modify_key( bm.left.find("one"), _key = "1" ); + + bm.left.modify_data( bm.left.begin(), _data *= 10 ); + //] + return 0; +} + + diff --git a/src/boost/libs/bimap/example/bimap_and_boost/property_map.cpp b/src/boost/libs/bimap/example/bimap_and_boost/property_map.cpp new file mode 100644 index 000000000..aa082781e --- /dev/null +++ b/src/boost/libs/bimap/example/bimap_and_boost/property_map.cpp @@ -0,0 +1,59 @@ +// Boost.Bimap +// +// Copyright (c) 2006-2007 Matias Capeletto +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// VC++ 8.0 warns on usage of certain Standard Library and API functions that +// can be cause buffer overruns or other possible security issues if misused. +// See https://web.archive.org/web/20071014014301/http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx +// But the wording of the warning is misleading and unsettling, there are no +// portable alternative functions, and VC++ 8.0's own libraries use the +// functions in question. So turn off the warnings. +#define _CRT_SECURE_NO_DEPRECATE +#define _SCL_SECURE_NO_DEPRECATE + +// Boost.Bimap Example +//----------------------------------------------------------------------------- + +#include <boost/config.hpp> + +#include <iostream> +#include <string> + +#include <boost/bimap/bimap.hpp> +#include <boost/bimap/multiset_of.hpp> +#include <boost/bimap/property_map/set_support.hpp> + +using namespace boost::bimaps; + +//[ code_bimap_and_boost_property_map + +template <typename AddressMap> +void foo(AddressMap & address_map) +{ + typedef typename boost::property_traits<AddressMap>::value_type value_type; + typedef typename boost::property_traits<AddressMap>::key_type key_type; + + value_type address; + key_type fred = "Fred"; + std::cout << boost::get(address_map, fred); +} + +int main() +{ + typedef bimap<std::string, multiset_of<std::string> > Name2Address; + typedef Name2Address::value_type location; + + Name2Address name2address; + name2address.insert( location("Fred", "710 West 13th Street") ); + name2address.insert( location( "Joe", "710 West 13th Street") ); + + foo( name2address.left ); + + return 0; +} +//] + diff --git a/src/boost/libs/bimap/example/bimap_and_boost/range.cpp b/src/boost/libs/bimap/example/bimap_and_boost/range.cpp new file mode 100644 index 000000000..046c8686f --- /dev/null +++ b/src/boost/libs/bimap/example/bimap_and_boost/range.cpp @@ -0,0 +1,121 @@ +// Boost.Bimap +// +// Copyright (c) 2006-2007 Matias Capeletto +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// VC++ 8.0 warns on usage of certain Standard Library and API functions that +// can be cause buffer overruns or other possible security issues if misused. +// See https://web.archive.org/web/20071014014301/http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx +// But the wording of the warning is misleading and unsettling, there are no +// portable alternative functions, and VC++ 8.0's own libraries use the +// functions in question. So turn off the warnings. +#define _CRT_SECURE_NO_DEPRECATE +#define _SCL_SECURE_NO_DEPRECATE + +// Boost.Bimap Example +//----------------------------------------------------------------------------- + +#include <boost/config.hpp> + +#include <iostream> + +#include <boost/range/functions.hpp> +#include <boost/range/metafunctions.hpp> + +//[ code_bimap_and_boost_range_functions + +template< class ForwardReadableRange, class UnaryFunctor > +UnaryFunctor for_each(const ForwardReadableRange & r, UnaryFunctor func) +{ + typedef typename + boost::range_const_iterator<ForwardReadableRange>::type const_iterator; + + for(const_iterator i= boost::begin(r), iend= boost::end(r); i!=iend; ++i ) + { + func(*i); + } + + return func; +} + +template< class ForwardReadableRange, class Predicate > +typename boost::range_difference<ForwardReadableRange>::type + count_if(const ForwardReadableRange & r, Predicate pred) +{ + typedef typename + boost::range_const_iterator<ForwardReadableRange>::type const_iterator; + + typename boost::range_difference<ForwardReadableRange>::type c = 0; + + for( const_iterator i = boost::begin(r), iend = boost::end(r); i != iend; ++i ) + { + if( pred(*i) ) ++c; + } + + return c; +} +//] + +#include <boost/bimap/bimap.hpp> +#include <boost/bimap/multiset_of.hpp> +#include <boost/bimap/support/lambda.hpp> +#include <boost/bind.hpp> + +using namespace boost::bimaps; +using namespace boost; + +//[ code_bimap_and_boost_range + +struct pair_printer +{ + pair_printer(std::ostream & o) : os(o) {} + template< class Pair > + void operator()(const Pair & p) + { + os << "(" << p.first << "," << p.second << ")"; + } + private: + std::ostream & os; +}; + +struct second_extractor +{ + template< class Pair > + const typename Pair::second_type & operator()(const Pair & p) + { + return p.second; + } +}; + +int main() +{ + typedef bimap< double, multiset_of<int> > bm_type; + + bm_type bm; + bm.insert( bm_type::value_type(2.5 , 1) ); + bm.insert( bm_type::value_type(3.1 , 2) ); + //... + bm.insert( bm_type::value_type(6.4 , 4) ); + bm.insert( bm_type::value_type(1.7 , 2) ); + + // Print all the elements of the left map view + + for_each( bm.left, pair_printer(std::cout) ); + + // Print a range of elements of the right map view + + for_each( bm.right.range( 2 <= _key, _key < 6 ), pair_printer(std::cout) ); + + // Count the number of elements where the data is equal to 2 from a + // range of elements of the left map view + + count_if( bm.left.range( 2.3 < _key, _key < 5.4 ), + bind<int>( second_extractor(), _1 ) == 2 ); + + return 0; +} +//] + diff --git a/src/boost/libs/bimap/example/bimap_and_boost/serialization.cpp b/src/boost/libs/bimap/example/bimap_and_boost/serialization.cpp new file mode 100644 index 000000000..034c444e6 --- /dev/null +++ b/src/boost/libs/bimap/example/bimap_and_boost/serialization.cpp @@ -0,0 +1,89 @@ +// Boost.Bimap +// +// Copyright (c) 2006-2007 Matias Capeletto +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// VC++ 8.0 warns on usage of certain Standard Library and API functions that +// can be cause buffer overruns or other possible security issues if misused. +// See https://web.archive.org/web/20071014014301/http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx +// But the wording of the warning is misleading and unsettling, there are no +// portable alternative functions, and VC++ 8.0's own libraries use the +// functions in question. So turn off the warnings. +#define _CRT_SECURE_NO_DEPRECATE +#define _SCL_SECURE_NO_DEPRECATE + +// Boost.Bimap Example +//----------------------------------------------------------------------------- + +#include <boost/config.hpp> + +#include <fstream> +#include <string> +#include <cassert> + +#include <boost/bimap/bimap.hpp> + +#include <boost/archive/text_oarchive.hpp> +#include <boost/archive/text_iarchive.hpp> + +using namespace boost::bimaps; + +int main() +{ + //[ code_bimap_and_boost_serialization + + typedef bimap< std::string, int > bm_type; + + // Create a bimap and serialize it to a file + { + bm_type bm; + bm.insert( bm_type::value_type("one",1) ); + bm.insert( bm_type::value_type("two",2) ); + + std::ofstream ofs("data"); + boost::archive::text_oarchive oa(ofs); + + oa << const_cast<const bm_type&>(bm); /*< + We must do a const cast because Boost.Serialization archives + only save const objects. Read Boost.Serializartion docs for the + rationale behind this decision >*/ + + /*<< We can only serialize iterators if the bimap was serialized first. + Note that the const cast is not requiered here because we create + our iterators as const. >>*/ + const bm_type::left_iterator left_iter = bm.left.find("two"); + oa << left_iter; + + const bm_type::right_iterator right_iter = bm.right.find(1); + oa << right_iter; + } + + // Load the bimap back + { + bm_type bm; + + std::ifstream ifs("data", std::ios::binary); + boost::archive::text_iarchive ia(ifs); + + ia >> bm; + + assert( bm.size() == 2 ); + + bm_type::left_iterator left_iter; + ia >> left_iter; + + assert( left_iter->first == "two" ); + + bm_type::right_iterator right_iter; + ia >> right_iter; + + assert( right_iter->first == 1 ); + } + //] + + return 0; +} + diff --git a/src/boost/libs/bimap/example/bimap_and_boost/typeof.cpp b/src/boost/libs/bimap/example/bimap_and_boost/typeof.cpp new file mode 100644 index 000000000..dac1015e1 --- /dev/null +++ b/src/boost/libs/bimap/example/bimap_and_boost/typeof.cpp @@ -0,0 +1,86 @@ +// Boost.Bimap +// +// Copyright (c) 2006-2007 Matias Capeletto +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// VC++ 8.0 warns on usage of certain Standard Library and API functions that +// can be cause buffer overruns or other possible security issues if misused. +// See https://web.archive.org/web/20071014014301/http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx +// But the wording of the warning is misleading and unsettling, there are no +// portable alternative functions, and VC++ 8.0's own libraries use the +// functions in question. So turn off the warnings. +#define _CRT_SECURE_NO_DEPRECATE +#define _SCL_SECURE_NO_DEPRECATE + +// Boost.Bimap Example +//----------------------------------------------------------------------------- + +#include <boost/config.hpp> + +#include <string> +#include <iostream> +#include <boost/bimap/bimap.hpp> + +#include <boost/typeof/typeof.hpp> + +using namespace boost::bimaps; + +struct name {}; +struct number {}; + +void using_auto() +{ + //[ code_bimap_and_boost_typeof_first + + typedef bimap< tagged<std::string,name>, tagged<int,number> > bm_type; + bm_type bm; + bm.insert( bm_type::value_type("one" ,1) ); + bm.insert( bm_type::value_type("two" ,2) ); + //] + + //[ code_bimap_and_boost_typeof_using_auto + + for( BOOST_AUTO(iter, bm.by<name>().begin()); iter!=bm.by<name>().end(); ++iter) + { + std::cout << iter->first << " --> " << iter->second << std::endl; + } + + BOOST_AUTO( iter, bm.by<number>().find(2) ); + std::cout << "2: " << iter->get<name>(); + //] +} + +void not_using_auto() +{ + typedef bimap< tagged<std::string,name>, tagged<int,number> > bm_type; + bm_type bm; + bm.insert( bm_type::value_type("one" ,1) ); + bm.insert( bm_type::value_type("two" ,2) ); + + //[ code_bimap_and_boost_typeof_not_using_auto + + for( bm_type::map_by<name>::iterator iter = bm.by<name>().begin(); + iter!=bm.by<name>().end(); ++iter) + { + std::cout << iter->first << " --> " << iter->second << std::endl; + } + + bm_type::map_by<number>::iterator iter = bm.by<number>().find(2); + std::cout << "2: " << iter->get<name>(); + //] +} + +int main() +{ + using_auto(); + not_using_auto(); + + return 0; +} + + + + diff --git a/src/boost/libs/bimap/example/bimap_and_boost/xpressive.cpp b/src/boost/libs/bimap/example/bimap_and_boost/xpressive.cpp new file mode 100644 index 000000000..7949c84dc --- /dev/null +++ b/src/boost/libs/bimap/example/bimap_and_boost/xpressive.cpp @@ -0,0 +1,57 @@ +// Boost.Bimap +// +// Copyright (c) 2006-2007 Matias Capeletto +// +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// VC++ 8.0 warns on usage of certain Standard Library and API functions that +// can be cause buffer overruns or other possible security issues if misused. +// See https://web.archive.org/web/20071014014301/http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx +// But the wording of the warning is misleading and unsettling, there are no +// portable alternative functions, and VC++ 8.0's own libraries use the +// functions in question. So turn off the warnings. +#define _CRT_SECURE_NO_DEPRECATE +#define _SCL_SECURE_NO_DEPRECATE + +// Boost.Bimap Example +//----------------------------------------------------------------------------- + +#include <boost/config.hpp> + +#include <string> + +#include <boost/bimap/bimap.hpp> + +#include <boost/xpressive/xpressive.hpp> +#include <boost/xpressive/regex_actions.hpp> + +using namespace boost::bimaps; +using namespace boost::xpressive; +namespace xp = boost::xpressive; + +int main() +{ + //[ code_bimap_and_boost_xpressive + + typedef bimap< std::string, int > bm_type; + bm_type bm; + + std::string rel_str("one <--> 1 two <--> 2 three <--> 3"); + + sregex rel = ( (s1= +_w) >> " <--> " >> (s2= +_d) ) + [ + xp::ref(bm)->*insert( xp::construct<bm_type::value_type>(s1, as<int>(s2)) ) + ]; + + sregex relations = rel >> *(+_s >> rel); + + regex_match(rel_str, relations); + + assert( bm.size() == 3 ); + //] + + return 0; +} + |