Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/sort by default, use stable sort



details:   https://anonhg.NetBSD.org/src/rev/795cfb0c532b
branches:  trunk
changeset: 501868:795cfb0c532b
user:      jdolecek <jdolecek%NetBSD.org@localhost>
date:      Mon Jan 08 18:00:31 2001 +0000

description:
by default, use stable sort
add -S flag to switch to non-stable sort; for GNU sort compatibility,
provide -s flag too

diffstat:

 usr.bin/sort/append.c |   9 ++++++---
 usr.bin/sort/fsort.c  |  18 ++++++++++++------
 usr.bin/sort/sort.1   |   9 ++++++++-
 usr.bin/sort/sort.c   |  18 +++++++++++++++---
 usr.bin/sort/sort.h   |   3 ++-
 5 files changed, 43 insertions(+), 14 deletions(-)

diffs (169 lines):

diff -r 785e9af90f3b -r 795cfb0c532b usr.bin/sort/append.c
--- a/usr.bin/sort/append.c     Mon Jan 08 17:55:28 2001 +0000
+++ b/usr.bin/sort/append.c     Mon Jan 08 18:00:31 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: append.c,v 1.6 2000/10/17 15:22:57 jdolecek Exp $      */
+/*     $NetBSD: append.c,v 1.7 2001/01/08 18:00:31 jdolecek Exp $      */
 
 /*-
  * Copyright (c) 1993
@@ -39,7 +39,7 @@
 #include "sort.h"
 
 #ifndef lint
-__RCSID("$NetBSD: append.c,v 1.6 2000/10/17 15:22:57 jdolecek Exp $");
+__RCSID("$NetBSD: append.c,v 1.7 2001/01/08 18:00:31 jdolecek Exp $");
 __SCCSID("@(#)append.c 8.1 (Berkeley) 6/6/93");
 #endif /* not lint */
 
@@ -51,7 +51,10 @@
                for (; ppos < cpos; ++ppos)                             \
                        *ppos -= odepth;                                \
                ppos -= n;                                              \
