Search the Community
Showing results for tags 'stack'.
-
This is a simple implementation of two stacks, one of FIFO type and the other of LIFO type. Both are based on the script.dictionary object and are self contained in two functions. If you call the function by passing an element as a parameter, that element is pushed on the stack, if you call the function without parameters, an element is retrieved (and removed) from the stack. both functions are used in the same way, the only difference is the order in which the data is returned, that is First In First Out for the _FIFO() function Last In First Out for the _LIFO() function here the two functions in its simplest form just to better see how they work. (see below for another version and example of use) Func _FIFO($vItem = '') Static Local $oFIFOStack = ObjCreate("Scripting.Dictionary") Static Local $iNDX = 1 Local $vPull = '' If $vItem = '' Then ; no Item to push, just pull from stack then If $oFIFOStack.Count Then ; ensure stack is not empty $vPull = $oFIFOStack.Item($iNDX) ; get the first item still in stack $oFIFOStack.Remove($iNDX) ; remove item from stack $iNDX += 1 ; raise the bottom index Return SetError(0, $oFIFOStack.Count, $vPull) ; return item to caller Else Return SetError(1, 0, '') EndIf Else $oFIFOStack.ADD($oFIFOStack.Count + $iNDX, $vItem) ; Add item SetExtended($oFIFOStack.Count) EndIf EndFunc ;==>_FIFO Func _LIFO($vItem = '') Static Local $oLIFOStack = ObjCreate("Scripting.Dictionary") Local $vPull = '' If $vItem = '' Then ; no Item to stack, just pull from stack then If $oLIFOStack.Count Then ; ensure stack is not empty $vPull = $oLIFOStack.Item($oLIFOStack.Count) ; get the top item $oLIFOStack.Remove($oLIFOStack.Count) ; remove item from stack Return SetError(0, $oLIFOStack.Count, $vPull) ; return item to caller Else Return SetError(1, 0, '') EndIf Else $oLIFOStack.ADD($oLIFOStack.Count + 1, $vItem) ; Add item SetExtended($oLIFOStack.Count) EndIf EndFunc ;==>_LIFO for a more convenient way of use, here below, there is a version that allows a way to get the Count property, and to perform the RemoveAll method by the use of an optional second parameter. See function header for more infos Have fun ; Example of FIFO stack: data keeps same order ConsoleWrite('FIFO example' & @CRLF & 'Push Data' & @CRLF) For $i = 1 To 50 ConsoleWrite($i & ' ') _FIFO($i) ; push $i in the stack Next ConsoleWrite(@CRLF & 'Pull Data' & @CRLF) For $i = 1 To _FIFO('', 3) ; _FIFO('', 3) return Count ConsoleWrite(_FIFO() & ' ') ; pull data & print it Next ; Example of LIFO stack: data returned in reverted order ConsoleWrite(@CRLF & @CRLF & 'LIFO example' & @CRLF & 'Push Data' & @CRLF) For $i = 1 To 50 ConsoleWrite($i & ' ') _LIFO($i) ; push $i in the stack Next ConsoleWrite(@CRLF & 'Pull Data' & @CRLF) For $i = 1 To _LIFO('', 3) ; _LIFO('', 3) return Count ConsoleWrite(_LIFO() & ' ') ; pull data & print it Next ConsoleWrite(@CRLF & @CRLF) ; #FUNCTION# ==================================================================================================================== ; Name ..........: _FIFO ; Description ...: A simple implementation of a FIFO stack (can be used to pull or push data from/to the stack) ; ; Syntax ........: _FIFO([$vItem = ''[, $vServiceCall = False]]) ; Parameters ....: $vItem - [optional] A variant value. Default is ''. ; this parameter can be empty or filled with data and behaves like: ; - if empty: the function pulls and return data from the stack ; - if filled: the function push that data to te stack ; $vServiceCall - [optional] A KeyWord or a value. Default is False. ; this parameter allows to perform some internal "methods" ; Note: When this parameter is used the previous parameter $vItem is simply ignored. ; 'Default' or 1 : Reindex the Key Names starting from 1 (internal use) ; 'Null' or 2 : Remove all items from the stack ; 'True' or 3 : Returns only the number of elements stored in the stack (returns 0 if stack is empty) ; ; Return values .: - when the function is used to Push data: ; returns nothing ; set @extended with the new total number of elements in the stack ; - when the function is used to Pull data: ; returns the pulled data (and removes it from the stack) ; set @extended with the number of elements still in the stack. ; If you try to pull data from an empty stack you get an empty string and @error is set to 1 ; ; Author ........: Chimp (Gianni Addiego) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: ; =============================================================================================================================== Func _FIFO($vItem = '', $vServiceCall = False) Static Local $oFIFOStack = ObjCreate("Scripting.Dictionary") Static Local $iNDX = 1, $KEYWORD_DEFAULT = 1, $KEYWORD_NULL = 2, $KEYWORD_True = 3 Local $vPull = '' If $vServiceCall Or IsKeyword($vServiceCall) Then ; check second parameter Local $iService = IsKeyword($vServiceCall) ? IsKeyword($vServiceCall) : $vServiceCall ; Default = 1, Null = 2 If String($vServiceCall) = "True" Then $iService = $KEYWORD_True Switch $iService Case $KEYWORD_DEFAULT ; 1 or 'Default' Keyword was passed ; we use the Default Keyword to "normalize" all keys index starting from 1 If $oFIFOStack.Count Then Local $aKeys = $oFIFOStack.keys For $i = 0 To UBound($aKeys) - 1 $oFIFOStack.key($aKeys[$i]) = $i + 1 Next $iNDX = 1 EndIf Return SetExtended($oFIFOStack.Count) Case $KEYWORD_NULL ; 2 or 'Null' Keyword was passed WARNING: the stack is being emptied ; we use the Null KeyWord or value 2 to remove all items from the stack $oFIFOStack.RemoveAll Return SetExtended($oFIFOStack.Count) Case $KEYWORD_True ; 3 or 'True' Keyword was passed just return items count Return SetExtended($oFIFOStack.Count, $oFIFOStack.Count) Case Else ; Return SetError(0, $oFIFOStack.Count, $oFIFOStack.Count) Return SetError(1, $oFIFOStack.Count) EndSwitch EndIf If $vItem = '' Then ; no Item to stack, just pull from stack then If $oFIFOStack.Count Then ; ensure stack is not empty $vPull = $oFIFOStack.Item($iNDX) ; get the first item still in stack $oFIFOStack.Remove($iNDX) ; remove item from stack $iNDX += 1 ; raise the bottom index Return SetError(0, $oFIFOStack.Count, $vPull) ; return item to caller Else Return SetError(1, 0, '') EndIf Else $oFIFOStack.ADD($oFIFOStack.Count + $iNDX, $vItem) ; Add item SetExtended($oFIFOStack.Count) EndIf EndFunc ;==>_FIFO ; #FUNCTION# ==================================================================================================================== ; Name ..........: _LIFO ; Description ...: A simple implementation of a LIFO stack (can be used to pull or push data from/to the stack) ; ; Syntax ........: _LIFO([$vItem = ''[, $vServiceCall = False]]) ; Parameters ....: $vItem - [optional] A variant value. Default is ''. ; this parameter can be empty or filled with data and behaves like: ; - if empty: the function pulls and return data from the stack ; - if filled: the function push that data to te stack ; $vServiceCall - [optional] A KeyWord or a value. Default is False. ; this parameter allows to perform some internal "methods" ; Note: When this parameter is used the previous parameter $vItem is simply ignored. ; 'Null' or 2 : Remove all items from the stack ; 'True' or 3 : Return the number of elements stored in the stack (returns 0 if stack is empty) ; When this parameter is used the previous parameter $vItem is simply ignored. ; ; Return values .: - when the function is used to Push data: ; returns nothing ; set @extended with the new total number of elements in the stack ; - when the function is used to Pull data: ; returns the pulled data (and removes it from the stack) ; set @extended with the number of elements still in the stack. ; If you try to pull data from an empty stack you get an empty string and @error is set to 1 ; ; Author ........: Chimp (Gianni Addiego) ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: ; =============================================================================================================================== Func _LIFO($vItem = '', $vServiceCall = False) Static Local $oLIFOStack = ObjCreate("Scripting.Dictionary") Static Local $KEYWORD_DEFAULT = 1, $KEYWORD_NULL = 2, $KEYWORD_True = 3 Local $vPull = '' ; check if second parameter is used If $vServiceCall Or IsKeyword($vServiceCall) Then Local $iService = IsKeyword($vServiceCall) ? IsKeyword($vServiceCall) : $vServiceCall ; Default = 1, Null = 2 If String($vServiceCall) = "True" Then $iService = $KEYWORD_True ; True = 3 Switch $iService Case $KEYWORD_DEFAULT ; 1 or 'Default' Keyword was passed Return SetExtended($oLIFOStack.Count) Case $KEYWORD_NULL ; 2 or 'Null' Keyword was passed WARNING: the stack is being emptied ; we use the Null KeyWord or value 2 to remove all items from the stack $oLIFOStack.RemoveAll Return SetExtended($oLIFOStack.Count) Case $KEYWORD_True ; 3 or 'True' Keyword was passed just return items count Return SetExtended($oLIFOStack.Count, $oLIFOStack.Count) Case Else ; Return SetError(0, $oFIFOStack.Count, $oFIFOStack.Count) Return SetError(1, $oLIFOStack.Count) EndSwitch EndIf If $vItem = '' Then ; no Item to stack, just pull from stack then If $oLIFOStack.Count Then ; ensure stack is not empty $vPull = $oLIFOStack.Item($oLIFOStack.Count) ; get the top item $oLIFOStack.Remove($oLIFOStack.Count) ; remove item from stack Return SetError(0, $oLIFOStack.Count, $vPull) ; return item to caller Else Return SetError(1, 0, '') EndIf Else $oLIFOStack.ADD($oLIFOStack.Count + 1, $vItem) ; Add item SetExtended($oLIFOStack.Count) EndIf EndFunc ;==>_LIFO