Sign in to follow this  
Followers 0
wolf9228

MEM CArray

9 posts in this topic

#1 ·  Posted (edited)

CArray Data Type ==> (String,Int,Binary,Float,HWnd,Ptr,Bool)

Data types (COM object,Autoit Array,DllStruct) are not allowed

CArray.zip

;CArray Members

http://msdn.microsoft.com/en-US/library/7y7b8fx3(v=VS.80).aspx

;GetCount

;Gets the number of elements in this array.

;GetUpperBound

;Returns the largest valid index.

;SetSize

;Sets the number of elements to be contained in this array.

;FreeExtra

;Frees all unused memory above the current upper bound.

;RemoveAll

;Removes all the elements from this array.

;GetAt

;Returns the value at a given index.

;SetAt

;Sets the value for a given index; array not allowed to grow.

;SetAtGrow

;Sets the value for a given index; grows the array if necessary.

;RemoveAt

;Removes an element at a specific index.

CArray.au3

#include <GuiListView.au3>
#include <GUIConstantsEx.au3>
;CArray Members
;http://msdn.microsoft.com/en-US/library/7y7b8fx3(v=VS.80).aspx
;GetCount
;Gets the number of elements in this array.
;GetUpperBound
;Returns the largest valid index.
;SetSize
;Sets the number of elements to be contained in this array.
;FreeExtra
;Frees all unused memory above the current upper bound.
;RemoveAll
;Removes all the elements from this array.
;GetAt
;Returns the value at a given index.
;SetAt
;Sets the value for a given index; array not allowed to grow.
;SetAtGrow
;Sets the value for a given index; grows the array if necessary.
;RemoveAt
;Removes an element at a specific index.


;CArray Data Type ==> (String,IsInt,Binary,Float,HWnd,Ptr,Bool)
;Data types (COM object,Autoit Array,DllStruct) are not allowed
Func CArray($Rows = 1,$Cols = 1)
Select
Case $Rows < 1 Or $Cols < 1 
Return SetError(1,0,False)
Case Else
$CArrayStruct = DllStructCreate("INT RE;INT CE;PTR ELEMENTS[" & $Rows * $Cols & "]")
DllStructSetData($CArrayStruct,"RE",$Rows)
DllStructSetData($CArrayStruct,"CE",$Cols)
Return $CArrayStruct
EndSelect
EndFunc

Func Add(ByRef $CArrayStruct,$Value,$Dimension = 1)
$RE = DllStructGetData($CArrayStruct,"RE") 
$CE = DllStructGetData($CArrayStruct,"CE")
if $RE < 1 Or $CE < 1 Then Return SetError(1,0,False)
Switch $Dimension
Case 1
$iLength = DllStructGetSize($CArrayStruct)
$iCArrayStruct = DllStructCreate("INT RE;INT CE;PTR ELEMENTS[" & (($RE + 1) * $CE) & "]")
DllCall("kernel32.dll","none","RtlMoveMemory","ptr",DllStructGetPtr($iCArrayStruct) _
,"ptr",DllStructGetPtr($CArrayStruct),"ulong_ptr",$iLength)
$CArrayStruct = $iCArrayStruct
DllStructSetData($CArrayStruct,"RE",($RE + 1))
DllStructSetData($CArrayStruct,"CE",$CE)
SetAt($CArrayStruct,$Value,$RE,0)
Case 2
Local $PtrSize = 4
$iLength = DllStructGetSize($CArrayStruct)
$iCArrayStruct = DllStructCreate("INT RE;INT CE;PTR ELEMENTS[" & ($RE * ($CE + 1)) & "]")
$OutPtrA = DllStructGetPtr($iCArrayStruct,3)
$OutPtrB = DllStructGetPtr($CArrayStruct,3)
For $i = 1 To $RE 
DllCall("kernel32.dll","none","RtlMoveMemory","ptr",$OutPtrA _
,"ptr",$OutPtrB,"ulong_ptr",$CE * $PtrSize)
$OutPtrA += ($CE + 1) * $PtrSize
$OutPtrB += $CE * $PtrSize
Next
$CArrayStruct = $iCArrayStruct
DllStructSetData($CArrayStruct,"RE",$RE)
DllStructSetData($CArrayStruct,"CE",$CE + 1)
SetAt($CArrayStruct,$Value,0,$CE)
Case Else
Return SetError(2,0,False)
EndSwitch

Return SetError(0,0,True)
EndFunc

