Annotation of 43BSDReno/pgrm/tsort/tsort.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1989 The Regents of the University of California.
                      3:  * All rights reserved.
                      4:  *
                      5:  * This code is derived from software contributed to Berkeley by
                      6:  * Michael Rendell of Memorial University of Newfoundland.
                      7:  *
                      8:  * Redistribution and use in source and binary forms are permitted provided
                      9:  * that: (1) source distributions retain this entire copyright notice and
                     10:  * comment, and (2) distributions including binaries display the following
                     11:  * acknowledgement:  ``This product includes software developed by the
                     12:  * University of California, Berkeley and its contributors'' in the
                     13:  * documentation or other materials provided with the distribution and in
                     14:  * all advertising materials mentioning features or use of this software.
                     15:  * Neither the name of the University nor the names of its contributors may
                     16:  * be used to endorse or promote products derived from this software without
                     17:  * specific prior written permission.
                     18:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
                     19:  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
                     20:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     21:  */
                     22: 
                     23: #ifndef lint
                     24: char copyright[] =
                     25: "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
                     26:  All rights reserved.\n";
                     27: #endif /* not lint */
                     28: 
                     29: #ifndef lint
                     30: static char sccsid[] = "@(#)tsort.c    5.3 (Berkeley) 6/1/90";
                     31: #endif /* not lint */
                     32: 
                     33: #include <sys/types.h>
                     34: #include <errno.h>
                     35: #include <stdio.h>
                     36: #include <ctype.h>
                     37: #include <string.h>
                     38: 
                     39: /*
                     40:  *  Topological sort.  Input is a list of pairs of strings seperated by
                     41:  *  white space (spaces, tabs, and/or newlines); strings are written to
                     42:  *  standard output in sorted order, one per line.
                     43:  *
                     44:  *  usage:
                     45:  *     tsort [inputfile]
                     46:  *  If no input file is specified, standard input is read.
                     47:  *
                     48:  *  Should be compatable with AT&T tsort HOWEVER the output is not identical
                     49:  *  (i.e. for most graphs there is more than one sorted order, and this tsort
                     50:  *  usually generates a different one then the AT&T tsort).  Also, cycle
                     51:  *  reporting seems to be more accurate in this version (the AT&T tsort
                     52:  *  sometimes says a node is in a cycle when it isn't).
                     53:  *
                     54:  *  Michael Rendell, [email protected] - Feb 26, '90
                     55:  */
                     56: #define        HASHSIZE        53              /* doesn't need to be big */
                     57: #define        NF_MARK         0x1             /* marker for cycle detection */
                     58: #define        NF_ACYCLIC      0x2             /* this node is cycle free */
                     59: 
                     60: typedef struct node_str NODE;
                     61: 
                     62: struct node_str {
                     63:        char *n_name;                   /* name of this node */
                     64:        NODE **n_prevp;                 /* pointer to previous node's n_next */
                     65:        NODE *n_next;                   /* next node in graph */
                     66:        NODE *n_hash;                   /* next node in hash table */
                     67:        int n_narcs;                    /* number of arcs in n_arcs[] */
                     68:        int n_arcsize;                  /* size of n_arcs[] array */
                     69:        NODE **n_arcs;                  /* array of arcs to other nodes */
                     70:        int n_refcnt;                   /* # of arcs pointing to this node */
                     71:        int n_flags;                    /* NF_* */
                     72: };
                     73: 
                     74: typedef struct _buf {
                     75:        char *b_buf;
                     76:        int b_bsize;
                     77: } BUF;
                     78: 
                     79: NODE *add_node(), *find_node();
                     80: void add_arc(), no_memory(), remove_node(), tsort();
                     81: char *grow_buf(), *malloc();
                     82: 
                     83: extern int errno;
                     84: NODE *graph;
                     85: NODE *hashtable[HASHSIZE];
                     86: NODE **cycle_buf;
                     87: NODE **longest_cycle;
                     88: 
                     89: main(argc, argv)
                     90:        int argc;
                     91:        char **argv;
                     92: {
                     93:        register BUF *b;
                     94:        register int c, n;
                     95:        FILE *fp;
                     96:        int bsize, nused;
                     97:        BUF bufs[2];
                     98: 
                     99:        if (argc < 2)
                    100:                fp = stdin;
                    101:        else if (argc == 2) {
                    102:                (void)fprintf(stderr, "usage: tsort [ inputfile ]\n");
                    103:                exit(1);
                    104:        } else if (!(fp = fopen(argv[1], "r"))) {
                    105:                (void)fprintf(stderr, "tsort: %s.\n", strerror(errno));
                    106:                exit(1);
                    107:        }
                    108: 
                    109:        for (b = bufs, n = 2; --n >= 0; b++)
                    110:                b->b_buf = grow_buf((char *)NULL, b->b_bsize = 1024);
                    111: 
                    112:        /* parse input and build the graph */
                    113:        for (n = 0, c = getc(fp);;) {
                    114:                while (c != EOF && isspace(c))
                    115:                        c = getc(fp);
                    116:                if (c == EOF)
                    117:                        break;
                    118: 
                    119:                nused = 0;
                    120:                b = &bufs[n];
                    121:                bsize = b->b_bsize;
                    122:                do {
                    123:                        b->b_buf[nused++] = c;
                    124:                        if (nused == bsize) {
                    125:                                bsize *= 2;
                    126:                                b->b_buf = grow_buf(b->b_buf, bsize);
                    127:                        }
                    128:                        c = getc(fp);
                    129:                } while (c != EOF && !isspace(c));
                    130: 
                    131:                b->b_buf[nused] = '\0';
                    132:                b->b_bsize = bsize;
                    133:                if (n)
                    134:                        add_arc(bufs[0].b_buf, bufs[1].b_buf);
                    135:                n = !n;
                    136:        }
                    137:        (void)fclose(fp);
                    138:        if (n) {
                    139:                (void)fprintf(stderr, "tsort: odd data count.\n");
                    140:                exit(1);
                    141:        }
                    142: 
                    143:        /* do the sort */
                    144:        tsort();
                    145:        exit(0);
                    146: }
                    147: 
                    148: /* double the size of oldbuf and return a pointer to the new buffer. */
                    149: char *
                    150: grow_buf(bp, size)
                    151:        char *bp;
                    152:        int size;
                    153: {
                    154:        char *realloc();
                    155: 
                    156:        if (!(bp = realloc(bp, (u_int)size)))
                    157:                no_memory();
                    158:        return(bp);
                    159: }
                    160: 
                    161: /*
                    162:  * add an arc from node s1 to node s2 in the graph.  If s1 or s2 are not in
                    163:  * the graph, then add them.
                    164:  */
                    165: void
                    166: add_arc(s1, s2)
                    167:        char *s1, *s2;
                    168: {
                    169:        register NODE *n1;
                    170:        NODE *n2;
                    171:        int bsize;
                    172: 
                    173:        n1 = find_node(s1);
                    174:        if (!n1)
                    175:                n1 = add_node(s1);
                    176: 
                    177:        if (!strcmp(s1, s2))
                    178:                return;
                    179: 
                    180:        n2 = find_node(s2);
                    181:        if (!n2)
                    182:                n2 = add_node(s2);
                    183: 
                    184:        /*
                    185:         * could check to see if this arc is here already, but it isn't
                    186:         * worth the bother -- there usually isn't and it doesn't hurt if
                    187:         * there is (I think :-).
                    188:         */
                    189:        if (n1->n_narcs == n1->n_arcsize) {
                    190:                if (!n1->n_arcsize)
                    191:                        n1->n_arcsize = 10;
                    192:                bsize = n1->n_arcsize * sizeof(*n1->n_arcs) * 2;
                    193:                n1->n_arcs = (NODE **)grow_buf((char *)n1->n_arcs, bsize);
                    194:                n1->n_arcsize = bsize / sizeof(*n1->n_arcs);
                    195:        }
                    196:        n1->n_arcs[n1->n_narcs++] = n2;
                    197:        ++n2->n_refcnt;
                    198: }
                    199: 
                    200: hash_string(s)
                    201:        char *s;
                    202: {
                    203:        register int hash, i;
                    204: 
                    205:        for (hash = 0, i = 1; *s; s++, i++)
                    206:                hash += *s * i;
                    207:        return(hash % HASHSIZE);
                    208: }
                    209: 
                    210: /*
                    211:  * find a node in the graph and return a pointer to it - returns null if not
                    212:  * found.
                    213:  */
                    214: NODE *
                    215: find_node(name)
                    216:        char *name;
                    217: {
                    218:        register NODE *n;
                    219: 
                    220:        for (n = hashtable[hash_string(name)]; n; n = n->n_hash)
                    221:                if (!strcmp(n->n_name, name))
                    222:                        return(n);
                    223:        return((NODE *)NULL);
                    224: }
                    225: 
                    226: /* Add a node to the graph and return a pointer to it. */
                    227: NODE *
                    228: add_node(name)
                    229:        char *name;
                    230: {
                    231:        register NODE *n;
                    232:        int hash;
                    233: 
                    234:        if (!(n = (NODE *)malloc(sizeof(NODE))) || !(n->n_name = strdup(name)))
                    235:                no_memory();
                    236: 
                    237:        n->n_narcs = 0;
                    238:        n->n_arcsize = 0;
                    239:        n->n_arcs = (NODE **)NULL;
                    240:        n->n_refcnt = 0;
                    241:        n->n_flags = 0;
                    242: 
                    243:        /* add to linked list */
                    244:        if (n->n_next = graph)
                    245:                graph->n_prevp = &n->n_next;
                    246:        n->n_prevp = &graph;
                    247:        graph = n;
                    248: 
                    249:        /* add to hash table */
                    250:        hash = hash_string(name);
                    251:        n->n_hash = hashtable[hash];
                    252:        hashtable[hash] = n;
                    253:        return(n);
                    254: }
                    255: 
                    256: /* do topological sort on graph */
                    257: void
                    258: tsort()
                    259: {
                    260:        register NODE *n, *next;
                    261:        register int cnt;
                    262: 
                    263:        while (graph) {
                    264:                /*
                    265:                 * keep getting rid of simple cases until there are none left,
                    266:                 * if there are any nodes still in the graph, then there is
                    267:                 * a cycle in it.
                    268:                 */
                    269:                do {
                    270:                        for (cnt = 0, n = graph; n; n = next) {
                    271:                                next = n->n_next;
                    272:                                if (n->n_refcnt == 0) {
                    273:                                        remove_node(n);
                    274:                                        ++cnt;
                    275:                                }
                    276:                        }
                    277:                } while (graph && cnt);
                    278: 
                    279:                if (!graph)
                    280:                        break;
                    281: 
                    282:                if (!cycle_buf) {
                    283:                        /*
                    284:                         * allocate space for two cycle logs - one to be used
                    285:                         * as scratch space, the other to save the longest
                    286:                         * cycle.
                    287:                         */
                    288:                        for (cnt = 0, n = graph; n; n = n->n_next)
                    289:                                ++cnt;
                    290:                        cycle_buf =
                    291:                            (NODE **)malloc((u_int)sizeof(NODE *) * cnt);
                    292:                        longest_cycle =
                    293:                            (NODE **)malloc((u_int)sizeof(NODE *) * cnt);
                    294:                        if (!cycle_buf || !longest_cycle)
                    295:                                no_memory();
                    296:                }
                    297:                for (n = graph; n; n = n->n_next)
                    298:                        if (!(n->n_flags & NF_ACYCLIC)) {
                    299:                                if (cnt = find_cycle(n, n, 0, 0)) {
                    300:                                        register int i;
                    301: 
                    302:                                        (void)fprintf(stderr,
                    303:                                            "tsort: cycle in data.\n");
                    304:                                        for (i = 0; i < cnt; i++)
                    305:                                                (void)fprintf(stderr,
                    306:                                "tsort: %s.\n", longest_cycle[i]->n_name);
                    307:                                        remove_node(n);
                    308:                                        break;
                    309:                                } else
                    310:                                        /* to avoid further checks */
                    311:                                        n->n_flags  = NF_ACYCLIC;
                    312:                        }
                    313: 
                    314:                if (!n) {
                    315:                        (void)fprintf(stderr,
                    316:                            "tsort: internal error -- could not find cycle.\n");
                    317:                        exit(1);
                    318:                }
                    319:        }
                    320: }
                    321: 
                    322: /* print node and remove from graph (does not actually free node) */
                    323: void
                    324: remove_node(n)
                    325:        register NODE *n;
                    326: {
                    327:        register NODE **np;
                    328:        register int i;
                    329: 
                    330:        (void)printf("%s\n", n->n_name);
                    331:        for (np = n->n_arcs, i = n->n_narcs; --i >= 0; np++)
                    332:                --(*np)->n_refcnt;
                    333:        n->n_narcs = 0;
                    334:        *n->n_prevp = n->n_next;
                    335:        if (n->n_next)
                    336:                n->n_next->n_prevp = n->n_prevp;
                    337: }
                    338: 
                    339: /* look for the longest cycle from node from to node to. */
                    340: find_cycle(from, to, longest_len, depth)
                    341:        NODE *from, *to;
                    342:        int depth, longest_len;
                    343: {
                    344:        register NODE **np;
                    345:        register int i, len;
                    346: 
                    347:        /*
                    348:         * avoid infinite loops and ignore portions of the graph known
                    349:         * to be acyclic
                    350:         */
                    351:        if (from->n_flags & (NF_MARK|NF_ACYCLIC))
                    352:                return(0);
                    353:        from->n_flags = NF_MARK;
                    354: 
                    355:        for (np = from->n_arcs, i = from->n_narcs; --i >= 0; np++) {
                    356:                cycle_buf[depth] = *np;
                    357:                if (*np == to) {
                    358:                        if (depth + 1 > longest_len) {
                    359:                                longest_len = depth + 1;
                    360:                                (void)memcpy((char *)longest_cycle,
                    361:                                    (char *)cycle_buf,
                    362:                                    longest_len * sizeof(NODE *));
                    363:                        }
                    364:                } else {
                    365:                        len = find_cycle(*np, to, longest_len, depth + 1);
                    366:                        if (len > longest_len)
                    367:                                longest_len = len;
                    368:                }
                    369:        }
                    370:        from->n_flags &= ~NF_MARK;
                    371:        return(longest_len);
                    372: }
                    373: 
                    374: void
                    375: no_memory()
                    376: {
                    377:        (void)fprintf(stderr, "tsort: %s.\n", strerror(ENOMEM));
                    378:        exit(1);
                    379: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.