8#ifndef INCLUDED_BDLB_TOPOLOGICALSORTUTIL
9#define INCLUDED_BDLB_TOPOLOGICALSORTUTIL
477#include <bdlscm_version.h>
488#include <bsl_iterator.h>
489#include <bsl_queue.h>
490#include <bsl_unordered_map.h>
491#include <bsl_utility.h>
492#include <bsl_vector.h>
529template <
class EDGE_TYPE>
535template <
class NODE_TYPE>
550 static const NODE_TYPE& from(
const EdgeType& edge);
554 static const NODE_TYPE& to(
const EdgeType& edge);
571template <
class INPUT_ITER>
576 typedef bsl::iterator_traits<INPUT_ITER> InIterTraits;
577 typedef typename InIterTraits::value_type EdgeType;
582 typedef typename EdgeTraits::NodeType NodeType;
601 int d_predecessorCount;
662 INPUT_ITER relationsEnd,
672 template <
class OUTPUT_ITER>
682 template <
class OUTPUT_ITER,
class UNSORTED_OUT_ITER>
683 bool sortImpl(OUTPUT_ITER resultOutIter,
684 UNSORTED_OUT_ITER unsortedOutIter);
737 template <
class INPUT_ITER,
739 class UNSORTED_OUTPUT_ITER>
740 static bool sort(INPUT_ITER relationsBegin,
741 INPUT_ITER relationsEnd,
742 OUTPUT_ITER resultOutIter,
743 UNSORTED_OUTPUT_ITER unsortedOutIter);
757 template <
class NODE_TYPE>
772template <
class NODE_TYPE>
781template <
class NODE_TYPE>
796template <
class INPUT_ITER>
800: d_predecessorCount(0)
801, d_successors(allocator)
805template <
class INPUT_ITER>
807TopologicalSortUtil_Helper<INPUT_ITER>::Links::Links(
808 const Links& original,
810: d_predecessorCount(original.d_predecessorCount)
811, d_successors(original.d_successors, allocator)
819template <
class INPUT_ITER>
821 INPUT_ITER relationsBegin,
822 INPUT_ITER relationsEnd,
824: d_workSet(allocator)
825, d_readyNodes(allocator)
830 for (INPUT_ITER iter = relationsBegin; iter != relationsEnd; ++iter) {
831 const NodeType& from = EdgeTraits::from(*iter);
832 const NodeType& to = EdgeTraits::to(*iter);
833 ++d_workSet[to].d_predecessorCount;
834 d_workSet[from].d_successors.push_back(to);
841 const LinksMapIter end = d_workSet.
end();
842 for (LinksMapIter iter = d_workSet.
begin(); iter != end; ++iter) {
843 if (0 == iter->second.d_predecessorCount) {
844 d_readyNodes.
push(iter);
850template <
class INPUT_ITER>
851template <
class OUTPUT_ITER>
853 OUTPUT_ITER *resultOutIter_p)
855 if (d_readyNodes.empty()) {
863 const LinksMapIter mapIter = d_readyNodes.front();
866 *(*resultOutIter_p) = mapIter->first;
867 ++(*resultOutIter_p);
872 for (ListIter successorIter = mapIter->second.d_successors.begin();
873 successorIter != mapIter->second.d_successors.end();
876 LinksMapIter succMapIter = d_workSet.find((*successorIter));
877 Links& succLinks = succMapIter->second;
880 if (--(succLinks.d_predecessorCount) == 0) {
882 d_readyNodes.push(succMapIter);
887 d_workSet.erase(mapIter);
892template <
class INPUT_ITER>
893template <
class OUTPUT_ITER,
class UNSORTED_OUT_ITER>
895 OUTPUT_ITER resultOutIter,
896 UNSORTED_OUT_ITER unsortedOutIter)
898 while (!d_workSet.empty()) {
899 if (!processNextNodeInOrder(&resultOutIter)) {
906 const LinksMapConstIter end = d_workSet.end();
907 for (LinksMapConstIter i = d_workSet.begin(); i != end; ++i) {
908 *unsortedOutIter = i->first;
922template <
class INPUT_ITER,
class OUTPUT_ITER,
class UNSORTED_OUT_ITER>
924 INPUT_ITER relationsEnd,
925 OUTPUT_ITER resultOutIter,
926 UNSORTED_OUT_ITER unsortedOutIter)
930 return MyHelper(relationsBegin, relationsEnd).
sortImpl(resultOutIter,
934template <
class NODE_TYPE>
942 return sort(relations.begin(),
944 bsl::back_insert_iterator<vector_type>(*result),
945 bsl::back_insert_iterator<vector_type>(*unsorted));
#define BSLMF_NESTED_TRAIT_DECLARATION(t_TYPE, t_TRAIT)
Definition bslmf_nestedtraitdeclaration.h:231
Definition bdlb_topologicalsortutil.h:572
bool sortImpl(OUTPUT_ITER resultOutIter, UNSORTED_OUT_ITER unsortedOutIter)
Definition bdlb_topologicalsortutil.h:894
bool processNextNodeInOrder(OUTPUT_ITER *resultOutIter_p)
Definition bdlb_topologicalsortutil.h:852
BSLMF_NESTED_TRAIT_DECLARATION(TopologicalSortUtil_Helper, bslma::UsesBslmaAllocator)
Definition bslstl_pair.h:1210
Definition bslstl_queue.h:304
void push(const value_type &value)
Definition bslstl_queue.h:775
Definition bslstl_unorderedmap.h:1089
iterator end() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_unorderedmap.h:2894
BloombergLP::bslstl::HashTableIterator< value_type, difference_type > iterator
Definition bslstl_unorderedmap.h:1158
iterator begin() BSLS_KEYWORD_NOEXCEPT
Definition bslstl_unorderedmap.h:2885
BloombergLP::bslstl::HashTableIterator< const value_type, difference_type > const_iterator
Definition bslstl_unorderedmap.h:1160
Definition bslstl_vector.h:1025
NodeType * iterator
Definition bslstl_vector.h:1057
NodeType const * const_iterator
Definition bslstl_vector.h:1058
Definition bslma_allocator.h:457
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition bdlb_algorithmworkaroundutil.h:74
Definition bdlb_printmethods.h:283
NODE_TYPE NodeType
The type of values of the nodes of the directed acyclic graph.
Definition bdlb_topologicalsortutil.h:544
bsl::pair< NODE_TYPE, NODE_TYPE > EdgeType
The type of the directed connection from one node to another.
Definition bdlb_topologicalsortutil.h:541
Definition bdlb_topologicalsortutil.h:530
Definition bdlb_topologicalsortutil.h:693
static bool sort(INPUT_ITER relationsBegin, INPUT_ITER relationsEnd, OUTPUT_ITER resultOutIter, UNSORTED_OUTPUT_ITER unsortedOutIter)
TYPE first
Definition bslstl_pair.h:605
TYPE second
Definition bslstl_pair.h:908
Definition bslma_usesbslmaallocator.h:343