Hi Mike,
Here is a set of macros for managing dynamic arrays of any flat type. by casting dynamic arrays as bstrings, they can be pointer-swapped, copied, resized, appended, inserted and deleted, and anything else you can do with a string. The underlying olestring, has a record of its byte length so the element count can be derived.
I think some of it, at least, might be useful for OxyLisp.
The key to these macros is
def DynRef cast bstring @
dynamic.inc
'DYNAMIC ARRAYS - FLAT TYPES ONLY
def Dynamic 'type varname
=========================
%1 * %2
end def
def DynRef cast bstring @
=========================
def DynDim 'type varname(quantity)
==================================
dim %1 * %2
@%2=getmemory sizeof(%1)*
end def
'not for arrays of strings & bstrings
'flat types only
def DynClone 'clone = original
==============================
typeof %3 * %1
DynRef %1 = DynRef %3
end def
def DynRedim 'varname(quantity)
===============================
scope
sys q1=len DynRef %1
sys q2=sizeof(%1) * %2
if q2>q1 then
DynRef %1 += nuls q2-q1
elseif q2<q1 then
DynRef %1 = left DynRef %1,q2
end if
end scope
end def
def DynExpand 'varname(quantity)
================================
scope
sys q1 = len(DynRef %1)
sys q2 = (%2)*sizeof(%1)
if q2>q1 then
DynRef %1 +=nuls q2-q1
end if
end scope
end def
macro DynGetFile(n,d) 'filename, array
======================================
DynRef d=GetFile n
end macro
macro DynPutFile(n,d) 'filename, array
======================================
putfile n,DynRef d
end macro
macro DynBytes(v) 'return byte count
====================================
len(DynRef v)
end macro
macro DynCount(v) 'return element count
=======================================
len(DynRef v)/sizeof(v)
end macro
macro DynAppend(v,w)
====================
DynRef v += DynRef w
macro DynInsert(v,w,i) 'String,InsString,elOffset
=================================================
DynRef v=left(DynRef v,i*sizeof(v)) &
DynRef w &
mid(DynRef v,1+i*sizeof(v))
end macro
macro DynDelete(v,i,n) 'String, elOffset, n to delete
=====================================================
DynRef v=left(DynRef v,i*sizeof(v)) &
mid(DynRef v,1+(i+n)*sizeof(v))
end macro
macro DynSwap(a,b) 'array a, array b
====================================
scope
sys _a=@a
@a=@b
@b=_a
end scope
end macro
macro DynFree(v) 'array
=======================
freememory @v
@v=0
end macro
EXAMPLES
dynamic1
includepath "$/inc/"
include "dynamic.inc"
include "console.inc"
indexbase 0
sys nx=100,ny=50
DynDim double d (nx*ny)
d(50+10*nx)=123
DynClone e = d
DynRedim e(nx*ny/2)
'print recordof d
print "array d" cr
print "value " & str(d(50+10*nx)) & cr
print "count " & str(DynCount d) & cr
print "bytes " & str(DynBytes d) & cr
print "array e" cr
print "value " & str(e(50+10*nx)) & cr
print "count " & str(DynCount e) & cr
print "bytes " & str(DynBytes e) & cr
Waitkey
DynFree d
DynFree e
Dynamic2
includepath "$/inc/"
include "dynamic.inc"
include "console.inc"
indexbase 0
DynDim sys a(1000)
a[2]=123
DynExpand a(2000)
Dynputfile "t.txt", a
printl DynBytes a
printl DynCount a
sys*b
DynGetFile "t.txt", b
printl DynCount b
DynInsert a,b,100
printl DynCount a
DynDelete a,10,5
printl DynCount a
printl a[2]
printl a[97] 'a[102-5]
waitkey
putfile "t.txt"," "
DynFree a
DynFree b