lldependencies.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /**
  2. * @file lldependencies.cpp
  3. * @author Nat Goodspeed
  4. * @date 2008-09-17
  5. * @brief Implementation for lldependencies.
  6. *
  7. * $LicenseInfo:firstyear=2008&license=viewergpl$
  8. *
  9. * Copyright (c) 2010, Linden Research, Inc.
  10. *
  11. * Second Life Viewer Source Code
  12. * The source code in this file ("Source Code") is provided by Linden Lab
  13. * to you under the terms of the GNU General Public License, version 2.0
  14. * ("GPL"), unless you have obtained a separate licensing agreement
  15. * ("Other License"), formally executed by you and Linden Lab. Terms of
  16. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  17. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  18. *
  19. * There are special exceptions to the terms and conditions of the GPL as
  20. * it is applied to this Source Code. View the full text of the exception
  21. * in the file doc/FLOSS-exception.txt in this software distribution, or
  22. * online at
  23. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  24. *
  25. * By copying, modifying or distributing this software, you acknowledge
  26. * that you have read and understood your obligations described above,
  27. * and agree to abide by those obligations.
  28. *
  29. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  30. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  31. * COMPLETENESS OR PERFORMANCE.
  32. * $/LicenseInfo$
  33. */
  34. #include "linden_common.h"
  35. #include <sstream>
  36. #include "boost/graph/graph_traits.hpp" // For boost::graph_traits
  37. #include "boost/graph/adjacency_list.hpp"
  38. #include "boost/graph/topological_sort.hpp"
  39. #include "boost/graph/exception.hpp"
  40. #include "lldependencies.h"
  41. LLDependenciesBase::VertexList LLDependenciesBase::topo_sort(size_t vertices,
  42. const EdgeList& edges) const
  43. {
  44. // Construct a Boost Graph Library graph according to the constraints
  45. // we've collected. It seems as though we ought to be able to capture
  46. // the uniqueness of vertex keys using a setS of vertices with a
  47. // string property -- but I don't yet understand adjacency_list well
  48. // enough to get there. All the examples I've seen so far use integers
  49. // for vertices.
  50. // Define the Graph type. Use a vector for vertices so we can use the
  51. // default topological_sort vertex lookup by int index. Use a set for
  52. // edges because the same dependency may be stated twice: Node "a" may
  53. // specify that it must precede "b", while "b" may also state that it
  54. // must follow "a".
  55. typedef boost::adjacency_list<boost::setS, boost::vecS, boost::directedS,
  56. boost::no_property> Graph;
  57. // Instantiate the graph. Without vertex properties, we need say no
  58. // more about vertices than the total number.
  59. Graph g(edges.begin(), edges.end(), vertices);
  60. // topo sort
  61. typedef boost::graph_traits<Graph>::vertex_descriptor VertexDesc;
  62. typedef std::vector<VertexDesc> SortedList;
  63. SortedList sorted;
  64. // note that it throws not_a_dag if it finds a cycle
  65. try
  66. {
  67. boost::topological_sort(g, std::back_inserter(sorted));
  68. }
  69. catch (const boost::not_a_dag& e)
  70. {
  71. // translate to the exception we define
  72. std::ostringstream out;
  73. out << "LLDependencies cycle: " << e.what() << '\n';
  74. // Omit independent nodes: display only those that might contribute to
  75. // the cycle.
  76. describe(out, false);
  77. throw Cycle(out.str());
  78. }
  79. // A peculiarity of boost::topological_sort() is that it emits results in
  80. // REVERSE topological order: to get the result you want, you must
  81. // traverse the SortedList using reverse iterators.
  82. return VertexList(sorted.rbegin(), sorted.rend());
  83. }
  84. std::ostream& LLDependenciesBase::describe(std::ostream& out, bool full) const
  85. {
  86. // Should never encounter this base-class implementation; may mean that
  87. // the KEY type doesn't have a suitable ostream operator<<().
  88. out << "<no description available>";
  89. return out;
  90. }
  91. std::string LLDependenciesBase::describe(bool full) const
  92. {
  93. // Just use the ostream-based describe() on a std::ostringstream. The
  94. // implementation is here mostly so that we can avoid #include <sstream>
  95. // in the header file.
  96. std::ostringstream out;
  97. describe(out, full);
  98. return out.str();
  99. }