Jump to content
Chimp

two simple 'standalone' FIFO and LIFO stacks

Recommended Posts

Posted (edited)

This is a simple implementation of two stacks, one of FIFO type and the other of LIFO type.

220px-FIFO-LIFO.svg.png

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

 

Edited by Chimp
script.dictionary link

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Bilgus
      Went looking for a FIFO queue there are a few examples here on the forum but I didn't like the complexit/lack of flexibility in what I found
      The Queue Ive implemented uses a single dimension array  set up as a circular buffer
      The position and Item Count are stored in an extra slot of the array inside a 'DllStruct'
      Convenience Functions are included
      FifoQueue_Create($iSlots) ;Initialize the Queue Func FifoQueue_($eFQ, ByRef $aQ) ;For Status Functions - (So you don't have to supply $vOut) Available operations:
      $eFQ_Create = 0, $eFQ_Push, $eFQ_Peek, $eFQ_Pop, $eFQ_IsEmpty, $eFQ_IsFull, $eFQ_Count, $eFQ_Empty, $eFQ_IsInit Example (With Debug function)
       
    • By Smitro
      Hi all,

      I'm in the middle of writing a print queue monitor. I would like to be able to run this on our print server to be able to track who's printing large amounts and reduce the effect on the environment.

      I've done a fair chunck of it, but I'm finding that it's causing a bit of a load on the CPU. On average the 'spoolsv.exe' process sits at about 15-20% constantly when nothing is happening. I'm wondering if there is a way I can reduce this.

      I'm using the following code to access the print queue.


      ; Connect to Print Queue $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2") While 1 $AllPrintJobs = $objWMIService.ExecQuery ("SELECT * FROM Win32_PrintJob") ; Run through Print Jobs For $PrintJob In $AllPrintJobs ; Process any new jobs here Next Sleep (50) WEnd
      I've found that I have to catch the job several times in the queue. If I don't I can miss the total number of pages as the jobs spools.

      Is there another way of me getting the same information? Does the information go to another location after the print queue?
×
×
  • Create New...