PageRenderTime 41ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llcommon/lldependencies.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 103 lines | 40 code | 4 blank | 59 comment | 0 complexity | 797b9b66682a8313bb65b7e9a2a96e9d MD5 | raw file
Possible License(s): LGPL-2.1
  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=viewerlgpl$
  8. * Second Life Viewer Source Code
  9. * Copyright (C) 2010, Linden Research, Inc.
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation;
  14. * version 2.1 of the License only.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  26. * $/LicenseInfo$
  27. */
  28. // Precompiled header
  29. #include "linden_common.h"
  30. // associated header
  31. #include "lldependencies.h"
  32. // STL headers
  33. #include <map>
  34. #include <sstream>
  35. // std headers
  36. // external library headers
  37. #include <boost/graph/graph_traits.hpp> // for boost::graph_traits
  38. #include <boost/graph/adjacency_list.hpp>
  39. #include <boost/graph/topological_sort.hpp>
  40. #include <boost/graph/exception.hpp>
  41. // other Linden headers
  42. LLDependenciesBase::VertexList LLDependenciesBase::topo_sort(int vertices, 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. }