Func Display(ByRef $CArrayStruct,$Title = "DisplayCArray")
$TrayWnd = WinGetHandle("[CLASS:Shell_TrayWnd]") ;Taskbar Window
$WPos = WinGetPos($TrayWnd)
if Not IsArray($WPos) Then 
$iH = @DesktopHeight - 68
Else
$iH = $WPos[1] - 30
EndIf
$RE = DllStructGetData($CArrayStruct,"RE") 
$CE = DllStructGetData($CArrayStruct,"CE")
if $RE < 1 Or $CE < 1 Then Return SetError(1,0,False)
$W = (($CE + 1) * 70) + 15 ; $CE + 1 ==> 1 Rows Number
$H = (($RE + 1) * 20) + 50 ; $RE + 1 ==> 1 Clos Number
if $W > (@DesktopWidth - 5) Then $W = @DesktopWidth - 5
if $H > $iH Then $H = $iH
GUICreate($Title,$W,$H,0,0)
$W -= 10
$H -= 40
$hListView = GUICtrlCreateListView("",5,5,$W,$H)
_GUICtrlListView_SetExtendedListViewStyle($hListView, _
BitOR($LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT, $LVS_EX_SUBITEMIMAGES,$LVS_EX_FLATSB))
$CopyButton = GUICtrlCreateButton("Copy Selected", 5,$H + 10, $W - 5, 25)
For $i = 0 To $RE - 1
For $j = 0 To $CE - 1
$Value = GetAt($CArrayStruct,$i,$j)
If $i = 0 Then
if $J = 0 Then _
_GUICtrlListView_AddColumn($hListView, "Row",70)
_GUICtrlListView_AddColumn($hListView, "Col " & $J,70)
EndIf
if $j = 0 Then 
_GUICtrlListView_AddItem($hListView,"",$i)
_GUICtrlListView_AddSubItem($hListView,$i, "[" & $i & "]",$j)
_GUICtrlListView_AddSubItem($hListView,$i,$Value,$j + 1)
Else
_GUICtrlListView_AddSubItem($hListView,$i,$Value,$j + 1)
EndIf
Next
Next
GUISetState()
Do
$msg = GUIGetMsg()
if $msg = $CopyButton Then
$selectionmark = _GUICtrlListView_GetSelectionMark($hListView)
ClipPut(_GUICtrlListView_GetItemTextString($hListView,$selectionmark))
EndIf
Until $msg = $GUI_EVENT_CLOSE
GUIDelete()
Return SetError(0,0,True)
EndFunc

