Reverse a list

Kay C Lan lan.kc.macmail at gmail.com
Mon Feb 9 04:46:49 EST 2015


On Mon, Feb 9, 2015 at 7:37 AM, Alex Tweedly <alex at tweedly.net> wrote:

> Wow. I can see why LC7 would be expected to be slower for this case - in
> the multi-byte Unicode world,
>

It just doesn't appear to be characters and bytes. I tried a slightly
different approach to Jacques, using brute force and lines, and get a
slightly faster response in 6.6.5 than Jacque, but considerably slower in
7.0.2 rc2. - see krevers(ta)

It should also be pointed out that all the results are WRONG! They all
leave a trailing CR which is easy enough to handle, BUT, if your field had
the last line "This is the last line" and the insertion point was at the
end of the line, then I would expect the first line of the reverse sort to
be "This is the last line" and that's what all the functions produce. But
if your last line was "This is the last line" and there was a CR and the
insertion point was sitting on the line below I would expect a reverse sort
to produce a blank line and the 2nd line would read "this is the last line".

I know this is an in built feature of LC and what it defines as a line, and
in most circumstances this is very convenient, but depending on what you're
trying to achieve the current solutions may not give the exact output you
require.

6.6.5

There are 1880 lines in tstart
There are now 9400 lines in tstart
revers(ta) took 350 ms
qrevers(ta) took 5 ms
Output OK
krevers(ta) took 328 ms
Output OK

7.0.2 rc2

There are 1880 lines in tstart
There are now 9396 lines in tstart
revers(ta) took 354 ms
qrevers(ta) took 542845 ms
Output OK
krevers(ta) took 2051 ms
Output OK

I only had a 1880 line piece of text at hand so I used a repeat loop to get
above 8000 lines which seemed the bar people were using to measure. The
reason there is 9400 <> 9396 is because the last test I did with 6.6.5 had
a cr at the end of the line so the insertion point was on the line below,
with 7.0.2 the insertion point was at the end of the line.

I've left a hard breakpoint at the end of the code so you can compare the
tops and bottoms of ta, tb, tc and tk.

Note the extra test to see of the first char of the original matches the
last char of the result and the last char of the original matches the first
char of the result. In my tests none were correct.

(watch for line wraps)

on mouseup
   put fld "fldout" into tstart

   put "There are " & the number of lines of tStart & " lines in tstart" &
cr into msg
   put tstart into ta
   repeat 4 times
      put tstart after ta
   end repeat
   put ta into tstart
   put "There are now " &  the number of lines of tStart & " lines in tstart"
& cr after msg

   put the millisecs into t1
   put revers(ta) into tb
   put "revers(ta) took " &  the millisecs - t1 & " ms" & CR after msg

   put tstart into ta
   put the millisecs into t1
   put qrevers(ta) into tc
   put "qrevers(ta) took " & the millisecs - t1 & " ms " & CR after msg

   if tb = tc then put "Output OK" & cr after msg
   if ((char 1 of ta = char -1 of tb) AND (char -1 of ta = char 1 of tb))
then put "revers(ta) is correct" & cr after msg
   if ((char 1 of ta = char -1 of tc) AND (char -1 of ta = char 1 of tc))
then put "krevers(ta) is correct" & cr after msg

   put the millisec into t1
   put krevers(ta) into tk
   put "krevers(ta) took " & the millisec - t1 & " ms" & cr after msg
   --breakpoint
   if tb = tk then
      put "Output OK"  & cr after msg
   else
      put "Fail!" & cr after msg
   end if
   if ((char 1 of ta = char -1 of tb) AND (char -1 of ta = char 1 of tb))
then put "revers(ta) is correct" & cr after msg
   if ((char 1 of ta = char -1 of tk) AND (char -1 of ta = char 1 of tk))
then put "krevers(ta) is correct" & cr after msg
   breakpoint
end mouseup

function revers p
   repeat for each line l  in p
      put L & CR before t
   end repeat
   return t
end revers

function qrevers p
   put p into t
   put the number of chars in t into tlen
   repeat for each line l in p
      put the number of chars in l into tl
      put l & cr into char (tLen-tl) to tLen of t
      subtract (tl+1) from tLen
   end repeat
   return t
end qrevers

function krevers p
   put "" into t
   repeat for each line l in p
      put l & cr & t into t
   end repeat
   return t
end krevers



More information about the Use-livecode mailing list