Over the years Nigel Garvey and others have published some scripts with very fast and sophisticated sorting algorithms.
Over the last few years I’ve had several tries at incorporating his 2010 combined QuickSort+InsertionSort script into my other works but have previously had to give it up due strange behaviour and lack of debugging time.
Now that I am retired and have more time and focus, I have identified the issue.
In 2015 I started using a script library which I implemented as follows:
use U : script “Utilities_1501.scpt”
The script that I copied from Nigel Garvey has a “qsort()” handler (of course), within that handler there is a Script “o”, and within that script several handlers that use the variable “u” and assign various things to it.
on qsort(theList, l, r) script o property cutoff : 10 -- The Quicksort will ignore ranges containing this number of items or fewer. property lst : theList on qsrt(l, r) -- The Quicksort handler. set pivot to my lst's item ((l + r) div 2) set i to l set j to r repeat until (i > j) set u to my lst's item i ----- ^ here is the problem
Once qsort() has run, the handlers in the Utilities Library script aren’t accessible any more, and for example executing U’s GetTick_Now() results in the bizarre error message:
“5298 doesn’t understand GetTick_Now()”
On careful reading, it seems that this use of use statement creates a property
The fix is simple, a “local U” statement at the beginning of qsort() - I took it two steps further.
on qsort(theList, l, r) local M, C, G, l, P, U -- hide my Script Library Designators from qsort() local cutoff, lst -- hide script o's properties from outside script o property cutoff : 10 -- The Quicksort will ignore ranges containing this number of items or fewer. property lst : theList on qsrt(l, r) -- The Quicksort handler. local pivot, i, j, u, w --protect the local variables set pivot to my lst's item ((l + r) div 2)