I understand that in general programming, algorithms are disconnected from containers. Therefore, it would be pointless to implement the general algorithm as an instance method (the same algorithm should work on several specific classes, we do not want all of them to inherit from one ABC, since it exponentially increased the number of classes).
But in the case of a source()function in the Boost Graph Library , I donβt understand why it is a global function, and not an instance method of a graph class.
As far as I can judge by reading the source code of BGL , I source(e, g)must know the implementation details of the graph and edge objects passed to it; this is not enough to know only their interfaces.
So, is source()not a general algorithm. In other words, he needs to know the specific class of the graph instance. Then why not put it in the same class as the instance method? Wouldn't that be much cleaner / less messy than creating a global function that needs to be tuned for every class he called for?
UPDATE
Relevant source code:
template <class Directed, class Vertex,
class OutEdgeListS,
class VertexListS,
class DirectedS,
class VertexProperty,
class EdgeProperty,
class GraphProperty, class EdgeListS>
inline Vertex
source(const detail::edge_base<Directed,Vertex>& e,
const adjacency_list<OutEdgeListS, VertexListS, DirectedS,
VertexProperty, EdgeProperty, GraphProperty, EdgeListS>&)
{
return e.m_source;
}
namespace boost {
namespace detail {
template <typename Directed, typename Vertex>
struct edge_base
{
inline edge_base() {}
inline edge_base(Vertex s, Vertex d)
: m_source(s), m_target(d) { }
Vertex m_source;
Vertex m_target;
};
}
}