| 02-12-2009, 03:13 AM | #1 |
Not sure what I'm doing wrong here, this puts the last element to every memeber of the stack for some reason. What it should do is add a new element to the stack and push the new order to the back: Here's my logic: start 4 3 2 1 Add Element 4 3 2 1 5 Switch first with last 5 3 2 1 4 Switch nth and nth-1 5 3 2 4 1 switch nth and nth-1 5 3 4 2 1 Done 5 4 3 2 1 However; it doesn't work like that? =o JASS:struct Orderstack integer norders = 0 Table orders endstruct function OrderStackAddInverse takes Orderstack p, integer order returns nothing local integer I = p.norders local integer temp if(p.norders == 0)then set p.orders[0] = order set p.norders = p.norders + 1 return endif set p.orders[p.norders] = order //Put the last element at the start of the stack set temp = p.orders[0] set p.orders[0] = p.orders[p.norders] set p.orders[p.norders] = temp //Swap all middle elements. loop set temp = p.orders[i] set p.orders[i] = p.orders[I-1] set p.orders[I-1] = temp exitwhen(I == 1) set I = I - 1 endloop set p.norders = p.norders + 1 endfunction This both adds and sorts the elements; I tried using this to avoid inverting them every time I remove an element from the stack. Just doesn't work though? =x |
| 02-12-2009, 04:42 AM | #2 |
Why not add to the back directly? Using a LinkedList would be your best bet. |
| 02-12-2009, 04:48 AM | #3 |
Actually that's a really good idea; as long as the order que is sufficiently long. However; I'd still like to know why this doesn't work. |
| 02-12-2009, 04:52 AM | #4 |
JASS:library Sort initializer Demo globals private constant real OUTPUT_SURVEY_TIME = 5.00 constant integer gINT_CountValue = 5 private integer array VALUE[gINT_CountValue] endglobals //! textmacro Sort takes SORTDIRECTION, RELATION function $SORTDIRECTION$ takes nothing returns nothing local integer INT_Index = 1 local integer INT_Index2 local integer INT_Index2Sub local integer INT_Temp loop exitwhen INT_Index == gINT_CountValue set INT_Index2 = INT_Index loop set INT_Index2Sub = INT_Index2 - 1 exitwhen INT_Index2 == 0 or VALUE[INT_Index2] $RELATION$= VALUE[INT_Index2Sub] set INT_Temp = VALUE[INT_Index2Sub] set VALUE[INT_Index2Sub] = VALUE[INT_Index2] set VALUE[INT_Index2] = INT_Temp set INT_Index2 = INT_Index2Sub endloop set INT_Index = INT_Index + 1 endloop endfunction //! endtextmacro //! runtextmacro Sort("GreatestToLowest", "<") //! runtextmacro Sort("LowestToGreatest", ">") function Demo takes nothing returns nothing local integer INT_Index = 0 call BJDebugMsg("==Greatest-to-Lowest==") call BJDebugMsg("INPUT:") loop exitwhen INT_Index == gINT_CountValue set VALUE[INT_Index] = GetRandomInt(0, 100) call BJDebugMsg(I2S(INT_Index + 1) + ") " + I2S(VALUE[INT_Index])) set INT_Index = INT_Index + 1 endloop call GreatestToLowest() call BJDebugMsg("OUTPUT:") set INT_Index = 0 loop exitwhen INT_Index == gINT_CountValue call BJDebugMsg(I2S(INT_Index + 1) + ") " + I2S(VALUE[INT_Index])) set INT_Index = INT_Index + 1 endloop call BJDebugMsg("Lowest-to-Greatest demonstration in " + R2S(OUTPUT_SURVEY_TIME) + " seconds.") call TriggerSleepAction(OUTPUT_SURVEY_TIME) call ClearTextMessages() call BJDebugMsg("==Lowest-to-Greatest==") call BJDebugMsg("INPUT:") set INT_Index = 0 loop exitwhen INT_Index == gINT_CountValue set VALUE[INT_Index] = GetRandomInt(0, 100) call BJDebugMsg(I2S(INT_Index + 1) + ") " + I2S(VALUE[INT_Index])) set INT_Index = INT_Index + 1 endloop call LowestToGreatest() call BJDebugMsg("OUTPUT:") set INT_Index = 0 loop exitwhen INT_Index == gINT_CountValue call BJDebugMsg(I2S(INT_Index + 1) + ") " + I2S(VALUE[INT_Index])) set INT_Index = INT_Index + 1 endloop endfunction endlibrary |
| 02-12-2009, 05:51 AM | #5 |
when norders == 1, you will switch and then switch back in the loop. Move the exitwhen to the start of the loop. |
| 02-12-2009, 06:35 AM | #6 | |
Quote:
Ahh I didin't notice that! Thank you; it works like clock work now! On a side note; I actually had the wrong procedure to pull the values out - so I guess I'm just a terrible person =x. JASS:function GetNext takes Table T, integer orders returns integer local integer temp = T[0] set t[0] = t[1] return t[0] endfunction Which was working with my old sorting algo. thanks you guys! I'll look into LL. |
