private HamtPMap < K , V > pivot ( K key , int hash , HamtPMap < K , V > parent , V [ ] result ) { int newMask = mask ; HamtPMap < K , V > [ ] newChildren = this . children ; if ( hash == this . hash && key . equals ( this . key ) ) { // Found the key: swap out this key/value with the parent and return the result in the holder. result [ 0 ] = this . value ; } else { // Otherwise, see if the value might be present in a child. int searchBucket = bucket ( hash ) ; int replacementBucket = bucket ( this . hash ) ; if ( searchBucket == replacementBucket ) { int bucketMask = 1 << searchBucket ; if ( ( mask & bucketMask ) != 0 ) { // Found a child, call pivot recursively. int index = index ( bucketMask ) ; HamtPMap < K , V > child = newChildren [ index ] ; HamtPMap < K , V > newChild = child . pivot ( key , shift ( hash ) , this , result ) ; newChildren = replaceChild ( newChildren , index , newChild ) ; } } else { int searchMask = 1 << searchBucket ; if ( ( mask & searchMask ) != 0 ) { int index = index ( searchMask ) ; HamtPMap < K , V > child = newChildren [ index ] ; HamtPMap < K , V > newChild = child . minus ( key , shift ( hash ) , result ) ; if ( ! newChild . isEmpty ( ) ) { newChildren = replaceChild ( newChildren , index , newChild ) ; } else { newChildren = deleteChild ( newChildren , index ) ; newMask &= ~ searchMask ; } } int replacementMask = 1 << replacementBucket ; int index = Integer . bitCount ( newMask & ( replacementMask - 1 ) ) ; if ( ( mask & replacementMask ) != 0 ) { HamtPMap < K , V > child = newChildren [ index ] ; HamtPMap < K , V > newChild = child . plus ( this . key , shift ( this . hash ) , this . value ) ; newChildren = replaceChild ( newChildren , index , newChild ) ; } else { newChildren = insertChild ( newChildren , index , new HamtPMap <> ( this . key , shift ( this . hash ) , this . value , 0 , emptyChildren ( ) ) ) ; newMask |= replacementMask ; } } } return parent != null ? new HamtPMap <> ( parent . key , shift ( parent . hash ) , parent . value , newMask , newChildren ) : new HamtPMap <> ( key , hash , result [ 0 ] , newMask , newChildren ) ; }
Internal recursive version of pivot . If parent is null then the result is used for the value in the returned map . The value if found is stored in the result array as a secondary return .