Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UDT problems
#31
Here is yet another update. Now it can have mixed data type arrays within a UDT.
Code: (Select All)
' Program to create an arrays in a UDT
SCREEN 12: _FONT 8
_TITLE "UDT_ARRAYS.bas"
DIM AS LONG i
DIM AS DOUBLE v
CONST cMAXELEMENTS = 10

' set byte counts for the data types
CONST cDT_BYTE = 1
CONST cDT_INTEGER = 2
CONST cDT_LONG = 4
CONST cDT_SINGLE = 4
CONST cDT_DOUBLE = 8
CONST cDT_INTEGER64 = 8
CONST cDT_FLOAT = 32

TYPE tARRAYS
  UDT_Array0 AS _MEM ' Array of Doubles
  UDT_Array1 AS _MEM ' Array of Singles
  UDT_Array2 AS _MEM ' Array of Integers
END TYPE

DIM AS tARRAYS UDT

' initialize the array
createUDTArray UDT.UDT_Array0, cDT_DOUBLE, cMAXELEMENTS
createUDTArray UDT.UDT_Array1, cDT_SINGLE, cMAXELEMENTS
createUDTArray UDT.UDT_Array2, cDT_INTEGER, cMAXELEMENTS
PRINT "ARRAY Doubles       ARRAY Singles        ARRAY Integers"
' set the array to random values
PRINT "Create initial Values, Store them, and retrieve"
FOR i = 0 TO cMAXELEMENTS
  v = INT(RND * 5 * 100) / 100
  setElementDBL UDT.UDT_Array0, i, v
  PRINT USING "index: ##-> #.##    "; i; getElementDBL(UDT.UDT_Array0, i);
  v = INT(RND * 5 * 100) / 100
  setElementSIGL UDT.UDT_Array1, i, v
  PRINT USING "index: ##-> #.##    "; i; getElementSIGL(UDT.UDT_Array1, i);
  v = INT(RND * 5)
  setElementINT UDT.UDT_Array2, i, v
  PRINT USING "index: ##-> #.##"; i; getElementINT(UDT.UDT_Array2, i)
NEXT

'resize array
resizeUDTArray UDT.UDT_Array0, cDT_DOUBLE, cMAXELEMENTS * 2
resizeUDTArray UDT.UDT_Array1, cDT_SINGLE, cMAXELEMENTS * 2
resizeUDTArray UDT.UDT_Array2, cDT_INTEGER, cMAXELEMENTS * 2

' retrieve values from array
PRINT "Double the size of the array"
FOR i = 0 TO cMAXELEMENTS * 2
  PRINT USING "index: ##-> #.##    "; i; getElementDBL(UDT.UDT_Array0, i);
  PRINT USING "index: ##-> #.##    "; i; getElementSIGL(UDT.UDT_Array1, i);
  PRINT USING "index: ##-> #.##"; i; getElementINT(UDT.UDT_Array2, i)
NEXT

'resize array
resizeUDTArray UDT.UDT_Array0, cDT_DOUBLE, cMAXELEMENTS / 2
resizeUDTArray UDT.UDT_Array1, cDT_SINGLE, cMAXELEMENTS / 2
resizeUDTArray UDT.UDT_Array2, cDT_INTEGER, cMAXELEMENTS / 2

' retrieve values from array
PRINT "Half the size of the array"
FOR i = 0 TO cMAXELEMENTS / 2
  PRINT USING "index: ##-> #.##    "; i; getElementDBL(UDT.UDT_Array0, i);
  PRINT USING "index: ##-> #.##    "; i; getElementSIGL(UDT.UDT_Array1, i);
  PRINT USING "index: ##-> #.##"; i; getElementINT(UDT.UDT_Array2, i)
NEXT


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

SUB resizeUDTArray (o AS _MEM, dt AS LONG, 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
  createUDTArray o, dt, size
  'clear new array
  iter = 0: DO WHILE iter < o.SIZE
    _MEMPUT o, o.OFFSET + iter, 0 AS _BYTE
  iter = iter + 1: LOOP
  ' 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 getElementBYTE%% (o AS _MEM, element AS LONG)
  getElementBYTE = _MEMGET(o, o.OFFSET + (element * cDT_BYTE), _BYTE)
END FUNCTION

SUB setElementBYTE (o AS _MEM, element AS LONG, v AS _BYTE)
  _MEMPUT o, o.OFFSET + (element * cDT_BYTE), v AS _BYTE
END SUB

FUNCTION getElementINT% (o AS _MEM, element AS LONG)
  getElementINT = _MEMGET(o, o.OFFSET + (element * cDT_INTEGER), INTEGER)
END FUNCTION

SUB setElementINT (o AS _MEM, element AS LONG, v AS INTEGER)
  _MEMPUT o, o.OFFSET + (element * cDT_INTEGER), v AS INTEGER
END SUB

FUNCTION getElementLNG& (o AS _MEM, element AS LONG)
  getElementLNG = _MEMGET(o, o.OFFSET + (element * cDT_LONG), LONG)
END FUNCTION

SUB setElementLNG (o AS _MEM, element AS LONG, v AS LONG)
  _MEMPUT o, o.OFFSET + (element * cDT_LONG), v AS LONG
END SUB

FUNCTION getElementSIGL! (o AS _MEM, element AS LONG)
  getElementSIGL = _MEMGET(o, o.OFFSET + (element * cDT_SINGLE), SINGLE)
END FUNCTION

SUB setElementSIGL (o AS _MEM, element AS LONG, v AS SINGLE)
  _MEMPUT o, o.OFFSET + (element * cDT_SINGLE), v AS SINGLE
END SUB

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

SUB setElementDBL (o AS _MEM, element AS LONG, v AS DOUBLE)
  _MEMPUT o, o.OFFSET + (element * cDT_DOUBLE), v AS DOUBLE
END SUB

FUNCTION getElementINT64&& (o AS _MEM, element AS LONG)
  getElementINT64 = _MEMGET(o, o.OFFSET + (element * cDT_INTEGER64), _INTEGER64)
END FUNCTION

SUB setElementINT64 (o AS _MEM, element AS LONG, v AS _INTEGER64)
  _MEMPUT o, o.OFFSET + (element * cDT_INTEGER64), v AS _INTEGER64
END SUB

FUNCTION getElementFLT## (o AS _MEM, element AS LONG)
  getElementFLT = _MEMGET(o, o.OFFSET + (element * cDT_FLOAT), _FLOAT)
END FUNCTION

SUB setElementFLT (o AS _MEM, element AS LONG, v AS _FLOAT)
  _MEMPUT o, o.OFFSET + (element * cDT_FLOAT), v AS _FLOAT
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: 1 Guest(s)