-               radixsort(ppos, n, wts1, REC_D);                        \
+               if (stable_sort)                                        \
+                       sradixsort(ppos, n, wts1, REC_D);               \
+               else                                                    \
+                       radixsort(ppos, n, wts1, REC_D);                \
                for (; ppos < cpos; ppos++) {                           \
                        prec = (const RECHEADER *) (*ppos - sizeof(TRECHEADER));\
                        put(prec, fp);                                  \
diff -r 785e9af90f3b -r 795cfb0c532b usr.bin/sort/fsort.c
--- a/usr.bin/sort/fsort.c      Mon Jan 08 17:55:28 2001 +0000
+++ b/usr.bin/sort/fsort.c      Mon Jan 08 18:00:31 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: fsort.c,v 1.6 2000/10/17 15:22:57 jdolecek Exp $       */
+/*     $NetBSD: fsort.c,v 1.7 2001/01/08 18:00:31 jdolecek Exp $       */
 
 /*-
  * Copyright (c) 1993
@@ -47,7 +47,7 @@
 #include "fsort.h"
 
 #ifndef lint
-__RCSID("$NetBSD: fsort.c,v 1.6 2000/10/17 15:22:57 jdolecek Exp $");
+__RCSID("$NetBSD: fsort.c,v 1.7 2001/01/08 18:00:31 jdolecek Exp $");
 __SCCSID("@(#)fsort.c  8.1 (Berkeley) 6/6/93");
 #endif /* not lint */
 
@@ -153,8 +153,11 @@
                        if (c == BUFFEND || ntfiles || mfct) {  /* push */
                                if (panic >= PANIC) {
                                        fstack[MAXFCT-16+mfct].fp = ftmp();
-                                       if (radixsort(keylist, nelem, weights,
-                                           REC_D))
+                                       if ((stable_sort)
+                                               ? sradixsort(keylist, nelem,
+                                                       weights, REC_D)
+                                               : radixsort(keylist, nelem,
+                                                       weights, REC_D) )
                                                err(2, NULL);
                                        append(keylist, nelem, depth, fstack[
                                         MAXFCT-16+mfct].fp, putrec, ftbl);
@@ -186,9 +189,12 @@
                }
                get = getnext;
                if (!ntfiles && !mfct) {        /* everything in memory--pop */
-                       if (nelem > 1)
-                          if (radixsort(keylist, nelem, weights, REC_D))
+                       if (nelem > 1) {
+                          if ((stable_sort)
+                               ? sradixsort(keylist, nelem, weights, REC_D)
+                               : radixsort(keylist, nelem, weights, REC_D) )
                                err(2, NULL);
+                       }
                        append(keylist, nelem, depth, outfp, putline, ftbl);
                        break;                                  /* pop */
                }
diff -r 785e9af90f3b -r 795cfb0c532b usr.bin/sort/sort.1
--- a/usr.bin/sort/sort.1       Mon Jan 08 17:55:28 2001 +0000
+++ b/usr.bin/sort/sort.1       Mon Jan 08 18:00:31 2001 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: sort.1,v 1.6 2000/11/07 06:43:35 lukem Exp $
+.\"    $NetBSD: sort.1,v 1.7 2001/01/08 18:00:31 jdolecek Exp $
 .\"
 .\" Copyright (c) 1991, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -135,6 +135,13 @@
 option.)
 .It Fl r
 Reverse the sense of comparisons.
+.It Fl S
+Don't use stable sort. Default is to use stable sort.
+.It Fl s
+Use stable sort. This is the default.
+Provided for compatiblity with GNU
+.Nm
+only.
 .It Fl H
 Use a merge sort instead of a radix sort.  This option should be
 used for files larger than 60Mb.
diff -r 785e9af90f3b -r 795cfb0c532b usr.bin/sort/sort.c
--- a/usr.bin/sort/sort.c       Mon Jan 08 17:55:28 2001 +0000
+++ b/usr.bin/sort/sort.c       Mon Jan 08 18:00:31 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: sort.c,v 1.8 2000/10/16 21:48:15 jdolecek Exp $        */
+/*     $NetBSD: sort.c,v 1.9 2001/01/08 18:00:31 jdolecek Exp $        */
 
 /*-
  * Copyright (c) 1993
@@ -51,7 +51,7 @@
 #endif /* not lint */
 
 #ifndef lint
-__RCSID("$NetBSD: sort.c,v 1.8 2000/10/16 21:48:15 jdolecek Exp $");
+__RCSID("$NetBSD: sort.c,v 1.9 2001/01/08 18:00:31 jdolecek Exp $");
 __SCCSID("@(#)sort.c   8.1 (Berkeley) 6/6/93");
 #endif /* not lint */
 
@@ -79,6 +79,11 @@
 extern struct coldesc clist[(ND+1)*2];
 extern int ncols;
 
+/*
+ * Default to stable sort.
+ */
+int stable_sort = 1;
+
 char devstdin[] = _PATH_STDIN;
 char toutpath[_POSIX_PATH_MAX];
 const char *tmpdir = _PATH_TMP;
@@ -111,7 +116,7 @@
        d_mask['\t'] = d_mask[' '] = BLANK | FLD_D;
        ftpos = fldtab;
        fixit(&argc, argv);
-       while ((ch = getopt(argc, argv, "bcdfik:mHno:rt:T:ux")) != EOF) {
+       while ((ch = getopt(argc, argv, "bcdfik:mHno:rsSt:T:ux")) != EOF) {
        if ((outfile = getenv("TMPDIR")))
                tmpdir = outfile;
        switch (ch) {
@@ -136,6 +141,13 @@
                case 'k':
                         setfield(optarg, ++ftpos, fldtab->flags);
                        break;
+               case 's':
+                       /* for GNU sort compatibility (this is our default) */
+                       stable_sort = 1;
+                       break;
+               case 'S':
+                       stable_sort = 0;
+                       break;
                case 't':
                        if (SEP_FLAG)
                                usage("multiple field delimiters");
diff -r 785e9af90f3b -r 795cfb0c532b usr.bin/sort/sort.h
--- a/usr.bin/sort/sort.h       Mon Jan 08 17:55:28 2001 +0000
+++ b/usr.bin/sort/sort.h       Mon Jan 08 18:00:31 2001 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: sort.h,v 1.5 2000/10/16 21:53:19 jdolecek Exp $        */
+/*     $NetBSD: sort.h,v 1.6 2001/01/08 18:00:31 jdolecek Exp $        */
 
 /*-
  * Copyright (c) 1993
@@ -142,5 +142,6 @@
 extern int SINGL_FLD, SEP_FLAG, UNIQUE;
 extern int REC_D;
 extern const char *tmpdir;
+extern int stable_sort;
 
 #include "extern.h"



Home | Main Index | Thread Index | Old Index