<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.11"/>
<title>proxygen: proxygen/folly/folly/experimental/observer/detail/GraphCycleDetector.h Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">proxygen
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.11 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li><a href="examples.html"><span>Examples</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>File&#160;Members</span></a></li>
    </ul>
  </div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('GraphCycleDetector_8h_source.html','');});
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">GraphCycleDetector.h</div>  </div>
</div><!--header-->
<div class="contents">
<a href="GraphCycleDetector_8h.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;<span class="comment">/*</span></div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;<span class="comment"> * Copyright 2016-present Facebook, Inc.</span></div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;<span class="comment"> *</span></div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;<span class="comment"> * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span></div><div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;<span class="comment"> * you may not use this file except in compliance with the License.</span></div><div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;<span class="comment"> * You may obtain a copy of the License at</span></div><div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;<span class="comment"> *</span></div><div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;<span class="comment"> *   http://www.apache.org/licenses/LICENSE-2.0</span></div><div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;<span class="comment"> *</span></div><div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;<span class="comment"> * Unless required by applicable law or agreed to in writing, software</span></div><div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;<span class="comment"> * distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span></div><div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;<span class="comment"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span></div><div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;<span class="comment"> * See the License for the specific language governing permissions and</span></div><div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;<span class="comment"> * limitations under the License.</span></div><div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;<span class="comment"> */</span></div><div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;<span class="preprocessor">#pragma once</span></div><div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;</div><div class="line"><a name="l00018"></a><span class="lineno">   18</span>&#160;<span class="preprocessor">#include &lt;unordered_map&gt;</span></div><div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;<span class="preprocessor">#include &lt;unordered_set&gt;</span></div><div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;</div><div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;<span class="preprocessor">#include &lt;glog/logging.h&gt;</span></div><div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;</div><div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;<span class="keyword">namespace </span><a class="code" href="namespacefolly.html">folly</a> {</div><div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;<span class="keyword">namespace </span>observer_detail {</div><div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;</div><div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;<span class="keyword">template</span> &lt;<span class="keyword">typename</span> NodeId&gt;</div><div class="line"><a name="l00027"></a><span class="lineno"><a class="line" href="classfolly_1_1observer__detail_1_1GraphCycleDetector.html">   27</a></span>&#160;<span class="keyword">class </span><a class="code" href="classfolly_1_1observer__detail_1_1GraphCycleDetector.html">GraphCycleDetector</a> {</div><div class="line"><a name="l00028"></a><span class="lineno"><a class="line" href="classfolly_1_1observer__detail_1_1GraphCycleDetector.html#ac554e6e938b2cd0c2991fd51f3e519d6">   28</a></span>&#160;  <span class="keyword">using</span> <a class="code" href="classfolly_1_1observer__detail_1_1GraphCycleDetector.html#ac554e6e938b2cd0c2991fd51f3e519d6">NodeSet</a> = std::unordered_set&lt;NodeId&gt;;</div><div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;</div><div class="line"><a name="l00030"></a><span class="lineno">   30</span>&#160; <span class="keyword">public</span>:</div><div class="line"><a name="l00035"></a><span class="lineno"><a class="line" href="classfolly_1_1observer__detail_1_1GraphCycleDetector.html#affdc3d3089bb7d3041be6de694e8e816">   35</a></span>&#160;  <span class="keywordtype">bool</span> <a class="code" href="classfolly_1_1observer__detail_1_1GraphCycleDetector.html#affdc3d3089bb7d3041be6de694e8e816">addEdge</a>(NodeId <a class="code" href="namespacefolly_1_1pushmi_1_1operators.html#a66a275bf0c134161fd2050e9ec72a29d">from</a>, NodeId <a class="code" href="namespacefolly.html#a005f91db4ecc55675cf5ad7ba25ec24d">to</a>) {</div><div class="line"><a name="l00036"></a><span class="lineno">   36</span>&#160;    <span class="comment">// In general case DFS may be expensive here, but in most cases to-node will</span></div><div class="line"><a name="l00037"></a><span class="lineno">   37</span>&#160;    <span class="comment">// have no edges, so it should be O(1).</span></div><div class="line"><a name="l00038"></a><span class="lineno">   38</span>&#160;    <a class="code" href="classfolly_1_1observer__detail_1_1GraphCycleDetector.html#ac554e6e938b2cd0c2991fd51f3e519d6">NodeSet</a> visitedNodes;</div><div class="line"><a name="l00039"></a><span class="lineno">   39</span>&#160;    <a class="code" href="classfolly_1_1observer__detail_1_1GraphCycleDetector.html#a8810ee05d97141c3133942f715b002c7">dfs</a>(visitedNodes, to);</div><div class="line"><a name="l00040"></a><span class="lineno">   40</span>&#160;    <span class="keywordflow">if</span> (visitedNodes.count(from)) {</div><div class="line"><a name="l00041"></a><span class="lineno">   41</span>&#160;      <span class="keywordflow">return</span> <span class="keyword">false</span>;</div><div class="line"><a name="l00042"></a><span class="lineno">   42</span>&#160;    }</div><div class="line"><a name="l00043"></a><span class="lineno">   43</span>&#160;</div><div class="line"><a name="l00044"></a><span class="lineno">   44</span>&#160;    <span class="keyword">auto</span>&amp; nodes = <a class="code" href="classfolly_1_1observer__detail_1_1GraphCycleDetector.html#a5d92327659c25deee7a83cb2ff2d5394">edges_</a>[<a class="code" href="namespacefolly_1_1pushmi_1_1operators.html#a66a275bf0c134161fd2050e9ec72a29d">from</a>];</div><div class="line"><a name="l00045"></a><span class="lineno">   45</span>&#160;    DCHECK_EQ(nodes.count(to), 0u);</div><div class="line"><a name="l00046"></a><span class="lineno">   46</span>&#160;    nodes.insert(to);</div><div class="line"><a name="l00047"></a><span class="lineno">   47</span>&#160;</div><div class="line"><a name="l00048"></a><span class="lineno">   48</span>&#160;    <span class="keywordflow">return</span> <span class="keyword">true</span>;</div><div class="line"><a name="l00049"></a><span class="lineno">   49</span>&#160;  }</div><div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;</div><div class="line"><a name="l00051"></a><span class="lineno"><a class="line" href="classfolly_1_1observer__detail_1_1GraphCycleDetector.html#af22b4c49188c01cd96b18fa62650b4fb">   51</a></span>&#160;  <span class="keywordtype">void</span> <a class="code" href="classfolly_1_1observer__detail_1_1GraphCycleDetector.html#af22b4c49188c01cd96b18fa62650b4fb">removeEdge</a>(NodeId <a class="code" href="namespacefolly_1_1pushmi_1_1operators.html#a66a275bf0c134161fd2050e9ec72a29d">from</a>, NodeId <a class="code" href="namespacefolly.html#a005f91db4ecc55675cf5ad7ba25ec24d">to</a>) {</div><div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;    <span class="keyword">auto</span>&amp; nodes = <a class="code" href="classfolly_1_1observer__detail_1_1GraphCycleDetector.html#a5d92327659c25deee7a83cb2ff2d5394">edges_</a>[<a class="code" href="namespacefolly_1_1pushmi_1_1operators.html#a66a275bf0c134161fd2050e9ec72a29d">from</a>];</div><div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;    DCHECK(nodes.count(to));</div><div class="line"><a name="l00054"></a><span class="lineno">   54</span>&#160;    nodes.erase(to);</div><div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;    <span class="keywordflow">if</span> (nodes.empty()) {</div><div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;      <a class="code" href="classfolly_1_1observer__detail_1_1GraphCycleDetector.html#a5d92327659c25deee7a83cb2ff2d5394">edges_</a>.erase(from);</div><div class="line"><a name="l00057"></a><span class="lineno">   57</span>&#160;    }</div><div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;  }</div><div class="line"><a name="l00059"></a><span class="lineno">   59</span>&#160;</div><div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160; <span class="keyword">private</span>:</div><div class="line"><a name="l00061"></a><span class="lineno"><a class="line" href="classfolly_1_1observer__detail_1_1GraphCycleDetector.html#a8810ee05d97141c3133942f715b002c7">   61</a></span>&#160;  <span class="keywordtype">void</span> <a class="code" href="classfolly_1_1observer__detail_1_1GraphCycleDetector.html#a8810ee05d97141c3133942f715b002c7">dfs</a>(<a class="code" href="classfolly_1_1observer__detail_1_1GraphCycleDetector.html#ac554e6e938b2cd0c2991fd51f3e519d6">NodeSet</a>&amp; visitedNodes, NodeId node) {</div><div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160;    <span class="comment">// We don&#39;t terminate early if cycle is detected, because this is considered</span></div><div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;    <span class="comment">// an error condition, so not worth optimizing for.</span></div><div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;    <span class="keywordflow">if</span> (visitedNodes.count(node)) {</div><div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;      <span class="keywordflow">return</span>;</div><div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;    }</div><div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160;</div><div class="line"><a name="l00068"></a><span class="lineno">   68</span>&#160;    visitedNodes.insert(node);</div><div class="line"><a name="l00069"></a><span class="lineno">   69</span>&#160;</div><div class="line"><a name="l00070"></a><span class="lineno">   70</span>&#160;    <span class="keywordflow">if</span> (!<a class="code" href="classfolly_1_1observer__detail_1_1GraphCycleDetector.html#a5d92327659c25deee7a83cb2ff2d5394">edges_</a>.count(node)) {</div><div class="line"><a name="l00071"></a><span class="lineno">   71</span>&#160;      <span class="keywordflow">return</span>;</div><div class="line"><a name="l00072"></a><span class="lineno">   72</span>&#160;    }</div><div class="line"><a name="l00073"></a><span class="lineno">   73</span>&#160;</div><div class="line"><a name="l00074"></a><span class="lineno">   74</span>&#160;    <span class="keywordflow">for</span> (<span class="keyword">const</span> <span class="keyword">auto</span>&amp; <a class="code" href="namespacefolly.html#a005f91db4ecc55675cf5ad7ba25ec24d">to</a> : <a class="code" href="classfolly_1_1observer__detail_1_1GraphCycleDetector.html#a5d92327659c25deee7a83cb2ff2d5394">edges_</a>[node]) {</div><div class="line"><a name="l00075"></a><span class="lineno">   75</span>&#160;      <a class="code" href="classfolly_1_1observer__detail_1_1GraphCycleDetector.html#a8810ee05d97141c3133942f715b002c7">dfs</a>(visitedNodes, <a class="code" href="namespacefolly.html#a005f91db4ecc55675cf5ad7ba25ec24d">to</a>);</div><div class="line"><a name="l00076"></a><span class="lineno">   76</span>&#160;    }</div><div class="line"><a name="l00077"></a><span class="lineno">   77</span>&#160;  }</div><div class="line"><a name="l00078"></a><span class="lineno">   78</span>&#160;</div><div class="line"><a name="l00079"></a><span class="lineno"><a class="line" href="classfolly_1_1observer__detail_1_1GraphCycleDetector.html#a5d92327659c25deee7a83cb2ff2d5394">   79</a></span>&#160;  std::unordered_map&lt;NodeId, NodeSet&gt; <a class="code" href="classfolly_1_1observer__detail_1_1GraphCycleDetector.html#a5d92327659c25deee7a83cb2ff2d5394">edges_</a>;</div><div class="line"><a name="l00080"></a><span class="lineno">   80</span>&#160;};</div><div class="line"><a name="l00081"></a><span class="lineno">   81</span>&#160;} <span class="comment">// namespace observer_detail</span></div><div class="line"><a name="l00082"></a><span class="lineno">   82</span>&#160;} <span class="comment">// namespace folly</span></div><div class="ttc" id="classfolly_1_1observer__detail_1_1GraphCycleDetector_html_a8810ee05d97141c3133942f715b002c7"><div class="ttname"><a href="classfolly_1_1observer__detail_1_1GraphCycleDetector.html#a8810ee05d97141c3133942f715b002c7">folly::observer_detail::GraphCycleDetector::dfs</a></div><div class="ttdeci">void dfs(NodeSet &amp;visitedNodes, NodeId node)</div><div class="ttdef"><b>Definition:</b> <a href="GraphCycleDetector_8h_source.html#l00061">GraphCycleDetector.h:61</a></div></div>
<div class="ttc" id="namespacefolly_html"><div class="ttname"><a href="namespacefolly.html">folly</a></div><div class="ttdoc">—— Concurrent Priority Queue Implementation —— </div><div class="ttdef"><b>Definition:</b> <a href="AtomicBitSet_8h_source.html#l00029">AtomicBitSet.h:29</a></div></div>
<div class="ttc" id="classfolly_1_1observer__detail_1_1GraphCycleDetector_html_a5d92327659c25deee7a83cb2ff2d5394"><div class="ttname"><a href="classfolly_1_1observer__detail_1_1GraphCycleDetector.html#a5d92327659c25deee7a83cb2ff2d5394">folly::observer_detail::GraphCycleDetector::edges_</a></div><div class="ttdeci">std::unordered_map&lt; NodeId, NodeSet &gt; edges_</div><div class="ttdef"><b>Definition:</b> <a href="GraphCycleDetector_8h_source.html#l00079">GraphCycleDetector.h:79</a></div></div>
<div class="ttc" id="classfolly_1_1observer__detail_1_1GraphCycleDetector_html_af22b4c49188c01cd96b18fa62650b4fb"><div class="ttname"><a href="classfolly_1_1observer__detail_1_1GraphCycleDetector.html#af22b4c49188c01cd96b18fa62650b4fb">folly::observer_detail::GraphCycleDetector::removeEdge</a></div><div class="ttdeci">void removeEdge(NodeId from, NodeId to)</div><div class="ttdef"><b>Definition:</b> <a href="GraphCycleDetector_8h_source.html#l00051">GraphCycleDetector.h:51</a></div></div>
<div class="ttc" id="namespacefolly_html_a005f91db4ecc55675cf5ad7ba25ec24d"><div class="ttname"><a href="namespacefolly.html#a005f91db4ecc55675cf5ad7ba25ec24d">folly::to</a></div><div class="ttdeci">std::enable_if&lt; detail::is_chrono_conversion&lt; Tgt, Src &gt;::value, Tgt &gt;::type to(const Src &amp;value)</div><div class="ttdef"><b>Definition:</b> <a href="chrono_2Conv_8h_source.html#l00677">Conv.h:677</a></div></div>
<div class="ttc" id="classfolly_1_1observer__detail_1_1GraphCycleDetector_html_ac554e6e938b2cd0c2991fd51f3e519d6"><div class="ttname"><a href="classfolly_1_1observer__detail_1_1GraphCycleDetector.html#ac554e6e938b2cd0c2991fd51f3e519d6">folly::observer_detail::GraphCycleDetector::NodeSet</a></div><div class="ttdeci">std::unordered_set&lt; NodeId &gt; NodeSet</div><div class="ttdef"><b>Definition:</b> <a href="GraphCycleDetector_8h_source.html#l00028">GraphCycleDetector.h:28</a></div></div>
<div class="ttc" id="namespacefolly_1_1pushmi_1_1operators_html_a66a275bf0c134161fd2050e9ec72a29d"><div class="ttname"><a href="namespacefolly_1_1pushmi_1_1operators.html#a66a275bf0c134161fd2050e9ec72a29d">folly::pushmi::operators::from</a></div><div class="ttdeci">PUSHMI_INLINE_VAR constexpr struct folly::pushmi::operators::from_fn from</div></div>
<div class="ttc" id="classfolly_1_1observer__detail_1_1GraphCycleDetector_html_affdc3d3089bb7d3041be6de694e8e816"><div class="ttname"><a href="classfolly_1_1observer__detail_1_1GraphCycleDetector.html#affdc3d3089bb7d3041be6de694e8e816">folly::observer_detail::GraphCycleDetector::addEdge</a></div><div class="ttdeci">bool addEdge(NodeId from, NodeId to)</div><div class="ttdef"><b>Definition:</b> <a href="GraphCycleDetector_8h_source.html#l00035">GraphCycleDetector.h:35</a></div></div>
<div class="ttc" id="classfolly_1_1observer__detail_1_1GraphCycleDetector_html"><div class="ttname"><a href="classfolly_1_1observer__detail_1_1GraphCycleDetector.html">folly::observer_detail::GraphCycleDetector</a></div><div class="ttdef"><b>Definition:</b> <a href="GraphCycleDetector_8h_source.html#l00027">GraphCycleDetector.h:27</a></div></div>
</div><!-- fragment --></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="dir_d333e33cf903653326082c1f32e03be5.html">proxygen</a></li><li class="navelem"><a class="el" href="dir_32ce190710b1753ff87b781fbaae9b47.html">folly</a></li><li class="navelem"><a class="el" href="dir_dcf8e1233ba68571c5027662569cd1d9.html">folly</a></li><li class="navelem"><a class="el" href="dir_81eb1dbd071cdd475d3d544204dd2893.html">experimental</a></li><li class="navelem"><a class="el" href="dir_9f608d4e9f40a5d7e0e8f78765c9d343.html">observer</a></li><li class="navelem"><a class="el" href="dir_6d540cd3c714170b21510d55128098d9.html">detail</a></li><li class="navelem"><a class="el" href="GraphCycleDetector_8h.html">GraphCycleDetector.h</a></li>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.11 </li>
  </ul>
</div>
</body>
</html>