I was playing around in DBC, and I've found a way to dynamically change the size of an array. The method is actually quite simple - Clone the existing array, UnDim the original array, ReDim the original array with an increased size, and copy the data back.
Array Insert at Bottom:
rem Dynamic Arrays in DBC!
Dim MainArray(0)
Dim CloneArray(0)
ArrayCounter = 0
One = Timer()
For I = 1 to 1000
rem Array Insert at Bottom
rem Put all data into the Clone Array, ReDim the MainArray
rem with one extra element and transfer all data back over
UnDim CloneArray(0)
Dim CloneArray(ArrayCounter)
For T = 0 to ArrayCounter
CloneArray(T) = MainArray(T)
Next T
UnDim MainArray(0)
Inc ArrayCounter
Dim MainArray(ArrayCounter)
For T = 0 to (ArrayCounter - 1)
MainArray(T) = CloneArray(T)
Next T
Next I
rem At this point MainArray has (1000) elements!
Two = Timer()
Print Two - One
wait key
End
Example
rem Dynamic Arrays in DBC!
Dim MainArray(10)
Dim CloneArray(10)
ArrayCounter = 10
For T = 0 to 10
MainArray(T) = T
Next T
One = Timer()
`For I = 1 to 1000
rem Array Insert at Bottom
rem Put all data into the Clone Array, ReDim the MainArray
rem with one extra element and transfer all data back over
UnDim CloneArray(0)
Dim CloneArray(ArrayCounter)
For T = 0 to ArrayCounter
CloneArray(T) = MainArray(T)
Next T
UnDim MainArray(0)
Inc ArrayCounter
Dim MainArray(ArrayCounter)
For T = 0 to (ArrayCounter - 1)
MainArray(T) = CloneArray(T)
Next T
`Next I
rem At this point MainArray has (1000) elements!
Two = Timer()
Print Two - One
Print "Output"
For T = 0 to ArrayCounter
Print MainArray(T)
Next T
wait key
End
Array Insert at Top:
rem Dynamic Arrays in DBC!
Dim MainArray(0)
Dim CloneArray(0)
ArrayCounter = 0
One = Timer()
For I = 1 to 1000
rem Array Insert at Top
rem Put all data into the Clone Array, ReDim the MainArray
rem with one extra element and transfer all data back over
UnDim CloneArray(0)
Dim CloneArray(ArrayCounter)
For T = 0 to ArrayCounter
CloneArray(T) = MainArray(T)
Next T
UnDim MainArray(0)
Inc ArrayCounter
Dim MainArray(ArrayCounter)
For T = 0 to (ArrayCounter - 1)
MainArray(T+1) = CloneArray(T)
Next T
Next I
rem At this point MainArray has (1000) elements!
Two = Timer()
Print Two - One
wait key
End
Example:
rem Dynamic Arrays in DBC!
Dim MainArray(10)
Dim CloneArray(10)
ArrayCounter = 10
For T = 0 to ArrayCounter
MainArray(T) = T
Next T
One = Timer()
`For I = 1 to 1000
rem Array Insert at Top
rem Put all data into the Clone Array, ReDim the MainArray
rem with one extra element and transfer all data back over
UnDim CloneArray(0)
Dim CloneArray(ArrayCounter)
For T = 0 to ArrayCounter
CloneArray(T) = MainArray(T)
Next T
UnDim MainArray(0)
Inc ArrayCounter
Dim MainArray(ArrayCounter)
For T = 0 to (ArrayCounter - 1)
MainArray(T+1) = CloneArray(T)
Next T
`Next I
rem At this point MainArray has (1000) elements!
Two = Timer()
Print Two - One
rem There is now a second zero at the top of the list!
Print "Output"
For T = 0 to ArrayCounter
Print MainArray(T)
Next T
wait key
End
Array Insert at Element:
rem Dynamic Arrays in DBC!
Dim MainArray(0)
Dim CloneArray(0)
ArrayCounter = 0
Element = 0
One = Timer()
For I = 1 to 1000
rem Array Insert at Element
rem Put all data into the Clone Array, ReDim the MainArray
rem with one extra element and transfer all data back over
rem Leave a gap around the "Element" index.
UnDim CloneArray(0)
Dim CloneArray(ArrayCounter)
For T = 0 to ArrayCounter
CloneArray(T) = MainArray(T)
Next T
UnDim MainArray(0)
Inc ArrayCounter
Dim MainArray(ArrayCounter)
rem Faster way to do this?
For T = 0 to (ArrayCounter - 1)
If T < Element
MainArray(T) = CloneArray(T)
else
MainArray(T+1) = CloneArray(T)
endif
Next T
Next I
Two = Timer()
Print Two - One
wait key
End
Example:
rem Dynamic Arrays in DBC!
Dim MainArray(10)
Dim CloneArray(10)
ArrayCounter = 10
rem Fill test array with numbers 0-10
For T = 0 to 10
MainArray(T) = T
Next T
rem Array index that is to be set to zero
Element = 5
One = Timer()
rem ***********************************
rem Array Insert at Element
rem Put all data into the Clone Array, ReDim the MainArray
rem with one extra element and transfer all data back over
rem Leave a gap around the "Element" index.
UnDim CloneArray(0)
Dim CloneArray(ArrayCounter)
For T = 0 to ArrayCounter
CloneArray(T) = MainArray(T)
Next T
UnDim MainArray(0)
Inc ArrayCounter
Dim MainArray(ArrayCounter)
rem Faster way to do this?
For T = 0 to (ArrayCounter - 1)
If T < Element
MainArray(T) = CloneArray(T)
else
MainArray(T+1) = CloneArray(T)
endif
Next T
rem ***********************************
Two = Timer()
rem Time to execute
Print Two - One
rem There is now a zero in element (5)
Print "Output"
For T = 0 to ArrayCounter
Print MainArray(T)
Next T
wait key
End
It seems to work quite well, although there seems to be a slight slow down with large array sizes - at 10000 entries it takes 3ms to add the next one!
We spend our lives chasing dreams. Dark Basic lets us catch some of them.