Skip to content
Tcl Source Code View Ticket
Login

View Ticket

lsort -unique flag

Closed / Fixed217912ffffffffffffffffffffffffffffffffffCreated 26 years agoUpdated 26 years ago
Descriptionplain2000-10-26 00:45:51
OriginalBugID: 2812 RFE
Version: 8.2
SubmitDate: '1999-09-16'
LastModified: '1999-12-15'
Severity: MED
Status: Closed
Submitter: techsupp
ChangedBy: hobbs
RelatedBugIDs: 3758
OS: Solaris
OSVersion: 2.7
Machine: Other
FixedDate: '2000-10-25'
ClosedDate: '1999-12-15'


Name:

Michael Karo



CustomShell:

See above patch. There are no known conflicts.



Comments:

The above patch has been provided in hopes that this functionality will

    appear as part of the Tcl core as soon as possible.



DesiredBehavior:

The lsort command does not currently have a means of sorting lists while

    eliminating redundant entries. This capability is present in the UNIX

    sort(1) command by specifying the -u flag. Although a workaround can be

    created in a Tcl script to iterate over a list and remove redundant

    entries,

    it is an inefficient solution for very large lists.



Patch:

*** tclCmdIL.c.old      Wed Sep 15 13:00:46 1999

--- tclCmdIL.c.new      Wed Sep 15 13:03:41 1999

***************

*** 44,49 ****

--- 44,51 ----

  

  typedef struct SortInfo {

      int isIncreasing;         /* Nonzero means sort in increasing order. */

+     int isUnique;             /* Nonzero means supress all but one of

+                                * each list element having equal keys. */

      int sortMode;             /* The sort mode.  One of SORTMODE_*

                                 * values defined below */

      Tcl_Obj *compareCmdPtr;     /* The Tcl comparison command when sortMode

***************

*** 2435,2441 ****

                                           * comparison function */

      static char *switches[] =

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

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

  

      resultPtr = Tcl_GetObjResult(interp);

      if (objc < 2) {

--- 2437,2444 ----

                                           * comparison function */

      static char *switches[] =

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

!           "-increasing", "-index", "-integer", "-real", "-unique",

!           (char *) NULL};

  

      resultPtr = Tcl_GetObjResult(interp);

      if (objc < 2) {

***************

*** 2448,2453 ****

--- 2451,2457 ----

       */

  

      sortInfo.isIncreasing = 1;

+     sortInfo.isUnique = 0;

      sortInfo.sortMode = SORTMODE_ASCII;

      sortInfo.index = -1;

      sortInfo.interp = interp;

***************

*** 2502,2507 ****

--- 2506,2514 ----

            case 7:                     /* -real */

                sortInfo.sortMode = SORTMODE_REAL;

                break;

+           case 8:                     /* -unique */

+               sortInfo.isUnique = 1;

+               break;

        }

      }

      if (sortInfo.sortMode == SORTMODE_COMMAND) {

***************

*** 2662,2667 ****

--- 2669,2687 ----

      }

      headPtr = tailPtr;

      while ((leftPtr != NULL) && (rightPtr != NULL)) {

+       if (infoPtr->isUnique != 0) {

+           while ((leftPtr != NULL) && (SortCompare(tailPtr->objPtr,

+                   leftPtr->objPtr, infoPtr) == 0)) {

+               leftPtr = leftPtr->nextPtr;

+           }

+           while ((rightPtr != NULL) && (SortCompare(tailPtr->objPtr,

+                   rightPtr->objPtr, infoPtr) == 0)) {

+               rightPtr = rightPtr->nextPtr;

+           }

+           if ((leftPtr == NULL) || (rightPtr == NULL)) {

+               break;

+           }

+       }

        if (SortCompare(leftPtr->objPtr, rightPtr->objPtr, infoPtr) > 0) {

            tailPtr->nextPtr = rightPtr;

            tailPtr = rightPtr;

***************

*** 2673,2681 ****

        }

      }

      if (leftPtr != NULL) {

!        tailPtr->nextPtr = leftPtr;

      } else {

!        tailPtr->nextPtr = rightPtr;

      }

      return headPtr;

  }

--- 2693,2723 ----

        }

      }

      if (leftPtr != NULL) {

!       if (infoPtr->isUnique != 0) {

!           while (leftPtr != NULL) {

!               if (SortCompare(tailPtr->objPtr, leftPtr->objPtr,

!                       infoPtr) != 0) {

!                   tailPtr->nextPtr = leftPtr;

!                   tailPtr = leftPtr;

!               }

!               leftPtr = leftPtr->nextPtr;

!           }

!       } else {

!           tailPtr->nextPtr = leftPtr;

!       }

      } else {

!       if (infoPtr->isUnique != 0) {

!           while (rightPtr != NULL) {

!               if (SortCompare(tailPtr->objPtr, rightPtr->objPtr,

!                       infoPtr) != 0) {

!                   tailPtr->nextPtr = rightPtr;

!                   tailPtr = rightPtr;

!               }

!               rightPtr = rightPtr->nextPtr;

!           }

!       } else {

!           tailPtr->nextPtr = rightPtr;

!       }

      }

      return headPtr;

  }



PatchFiles:

generic/tclCmdIL.c





Would be a good new feature.  Must be tested. 
-- 09/18/1999 hobbs
Through the magic of the byte code compiler, this is actually
slower than:

# lunique --
#   order independent list unique proc.  most efficient, but requires
#   __LIST never be an element of the input list
# Arguments:
#   __LISTlist of items to make unique
# Returns:
#   list of only unique items, order not defined
#
;proc lunique __LIST {
    if {[llength $__LIST]} {
foreach $__LIST $__LIST break
unset __LIST
return [info locals]
    }
}

lsort [lunique $list]

Still working on something though, because it'd be nice. 
-- 12/15/1999 hobbs
welch Closed ticket [217912ffff]. [History diff]2000-10-26 08:09:12
dgp Open ticket [217912ffff]. [History diff]2001-04-14 02:12:13
dgp Closed ticket [217912ffff]. [History diff]2001-04-14 02:12:47
Log in (anonymous is fine) to add a comment.