souffle  2.0.2-371-g6315b36
Public Member Functions | Private Member Functions | Private Attributes | Friends
souffle::Graph< Vertex, Compare > Class Template Reference

A simple graph structure for graph-based operations. More...

#include <GraphUtils.h>

Collaboration diagram for souffle::Graph< Vertex, Compare >:
Collaboration graph

Public Member Functions

const std::set< Vertex, Compare > clique (const Vertex &vertex) const
 Obtains the set of all vertices in the same clique than the given vertex. More...
 
bool contains (const Vertex &from, const Vertex &to) const
 Determines whether the given edge is present. More...
 
bool contains (const Vertex &vertex) const
 Determines whether the given vertex is present. More...
 
void insert (const Vertex &from, const Vertex &to)
 Adds a new edge from the given vertex to the target vertex. More...
 
void insert (const Vertex &vertex)
 Adds a vertex. More...
 
const std::set< Vertex, Compare > & predecessors (const Vertex &to) const
 Returns the set of vertices the given vertex has edges from. More...
 
void print (std::ostream &out) const
 Enables graphs to be printed (e.g. More...
 
bool reaches (const Vertex &from, const Vertex &to) const
 Determines whether there is a directed path between the two vertices. More...
 
const std::set< Vertex, Compare > & successors (const Vertex &from) const
 Returns the set of vertices the given vertex has edges to. More...
 
const std::set< Vertex, Compare > & vertices () const
 Obtains a reference to the set of all vertices. More...
 
template<typename Lambda >
void visitDepthFirst (const Vertex &vertex, const Lambda &lambda) const
 A generic utility for depth-first visits. More...
 

Private Member Functions

template<typename Lambda >
void visitDepthFirst (const Vertex &vertex, const Lambda &lambda, std::set< Vertex, Compare > &visited) const
 The internal implementation of depth-first visits. More...
 

Private Attributes

std::map< Vertex, std::set< Vertex, Compare > > _predecessors
 
std::map< Vertex, std::set< Vertex, Compare > > _successors
 
std::set< Vertex, Compare > _vertices
 

Friends

std::ostream & operator<< (std::ostream &out, const Graph &g)
 

Detailed Description

template<typename Vertex, typename Compare = std::less<Vertex>>
class souffle::Graph< Vertex, Compare >

A simple graph structure for graph-based operations.

Definition at line 40 of file GraphUtils.h.

Member Function Documentation

◆ clique()

template<typename Vertex , typename Compare = std::less<Vertex>>
const std::set<Vertex, Compare> souffle::Graph< Vertex, Compare >::clique ( const Vertex &  vertex) const
inline

Obtains the set of all vertices in the same clique than the given vertex.

Definition at line 117 of file GraphUtils.h.

118  {
119  std::set<Vertex, Compare> visited;
120  visitDepthFirst(vertex, lambda, visited);
121  }
122 
123  /** Enables graphs to be printed (e.g. for debugging) */
124  void print(std::ostream& out) const {
125  bool first = true;
126  out << "{";

◆ contains() [1/2]

template<typename Vertex , typename Compare = std::less<Vertex>>
bool souffle::Graph< Vertex, Compare >::contains ( const Vertex &  from,
const Vertex &  to 
) const
inline

Determines whether the given edge is present.

Definition at line 90 of file GraphUtils.h.

90  {
91  return false;
92  }
93 
94  // conduct a depth-first search starting at from
95  bool found = false;
96  bool first = true;
97  visitDepthFirst(from, [&](const Vertex& cur) {

◆ contains() [2/2]

template<typename Vertex , typename Compare = std::less<Vertex>>
bool souffle::Graph< Vertex, Compare >::contains ( const Vertex &  vertex) const
inline

Determines whether the given vertex is present.

Definition at line 85 of file GraphUtils.h.

88  {

◆ insert() [1/2]

template<typename Vertex , typename Compare = std::less<Vertex>>
void souffle::Graph< Vertex, Compare >::insert ( const Vertex &  from,
const Vertex &  to 
)
inline

Adds a new edge from the given vertex to the target vertex.

Definition at line 51 of file GraphUtils.h.

51  {
52  _successors.insert(std::make_pair(vertex, std::set<Vertex, Compare>()));
53  _predecessors.insert(std::make_pair(vertex, std::set<Vertex, Compare>()));
54  }
55  }
56 

◆ insert() [2/2]

template<typename Vertex , typename Compare = std::less<Vertex>>
void souffle::Graph< Vertex, Compare >::insert ( const Vertex &  vertex)
inline

Adds a vertex.

Definition at line 61 of file GraphUtils.h.

63  {
64  return _successors.at(from);
65  }
66 
67  /** Returns the set of vertices the given vertex has edges from */

◆ predecessors()

template<typename Vertex , typename Compare = std::less<Vertex>>
const std::set<Vertex, Compare>& souffle::Graph< Vertex, Compare >::predecessors ( const Vertex &  to) const
inline

Returns the set of vertices the given vertex has edges from.

Definition at line 80 of file GraphUtils.h.

80  {
81  return false;
82  }

◆ print()

template<typename Vertex , typename Compare = std::less<Vertex>>
void souffle::Graph< Vertex, Compare >::print ( std::ostream &  out) const
inline

Enables graphs to be printed (e.g.

for debugging)

Definition at line 136 of file GraphUtils.h.

139  {
140  g.print(out);
141  return out;
142  }
143 
144 private:
145  // not a very efficient but simple graph representation
146  std::set<Vertex, Compare> _vertices; // all the vertices in the graph
147  std::map<Vertex, std::set<Vertex, Compare>> _successors; // all edges forward directed
148  std::map<Vertex, std::set<Vertex, Compare>> _predecessors; // all edges backward
149 

◆ reaches()

template<typename Vertex , typename Compare = std::less<Vertex>>
bool souffle::Graph< Vertex, Compare >::reaches ( const Vertex &  from,
const Vertex &  to 
) const
inline

Determines whether there is a directed path between the two vertices.

Definition at line 100 of file GraphUtils.h.

105  {
106  std::set<Vertex, Compare> res;
107  res.insert(vertex);
108  for (const auto& cur : vertices()) {
109  if (reaches(vertex, cur) && reaches(cur, vertex)) {
110  res.insert(cur);
111  }
112  }
113  return res;
114  }

◆ successors()

template<typename Vertex , typename Compare = std::less<Vertex>>
const std::set<Vertex, Compare>& souffle::Graph< Vertex, Compare >::successors ( const Vertex &  from) const
inline

Returns the set of vertices the given vertex has edges to.

Definition at line 75 of file GraphUtils.h.

78  {

◆ vertices()

template<typename Vertex , typename Compare = std::less<Vertex>>
const std::set<Vertex, Compare>& souffle::Graph< Vertex, Compare >::vertices ( ) const
inline

Obtains a reference to the set of all vertices.

Definition at line 70 of file GraphUtils.h.

73  {

◆ visitDepthFirst() [1/2]

template<typename Vertex , typename Compare = std::less<Vertex>>
template<typename Lambda >
void souffle::Graph< Vertex, Compare >::visitDepthFirst ( const Vertex &  vertex,
const Lambda &  lambda 
) const
inline

A generic utility for depth-first visits.

Definition at line 130 of file GraphUtils.h.

Referenced by souffle::Graph< const souffle::ast::Relation *, souffle::ast::NameComparison >::clique().

◆ visitDepthFirst() [2/2]

template<typename Vertex , typename Compare = std::less<Vertex>>
template<typename Lambda >
void souffle::Graph< Vertex, Compare >::visitDepthFirst ( const Vertex &  vertex,
const Lambda &  lambda,
std::set< Vertex, Compare > &  visited 
) const
inlineprivate

The internal implementation of depth-first visits.

Definition at line 164 of file GraphUtils.h.

167  {
168  static const std::vector<char> table = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
169  'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
170  'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
171  'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
172  std::string result;
173  std::string tmp = data;
174  unsigned int padding = 0;
175  if (data.size() % 3 == 2) {
176  padding = 1;

Friends And Related Function Documentation

◆ operator<<

template<typename Vertex , typename Compare = std::less<Vertex>>
std::ostream& operator<< ( std::ostream &  out,
const Graph< Vertex, Compare > &  g 
)
friend

Definition at line 151 of file GraphUtils.h.

153  {
154  lambda(vertex);

Field Documentation

◆ _predecessors

template<typename Vertex , typename Compare = std::less<Vertex>>
std::map<Vertex, std::set<Vertex, Compare> > souffle::Graph< Vertex, Compare >::_predecessors
private

◆ _successors

template<typename Vertex , typename Compare = std::less<Vertex>>
std::map<Vertex, std::set<Vertex, Compare> > souffle::Graph< Vertex, Compare >::_successors
private

◆ _vertices

template<typename Vertex , typename Compare = std::less<Vertex>>
std::set<Vertex, Compare> souffle::Graph< Vertex, Compare >::_vertices
private

Definition at line 158 of file GraphUtils.h.


The documentation for this class was generated from the following file:
souffle::Graph::visitDepthFirst
void visitDepthFirst(const Vertex &vertex, const Lambda &lambda) const
A generic utility for depth-first visits.
Definition: GraphUtils.h:130
souffle::Graph::print
void print(std::ostream &out) const
Enables graphs to be printed (e.g.
Definition: GraphUtils.h:136
souffle::Graph::_successors
std::map< Vertex, std::set< Vertex, Compare > > _successors
Definition: GraphUtils.h:159
souffle::Graph::_predecessors
std::map< Vertex, std::set< Vertex, Compare > > _predecessors
Definition: GraphUtils.h:160
souffle::Graph::reaches
bool reaches(const Vertex &from, const Vertex &to) const
Determines whether there is a directed path between the two vertices.
Definition: GraphUtils.h:100
TCB_SPAN_NAMESPACE_NAME::detail::data
constexpr auto data(C &c) -> decltype(c.data())
Definition: span.h:210
souffle::Graph::_vertices
std::set< Vertex, Compare > _vertices
Definition: GraphUtils.h:158
souffle::Graph::vertices
const std::set< Vertex, Compare > & vertices() const
Obtains a reference to the set of all vertices.
Definition: GraphUtils.h:70