Skip to content

Commit

Permalink
Add a parallel option.
Browse files Browse the repository at this point in the history
If `-P` is specified, nodes that rank the same are printed on the same line.  It can be combined with `-d` if `-D` is also used, or with `-p` if `-D` is not used.
  • Loading branch information
dag-erling committed Sep 13, 2024
1 parent 07c04d1 commit c217ce2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
17 changes: 14 additions & 3 deletions bin/ptsort.1
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.\"-
.\" Copyright (c) 2016-2017 Dag-Erling Smørgrav
.\" Copyright (c) 2016-2024 Dag-Erling Smørgrav
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
Expand All @@ -26,15 +26,15 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.Dd September 4, 2017
.Dd September 13, 2024
.Dt PTSORT 1
.Os
.Sh NAME
.Nm ptsort
.Nd Prioritized topological sort
.Sh SYNOPSIS
.Nm
.Op Fl Ddpqsv
.Op Fl DdPpqsv
.Op Fl o Ar output
.Op Ar input ...
.Sh DESCRIPTION
Expand All @@ -58,6 +58,17 @@ The following options are available:
Sort the output by depth first instead of priority first.
.It Fl d
Include each node's depth in the output.
.It Fl P
Print all nodes with the same priority (or the same depth if
.Fl D
was specified) on the same line.
Can be combined with
.Fl d
if and only if
.Fl D
is also specified, and with
.Fl p
if and only if it is not.
.It Fl p
Include each node's priority in the output.
.It Fl q
Expand Down
35 changes: 28 additions & 7 deletions bin/ptsort.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "fline.h"

static bool bydepth;
static bool parallel;
static bool printdepth;
static bool printprio;
static bool quiet;
Expand Down Expand Up @@ -397,16 +398,18 @@ output(const char *fn)
pnode **all, **p;
pnode *n;
FILE *f;
bool same = false;

/* allocate array of pointers */
if ((p = all = malloc(tnnodes * sizeof *all)) == NULL)
if ((p = all = malloc((tnnodes + 1) * sizeof *all)) == NULL)
err(1, "malloc()");

/* copy nodes into array in lexical order */
for (n = aa_first(&nodes, &nit); n != NULL; n = aa_next(&nit))
*p++ = n;
aa_finish(&nit);
/* p now points one past the end of the array */
/* p now points to the sentinel at the end of the array */
*p = NULL;

/* sort by either priority or depth */
#if HAVE_MERGESORT
Expand All @@ -424,12 +427,21 @@ output(const char *fn)

/* reverse through the array and print each node's name */
while (p-- > all) {
if (printdepth)
if (parallel && p[1] != NULL) {
same = bydepth ? p[0]->depth == p[1]->depth :
p[0]->prio == p[1]->prio;
fputc(same ? ' ' : '\n', f);
}
if (printdepth && !same)
fprintf(f, "%7lu ", (*p)->depth);
if (printprio)
if (printprio && !same)
fprintf(f, "%7lu ", (*p)->prio);
fprintf(f, "%s\n", (*p)->name);
fprintf(f, "%s", (*p)->name);
if (!parallel)
fputc('\n', f);
}
if (parallel)
fputc('\n', f);

/* done */
if (f != stdout)
Expand All @@ -441,7 +453,7 @@ static void
usage(void)
{

fprintf(stderr, "usage: ptsort [-Ddpqsv] [-o output] [input ...]\n");
fprintf(stderr, "usage: ptsort [-DdPpqsv] [-o output] [input ...]\n");
exit(1);
}

Expand All @@ -464,6 +476,9 @@ main(int argc, char *argv[])
case 'd':
printdepth = true;
break;
case 'P':
parallel = true;
break;
case 'p':
printprio = true;
break;
Expand All @@ -483,12 +498,18 @@ main(int argc, char *argv[])
argc -= optind;
argv += optind;

if (parallel && bydepth && printprio)
errx(1, "with -P, -p cannot be combined with -D");
if (parallel && !bydepth && printdepth)
errx(1, "with -P, -d must be combined with -D");

if (argc == 0)
input(NULL);
else
while (argc--)
input(*argv++);
verbose("graph has %lu nodes and %lu edges", tnnodes, tnedges);
output(ofn);
if (tnnodes > 0)
output(ofn);
exit(0);
}

0 comments on commit c217ce2

Please sign in to comment.