Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Added options to lsort (-unique, -count and -stride)

Closed / Fixed218999ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 26 years ago
Descriptionplain2000-10-26 01:09:10
OriginalBugID: 3758 RFE
Version: 8.2.2
SubmitDate: '1999-12-02'
LastModified: '1999-12-20'
Severity: MED
Status: Released
Submitter: techsupp
ChangedBy: hobbs
RelatedBugIDs: 2812
OS: Solaris
OSVersion: Solaris 2.6
Machine: Intel architecture
FixedDate: '1999-12-20'
FixedInVersion: 8.3b1
ClosedDate: '2000-10-25'


Name:

Ted Dunning


CVS:

Thu Dec  2 12:28:02 PST 1999


Extensions:

None


CustomShell:

None other than the patch described here.


DesiredBehavior:

I have added several options to lsort and have updated the man page and test suite to match.


These options include -unique and -count options which eliminate duplicate items from the output of lsort and optionally count these duplicates.


Also, I have added a -stride argument which allows a flat list to be treated as if it has a list of lists structure.  This means that lists in the style normally used by foreach can be passed to lsort without building a temporary copy which is in the normally required list of lists structure.  This allows, for example, the output of array get to be sorted easily.  For instance, if we wanted to sort the contents of an array by the value in the array, we could use


    lsort -stride 2 [array get array]


This is considerably more efficient than the alternative form:


    foreach {key value} [array get array] {

       lappend tmp [list $key $value]

    }

    lsort $tmp


Patch:

? unix/configure

? unix/config.log

? unix/config.cache

? unix/config.status

? unix/Makefile

? unix/tclConfig.sh

? unix/tclsh

? unix/tcltest

? unix/dltest/configure

? unix/dltest/config.log

? unix/dltest/config.cache

? unix/dltest/config.status

? unix/dltest/Makefile

Index: doc/lsort.n

===================================================================

RCS file: /cvsroot/tcl/doc/lsort.n,v

retrieving revision 1.4

diff -r1.4 lsort.n

26c26,27

< n) performance characteristics.

---

> n) performance characteristics.  Options allow the sort to eliminate

> duplicates and optionally return counts of duplicated items. 

83a85,119

> .RE

> .TP 20

> \fB\-stride\0\fIsize\fR

> If this option is specified, then elements of the list will be

> collected into sub-lists each of which is \fIsize\fR long.  This

> allows a flat list to be treated as if it were instead made up of

> sub-lists each \fIsize\fR in length.  This option is often

> useful in conjunction with the \fB-index\fR option.

> For example,

> .RS

> .CS

> lsort -stride 2 -integer -index 1 {First 24 Second 18 Third 30}

> .CE

> returns \fB{Second 18} {First 24} {Third 30}\fR even though the

> original list has no substructure.  This option is more efficient than

> using a foreach loop to construct a list of lists to pass to lsort.

> .RE

> .TP 20

> \fB\-unique\0

> If this option is specified, then only the last of a set of duplicate

> elements found in the list will be retained.  Note that duplicates are

> only determined relative to the comparison used in the sort.  Thus if 

> \fI-index 0\fR is used, \fB{1 a}\fR and \fB{1 b}\fR would be

> considered duplicates and only the second element, \fB{1 b}\fR, would

> be retained. 

> .TP 20

> \fB\-count\0

> This option is similar to the \fI-unique\fR option except that a count

> of the number of duplicate items is kept.  For example,

> .RS

> .CS

> lsort {a b c d a b c a b a}

> .CE

> returns \fB{a 4 b 3 c 2 d 1}\fR.  The rule about how

> duplicates are detected is the same as with the \fI-unique\fR option.

Index: generic/tclCmdIL.c

===================================================================

RCS file: /cvsroot/tcl/generic/tclCmdIL.c,v

retrieving revision 1.15.4.1

diff -r1.15.4.1 tclCmdIL.c

2430a2431

>     int stride, uniqueFlag, countFlag;

2441,2442c2442,2444

<     {"-ascii", "-command", "-decreasing", "-dictionary",

<     "-increasing", "-index", "-integer", "-real", (char *) NULL};

---

>     {"-ascii", "-command", "-count", "-decreasing", "-dictionary",

>      "-increasing", "-index", "-integer", "-real", "-stride",

>      "-unique", (char *) NULL};

2453a2456,2459

>     stride = 0;

>     uniqueFlag = 0;

>     countFlag = 0;

> 

2462c2468

