In light of the problems with arrays I've been fighting for some time I made this.
#constant true = 1
arr$ = new_SArray()
for i = 0 to 20
arr$ = SArray_addEnd(arr$, random(0, 100000)/100.0)
next i
oarr$ = arr$
arr$ = SArray_bubbleSort(arr$)
do
print("Before Sort:")
for i = 0 to SArray_length(oarr$)-1
print(str(i)+": "+str(SArray_get(oarr$, i)))
next i
print("After Sort:")
for i = 0 to SArray_length(arr$)-1
print(str(i)+": "+str(SArray_get(arr$, i)))
next i
sync()
loop
function new_SArray() '{ returns SArray O(1)
out$ = "0,"
endfunction out$ '}'
function SArray_addEnd(arr$, num#) '{ returns SArray O(1)
length = SArray_length(arr$)
if length <> 0 then payload$ = mid(arr$, len(str(length))+2, -1)
numToStore$ = str(num#)
if length <> 0
newArr$ = str(length+1)+","+payload$+","+numToStore$
else
newArr$ = str(length+1)+","+numToStore$
endif
endfunction newArr$ '}
function SArray_add(arr$, num#, index) '{ returns SArray O(n)
length = SArray_length(arr$)
if length <> 0 then payload$ = mid(arr$, len(str(length))+2, -1)
numToStore$ = str(num#)
if length = 0 and index = 0
newArr$ = str(length+1)+","+numToStore$
else
leftEnd$ = ""
//parse
for i = 0 to index-1 //locate first comma
scanIndex = 1
foundComma = false
while foundComma = false
if mid(payload$, scanIndex, 1) = ","
leftEnd$ = leftEnd$ + mid(payload$, 1, scanIndex)
payload$ = mid(payload$, scanIndex+1, -1)
foundComma = true
else
scanIndex = scanIndex+1
if scanIndex = len(payload$) //break if end of content
leftEnd$ = leftEnd$ + mid(payload$, 1, scanIndex)
payload$ = ""
foundComma = true
endif
endif
endwhile
next i
if len(payload$) = 0
newArr$ = str(length+1)+","+leftEnd$+","+numToStore$
else
newArr$ = str(length+1)+","+leftEnd$+numToStore$+","+payload$
endif
endif
endfunction newArr$ '}
function SArray_set(arr$, num#, index) '{ returns SArray O(n)
length = SArray_length(arr$)
numToStore$ = str(num#)
payload$ = mid(arr$, len(str(length))+2, -1)
leftEnd$ = ""
//parse
for i = 0 to index-1 //locate first comma
scanIndex = 1
foundComma = false
while foundComma = false
if mid(payload$, scanIndex, 1) = ","
leftEnd$ = leftEnd$ + mid(payload$, 1, scanIndex)
payload$ = mid(payload$, scanIndex+1, -1)
foundComma = true
else
scanIndex = scanIndex+1
endif
endwhile
next i
scanIndex = 1 //locate index of second comma
foundComma = false
while foundComma = false
if mid(payload$, scanIndex, 1) = ","
foundComma = true
scanIndex = scanIndex //keep comma
rightEnd$ = mid(payload$, scanIndex, -1)
else
scanIndex = scanIndex+1
if scanIndex = len(payload$) //break if end of content
foundComma = true
rightEnd$ = ""
endif
endif
endwhile
newArr$ = str(length)+","+leftEnd$+numToStore$+rightEnd$
endfunction newArr$ '}
function SArray_get(arr$, index) '{ returns float O(n)
length = SArray_length(arr$)
payload$ = mid(arr$, len(str(length))+2, -1)
//parse
for i = 0 to index-1 //locate first comma
scanIndex = 1
foundComma = false
while foundComma = false
if mid(payload$, scanIndex, 1) = ","
payload$ = mid(payload$, scanIndex+1, -1)
foundComma = true
else
scanIndex = scanIndex+1
endif
endwhile
next i
scanIndex = 1 //locate index of second comma
foundComma = false
while foundComma = false
if mid(payload$, scanIndex, 1) = ","
foundComma = true
scanIndex = scanIndex-1 //go back 1 before the comma
else
scanIndex = scanIndex+1
if scanIndex = len(payload$) //break if end of content
foundComma = true
endif
endif
endwhile
out# = valFloat(mid(payload$, 1, scanIndex-1))
endfunction out# '}
function SArray_remove(arr$, index) '{ returns SArray O(n)
length = SArray_length(arr$)
payload$ = mid(arr$, len(str(length))+2, -1)
leftEnd$ = ""
//parse
for i = 0 to index-1 //locate first comma
scanIndex = 1
foundComma = false
while foundComma = false
if mid(payload$, scanIndex, 1) = ","
leftEnd$ = leftEnd$ + mid(payload$, 1, scanIndex)
payload$ = mid(payload$, scanIndex+1, -1)
foundComma = true
else
scanIndex = scanIndex+1
endif
endwhile
next i
scanIndex = 1 //locate index of second comma
foundComma = false
while foundComma = false
if mid(payload$, scanIndex, 1) = ","
foundComma = true
scanIndex = scanIndex+1 //go 1 past comma
rightEnd$ = mid(payload$, scanIndex, -1)
else
scanIndex = scanIndex+1
if scanIndex = len(payload$) //break if end of content
foundComma = true
rightEnd$ = ""
if length <> 1 then leftEnd$ = left(leftEnd$, len(leftEnd$)-2) //remove comma if left end has content
endif
endif
endwhile
newArr$ = str(length-1)+","+leftEnd$+rightEnd$
endfunction newArr$ '}
function SArray_length(arr$) '{ returns integer O(1)
scanIndex = 1 //locate index of comma
foundComma = false
while foundComma = false
if mid(arr$, scanIndex, 1) = ","
foundComma = true
scanIndex = scanIndex-1 //back before comma
else
scanIndex = scanIndex+1
endif
endwhile
length = val(left(arr$, scanIndex))
endfunction length '}
function SArray_bubbleSort(arr$) '{ returns SArray O(n^3)
i = 0
while i < SArray_length(arr$)-1
geti# = SArray_get(arr$, i)
geti1# = SArray_get(arr$, i+1)
if geti# > geti1#
arr$ = SArray_set(arr$, geti1#, i)
arr$ = SArray_set(arr$, geti#, i+1)
if i <> 0 then i = i-1
else
i = i+1
endif
endwhile
newArr$ = arr$
endfunction newArr$ '}
It's essentially a dynamic array of floats from index 0 to size-1. The code wouldn't be too hard to adapt for integers, but I felt using floats would provide a better basis. I don't at all intend to replace AGK's arrays with this inefficient alternative but these arrays give you some unique powers. For example, you can have 2D arrays of varied lengths. Create an array of SArrays
and each index can contain an array of independent length. They can also be passed in an out of functions and can be created within functions without concern for AppGameKit errors. Of course they don't support UDTs, but if you're willing to go digging deep into the realm of Strings they certainly could
Just thought I'd share it as an alternative
I included bubble sort mostly as a method of testing. Don't mock the sort
. It took under a minute to write!
Edit: If you're here it's likely you'd prefer
this.