'=========================================================================== ' Subject: SWAP ARRAYS IN PB Date: 04-04-96 (06:50) ' Author: Dave Navarro, Jr. Code: PB ' Origin: comp.lang.basic.misc Packet: PB.ABC '=========================================================================== ' The following example shows how useful ArrayInfo() is. ' ' It will allow you to completely swap two arrays of like type and ' element size. ' ' Swapping each element individually is slow, and all we really need to ' do is swap the array descriptors. ' ' Use at your own risk! ' ' Donated to the Public Domain ' by Dave Navarro, Jr. %TotalElements = 1 %ElementSize = 2 %ElementsPerPage = 3 %ArrayType = 4 %SubscriptCount = 5 DEFINT A-Z DECLARE FUNCTION ArrayInfo(BYVAL Code%, BYVAL Pointr AS DWORD) AS LONG DIM X(1:5) DIM Y(1:5) X(1) = 200 'assign a value to the first element of X PRINT X(1), Y(1) 'print the value of X(1) and Y(1) ArraySwap X(), Y() 'Do NOT use element numbers. Empty parenthesis ' tell PowerBASIC to pass the array descriptor. PRINT X(1), Y(1) 'print the value of X(1) and Y(2), should be swapped END SUB ArraySwap(ANY, ANY) PUBLIC DIM Pointer1 AS DWORD DIM Pointer2 AS DWORD DIM Temp AS STRING * 64 ! push DS ! les DI, [BP+10] ;get pointer to first array ! mov Pointer1[0], DI ;store offset in Pointer1 ! mov Pointer1[2], ES ;store segment in Pointer1 ! les DI, [BP+6] ;get pointer to second array ! mov Pointer2[0], DI ;store offset in Pointer2 ! mov Pointer2[2], ES ;store segment in Pointer2 ! pop DS 'If either array is not dimensioned, then exit IF (ArrayInfo(%TotalElements, Pointer1) * ArrayInfo(%TotalElements, Pointer2)) = 0 THEN EXIT SUB END IF 'If both array types match, swap the descriptors ' make sure the element sizes are the same in case of fixed length IF (ArrayInfo(%ArrayType, Pointer1) = ArrayInfo(%ArrayType, Pointer2)) AND (ArrayInfo(%ElementSize, Pointer1) = ArrayInfo(%ElementSize, Pointer2)) THEN ! push DS ! lds SI, [BP+6] ;point to second array ! lea BX, Temp ;point BX at Temp buffer ! mov DI, BX ;put offset in DI ! push DI ;save offset for later ! push SS ;save segment for later ! push SS ;push stack segment on the stack ! pop ES ;and put it in ES ! mov CX, 32 ;descriptor is 32 words long ! rep movsw ;copy descriptor to temp buffer ! lds SI, [BP+10] ;point to first array ! les DI, [BP+6] ;point to second array ! mov CX, 32 ;descriptor is 32 words long ! rep movsw ;copy first descriptor to second descriptor ! les DI, [BP+10] ;point to first array ! pop DS ;restore segment of Temp buffer ! pop SI ;restore offset of Temp buffer ! mov CX, 32 ;descriptor is 32 words long ! rep movsw ;copy second descriptor to first descriptor ! pop DS END IF END SUB