Func SetAt(ByRef $CArrayStruct,$Value,$RowElement = 0,$ColuElement = 0)
Local $DataType = 0 
$RowElement += 1 
$ColuElement += 1
$RE = DllStructGetData($CArrayStruct,"RE") 
$CE = DllStructGetData($CArrayStruct,"CE")
Select
Case $RE < 1 Or $CE < 1 
Return SetError(1,0,False)
Case $RowElement < 1 Or $ColuElement < 1
Return SetError(2,0,False)
Case ($RE * $CE) < ($RowElement * $ColuElement)
Return SetError(3,0,False)
EndSelect
$Element = ($RowElement * $CE) - ($CE - $ColuElement)
Local $IntSize = 4 ;DllStructGetSize(DllStructCreate("INT"))
Local $BoolSize = 4 ;DllStructGetSize(DllStructCreate("BOOL"))
Local $PtrSize = 4 ;DllStructGetSize(DllStructCreate("PTR"))
Local $HWndSize = 4 ;DllStructGetSize(DllStructCreate("HWND"))
$Ptr = DllStructGetData($CArrayStruct,"ELEMENTS",$Element)
$hMemory = DllCall("kernel32.dll","handle","GlobalHandle","ptr",$Ptr)
DllCall("kernel32.dll","handle","GlobalFree","ptr",$hMemory[0])
Select
Case IsString($Value)
$DataSize = StringLen($Value)
$hMemory = DllCall("kernel32.dll","handle","GlobalAlloc","uint",(0x0002 + 0x0040),"ulong_ptr",$DataSize + ($IntSize * 2))
;(0x0002 + 0x0040) ==> $GHND / ($IntSize * 2) 2 ==> "int DataSize;int DataType;
$hLock = DllCall("kernel32.dll","ptr","GlobalLock","handle",$hMemory[0])
$Struct = DllStructCreate("INT DataSize;INT DataType;CHAR Data[" & $DataSize & "]",$hLock[0])
DllStructSetData($Struct,"DataSize",$DataSize)
DllStructSetData($Struct,"DataType",1)
DllStructSetData($Struct,"Data",$Value)
DllStructSetData($CArrayStruct,"ELEMENTS",$hLock[0],$Element)
Case IsBinary($Value)
$DataSize = BinaryLen($Value)
$hMemory = DllCall("kernel32.dll","handle","GlobalAlloc","uint",(0x0002 + 0x0040),"ulong_ptr",$DataSize + ($IntSize * 2))
;(0x0002 + 0x0040) ==> $GHND / ($IntSize * 2) 2 ==> "int DataSize;int DataType;
$hLock = DllCall("kernel32.dll","ptr","GlobalLock","handle",$hMemory[0])
$Struct = DllStructCreate("INT DataSize;INT DataType;BYTE Data[" & $DataSize & "]",$hLock[0])
DllStructSetData($Struct,"DataSize",$DataSize)
DllStructSetData($Struct,"DataType",2)
DllStructSetData($Struct,"Data",$Value)
DllStructSetData($CArrayStruct,"ELEMENTS",$hLock[0],$Element)
Case IsInt($Value)
$Value = String($Value)
$DataSize = StringLen($Value)
$hMemory = DllCall("kernel32.dll","handle","GlobalAlloc","uint",(0x0002 + 0x0040),"ulong_ptr",$DataSize + ($IntSize * 2))
;(0x0002 + 0x0040) ==> $GHND / ($IntSize * 2) 2 ==> "int DataSize;int DataType;
$hLock = DllCall("kernel32.dll","ptr","GlobalLock","handle",$hMemory[0])
$Struct = DllStructCreate("INT DataSize;INT DataType;CHAR Data[" & $DataSize & "]",$hLock[0])
DllStructSetData($Struct,"DataSize",$DataSize)
DllStructSetData($Struct,"DataType",3)
DllStructSetData($Struct,"Data",$Value)
DllStructSetData($CArrayStruct,"ELEMENTS",$hLock[0],$Element)
Case IsFloat($Value)
$Value = String($Value)
$DataSize = StringLen($Value)
$hMemory = DllCall("kernel32.dll","handle","GlobalAlloc","uint",(0x0002 + 0x0040),"ulong_ptr",$DataSize + ($IntSize * 2))
;(0x0002 + 0x0040) ==> $GHND / ($IntSize * 2) 2 ==> "int DataSize;int DataType;
$hLock = DllCall("kernel32.dll","ptr","GlobalLock","handle",$hMemory[0])
$Struct = DllStructCreate("INT DataSize;INT DataType;CHAR Data[" & $DataSize & "]",$hLock[0])
DllStructSetData($Struct,"DataSize",$DataSize)
DllStructSetData($Struct,"DataType",4)
DllStructSetData($Struct,"Data",$Value)
DllStructSetData($CArrayStruct,"ELEMENTS",$hLock[0],$Element)
Case IsBool($Value)
$hMemory = DllCall("kernel32.dll","handle","GlobalAlloc","uint",(0x0002 + 0x0040),"ulong_ptr",$BoolSize + ($IntSize * 2))
;(0x0002 + 0x0040) ==> $GHND / $BoolSize + ($IntSize * 2) ==> INT DataSize;INT DataType;BOOL Data
$hLock = DllCall("kernel32.dll","ptr","GlobalLock","handle",$hMemory[0])
$Struct = DllStructCreate("INT DataSize;INT DataType;BOOL Data",$hLock[0])
DllStructSetData($Struct,"DataType",5)
DllStructSetData($Struct,"Data",$Value)
DllStructSetData($CArrayStruct,"ELEMENTS",$hLock[0],$Element)
Case IsPtr($Value)
$hMemory = DllCall("kernel32.dll","handle","GlobalAlloc","uint",(0x0002 + 0x0040),"ulong_ptr",$PtrSize + ($IntSize * 2))
;(0x0002 + 0x0040) ==> $GHND / $PtrSize + ($IntSize * 2) ==> "INT DataSize;INT DataType;PTR Data"
$hLock = DllCall("kernel32.dll","ptr","GlobalLock","handle",$hMemory[0])
$Struct = DllStructCreate("INT DataSize;INT DataType;PTR Data",$hLock[0])
DllStructSetData($Struct,"DataType",6)
DllStructSetData($Struct,"Data",$Value)
DllStructSetData($CArrayStruct,"ELEMENTS",$hLock[0],$Element)
Case IsHWnd($Value)
$hMemory = DllCall("kernel32.dll","handle","GlobalAlloc","uint",(0x0002 + 0x0040),"ulong_ptr",$HWndSize + ($IntSize * 2))
;(0x0002 + 0x0040) ==> $GHND / $HWndSize + ($IntSize * 2) ==> "INT DataSize;INT DataType;HWND Data"
$hLock = DllCall("kernel32.dll","ptr","GlobalLock","handle",$hMemory[0])
$Struct = DllStructCreate("INT DataSize;INT DataType;HWND Data",$hLock[0])
DllStructSetData($Struct,"DataType",7)
DllStructSetData($Struct,"Data",$Value)
DllStructSetData($CArrayStruct,"ELEMENTS",$hLock[0],$Element)
Case IsKeyword($Value)
;Default Keyword
$hMemory = DllCall("kernel32.dll","handle","GlobalAlloc","uint",(0x0002 + 0x0040),"ulong_ptr",($IntSize * 3))
;(0x0002 + 0x0040) ==> $GHND / ($IntSize * 3) 3 ==> "INT DataSize;INT DataType;INT Data"
$hLock = DllCall("kernel32.dll","ptr","GlobalLock","handle",$hMemory[0])
$Struct = DllStructCreate("INT DataSize;INT DataType;INT Data",$hLock[0])
DllStructSetData($Struct,"DataType",8)
DllStructSetData($Struct,"Data",$Value)
DllStructSetData($CArrayStruct,"ELEMENTS",$hLock[0],$Element)
Case Else
Return SetError(4,0,False)
EndSelect
Return SetError(0,0,True)
EndFunc

