rippled
GraphAlgorithms.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_PEERFINDER_SIM_GRAPHALGORITHMS_H_INCLUDED
21 #define RIPPLE_PEERFINDER_SIM_GRAPHALGORITHMS_H_INCLUDED
22 
23 namespace ripple {
24 namespace PeerFinder {
25 namespace Sim {
26 
27 template <typename Vertex>
28 struct VertexTraits;
29 
35 template <typename Vertex, typename Function>
36 void
37 breadth_first_traverse(Vertex& start, Function f)
38 {
39  using Traits = VertexTraits<Vertex>;
40  using Edges = typename Traits::Edges;
41  using Edge = typename Traits::Edge;
42 
43  using Probe = std::pair<Vertex*, int>;
44  using Work = std::deque<Probe>;
45  using Visited = std::set<Vertex*>;
46  Work work;
47  Visited visited;
48  work.emplace_back(&start, 0);
49  int diameter(0);
50  while (!work.empty())
51  {
52  Probe const p(work.front());
53  work.pop_front();
54  if (visited.find(p.first) != visited.end())
55  continue;
56  diameter = std::max(p.second, diameter);
57  visited.insert(p.first);
58  for (typename Edges::iterator iter(Traits::edges(*p.first).begin());
59  iter != Traits::edges(*p.first).end();
60  ++iter)
61  {
62  Vertex* v(Traits::vertex(*iter));
63  if (visited.find(v) != visited.end())
64  continue;
65  if (!iter->closed())
66  work.emplace_back(v, p.second + 1);
67  }
68  f(*p.first, diameter);
69  }
70 }
71 
72 } // namespace Sim
73 } // namespace PeerFinder
74 } // namespace ripple
75 
76 #endif
std::pair
ripple::PeerFinder::Sim::breadth_first_traverse
void breadth_first_traverse(Vertex &start, Function f)
Call a function for each vertex in a connected graph.
Definition: GraphAlgorithms.h:37
ripple::PeerFinder::Sim::VertexTraits
Definition: GraphAlgorithms.h:28
std::deque
STL class.
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std::max
T max(T... args)
std::set
STL class.