< != TCL_OK) {

---

>     != TCL_OK) {

2466,2508c2472,2540

<     case 0:/* -ascii */

< sortInfo.sortMode = SORTMODE_ASCII;

< break;

<     case 1:/* -command */

< if (i == (objc-2)) {

<     Tcl_AppendToObj(resultPtr,

<     "\"-command\" option must be followed by comparison command",

<     -1);

<     return TCL_ERROR;

< }

< sortInfo.sortMode = SORTMODE_COMMAND;

< cmdPtr = objv[i+1];

< i++;

< break;

<     case 2:/* -decreasing */

< sortInfo.isIncreasing = 0;

< break;

<     case 3:/* -dictionary */

< sortInfo.sortMode = SORTMODE_DICTIONARY;

< break;

<     case 4:/* -increasing */

< sortInfo.isIncreasing = 1;

< break;

<     case 5:/* -index */

< if (i == (objc-2)) {

<     Tcl_AppendToObj(resultPtr,

<     "\"-index\" option must be followed by list index",

<     -1);

<     return TCL_ERROR;

< }

< if (TclGetIntForIndex(interp, objv[i+1], -2, &sortInfo.index)

< != TCL_OK) {

<     return TCL_ERROR;

< }

< cmdPtr = objv[i+1];

< i++;

< break;

<     case 6:/* -integer */

< sortInfo.sortMode = SORTMODE_INTEGER;

< break;

<     case 7:/* -real */

< sortInfo.sortMode = SORTMODE_REAL;

< break;

---

> case 0:/* -ascii */

>     sortInfo.sortMode = SORTMODE_ASCII;

>     break;

> case 1:/* -command */

>     if (i == (objc-2)) {

> Tcl_AppendToObj(resultPtr,

> "\"-command\" option must be followed by comparison command",

> -1);

> return TCL_ERROR;

>     }

>     sortInfo.sortMode = SORTMODE_COMMAND;

>     cmdPtr = objv[i+1];

>     i++;

>     break;

> case 2:/* -count */

>     countFlag = 1;

>     break;

> case 3:/* -decreasing */

>     sortInfo.isIncreasing = 0;

>     break;

> case 4:/* -dictionary */

>     sortInfo.sortMode = SORTMODE_DICTIONARY;

>     break;

> case 5:/* -increasing */

>     sortInfo.isIncreasing = 1;

>     break;

> case 6:/* -index */

>     if (i == (objc-2)) {

> Tcl_AppendToObj(resultPtr,

> "\"-index\" option must be followed by list index",

> -1);

> return TCL_ERROR;

>     }

>     if (TclGetIntForIndex(interp, objv[i+1], -2, &sortInfo.index)

> != TCL_OK) {

> return TCL_ERROR;

>     }

>     cmdPtr = objv[i+1];

>     i++;

>     break;

> case 7:/* -integer */

>     sortInfo.sortMode = SORTMODE_INTEGER;

>     break;

> case 8:/* -real */

>     sortInfo.sortMode = SORTMODE_REAL;

>     break;

> case 9:/* -stride */

>     if (i == (objc-2)) {

> Tcl_AppendToObj(resultPtr,

> "\"-stride\" option must be followed by stride length",

> -1);

> return TCL_ERROR;

>     }

>     if (TclGetIntForIndex(interp, objv[i+1], -2, &stride)

> != TCL_OK) {

> return TCL_ERROR;

>     }

>     if (stride <= 0) {

> Tcl_AppendToObj(resultPtr,

> "\"-stride\" option must have positive integer value", -1);

> return TCL_ERROR;

>     }

>     cmdPtr = objv[i+1];

>     i++;

>     break;

>     

> case 10:/* -unique */

>     uniqueFlag = 1;

>     break;

2521c2553

<         != TCL_OK) {

---

>     != TCL_OK) {

2530c2562

<     &length, &listObjPtrs);

---

>  &length, &listObjPtrs);

2537,2540c2569,2590

<     elementArray = (SortElement *) ckalloc(length * sizeof(SortElement));

