;==================================================================================================================================================================================== ; UDF Name: List.au3 ; ; Date: 2018-02-17, 10:52 ; Description: Simple udf to create System Collections as ArrayList and make multiple actions on them. ; ; Function(s): _ListCreate -> Creates a new list ; _ListCapacity -> Gets a list size in bytes ; _ListCount -> Gets items count in list ; _ListIsFixedSize -> Get bool if list if fixed size ; _ListIsReadOnly -> Get bool if list is read only ; _ListIsSynchronized -> Get bool if list is synchronized ; _ListGetItem -> Get item on index ; _ListSetItem -> Set item on index ; ; _ListAdd -> Add item at end of list ; _ListClear -> Remove all list items ; _ListClone -> Duplicate list in new var ; _ListContains -> Get bool if item is in list ; _ListGetHashCode -> Get hash code for list ; _ListGetRange -> Get list with items between indexs ; _ListIndexOf -> Get index of item ; _ListInsert -> Insert a new item on index ; _ListInsertRange -> Insert list into list on index ; _ListLastIndexOf -> Get index last of item ; _ListRemove -> Remove first found item ; _ListRemoveAt -> Remove item in index ; _ListRemoveRange -> Remove items between indexs ; _ListReverse -> Reverse all items in list ; _ListSetRange -> Set new value for items in range ; _ListSort -> Sort items in list (speed of reading) ; _ListToString -> Get list object name ; _ListTrimToSize -> Remove unused space in list ; ; Author(s): Ascer ;==================================================================================================================================================================================== #Region COM Event handler. Local $oMyError = ObjEvent("AutoIt.Error","MyErrFunc") ; run function for verify COM exceptions. Func MyErrFunc() Local $HexNumber = hex($oMyError.number,8) ConsoleWrite("+++ We intercepted a COM Error !" & @CRLF & @CRLF & _ "+++ err.description is: " & @TAB & $oMyError.description & @CRLF & _ "+++ err.windescription:" & @TAB & $oMyError.windescription & @CRLF & _ "+++ err.number is: " & @TAB & $HexNumber & @CRLF & _ "+++ err.lastdllerror is: " & @TAB & $oMyError.lastdllerror & @CRLF & _ "+++ err.scriptline is: " & @TAB & $oMyError.scriptline & @CRLF & _ "+++ err.source is: " & @TAB & $oMyError.source & @CRLF & _ "+++ err.helpfile is: " & @TAB & $oMyError.helpfile & @CRLF & _ "+++ err.helpcontext is: " & @TAB & $oMyError.helpcontext _ ) SetError(1) Endfunc #EndRegion #Region Properites. ;==================================================================================================================================================================================== ; Function: _ListCreate() ; ; Description: Creates a System.Collections.ArrayList with default count = 0. ; ; Parameter(s): None ; ; Return Value(s): On Success: ; Set @error to 0. All is OK. ; Returns list | New created Sys.ArrayList. ; On Failure: ; Set @error to 1. $aList is not object. ; Returns integer | 1. ; ; Author (s): Ascer ;==================================================================================================================================================================================== Func _ListCreate() Local $aList = ObjCreate("System.Collections.ArrayList") If Not IsObj($aList) Then Return SetError(1, 0, 1) Return SetError(0, 0, $aList) EndFunc ;==================================================================================================================================================================================== ; Function: _ListCapacity(ByRef $aList [, $iSet = Default]) ; ; Description: Get size of available space in System.ArrayList. ; ; Parameter(s): $aList - list | Working list. ; $iSet - integer | (Default = not used) Set a new capacity for ArrayList. Change size of bytes in VirtualMemory for $aList. ; ; Return Value(s): On Success: ; Set @error to 0. ; Returns integer | List.capacity = bytes in memory ; On Failure: ; Set @error to: ; 1. $aList is not object. ; 2. $iSet is not integer. ; Returns integer | 0. ; ; Author (s): Ascer ;==================================================================================================================================================================================== Func _ListCapacity(ByRef $aList, $iSet = Default) If Not IsObj($aList) Then Return SetError(1, 0, 0) If $iSet <> Default Then If Not IsInt($iSet) Then Return SetError(2, 0, 0) $aList.Capacity = $iSet EndIf Return SetError(0, 0, $aList.Capacity) EndFunc ;==================================================================================================================================================================================== ; Function: _ListCount(ByRef $aList) ; ; Description: Get amount of elements inside of System.ArrayList. ; ; Parameter(s): $aList - list | Working list. ; ; Return Value(s): On Success: ; Set @error to 0. ; Returns integer | List.count. ; On Failure: ; Set @error to 1. $aList is not object. ; Returns integer | 0. ; ; Author (s): Ascer ;==================================================================================================================================================================================== Func _ListCount(ByRef $aList) If Not IsObj($aList) Then Return SetError(1, 0, 0) Return SetError(0, 0, $aList.Count) EndFunc ;==================================================================================================================================================================================== ; Function: _ListIsFixedSize(ByRef $aList) ; ; Description: Get if list have changed size. ; ; Parameter(s): $aList - list | Working list. ; ; Return Value(s): On Success: ; Set @error to 0. ; Returns bool | List.IsFixedSize ; On Failure: ; Set @error to 1. $aList is not object. ; Returns var | Null. ; ; Author (s): Ascer ;==================================================================================================================================================================================== Func _ListIsFixedSize(ByRef $aList) If Not IsObj($aList) Then Return SetError(1, 0, Null) Return SetError(0, 0, $aList.IsFixedSize) EndFunc ;==================================================================================================================================================================================== ; Function: _ListIsReadOnly(ByRef $aList) ; ; Description: Get bool if list is readOnly else not. ; ; Parameter(s): $aList - list | Working list. ; ; Return Value(s): On Success: ; Set @error to 0. ; Returns bool | List.IsReadOnly ; On Failure: ; Set @error to 1. $aList is not object. ; Returns var | Null. ; ; Author (s): Ascer ;==================================================================================================================================================================================== Func _ListIsReadOnly(ByRef $aList) If Not IsObj($aList) Then Return SetError(1, 0, Null) Return SetError(0, 0, $aList.IsReadOnly) EndFunc ;==================================================================================================================================================================================== ; Function: _ListIsSynchronized(ByRef $aList) ; ; Description: Get bool if list is Synchronized else not. ; ; Parameter(s): $aList - list | Working list. ; ; Return Value(s): On Success: ; Set @error to 0. ; Returns bool | List.IsSynchronized ; On Failure: ; Set @error to 1. $aList is not object. ; Returns var | Null. ; ; Author (s): Ascer ;==================================================================================================================================================================================== Func _ListIsSynchronized(ByRef $aList) If Not IsObj($aList) Then Return SetError(1, 0, Null) Return SetError(0, 0, $aList.IsSynchronized) EndFunc ;==================================================================================================================================================================================== ; Function: _ListGetItem(ByRef $aList, $iIndex) ; ; Description: Get item on $iIndex in list. ; ; Parameter(s): $aList - list | Working list. ; $iIndex - integer | Item index in list (0 base index) ; ; Return Value(s): On Success: ; Set @error to 0. ; Returns var | List.Iitem($iIndex) ; On Failure: ; Set @error to: ; 1. $aList is not object. ; 2. Item in $iIndex do not exist. ; Returns var | Null. ; ; Author (s): Ascer ;==================================================================================================================================================================================== Func _ListGetItem(ByRef $aList, $iIndex) If Not IsObj($aList) Then Return SetError(1, 0, Null) If _ListCount($aList) <= $iIndex Then Return SetError(2, 0, Null) Return SetError(0, 0, $aList.Item($iIndex)) EndFunc ;==================================================================================================================================================================================== ; Function: _ListSetItem(ByRef $aList, $iIndex, $vItem) ; ; Description: Replace item in List. ; ; Parameter(s): $aList - list | Working list. ; $iIndex - integer | Item index in list (0 base index) ; $vItem - var | Item to add. Can be Int32, string, bool, array etc. ; ; Return Value(s): On Success: ; Set @error to 0. ; Returns var | List.Iitem($iIndex) ; On Failure: ; Set @error to: ; 1. $aList is not object. ; 2. Item in $iIndex do not exist. ; Returns var | Null. ; ; Author (s): Ascer ;==================================================================================================================================================================================== Func _ListSetItem(ByRef $aList, $iIndex, $vItem) If Not IsObj($aList) Then Return SetError(1, 0, Null) If _ListCount($aList) <= $iIndex Then Return SetError(2, 0, Null) $aList.Item($iIndex) = $vItem Return SetError(0, 0, $aList.Item($iIndex)) EndFunc #EndRegion #Region Methods. ;==================================================================================================================================================================================== ; Function: _ListAdd(ByRef $aList, $vItem) ; ; Description: Add item to System.Collections.ArrayList. ; ; Parameter(s): $aList - list | Working list. ; $vItem - var | Item to add. Can be Int32, string, bool, array etc. ; ; Return Value(s): On Success: ; Set @error to 0. ; Returns integer | Index of new added item. ; On Failure: ; Set @error to 1. $aList is not object. ; Returns var | Null. ; ; Author (s): Ascer ;==================================================================================================================================================================================== Func _ListAdd(ByRef $aList, $vItem) If Not IsObj($aList) Then Return SetError(1, 0, Null) Return SetError(0, 0, $aList.Add($vItem)) EndFunc ;==================================================================================================================================================================================== ; Function: _ListClear(ByRef $aList) ; ; Description: Clear all items in List. Capacity will not changed. ; ; Parameter(s): $aList - list | Working list. ; ; Return Value(s): On Success: ; Set @error to 0. ; Returns bool | True or False (!Important when ret=False then $aList isReadOnly or Size is wrong) ; On Failure: ; Set @error to 1. $aList is not object. ; Returns bool | False. ; ; Author (s): Ascer ;==================================================================================================================================================================================== Func _ListClear(ByRef $aList) If Not IsObj($aList) Then Return SetError(1, 0, False) Local $vRet = $aList.Clear Return SetError(0, 0, $vRet = "" ? True : False) EndFunc ;==================================================================================================================================================================================== ; Function: _ListClone(ByRef $aList) ; ; Description: Shallow clone list to new variable. Allow on easy access. ; ; Parameter(s): $aList - list | Working list. ; ; Return Value(s): On Success: ; Set @error to 0. ; Returns object | A new System.Collections.ArrayList can by save in variable: Local $aNewList = _ListClone($aList) ; On Failure: ; Set @error to 1. $aList is not object. ; Returns var | Null. ; ; Author (s): Ascer ;==================================================================================================================================================================================== Func _ListClone(ByRef $aList) If Not IsObj($aList) Then Return SetError(1, 0, Null) Return SetError(0, 0, $aList.Clone) EndFunc ;==================================================================================================================================================================================== ; Function: _ListContains(ByRef $aList, $vItem) ; ; Description: Check is $vItem is on $aList. ; ; Parameter(s): $aList - list | Working list. ; $vItem - var | Item to add. Can be Int32, string, bool, array etc. ; ; Return Value(s): On Success: ; Set @error to 0. ; Returns bool | True - item found, False - item not found. ; On Failure: ; Set @error to 1. $aList is not object. ; Returns bool | False. ; ; Author (s): Ascer ;==================================================================================================================================================================================== Func _ListContains(ByRef $aList, $vItem) If Not IsObj($aList) Then Return SetError(1, 0, Null) Return SetError(0, 0, $aList.Contains($vItem)) EndFunc ;==================================================================================================================================================================================== ; Function: _ListGetHashCode(ByRef $aList) ; ; Description: Get hash code for $aList. ; ; Parameter(s): $aList - list | Working list. ; ; Return Value(s): On Success: ; Set @error to 0. ; Returns integer | Hash.key ; On Failure: ; Set @error to 1. $aList is not object. ; Returns var | Null. ; ; Author (s): Ascer ;==================================================================================================================================================================================== Func _ListGetHashCode(ByRef $aList) If Not IsObj($aList) Then Return SetError(1, 0, Null) Return SetError(0, 0, $aList.GetHashCode) EndFunc ;==================================================================================================================================================================================== ; Function: _ListGetRange(ByRef $aList, $iStart, $iEnd) ; ; Description: Saparate list by range. Exmple create a new [object] list insert items between index 2, 4. ; ; Parameter(s): $aList - list | Working list. ; $iStart - integer | Start index range. ; $iEnd - integer | End index range. ; ; Return Value(s): On Success: ; Set @error to 0. ; Returns object | Saparated list with items range. ; On Failure: ; Set @error to : ; 1. $aList is not object. ; 2. List.count is 0. ; 3. Wrong range. $iStart or $iEnd doesn't match to list dimension. ; Returns var | Null. ; ; Author (s): Ascer ;==================================================================================================================================================================================== Func _ListGetRange(ByRef $aList, $iStart, $iEnd) If Not IsObj($aList) Then Return SetError(1, 0, Null) Local $iCount = _ListCount($aList) If $iCount = 0 Then Return SetError(2, 0, Null) $iStart = $iStart < 0 ? 0 : $iStart If $iStart >= $iCount Or $iEnd >= $iCount Then Return SetError(3, 0, Null) Return SetError(0, 0, $aList.GetRange($iStart, $iEnd)) EndFunc ;==================================================================================================================================================================================== ; Function: _ListIndexOf(ByRef $aList, $vItem [, $fObjArray = False]) ; ; Description: Get item(s) index in list. ; ; Parameter(s): $aList - list | Working list. ; $vItem - var | Item to add. Can be Int32, string, bool, array etc. ; $fObjArray - bool | False - Return int first found item index. True - Return Object.List with item(s) index. ; ; Return Value(s): On Success: ; Set @error to 0. ; Returns integer or object | Depent on $fObjArray. (Look param $fObjArray description) ; On Failure: ; Set @error to 1. $aList is not object. ; Returns integer | -1. ; ; Author (s): Ascer ;==================================================================================================================================================================================== Func _ListIndexOf(ByRef $aList, $vItem, $fObjArray = False) If Not IsObj($aList) Then Return SetError(1, 0, Null) Local $vSubList = Null Local $iLastIndex = -1 If $fObjArray Then $vSubList = _ListCreate() For $i = 0 To _ListCount($aList) - 1 Local $iRet = $aList.IndexOf($vItem, $i) If $iRet <> -1 Then If Not $fObjArray Then Return SetError(0, 0, $iRet) If $iLastIndex <> $iRet Then _ListAdd($vSubList, $iRet) $iLastIndex = $iRet EndIf Next Return SetError(0, 0, $vSubList) EndFunc ;==================================================================================================================================================================================== ; Function: _ListInsert(ByRef $aList, $iIndex, $vItem) ; ; Description: Insert item to current list. When item on index 0 exists after insert his index will +1. ; ; Parameter(s): $aList - list | Working list. ; $iIndex - integer | Index to insert item. ; $vItem - var | Item to add. Can be Int32, string, bool, array etc. ; ; Return Value(s): On Success: ; Set @error to 0. ; Returns bool | True - successfully insert object, False - $aList in readOnly or maxSize has reached. ; On Failure: ; Set @error to: ; 1. $aList is not object. ; 2. Invalid $iIndex. List.count is lower than insert index ; Returns bool | False. ; ; Author (s): Ascer ;==================================================================================================================================================================================== Func _ListInsert(ByRef $aList, $iIndex, $vItem) If Not IsObj($aList) Then Return SetError(1, 0, False) $iIndex = $iIndex < 0 ? 0 : $iIndex If $iIndex > _ListCount($aList) Then Return SetError(2, 0, False) Return SetError(0, 0, $aList.Insert($iIndex, $vItem) = "" ? True : False) EndFunc ;==================================================================================================================================================================================== ; Function: _ListInsertRange(ByRef $aList, $iIndex, $aRangeList) ; ; Description: Insert item to current list. When item on index 0 exists after insert his index will +1. ; ; Parameter(s): $aList - list | Working list. ; $iIndex - integer | Index to insert item. ; $aRangeList - object | ICollection or other _ListCreate with items. ; ; Return Value(s): On Success: ; Set @error to 0. ; Returns bool | True - successfully insert object, False - $aList in readOnly or maxSize has reached. ; On Failure: ; Set @error to: ; 1. $aList or $aRangeList is not object. ; 2. Invalid $iIndex. List.count is lower than insert index ; Returns bool | False. ; ; Author (s): Ascer ;==================================================================================================================================================================================== Func _ListInsertRange(ByRef $aList, $iIndex, $aRangeList) If Not IsObj($aList) Or Not IsObj($aRangeList) Then Return SetError(1, 0, False) $iIndex = $iIndex < 0 ? 0 : $iIndex If $iIndex > _ListCount($aList) Then Return SetError(2, 0, False) Return SetError(0, 0, $aList.InsertRange($iIndex, $aRangeList) = "" ? True : False) EndFunc ;==================================================================================================================================================================================== ; Function: _ListLastIndexOf(ByRef $aList, $vItem) ; ; Description: Get last index of $vItem in list. ; ; Parameter(s): $aList - list | Working list. ; $vItem - var | Item to add. Can be Int32, string, bool, array etc. ; ; Return Value(s): On Success: ; Set @error to 0. ; Returns integer | Last item index in list. In not found return -1. ; On Failure: ; Set @error to 1. $aList is not object. ; Returns integer | -1. ; ; Author (s): Ascer ;==================================================================================================================================================================================== Func _ListLastIndexOf(ByRef $aList, $vItem) If Not IsObj($aList) Then Return SetError(1, 0, -1) Return SetError(0, 0, $aList.LastIndexOf($vItem)) EndFunc ;==================================================================================================================================================================================== ; Function: _ListRemove(ByRef $aList, $vItem) ; ; Description: Remove first found $vItem from list. ; ; Parameter(s): $aList - list | Working list. ; $vItem - var | Item to add. Can be Int32, string, bool, array etc. ; ; Return Value(s): On Success: ; Set @error to 0. ; Returns bool | True - when item not found or just removed. False - List isReadOnly or wrong Size. ; On Failure: ; Set @error to 1. $aList is not object. ; Returns bool | False. ; ; Author (s): Ascer ;==================================================================================================================================================================================== Func _ListRemove(ByRef $aList, $vItem) If Not IsObj($aList) Then Return SetError(1, 0, False) Return SetError(0, 0, $aList.Remove($vItem) = "" ? True : False) EndFunc ;==================================================================================================================================================================================== ; Function: _ListRemoveAt(ByRef $aList, $iIndex) ; ; Description: Remove $iIndex from List, all items with higher index will have -1. ; ; Parameter(s): $aList - list | Working list. ; $iIndex - integer | Index to remove from list ; ; Return Value(s): On Success: ; Set @error to 0. ; Returns bool | True - when item not found or just removed. False - List isReadOnly or wrong Size. ; On Failure: ; Set @error to: ; 1. $aList is not object. ; 2. Invalid $iIndex. ; Returns bool | False. ; ; Author (s): Ascer ;==================================================================================================================================================================================== Func _ListRemoveAt(ByRef $aList, $iIndex) If Not IsObj($aList) Then Return SetError(1, 0, False) $iIndex = $iIndex < 0 ? 0 : $iIndex If $iIndex >= _ListCount($aList) Then Return SetError(2, 0, False) Return SetError(0, 0, $aList.Remove($iIndex) = "" ? True : False) EndFunc ;==================================================================================================================================================================================== ; Function: _ListRemoveRange(ByRef $aList, $iStart, $iEnd) ; ; Description: Remove range indexs from List. Example remove {2, 5} ; ; Parameter(s): $aList - list | Working list. ; $iStart - integer | Start index to remove. ; $iEnd - integer | End index to remove. ; ; Return Value(s): On Success: ; Set @error to 0. ; Returns bool | True - when items removed. False - List isReadOnly or wrong Size. ; On Failure: ; Set @error to: ; 1. $aList is not object. ; 2. $aList is empty. ; 3. Invalid index for $iStart or $iEnd. ; Returns bool | False. ; ; Author (s): Ascer ;==================================================================================================================================================================================== Func _ListRemoveRange(ByRef $aList, $iStart, $iEnd) If Not IsObj($aList) Then Return SetError(1, 0, False) Local $iCount = _ListCount($aList) If $iCount = 0 Then Return SetError(2, 0, False) $iStart = $iStart < 0 ? 0 : $iStart If $iStart >= $iCount Or $iEnd >= $iCount Then Return SetError(3, 0, False) Return SetError(0, 0, $aList.RemoveRange($iStart, $iEnd) = "" ? True : False) EndFunc ;==================================================================================================================================================================================== ; Function: _ListReverse(ByRef $aList) ; ; Description: Reverse all items in List. ; ; Parameter(s): $aList - list | Working list. ; ; Return Value(s): On Success: ; Set @error to 0. ; Returns bool | True - when reversed done False - List isReadOnly. ; On Failure: ; Set @error to 1. $aList is not object. ; Returns bool | False. ; ; Author (s): Ascer ;==================================================================================================================================================================================== Func _ListReverse(ByRef $aList) If Not IsObj($aList) Then Return SetError(1, 0, False) Return SetError(0, 0, $aList.Reverse() = "" ? True : False) EndFunc ;==================================================================================================================================================================================== ; Function: _ListInsertRange(ByRef $aList, $iIndex, $aRangeList) ; ; Description: Insert item to current list. When item on index 0 exists after insert his index will +1. ; ; Parameter(s): $aList - list | Working list. ; $iIndex - integer | Index to insert item. ; $aRangeList - object | ICollection or other _ListCreate with items. ; ; Return Value(s): On Success: ; Set @error to 0. ; Returns bool | True - successfully insert object, False - $aList in readOnly or maxSize has reached. ; On Failure: ; Set @error to: ; 1. $aList or $aRangeList is not object. ; 2. Invalid $iIndex. List.count is lower than insert index ; Returns bool | False. ; ; Author (s): Ascer ;==================================================================================================================================================================================== Func _ListSetRange(ByRef $aList, $iIndex, $aRangeList) If Not IsObj($aList) Or Not IsObj($aRangeList) Then Return SetError(1, 0, False) $iIndex = $iIndex < 0 ? 0 : $iIndex If $iIndex > _ListCount($aList) Then Return SetError(2, 0, False) Return SetError(0, 0, $aList.SetRange($iIndex, $aRangeList) = "" ? True : False) EndFunc ;==================================================================================================================================================================================== ; Function: _ListSort(ByRef $aList) ; ; Description: Sort items in List to improve later searching. Example before sort [5, 1, 3] -> after [1, 3, 5] ; ; Parameter(s): $aList - list | Working list. ; ; Return Value(s): On Success: ; Set @error to 0. ; Returns bool | True - when reversed done False - List isReadOnly. ; On Failure: ; Set @error to 1. $aList is not object. ; Returns bool | False. ; ; Author (s): Ascer ;==================================================================================================================================================================================== Func _ListSort(ByRef $aList) If Not IsObj($aList) Then Return SetError(1, 0, False) Return SetError(0, 0, $aList.Sort() = "" ? True : False) EndFunc ;==================================================================================================================================================================================== ; Function: _ListToString(ByRef $aList) ; ; Description: Get name on $aList object. ; ; Parameter(s): $aList - list | Working list. ; ; Return Value(s): On Success: ; Set @error to 0. ; Returns string | Name of object. ; On Failure: ; Set @error to 1. $aList is not object. ; Returns var | Null. ; ; Author (s): Ascer ;==================================================================================================================================================================================== Func _ListToString(ByRef $aList) If Not IsObj($aList) Then Return SetError(1, 0, Null) Return SetError(0, 0, $aList.ToString) EndFunc ;==================================================================================================================================================================================== ; Function: _ListTrimToSize(ByRef $aList) ; ; Description: Trim $aList to size with used space. Remove empty space. ; ; Parameter(s): $aList - list | Working list. ; ; Return Value(s): On Success: ; Set @error to 0. ; Returns bool | True - successfully trim list, Faise - list isReadOnly or we can not change size. ; On Failure: ; Set @error to 1. $aList is not object. ; Returns var | Null. ; ; Author (s): Ascer ;==================================================================================================================================================================================== Func _ListTrimToSize(ByRef $aList) If Not IsObj($aList) Then Return SetError(1, 0, Null) Return SetError(0, 0, $aList.TrimToSize = "" ? True : False) EndFunc #EndRegion