A successful work-around for using const map reference as an output
authorbeckerjc
Sat, 17 Apr 2004 21:50:48 +0000
changeset 3524b89077ab715
parent 351 01fb9da7a363
child 353 eeae2f4a0d74
A successful work-around for using const map reference as an output
parameter to Kruskal().
src/work/johanna/kruskal.h
src/work/johanna/kruskal_test.cc
     1.1 --- a/src/work/johanna/kruskal.h	Sat Apr 17 19:54:04 2004 +0000
     1.2 +++ b/src/work/johanna/kruskal.h	Sat Apr 17 21:50:48 2004 +0000
     1.3 @@ -37,6 +37,29 @@
     1.4      return tot_cost;
     1.5    }
     1.6  
     1.7 +  /* A work-around for running Kruskal with const-reference bool maps... */
     1.8 +
     1.9 +  template<typename Map>
    1.10 +  class NonConstMapWr {
    1.11 +    const Map &m;
    1.12 +  public:
    1.13 +    typedef typename Map::ValueType ValueType;
    1.14 +
    1.15 +    NonConstMapWr(const Map &_m) : m(_m) {}
    1.16 +
    1.17 +    template<typename KeyType>
    1.18 +    void set(KeyType const& k, ValueType const &v) const { m.set(k,v); }
    1.19 +  };
    1.20 +
    1.21 +  template <typename Graph, typename InputEdgeOrder, typename OutBoolMap>
    1.22 +  inline
    1.23 +  typename InputEdgeOrder::ValueType
    1.24 +  Kruskal(Graph const& G, InputEdgeOrder const& edges, 
    1.25 +	  OutBoolMap const& out_map)
    1.26 +  {
    1.27 +    NonConstMapWr<OutBoolMap> map_wr(out_map);
    1.28 +    return Kruskal(G, edges, map_wr);
    1.29 +  }  
    1.30  
    1.31    
    1.32    /* ** ** Output-objektumok: egyszeruen extra bool mapek ** ** */
    1.33 @@ -49,7 +72,7 @@
    1.34  
    1.35    template<typename Iterator>
    1.36    class SequenceOutput {
    1.37 -    Iterator it;
    1.38 +    mutable Iterator it;
    1.39  
    1.40    public:
    1.41      typedef bool ValueType;
    1.42 @@ -57,7 +80,7 @@
    1.43      SequenceOutput(Iterator const &_it) : it(_it) {}
    1.44  
    1.45      template<typename KeyType>
    1.46 -    void set(KeyType const& k, bool v) { if(v) {*it=k; ++it;} }
    1.47 +    void set(KeyType const& k, bool v) const { if(v) {*it=k; ++it;} }
    1.48    };
    1.49  
    1.50    template<typename Iterator>
    1.51 @@ -177,6 +200,7 @@
    1.52    /// \brief Wrapper to Kruskal().
    1.53    /// Input is from an EdgeMap, output is a plain boolmap.
    1.54    template <typename Graph, typename EdgeCostMap, typename RetEdgeBoolMap>
    1.55 +  inline
    1.56    typename EdgeCostMap::ValueType
    1.57    Kruskal_EdgeCostMapIn_BoolMapOut(Graph const& G,
    1.58  				   EdgeCostMap const& edge_costs,
    1.59 @@ -193,6 +217,7 @@
    1.60    /// \brief Wrapper to Kruskal().
    1.61    /// Input is from an EdgeMap, output is to a sequence.
    1.62    template <typename Graph, typename EdgeCostMap, typename RetIterator>
    1.63 +  inline
    1.64    typename EdgeCostMap::ValueType
    1.65    Kruskal_EdgeCostMapIn_IteratorOut(Graph const& G,
    1.66  				    EdgeCostMap const& edge_costs,
     2.1 --- a/src/work/johanna/kruskal_test.cc	Sat Apr 17 19:54:04 2004 +0000
     2.2 +++ b/src/work/johanna/kruskal_test.cc	Sat Apr 17 21:50:48 2004 +0000
     2.3 @@ -100,12 +100,18 @@
     2.4    }
     2.5  
     2.6    tree_edge_vec.clear();
     2.7 -  SequenceOutput< back_insert_iterator< vector<Edge> > > 
     2.8 -    vec_filler(back_inserter(tree_edge_vec));
     2.9 +//   SequenceOutput< back_insert_iterator< vector<Edge> > > 
    2.10 +//     vec_filler(back_inserter(tree_edge_vec));
    2.11 +//   cout << "Nemkonst koltseggel tarhatekonyabban: "
    2.12 +//        << Kruskal(G,
    2.13 +// 		  KruskalMapVec<ECostMap>(G, edge_cost_map),
    2.14 +// 		  vec_filler)
    2.15 +//        << endl;
    2.16    cout << "Nemkonst koltseggel tarhatekonyabban: "
    2.17         << Kruskal(G,
    2.18  		  KruskalMapVec<ECostMap>(G, edge_cost_map),
    2.19 -		  vec_filler)
    2.20 +		  makeSequenceOutput(back_inserter(tree_edge_vec))
    2.21 +		  )
    2.22         << endl;
    2.23  
    2.24    i = 1;