<     for (i=0; i < length; i++){

< elementArray[i].objPtr = listObjPtrs[i];

< elementArray[i].nextPtr = &elementArray[i+1];

---

>     if (stride == 0) {

> elementArray = (SortElement *) ckalloc(length * sizeof(SortElement));

> for (i=0; i < length; i++){

>     elementArray[i].objPtr = listObjPtrs[i];

>     elementArray[i].nextPtr = &elementArray[i+1];

> }

> elementArray[length-1].nextPtr = NULL;

>     }

>     else {

> int j, n;

> n = (length+stride-1)/stride;

> elementArray = (SortElement *) ckalloc(n * sizeof(SortElement));

> for (i=j=0; i < length; i+=stride,j++) {

>     if (i+stride <= length) {

> elementArray[j].objPtr = Tcl_NewListObj(stride, listObjPtrs+i);

>     }

>     else {

> elementArray[j].objPtr = Tcl_NewListObj(length-i, listObjPtrs+i);

>     }

>     elementArray[j].nextPtr = &elementArray[j+1];

> }

> elementArray[n-1].nextPtr = NULL;

2542c2592

<     elementArray[length-1].nextPtr = NULL;

---

> 

2544a2595,2597

> Tcl_Obj *last;

> int cnt;

> 

2552,2553c2605,2623

< for (; elementPtr != NULL; elementPtr = elementPtr->nextPtr){

<     Tcl_ListObjAppendElement(interp, resultPtr, elementPtr->objPtr);

---

> while (elementPtr != NULL) {

>     if (uniqueFlag || countFlag) {

> last = elementPtr->objPtr;

> cnt = 0;

> while (elementPtr != NULL && SortCompare(last, elementPtr->objPtr, &sortInfo) == 0) {

>     cnt++;

>     last = elementPtr->objPtr;

>     elementPtr = elementPtr->nextPtr;

> }

> Tcl_ListObjAppendElement(interp, resultPtr, last);

> if (countFlag) {

>     Tcl_ListObjAppendElement(interp, resultPtr,

>      Tcl_NewIntObj(cnt));

> }

>     }

>     else {

> Tcl_ListObjAppendElement(interp, resultPtr, elementPtr->objPtr);

> elementPtr = elementPtr->nextPtr;

>     }

2558c2628

<     done:

---

> done:

Index: tests/cmdIL.test

===================================================================

RCS file: /cvsroot/tcl/tests/cmdIL.test,v

retrieving revision 1.9

diff -r1.9 cmdIL.test

23c23

< } {1 {bad option "-foo": must be -ascii, -command, -decreasing, -dictionary, -increasing, -index, -integer, or -real}}

---

> } {1 {bad option "-foo": must be -ascii, -command, -count, -decreasing, -dictionary, -increasing, -index, -integer, -real, -stride, or -unique}}

77a78,98

> test cmdIL-1.20 {Tcl_LsortObjCmd procedure, invalid stride} {

>     list [catch {lsort -stride foo} msg] $msg

> } {1 {"-stride" option must be followed by stride length}}

> test cmdIL-1.21 {Tcl_LsortObjCmd procedure, invalid stride} {

>     list [catch {lsort -stride -1} msg] $msg

> } {1 {"-stride" option must be followed by stride length}}

> test cmdIL-1.22 {Tcl_LsortObjCmd procedure, unique sort} {

>     lsort -integer -unique {3 1 2 3 1 4 3}

> } {1 2 3 4}

> test cmdIL-1.23 {Tcl_LsortObjCmd procedure, counted sort} {

>     lsort -count {a b c b a c d a}

> } {a 3 b 2 c 2 d 1}

> test cmdIL-1.24 {Tcl_LsortObjCmd procedure, sort with stride} {

>     lsort -stride 2 -index 0 -integer {2 a 1 b 3 c 1 a}

> } {{1 b} {1 a} {2 a} {3 c}}

> test cmdIL-1.24 {Tcl_LsortObjCmd procedure, sort uniquely with stride} {

>     lsort -unique -stride 2 -index 0 -integer {2 a 1 b 3 c 1 a}

> } {{1 a} {2 a} {3 c}}

> test cmdIL-1.24 {Tcl_LsortObjCmd procedure, sort with count with stride} {

>     lsort -count -stride 2 -index 0 -integer {2 a 1 b 3 c 1 a}

> } {{1 a} 2 {2 a} 1 {3 c} 1}


PatchFiles:

tcl_CmdIL.c

    lsort.n

    cmdIL-test.test




#2812 has a patch for just -unique 
-- 12/03/1999 hobbs
8.3b1 will have an 'lsort -unique' implementation (a really fast
one).  It might be interesting to see -count and -stride based
off the new fast implementation, but otherwise they seem a bit
eccentric for core.  The new listx command may find an appropriate
place for them. 
-- 12/20/1999 hobbs
welch Closed ticket [218999ffff]. [History diff]2000-10-26 08:33:08
dgp Open ticket [218999ffff]. [History diff]2001-04-05 07:49:31
dgp Closed ticket [218999ffff]. [History diff]2001-04-05 07:51:26
dgp Ticket [218999ffff]: 3 changes [History diff]2001-04-05 07:51:27
Log in (anonymous is fine) to add a comment.