Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:43:31

0001 //
0002 //=======================================================================
0003 // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
0004 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
0005 //
0006 // Distributed under the Boost Software License, Version 1.0. (See
0007 // accompanying file LICENSE_1_0.txt or copy at
0008 // http://www.boost.org/LICENSE_1_0.txt)
0009 //=======================================================================
0010 //
0011 #ifndef BOOST_DISJOINT_SETS_HPP
0012 #define BOOST_DISJOINT_SETS_HPP
0013 
0014 #include <vector>
0015 #include <boost/graph/properties.hpp>
0016 #include <boost/pending/detail/disjoint_sets.hpp>
0017 
0018 namespace boost
0019 {
0020 
0021 struct find_with_path_halving
0022 {
0023     template < class ParentPA, class Vertex >
0024     Vertex operator()(ParentPA p, Vertex v)
0025     {
0026         return detail::find_representative_with_path_halving(p, v);
0027     }
0028 };
0029 
0030 struct find_with_full_path_compression
0031 {
0032     template < class ParentPA, class Vertex >
0033     Vertex operator()(ParentPA p, Vertex v)
0034     {
0035         return detail::find_representative_with_full_compression(p, v);
0036     }
0037 };
0038 
0039 // This is a generalized functor to provide disjoint sets operations
0040 // with "union by rank" and "path compression".  A disjoint-set data
0041 // structure maintains a collection S={S1, S2, ..., Sk} of disjoint
0042 // sets. Each set is identified by a representative, which is some
0043 // member of of the set. Sets are represented by rooted trees. Two
0044 // heuristics: "union by rank" and "path compression" are used to
0045 // speed up the operations.
0046 
0047 // Disjoint Set requires two vertex properties for internal use.  A
0048 // RankPA and a ParentPA. The RankPA must map Vertex to some Integral type
0049 // (preferably the size_type associated with Vertex). The ParentPA
0050 // must map Vertex to Vertex.
0051 template < class RankPA, class ParentPA,
0052     class FindCompress = find_with_full_path_compression >
0053 class disjoint_sets
0054 {
0055     typedef disjoint_sets self;
0056 
0057     inline disjoint_sets() {}
0058 
0059 public:
0060     inline disjoint_sets(RankPA r, ParentPA p) : rank(r), parent(p) {}
0061 
0062     inline disjoint_sets(const self& c) : rank(c.rank), parent(c.parent) {}
0063 
0064     // Make Set -- Create a singleton set containing vertex x
0065     template < class Element > inline void make_set(Element x)
0066     {
0067         put(parent, x, x);
0068         typedef typename property_traits< RankPA >::value_type R;
0069         put(rank, x, R());
0070     }
0071 
0072     // Link - union the two sets represented by vertex x and y
0073     template < class Element > inline void link(Element x, Element y)
0074     {
0075         detail::link_sets(parent, rank, x, y, rep);
0076     }
0077 
0078     // Union-Set - union the two sets containing vertex x and y
0079     template < class Element > inline void union_set(Element x, Element y)
0080     {
0081         link(find_set(x), find_set(y));
0082     }
0083 
0084     // Find-Set - returns the Element representative of the set
0085     // containing Element x and applies path compression.
0086     template < class Element > inline Element find_set(Element x)
0087     {
0088         return rep(parent, x);
0089     }
0090 
0091     template < class ElementIterator >
0092     inline std::size_t count_sets(ElementIterator first, ElementIterator last)
0093     {
0094         std::size_t count = 0;
0095         for (; first != last; ++first)
0096             if (get(parent, *first) == *first)
0097                 ++count;
0098         return count;
0099     }
0100 
0101     template < class ElementIterator >
0102     inline void normalize_sets(ElementIterator first, ElementIterator last)
0103     {
0104         for (; first != last; ++first)
0105             detail::normalize_node(parent, *first);
0106     }
0107 
0108     template < class ElementIterator >
0109     inline void compress_sets(ElementIterator first, ElementIterator last)
0110     {
0111         for (; first != last; ++first)
0112             detail::find_representative_with_full_compression(parent, *first);
0113     }
0114 
0115 protected:
0116     RankPA rank;
0117     ParentPA parent;
0118     FindCompress rep;
0119 };
0120 
0121 template < class ID = identity_property_map,
0122     class InverseID = identity_property_map,
0123     class FindCompress = find_with_full_path_compression >
0124 class disjoint_sets_with_storage
0125 {
0126     typedef typename property_traits< ID >::value_type Index;
0127     typedef std::vector< Index > ParentContainer;
0128     typedef std::vector< unsigned char > RankContainer;
0129 
0130 public:
0131     typedef typename ParentContainer::size_type size_type;
0132 
0133     disjoint_sets_with_storage(
0134         size_type n = 0, ID id_ = ID(), InverseID inv = InverseID())
0135     : id(id_), id_to_vertex(inv), rank(n, 0), parent(n)
0136     {
0137         for (Index i = 0; i < n; ++i)
0138             parent[i] = i;
0139     }
0140     // note this is not normally needed
0141     template < class Element > inline void make_set(Element x)
0142     {
0143         parent[x] = x;
0144         rank[x] = 0;
0145     }
0146     template < class Element > inline void link(Element x, Element y)
0147     {
0148         extend_sets(x, y);
0149         detail::link_sets(&parent[0], &rank[0], get(id, x), get(id, y), rep);
0150     }
0151     template < class Element > inline void union_set(Element x, Element y)
0152     {
0153         Element rx = find_set(x);
0154         Element ry = find_set(y);
0155         link(rx, ry);
0156     }
0157     template < class Element > inline Element find_set(Element x)
0158     {
0159         return id_to_vertex[rep(&parent[0], get(id, x))];
0160     }
0161 
0162     template < class ElementIterator >
0163     inline std::size_t count_sets(ElementIterator first, ElementIterator last)
0164     {
0165         std::size_t count = 0;
0166         for (; first != last; ++first)
0167             if (parent[*first] == *first)
0168                 ++count;
0169         return count;
0170     }
0171 
0172     template < class ElementIterator >
0173     inline void normalize_sets(ElementIterator first, ElementIterator last)
0174     {
0175         for (; first != last; ++first)
0176             detail::normalize_node(&parent[0], *first);
0177     }
0178 
0179     template < class ElementIterator >
0180     inline void compress_sets(ElementIterator first, ElementIterator last)
0181     {
0182         for (; first != last; ++first)
0183             detail::find_representative_with_full_compression(
0184                 &parent[0], *first);
0185     }
0186 
0187     const ParentContainer& parents() { return parent; }
0188 
0189 protected:
0190     template < class Element > inline void extend_sets(Element x, Element y)
0191     {
0192         Index needed
0193             = get(id, x) > get(id, y) ? get(id, x) + 1 : get(id, y) + 1;
0194         if (needed > parent.size())
0195         {
0196             rank.insert(rank.end(), needed - rank.size(), 0);
0197             for (Index k = parent.size(); k < needed; ++k)
0198                 parent.push_back(k);
0199         }
0200     }
0201 
0202     ID id;
0203     InverseID id_to_vertex;
0204     RankContainer rank;
0205     ParentContainer parent;
0206     FindCompress rep;
0207 };
0208 
0209 } // namespace boost
0210 
0211 #endif // BOOST_DISJOINT_SETS_HPP