Func GetAt(ByRef $CArrayStruct,$RowElement = 0,$ColuElement = 0)
$RowElement += 1
$ColuElement += 1
$RE = DllStructGetData($CArrayStruct,"RE") 
$CE = DllStructGetData($CArrayStruct,"CE")
Select
Case $RE < 1 Or $CE < 1 
Return SetError(1,0,False)
Case $RowElement < 1 Or $ColuElement < 1
Return SetError(2,0,False)
Case ($RE * $CE) < ($RowElement * $ColuElement)
Return SetError(3,0,False)
EndSelect
$Element = ($RowElement * $CE) - ($CE - $ColuElement)
$hLock = DllStructGetData($CArrayStruct,"ELEMENTS",$Element)
$Struct = DllStructCreate("INT DataSize;INT DataType",$hLock)
$DataType = DllStructGetData($Struct,"DataType")
Switch $DataType
Case 1
$DataSize = DllStructGetData($Struct,"DataSize")
$Struct = DllStructCreate("INT DataSize;INT DataType;CHAR Data[" & $DataSize & "]",$hLock)
Return SetError(0,0,DllStructGetData($Struct,"Data"))
Case 2
$DataSize = DllStructGetData($Struct,"DataSize")
$Struct = DllStructCreate("INT DataSize;INT DataType;BYTE Data[" & $DataSize & "]",$hLock)
Return SetError(0,0,DllStructGetData($Struct,"Data"))
Case 3
$DataSize = DllStructGetData($Struct,"DataSize")
$Struct = DllStructCreate("INT DataSize;INT DataType;CHAR Data[" & $DataSize & "]",$hLock)
Return SetError(0,0,Number(DllStructGetData($Struct,"Data")))
Case 4
$DataSize = DllStructGetData($Struct,"DataSize")
$Struct = DllStructCreate("INT DataSize;INT DataType;CHAR Data[" & $DataSize & "]",$hLock)
Return SetError(0,0,Number(DllStructGetData($Struct,"Data")))
Case 5
$Struct = DllStructCreate("INT DataSize;INT DataType;BOOL Data",$hLock)
Return SetError(0,0,DllStructGetData($Struct,"Data") == 1)
Case 6
$Struct = DllStructCreate("INT DataSize;INT DataType;PTR Data",$hLock)
Return SetError(0,0,DllStructGetData($Struct,"Data"))
Case 7
$Struct = DllStructCreate("INT DataSize;INT DataType;HWND Data",$hLock)
Return SetError(0,0,DllStructGetData($Struct,"Data"))
Case 8
Return SetError(0,0,Default)
Case Else
Return SetError(0,0,"")
EndSwitch
EndFunc

Func SetAtGrow(ByRef $CArrayStruct,$Value,$Row_Or_Colu_No = 0,$Dimension = 1)
$RE = DllStructGetData($CArrayStruct,"RE") 
$CE = DllStructGetData($CArrayStruct,"CE")
if $RE < 1 Or $CE < 1 Or $Row_Or_Colu_No < 0 Then Return SetError(1,0,False)
Local $PtrSize = 4
Switch $Dimension
Case 1
Select
Case ($Row_Or_Colu_No + 1) <= ($RE + 1)
InsertAt($CArrayStruct,$Value,$Row_Or_Colu_No,$Dimension)
Case Else
$iCArrayStruct = DllStructCreate("INT RE;INT CE;PTR ELEMENTS[" & (($Row_Or_Colu_No + 1) * $CE) & "]")
DllCall("kernel32.dll","none","RtlMoveMemory","ptr",DllStructGetPtr($iCArrayStruct) _
,"ptr",DllStructGetPtr($CArrayStruct),"ulong_ptr",DllStructGetSize($CArrayStruct))
$CArrayStruct = $iCArrayStruct
DllStructSetData($CArrayStruct,"RE",($Row_Or_Colu_No + 1))
DllStructSetData($CArrayStruct,"CE",$CE)
SetAt($CArrayStruct,$Value,$Row_Or_Colu_No,0)
EndSelect
Case 2
Select
Case ($Row_Or_Colu_No + 1) <= ($CE + 1)
InsertAt($CArrayStruct,$Value,$Row_Or_Colu_No,$Dimension)
Case Else
$AddCount = (($Row_Or_Colu_No + 1) - $CE)
$iCArrayStruct = DllStructCreate("INT RE;INT CE;PTR ELEMENTS[" & ($RE * ($Row_Or_Colu_No + 1)) & "]")
$OutPtrA = DllStructGetPtr($iCArrayStruct,3) 
$OutPtrB = DllStructGetPtr($CArrayStruct,3)
For $i = 1 To $RE
DllCall("kernel32.dll","none","RtlMoveMemory","ptr",$OutPtrA _
,"ptr",$OutPtrB,"ulong_ptr",$CE * $PtrSize)
$OutPtrA += ($Row_Or_Colu_No + 1) * $PtrSize
$OutPtrB += $CE * $PtrSize
Next
$CArrayStruct = $iCArrayStruct
DllStructSetData($CArrayStruct,"RE",$RE)
DllStructSetData($CArrayStruct,"CE",($Row_Or_Colu_No + 1))
SetAt($CArrayStruct,$Value,0,$Row_Or_Colu_No)
EndSelect
Case Else
Return SetError(2,0,False)
EndSwitch
Return SetError(0,0,True)
EndFunc

