Sorting an array in spin
youngdave
Posts: 70
Dear All
* I have an array of word variables which starts out as Variables[noparse][[/noparse]Population_Size]. During processing, values of the word variables change, and some values drop below a critical level and become ‘dead’.
* I'd appreciate ideas in Spin on packing the array to the left each time a ‘dead’ value is found so that it contains only ‘live’ values, and keeping a track of (updating) ‘Population_Size’.
TIA David Young
Post Edited By Moderator (Bean (Hitt Consulting)) : 8/10/2009 11:59:52 AM GMT
* I have an array of word variables which starts out as Variables[noparse][[/noparse]Population_Size]. During processing, values of the word variables change, and some values drop below a critical level and become ‘dead’.
* I'd appreciate ideas in Spin on packing the array to the left each time a ‘dead’ value is found so that it contains only ‘live’ values, and keeping a track of (updating) ‘Population_Size’.
TIA David Young
Post Edited By Moderator (Bean (Hitt Consulting)) : 8/10/2009 11:59:52 AM GMT
Comments
BTW - it helps if you put a subject title on the forum thread. You can edit your post and write a title like, "need help on array processing" or something similar. This will help to attract various users and help to index the thread in the search engine.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Timothy D. Swieter, E.I.
www.brilldea.com - Prop Blade, LED Painter, RGB LEDs, 3.0" LCD Composite video display, eProto for SunSPOT
www.tdswieter.com
If the dead values are not needed for further processing (say .. all values are needed to calculate the each single value and processing is kind of round-based), then the processing method can simply use the wordmove instruction to move the rest of the array one element to the left (overwriting the dead value) and decrease the population size variable. Advantage here is that the move instructions are executed in PASM which will be faster than looping through the array in spin.
The previous post suggested to move the last element to the hole .. why not have 2 index-counters? One for the next element to process and one for the place where to store the result. In the beginning both are equal. If the·current element dies, you only increase the pointer to the next element. If the current element does not die, you store it at the place the result pointer points to and increase both. In the end the difference of the pointers show you how much values died and have to be subtracted from the population size.
This way the order can be kept.
Post Edited (MagIO2) : 8/10/2009 4:16:31 PM GMT
if that's not sounding clear, let me know, and I'll explain it a bit more.
Baggers.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
http://www.propgfx.co.uk/forum/·home of the PropGFX Lite
·
Thanks for the suggestions.
I've got it worked out.
BTW - it helps if you put a subject title on the forum thread. >>>>>>>>>>>Probably a stupid question, but how do I do this?
TIA David Young··
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Timothy D. Swieter, E.I.
www.brilldea.com - Prop Blade, LED Painter, RGB LEDs, 3.0" LCD Composite video display, eProto for SunSPOT
www.tdswieter.com