jaberwacky Posted October 20, 2010 Share Posted October 20, 2010 (edited) Here's a useless stack data structure using AutoItObject and the latest AutoIt beta. [update: March 05, 2015] -- Maps behind the scenes. Use maps as a stack. Stack to map. Bug fixes. [update: April 04, 2014] -- Uses ternary operators now. New method: ToString. Etc. [update: October 02, 2011] -- Two new functions and a bug fix.SimpleStack.au3 expandcollapse popup#include-once #include <Array.au3> #include "AutoItObject.au3" #Region ; SimpleStack Func SimpleStack() With _AutoItObject_Class() .AddMethod("Push", "SS_Push") .AddMethod("Pop", "SS_Pop") .AddMethod("Peek", "SS_Peek") .AddMethod("Length", "SS_Length") .AddMethod("Reset", "SS_Reset") .Addmethod("ArrayToStack", "SS_ArrayToStack") .Addmethod("MapToStack", "SS_MapToStack") .Addmethod("ToArray", "SS_ToArray") .Addmethod("ToMap", "SS_ToMap") .Addmethod("ToString", "SS_ToString") Local $mMap[] .AddProperty("Stack", $elscope_private, $mMap) .AddProperty("Size" , $elscope_private, 0) Return .Object EndWith EndFunc ; = Method ================================================================================ ; Name .....: SS_Push ; Syntax ...: $oSimpleStack.Push($data) ; Param(s) .: $data -- The value that you would like to store in the stack. ; Success ..: Stores a value on the top of the stack and returns the value. ; Fail .....: ; Desc. ....: Places an element on top of the stack and returns that element. ; ========================================================================================= Func SS_Push($this, Const $data) With $this Switch IsMap(.Stack) Case 1 Local $mStack[] $mStack = .Stack $mStack[.Size + 1] = $data .Stack = $mStack ;ConsoleWrite('- ' & IsMap($mStack) & @TAB & UBound(MapKeys(.Stack)) & @CRLF) Switch MapExists(.Stack, .Size + 1) Case True .Size += 1 Return .Peek() Case False Return SetError(2, 0, False) EndSwitch Case 0 Return SetError(1, 0, False) EndSwitch EndWith EndFunc ; = Method ================================================================================ ; Name .....: SS_Pop ; Syntax ...: $oSimpleStack.Pop() ; Param(s) .: none ; Success ..: Deletes an element from the top of the stack. Returns the element that was deleted. ; Fail .....: @error: 1 ; Desc. ....: ; ========================================================================================= Func SS_Pop($this) With $this Switch .Size Case True Local Const $peek = .Peek() Local $mStack[] $mStack = .Stack Switch MapRemove($mStack, .Size) Case 1 .Stack = $mStack .Size -= 1 Return $peek Case 0 Return SetError(2, 0, False) EndSwitch Case False Return SetError(1, 0, False) EndSwitch EndWith EndFunc ; = Method ================================================================================ ; Name .....: SS_Peek ; Syntax ...: $oSimpleStack.Peek() ; Param(s) .: none ; Success ..: Returns the data from the top of the stack leaving the stack state unchanged. ; Fail .....: @error: 1 ; Desc. ....: ; ========================================================================================= Func SS_Peek(Const $this) With $this Switch .Size Case True Switch IsMap(.Stack) Case 1 Return .Stack[.Size] Case 0 Return SetError(1, 0, Null) EndSwitch Case False Return SetError(1, 0, Null) EndSwitch EndWith EndFunc ; = Method ================================================================================ ; Name .....: SS_Length ; Syntax ...: $oSimpleStack.Length() ; Param(s) .: none ; Success ..: Returns the count of elements that have been pushed on to the stack. ; Fail .....: ; Desc. ....: ; ========================================================================================= Func SS_Length(Const $this) Return $this.Size EndFunc ; = Method ================================================================================ ; Name .....: SS_Reset ; Syntax ...: $oSimpleStack.Reset() ; Success ..: True & Sets the state of the stack to default. ; Desc. ....: ; ========================================================================================= Func SS_Reset($this) With $this .Stack = Null .Size = 0 EndWith Return True EndFunc ; = Method ================================================================================ ; Name .....: SS_ArrayToStack ; Syntax ...: $oSimpleStack.ArrayToStack($array) ; Param(s) .: $array -- the array to use to populate a stack structure ; Success ..: 1 ; Fail .....: 0 & @error: 1 -- $array is not an array ; Desc. ....: Takes the elements of an existing array and uses them to populate a stack structure. ; ========================================================================================= Func SS_ArrayToStack($this, Const $array) Switch IsArray($array) Case True Local Const $upbound = UBound($array) Local $mStack[] For $i = 1 To $upbound $mStack[$i] = $array[$i - 1] Next With $this .Stack = $mStack .Size = $upbound EndWith Case False Return SetError(1, 0, False) EndSwitch EndFunc ; = Method ================================================================================ ; Name .....: SS_MapToStack ; Syntax ...: $oSimpleStack.MapToStack($map) ; Param(s) .: $map -- the map to use to populate a stack structure ; Success ..: 1 ; Fail .....: 0 & @error: 1 -- $map is not an map ; Desc. ....: Takes the keys of an existing map and uses them to populate a stack structure. ; ========================================================================================= Func SS_MapToStack($this, Const $map) Switch IsMap($map) Case True Local Const $keys = MapKeys($map) Local Const $size = UBound($keys) Local $mStack[] For $i = 1 To $size $mStack[$i] = $map[$keys[$i - 1]] Next With $this .Stack = $mStack .Size = $size EndWith Case False Return SetError(1, 0, False) EndSwitch EndFunc ; = Method ================================================================================ ; Name .....: SS_ToArray ; Syntax ...: $oSimpleStack.ToArray() ; Success ..: Returns the current stack as an array. ; Fail .....: 0 | @error: 1 -- stack is empty ; Desc. ....: ; ========================================================================================= Func SS_ToArray(Const $this) With $this Local Const $size = .Size Switch $size Case True Local $array[$size] For $i = 1 To $size $array[$i - 1] = .Stack[$i] Next Return $array Case False Return SetError(1, 0, False) EndSwitch EndWith EndFunc ; = Method ================================================================================ ; Name .....: SS_ToMap ; Syntax ...: $oSimpleStack.ToMap() ; Success ..: Returns the current stack as an map. ; Fail .....: 0 | @error: 1 -- stack is empty ; Desc. ....: ; ========================================================================================= Func SS_ToMap(Const $this) With $this Switch .Size Case True Return .Stack Case False Return SetError(1, 0, False) EndSwitch EndWith EndFunc ; = Method ================================================================================ ; Name .....: SS_ToString ; Syntax ...: $oSimpleStack.ToString() ; Param(s) .: $seperator -- string to delimit the string ; Success ..: the contents of the stack as a space seperated string ; Fail .....: '' | @error: 1 -- stack is empty ; Desc. ....: ; ========================================================================================= Func SS_ToString(Const $this, Const $seperator = ' ') With $this Local Const $size = .Size If $size Then Local $string = '' For $i = $size To 1 Step -1 $string &= .Stack[$i] & $seperator Next Return $string Else Return SetError(1, 0, '') EndIf EndWith EndFunc #EndRegion Edited March 5, 2015 by jaberwacky qsek 1 Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
jaberwacky Posted October 3, 2011 Author Share Posted October 3, 2011 Ahh, been a while since I've posted in these good forums. How has everybody been? This update includes two new methods: ToArray and ArrayToStack. Also includes a bug fix in SS_Reset where the limit was not being reset to default. Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
Beege Posted October 3, 2011 Share Posted October 3, 2011 I like it. Good use of AutoitObject too. Nice job Assembly Code: fasmg . fasm . BmpSearch . Au3 Syntax Highlighter . Bounce Multithreading Example . IDispatchASMUDFs: Explorer Frame . ITaskBarList . Scrolling Line Graph . Tray Icon Bar Graph . Explorer Listview . Wiimote . WinSnap . Flicker Free Labels . iTunesPrograms: Ftp Explorer . Snipster . Network Meter . Resistance Calculator Link to comment Share on other sites More sharing options...
jaberwacky Posted October 3, 2011 Author Share Posted October 3, 2011 Thank you Beege. Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
jaberwacky Posted March 5, 2015 Author Share Posted March 5, 2015 Decided to update my useless stack structure to use maps behind the scenes. Can also now feed it a map and start using it like a stack. Other misc. bug fixes. Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now