BGL dfs from a set of source nodes - c++

Problem
Having an adjacency list graph, I would like to traverse it with a DFS algorithm from a specific set of source nodes. The main problem is that the color map is passed by value.
I tried
To encapsulate the color map by reference into a structure :
class ref_color_map_wrapper
{
public:
typedef boost::default_color_type color_type;
typedef std::vector<color_type> color_map_type;
private:
color_map_type &color_map;
public:
ref_color_map_wrapper(color_map_type& color_map)
: color_map(color_map)
{}
color_type& operator[](size_t i)
{
return color_map[i];
}
const color_type& operator[](size_t i) const
{
return color_map[i];
}
};
namespace boost
{
template <>
struct property_traits<ref_color_map_wrapper>
{
typedef boost::read_write_property_map_tag category;
typedef boost::default_color_type value_type;
typedef boost::default_color_type& reference;
typedef size_t key_type;
};
void put(ref_color_map_wrapper& color_map, vertex_descriptor& v, boost::default_color_type color)
{
color_map[v] = color;
}
boost::default_color_type get(ref_color_map_wrapper& color_map, vertex_descriptor& v)
{
return color_map[v];
}
void put(ref_color_map_wrapper& color_map, const vertex_descriptor& v, boost::default_color_type color)
{
color_map[v] = color;
}
boost::default_color_type get(const ref_color_map_wrapper& color_map, const vertex_descriptor& v)
{
return color_map[v];
}
}
And finally the code of the DFS :
typedef std::vector<boost::default_color_type> color_map_type;
color_map_type color_map(boost::num_vertices(graph), boost::white_color);
ref_color_map_wrapper ref_color_map(color_map);
for(auto it = root_set.begin(); it != root_set.end(); ++it)
{
size_t i = boost::get(boost::vertex_index_t(), graph, *it);
if(color_map[i] == boost::white_color)
{
boost::depth_first_visit(graph, *it, boost::default_dfs_visitor(), ref_color_map);
}
}
Compilation error
/usr/local/include/boost/property_map/property_map.hpp: In instantiation of ‘void boost::ReadablePropertyMapConcept<PMap, Key>::constraints() [with PMap = gc::ref_color_map_wrapper; Key = long unsigned int]’:
/usr/local/include/boost/concept/detail/has_constraints.hpp:32:14: required by substitution of ‘template<class Model> boost::concepts::detail::yes boost::concepts::detail::has_constraints_(Model*, boost::concepts::detail::wrap_constraints<Model, (& Model:: constraints)>*) [with Model = boost::ReadablePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int>]’
/usr/local/include/boost/concept/detail/has_constraints.hpp:42:5: required from ‘const bool boost::concepts::not_satisfied<boost::ReadablePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int> >::value’
/usr/local/include/boost/concept/detail/has_constraints.hpp:45:31: required from ‘struct boost::concepts::not_satisfied<boost::ReadablePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int> >’
/usr/local/include/boost/mpl/if.hpp:67:11: required from ‘struct boost::mpl::if_<boost::concepts::not_satisfied<boost::ReadablePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int> >, boost::concepts::constraint<boost::ReadablePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int> >, boost::concepts::requirement<boost::concepts::failed************ boost::ReadablePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int>::************> >’
/usr/local/include/boost/concept/detail/general.hpp:50:8: required from ‘struct boost::concepts::requirement_<void (*)(boost::ReadablePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int>)>’
/usr/local/include/boost/concept_check.hpp:45:1: [ skipping 4 instantiation contexts ]
/usr/local/include/boost/concept/detail/has_constraints.hpp:45:31: required from ‘struct boost::concepts::not_satisfied<boost::ReadWritePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int> >’
/usr/local/include/boost/mpl/if.hpp:67:11: required from ‘struct boost::mpl::if_<boost::concepts::not_satisfied<boost::ReadWritePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int> >, boost::concepts::constraint<boost::ReadWritePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int> >, boost::concepts::requirement<boost::concepts::failed************ boost::ReadWritePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int>::************> >’
/usr/local/include/boost/concept/detail/general.hpp:50:8: required from ‘struct boost::concepts::requirement_<void (*)(boost::ReadWritePropertyMapConcept<gc::ref_color_map_wrapper, long unsigned int>)>’
/usr/local/include/boost/graph/depth_first_search.hpp:88:1: required from ‘void boost::detail::depth_first_visit_impl(const IncidenceGraph&, typename boost::graph_traits<Graph>::vertex_descriptor, DFSVisitor&, ColorMap, TerminatorFunc) [with IncidenceGraph = boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, boost::variant<const void*, std::pair<void*, void*> > >; DFSVisitor = boost::dfs_visitor<>; ColorMap = gc::ref_color_map_wrapper; TerminatorFunc = boost::detail::nontruth2; typename boost::graph_traits<Graph>::vertex_descriptor = long unsigned int]’
/usr/local/include/boost/graph/depth_first_search.hpp:314:5: required from ‘void boost::depth_first_visit(const IncidenceGraph&, typename boost::graph_traits<Graph>::vertex_descriptor, DFSVisitor, ColorMap) [with IncidenceGraph = boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, boost::variant<const void*, std::pair<void*, void*> > >; DFSVisitor = boost::dfs_visitor<>; ColorMap = gc::ref_color_map_wrapper; typename boost::graph_traits<Graph>::vertex_descriptor = long unsigned int]’
../include/garbage_collector.hpp:169:87: required from here
/usr/local/include/boost/property_map/property_map.hpp:200:7: error: no matching function for call to ‘get(gc::ref_color_map_wrapper&, long unsigned int&)’
/usr/local/include/boost/property_map/property_map.hpp:200:7: note: candidates are:
In file included from /usr/local/include/boost/tuple/tuple.hpp:33:0,
from /usr/local/include/boost/unordered/detail/allocate.hpp:27,
from /usr/local/include/boost/unordered/detail/buckets.hpp:15,
from /usr/local/include/boost/unordered/detail/table.hpp:10,
from /usr/local/include/boost/unordered/detail/equivalent.hpp:14,
from /usr/local/include/boost/unordered/unordered_set.hpp:17,
from /usr/local/include/boost/unordered_set.hpp:16,
from /usr/local/include/boost/graph/adjacency_list.hpp:21,
from ../include/garbage_collector.hpp:6,
from main.cpp:3:
/usr/local/include/boost/tuple/detail/tuple_basic.hpp:225:1: note: template<int N, class HT, class TT> typename boost::tuples::access_traits<typename boost::tuples::element<N, boost::tuples::cons<HT, TT> >::type>::const_type boost::tuples::get(const boost::tuples::cons<HT, TT>&)
/usr/local/include/boost/tuple/detail/tuple_basic.hpp:225:1: note: template argument deduction/substitution failed:
In file included from /usr/local/include/boost/graph/adjacency_list.hpp:36:0,
from ../include/garbage_collector.hpp:6,
from main.cpp:3:
/usr/local/include/boost/property_map/property_map.hpp:200:7: note: ‘gc::ref_color_map_wrapper’ is not derived from ‘const boost::tuples::cons<HT, TT>’
In file included from /usr/local/include/boost/tuple/tuple.hpp:33:0,
from /usr/local/include/boost/unordered/detail/allocate.hpp:27,
from /usr/local/include/boost/unordered/detail/buckets.hpp:15,
from /usr/local/include/boost/unordered/detail/table.hpp:10,
from /usr/local/include/boost/unordered/detail/equivalent.hpp:14,
from /usr/local/include/boost/unordered/unordered_set.hpp:17,
from /usr/local/include/boost/unordered_set.hpp:16,
from /usr/local/include/boost/graph/adjacency_list.hpp:21,
from ../include/garbage_collector.hpp:6,
from main.cpp:3:
/usr/local/include/boost/tuple/detail/tuple_basic.hpp:211:1: note: template<int N, class HT, class TT> typename boost::tuples::access_traits<typename boost::tuples::element<N, boost::tuples::cons<HT, TT> >::type>::non_const_type boost::tuples::get(boost::tuples::cons<HT, TT>&)
/usr/local/include/boost/tuple/detail/tuple_basic.hpp:211:1: note: template argument deduction/substitution failed:
In file included from /usr/local/include/boost/graph/adjacency_list.hpp:36:0,
from ../include/garbage_collector.hpp:6,
from main.cpp:3:
/usr/local/include/boost/property_map/property_map.hpp:200:7: note: ‘gc::ref_color_map_wrapper’ is not derived from ‘boost::tuples::cons<HT, TT>’
/usr/local/include/boost/property_map/property_map.hpp:179:19: note: template<class T> const T& get(const T*, std::ptrdiff_t)
/usr/local/include/boost/property_map/property_map.hpp:179:19: note: template argument deduction/substitution failed:
/usr/local/include/boost/property_map/property_map.hpp:200:7: note: mismatched types ‘const T*’ and ‘gc::ref_color_map_wrapper’
Graph definition
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, vertex_info_type> graph_type;
What's working
boost::get(ref_color_map, *it);
boost::put(ref_color_map, *it, boost::white_color);
works without any compilation error…