Func InsertAt(ByRef $CArrayStruct,$Value,$Row_Or_Colu_No = 0,$Dimension = 1)
$RE = DllStructGetData($CArrayStruct,"RE") 
$CE = DllStructGetData($CArrayStruct,"CE")
if $RE < 1 Or $CE < 1 Or $Row_Or_Colu_No < 0 Then Return SetError(1,0,False)
Local $PtrSize = 4
Switch $Dimension
Case 1
if ($Row_Or_Colu_No + 1) > ($RE + 1) Then Return SetError(2,0,False)
$iCArrayStruct = DllStructCreate("INT RE;INT CE;PTR ELEMENTS[" & (($RE + 1) * $CE) & "]")
$iLength = ((($Row_Or_Colu_No) * $CE) * $PtrSize)
$jLength = (($RE * $CE) * $PtrSize) - $iLength 
$OutPtrA = DllStructGetPtr($iCArrayStruct,3)
$OutPtrB = DllStructGetPtr($CArrayStruct,3)
DllCall("kernel32.dll","none","RtlMoveMemory","ptr",$OutPtrA,"ptr",$OutPtrB,"ulong_ptr",$iLength)
$OutPtrA = DllStructGetPtr($iCArrayStruct,3) + ($iLength + ($CE * $PtrSize))
$OutPtrB = DllStructGetPtr($CArrayStruct,3) + $iLength
DllCall("kernel32.dll","none","RtlMoveMemory","ptr",$OutPtrA,"ptr",$OutPtrB,"ulong_ptr",$jLength)
$CArrayStruct = $iCArrayStruct
DllStructSetData($CArrayStruct,"RE",($RE + 1))
DllStructSetData($CArrayStruct,"CE",$CE)
SetAt($CArrayStruct,$Value,$Row_Or_Colu_No,0)
Case 2
if ($Row_Or_Colu_No + 1) > ($CE + 1) Then Return SetError(2,0,False)
$iCArrayStruct = DllStructCreate("INT RE;INT CE;PTR ELEMENTS[" & ($RE * ($CE + 1)) & "]")
$OutPtrA = DllStructGetPtr($iCArrayStruct,3) 
$OutPtrB = DllStructGetPtr($CArrayStruct,3)
For $i = 1 To $RE
DllCall("kernel32.dll","none","RtlMoveMemory","ptr",$OutPtrA _
,"ptr",$OutPtrB,"ulong_ptr",$Row_Or_Colu_No * $PtrSize)
$OutPtrA += ($Row_Or_Colu_No * $PtrSize) + $PtrSize
$OutPtrB += $Row_Or_Colu_No * $PtrSize
DllCall("kernel32.dll","none","RtlMoveMemory","ptr",$OutPtrA _
,"ptr",$OutPtrB,"ulong_ptr",($CE - $Row_Or_Colu_No) * $PtrSize)
$OutPtrA += ($CE - $Row_Or_Colu_No) * $PtrSize
$OutPtrB += ($CE - $Row_Or_Colu_No) * $PtrSize
Next
$CArrayStruct = $iCArrayStruct
DllStructSetData($CArrayStruct,"RE",$RE)
DllStructSetData($CArrayStruct,"CE",($CE + 1))
SetAt($CArrayStruct,$Value,0,$Row_Or_Colu_No)
Case Else
Return SetError(3,0,False)
EndSwitch
Return SetError(0,0,True)
EndFunc

