proxygen
folly::observer_detail::GraphCycleDetector< NodeId > Class Template Reference

#include <GraphCycleDetector.h>

Public Member Functions

bool addEdge (NodeId from, NodeId to)
 
void removeEdge (NodeId from, NodeId to)
 

Private Types

using NodeSet = std::unordered_set< NodeId >
 

Private Member Functions

void dfs (NodeSet &visitedNodes, NodeId node)
 

Private Attributes

std::unordered_map< NodeId, NodeSetedges_
 

Detailed Description

template<typename NodeId>
class folly::observer_detail::GraphCycleDetector< NodeId >

Definition at line 27 of file GraphCycleDetector.h.

Member Typedef Documentation

template<typename NodeId >
using folly::observer_detail::GraphCycleDetector< NodeId >::NodeSet = std::unordered_set<NodeId>
private

Definition at line 28 of file GraphCycleDetector.h.

Member Function Documentation

template<typename NodeId >
bool folly::observer_detail::GraphCycleDetector< NodeId >::addEdge ( NodeId  from,
NodeId  to 
)
inline

Add new edge. If edge creates a cycle then it's not added and false is returned.

Definition at line 35 of file GraphCycleDetector.h.

References folly::observer_detail::GraphCycleDetector< NodeId >::dfs(), folly::observer_detail::GraphCycleDetector< NodeId >::edges_, and folly::pushmi::operators::from.

Referenced by folly::observer_detail::ObserverManager::DependencyRecorder::markRefreshDependency().

35  {
36  // In general case DFS may be expensive here, but in most cases to-node will
37  // have no edges, so it should be O(1).
38  NodeSet visitedNodes;
39  dfs(visitedNodes, to);
40  if (visitedNodes.count(from)) {
41  return false;
42  }
43 
44  auto& nodes = edges_[from];
45  DCHECK_EQ(nodes.count(to), 0u);
46  nodes.insert(to);
47 
48  return true;
49  }
void dfs(NodeSet &visitedNodes, NodeId node)
std::unordered_map< NodeId, NodeSet > edges_
std::enable_if< detail::is_chrono_conversion< Tgt, Src >::value, Tgt >::type to(const Src &value)
Definition: Conv.h:677
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::operators::from_fn from
template<typename NodeId >
void folly::observer_detail::GraphCycleDetector< NodeId >::dfs ( NodeSet visitedNodes,
NodeId  node 
)
inlineprivate

Definition at line 61 of file GraphCycleDetector.h.

References folly::observer_detail::GraphCycleDetector< NodeId >::edges_, and folly::to().

Referenced by folly::observer_detail::GraphCycleDetector< NodeId >::addEdge().

61  {
62  // We don't terminate early if cycle is detected, because this is considered
63  // an error condition, so not worth optimizing for.
64  if (visitedNodes.count(node)) {
65  return;
66  }
67 
68  visitedNodes.insert(node);
69 
70  if (!edges_.count(node)) {
71  return;
72  }
73 
74  for (const auto& to : edges_[node]) {
75  dfs(visitedNodes, to);
76  }
77  }
void dfs(NodeSet &visitedNodes, NodeId node)
std::unordered_map< NodeId, NodeSet > edges_
std::enable_if< detail::is_chrono_conversion< Tgt, Src >::value, Tgt >::type to(const Src &value)
Definition: Conv.h:677
template<typename NodeId >
void folly::observer_detail::GraphCycleDetector< NodeId >::removeEdge ( NodeId  from,
NodeId  to 
)
inline

Definition at line 51 of file GraphCycleDetector.h.

References folly::observer_detail::GraphCycleDetector< NodeId >::edges_, and folly::pushmi::operators::from.

Referenced by folly::observer_detail::ObserverManager::DependencyRecorder::unmarkRefreshDependency().

51  {
52  auto& nodes = edges_[from];
53  DCHECK(nodes.count(to));
54  nodes.erase(to);
55  if (nodes.empty()) {
56  edges_.erase(from);
57  }
58  }
std::unordered_map< NodeId, NodeSet > edges_
std::enable_if< detail::is_chrono_conversion< Tgt, Src >::value, Tgt >::type to(const Src &value)
Definition: Conv.h:677
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::operators::from_fn from

Member Data Documentation


The documentation for this class was generated from the following file: