From f3d254c7b6e1dcd2df49fc3f7a54b2e0619b4b93 Mon Sep 17 00:00:00 2001 From: Ayush Mehra <97939637+amehra-ni@users.noreply.github.com> Date: Thu, 16 Jun 2022 23:07:25 +0530 Subject: [PATCH] Unexpected Output from delete from array (#700) Changed how the non-flat array gets copied from the CopySubarray function. The changes are done for non-flat type arrays and for flat type it's still the same. I have used what we have for elementType->CopyData so that a proper copy(deep copy) happens. --- source/core/Array.cpp | 40 +++++++++---- .../ArrayDeleteMultiDimensionString.vtr | 21 +++++++ .../ArrayDeleteMultiDimensionString.via | 60 +++++++++++++++++++ test-it/testList.json | 1 + 4 files changed, 110 insertions(+), 12 deletions(-) create mode 100644 test-it/ExpectedResults/ArrayDeleteMultiDimensionString.vtr create mode 100644 test-it/ViaTests/ArrayDeleteMultiDimensionString.via diff --git a/source/core/Array.cpp b/source/core/Array.cpp index df1b97120..0a5fd8c0f 100644 --- a/source/core/Array.cpp +++ b/source/core/Array.cpp @@ -1201,15 +1201,31 @@ VIREO_FUNCTION_SIGNATURET(ArrayMaxMinInternal, FindArrayMaxMinInstruction) } // NOLINT(runtime/references) static void CopySubArray(AQBlock1* &sourcePtr, AQBlock1* &destinationPtr, // NOLINT(runtime/references) - const UInt32 elementSize, const size_t elementsToCopy) + const size_t elementsToCopy, TypedArrayCoreRef arraySource, TypedArrayCoreRef arrayDest) { - if (elementsToCopy) { - size_t bytesToCopy = elementsToCopy * elementSize; - if (destinationPtr) { - memmove(destinationPtr, sourcePtr, bytesToCopy); - sourcePtr += bytesToCopy, destinationPtr += bytesToCopy; - } else { - sourcePtr += bytesToCopy; + NIError err = kNIError_Success; + TypeRef elementType = arraySource->ElementType(); + if (elementType->IsFlat()) { + if (elementsToCopy) { + size_t bytesToCopy = elementsToCopy * elementType->TopAQSize(); + if (destinationPtr) { + memmove(destinationPtr, sourcePtr, bytesToCopy); + sourcePtr += bytesToCopy, destinationPtr += bytesToCopy; + } else { + sourcePtr += bytesToCopy; + } + } + } else { + IntIndex stride = elementType->TopAQSize(); + IntIndex count = elementsToCopy; + for (Int32 i = 0; i < count; i++) { + err = elementType->CopyData(sourcePtr, destinationPtr); + if (err != kNIError_Success) { + arrayDest->Resize1D(0); + break; + } + sourcePtr += stride; + destinationPtr += stride; } } } @@ -1316,11 +1332,11 @@ VIREO_FUNCTION_SIGNATURE7(ArrayDeleteND, TypedArrayCoreRef, StaticType, void, size_t numberOfElementsToBeDeleted = deletedPortionLength * numberOfElementsInDeletedDimension; size_t numberOfElementsAfterDeleted = (dimensionSize[dimensionToDelete] - (startIndex + deletedPortionLength)) * numberOfElementsInDeletedDimension; -Int32 currentDimension; + Int32 currentDimension; do { - CopySubArray(inputArrayPtr, outputArrayPtr, elementSize, numberOfElementsBeforeDeleted); - CopySubArray(inputArrayPtr, deletedArrayPtr, elementSize, numberOfElementsToBeDeleted); - CopySubArray(inputArrayPtr, outputArrayPtr, elementSize, numberOfElementsAfterDeleted); + CopySubArray(inputArrayPtr, outputArrayPtr, numberOfElementsBeforeDeleted, arrayIn, arrayOut); + CopySubArray(inputArrayPtr, deletedArrayPtr, numberOfElementsToBeDeleted, arrayIn, deletedArray); + CopySubArray(inputArrayPtr, outputArrayPtr, numberOfElementsAfterDeleted, arrayIn, arrayOut); currentDimension = dimensionToDelete; while (--currentDimension >= 0 && ++index[currentDimension] >= dimensionSize[currentDimension]) index[currentDimension] = 0; diff --git a/test-it/ExpectedResults/ArrayDeleteMultiDimensionString.vtr b/test-it/ExpectedResults/ArrayDeleteMultiDimensionString.vtr new file mode 100644 index 000000000..33eff9cb6 --- /dev/null +++ b/test-it/ExpectedResults/ArrayDeleteMultiDimensionString.vtr @@ -0,0 +1,21 @@ +Loop no - 1 +Input 4D Array of String +(((('a1' 'a2' 'a3' 'a4') ('b1' 'b2' 'b3' 'b4') ('c1' 'c2' 'c3' 'c4') ('d1' 'd2' 'd3' 'd4') ('e1' 'e2' 'e3' 'e4') ('f1' 'f2' 'f3' 'f4') ('g1' 'g2' 'g3' 'g4')))) +Deleted Array +((('b1' 'b2' 'b3' 'b4'))) +Output Array +(((('a1' 'a2' 'a3' 'a4') ('c1' 'c2' 'c3' 'c4') ('d1' 'd2' 'd3' 'd4') ('e1' 'e2' 'e3' 'e4') ('f1' 'f2' 'f3' 'f4') ('g1' 'g2' 'g3' 'g4')))) +Loop no - 2 +Input 4D Array of String +(((('a1' 'a2' 'a3' 'a4') ('b1' 'b2' 'b3' 'b4') ('c1' 'c2' 'c3' 'c4') ('d1' 'd2' 'd3' 'd4') ('e1' 'e2' 'e3' 'e4') ('f1' 'f2' 'f3' 'f4') ('g1' 'g2' 'g3' 'g4')))) +Deleted Array +((('b1' 'b2' 'b3' 'b4'))) +Output Array +(((('a1' 'a2' 'a3' 'a4') ('c1' 'c2' 'c3' 'c4') ('d1' 'd2' 'd3' 'd4') ('e1' 'e2' 'e3' 'e4') ('f1' 'f2' 'f3' 'f4') ('g1' 'g2' 'g3' 'g4')))) +Loop no - 3 +Input 4D Array of String +(((('a1' 'a2' 'a3' 'a4') ('b1' 'b2' 'b3' 'b4') ('c1' 'c2' 'c3' 'c4') ('d1' 'd2' 'd3' 'd4') ('e1' 'e2' 'e3' 'e4') ('f1' 'f2' 'f3' 'f4') ('g1' 'g2' 'g3' 'g4')))) +Deleted Array +((('b1' 'b2' 'b3' 'b4'))) +Output Array +(((('a1' 'a2' 'a3' 'a4') ('c1' 'c2' 'c3' 'c4') ('d1' 'd2' 'd3' 'd4') ('e1' 'e2' 'e3' 'e4') ('f1' 'f2' 'f3' 'f4') ('g1' 'g2' 'g3' 'g4')))) diff --git a/test-it/ViaTests/ArrayDeleteMultiDimensionString.via b/test-it/ViaTests/ArrayDeleteMultiDimensionString.via new file mode 100644 index 000000000..6a2e1f505 --- /dev/null +++ b/test-it/ViaTests/ArrayDeleteMultiDimensionString.via @@ -0,0 +1,60 @@ +define (WebApp%3A%3Aindex%2Egviweb dv(.VirtualInstrument ( + Locals: c( // Data Space + e(dv(.String 'WebApp::index.gviweb')local0) + ce(dv(.Int32 3)c1) + ce(dv(.Int32 0)c2) + e(.Int32 local3) + e(.Int32 local4) + ce(dv(.Int32 10)c5) + e(.UInt32 local6) + ce(dv(.Int32 1)c7) + ce(dv(a(.String * * * *) (((('a1' 'a2' 'a3' 'a4' )('b1' 'b2' 'b3' 'b4' )('c1' 'c2' 'c3' 'c4' )('d1' 'd2' 'd3' 'd4' )('e1' 'e2' 'e3' 'e4' )('f1' 'f2' 'f3' 'f4' )('g1' 'g2' 'g3' 'g4' )))))c8) + e(a(.String * * * *) local9) + e(a(.String * * *) local10) + de(a(.String * * *) dataItem_DeletedPortion) + e(a(.String * * *) local12) + de(a(.String * * * *) dataItem_ArrayWithSubsetDeleted) + e(a(.String * * * *) local14) + e(.UInt32 local15) + e(.Boolean local16) + ce(dv(.Int32 0)c17) + e(dv(.Int32 2)local18) + ) + clump(1 + DebugPoint("1;{::VireoDevSystem::Interactive::WebApp::index.gviweb::#12##WebApp;}" c1) + IsGE(c2 c1 local16) + BranchIfTrue(1 local16) + Branch(2) + Perch(2) + Copy(c17 local3) + Copy(c1 local4) + Perch(3) + Convert(c5 local6) + DebugPoint("1;{::VireoDevSystem::Interactive::WebApp::index.gviweb::#23d77726c0714674bcf64c1054667ee4##WebApp;}" c5 c7 c8) + ArrayDelete(local9 local10 c8 * c7 local18 ) + Copy(local10 dataItem_DeletedPortion) + DebugPoint("1;{::VireoDevSystem::Interactive::WebApp::index.gviweb::#110262b5adda4912928eedd243f5bf67##WebApp;}" local9 local10) + Copy(local9 dataItem_ArrayWithSubsetDeleted) + WaitMilliseconds(local6 * ) + DebugPoint("1;{::VireoDevSystem::Interactive::WebApp::index.gviweb::#58099fec38a94a8297cf980d7e70ae5a##WebApp;}" ) + Increment(local3 local3 ) + Printf("Loop no - ") + Printf("%d\n" local3) + Println("Input 4D Array of String") + Println(c8) + Println("Deleted Array") + Println(dataItem_DeletedPortion) + Println("Output Array") + Println(dataItem_ArrayWithSubsetDeleted) + BranchIfGE(4 local3 local4) + Branch(3) + Perch(4) + Branch(0) + Perch(1) + Branch(0) + Perch(0) + DebugPoint("1;{::VireoDevSystem::Interactive::WebApp::index.gviweb::#e9c42849feb046fabc82ca61e2684d21##WebApp;}" ) +/* Clump Ended. */ ) +))) +enqueue (WebApp%3A%3Aindex%2Egviweb) +//Finished!! :D diff --git a/test-it/testList.json b/test-it/testList.json index 8969d4c77..1200b7542 100644 --- a/test-it/testList.json +++ b/test-it/testList.json @@ -110,6 +110,7 @@ "ArrayDelete.via", "ArrayDeleteFail.via", "ArrayDeleteFail2.via", + "ArrayDeleteMultiDimensionString.via", "ArrayDemo.via", "ArrayFillNDV.via", "ArrayFillNDVFail.via",