theTavis

12 December 2010, 03:08 AM

Hi,

I am trying to optimize a function using qsort to sort a very large array of Point3 world coordinates. I found it was faster to precompute the values and store them in a parallel array with a for-collect loop. My question is: using qsort, how do I reference these values to sort the Point3 array directly? My current work-around requires two additional for-collect loops as in this example:

valueArr = #() -- declare here so it can be seen by functions

fn sortFn a b = if valueArr[a] > valueArr[b] then 1 else -1

fn sortPoints pointArr =

(

indexArr = for i = 1 to pointArr.count collect i -- collect the indices, these are what we sort

valueArr = for p in pointArr collect (cos p.x + sin p.y) -- compute a value to sort by

qsort indexArr sortFn -- sort the indices

for i = 1 to pointArr.count collect pointArr[indexArr[i]] -- return the points sorted by index

)

This works fine, but for large point counts it would be faster without the first and last for-collect loops. Is it possible to do something like this:

valueArr = #() -- declare here so it can be seen by functions

-- here's the part I don't know how to do: getting the index of a or b

fn sortFn a b = if valueArr[(index of a)] > valueArr[(index of b)] then 1 else -1

fn sortPoints pointArr =

(

valueArr = for p in pArr collect (cos p.x + sin p.y) -- compute some value to sort by

qsort pointArr sortFn -- sort the actual point array

)

any help is appreciated. Thanks!

I am trying to optimize a function using qsort to sort a very large array of Point3 world coordinates. I found it was faster to precompute the values and store them in a parallel array with a for-collect loop. My question is: using qsort, how do I reference these values to sort the Point3 array directly? My current work-around requires two additional for-collect loops as in this example:

valueArr = #() -- declare here so it can be seen by functions

fn sortFn a b = if valueArr[a] > valueArr[b] then 1 else -1

fn sortPoints pointArr =

(

indexArr = for i = 1 to pointArr.count collect i -- collect the indices, these are what we sort

valueArr = for p in pointArr collect (cos p.x + sin p.y) -- compute a value to sort by

qsort indexArr sortFn -- sort the indices

for i = 1 to pointArr.count collect pointArr[indexArr[i]] -- return the points sorted by index

)

This works fine, but for large point counts it would be faster without the first and last for-collect loops. Is it possible to do something like this:

valueArr = #() -- declare here so it can be seen by functions

-- here's the part I don't know how to do: getting the index of a or b

fn sortFn a b = if valueArr[(index of a)] > valueArr[(index of b)] then 1 else -1

fn sortPoints pointArr =

(

valueArr = for p in pArr collect (cos p.x + sin p.y) -- compute some value to sort by

qsort pointArr sortFn -- sort the actual point array

)

any help is appreciated. Thanks!