Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UDT problems
#27
For multiple arrays within a UDT you could do this.

Code: (Select All)
' Program to create an array of Doubles in a UDT
DIM AS LONG i
DIM AS DOUBLE v
CONST cMAXELEMENTS = 10

TYPE tARRAYS
  array0 AS _MEM
  array1 AS _MEM
  array2 AS _MEM
END TYPE

DIM AS tARRAYS object

SCREEN 12: _FONT 8

' initialize the array
createObject object.array0, cMAXELEMENTS
createObject object.array1, cMAXELEMENTS
createObject object.array2, cMAXELEMENTS
PRINT "ARRAY 0          ARRAY 1          ARRAY 2"
' set the array to random values
PRINT "Create initial Values"
FOR i = 0 TO cMAXELEMENTS
  v = INT(RND * 5)
  setElement object.array0, i, v
  PRINT "index:"; i; "->"; v; "   ";
  v = INT(RND * 5)
  setElement object.array1, i, v
  PRINT "index:"; i; "->"; v; "   ";
  v = INT(RND * 5)
  setElement object.array2, i, v
  PRINT "index:"; i; "->"; v
NEXT

' retrieve values from array
PRINT "Retrieve Values"
FOR i = 0 TO cMAXELEMENTS
  PRINT "index:"; i; "->"; getElement(object.array0, i); "  ";
  PRINT "index:"; i; "->"; getElement(object.array1, i); "  ";
  PRINT "index:"; i; "->"; getElement(object.array2, i)
NEXT

'resize array
resizeObject object.array0, cMAXELEMENTS * 2
resizeObject object.array1, cMAXELEMENTS * 2
resizeObject object.array2, cMAXELEMENTS * 2



' retrieve values from array
PRINT "Double the size of the array"
FOR i = 0 TO cMAXELEMENTS * 2
  PRINT "index:"; i; "->"; getElement(object.array0, i); "  ";
  PRINT "index:"; i; "->"; getElement(object.array1, i); "  ";
  PRINT "index:"; i; "->"; getElement(object.array2, i)
NEXT

'resize array
resizeObject object.array0, cMAXELEMENTS / 2
resizeObject object.array1, cMAXELEMENTS / 2
resizeObject object.array2, cMAXELEMENTS / 2

' retrieve values from array
PRINT "Half the size of the array"
FOR i = 0 TO cMAXELEMENTS / 2
  PRINT "index:"; i; "->"; getElement(object.array0, i); "  ";
  PRINT "index:"; i; "->"; getElement(object.array1, i); "   ";
  PRINT "index:"; i; "->"; getElement(object.array2, i)
NEXT


SUB createObject (o AS _MEM, size AS LONG)
  ' make array one larger than number of elements
  o = _MEMNEW((size + 1) * 8) ' 8 is the number of bytes in a double
END SUB

SUB resizeObject (o AS _MEM, size AS LONG)
  DIM AS _MEM old
  DIM AS LONG iter
  ' Copy old _MEM to a temp _MEM
  old = o
  ' Create new array of the new size
  createObject o, size
  ' Copy data from old array to new array
  iter = 0: DO WHILE iter < o.SIZE AND iter < old.SIZE
    _MEMPUT o, o.OFFSET + iter, _MEMGET(old, old.OFFSET + iter, _BYTE) AS _BYTE
  iter = iter + 1: LOOP
  ' free old array
  _MEMFREE old
END SUB

FUNCTION getElement# (o AS _MEM, element AS LONG)
  getElement = _MEMGET(o, o.OFFSET + (element * 8), DOUBLE)
END FUNCTION

SUB setElement (o AS _MEM, element AS LONG, v AS DOUBLE)
  _MEMPUT o, o.OFFSET + (element * 8), v AS DOUBLE
END SUB
Reply


Messages In This Thread
UDT problems - by eoredson - 11-12-2023, 05:46 AM
RE: UDT problems - by TerryRitchie - 11-12-2023, 05:49 AM
RE: UDT problems - by eoredson - 11-12-2023, 06:05 AM
RE: UDT problems - by mnrvovrfc - 11-12-2023, 06:33 AM
RE: UDT problems - by SpriggsySpriggs - 11-12-2023, 06:19 AM
RE: UDT problems - by eoredson - 11-12-2023, 06:42 AM
RE: UDT problems - by SMcNeill - 11-12-2023, 06:58 AM
RE: UDT problems - by TerryRitchie - 11-12-2023, 06:44 AM
RE: UDT problems - by eoredson - 11-12-2023, 06:50 AM
RE: UDT problems - by TerryRitchie - 11-12-2023, 06:57 AM
RE: UDT problems - by TerryRitchie - 11-12-2023, 06:54 AM
RE: UDT problems - by eoredson - 11-12-2023, 07:02 AM
RE: UDT problems - by eoredson - 11-12-2023, 07:09 AM
RE: UDT problems - by SMcNeill - 11-12-2023, 07:20 AM
RE: UDT problems - by eoredson - 11-12-2023, 07:22 AM
RE: UDT problems - by TerryRitchie - 11-12-2023, 07:25 AM
RE: UDT problems - by SpriggsySpriggs - 11-12-2023, 07:27 AM
RE: UDT problems - by eoredson - 11-12-2023, 07:48 AM
RE: UDT problems - by justsomeguy - 11-12-2023, 11:06 AM
RE: UDT problems - by eoredson - 11-12-2023, 11:41 PM
RE: UDT problems - by eoredson - 11-13-2023, 12:38 AM
RE: UDT problems - by SMcNeill - 11-13-2023, 12:52 AM
RE: UDT problems - by justsomeguy - 11-13-2023, 12:58 AM
RE: UDT problems - by eoredson - 11-13-2023, 01:34 AM
RE: UDT problems - by SMcNeill - 11-13-2023, 02:34 AM
RE: UDT problems - by mnrvovrfc - 11-13-2023, 01:48 AM
RE: UDT problems - by eoredson - 11-13-2023, 01:59 AM
RE: UDT problems - by justsomeguy - 11-13-2023, 02:18 AM
RE: UDT problems - by eoredson - 11-13-2023, 02:26 AM
RE: UDT problems - by eoredson - 11-13-2023, 02:52 AM
RE: UDT problems - by justsomeguy - 11-13-2023, 04:57 AM
RE: UDT problems - by eoredson - 11-13-2023, 05:20 AM



Users browsing this thread: 9 Guest(s)