Func SetSize(ByRef $CArrayStruct,$RowCount,$ColuCount = 1)
$RE = DllStructGetData($CArrayStruct,"RE") 
$CE = DllStructGetData($CArrayStruct,"CE")
Local $PtrSize = 4 ;DllStructGetSize(DllStructCreate("PTR"))
Select
Case $RE < 1 Or $CE < 1 
Return SetError(1,0,False)
Case $RowCount < 1 Or $ColuCount < 1
Return SetError(2,0,False)
Case $ColuCount <> $CE
Select
Case $RowCount  > $RE
$iLength = DllStructGetSize($CArrayStruct)
$iCArrayStruct = DllStructCreate("INT RE;INT CE;PTR ELEMENTS[" & ($RowCount  * $CE) & "]")
DllCall("kernel32.dll","none","RtlMoveMemory","ptr",DllStructGetPtr($iCArrayStruct) _
,"ptr",DllStructGetPtr($CArrayStruct),"ulong_ptr",$iLength)
$CArrayStruct = $iCArrayStruct
DllStructSetData($CArrayStruct,"RE",$RowCount)
DllStructSetData($CArrayStruct,"CE",$CE)
Case $RowCount  < $RE
For $i = ($RowCount * $CE) + 1 To ($RE * $CE)
$Ptr = DllStructGetData($CArrayStruct,"ELEMENTS",$i)
$hMemory = DllCall("kernel32.dll","handle","GlobalHandle","ptr",$Ptr)
DllCall("kernel32.dll","handle","GlobalFree","ptr",$hMemory[0])
Next
$iCArrayStruct = DllStructCreate("INT RE;INT CE;PTR ELEMENTS[" & ($RowCount * $CE) & "]")
$iLength = DllStructGetSize($iCArrayStruct)
DllCall("kernel32.dll","none","RtlMoveMemory","ptr",DllStructGetPtr($iCArrayStruct) _
,"ptr",DllStructGetPtr($CArrayStruct),"ulong_ptr",$iLength)
$CArrayStruct = $iCArrayStruct
DllStructSetData($CArrayStruct,"RE",$RowCount)
DllStructSetData($CArrayStruct,"CE",$CE)
EndSelect
$RE = DllStructGetData($CArrayStruct,"RE") 
$CE = DllStructGetData($CArrayStruct,"CE")
Select
Case $ColuCount > $CE
$iCArrayStruct = DllStructCreate("INT RE;INT CE;PTR ELEMENTS[" & ($RE * $ColuCount) & "]")
$OutPtrA = DllStructGetPtr($iCArrayStruct,3) 
$OutPtrB = DllStructGetPtr($CArrayStruct,3)
For $i = 1 To $RE
DllCall("kernel32.dll","none","RtlMoveMemory","ptr",$OutPtrA _
,"ptr",$OutPtrB,"ulong_ptr",$CE * $PtrSize)
$OutPtrA += ($ColuCount * $PtrSize)
$OutPtrB += $CE * $PtrSize
Next
$CArrayStruct = $iCArrayStruct
DllStructSetData($CArrayStruct,"RE",$RE)
DllStructSetData($CArrayStruct,"CE",$ColuCount)
Case $ColuCount < $CE
$iCArrayStruct = DllStructCreate("INT RE;INT CE;PTR ELEMENTS[" & ($RE * $ColuCount) & "]")
$OutPtrA = DllStructGetPtr($iCArrayStruct,3) 
$OutPtrB = DllStructGetPtr($CArrayStruct,3)
For $i = 1 To $RE
DllCall("kernel32.dll","none","RtlMoveMemory","ptr",$OutPtrA _
,"ptr",$OutPtrB,"ulong_ptr",($ColuCount + 1) * $PtrSize)
$OutPtrA += $ColuCount * $PtrSize
$OutPtrB += $CE * $PtrSize
For $j = ($ColuCount + 1) To $CE
$Ptr = DllStructGetData($CArrayStruct,"ELEMENTS",$j)
$hMemory = DllCall("kernel32.dll","handle","GlobalHandle","ptr",$Ptr)
DllCall("kernel32.dll","handle","GlobalFree","ptr",$hMemory[0])
Next
Next
$CArrayStruct = $iCArrayStruct
DllStructSetData($CArrayStruct,"RE",$RE)
DllStructSetData($CArrayStruct,"CE",$ColuCount)
EndSelect
Case $ColuCount = $CE
Select
Case $RowCount > $RE
$iLength = DllStructGetSize($CArrayStruct)
$iCArrayStruct = DllStructCreate("INT RE;INT CE;PTR ELEMENTS[" & ($RowCount * $CE) & "]")
DllCall("kernel32.dll","none","RtlMoveMemory","ptr",DllStructGetPtr($iCArrayStruct) _
,"ptr",DllStructGetPtr($CArrayStruct),"ulong_ptr",$iLength)
$CArrayStruct = $iCArrayStruct
DllStructSetData($CArrayStruct,"RE",$RowCount)
DllStructSetData($CArrayStruct,"CE",$CE)
Case $RowCount < $RE
For $i = ($RowCount * $CE) + 1 To ($RE * $CE)
$Ptr = DllStructGetData($CArrayStruct,"ELEMENTS",$i)
$hMemory = DllCall("kernel32.dll","handle","GlobalHandle","ptr",$Ptr)
DllCall("kernel32.dll","handle","GlobalFree","ptr",$hMemory[0])
Next
$iCArrayStruct = DllStructCreate("INT RE;INT CE;PTR ELEMENTS[" & ($RowCount * $CE) & "]")
$iLength = DllStructGetSize($iCArrayStruct)
DllCall("kernel32.dll","none","RtlMoveMemory","ptr",DllStructGetPtr($iCArrayStruct) _
,"ptr",DllStructGetPtr($CArrayStruct),"ulong_ptr",$iLength)
$CArrayStruct = $iCArrayStruct
DllStructSetData($CArrayStruct,"RE",$RowCount)
DllStructSetData($CArrayStruct,"CE",$CE)
EndSelect
EndSelect
Return SetError(0,0,True)
EndFunc

Func RemoveAt(ByRef $CArrayStruct,$Row_Or_Colu_No = 0,$Count = 1,$Dimension = 1)
$RE = DllStructGetData($CArrayStruct,"RE") 
$CE = DllStructGetData($CArrayStruct,"CE")
if $RE < 1 Or $CE < 1 Or $Row_Or_Colu_No < 0 Or $Count < 1 Then Return SetError(1,0,False)
Local $PtrSize = 4
Switch $Dimension
Case 1
if ($Row_Or_Colu_No + $Count) > $RE Then Return SetError(2,0,False)
$iCArrayStruct = DllStructCreate("INT RE;INT CE;PTR ELEMENTS[" & (($RE - $Count) * $CE) & "]")
$iLength = (($Row_Or_Colu_No * $CE) * $PtrSize)
$OutPtrA = DllStructGetPtr($iCArrayStruct,3)
$OutPtrB = DllStructGetPtr($CArrayStruct,3)
DllCall("kernel32.dll","none","RtlMoveMemory","ptr",$OutPtrA,"ptr",$OutPtrB,"ulong_ptr",$iLength)
$OutPtrA += $iLength
$OutPtrB += ($iLength + (($CE * $Count) * $PtrSize))
$jLength = ((($RE * $CE) - ($CE * $Count)) * $PtrSize) - $iLength
DllCall("kernel32.dll","none","RtlMoveMemory","ptr",$OutPtrA,"ptr",$OutPtrB,"ulong_ptr",$jLength)
$CArrayStruct = $iCArrayStruct
DllStructSetData($CArrayStruct,"RE",($RE - $Count))
DllStructSetData($CArrayStruct,"CE",$CE)
Case 2
if ($Row_Or_Colu_No + $Count) > $CE Then Return SetError(2,0,False)
$iCArrayStruct = DllStructCreate("INT RE;INT CE;PTR ELEMENTS[" & ($RE * ($CE - $Count)) & "]")
$OutPtrA = DllStructGetPtr($iCArrayStruct,3) 
$OutPtrB = DllStructGetPtr($CArrayStruct,3)
For $i = 1 To $RE
DllCall("kernel32.dll","none","RtlMoveMemory","ptr",$OutPtrA _
,"ptr",$OutPtrB,"ulong_ptr",$Row_Or_Colu_No * $PtrSize)
$OutPtrA += ($Row_Or_Colu_No * $PtrSize)
$OutPtrB += ($Row_Or_Colu_No * $PtrSize) + ($Count * $PtrSize)
DllCall("kernel32.dll","none","RtlMoveMemory","ptr",$OutPtrA _
,"ptr",$OutPtrB,"ulong_ptr",($CE - ($Row_Or_Colu_No + $Count)) * $PtrSize)
$OutPtrA += ($CE - ($Row_Or_Colu_No + $Count)) * $PtrSize
$OutPtrB += ($CE - ($Row_Or_Colu_No + $Count)) * $PtrSize
Next
$CArrayStruct = $iCArrayStruct
DllStructSetData($CArrayStruct,"RE",$RE)
DllStructSetData($CArrayStruct,"CE",($CE - $Count))
Case Else
Return SetError(3,0,False)
EndSwitch
Return SetError(0,0,True)
EndFunc


