public void addOrUpdate ( long [ ] indexes , double value ) { long [ ] physicalIndexes = isView ( ) ? translateToPhysical ( indexes ) : indexes ; for ( int i = 0 ; i < length ; i ++ ) { long [ ] idx = getUnderlyingIndicesOf ( i ) . asLong ( ) ; if ( Arrays . equals ( idx , physicalIndexes ) ) { // There is already a non-null value at this index // -> update the current value, the sort is maintained if ( value == 0 ) { removeEntry ( i ) ; length -- ; } else { values . put ( i , value ) ; length ++ ; } return ; } } // If the value is 0 and there is no existing non-null value at the given index if ( value == 0 ) { return ; } /* It's a new non-null element. We add the value and the indexes at the end of their respective databuffers. * The buffers are no longer sorted ! * /!\ We need to reallocate the buffers if they are full */ while ( ! canInsert ( values , 1 ) ) { long size = ( long ) Math . ceil ( ( values . capacity ( ) * THRESHOLD_MEMORY_ALLOCATION ) ) ; values . reallocate ( size ) ; } values . put ( length , value ) ; while ( ! canInsert ( indices , physicalIndexes . length ) ) { long size = ( long ) Math . ceil ( ( indices . capacity ( ) * THRESHOLD_MEMORY_ALLOCATION ) ) ; indices . reallocate ( size ) ; } for ( int i = 0 ; i < physicalIndexes . length ; i ++ ) { indices . put ( length * rank ( ) + i , physicalIndexes [ i ] ) ; } length ++ ; isSorted = false ; }
Add a new element in the ndarray or update the value if there is already a non - null element at this position