Update of /cvsroot/boost/boost/libs/bimap/example
In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv24149/libs/bimap/example
Added Files:
Jamfile.v2 at_function_examples.cpp mighty_bimap.cpp
population_bimap.cpp projection.cpp repetitions_counter.cpp
simple_bimap.cpp standard_map_comparison.cpp step_by_step.cpp
tagged_simple_bimap.cpp tutorial_modify_and_replace.cpp
tutorial_range.cpp unconstrained_collection.cpp
user_defined_names.cpp
Log Message:
first bimap commit
--- NEW FILE: Jamfile.v2 ---
# 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 projection.cpp ]
;
test-suite "bimap_and_boost"
:
[ run bimap_and_boost/property_map.cpp ]
[ run bimap_and_boost/range.cpp ]
[ run bimap_and_boost/lambda.cpp ]
[ run bimap_and_boost/assign.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 ]
;
--- NEW FILE: at_function_examples.cpp ---
// 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 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;
}
--- NEW FILE: mighty_bimap.cpp ---
// 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 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
<
tagged< unordered_set_of< std::string >, spanish >,
tagged< unordered_set_of< 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;
}
//]
--- NEW FILE: population_bimap.cpp ---
// 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 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>
using namespace boost::bimaps;
int main()
{
//[ 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;
}
--- NEW FILE: projection.cpp ---
// 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 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;
}
--- NEW FILE: repetitions_counter.cpp ---
// 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 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;
}
--- NEW FILE: simple_bimap.cpp ---
// 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 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;
}
//]
--- NEW FILE: standard_map_comparison.cpp ---
// 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 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;
}
//]
--- NEW FILE: step_by_step.cpp ---
// 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 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;
}
//]
--- NEW FILE: tagged_simple_bimap.cpp ---
// 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 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;
}
//]
--- NEW FILE: tutorial_modify_and_replace.cpp ---
// 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 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;
}
//]
--- NEW FILE: tutorial_range.cpp ---
// 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 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;
// ...
std::pair< bm_type::left_iterator, bm_type::left_iterator > 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;
}
--- NEW FILE: unconstrained_collection.cpp ---
// 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 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;
}
--- NEW FILE: user_defined_names.cpp ---
// 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 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 >,
tagged< multiset_of<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;
}
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Boost-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/boost-cvs