def sorted_dependencies(dist, reverse=False):
    from .. import baseclass
    collection = [dist]
    nodes = [dist]
    edges = []
    pool = [dist]
    while pool:
        dist = pool.pop()
        for key in sorted(dist.prm):
            value = dist.prm[key]
            if not isinstance(value, baseclass.Dist):
                continue
            if (dist, value) not in edges:
                edges.append((dist, value))
            if value not in nodes:
                nodes.append(value)
                pool.append(value)
    permanent_marks = set()
    temporary_marks = set()
    def visit(node):
        if node in permanent_marks:
            return
        if node in temporary_marks:
            raise DependencyError("cycles in dependency structure.")
        nodes.remove(node)
        temporary_marks.add(node)
        for node1, node2 in edges:
            if node1 is node:
                visit(node2)
        temporary_marks.remove(node)
        permanent_marks.add(node)
        pool.append(node)
    while nodes:
        node = nodes[0]
        visit(node)
    if not reverse:
        pool = list(reversed(pool))
    return pool