You need to put your get() and put() functions in the namespace where ref_color_map resides, since they are found via ADL. See here.

Related

How can I use std::rotate to rotate an array of pairs whose first member is const?

I am implementing a map, and I would like to use std::rotate on an array of pairs, of which the first member is const (so that the key cannot be changed when inserted in the map). It is equivalent to the code below which doesn't compile:
#include <utility>
#include <array>
#include <algorithm>
int main()
{
typedef std::pair<const int, int> map_entry;
std::array<map_entry, 3> a{ { {2, 0}, {1, 0}, {3, 0} } };
std::rotate(&a[0], &a[1], &a[3]);
}
Unfortunately, I cannot control the type of the pair ("value_type") which needs to be defined as follows be compatible with std::unordered_map:
template <class K, class T, class H, class P, class A>
class unordered_map
{
public:
typedef K key_type;
typedef std::pair<const K, T> value_type;
typedef T mapped_type;
Is there way for me to use std::rotate on such an array, maybe by removing the const somehow?
Here is the compile error:
$ g++ -std=c++11 xx.cxx
In file included from /usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/utility:70:0,
from xx.cxx:1:
/usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/bits/stl_pair.h: In instantiation of ‘std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(std::pair<_T1 , _T2>&&) [with _T1 = const int; _T2 = int]’:
/usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/bits/stl_algo.h:1610:22: required from ‘void std::__rotate(_RandomAccessIterator, _RandomAccessIterat or, _RandomAccessIterator, std::random_access_iterator_tag) [with _RandomAccessIterator = std::pair<const int, int>*]’
/usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/bits/stl_algo.h:1686:59: required from ‘void std::rotate(_FIter, _FIter, _FIter) [with _FIter = std:: pair<const int, int>*]’
xx.cxx:9:36: required from here
/usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/bits/stl_pair.h:170:8: error: assignment of read-only member ‘std::pair<const int, int>::first’
first = std::forward<first_type>(__p.first);
^
In file included from /usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/bits/stl_pair.h:59:0,
from /usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/utility:70,
from xx.cxx:1:
/usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/bits/move.h: In instantiation of ‘void std::swap(_Tp&, _Tp&) [with _Tp = const int]’:
/usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/bits/stl_pair.h:199:23: required from ‘void std::pair<_T1, _T2>::swap(std::pair<_T1, _T2>&) [with _T1 = const int; _T2 = int]’
/usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/bits/stl_pair.h:256:7: required from ‘void std::swap(std::pair<_T1, _T2>&, std::pair<_T1, _T2>&) [wit h _T1 = const int; _T2 = int]’
/usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/bits/stl_algobase.h:147:22: required from ‘void std::iter_swap(_ForwardIterator1, _ForwardIterator2) [with _ForwardIterator1 = std::pair<const int, int>*; _ForwardIterator2 = std::pair<const int, int>*]’
/usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/bits/stl_algo.h:1616:28: required from ‘void std::__rotate(_RandomAccessIterator, _RandomAccessIterat or, _RandomAccessIterator, std::random_access_iterator_tag) [with _RandomAccessIterator = std::pair<const int, int>*]’
/usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/bits/stl_algo.h:1686:59: required from ‘void std::rotate(_FIter, _FIter, _FIter) [with _FIter = std:: pair<const int, int>*]’
xx.cxx:9:36: required from here
/usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/bits/move.h:176:11: error: assignment of read-only reference ‘__a’
__a = _GLIBCXX_MOVE(__b);
^
/usr/lib/gcc/i686-pc-cygwin/4.8.2/include/c++/bits/move.h:177:11: error: assignment of read-only reference ‘__b’
__b = _GLIBCXX_MOVE(__tmp);
^
Simple: don't use std::pair. Design a class that has the semantics that you need and use that. In particular, don't make the data members const. Instead, make them private and write accessors to enforce const-ness. int first() const;, int second() const; and int& second(); are a good start. But they really should be named key() and value(), or something else that matches your design better.
I found my solution. Internally, I store the pair without the const (mutable_value_type), but I still define value_type with the const and this is what is returned when you dereference an iterator for example.
template <class K, class T, class H, class P, class A>
class unordered_map
{
public:
typedef K key_type;
typedef std::pair<const K, T> value_type;
typedef std::pair<K, T> mutable_value_type;

How to use unordered_map in base class with curiously recurring template pattern?

I'm using curiously recurring template pattern:
#include <unordered_map>
using namespace std;
template<class S>
struct State {
unordered_map<int, S> children; // <-- problem!
};
struct TicTacToeState : public State<TicTacToeState> {
// implementation
};
int main() {
return 0;
}
Basically, I want to have an unordered_map of children states in a (base) State.
Compile with g++ file.cpp -std=c++11:
In file included from /usr/include/c++/4.8/utility:70:0,
from /usr/include/boost/config/no_tr1/utility.hpp:21,
from /usr/include/boost/config/select_stdlib_config.hpp:37,
from /usr/include/boost/config.hpp:40,
from /usr/include/boost/functional/hash/hash_fwd.hpp:17,
from /usr/include/boost/functional/hash/hash.hpp:13,
from /usr/include/boost/functional/hash.hpp:6,
from tests/../examples/tic_tac_toe.cpp:1,
from tests/test_tic_tac_toe.cpp:3:
/usr/include/c++/4.8/bits/stl_pair.h: In instantiation of ‘struct std::pair<const long unsigned int, TicTacToeState>’:
/usr/include/c++/4.8/type_traits:615:28: required from ‘struct std::__is_destructible_impl<std::pair<const long unsigned int, TicTacToeState> >’
/usr/include/c++/4.8/type_traits:637:12: required from ‘struct std::__is_destructible_safe<std::pair<const long unsigned int, TicTacToeState>, false, false>’
/usr/include/c++/4.8/type_traits:652:12: required from ‘struct std::is_destructible<std::pair<const long unsigned int, TicTacToeState> >’
/usr/include/c++/4.8/type_traits:116:12: required from ‘struct std::__and_<std::is_destructible<std::pair<const long unsigned int, TicTacToeState> >, std::__is_direct_constructible_impl<std::pair<const long unsigned int, TicTacToeState>, const std::pair<const long unsigned int, TicTacToeState>&> >’
/usr/include/c++/4.8/type_traits:817:12: required from ‘struct std::__is_direct_constructible_new_safe<std::pair<const long unsigned int, TicTacToeState>, const std::pair<const long unsigned int, TicTacToeState>&>’
/usr/include/c++/4.8/type_traits:895:12: [ skipping 5 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/usr/include/c++/4.8/type_traits:974:12: required from ‘struct std::is_copy_constructible<std::pair<const long unsigned int, TicTacToeState> >’
/usr/include/c++/4.8/bits/alloc_traits.h:540:12: required from ‘struct std::__is_copy_insertable<std::allocator<std::pair<const long unsigned int, TicTacToeState> > >’
/usr/include/c++/4.8/bits/alloc_traits.h:560:63: required by substitution of ‘template<class _Alloc> using __check_copy_constructible = std::__allow_copy_cons<std::__is_copy_insertable<_Alloc>::value> [with _Alloc = std::allocator<std::pair<const long unsigned int, TicTacToeState> >]’
/usr/include/c++/4.8/bits/unordered_map.h:97:11: required from ‘class std::unordered_map<long unsigned int, TicTacToeState, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, TicTacToeState> > >’
tests/../examples/../gtsa.hpp:110:30: required from ‘struct State<TicTacToeState, TicTacToeMove>’
tests/../examples/tic_tac_toe.cpp:69:32: required from here
/usr/include/c++/4.8/bits/stl_pair.h:102:11: error: ‘std::pair<_T1, _T2>::second’ has incomplete type
_T2 second; /// #c second is a copy of the second object
^
In file included from tests/test_tic_tac_toe.cpp:3:0:
tests/../examples/tic_tac_toe.cpp:69:8: error: forward declaration of ‘struct TicTacToeState’
struct TicTacToeState : public State<TicTacToeState, TicTacToeMove> {
^
In file included from /usr/include/c++/4.8/utility:70:0,
from /usr/include/boost/config/no_tr1/utility.hpp:21,
from /usr/include/boost/config/select_stdlib_config.hpp:37,
from /usr/include/boost/config.hpp:40,
from /usr/include/boost/functional/hash/hash_fwd.hpp:17,
from /usr/include/boost/functional/hash/hash.hpp:13,
from /usr/include/boost/functional/hash.hpp:6,
from tests/../examples/tic_tac_toe.cpp:1,
from tests/test_tic_tac_toe.cpp:3:
/usr/include/c++/4.8/bits/stl_pair.h: In instantiation of ‘constexpr std::pair<_T1, _T2>::pair(_U1&&, _U2&&) [with _U1 = long unsigned int&; _U2 = TicTacToeState&; <template-parameter-2-3> = void; _T1 = const long unsigned int; _T2 = TicTacToeState]’:
tests/../examples/../gtsa.hpp:640:57: required from ‘S* MonteCarloTreeSearch<S, M>::add_child(S*, M&) [with S = TicTacToeState; M = TicTacToeMove]’
tests/../examples/../gtsa.hpp:472:41: required from ‘S* MonteCarloTreeSearch<S, M>::tree_policy(S*, S*) [with S = TicTacToeState; M = TicTacToeMove]’
tests/../examples/../gtsa.hpp:453:44: required from ‘void MonteCarloTreeSearch<S, M>::monte_carlo_tree_search(S*) [with S = TicTacToeState; M = TicTacToeMove]’
tests/../examples/../gtsa.hpp:433:41: required from ‘M MonteCarloTreeSearch<S, M>::get_move(S*) [with S = TicTacToeState; M = TicTacToeMove]’
tests/test_tic_tac_toe.cpp:123:1: required from here
/usr/include/c++/4.8/bits/stl_pair.h:145:64: error: using invalid field ‘std::pair<_T1, _T2>::second’
: first(std::forward<_U1>(__x)), second(std::forward<_U2>(__y)) { }
^
My understanding is:
I'm asking to create a struct TicTacToeState, it looks what's the base class, aha, it's template, fine, it tries to instantiate State with S = TicTacToeState but fails, because unordered_map<int, S> needs already defined type for map values, but TicTacToeState wasn't defined.
Placing TicTacToeState in front won't help, because then it will have no State declaration at hand.
If somebody would like to have a broader look at the original code, State is defined here. I use CRTP for the first time, maybe I don't need it and it's only complicating the code? I don't know, I haven't found anything simpler.
How to make it work?
Classes/structs can't have members which are incomplete types, so they cannot refer to itself, since they need a definition of itself to form this class.
Since looping over std::unordered_map should give you a reference to std::pair<const Key, Value> it requires a complete type.
So, in order to do something like this, you'll have to use a wrapper class which makes use of allocations, allowing you to first complete the type and use it elsewhere.
In most cases I would use std::unordered_map<int, std::unique_ptr<S>>, though if you really require the element to be created, you can wrap it.
struct SWrapper final
{
SWrapper();
operator S&() { return *_S; }
operator const S&() const { return *_S; }
private:
std::unique_ptr<S> _S;
};
// In .cpp
SWrapper::SWrapper() : _S(std::make_unique<S>()) {}

Why does boost require &vector[0]?

Take edmonds_maximum_cardinality_matching for example. I can do:
vector<uint32_t> mate_map(n_vertices);
edmonds_maximum_cardinality_matching(g, &mate_map[0]);
But not:
vector<uint32_t> mate_map(n_vertices);
edmonds_maximum_cardinality_matching(g, mate_map);
Why is that so? A vector has all operations an array has right? NB: Here is the error message (for the second snippet) which I could not really handle:
In file included from main.C:5:0:
/usr/include/boost/graph/max_cardinality_matching.hpp: In instantiation of ‘static void boost::extra_greedy_matching<Graph, MateMap>::find_matching(const Graph&, MateMap) [with Graph = boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS>; MateMap = std::vector<unsigned int>]’:
/usr/include/boost/graph/max_cardinality_matching.hpp:842:63: required from ‘bool boost::matching(const Graph&, MateMap, VertexIndexMap) [with Graph = boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS>; MateMap = std::vector<unsigned int>; VertexIndexMap = boost::vec_adj_list_vertex_id_map<boost::no_property, long unsigned int>; AugmentingPathFinder = boost::edmonds_augmenting_path_finder; InitialMatchingFinder = boost::extra_greedy_matching; MatchingVerifier = boost::no_matching_verifier]’
/usr/include/boost/graph/max_cardinality_matching.hpp:885:19: required from ‘void boost::edmonds_maximum_cardinality_matching(const Graph&, MateMap, VertexIndexMap) [with Graph = boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS>; MateMap = std::vector<unsigned int>; VertexIndexMap = boost::vec_adj_list_vertex_id_map<boost::no_property, long unsigned int>]’
/usr/include/boost/graph/max_cardinality_matching.hpp:894:70: required from ‘void boost::edmonds_maximum_cardinality_matching(const Graph&, MateMap) [with Graph = boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS>; MateMap = std::vector<unsigned int>]’
main.C:20:51: required from here
/usr/include/boost/graph/max_cardinality_matching.hpp:617:58: error: no matching function for call to ‘put(std::vector<unsigned int>&, boost::iterators::detail::iterator_facade_base<boost::range_detail::integer_iterator<long unsigned int>, long unsigned int, boost::iterators::random_access_traversal_tag, long unsigned int, long int, false, false>::reference, boost::graph_traits<boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS> >::vertex_descriptor)’
put(mate, *vi, graph_traits<Graph>::null_vertex());
^
/usr/include/boost/graph/max_cardinality_matching.hpp:617:58: note: candidates are:
In file included from /usr/include/boost/graph/adjacency_list.hpp:36:0,
from main.C:4:
/usr/include/boost/property_map/property_map.hpp:124:15: note: template<class T, class V> void put(T*, std::ptrdiff_t, const V&)
inline void put(T* pa, std::ptrdiff_t k, const V& val) { pa[k] = val; }
^
/usr/include/boost/property_map/property_map.hpp:124:15: note: template argument deduction/substitution failed:
In file included from main.C:5:0:
/usr/include/boost/graph/max_cardinality_matching.hpp:617:58: note: mismatched types ‘T*’ and ‘std::vector<unsigned int>’
put(mate, *vi, graph_traits<Graph>::null_vertex());
^
In file included from /usr/include/boost/graph/adjacency_list.hpp:36:0,
from main.C:4:
/usr/include/boost/property_map/property_map.hpp:193:8: note: template<class K, class V> void boost::put(const boost::writable_property_map_archetype<K, V>&, const typename boost::writable_property_map_archetype<K, V>::key_type&, const typename boost::writable_property_map_archetype<K, V>::value_type&)
void put(const writable_property_map_archetype<K,V>&,
^
/usr/include/boost/property_map/property_map.hpp:193:8: note: template argument deduction/substitution failed:
In file included from main.C:5:0:
/usr/include/boost/graph/max_cardinality_matching.hpp:617:58: note: ‘std::vector<unsigned int>’ is not derived from ‘const boost::writable_property_map_archetype<K, V>’
put(mate, *vi, graph_traits<Graph>::null_vertex());
^
In file included from /usr/include/boost/graph/adjacency_list.hpp:36:0,
from main.C:4:
/usr/include/boost/property_map/property_map.hpp:307:3: note: template<class PropertyMap, class Reference, class K, class V> void boost::put(const boost::put_get_helper<Reference, PropertyMap>&, K, const V&)
put(const put_get_helper<Reference, PropertyMap>& pa, K k, const V& v)
^
/usr/include/boost/property_map/property_map.hpp:307:3: note: template argument deduction/substitution failed:
In file included from main.C:5:0:
/usr/include/boost/graph/max_cardinality_matching.hpp:617:58: note: ‘std::vector<unsigned int>’ is not derived from ‘const boost::put_get_helper<Reference, PropertyMap>’
put(mate, *vi, graph_traits<Graph>::null_vertex());
^
In file included from /usr/include/boost/graph/properties.hpp:21:0,
from /usr/include/boost/graph/adjacency_list.hpp:44,
from main.C:4:
/usr/include/boost/graph/property_maps/null_property_map.hpp:32:10: note: template<class K, class V> void boost::put(boost::null_property_map<K, V>&, const K&, const V&)
void put(null_property_map<K,V>& /*pm*/, const K& /*key*/, const V& /*value*/)
^
/usr/include/boost/graph/property_maps/null_property_map.hpp:32:10: note: template argument deduction/substitution failed:
In file included from main.C:5:0:
/usr/include/boost/graph/max_cardinality_matching.hpp:617:58: note: ‘std::vector<unsigned int>’ is not derived from ‘boost::null_property_map<K, V>’
put(mate, *vi, graph_traits<Graph>::null_vertex());
^
In file included from /usr/include/boost/graph/adjacency_list.hpp:246:0,
from main.C:4:
/usr/include/boost/graph/detail/adjacency_list.hpp:1760:5: note: template<class Config, class Base, class Property, class Key, class Value> void boost::put(Property, boost::adj_list_helper<Config, Base>&, const Key&, const Value&)
put(Property p, adj_list_helper<Config, Base>& g,
^
/usr/include/boost/graph/detail/adjacency_list.hpp:1760:5: note: template argument deduction/substitution failed:
In file included from main.C:5:0:
/usr/include/boost/graph/max_cardinality_matching.hpp:617:58: note: mismatched types ‘boost::adj_list_helper<Config, Base>’ and ‘boost::iterators::detail::iterator_facade_base<boost::range_detail::integer_iterator<long unsigned int>, long unsigned int, boost::iterators::random_access_traversal_tag, long unsigned int, long int, false, false>::reference {aka long unsigned int}’
put(mate, *vi, graph_traits<Graph>::null_vertex());
^
In file included from /usr/include/boost/graph/max_cardinality_matching.hpp:22:0,
from main.C:5:
/usr/include/boost/graph/filtered_graph.hpp:472:3: note: template<class G, class EP, class VP, class Property, class Key, class Value> void boost::put(Property, const boost::filtered_graph<Graph, EdgePredicate, VertexPredicate>&, const Key&, const Value&)
put(Property p, const filtered_graph<G, EP, VP>& g, const Key& k,
^
/usr/include/boost/graph/filtered_graph.hpp:472:3: note: template argument deduction/substitution failed:
In file included from main.C:5:0:
/usr/include/boost/graph/max_cardinality_matching.hpp:617:58: note: mismatched types ‘const boost::filtered_graph<Graph, EdgePredicate, VertexPredicate>’ and ‘boost::iterators::detail::iterator_facade_base<boost::range_detail::integer_iterator<long unsigned int>, long unsigned int, boost::iterators::random_access_traversal_tag, long unsigned int, long int, false, false>::reference {aka long unsigned int}’
put(mate, *vi, graph_traits<Graph>::null_vertex());
^
Full output here: http://pastebin.com/fG2aLRiG (Outsourced because of SO's restrictions on post length)
"A vector has all operations an array has" No. Arrays implicitly decay to pointers, vector does not. So when boost tries to pass it to the put function it generates this error:
error: no matching function for call to ‘put(std::vector<unsigned int>&, [blah blah])
note: candidates are:
template<class T, class V> void put(T*, std::ptrdiff_t, const V&)
template argument deduction/substitution failed:
mismatched types ‘T*’ and ‘std::vector<unsigned int>’
[followed by details about the other potential put functions]
Apperently the put function would accept one of these, or something that implicitly converts to one of these.
T*
boost::writable_property_map_archetype<K, V>&
const boost::put_get_helper<Reference, PropertyMap>&
boost::null_property_map<K, V>&
Property
(edmonds_maximum_cardinality_matching may have other requirements that cause these other types to also be invalid, I'm just saying these five types pass this particular hurdle.)
No, a std::vector does not have all these operations and won't convert to an array implicitly. Ignoring the fact that a std::array won't change that, for your purposes, it would be better suited.

Return an iterator inside a template

I'm trying to implement a simple template function, this code doesn't compile but I hope it will give you an idea about what I'm trying to do :
template<typename T>
typename T::iterator do_find(const T& container, const int val)
{
return std::find(container.begin(), container.end(), val);
}
I want to return the iterator that find itself returns, without knowing what type of container I receive in my template function do_find. What am I doing wrong ?
Here's the main :
int main()
{
std::vector<int> c;
c.push_back(42);
c.push_back(0);
c.push_back(1);
c.push_back(-58);
c.push_back(42);
c.push_back(777);
c.push_back(1911);
c.push_back(9);
do_find(c, 42);
return 0;
}
And the compiler error :
In file included from main.cpp:14:0:
find.hpp: In instantiation of ‘typename T::iterator do_find(const T&, int) [with T = std::vector<int>; typename T::iterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]’:
main.cpp:29:16: required from here
find.hpp:17:59: error: could not convert ‘std::find<__gnu_cxx::__normal_iterator<const int*, std::vector<int> >, int>((& container)->std::vector<_Tp, _Alloc>::begin<int, std::allocator<int> >(), (& container)->std::vector<_Tp, _Alloc>::end<int, std::allocator<int> >(), (* & val))’ from ‘__gnu_cxx::__normal_iterator<const int*, std::vector<int> >’ to ‘std::vector<int>::iterator {aka __gnu_cxx::__normal_iterator<int*, std::vector<int> >}’
find.hpp: In function ‘typename T::iterator do_find(const T&, int) [with T = std::vector<int>; typename T::iterator = __gnu_cxx::__normal_iterator<int*, std::vector<int> >]’:
find.hpp:18:1: error: control reaches end of non-void function [-Werror=return-type]
cc1plus: all warnings being treated as errors
If you have const T& container as your first parameter to this function, you have to return typename T::const_iterator instead.

Wrapping unordered_map to build an unmodifiable (immutable) map

I am playing with container types, gettingmore into the details and try to build an unmodifiable (or immutable) map.
For that, I built some kind like copy-constructor to add all elements from another unordered_map into my UnmodifiableMap (which is basically a wrapper for std::unordered_map). To make it unmodifiable, I only provide const iterators and read-only methods. But I get stuck with the constructor, I am sure I miss something, maybe somebody can point me the problem here. Maybe it is a totally wrong way like this, but his is what tried so far:
template <typename Key, typename T,
typename HashFcn = std::hash<Key>,
typename EqualKey = std::equal_to<Key>,
typename Alloc = std::allocator<std::pair<const Key, T> > >
class UnmodifiableMap {
public:
// The actual data
typedef std::unordered_map<T, Key, HashFcn, EqualKey> base;
typedef Key key_type;
typedef T data_type;
typedef T mapped_type;
typedef std::pair<const key_type, data_type> value_type;
typedef HashFcn hasher;
typedef EqualKey key_equal;
typedef Alloc allocator_type;
typedef typename base::size_type size_type;
typedef typename base::const_iterator const_iterator;
typedef typename base::iterator iterator;
private:
base _map;
/**
* Constructs an empty unordered_map
*/
UnmodifiableMap(
size_type n = 0, const hasher& hf = hasher(),
const key_equal& eql = key_equal(),
const allocator_type &alloc = allocator_type())
: _map(n, hf, eql, alloc) {}
public:
/** Constructs a copy of unordered_map */
UnmodifiableMap(const base& other)
: _map(static_cast<const base&>(other)) {}
~UnmodifiableMap() {}
iterator begin() { return _map.begin(); }
iterator end() { return _map.end(); }
const_iterator begin() const { return _map.begin(); }
const_iterator end() const { return _map.end(); }
bool empty() const { return _map.empty(); }
bool contains(const key_type& key) const {
return _map.find(key) != _map.end(); }
};
And here the main body:
int main(int argc, char **argv) {
typedef std::unordered_map<int, std::string> Items;
Items map;
map[1] = "first string";
map[4] = "string 4";
map[5] = "string 5";
map[22] = "string 22";
map[12] = "string 12";
map[18] = "string 18";
typedef UnmodifiableMap<int, std::string> ReadOnlyItems;
ReadOnlyItems readonlymap(map);
return 0;
}
The error I get is
Unmodifiable_map.cpp: In function ‘int main(int, char**)’:
Unmodifiable_map.cpp:56:25: error: no matching function for call to ‘UnmodifiableMap<int, std::basic_string<char> >::UnmodifiableMap(Items&)’
Unmodifiable_map.cpp:56:25: note: candidates are:
Unmodifiable_map.h:45:2: note: UnmodifiableMap<Key, T, HashFcn, EqualKey, Alloc>::UnmodifiableMap(const base&) [with Key = int, T = std::basic_string<char>, HashFcn = std::hash<int>, EqualKey = std::equal_to<int>, Alloc = std::allocator<std::pair<const int, std::basic_string<char> > >, UnmodifiableMap<Key, T, HashFcn, EqualKey, Alloc>::base = std::unordered_map<std::basic_string<char>, int, std::hash<int>, std::equal_to<int>, std::allocator<std::pair<const std::basic_string<char>, int> > >]
Unmodifiable_map.h:45:2: note: no known conversion for argument 1 from ‘Items {aka std::unordered_map<int, std::basic_string<char> >}’ to ‘const base& {aka const std::unordered_map<std::basic_string<char>, int, std::hash<int>, std::equal_to<int>, std::allocator<std::pair<const std::basic_string<char>, int> > >&}’
Unmodifiable_map.h:37:2: note: UnmodifiableMap<Key, T, HashFcn, EqualKey, Alloc>::UnmodifiableMap(UnmodifiableMap<Key, T, HashFcn, EqualKey, Alloc>::size_type, const hasher&, const key_equal&, const allocator_type&) [with Key = int, T = std::basic_string<char>, HashFcn = std::hash<int>, EqualKey = std::equal_to<int>, Alloc = std::allocator<std::pair<const int, std::basic_string<char> > >, UnmodifiableMap<Key, T, HashFcn, EqualKey, Alloc>::size_type = long unsigned int, UnmodifiableMap<Key, T, HashFcn, EqualKey, Alloc>::hasher = std::hash<int>, UnmodifiableMap<Key, T, HashFcn, EqualKey, Alloc>::key_equal = std::equal_to<int>, UnmodifiableMap<Key, T, HashFcn, EqualKey, Alloc>::allocator_type = std::allocator<std::pair<const int, std::basic_string<char> > >]
Unmodifiable_map.h:37:2: note: no known conversion for argument 1 from ‘Items {aka std::unordered_map<int, std::basic_string<char> >}’ to ‘long unsigned int’
Unmodifiable_map.h:14:7: note: UnmodifiableMap<int, std::basic_string<char> >::UnmodifiableMap(const UnmodifiableMap<int, std::basic_string<char> >&)
Unmodifiable_map.h:14:7: note: no known conversion for argument 1 from ‘Items {aka std::unordered_map<int, std::basic_string<char> >}’ to ‘const UnmodifiableMap<int, std::basic_string<char> >&’
Unmodifiable_map.h:14:7: note: UnmodifiableMap<int, std::basic_string<char> >::UnmodifiableMap(UnmodifiableMap<int, std::basic_string<char> >&&)
Unmodifiable_map.h:14:7: note: no known conversion for argument 1 from ‘Items {aka std::unordered_map<int, std::basic_string<char> >}’ to ‘UnmodifiableMap<int, std::basic_string<char> >&&’
Hope somebody can shed some light on that. Also I think I need to do more in the copy constructor, propably copy the elements (like a swap function) and an implementaton of operator= ?!
You are swapping the role of T and Key in your base type definition inside your wrapper class:
template <typename Key, typename T,
typename HashFcn = std::hash<Key>,
typename EqualKey = std::equal_to<Key>,
typename Alloc = std::allocator<std::pair<const Key, T> > >
class UnmodifiableMap {
public:
// typedef std::unordered_map<T, Key, HashFcn, EqualKey> base; // ERROR
typedef std::unordered_map<Key, T, HashFcn, EqualKey> base; // OK
...
Hence, your underlying map ends up being an unordered_map<string, int> rather than an unordered_map<int, string>, and the compiler complains about the mismatch.
Also notice, that you have an unnecessary static_cast in your constructor:
UnmodifiableMap(const base& other)
// : _map(static_cast<const base&>(other)) {} // NOT NEEDED. Just do:
: _map(other) {}