Func GetUpperBound(ByRef $CArrayStruct,$Dimension = 1)
$RE = DllStructGetData($CArrayStruct,"RE") 
$CE = DllStructGetData($CArrayStruct,"CE")
if $RE < 1 Or $CE < 1 Then Return SetError(1,0,0)
Switch $Dimension
Case 1
Return SetError(0,0,$RE - 1)
Case 2
Return SetError(0,0,$CE - 1)
Case Else
Return SetError(2,0,0)
EndSwitch
EndFunc

Func GetCount(ByRef $CArrayStruct,$Dimension = 1)
$RE = DllStructGetData($CArrayStruct,"RE") 
$CE = DllStructGetData($CArrayStruct,"CE")
if $RE < 1 Or $CE < 1 Then Return SetError(1,0,0)
Switch $Dimension
Case 0
Select
Case $CE > 1
Return SetError(0,0,2)
Case Else
Return SetError(0,0,1)
EndSelect
Case 1
Return SetError(0,0,$RE)
Case 2
Return SetError(0,0,$CE)
Case Else
Return SetError(2,0,0)
EndSwitch
EndFunc

Func FreeExtra(ByRef $CArrayStruct)
$RE = DllStructGetData($CArrayStruct,"RE") 
$CE = DllStructGetData($CArrayStruct,"CE")
if $RE < 1 Or $CE < 1 Then Return SetError(1,0,False)
For $i = 1 To ($RE * $CE) + 1
$Ptr = Ptr(DllStructGetData($CArrayStruct,"ELEMENTS",$i))
$hMemory = DllCall("kernel32.dll","handle","GlobalHandle","ptr",$Ptr)
DllCall("kernel32.dll","handle","GlobalFree","ptr",$hMemory[0])
Next
Return SetError(0,0,True)
EndFunc

Func RemoveAll(ByRef $CArrayStruct)
$RE = DllStructGetData($CArrayStruct,"RE") 
$CE = DllStructGetData($CArrayStruct,"CE")
if $RE < 1 Or $CE < 1 Then Return SetError(1,0,False)
For $i = 1 To ($RE * $CE) + 1
$Ptr = ptr(DllStructGetData($CArrayStruct,"ELEMENTS",$i))
$hMemory = DllCall("kernel32.dll","handle","GlobalHandle","ptr",$Ptr)
DllCall("kernel32.dll","handle","GlobalFree","ptr",$hMemory[0])
Next
$CArrayStruct = DllStructCreate("INT RE;INT CE;PTR ELEMENTS[1]")
DllStructSetData($CArrayStruct,"RE",1)
DllStructSetData($CArrayStruct,"CE",1)
Return SetError(0,0,True)
EndFunc

Examples

InsertAt

#include "CArray.au3"
$Array = CArray(1)
SetAt($Array,"Holger")
Add($Array,"Jon")
Add($Array,"Larry")
Add($Array,"Jeremy")
Add($Array,"Valik")
Add($Array,"Cyberslug")
Add($Array,"Nutster")
Add($Array,"JdeB")
Add($Array,"Tylo")
Display($Array)
InsertAt($Array,"New",4,1)
Display($Array)

$Array = CArray(1)
SetAt($Array,"Holger")
Add($Array,"Jon",2)
Add($Array,"Larry",2)
Add($Array,"Jeremy",2)
Add($Array,"Valik",2)
Add($Array,"Cyberslug",2)
Add($Array,"Nutster",2)
Add($Array,"JdeB",2)
Add($Array,"Tylo",2)
Display($Array)
InsertAt($Array,"New",4,2)
Display($Array)

SetAtGrow

#include "CArray.au3"
$Array = CArray(9) 
SetAt($Array,"Holger",0)
SetAt($Array,"Jon",1)
SetAt($Array,"Larry",2)
SetAt($Array,"Jeremy",3)
SetAt($Array,"Valik",4)
SetAt($Array,"Cyberslug",5)
SetAt($Array,"Nutster",6)
SetAt($Array,"JdeB",7)
SetAt($Array,"Tylo",8)
Display($Array)
SetAtGrow($Array,"New",20,1)
Display($Array)

