PageRenderTime 23ms CodeModel.GetById 16ms app.highlight 4ms RepoModel.GetById 1ms 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
  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
 29// Precompiled header
 30#include "linden_common.h"
 31// associated header
 32#include "lldependencies.h"
 33// STL headers
 34#include <map>
 35#include <sstream>
 36// std headers
 37// external library headers
 38#include <boost/graph/graph_traits.hpp>  // for boost::graph_traits
 39#include <boost/graph/adjacency_list.hpp>
 40#include <boost/graph/topological_sort.hpp>
 41#include <boost/graph/exception.hpp>
 42// other Linden headers
 43
 44LLDependenciesBase::VertexList LLDependenciesBase::topo_sort(int vertices, const EdgeList& edges) const
 45{
 46    // Construct a Boost Graph Library graph according to the constraints
 47    // we've collected. It seems as though we ought to be able to capture
 48    // the uniqueness of vertex keys using a setS of vertices with a
 49    // string property -- but I don't yet understand adjacency_list well
 50    // enough to get there. All the examples I've seen so far use integers
 51    // for vertices.
 52    // Define the Graph type. Use a vector for vertices so we can use the
 53    // default topological_sort vertex lookup by int index. Use a set for
 54    // edges because the same dependency may be stated twice: Node "a" may
 55    // specify that it must precede "b", while "b" may also state that it
 56    // must follow "a".
 57    typedef boost::adjacency_list<boost::setS, boost::vecS, boost::directedS,
 58                                  boost::no_property> Graph;
 59    // Instantiate the graph. Without vertex properties, we need say no
 60    // more about vertices than the total number.
 61    Graph g(edges.begin(), edges.end(), vertices);
 62    // topo sort
 63    typedef boost::graph_traits<Graph>::vertex_descriptor VertexDesc;
 64    typedef std::vector<VertexDesc> SortedList;
 65    SortedList sorted;
 66    // note that it throws not_a_dag if it finds a cycle
 67    try
 68    {
 69        boost::topological_sort(g, std::back_inserter(sorted));
 70    }
 71    catch (const boost::not_a_dag& e)
 72    {
 73        // translate to the exception we define
 74        std::ostringstream out;
 75        out << "LLDependencies cycle: " << e.what() << '\n';
 76        // Omit independent nodes: display only those that might contribute to
 77        // the cycle.
 78        describe(out, false);
 79        throw Cycle(out.str());
 80    }
 81    // A peculiarity of boost::topological_sort() is that it emits results in
 82    // REVERSE topological order: to get the result you want, you must
 83    // traverse the SortedList using reverse iterators.
 84    return VertexList(sorted.rbegin(), sorted.rend());
 85}
 86
 87std::ostream& LLDependenciesBase::describe(std::ostream& out, bool full) const
 88{
 89    // Should never encounter this base-class implementation; may mean that
 90    // the KEY type doesn't have a suitable ostream operator<<().
 91    out << "<no description available>";
 92    return out;
 93}
 94
 95std::string LLDependenciesBase::describe(bool full) const
 96{
 97    // Just use the ostream-based describe() on a std::ostringstream. The
 98    // implementation is here mostly so that we can avoid #include <sstream>
 99    // in the header file.
100    std::ostringstream out;
101    describe(out, full);
102    return out.str();
103}