3 hours ago
This is here with _MEM
Very fast: 13 seconds add and delete on my laptop...
Very fast: 13 seconds add and delete on my laptop...
Code: (Select All)
$Console:Only
Const Total = 16777216
ST! = Timer(0.01)
C$ = ContainerNewCapacity$(16777216)
Print "Container built in"; Timer(0.01) - ST!; "seconds"
ST! = Timer(0.01)
For I = 1 To Total
B~& = _RGBA32(Int(Rnd * 256), Int(Rnd * 256), Int(Rnd * 256), Int(Rnd * 256))
ContainerAdd C$, B~&
Next I
Print "Capacity: "; ContainerCapacity(C$)
Print "Length: "; ContainerLength(C$)
Print "Added Everything in"; Timer(0.01) - ST!
ST! = Timer(0.01)
For I = 1 To Total
ContainerDelete C$, 0
Next I
Print "Capacity: "; ContainerCapacity(C$)
Print "Length: "; ContainerLength(C$)
Print "Deleted Everything in"; Timer(0.01) - ST!
Sleep
System
'$Checking:Off
Function ContainerNew$ () Static
Static I As _Unsigned Long
Static __S$
__S$ = String$(64, 0): For I = 1 To 64 Step 4: Mid$(__S$, I, 4) = MKL$(_ShR(I, 2)): Next I
ContainerNew$ = Chr$(16) + MKL$(16) + MKL$(0) + __S$ + String$(64, 0)
' Signature + Capacity + Length + Index + Data
End Function
Function ContainerNewCapacity$ (Capacity As _Unsigned Long) Static
Static As _Unsigned Long I, J
Static __S$
__S$ = String$(_ShL(Capacity, 2), 0)
For I = 1 To _ShL(Capacity, 2) Step 4
J = _ShR(I, 2)
Asc(__S$, I) = _Blue32(J): Asc(__S$, I + 1) = _Green32(J): Asc(__S$, I + 2) = _Red32(J): Asc(__S$, I + 3) = _Alpha32(J)
Next I
ContainerNewCapacity$ = Chr$(16) + MKL$(Capacity) + MKL$(0) + __S$ + String$(_ShL(Capacity, 2), 0)
End Function
Sub ContainerAdd (Container As String, Element As _Unsigned Long) Static
Static Length~&, Capacity~&
If Len(Container) < 10 _OrElse Asc(Container) <> 16 Then Container = ContainerNew$
Static M As _MEM
M = _Mem(_Offset(Container), Len(Container))
_MemGet M, M.OFFSET + 1, Capacity~&: _MemGet M, M.OFFSET + 5, Length~&
_MemFree M
If Capacity~& <= Length~& Then ContainerResizeCapacity Container, _IIf(Capacity~& < 1048576, _Max(1, _ShL(Capacity~&, 1)), Capacity~& + 1048576)
M = _Mem(_Offset(Container), Len(Container))
_MemGet M, M.OFFSET + 1, Capacity~&
__T~& = Length~& + 1: _MemPut M, M.OFFSET + 5, __T~&
__T~& = 9 + _ShL(Length~&, 2)
_MemGet M, M.OFFSET + __T~&, __T~&
__T~& = 9 + _ShL(Capacity~&, 2) + _ShL(__T~&, 2)
_MemPut M, M.OFFSET + __T~&, Element
_MemFree M
End Sub
Function ContainerLength~& (Container As String) Static
If Len(Container) < 10 _OrElse Asc(Container) <> 16 Then Container = ContainerNew$
Static M As _MEM, Length~&
M = _Mem(_Offset(Container), Len(Container))
_MemGet M, M.OFFSET + 5, Length~&
ContainerLength~& = Length~&
_MemFree M
End Function
Function ContainerCapacity~& (Container As String) Static
If Len(Container) < 10 _OrElse Asc(Container) <> 16 Then Container = ContainerNewCapacity$(0)
Static M As _MEM, Capacity~&
M = _Mem(_Offset(Container), Len(Container))
_MemGet M, M.OFFSET + 1, Capacity~&
ContainerCapacity~& = Capacity~&
_MemFree M
End Function
Sub ContainerResizeCapacity (Container As String, NewSize As _Unsigned Long) Static
If Len(Container) < 10 _OrElse Asc(Container) <> 16 Then Container = ContainerNew$
Static Length~&, Capacity~&, M As _MEM
M = _Mem(_Offset(Container), Len(Container))
_MemGet M, M.OFFSET + 1, Capacity~&
_MemGet M, M.OFFSET + 5, Length~&
_MemFree M
__T~& = _ShL(Capacity~&, 2)
__Index$ = Mid$(Container, 10, __T~&)
__Data$ = Mid$(Container, 10 + __T~&, _ShL(__T~&, 2))
NewSize = _Max(Length~&, NewSize)
__T~& = NewSize - Capacity~&
If NewSize > Capacity~& Then
__Index$ = __Index$ + String$(_ShL(__T~&, 2), 0)
__Data$ = __Data$ + String$(_ShL(__T~&, 2), 0)
Else
__Index$ = Left$(__Index$, _ShL(NewSize, 2))
__Data$ = Left$(__Data$, _ShL(Length~&, 2))
End If
For I = 1 + _ShL(Capacity~&, 2) To _ShL(NewSize, 2) Step 4
Mid$(__Index$, I, 4) = MKL$(_ShR(I, 2))
Next I
Container = Chr$(16) + MKL$(NewSize) + MKL$(Length~&) + __Index$ + __Data$
End Sub
Function ContainerGet~& (Container As String, Index As _Unsigned Long) Static
If Len(Container) < 10 _OrElse Asc(Container) <> 16 Then Exit Function
Static Length~&, Capacity~&, __Index~&, M As _MEM, Element~&
M = _Mem(_Offset(Container), Len(Container))
_MemGet M, M.OFFSET + 1, Capacity~&
_MemGet M, M.OFFSET + 5, Length~&
If Index >= Length~& Then _MemFree M: Exit Function
__T~& = _ShL(Index, 2)
_MemGet M, M.OFFSET + 9 + __T~&, __Index~&
_MemGet M, M.OFFSET + 9 + _ShL(__Index~& + Capacity~&, 2), Element~&
_MemFree M
ContainerGet~& = Element~&
End Function
Sub ContainerDelete (Container As String, Index As _Unsigned Long) Static
If Len(Container) < 10 _OrElse Asc(Container) <> 16 Then Exit Sub
Static Length~&, Capacity~&, __Index~&, M As _MEM, __IndexLast~&
M = _Mem(_Offset(Container), Len(Container))
_MemGet M, M.OFFSET + 1, Capacity~&
_MemGet M, M.OFFSET + 5, Length~&
If Index < Length~& Then
' Update Length
__T~& = Length~& - 1: _MemPut M, M.OFFSET + 5, __T~&
' Swap Element
_MemGet M, M.OFFSET + 9 + _ShL(Index, 2), __Index~&: __Index~& = 9 + _ShL(Capacity~&, 2) + _ShL(__Index~&, 2)
_MemGet M, M.OFFSET + 5 + _ShL(Length~&, 2), __IndexLast~&: _MemGet M, M.OFFSET + 9 + _ShL(Capacity~& + __IndexLast~&, 2), __IndexLast~&
_MemPut M, M.OFFSET + __Index~&, __IndexLast~&
' Swap Index
_MemGet M, M.OFFSET + __Index~&, __T~&
_MemGet M, M.OFFSET + 5 + _ShL(Length~&, 2), __IndexLast~&
_MemPut M, M.OFFSET + __Index~&, __IndexLast~&
_MemPut M, M.OFFSET + 5 + _ShL(Length~&, 2), __T~&
End If
_MemFree M
End Sub
Sub ContainerPrint (Container As String) Static
If Len(Container) < 10 _OrElse Asc(Container) <> 16 Then Exit Sub
Static Length~&, Capacity~&, I, __Index~&, M As _MEM
M = _Mem(_Offset(Container), Len(Container))
_MemGet M, M.OFFSET + 1, Capacity~&
_MemGet M, M.OFFSET + 5, Length~&
For I = 0 To Length~& - 1
_MemGet M, M.OFFSET + 9 + _ShL(I, 2), __Index~&
_MemGet M, M.OFFSET + 9 + _ShL(Capacity~& + __Index~&, 2), __Index~&
Print _IIf(I > 0, ",", ""); Hex$(__Index~&);
Next I
Print
_MemFree M
End Sub