$Array = CArray(1,4)
SetAt($Array,"Holger",0)
SetAt($Array,"Jon",0,1)
SetAt($Array,"Larry",0,2)
SetAt($Array,"Jeremy",0,3)
Display($Array)
SetAtGrow($Array,"New",8,2)
Display($Array)

RemoveAt

#include "CArray.au3"
$Array = CArray(9) 
SetAt($Array,"Holger",0)
SetAt($Array,"Jon",1)
SetAt($Array,"Larry",2)
SetAt($Array,"Jeremy",3)
SetAt($Array,"Valik",4)
SetAt($Array,"Cyberslug",5)
SetAt($Array,"Nutster",6)
SetAt($Array,"JdeB",7)
SetAt($Array,"Tylo",8)
Display($Array)
RemoveAt($Array,1,4,1)
Display($Array)

$Array = CArray(1,4)
SetAt($Array,"Holger",0)
SetAt($Array,"Jon",0,1)
SetAt($Array,"Larry",0,2)
SetAt($Array,"Jeremy",0,3)
Display($Array)
RemoveAt($Array,0,2,2)
Display($Array)

SetSize

#include "CArray.au3"
$Array = CArray(8) 
SetAt($Array,"Holger",0)
SetAt($Array,"Jon",1)
SetAt($Array,"Larry",2)
SetAt($Array,"Jeremy",3)
SetAt($Array,"Valik",4)
SetAt($Array,"Cyberslug",5)
SetAt($Array,"Nutster",6)
SetAt($Array,"JdeB",7)
SetAt($Array,"Tylo",8)
Display($Array)
SetSize($Array,8,8)
Display($Array)

$Array = CArray(1,4)
SetAt($Array,"Holger",0)
SetAt($Array,"Jon",0,1)
SetAt($Array,"Larry",0,2)
SetAt($Array,"Jeremy",0,3)
Display($Array)
SetSize($Array,4,4)
Display($Array)

RemoveAll

#include "CArray.au3"
$Array = CArray(8) 
SetAt($Array,"Holger",0)
SetAt($Array,"Jon",1)
SetAt($Array,"Larry",2)
SetAt($Array,"Jeremy",3)
SetAt($Array,"Valik",4)
SetAt($Array,"Cyberslug",5)
SetAt($Array,"Nutster",6)
SetAt($Array,"JdeB",7)
SetAt($Array,"Tylo",8)
Display($Array)
RemoveAll($Array)
Display($Array)

FreeExtra

#include "CArray.au3"
$Array = CArray(8) 
SetAt($Array,"Holger",0)
SetAt($Array,"Jon",1)
SetAt($Array,"Larry",2)
SetAt($Array,"Jeremy",3)
SetAt($Array,"Valik",4)
SetAt($Array,"Cyberslug",5)
SetAt($Array,"Nutster",6)
SetAt($Array,"JdeB",7)
SetAt($Array,"Tylo",8)
Display($Array)
FreeExtra($Array)
Display($Array)
Edited by wolf9228

صرح السماء كان هنا

 

Share this post


Link to post
Share on other sites



Interessting stuff. Need to find out the benefit of CArrays!

Thanks for sharing.

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯

Share this post


Link to post
Share on other sites

Interessting stuff. Need to find out the benefit of CArrays!

Thanks for sharing.

Br,

UEZ

CArray

Supports arrays that are similar to C arrays, but can dynamically shrink and grow as necessary.

Remarks

Array indexes always start at position 0. You can decide whether to fix the upper bound or allow the array to expand when you add elements past the current bound. Memory is allocated contiguously to the upper bound, even if some elements are null.

As with a C array, the access time for a CArray indexed element is constant and is independent of the array size.

Before using an array, use SetSize to establish its size and allocate memory for it. If you do not use SetSize, adding elements to your array causes it to be frequently reallocated and copied. Frequent reallocation and copying are inefficient and can fragment memory.

Header: CArray.au3

See Also :graduated:

wolf9228 Topics

http://www.autoitscript.com/forum/index.php?showuser=24246


صرح السماء كان هنا

 

Share this post


Link to post
Share on other sites

Hello, Mr. Ben ... In fact you are a professional

Is not a compliment ... Your movements Code up to

expression...Good work Thanks Wolf :graduated:

Share this post


Link to post
Share on other sites

Is this faster than AutoIt's default array system?

Share this post


Link to post
Share on other sites

Hello, Mr. Ben ... In fact you are a professional

Is not a compliment ... Your movements Code up to

expression...Good work Thanks Wolf :(

Welcome .. Mr. Ben a great personality...The Programming is application an explanation and an expression ... Thank fighter ... Good luck :graduated:


صرح السماء كان هنا

 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Is this faster than AutoIt's default array system?

Yes, that's right ... There are varying speed

This functions is very fast faster than AutoIt's default array

RemoveAt

InsertAt

SetAtGrow

There are also some slow functions than AutoIt's default array

SetAt

GetAt

Speed relative to the CArray more than the speed of the

Autoit Array

Also a different speeds Between 1D And 2D

Thanks nikitanova :graduated:

Edited by wolf9228

صرح السماء كان هنا

 

Share this post


Link to post
Share on other sites

Oh. thanks for the clarification.

Forgot to mention, great UDF. useful too.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Oh. thanks for the clarification.

Forgot to mention, great UDF. useful too.

Thank you :graduated:

Edited by wolf9228

صرح السماء كان هنا

 

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
Sign in to follow this  
Followers 0