Boost logo

Boost :

Subject: Re: [boost] [iterator] iterator_facade reference type problem
From: er (erwann.rogard_at_[hidden])
Date: 2010-01-14 10:24:46


>>
>> But what do I do about the reference type? I can't use
>> std::pair<const KEY,VALUE>& because
>

Provided I understood correctly, would this be a possibility?

#ifndef BOOST_MAP2_ZIP_FUNC
#define BOOST_MAP2_ZIP_FUNC
#include <boost/tuple/tuple.hpp>
#include <boost/iterator/zip_iterator.hpp>
#include <boost/iterator/transform_iterator.hpp>
#include <boost/ref.hpp>
#include <utility>

namespace boost{
namespace map2{

template<typename Key,typename Value>
struct zip_func
{
        zip_func(){}

        typedef const boost::tuple<
             const Key&,
         Value&
>& argument_type;
     typedef std::pair<Key,boost::reference_wrapper<Value> > result_type;

           result_type operator()(argument_type t) const
           {
                return result_type(
                 boost::get<0>(t),
             boost::ref(boost::get<1>(t))
         );
           }

};

template<typename ItK,typename ItV>
struct iterator{
        typedef boost::tuple<
                   ItK,
                   ItV
> it_tuple_;

        typedef typename boost::zip_iterator<it_tuple_> zip_iter_;
     typedef typename boost::iterator_value<ItK>::type k_;
     typedef typename boost::iterator_value<ItV>::type v_;
     typedef zip_func<k_,v_> func_;
     typedef boost::transform_iterator<func_,zip_iter_> type;

        static type call(ItK it_k,ItV it_v){
             return type(zip_iter_(it_tuple_(it_k,it_v)),func_());
     }

};

}// map2
}// boost

#endif

#include <vector>
#include <iostream>
#include <boost/assign/std/vector.hpp>
#include <map2_iterator.hpp>

int main (int argc, char * const argv[]) {

        typedef int key_;
     typedef int val_;
        typedef std::vector<key_> keys_;
     typedef std::vector<val_> vals_;

     using namespace boost::assign;

     keys_ keys; keys += 1,2,3;
     vals_ vals; vals += 1,2,3;

        typedef boost::range_iterator<keys_>::type it_k_;
        typedef boost::range_iterator<vals_>::type it_v_;

     typedef boost::map2::iterator<it_k_,it_v_> meta_map2_;
        typedef meta_map2_::type it_map_;

        it_map_ b_map = meta_map2_::call(boost::begin(keys),boost::begin(vals));
        it_map_ e_map = meta_map2_::call(boost::end(keys),boost::end(vals));

        int i = 1;
        while(b_map!=e_map){
             BOOST_ASSERT(vals[i-1] == i);
         ++(b_map->second);
                BOOST_ASSERT(vals[i-1] == i+1);
         ++b_map;
         ++i;
     }

     return 0;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk