1 /* tsort - topological sort.
2 Copyright (C) 1998-2003 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Mark Kettenis <kettenis@phys.uva.nl>. */
20 /* The topological sort is done according to Algorithm T (Topological
21 sort) in Donald E. Knuth, The Art of Computer Programming, Volume
22 1/Fundamental Algorithms, page 262. */
29 #include <sys/types.h>
32 #include "long-options.h"
34 #include "readtokens.h"
36 /* The official name of this program (e.g., no `g' prefix). */
37 #define PROGRAM_NAME "tsort"
39 #define AUTHORS "Mark Kettenis"
41 /* Token delimiters when reading from a file. */
44 /* Members of the list of successors. */
48 struct successor
*next
;
51 /* Each string is held in core as the head of a list of successors. */
55 struct item
*left
, *right
;
59 struct successor
*top
;
62 /* The name this program was run with. */
65 /* Nonzero if any of the input files are the standard input. */
66 static int have_read_stdin
;
68 /* The error code to return to the system. */
69 static int exit_status
;
71 /* The head of the sorted list. */
72 static struct item
*head
= NULL
;
74 /* The tail of the list of `zeros', strings that have no predecessors. */
75 static struct item
*zeros
= NULL
;
77 /* Used for loop detection. */
78 static struct item
*loop
= NULL
;
80 /* The number of strings to sort. */
81 static int n_strings
= 0;
83 static struct option
const long_options
[] =
92 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
97 Usage: %s [OPTION] [FILE]\n\
98 Write totally ordered list consistent with the partial ordering in FILE.\n\
99 With no FILE, or when FILE is -, read standard input.\n\
102 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
103 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
104 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
107 exit (status
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);
110 /* Create a new item/node for STR. */
112 new_item (const char *str
)
114 struct item
*k
= xmalloc (sizeof *k
);
116 k
->str
= (str
? xstrdup (str
): NULL
);
117 k
->left
= k
->right
= NULL
;
120 /* T1. Initialize (COUNT[k] <- 0 and TOP[k] <- ^). */
128 /* Search binary tree rooted at *ROOT for STR. Allocate a new tree if
129 *ROOT is NULL. Insert a node/item for STR if not found. Return
130 the node/item found/created for STR.
132 This is done according to Algorithm A (Balanced tree search and
133 insertion) in Donald E. Knuth, The Art of Computer Programming,
134 Volume 3/Searching and Sorting, pages 455--457. */
137 search_item (struct item
*root
, const char *str
)
139 struct item
*p
, *q
, *r
, *s
, *t
;
144 /* Make sure the tree is not empty, since that is what the algorithm
146 if (root
->right
== NULL
)
147 return (root
->right
= new_item (str
));
149 /* A1. Initialize. */
156 a
= strcmp (str
, p
->str
);
160 /* A3 & A4. Move left & right. */
171 /* A3 & A4. (continued). */
177 /* A6. Adjust balance factors. */
178 assert (!STREQ (str
, s
->str
));
179 if (strcmp (str
, s
->str
) < 0)
192 assert (!STREQ (str
, p
->str
));
193 if (strcmp (str
, p
->str
) < 0)
205 /* A7. Balancing act. */
206 if (s
->balance
== 0 || s
->balance
== -a
)
214 /* A8. Single Rotation. */
226 s
->balance
= r
->balance
= 0;
230 /* A9. Double rotation. */
252 else if (p
->balance
== -a
)
257 /* A10. Finishing touch. */
266 /* A3 & A4. (continued). */
279 /* Record the fact that J precedes K. */
282 record_relation (struct item
*j
, struct item
*k
)
286 if (!STREQ (j
->str
, k
->str
))
289 p
= xmalloc (sizeof *p
);
297 count_items (struct item
*unused ATTRIBUTE_UNUSED
)
304 scan_zeros (struct item
*k
)
306 /* Ignore strings that have already been printed. */
307 if (k
->count
== 0 && k
->str
)
320 /* Try to detect the loop. If we have detected that K is part of a
321 loop, print the loop on standard error, remove a relation to break
322 the loop, and return non-zero.
324 The loop detection strategy is as follows: Realise that what we're
325 dealing with is essentially a directed graph. If we find an item
326 that is part of a graph that contains a cycle we traverse the graph
327 in backwards direction. In general there is no unique way to do
328 this, but that is no problem. If we encounter an item that we have
329 encountered before, we know that we've found a cycle. All we have
330 to do now is retrace our steps, printing out the items until we
331 encounter that item again. (This is not necessarily the item that
332 we started from originally.) Since the order in which the items
333 are stored in the tree is not related to the specified partial
334 ordering, we may need to walk the tree several times before the
335 loop has completely been constructed. If the loop was found, the
336 global variable LOOP will be NULL. */
339 detect_loop (struct item
*k
)
343 /* K does not have to be part of a cycle. It is however part of
344 a graph that contains a cycle. */
347 /* Start traversing the graph at K. */
351 struct successor
**p
= &k
->top
;
355 if ((*p
)->suc
== loop
)
359 /* We have found a loop. Retrace our steps. */
362 struct item
*tmp
= loop
->qlink
;
364 fprintf (stderr
, "%s: %s\n", program_name
,
367 /* Until we encounter K again. */
370 /* Remove relation. */
376 /* Tidy things up since we might have to
377 detect another loop. */
384 struct item
*tmp
= loop
->qlink
;
390 /* Since we have found the loop, stop walking
410 /* Recurse (sub)tree rooted at ROOT, calling ACTION for each node.
411 Stop when ACTION returns non-zero. */
414 recurse_tree (struct item
*root
, int (*action
) (struct item
*))
416 if (root
->left
== NULL
&& root
->right
== NULL
)
417 return (*action
) (root
);
420 if (root
->left
!= NULL
)
421 if (recurse_tree (root
->left
, action
))
423 if ((*action
) (root
))
425 if (root
->right
!= NULL
)
426 if (recurse_tree (root
->right
, action
))
433 /* Walk the tree specified by the head ROOT, calling ACTION for
437 walk_tree (struct item
*root
, int (*action
) (struct item
*))
440 recurse_tree (root
->right
, action
);
443 /* Do a topological sort on FILE. */
446 tsort (const char *file
)
449 struct item
*j
= NULL
;
450 struct item
*k
= NULL
;
452 token_buffer tokenbuffer
;
454 /* Intialize the head of the tree will hold the strings we're sorting. */
455 root
= new_item (NULL
);
457 if (STREQ (file
, "-"))
464 fp
= fopen (file
, "r");
466 error (EXIT_FAILURE
, errno
, "%s", file
);
469 init_tokenbuffer (&tokenbuffer
);
475 /* T2. Next Relation. */
476 len
= readtoken (fp
, DELIM
, sizeof (DELIM
) - 1, &tokenbuffer
);
482 k
= search_item (root
, tokenbuffer
.buffer
);
485 /* T3. Record the relation. */
486 record_relation (j
, k
);
494 error (EXIT_FAILURE
, 0, _("%s: input contains an odd number of tokens"),
497 /* T1. Initialize (N <- n). */
498 walk_tree (root
, count_items
);
500 while (n_strings
> 0)
502 /* T4. Scan for zeros. */
503 walk_tree (root
, scan_zeros
);
507 struct successor
*p
= head
->top
;
509 /* T5. Output front of queue. */
510 printf ("%s\n", head
->str
);
511 head
->str
= NULL
; /* Avoid printing the same string twice. */
514 /* T6. Erase relations. */
518 if (p
->suc
->count
== 0)
520 zeros
->qlink
= p
->suc
;
527 /* T7. Remove from queue. */
531 /* T8. End of process. */
532 assert (n_strings
>= 0);
535 /* The input contains a loop. */
536 error (0, 0, _("%s: input contains a loop:"), file
);
539 /* Print the loop and remove a relation to break it. */
541 walk_tree (root
, detect_loop
);
548 main (int argc
, char **argv
)
552 initialize_main (&argc
, &argv
);
553 program_name
= argv
[0];
554 setlocale (LC_ALL
, "");
555 bindtextdomain (PACKAGE
, LOCALEDIR
);
556 textdomain (PACKAGE
);
558 atexit (close_stdout
);
562 parse_long_options (argc
, argv
, PROGRAM_NAME
, PACKAGE
, VERSION
,
563 usage
, AUTHORS
, (char const *) NULL
);
565 while ((opt
= getopt_long (argc
, argv
, "", long_options
, NULL
)) != -1)
568 case 0: /* long option */
571 usage (EXIT_FAILURE
);
576 if (1 < argc
- optind
)
578 error (0, 0, _("only one argument may be specified"));
579 usage (EXIT_FAILURE
);
582 tsort (optind
== argc
? "-" : argv
[optind
]);
584 if (have_read_stdin
&& fclose (stdin
) == EOF
)
585 error (EXIT_FAILURE
, errno
, _("standard input"));
587 exit (exit_status
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);