function for greatest object in list less than or equal to a value

dunbarx at aol.com dunbarx at aol.com
Fri Oct 16 14:02:34 EDT 2015


Geoff.


My original post:


on mouseUp 
  answer findItemLessThanIndex(fld 1,"5") 
end mouseUp 

function findItemLessThanIndex tData,tIndex 
   put comma & tIndex after tData 
   sort items of tData numeric 
   return  item itemOffset("5",tData) -1 of tData 
end findItemLessThanIndex 


Craig



-----Original Message-----
From: Geoff Canyon <gcanyon at gmail.com>
To: How to use LiveCode <use-livecode at lists.runrev.com>
Sent: Fri, Oct 16, 2015 12:04 pm
Subject: Re: function for greatest object in list less than or equal to a value


I couldn't find the original code for the offset version, but I took a shot
at
it and got the following results, with the repeat solution still faster
(in
6.7.3):

Run Count: 10

Test ID: 1 Looking for greatest value <
500000000
sort 0.920932 499998402
repeat 0.33892 499998402
PMB 0.337938
499998402
ByOffset 0.448632 499998402

Test ID: 2 Looking for greatest value
< 5
sort 0.914448
repeat 0.223951
PMB 0.213347
ByOffset 0.426248

Test ID:
3 Looking for greatest value < 999999995
sort 0.913603 999999959
repeat
0.439324 999999959
PMB 0.51457 999999959
ByOffset 0.46632 999999959


The
code:

on mouseUp
   repeat 1000000
      put random(1000000000),"" after
L
   end repeat

   put 500000000 into testcase[1]
   put 5 into
testcase[2]
   put 999999995 into testcase[3]

   put 10 into runCount

  
repeat with i = 1 to runCount
      repeat with testID = 1 to 3
        
repeat for each item testType in "sort,repeat,PMB,ByOffset"
            put i
&& testID && testType into fld 3
            put the long seconds into S
     
do format("put greatestLessThan%s(L,testcase[%s])
into
R[%s][%s]",testType,testID,testType,testID)
            add (the long
seconds - S) to T[testType][testID]
         end repeat
      end repeat
  
end repeat

   put "Run Count:" && runCount & cr & cr into fld 3
   repeat
with testID = 1 to 3
      put "Test ID:" && testID && "Looking for greatest
value <" &&
testcase[testID] & cr after fld 3
      repeat for each item
testType in "sort,repeat,PMB,ByOffset"
         put testType &&
T[testType][testID]/runCount &&
R[testType][testID] & cr after fld 3
      end
repeat
      put cr after fld 3
   end repeat

end mouseUp

function
greatestLessThanSort pList,V
   sort items of pList descending numeric
   sort
items of pList by each >= V
   return item 1 of pList
end
greatestLessThanSort

function greatestLessThanRepeat pList,V
   put empty
into R
   repeat for each item i in pList
      if i < V and i > R then put i
into R
   end repeat
   return R
end greatestLessThanRepeat

function
greatestLessThanPMB tList,maxVal
   repeat for each item i in tList
      if i
< maxVal then put i & comma after outList
   end repeat
   if outList is empty
then return empty else return max(outList)
end greatestLessThanPMB

function
greatestLessThanByOffset pList,V
   put "",V after pList
   sort items of
pList numeric
   return item (itemOffset(V,pList) - 1) of pList
end
greatestLessThanByOffset



On Wed, Oct 14, 2015 at 6:27 PM, Craig Newman
<dunbarx at aol.com> wrote:

> Hi.
>
> The param "3" would be added to the
list. The sorted list would be:
> "2,3,4". When the itemOffset finds the "3",
the item just before it would
> be "2". That is how it works. To find the item
just before itemOffset does.
>
> This is faster than any of the "repeat for
each..." variants I have seen
> here. The reason, as I stated earlier as an
uninformed opinion, is that the
> low-level "sort" routine beats the high-level
"repeat" routine.
>
> Craig
>
> Sent from my iPhone
>
> > On Oct 14, 2015,
at 2:25 PM, Dr. Hawkins <dochawk at gmail.com> wrote:
> >
> >> On Mon, Oct 12,
2015 at 6:35 AM, <dunbarx at aol.com> wrote:
> >>
> >> My gadget adds the index
in a parameter to the function. if it already
> >> exists in the list, the
addition is superfluous, but harmless.
> >
> > But if the search list is "2,
4", and the search value is 3, doesn't this
> > return "3" rather than 2?
>
>
> >
> > --
> > Dr. Richard E. Hawkins, Esq.
> > (702) 508-8462
> >
_______________________________________________
> > use-livecode mailing
list
> > use-livecode at lists.runrev.com
> > Please visit this url to subscribe,
unsubscribe and manage your
> subscription preferences:
> >
http://lists.runrev.com/mailman/listinfo/use-livecode
>
>
_______________________________________________
> use-livecode mailing list
>
use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe
and manage your
> subscription preferences:
>
http://lists.runrev.com/mailman/listinfo/use-livecode
>
_______________________________________________
use-livecode
mailing list
use-livecode at lists.runrev.com
Please visit this url to subscribe,
unsubscribe and manage your subscription
preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode

 



More information about the use-livecode mailing list