Skip to content

Instantly share code, notes, and snippets.

@tatewake
Created June 21, 2017 23:54
Show Gist options
  • Save tatewake/549aab27970557a04e25d8f03ec9c8e9 to your computer and use it in GitHub Desktop.
Save tatewake/549aab27970557a04e25d8f03ec9c8e9 to your computer and use it in GitHub Desktop.
tGraph: introspection of edges based on a single vertex
//-- Helper method
template<typename V, typename E>
template<typename T> bool tGraph<V, E>::Equal(T a, T b)
{
return !(a < b) && !(b < a);
}
//-- Introspection of edges based on a single vertex
template<typename V, typename E> typename tGraph<V, E>::EdgeSet tGraph<V, E>::outgoingEdgesOf(const VertexType& v) const
{
EdgeSet result;
for(typename EdgeSet::iterator iter = mEdges.begin(); iter != mEdges.end(); iter++)
{
if (Equal((*iter).second.first, v))
{
result.insert(*iter);
}
}
return result;
}
template<typename V, typename E> typename tGraph<V, E>::EdgeSet tGraph<V, E>::incomingEdgesOf(const VertexType& v) const
{
EdgeSet result;
for(typename EdgeSet::iterator iter = mEdges.begin(); iter != mEdges.end(); iter++)
{
if (Equal((*iter).second.second, v))
{
result.insert(*iter);
}
}
return result;
}
template<typename V, typename E> typename tGraph<V, E>::EdgeSet tGraph<V, E>::edgesOf(const VertexType& v) const
{
EdgeSet incoming, result;
typename EdgeSet::iterator iter, next;
result = outgoingEdgesOf(v);
incoming = incomingEdgesOf(v);
//Remove loop edges from "incoming", we already got them once in outgoing.
iter = incoming.begin();
while(iter != incoming.end())
{
next = iter;
next++;
if (Equal((*iter).second.first, (*iter).second.second))
{
incoming.erase(iter);
}
iter = next;
}
result.insert(incoming.begin(), incoming.end());
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment