Update of /cvsroot/boost/boost/libs/bimap/example/bimap_and_boost
In directory 
sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv24149/libs/bimap/example/bimap_and_boost

Added Files:
        assign.cpp lambda.cpp property_map.cpp range.cpp 
        serialization.cpp 
Log Message:
first bimap commit

--- NEW FILE: assign.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 <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;
}

--- NEW FILE: lambda.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 <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;
}



--- NEW FILE: property_map.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/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 << 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;
}
//]


--- NEW FILE: 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/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;
}
//]


--- NEW FILE: serialization.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 <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;
}



-------------------------------------------------------------------------
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

Reply via email to