diff options
Diffstat (limited to 'src/boost/libs/bimap/example')
28 files changed, 2632 insertions, 0 deletions
diff --git a/src/boost/libs/bimap/example/Jamfile.v2 b/src/boost/libs/bimap/example/Jamfile.v2 new file mode 100644 index 00000000..42416294 --- /dev/null +++ b/src/boost/libs/bimap/example/Jamfile.v2 @@ -0,0 +1,50 @@ +# 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) + +# bring in rules for testing +import testing ; + +test-suite "examples" + : + [ compile mighty_bimap.cpp ] + [ run simple_bimap.cpp ] + [ run tagged_simple_bimap.cpp ] + [ run step_by_step.cpp ] + [ run population_bimap.cpp ] + [ run repetitions_counter.cpp ] + [ compile user_defined_names.cpp ] + [ run standard_map_comparison.cpp ] + [ run at_function_examples.cpp ] + [ run tutorial_modify_and_replace.cpp ] + [ run tutorial_range.cpp ] + [ run unconstrained_collection.cpp ] + [ run tutorial_info_hook.cpp ] + [ run projection.cpp ] + ; + +test-suite "bimap_and_boost" + : + [ run bimap_and_boost/property_map.cpp ] + [ run bimap_and_boost/range.cpp ] + [ run bimap_and_boost/foreach.cpp ] + [ run bimap_and_boost/lambda.cpp ] + [ run bimap_and_boost/assign.cpp ] + [ run bimap_and_boost/xpressive.cpp ] + [ run bimap_and_boost/typeof.cpp ] + [ run bimap_and_boost/serialization.cpp + /boost/serialization//boost_serialization ] + ; + +test-suite "mi_to_b_path" + : + [ compile mi_to_b_path/bidirectional_map.cpp ] + [ run mi_to_b_path/hashed_indices.cpp ] + [ compile mi_to_b_path/tagged_bidirectional_map.cpp ] + [ compile mi_to_b_path/mi_bidirectional_map.cpp ] + [ run mi_to_b_path/mi_hashed_indices.cpp ] + ; diff --git a/src/boost/libs/bimap/example/at_function_examples.cpp b/src/boost/libs/bimap/example/at_function_examples.cpp new file mode 100644 index 00000000..04f11113 --- /dev/null +++ b/src/boost/libs/bimap/example/at_function_examples.cpp @@ -0,0 +1,97 @@ +// 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 <cassert> + +#include <boost/bimap/bimap.hpp> +#include <boost/bimap/unordered_set_of.hpp> +#include <boost/bimap/list_of.hpp> +#include <boost/bimap/multiset_of.hpp> + +using namespace boost::bimaps; + +void first_bimap() +{ + //[ code_at_function_first + + typedef bimap< set_of< std::string >, list_of< int > > bm_type; + bm_type bm; + + try + { + bm.left.at("one") = 1; // throws std::out_of_range + } + catch( std::out_of_range & e ) {} + + assert( bm.empty() ); + + bm.left["one"] = 1; // Ok + + assert( bm.left.at("one") == 1 ); // Ok + //] +} + +void second_bimap() +{ + //[ code_at_function_second + + typedef bimap< multiset_of<std::string>, unordered_set_of<int> > bm_type; + bm_type bm; + + //<- + /* + //-> + bm.right[1] = "one"; // compilation error + //<- + */ + //-> + + bm.right.insert( bm_type::right_value_type(1,"one") ); + + assert( bm.right.at(1) == "one" ); // Ok + + try + { + std::cout << bm.right.at(2); // throws std::out_of_range + } + catch( std::out_of_range & e ) {} + + //<- + /* + //-> + bm.right.at(1) = "1"; // compilation error + //<- + */ + //-> + + //] +} + +int main() +{ + first_bimap(); + second_bimap(); + return 0; +} + 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 00000000..841c6353 --- /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 00000000..a1d34411 --- /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 00000000..cd7ce851 --- /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 00000000..aa082781 --- /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 00000000..046c8686 --- /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 00000000..034c444e --- /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 00000000..dac1015e --- /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 00000000..7949c84d --- /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; +} + diff --git a/src/boost/libs/bimap/example/mi_to_b_path/bidirectional_map.cpp b/src/boost/libs/bimap/example/mi_to_b_path/bidirectional_map.cpp new file mode 100644 index 00000000..20edf941 --- /dev/null +++ b/src/boost/libs/bimap/example/mi_to_b_path/bidirectional_map.cpp @@ -0,0 +1,87 @@ +// 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) + + +// Boost.Bimap Example +//----------------------------------------------------------------------------- +// This example shows how to construct a bidirectional map with +// multi_index_container. +// By a bidirectional map we mean a container of elements of +// std::pair<const FromType,const ToType> such that no two elements exists with +// the same first or second value (std::map only guarantees uniqueness of the +// first member). +// Fast lookup is provided for both keys. The program features a tiny +// Spanish-English dictionary with online query of words in both languages. + +#include <boost/config.hpp> + +//[ code_mi_to_b_path_bidirectional_map + +#include <iostream> +#include <boost/tokenizer.hpp> +#include <boost/bimap/bimap.hpp> + +using namespace boost::bimaps; + +// A dictionary is a bidirectional map from strings to strings + +typedef bimap<std::string,std::string> dictionary; +typedef dictionary::value_type translation; + +int main() +{ + dictionary d; + + // Fill up our microdictionary. + // first members Spanish, second members English. + + d.insert( translation("hola" ,"hello" )); + d.insert( translation("adios","goodbye")); + d.insert( translation("rosa" ,"rose" )); + d.insert( translation("mesa" ,"table" )); + + std::cout << "enter a word" << std::endl; + std::string word; + std::getline(std::cin,word); + + // search the queried word on the from index (Spanish) + + dictionary::left_const_iterator it = d.left.find(word); + + if( it != d.left.end() ) + { + // the second part of the element is the equivalent in English + + std::cout << word << " is said " + << it->second /*< `it` is an iterator of the left view, so + `it->second` refers to the right element of + the relation, the word in english >*/ + << " in English" << std::endl; + } + else + { + // word not found in Spanish, try our luck in English + + dictionary::right_const_iterator it2 = d.right.find(word); + if( it2 != d.right.end() ) + { + std::cout << word << " is said " + << it2->second /*< `it2` is an iterator of the right view, so + `it2->second` refers to the left element of + the relation, the word in spanish >*/ + << " in Spanish" << std::endl; + } + else + { + std::cout << "No such word in the dictionary" << std::endl; + } + } + + return 0; +} +//] diff --git a/src/boost/libs/bimap/example/mi_to_b_path/hashed_indices.cpp b/src/boost/libs/bimap/example/mi_to_b_path/hashed_indices.cpp new file mode 100644 index 00000000..22bb391b --- /dev/null +++ b/src/boost/libs/bimap/example/mi_to_b_path/hashed_indices.cpp @@ -0,0 +1,94 @@ +// 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) + + +// Boost.Bimap Example +//----------------------------------------------------------------------------- +// Hashed indices can be used as an alternative to ordered indices when fast +// lookup is needed and sorting information is of no interest. The example +// features a word counter where duplicate entries are checked by means of a +// hashed index. + +#include <boost/config.hpp> + +//[ code_mi_to_b_path_hashed_indices + +#include <iostream> +#include <iomanip> + +#include <boost/tokenizer.hpp> + +#include <boost/bimap/bimap.hpp> +#include <boost/bimap/unordered_set_of.hpp> +#include <boost/bimap/multiset_of.hpp> +#include <boost/bimap/support/lambda.hpp> + +using namespace boost::bimaps; + +struct word {}; +struct occurrences {}; + +typedef bimap +< + + multiset_of< tagged<unsigned int,occurrences>, std::greater<unsigned int> >, +unordered_set_of< tagged< std::string, word> > + +> word_counter; + +typedef boost::tokenizer<boost::char_separator<char> > text_tokenizer; + +int main() +{ + + std::string text= + "Relations between data in the STL are represented with maps." + "A map is a directed relation, by using it you are representing " + "a mapping. In this directed relation, the first type is related to " + "the second type but it is not true that the inverse relationship " + "holds. This is useful in a lot of situations, but there are some " + "relationships that are bidirectional by nature."; + + // feed the text into the container + + word_counter wc; + text_tokenizer tok(text,boost::char_separator<char>(" \t\n.,;:!?'\"-")); + unsigned int total_occurrences = 0; + + for( text_tokenizer::const_iterator it = tok.begin(), it_end = tok.end(); + it != it_end ; ++it ) + { + ++total_occurrences; + + word_counter::map_by<occurrences>::iterator wit = + wc.by<occurrences>().insert( + word_counter::map_by<occurrences>::value_type(0,*it) + ).first; + + wc.by<occurrences>().modify_key( wit, ++_key); + } + + // list words by frequency of appearance + + std::cout << std::fixed << std::setprecision(2); + + for( word_counter::map_by<occurrences>::const_iterator + wit = wc.by<occurrences>().begin(), + wit_end = wc.by<occurrences>().end(); + + wit != wit_end; ++wit ) + { + std::cout << std::setw(15) << wit->get<word>() << ": " + << std::setw(5) + << 100.0 * wit->get<occurrences>() / total_occurrences << "%" + << std::endl; + } + + return 0; +} +//] diff --git a/src/boost/libs/bimap/example/mi_to_b_path/mi_bidirectional_map.cpp b/src/boost/libs/bimap/example/mi_to_b_path/mi_bidirectional_map.cpp new file mode 100644 index 00000000..27045d0b --- /dev/null +++ b/src/boost/libs/bimap/example/mi_to_b_path/mi_bidirectional_map.cpp @@ -0,0 +1,107 @@ +// 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) + +/****************************************************************************** +Boost.MultiIndex +******************************************************************************/ + +#include <boost/config.hpp> + +//[ code_mi_to_b_path_mi_bidirectional_map + +#include <iostream> +#include <boost/tokenizer.hpp> + +#include <boost/multi_index_container.hpp> +#include <boost/multi_index/key_extractors.hpp> +#include <boost/multi_index/ordered_index.hpp> + +using namespace boost; +using namespace boost::multi_index; + +// tags for accessing both sides of a bidirectional map + +struct from {}; +struct to {}; + +// The class template bidirectional_map wraps the specification +// of a bidirectional map based on multi_index_container. + +template<typename FromType,typename ToType> +struct bidirectional_map +{ + typedef std::pair<FromType,ToType> value_type; + + typedef multi_index_container< + value_type, + indexed_by + < + ordered_unique + < + tag<from>, member<value_type,FromType,&value_type::first> + >, + ordered_unique + < + tag<to>, member<value_type,ToType,&value_type::second> + > + > + + > type; + +}; + +// A dictionary is a bidirectional map from strings to strings + +typedef bidirectional_map<std::string,std::string>::type dictionary; + +int main() +{ + dictionary d; + + // Fill up our microdictionary. + // first members Spanish, second members English. + + d.insert(dictionary::value_type("hola","hello")); + d.insert(dictionary::value_type("adios","goodbye")); + d.insert(dictionary::value_type("rosa","rose")); + d.insert(dictionary::value_type("mesa","table")); + + std::cout << "enter a word" << std::endl; + std::string word; + std::getline(std::cin,word); + + // search the queried word on the from index (Spanish) + + dictionary::iterator it = d.get<from>().find(word); + + if( it != d.end() ) + { + // the second part of the element is the equivalent in English + + std::cout << word << " is said " + << it->second << " in English" << std::endl; + } + else + { + // word not found in Spanish, try our luck in English + + dictionary::index_iterator<to>::type it2 = d.get<to>().find(word); + if( it2 != d.get<to>().end() ) + { + std::cout << word << " is said " + << it2->first << " in Spanish" << std::endl; + } + else + { + std::cout << "No such word in the dictionary" << std::endl; + } + } + + return 0; +} +//] diff --git a/src/boost/libs/bimap/example/mi_to_b_path/mi_hashed_indices.cpp b/src/boost/libs/bimap/example/mi_to_b_path/mi_hashed_indices.cpp new file mode 100644 index 00000000..f91b835d --- /dev/null +++ b/src/boost/libs/bimap/example/mi_to_b_path/mi_hashed_indices.cpp @@ -0,0 +1,100 @@ +// 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) + + +/***************************************************************************** +Boost.MultiIndex +*****************************************************************************/ + +#include <boost/config.hpp> + +//[ code_mi_to_b_path_mi_hashed_indices + +#include <iostream> +#include <iomanip> + +#include <boost/tokenizer.hpp> + +#include <boost/multi_index_container.hpp> +#include <boost/multi_index/key_extractors.hpp> +#include <boost/multi_index/ordered_index.hpp> +#include <boost/multi_index/hashed_index.hpp> +#include <boost/lambda/lambda.hpp> + +using namespace boost::multi_index; +namespace bl = boost::lambda; + +// word_counter keeps the ocurrences of words inserted. A hashed +// index allows for fast checking of preexisting entries. + +struct word_counter_entry +{ + std::string word; + unsigned int occurrences; + + word_counter_entry( std::string word_ ) : word(word_), occurrences(0) {} +}; + +typedef multi_index_container +< + word_counter_entry, + indexed_by + < + ordered_non_unique + < + BOOST_MULTI_INDEX_MEMBER( + word_counter_entry,unsigned int,occurrences), + std::greater<unsigned int> + >, + hashed_unique + < + BOOST_MULTI_INDEX_MEMBER(word_counter_entry,std::string,word) + > + > + +> word_counter; + +typedef boost::tokenizer<boost::char_separator<char> > text_tokenizer; + +int main() +{ + std::string text= + "En un lugar de la Mancha, de cuyo nombre no quiero acordarme... " + "...snip..." + "...no se salga un punto de la verdad."; + + // feed the text into the container + + word_counter wc; + text_tokenizer tok(text,boost::char_separator<char>(" \t\n.,;:!?'\"-")); + unsigned int total_occurrences = 0; + + for( text_tokenizer::iterator it = tok.begin(), it_end = tok.end(); + it != it_end ; ++it ) + { + ++total_occurrences; + word_counter::iterator wit = wc.insert(*it).first; + wc.modify_key( wit, ++ bl::_1 ); + } + + // list words by frequency of appearance + + std::cout << std::fixed << std::setprecision(2); + + for( word_counter::iterator wit = wc.begin(), wit_end=wc.end(); + wit != wit_end; ++wit ) + { + std::cout << std::setw(11) << wit->word << ": " + << std::setw(5) + << 100.0 * wit->occurrences / total_occurrences << "%" + << std::endl; + } + + return 0; +} +//] diff --git a/src/boost/libs/bimap/example/mi_to_b_path/tagged_bidirectional_map.cpp b/src/boost/libs/bimap/example/mi_to_b_path/tagged_bidirectional_map.cpp new file mode 100644 index 00000000..4c0ddb61 --- /dev/null +++ b/src/boost/libs/bimap/example/mi_to_b_path/tagged_bidirectional_map.cpp @@ -0,0 +1,90 @@ +// 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) + + +// Boost.Bimap Example +//----------------------------------------------------------------------------- +// This example shows how to construct a bidirectional map with +// multi_index_container. +// By a bidirectional map we mean a container of elements of +// std::pair<const FromType,const ToType> such that no two elements exists with +// the same first or second value (std::map only guarantees uniqueness of the +// first member). +// Fast lookup is provided for both keys. The program features a tiny +// Spanish-English dictionary with online query of words in both languages. + +//[ code_mi_to_b_path_tagged_bidirectional_map + +#include <iostream> + +#include <boost/bimap/bimap.hpp> + +using namespace boost::bimaps; + +// tags + +struct spanish {}; +struct english {}; + +// A dictionary is a bidirectional map from strings to strings + +typedef bimap +< + tagged< std::string,spanish >, tagged< std::string,english > + +> dictionary; + +typedef dictionary::value_type translation; + +int main() +{ + dictionary d; + + // Fill up our microdictionary. + // first members Spanish, second members English. + + d.insert( translation("hola" ,"hello" )); + d.insert( translation("adios","goodbye")); + d.insert( translation("rosa" ,"rose" )); + d.insert( translation("mesa" ,"table" )); + + std::cout << "enter a word" << std::endl; + std::string word; + std::getline(std::cin,word); + + // search the queried word on the from index (Spanish) */ + + dictionary::map_by<spanish>::const_iterator it = + d.by<spanish>().find(word); + + if( it != d.by<spanish>().end() ) + { + std::cout << word << " is said " + << it->get<english>() << " in English" << std::endl; + } + else + { + // word not found in Spanish, try our luck in English + + dictionary::map_by<english>::const_iterator it2 = + d.by<english>().find(word); + + if( it2 != d.by<english>().end() ) + { + std::cout << word << " is said " + << it2->get<spanish>() << " in Spanish" << std::endl; + } + else + { + std::cout << "No such word in the dictionary" << std::endl; + } + } + + return 0; +} +//] diff --git a/src/boost/libs/bimap/example/mighty_bimap.cpp b/src/boost/libs/bimap/example/mighty_bimap.cpp new file mode 100644 index 00000000..a4b369ba --- /dev/null +++ b/src/boost/libs/bimap/example/mighty_bimap.cpp @@ -0,0 +1,109 @@ +// 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 +//----------------------------------------------------------------------------- +// This is the translator example from the tutorial. +// In this example the set type of relation is changed to allow the iteration +// of the container. + +#include <boost/config.hpp> + +//[ code_mighty_bimap + +#include <iostream> +#include <string> +#include <boost/bimap/bimap.hpp> +#include <boost/bimap/list_of.hpp> +#include <boost/bimap/unordered_set_of.hpp> + +struct english {}; +struct spanish {}; + +int main() +{ + using namespace boost::bimaps; + + typedef bimap + < + unordered_set_of< tagged< std::string, spanish > >, + unordered_set_of< tagged< std::string, english > >, + list_of_relation + + > translator; + + translator trans; + + // We have to use `push_back` because the collection of relations is + // a `list_of_relation` + + trans.push_back( translator::value_type("hola" ,"hello" ) ); + trans.push_back( translator::value_type("adios" ,"goodbye" ) ); + trans.push_back( translator::value_type("rosa" ,"rose" ) ); + trans.push_back( translator::value_type("mesa" ,"table" ) ); + + std::cout << "enter a word" << std::endl; + std::string word; + std::getline(std::cin,word); + + // Search the queried word on the from index (Spanish) + + translator::map_by<spanish>::const_iterator is + = trans.by<spanish>().find(word); + + if( is != trans.by<spanish>().end() ) + { + std::cout << word << " is said " + << is->get<english>() + << " in English" << std::endl; + } + else + { + // Word not found in Spanish, try our luck in English + + translator::map_by<english>::const_iterator ie + = trans.by<english>().find(word); + + if( ie != trans.by<english>().end() ) + { + std::cout << word << " is said " + << ie->get<spanish>() + << " in Spanish" << std::endl; + } + else + { + // Word not found, show the possible translations + + std::cout << "No such word in the dictionary" << std::endl; + std::cout << "These are the possible translations" << std::endl; + + for( translator::const_iterator + i = trans.begin(), + i_end = trans.end(); + + i != i_end ; ++i ) + { + std::cout << i->get<spanish>() + << " <---> " + << i->get<english>() + << std::endl; + } + } + } + return 0; +} +//] diff --git a/src/boost/libs/bimap/example/population_bimap.cpp b/src/boost/libs/bimap/example/population_bimap.cpp new file mode 100644 index 00000000..09fdf7bd --- /dev/null +++ b/src/boost/libs/bimap/example/population_bimap.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 <string> + +#include <boost/bimap/bimap.hpp> +#include <boost/bimap/unordered_set_of.hpp> +#include <boost/bimap/multiset_of.hpp> + +#include <boost/optional.hpp> +#include <boost/none.hpp> +#include <boost/foreach.hpp> +#include <boost/assign/list_inserter.hpp> + +using namespace boost::bimaps; +using namespace boost; +using namespace std; + +int main() +{ + { + + typedef bimap< + + string, + multiset_of< optional<string> > + + > bm_type; + + bm_type bm; + + assign::insert( bm ) + + ( "John" , string("lazarus" ) ) + ( "Peter", string("vinicius") ) + ( "Simon", string("vinicius") ) + ( "Brian", none ) + ; + + cout << "John is working in " + << bm.left.at( "John" ).get_value_or( "no project" ) + << endl; + + cout << "Project vinicius is being developed by " << endl; + BOOST_FOREACH( bm_type::right_reference rp, + bm.right.equal_range( std::string("vinicius") ) ) + { + cout << rp.second << endl; + } + + cout << "This workers need a project " << endl; + BOOST_FOREACH( bm_type::right_reference rp, + bm.right.equal_range(none) ) + { + cout << rp.second << endl; + } + +} + + //[ code_population_bimap + + typedef bimap< + + unordered_set_of< std::string >, + multiset_of< long, std::greater<long> > + + > population_bimap; + + typedef population_bimap::value_type population; + + population_bimap pop; + pop.insert( population("China", 1321000000) ); + pop.insert( population("India", 1129000000) ); + pop.insert( population("United States", 301950000) ); + pop.insert( population("Indonesia", 234950000) ); + pop.insert( population("Brazil", 186500000) ); + pop.insert( population("Pakistan", 163630000) ); + + std::cout << "Countries by their population:" << std::endl; + + // First requirement + /*<< The right map view works like a + `std::multimap< long, std::string, std::greater<long> >`, + We can iterate over it to print the results in the required order. >>*/ + for( population_bimap::right_const_iterator + i = pop.right.begin(), iend = pop.right.end(); + i != iend ; ++i ) + { + std::cout << i->second << " with " << i->first << std::endl; + } + + // Second requirement + /*<< The left map view works like a `std::unordered_map< std::string, long >`, + given the name of the country we can use it to search for the population + in constant time >>*/ + std::cout << "Population of China: " << pop.left.at("China") << std::endl; + //] + + return 0; +} + diff --git a/src/boost/libs/bimap/example/projection.cpp b/src/boost/libs/bimap/example/projection.cpp new file mode 100644 index 00000000..8332385a --- /dev/null +++ b/src/boost/libs/bimap/example/projection.cpp @@ -0,0 +1,60 @@ +// 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/bimap/multiset_of.hpp> +using namespace boost::bimaps; + +void years_example() +{ + //[ code_projection_years + + typedef bimap<std::string,multiset_of<int,std::greater<int> > > bm_type; + + bm_type bm; + bm.insert( bm_type::value_type("John" ,34) ); + bm.insert( bm_type::value_type("Peter",24) ); + bm.insert( bm_type::value_type("Mary" ,12) ); + + // Find the name of the next younger person after Peter + + bm_type::left_const_iterator name_iter = bm.left.find("Peter"); + + bm_type::right_const_iterator years_iter = bm.project_right(name_iter); + + ++years_iter; + + std::cout << "The next younger person after Peter is " << years_iter->second; + //] +} + +int main() +{ + years_example(); + + return 0; +} + + diff --git a/src/boost/libs/bimap/example/repetitions_counter.cpp b/src/boost/libs/bimap/example/repetitions_counter.cpp new file mode 100644 index 00000000..d1657098 --- /dev/null +++ b/src/boost/libs/bimap/example/repetitions_counter.cpp @@ -0,0 +1,91 @@ +// 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/tokenizer.hpp> + +#include <boost/bimap/bimap.hpp> +#include <boost/bimap/unordered_set_of.hpp> +#include <boost/bimap/list_of.hpp> + +using namespace boost::bimaps; + +struct counter { + counter() : c(0) {} + counter& operator++() { ++c; return *this; } + unsigned int operator++(int) { return c++; } + operator const unsigned int() const { return c; } + private: + unsigned int c; +}; + +int main() +{ + //[ code_repetitions_counter + + typedef bimap + < + unordered_set_of< std::string >, + list_of< counter > /*< `counter` is an integer that is initialized + in zero in the constructor >*/ + + > word_counter; + + typedef boost::tokenizer<boost::char_separator<char> > text_tokenizer; + + std::string text= + "Relations between data in the STL are represented with maps." + "A map is a directed relation, by using it you are representing " + "a mapping. In this directed relation, the first type is related to " + "the second type but it is not true that the inverse relationship " + "holds. This is useful in a lot of situations, but there are some " + "relationships that are bidirectional by nature."; + + // feed the text into the container + word_counter wc; + text_tokenizer tok(text,boost::char_separator<char>(" \t\n.,;:!?'\"-")); + + for( text_tokenizer::const_iterator it = tok.begin(), it_end = tok.end(); + it != it_end ; ++it ) + { + /*<< Because the right collection type is `list_of`, the right data + is not used a key and can be modified in the same way as with + standard maps. >>*/ + ++ wc.left[*it]; + } + + // list words with counters by order of appearance + /*<< When we insert the elements using the left map view, the element + is inserted at the end of the list. >>*/ + for( word_counter::right_const_iterator + wit = wc.right.begin(), wit_end = wc.right.end(); + + wit != wit_end; ++wit ) + { + std::cout << wit->second << ": " << wit->first; + } + //] + + return 0; +} + + diff --git a/src/boost/libs/bimap/example/simple_bimap.cpp b/src/boost/libs/bimap/example/simple_bimap.cpp new file mode 100644 index 00000000..d7db74c2 --- /dev/null +++ b/src/boost/libs/bimap/example/simple_bimap.cpp @@ -0,0 +1,82 @@ +// 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> + +//[ code_simple_bimap + +#include <string> +#include <iostream> + +#include <boost/bimap.hpp> + +template< class MapType > +void print_map(const MapType & map, + const std::string & separator, + std::ostream & os ) +{ + typedef typename MapType::const_iterator const_iterator; + + for( const_iterator i = map.begin(), iend = map.end(); i != iend; ++i ) + { + os << i->first << separator << i->second << std::endl; + } +} + +int main() +{ + // Soccer World cup + + typedef boost::bimap< std::string, int > results_bimap; + typedef results_bimap::value_type position; + + results_bimap results; + results.insert( position("Argentina" ,1) ); + results.insert( position("Spain" ,2) ); + results.insert( position("Germany" ,3) ); + results.insert( position("France" ,4) ); + + std::cout << "The number of countries is " << results.size() + << std::endl; + + std::cout << "The winner is " << results.right.at(1) + << std::endl + << std::endl; + + std::cout << "Countries names ordered by their final position:" + << std::endl; + + // results.right works like a std::map< int, std::string > + + print_map( results.right, ") ", std::cout ); + + std::cout << std::endl + << "Countries names ordered alphabetically along with" + "their final position:" + << std::endl; + + // results.left works like a std::map< std::string, int > + + print_map( results.left, " ends in position ", std::cout ); + + return 0; +} +//] + diff --git a/src/boost/libs/bimap/example/standard_map_comparison.cpp b/src/boost/libs/bimap/example/standard_map_comparison.cpp new file mode 100644 index 00000000..37ff660e --- /dev/null +++ b/src/boost/libs/bimap/example/standard_map_comparison.cpp @@ -0,0 +1,93 @@ +// 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 <map> +#include <boost/bimap/bimap.hpp> + +using namespace boost::bimaps; + +//[ code_standard_map_comparison + +template< class Map, class CompatibleKey, class CompatibleData > +void use_it( Map & m, + const CompatibleKey & key, + const CompatibleData & data ) +{ + typedef typename Map::value_type value_type; + typedef typename Map::const_iterator const_iterator; + + m.insert( value_type(key,data) ); + const_iterator iter = m.find(key); + if( iter != m.end() ) + { + assert( iter->first == key ); + assert( iter->second == data ); + + std::cout << iter->first << " --> " << iter->second; + } + m.erase(key); +} + +int main() +{ + typedef bimap< set_of<std::string>, set_of<int> > bimap_type; + bimap_type bm; + + // Standard map + { + typedef std::map< std::string, int > map_type; + map_type m; + + use_it( m, "one", 1 ); + } + + // Left map view + { + typedef bimap_type::left_map map_type; + map_type & m = bm.left; + + use_it( m, "one", 1 ); + } + + // Reverse standard map + { + typedef std::map< int, std::string > reverse_map_type; + reverse_map_type rm; + + use_it( rm, 1, "one" ); + } + + // Right map view + { + typedef bimap_type::right_map reverse_map_type; + reverse_map_type & rm = bm.right; + + use_it( rm, 1, "one" ); + } + + return 0; +} +//] + diff --git a/src/boost/libs/bimap/example/step_by_step.cpp b/src/boost/libs/bimap/example/step_by_step.cpp new file mode 100644 index 00000000..f1f41355 --- /dev/null +++ b/src/boost/libs/bimap/example/step_by_step.cpp @@ -0,0 +1,102 @@ +// 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 <cassert> + +// A convinience header is avaiable in the boost directory: +#include <boost/bimap.hpp> + +int main() +{ + //[ code_step_by_step_definition + + typedef boost::bimap< int, std::string > bm_type; + bm_type bm; + //] + + //[ code_step_by_step_set_of_relations_view + + bm.insert( bm_type::value_type(1, "one" ) ); + bm.insert( bm_type::value_type(2, "two" ) ); + + std::cout << "There are " << bm.size() << "relations" << std::endl; + + for( bm_type::const_iterator iter = bm.begin(), iend = bm.end(); + iter != iend; ++iter ) + { + // iter->left : data : int + // iter->right : data : std::string + + std::cout << iter->left << " <--> " << iter->right << std::endl; + } + //] + + //[ code_step_by_step_left_map_view + + /*<< The type of `bm.left` is `bm_type::left_map` and the type + of `bm.right` is `bm_type::right_map` >>*/ + typedef bm_type::left_map::const_iterator left_const_iterator; + + for( left_const_iterator left_iter = bm.left.begin(), iend = bm.left.end(); + left_iter != iend; ++left_iter ) + { + // left_iter->first : key : int + // left_iter->second : data : std::string + + std::cout << left_iter->first << " --> " << left_iter->second << std::endl; + } + + /*<< `bm_type::left_`\ -type- can be used as a shortcut for the more verbose + `bm_type::left_map::`\ -type- >>*/ + bm_type::left_const_iterator left_iter = bm.left.find(2); + assert( left_iter->second == "two" ); + + /*<< This line produces the same effect of + `bm.insert( bm_type::value_type(3,"three") );` >>*/ + bm.left.insert( bm_type::left_value_type( 3, "three" ) ); + //] + + + + //[ code_step_by_step_right_map_view + + bm_type::right_const_iterator right_iter = bm.right.find("two"); + + // right_iter->first : key : std::string + // right_iter->second : data : int + + assert( right_iter->second == 2 ); + + assert( bm.right.at("one") == 1 ); + + bm.right.erase("two"); + + /*<< This line produces the same effect of + `bm.insert( bm_type::value_type(4,"four") );` >>*/ + bm.right.insert( bm_type::right_value_type( "four", 4 ) ); + //] + + return 0; +} + + diff --git a/src/boost/libs/bimap/example/tagged_simple_bimap.cpp b/src/boost/libs/bimap/example/tagged_simple_bimap.cpp new file mode 100644 index 00000000..1160ef5c --- /dev/null +++ b/src/boost/libs/bimap/example/tagged_simple_bimap.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> + +//[ code_tagged_simple_bimap + +#include <iostream> + +#include <boost/bimap.hpp> + +struct country {}; +struct place {}; + +int main() +{ + using namespace boost::bimaps; + + // Soccer World cup. + + typedef bimap + < + tagged< std::string, country >, + tagged< int , place > + + > results_bimap; + + typedef results_bimap::value_type position; + + results_bimap results; + results.insert( position("Argentina" ,1) ); + results.insert( position("Spain" ,2) ); + results.insert( position("Germany" ,3) ); + results.insert( position("France" ,4) ); + + std::cout << "Countries names ordered by their final position:" + << std::endl; + + /*<< `results.by<place>()` is equivalent to `results.right` >>*/ + for( results_bimap::map_by<place>::const_iterator + i = results.by<place>().begin(), + iend = results.by<place>().end() ; + i != iend; ++i ) + { + /*<< `get<Tag>` works for each view of the bimap >>*/ + std::cout << i->get<place >() << ") " + << i->get<country>() << std::endl; + } + + std::cout << std::endl + << "Countries names ordered alfabetically along with" + "their final position:" + << std::endl; + + /*<< `results.by<country>()` is equivalent to `results.left` >>*/ + for( results_bimap::map_by<country>::const_iterator + i = results.by<country>().begin(), + iend = results.by<country>().end() ; + i != iend; ++i ) + { + std::cout << i->get<country>() << " ends " + << i->get<place >() << "º" + << std::endl; + } + + return 0; +} +//] + diff --git a/src/boost/libs/bimap/example/tutorial_info_hook.cpp b/src/boost/libs/bimap/example/tutorial_info_hook.cpp new file mode 100644 index 00000000..a2e32148 --- /dev/null +++ b/src/boost/libs/bimap/example/tutorial_info_hook.cpp @@ -0,0 +1,163 @@ +// 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/bimap/multiset_of.hpp> + +using namespace boost::bimaps; + + +void tutorial_about_info_hook() +{ + //[ code_tutorial_info_hook_first + + typedef bimap< + + multiset_of< std::string >, // author + set_of< std::string >, // title + + with_info< std::string > // abstract + + > bm_type; + typedef bm_type::value_type book; + + bm_type bm; + + bm.insert( + + book( "Bjarne Stroustrup" , "The C++ Programming Language", + + "For C++ old-timers, the first edition of this book is" + "the one that started it all—the font of our knowledge." ) + ); + + + // Print the author of the bible + std::cout << bm.right.at("The C++ Programming Language"); + + // Print the abstract of this book + bm_type::left_iterator i = bm.left.find("Bjarne Stroustrup"); + std::cout << i->info; + //] + + // Contrary to the two key types, the information will be mutable + // using iterators. + + //[ code_tutorial_info_hook_mutable + + i->info += "More details about this book"; + //] + + // A new function is included in unique map views: info_at(key), that + // mimics the standard at(key) function but returned the associated + // information instead of the data. + + //[ code_tutorial_info_hook_info_at + + // Print the new abstract + std::cout << bm.right.info_at("The C++ Programming Language"); + //] +} + +struct author {}; +struct title {}; +struct abstract {}; + +void tutorial_about_tagged_info_hook() +{ + //[ code_tutorial_info_hook_tagged_info + + typedef bimap< + + multiset_of< tagged< std::string, author > >, + set_of< tagged< std::string, title > >, + + with_info< tagged< std::string, abstract > > + + > bm_type; + typedef bm_type::value_type book; + + bm_type bm; + + bm.insert( + + book( "Bjarne Stroustrup" , "The C++ Programming Language", + + "For C++ old-timers, the first edition of this book is" + "the one that started it all—the font of our knowledge." ) + ); + + // Print the author of the bible + std::cout << bm.by<title>().at("The C++ Programming Language"); + + // Print the abstract of this book + bm_type::map_by<author>::iterator i = bm.by<author>().find("Bjarne Stroustrup"); + std::cout << i->get<abstract>(); + + // Contrary to the two key types, the information will be mutable + // using iterators. + + i->get<abstract>() += "More details about this book"; + + // Print the new abstract + std::cout << bm.by<title>().info_at("The C++ Programming Language"); + //] +} + + +void bimap_without_an_info_hook() +{ + //[ code_tutorial_info_hook_nothing + + typedef bimap< + + multiset_of< std::string >, // author + set_of< std::string > // title + + > bm_type; + typedef bm_type::value_type book; + + bm_type bm; + + bm.insert( book( "Bjarne Stroustrup" , "The C++ Programming Language" ) ); + bm.insert( book( "Scott Meyers" , "Effective C++" ) ); + bm.insert( book( "Andrei Alexandrescu" , "Modern C++ Design" ) ); + + // Print the author of Modern C++ + std::cout << bm.right.at( "Modern C++ Design" ); + //] +} + + +int main() +{ + tutorial_about_info_hook(); + tutorial_about_tagged_info_hook(); + bimap_without_an_info_hook(); + + return 0; +} + + diff --git a/src/boost/libs/bimap/example/tutorial_modify_and_replace.cpp b/src/boost/libs/bimap/example/tutorial_modify_and_replace.cpp new file mode 100644 index 00000000..fbb93cef --- /dev/null +++ b/src/boost/libs/bimap/example/tutorial_modify_and_replace.cpp @@ -0,0 +1,118 @@ +// 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/bimap/support/lambda.hpp> + +using namespace boost::bimaps; + +void test_replace() +{ + //[ code_tutorial_replace + + typedef bimap< int, std::string > bm_type; + bm_type bm; + + bm.insert( bm_type::value_type(1,"one") ); + + // Replace (1,"one") with (1,"1") using the right map view + { + bm_type::right_iterator it = bm.right.find("one"); + + bool successful_replace = bm.right.replace_key( it, "1" ); + + assert( successful_replace ); + } + + bm.insert( bm_type::value_type(2,"two") ); + + // Fail to replace (1,"1") with (1,"two") using the left map view + { + assert( bm.size() == 2 ); + + bm_type::left_iterator it = bm.left.find(1); + + bool successful_replace = bm.left.replace_data( it, "two" ); + + /*<< `it` is still valid here, and the bimap was left unchanged >>*/ + assert( ! successful_replace ); + assert( bm.size() == 2 ); + } + //] +} + +void test_modify() +{ + //[ code_tutorial_modify + + typedef bimap< int, std::string > bm_type; + bm_type bm; + bm.insert( bm_type::value_type(1,"one") ); + + // Modify (1,"one") to (1,"1") using the right map view + { + bm_type::right_iterator it = bm.right.find("one"); + + bool successful_modify = bm.right.modify_key( it , _key = "1" ); + + assert( successful_modify ); + } + + bm.insert( bm_type::value_type(2,"two") ); + + // Fail to modify (1,"1") to (1,"two") using the left map view + { + assert( bm.size() == 2 ); + + bm_type::left_iterator it = bm.left.find(1); + + bool successful_modify = bm.left.modify_data( it, _data = "two" ); + + /*<< `it` is not longer valid and `(1,"1")` is removed from the bimap >>*/ + assert( ! successful_modify ); + assert( bm.size() == 1 ); + } + //] + + /* + // Modify (2,"two") to (3,"two") using the set of relations view + { + bm_type::iterator it = bm.begin(); + + bool successful_modify = bm.modify( it, ++_left ); + + assert( successful_modify ); + } + */ +} + +int main() +{ + test_replace(); + test_modify(); + return 0; +} + + diff --git a/src/boost/libs/bimap/example/tutorial_range.cpp b/src/boost/libs/bimap/example/tutorial_range.cpp new file mode 100644 index 00000000..6d3259ae --- /dev/null +++ b/src/boost/libs/bimap/example/tutorial_range.cpp @@ -0,0 +1,100 @@ +// 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/bimap/support/lambda.hpp> + +using namespace boost::bimaps; + +void using_upper_and_lower_bound() +{ + //[ code_tutorial_range_standard_way + + typedef bimap<int,std::string> bm_type; + bm_type bm; + + // ... + + bm_type::left_iterator iter_first = bm.left.lower_bound(20); + bm_type::left_iterator iter_second = bm.left.upper_bound(50); + + // range [iter_first,iter_second) contains the elements in [20,50] + //] + + // Subtle changes + { + //[ code_tutorial_range_standard_way_subtle_changes + + bm_type::left_iterator iter_first = bm.left.upper_bound(20); + bm_type::left_iterator iter_second = bm.left.lower_bound(50); + + // range [iter_first,iter_second) contains the elements in (20,50) + //] + } +} + +void using_range() +{ + //[ code_tutorial_range + + typedef bimap<int,std::string> bm_type; + bm_type bm; + + // ... + + /*<< `range_type` is a handy typedef equal to `std::pair<iterator,iterator>`. + `const_range_type` is provided too, and it is equal to + `std::pair<const_iterator,const_iterator>` >>*/ + bm_type::left_range_type r; + + /*<< _key is a Boost.Lambda placeholder. To use it you have to include + `<boost/bimap/support/lambda.hpp>` >>*/ + r = bm.left.range( 20 <= _key, _key <= 50 ); // [20,50] + + r = bm.left.range( 20 < _key, _key < 50 ); // (20,50) + + r = bm.left.range( 20 <= _key, _key < 50 ); // [20,50) + //] + + //[ code_tutorial_range_unbounded + + r = bm.left.range( 20 <= _key, unbounded ); // [20,inf) + + r = bm.left.range( unbounded , _key < 50 ); // (-inf,50) + + /*<< This is equivalent to std::make_pair(s.begin(),s.end()) >>*/ + r = bm.left.range( unbounded , unbounded ); // (-inf,inf) + //] +} + +int main() +{ + using_upper_and_lower_bound(); + using_range(); + + return 0; +} + + diff --git a/src/boost/libs/bimap/example/unconstrained_collection.cpp b/src/boost/libs/bimap/example/unconstrained_collection.cpp new file mode 100644 index 00000000..1af03563 --- /dev/null +++ b/src/boost/libs/bimap/example/unconstrained_collection.cpp @@ -0,0 +1,94 @@ +// 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 <map> +#include <boost/bimap/bimap.hpp> +#include <boost/bimap/unconstrained_set_of.hpp> +#include <boost/bimap/support/lambda.hpp> + +using namespace boost::bimaps; + +int main() +{ + // Boost.Bimap + { + //[ code_unconstrained_collection_bimap + + typedef bimap< std::string, unconstrained_set_of<int> > bm_type; + typedef bm_type::left_map map_type; + + bm_type bm; + map_type & m = bm.left; + //] + + //[ code_unconstrained_collection_common + + m["one"] = 1; + + assert( m.find("one") != m.end() ); + + for( map_type::iterator i = m.begin(), iend = m.end(); i != iend; ++i ) + { + /*<< The right collection of the bimap is mutable so its elements + can be modified using iterators. >>*/ + ++(i->second); + } + + m.erase("one"); + //] + + m["one"] = 1; + m["two"] = 2; + + //[ code_unconstrained_collection_only_for_bimap + typedef map_type::const_iterator const_iterator; + typedef std::pair<const_iterator,const_iterator> const_range; + + /*<< This range is a model of BidirectionalRange, read the docs of + Boost.Range for more information. >>*/ + const_range r = m.range( "one" <= _key, _key <= "two" ); + for( const_iterator i = r.first; i != r.second; ++i ) + { + std::cout << i->first << "-->" << i->second << std::endl; + } + + m.modify_key( m.begin(), _key = "1" ); + //] + } + + // Standard map + { + //[ code_unconstrained_collection_map + + typedef std::map< std::string, int > map_type; + + map_type m; + //] + } + + return 0; +} + + diff --git a/src/boost/libs/bimap/example/user_defined_names.cpp b/src/boost/libs/bimap/example/user_defined_names.cpp new file mode 100644 index 00000000..53cb2a30 --- /dev/null +++ b/src/boost/libs/bimap/example/user_defined_names.cpp @@ -0,0 +1,142 @@ +// 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/bimap/multiset_of.hpp> + +using namespace boost::bimaps; + +void untagged_version() +{ + //[ code_user_defined_names_untagged_version + + typedef bimap + < + multiset_of<std::string>, + int + + > People; + + People people; + + // ... + + int user_id; + std::cin >> user_id; + + // people.right : map<id,name> + + People::right_const_iterator id_iter = people.right.find(user_id); + if( id_iter != people.right.end() ) + { + // first : id + // second : name + + std::cout << "name: " << id_iter->second << std::endl + << "id: " << id_iter->first << std::endl; + } + else + { + std::cout << "Unknown id, users are:" << std::endl; + + // people.left : map<name,id> + + for( People::left_const_iterator + name_iter = people.left.begin(), + iend = people.left.end(); + + name_iter != iend; ++name_iter ) + { + // first : name + // second : id + + std::cout << "name: " << name_iter->first << std::endl + << "id: " << name_iter->second << std::endl; + } + } + //] +} + +struct id {}; +struct name {}; + +void tagged_version() +{ + //[ code_user_defined_names_tagged_version + + //<- + /* + //-> + struct id {}; // Tag for the identification number + struct name {}; // Tag for the name of the person + //<- + */ + //-> + + typedef bimap + < + tagged< int , id > , + multiset_of< tagged< std::string, name > > + + > People; + + People people; + + // ... + + int user_id; + std::cin >> user_id; + + People::map_by<id>::const_iterator id_iter = people.by<id>().find(user_id); + if( id_iter != people.by<id>().end() ) + { + std::cout << "name: " << id_iter->get<name>() << std::endl + << "id: " << id_iter->get<id>() << std::endl; + } + else + { + std::cout << "Unknown id, users are:" << std::endl; + + for( People::map_by<name>::const_iterator + name_iter = people.by<name>().begin(), + iend = people.by<name>().end(); + + name_iter != iend; ++name_iter ) + { + std::cout << "name: " << name_iter->get<name>() << std::endl + << "id: " << name_iter->get<id>() << std::endl; + } + } + //] +} + +int main() +{ + untagged_version(); + tagged_version(); + + return 0; +} + |