@@ -3748,90 +3748,80 @@ public MatrixBlock append(MatrixBlock[] that, MatrixBlock result, boolean cbind)
37483748 else
37493749 result .reset (m , n , sp , nnz );
37503750
3751- if ( !result .sparse && nnz !=0 )
3752- return appendDense (that , result , cbind , nnz , shallowCopy , m , n );
3753- else if (nnz != 0 )
3754- return appendSparse (that , result , cbind , nnz , shallowCopy );
3755-
3756-
3757- //update meta data
3758- result .nonZeros = nnz ;
3759- return result ;
3760- }
3761-
3762- private MatrixBlock appendDense (MatrixBlock [] that , MatrixBlock result , boolean cbind , long nnz , boolean shallowCopy ,
3763- int m , int n ) {
3764- if (cbind ) {
3765- DenseBlock resd = result .allocateBlock ().getDenseBlock ();
3766- MatrixBlock [] in = ArrayUtils .addAll (new MatrixBlock [] {this }, that );
3767-
3768- for (int i = 0 ; i < m ; i ++) {
3769- for (int k = 0 , off = 0 ; k < in .length ; off += in [k ].clen , k ++) {
3770- if (in [k ].isEmptyBlock (false ))
3771- continue ;
3772- if (in [k ].sparse ) {
3773- SparseBlock src = in [k ].sparseBlock ;
3774- if (src .isEmpty (i ))
3751+ //core append operation
3752+ //copy left and right input into output
3753+ if ( !result .sparse && nnz !=0 ) //DENSE
3754+ {
3755+ if ( cbind ) {
3756+ DenseBlock resd = result .allocateBlock ().getDenseBlock ();
3757+ MatrixBlock [] in = ArrayUtils .addAll (new MatrixBlock []{this }, that );
3758+
3759+ for ( int i =0 ; i <m ; i ++ ) {
3760+ for ( int k =0 , off =0 ; k <in .length ; off +=in [k ].clen , k ++ ) {
3761+ if ( in [k ].isEmptyBlock (false ) )
37753762 continue ;
3776- int srcpos = src .pos (i );
3777- int srclen = src .size (i );
3778- int [] srcix = src .indexes (i );
3779- double [] srcval = src .values (i );
3780- double [] resval = resd .values (i );
3781- int resix = resd .pos (i , off );
3782- for (int j = srcpos ; j < srcpos + srclen ; j ++)
3783- resval [resix + srcix [j ]] = srcval [j ];
3784- }
3785- else {
3786- DenseBlock src = in [k ].getDenseBlock ();
3787- double [] srcval = src .values (i );
3788- double [] resval = resd .values (i );
3789- System .arraycopy (srcval , src .pos (i ), resval , resd .pos (i , off ), in [k ].clen );
3763+ if ( in [k ].sparse ) {
3764+ SparseBlock src = in [k ].sparseBlock ;
3765+ if ( src .isEmpty (i ) )
3766+ continue ;
3767+ int srcpos = src .pos (i );
3768+ int srclen = src .size (i );
3769+ int [] srcix = src .indexes (i );
3770+ double [] srcval = src .values (i );
3771+ double [] resval = resd .values (i );
3772+ int resix = resd .pos (i , off );
3773+ for (int j =srcpos ; j <srcpos +srclen ; j ++)
3774+ resval [resix +srcix [j ]] = srcval [j ];
3775+ }
3776+ else {
3777+ DenseBlock src = in [k ].getDenseBlock ();
3778+ double [] srcval = src .values (i );
3779+ double [] resval = resd .values (i );
3780+ System .arraycopy (srcval , src .pos (i ),
3781+ resval , resd .pos (i , off ), in [k ].clen );
3782+ }
37903783 }
37913784 }
37923785 }
3793- }
3794- else { // rbind
3795- result . copy ( 0 , rlen - 1 , 0 , n - 1 , this , false );
3796- for ( int i = 0 , off = rlen ; i < that . length ; i ++) {
3797- result . copy ( off , off + that [i ].rlen - 1 , 0 , n - 1 , that [ i ], false ) ;
3798- off += that [ i ]. rlen ;
3786+ else { //rbind
3787+ result . copy ( 0 , rlen - 1 , 0 , n - 1 , this , false );
3788+ for ( int i = 0 , off = rlen ; i < that . length ; i ++) {
3789+ result . copy ( off , off + that [ i ]. rlen - 1 , 0 , n - 1 , that [ i ], false );
3790+ off += that [i ].rlen ;
3791+ }
37993792 }
38003793 }
3801-
3802- result .nonZeros = nnz ;
3803- return result ;
3804- }
3805-
3806- private MatrixBlock appendSparse (MatrixBlock [] that , MatrixBlock result , boolean cbind , long nnz ,
3807- boolean shallowCopy ) {
3808- // adjust sparse rows if required
3809- result .allocateSparseRowsBlock ();
3810- // allocate sparse rows once for cbind
3811- if (cbind && nnz > rlen && !shallowCopy && result .getSparseBlock () instanceof SparseBlockMCSR ) {
3812- final SparseBlock sblock = result .getSparseBlock ();
3813- // for each row calculate how many non zeros are pressent.
3814- for (int i = 0 ; i < result .rlen ; i ++)
3815- sblock .allocate (i , computeNNzRow (that , i ));
3816-
3817- }
3818-
3819- // core append operation
3820- // we can always append this directly to offset 0.0 in both cbind and rbind.
3821- result .appendToSparse (this , 0 , 0 , !shallowCopy );
3822- if (cbind ) {
3823- for (int i = 0 , off = clen ; i < that .length ; i ++) {
3824- result .appendToSparse (that [i ], 0 , off );
3825- off += that [i ].clen ;
3794+ //SPARSE
3795+ else if (nnz != 0 ) {
3796+ //adjust sparse rows if required
3797+ result .allocateSparseRowsBlock ();
3798+ //allocate sparse rows once for cbind
3799+ if ( cbind && nnz > rlen && !shallowCopy && result .getSparseBlock () instanceof SparseBlockMCSR ) {
3800+ final SparseBlock sblock = result .getSparseBlock ();
3801+ // for each row calculate how many non zeros are pressent.
3802+ for ( int i =0 ; i <result .rlen ; i ++ )
3803+ sblock .allocate (i , computeNNzRow (that , i ));
3804+
38263805 }
3827- }
3828- else { // rbind
3829- for (int i = 0 , off = rlen ; i < that .length ; i ++) {
3830- result .appendToSparse (that [i ], off , 0 );
3831- off += that [i ].rlen ;
3806+
3807+ //core append operation
3808+ // we can always append this directly to offset 0.0 in both cbind and rbind.
3809+ result .appendToSparse (this , 0 , 0 , !shallowCopy );
3810+ if ( cbind ) {
3811+ for (int i =0 , off =clen ; i <that .length ; i ++) {
3812+ result .appendToSparse (that [i ], 0 , off );
3813+ off += that [i ].clen ;
3814+ }
3815+ }
3816+ else { //rbind
3817+ for (int i =0 , off =rlen ; i <that .length ; i ++) {
3818+ result .appendToSparse (that [i ], off , 0 );
3819+ off += that [i ].rlen ;
3820+ }
38323821 }
38333822 }
3834-
3823+
3824+ //update meta data
38353825 result .nonZeros = nnz ;
38363826 return result ;
38373827 }
0 commit comments