Oxygen Basic
Programming => Problems & Solutions => Topic started by: Brian Alvarez on November 24, 2018, 01:56:22 PM
-
This code is failing and I don't know how to fix it, actually it should work i think.
the only thing that changes is the datatype. With strings it works, with numbers,
it fails:
'=======================================================
macro reads_arr_defs(dtype)
function c(int d1, dtype v) {dat[d1] = v}
function c(int d1) as dtype {return dat[d1]}
function c(int d1, d2, dtype v) {dat[d1 * d2] = v}
function c(int d1, d2) as dtype {return dat[d1 * d2]}
function c(int d1, d2, d3, dtype v) {print "1/2: " & str(d1 * d2 + d3) : dat[d1 * d2 + d3] = v}
function c(int d1, d2, d3) as dtype {print "2/2: " & str(d1 * d2 + d3) : return dat[d1 * d2 + d3]}
function c(int d1, d2, d3, d4, dtype v) {dat[(d1 * d2 * d3) + d4] = v}
function c(int d1, d2, d3, d4) as dtype {return dat[(d1 * d2 * d3) + d4]}
end macro
macro dimension_storage(dtype)
method constructor(int * d, n)
dims = n / 2
int i, d1, d2
for i = 1 to dims
d1 = d[i+0]
d2 = d[i+1]
bnd[i+0] = d1
bnd[i+1] = d2
elem += (d2-d1)
next
end method
end macro
#def class_nam_def class array_%1
macro declare_array_type(dtype)
class_nam_def(dtype)
dtype dat[10000]
int dims, elem
int bnd[10]
dimension_storage(dtype)
reads_arr_defs(dtype)
end class
end macro
'=======================================================
print "testing overflow for strings (2 steps)."
declare_array_type(string)
new array_string sss(int{0, 100, 0, 100, 0, 100}, countof)
sss.c(10, 10, 10) = "1537"
print ">>>>" & sss.c(10, 10, 10)
print "testing overflow for long (2 steps)."
declare_array_type(int)
new array_int fff(int{0, 100, 0, 100, 0, 100}, countof)
fff.c(10, 10, 10) = 1537 ' <---- is never fired up!
print ">>>>" & fff.c(10, 10, 10)
-
If i comment out the last 2 overflow functions, it works fine. Does it have an internal limit?
For now, the limit will be 3 dimensions then...
-
It's a little hard to follow, but I understand you want to produce classes supporting indeterminate numbers of dimensions. Is that right?
-
Yes. Of all types.
-
This is one possible solution which will require a minor o2 update.
'N DIMENSIONAL
def UseMultidim
===============
class Multidim%1
================
'
'order minor to major
indexbase 0
'
int dms[63] 'up to 64 dimensions
int dn 'number of dimensions
int sz 'number of bytes
sys bu 'dynamic buffer
'
function constructor(int*dm, n)
===============================
int i
dms[0]=dm[0]+1
for i=1 to n-1
dms[i]=dm[i]+1
dms[i]*=dms[i-1]
next
dn=n
sz=dms[n-1]*sizeof %1
bu=getmemory sz
end function
'
function destructor()
=====================
'del all members
#if typecodeof(%1)>0xa0 'string types and above
%1 *v
int i
for i=0 to sz-1 step sizeof(%1)
@v=bu+i
del v
next
#endif
freememory bu
dn=0 : sz=0 : bu=0
end function
'
function locator(int*d) as sys
==============================
int i
int j=d[0]
for i=1 to dn-1
j+=dms[i-1]*d[i]
next
return j*sizeof(%1)+bu
end function
'
function vf(int*d) as %1
========================
%1 v at locator(d)
function=v
end function
'
function vf(int*d, %1 *a)
=========================
%1 v at locator(d)
v=a
end function
'
end class
end def
'TESTS:
=======
UseMultiDim float
'new Multidim m(int{7,3,1},countof)
'print m.sz '256
'print m.locate(int{0,0,0})
'print m.locate(int{0,0,1})
'print m.locate(int{0,1,0})
'print m.locate(int{1,0,0})
'print m.locate(int{2,1,1})
'm.vf(int{2,2,1})=12.5
'print m.vf(int{2,2,1})
'def mvf m.vf(int{%1}) 'syntax compaction
'mvf(2,2,1)=42
'float f=200+mvf(2,2,1)+100 '+mvf(2,2,1)
'del m
'
def NewMultidim
===============
new Multidim%1 %2(int{%3},countof)
def %2vf
%2.vf(int{%%1})
end def
end def
'
def Multidim
===============
Multidim%1 %2
%2.constructor(int{%3},countof)
def %2vf
%2.vf(int{%%1})
end def
end def
'
NewMultidim float m(7,3,1)
Multidim float m(7,3,1)
mvf(2,2,1)=10
mvf(1,2,1)=1
float f=1000+mvf(2,2,1)+mvf(1,2,1)+100
f '1111
del m
UseMultidim float
Multidim float m(7,3,1)
mvf(2,2,1)=10
mvf(1,2,1)=1
float f=1000+mvf(2,2,1)+mvf(1,2,1)+100
'print f '1111
-
I also gave a consideration to that Charles, but i think it would take too much CPU at execution.
I think using overflowing is better, because although it takes slightly longer to compile (unperceptible to humans),
it produces faster code, because the locator is simpler. At least thats what i think.
With the overflow, there are less operations to make everytime an element is accessed. No rush,
for now 3 dimensions are excelent and anybody will be able to modify this core functions to improve them
at a later time.