sehr einfacher Stapel
Verfasst: 11.07.2015 13:51
Da auch diese Lösung sehr einfach ist und ich mich damit nicht in Tips&Tricks traue formuliere ich es wieder einmal als Frage :
Hat jemand Verbesserungsvorschläge ?
Hat jemand Verbesserungsvorschläge ?
Code: Alles auswählen
; very simple LIFO (LastInFirstOut) based on fix array - no dynamic allocation
; e.g. for Thread-Pool
Global LIFO_Elements = 5, LIFO_Fields=42 ; Base1 because 0 is used by LIFO
Global Dim LIFO(LIFO_Elements,LIFO_Fields)
Macro LIFO_FirstAvailable : LIFO(0,0) : EndMacro
Macro LIFO_NexAvailable(x) : LIFO(x,0) : EndMacro
Macro LIFO_push(x) : LIFO_NexAvailable(x) = LIFO_FirstAvailable : LIFO_FirstAvailable = x : EndMacro
Macro LIFO_pop(x) : LIFO_FirstAvailable = LIFO_NexAvailable(x) : LIFO_NexAvailable(x) = 0 : EndMacro
; ->>>> LIFO_push(random but only poped) & LIFO_pop(only LIFO_FirstAvailable)
;- Test
#poped = 0
#pushed = 1
#LIFO_TestField = 1
Macro DebugLIFO
Debug Str(LIFO_FirstAvailable)+" "+Str(LIFO_NexAvailable(1))+" "+Str(LIFO_NexAvailable(2))+" "+Str(LIFO_NexAvailable(3))+" "+Str(LIFO_NexAvailable(4))+" "+Str(LIFO_NexAvailable(5))
EndMacro
For i = 1 To 3
Repeat : x = Random(5,1) : Until LIFO(x, #LIFO_TestField) = #poped
Debug "" : Debug "0 1 2 3 4 5 "+Str(x) : DebugLIFO
LIFO_push(x) : LIFO(x, #LIFO_TestField) = #pushed : DebugLIFO
Next
For i = 1 To 2
x = LIFO_FirstAvailable
Debug "" : Debug "0 1 2 3 4 5 -"+Str(x) : DebugLIFO
LIFO_pop(x) : LIFO(x, #LIFO_TestField) = #poped : DebugLIFO
Next
For i = 1 To 2
Repeat : x = Random(5,1) : Until LIFO(x, #LIFO_TestField) = #poped
Debug "" : Debug "0 1 2 3 4 5 "+Str(x) : DebugLIFO
LIFO_push(x) : LIFO(x, #LIFO_TestField) = #pushed : DebugLIFO
Next
For i = 1 To 3
x = LIFO_FirstAvailable
Debug "" : Debug "0 1 2 3 4 5 -"+Str(x) : DebugLIFO
LIFO_pop(x) : LIFO(x, #LIFO_TestField) = #poped : DebugLIFO
Next