Example usage for java.lang Float floatToIntBits

List of usage examples for java.lang Float floatToIntBits

Introduction

In this page you can find the example usage for java.lang Float floatToIntBits.

Prototype

@HotSpotIntrinsicCandidate
public static int floatToIntBits(float value) 

Source Link

Document

Returns a representation of the specified floating-point value according to the IEEE 754 floating-point "single format" bit layout.

Usage

From source file:com.datatorrent.lib.appdata.gpo.GPOUtils.java

public static byte[] serializeFloat(float valf) {
    byte[] buffer = new byte[Type.FLOAT.getByteSize()];
    int val = Float.floatToIntBits(valf);

    buffer[0] = (byte) ((val >> 24) & 0xFF);
    buffer[1] = (byte) ((val >> 16) & 0xFF);
    buffer[2] = (byte) ((val >> 8) & 0xFF);
    buffer[3] = (byte) (val & 0xFF);

    return buffer;
}

From source file:com.datatorrent.lib.appdata.gpo.GPOUtils.java

public static int hashcode(GPOMutable gpo) {
    int hashCode = 0;

    {/*from  w ww. ja  v  a2  s .co m*/
        String[] stringArray = gpo.getFieldsString();
        if (stringArray != null) {
            for (int index = 0; index < stringArray.length; index++) {
                hashCode ^= stringArray[index].hashCode();
            }
        }
    }

    {
        boolean[] booleanArray = gpo.getFieldsBoolean();
        if (booleanArray != null) {
            for (int index = 0; index < booleanArray.length; index++) {
                hashCode ^= booleanArray[index] ? 1 : 0;
            }
        }
    }

    {
        char[] charArray = gpo.getFieldsCharacter();
        if (charArray != null) {
            for (int index = 0; index < charArray.length; index++) {
                hashCode ^= Character.getNumericValue(charArray[index]);
            }
        }
    }

    {
        byte[] byteArray = gpo.getFieldsByte();
        if (byteArray != null) {
            for (int index = 0; index < byteArray.length; index++) {
                hashCode ^= byteArray[index];
            }
        }
    }

    {
        short[] shortArray = gpo.getFieldsShort();
        if (shortArray != null) {
            for (int index = 0; index < shortArray.length; index++) {
                hashCode ^= shortArray[index];
            }
        }
    }

    {
        int[] integerArray = gpo.getFieldsInteger();
        if (integerArray != null) {
            for (int index = 0; index < integerArray.length; index++) {
                hashCode ^= integerArray[index];
            }
        }
    }

    {
        long[] longArray = gpo.getFieldsLong();
        if (longArray != null) {
            for (int index = 0; index < longArray.length; index++) {
                hashCode ^= longArray[index];
            }
        }
    }

    {
        float[] floatArray = gpo.getFieldsFloat();
        if (floatArray != null) {
            for (int index = 0; index < floatArray.length; index++) {
                hashCode ^= Float.floatToIntBits(floatArray[index]);
            }
        }
    }

    {
        double[] doubleArray = gpo.getFieldsDouble();
        if (doubleArray != null) {
            for (int index = 0; index < doubleArray.length; index++) {
                hashCode ^= Double.doubleToLongBits(doubleArray[index]);
            }
        }
    }

    {
        Object[] objectArray = gpo.getFieldsObject();
        if (objectArray != null) {
            for (int index = 0; index < objectArray.length; index++) {
                hashCode ^= objectArray[index].hashCode();
            }
        }
    }

    return hashCode;
}

From source file:com.datatorrent.lib.appdata.gpo.GPOUtils.java

/**
 * This function computes the hashcode of a {@link GPOMutable} based on a specified subset of its data.
 * <br/>//from  w w w . j a  va 2 s.  c o m
 * <br/>
 * <b>Note:</b> In some cases a {@link GPOMutable} object contains a field which is bucketed. In the case of
 * bucketed fields, you may want to preserve the original value of the field, but only use the bucketed value
 * of the field for computing a hashcode. In order to do this you can store the original value of {@link GPOMutable}'s
 * field before calling this function, and replace it with the bucketed value. Then after the hashcode is computed, the
 * original value of the field can be restored.
 *
 * @param gpo The {@link GPOMutable} to compute a hashcode for.
 * @param indexSubset The subset of the {@link GPOMutable} used to compute the hashcode.
 * @return The hashcode for the given {@link GPOMutable} computed from the specified subset of its data.
 */
public static int indirectHashcode(GPOMutable gpo, IndexSubset indexSubset) {
    int hashCode = 7;
    final int hashMultiplier = 23;

    {
        String[] stringArray = gpo.getFieldsString();
        int[] srcIndex = indexSubset.fieldsStringIndexSubset;
        if (srcIndex != null) {
            for (int index = 0; index < srcIndex.length; index++) {
                if (srcIndex[index] == -1) {
                    continue;
                }
                hashCode = hashMultiplier * hashCode + stringArray[srcIndex[index]].hashCode();
            }
        }
    }

    {
        boolean[] booleanArray = gpo.getFieldsBoolean();
        int[] srcIndex = indexSubset.fieldsBooleanIndexSubset;
        if (srcIndex != null) {
            for (int index = 0; index < srcIndex.length; index++) {
                if (srcIndex[index] == -1) {
                    continue;
                }
                hashCode = hashMultiplier * hashCode + (booleanArray[srcIndex[index]] ? 1 : 0);
            }
        }
    }

    {
        char[] charArray = gpo.getFieldsCharacter();
        int[] srcIndex = indexSubset.fieldsCharacterIndexSubset;
        if (srcIndex != null) {
            for (int index = 0; index < srcIndex.length; index++) {
                if (srcIndex[index] == -1) {
                    continue;
                }
                hashCode = hashMultiplier * hashCode + Character.getNumericValue(charArray[srcIndex[index]]);
            }
        }
    }

    {
        byte[] byteArray = gpo.getFieldsByte();
        int[] srcIndex = indexSubset.fieldsByteIndexSubset;
        if (srcIndex != null) {
            for (int index = 0; index < srcIndex.length; index++) {
                if (srcIndex[index] == -1) {
                    continue;
                }
                hashCode = hashMultiplier * hashCode + byteArray[srcIndex[index]];
            }
        }
    }

    {
        short[] shortArray = gpo.getFieldsShort();
        int[] srcIndex = indexSubset.fieldsShortIndexSubset;
        if (srcIndex != null) {
            for (int index = 0; index < srcIndex.length; index++) {
                if (srcIndex[index] == -1) {
                    continue;
                }
                hashCode = hashMultiplier * hashCode + shortArray[srcIndex[index]];
            }
        }
    }

    {
        int[] integerArray = gpo.getFieldsInteger();
        int[] srcIndex = indexSubset.fieldsIntegerIndexSubset;
        if (srcIndex != null) {
            for (int index = 0; index < srcIndex.length; index++) {
                if (srcIndex[index] == -1) {
                    continue;
                }
                hashCode = hashMultiplier * hashCode + integerArray[srcIndex[index]];
            }
        }
    }

    {
        long[] longArray = gpo.getFieldsLong();
        int[] srcIndex = indexSubset.fieldsLongIndexSubset;
        if (srcIndex != null) {
            for (int index = 0; index < srcIndex.length; index++) {
                if (srcIndex[index] == -1) {
                    continue;
                }
                long element = longArray[srcIndex[index]];
                int elementHash = (int) (element ^ (element >>> 32));
                hashCode = hashMultiplier * hashCode + elementHash;
            }
        }
    }

    {
        float[] floatArray = gpo.getFieldsFloat();
        int[] srcIndex = indexSubset.fieldsFloatIndexSubset;
        if (srcIndex != null) {
            for (int index = 0; index < srcIndex.length; index++) {
                if (srcIndex[index] == -1) {
                    continue;
                }
                hashCode = hashMultiplier * hashCode + Float.floatToIntBits(floatArray[srcIndex[index]]);
            }
        }
    }

    {
        double[] doubleArray = gpo.getFieldsDouble();
        int[] srcIndex = indexSubset.fieldsDoubleIndexSubset;
        if (srcIndex != null) {
            for (int index = 0; index < srcIndex.length; index++) {
                if (srcIndex[index] == -1) {
                    continue;
                }
                long element = Double.doubleToLongBits(doubleArray[srcIndex[index]]);
                int elementHash = (int) (element ^ (element >>> 32));
                hashCode = hashMultiplier * hashCode + elementHash;
            }
        }
    }

    {
        Object[] objectArray = gpo.getFieldsObject();
        int[] srcIndex = indexSubset.fieldsObjectIndexSubset;
        if (srcIndex != null) {
            for (int index = 0; index < srcIndex.length; index++) {
                if (srcIndex[index] == -1) {
                    continue;
                }

                hashCode = hashMultiplier * hashCode + objectArray[srcIndex[index]].hashCode();
            }
        }
    }

    return hashCode;
}

From source file:edu.cornell.med.icb.goby.alignments.AlignmentCollectionHandler.java

private Alignments.AlignmentEntry transform(final int index, final int indexInReducedCollection,
        final Alignments.AlignmentEntry source) {
    final Alignments.AlignmentEntry.Builder result = Alignments.AlignmentEntry.newBuilder(source);
    final int position = source.getPosition();
    final int targetIndex = source.getTargetIndex();

    // clear the strings we collected earlier:
    result.clearSoftClippedBasesLeft();/*from   w  w  w.j  a va  2s  .c  o m*/
    result.clearSoftClippedBasesRight();
    result.clearSoftClippedQualityLeft();
    result.clearSoftClippedQualityRight();

    if (index > 0 && targetIndex == previousTargetIndex) {
        result.clearPosition();
        result.clearTargetIndex();

        deltaPositions.add(position - previousPosition);
        deltaTargetIndices.add(targetIndex - previousTargetIndex);
        //       System.out.printf("entry delta position: %d delta-target=%d %n",position - previousPosition, targetIndex - previousTargetIndex);
    }

    final int queryIndex = source.getQueryIndex();

    queryIndices.add(queryIndex);
    previousPosition = position;
    previousTargetIndex = targetIndex;

    if (debug(1) && source.hasQueryLength()) {
        writtenBases += source.hasQueryLength() ? source.getQueryLength() : source.getQueryAlignedLength();
    }
    result.clearQueryIndex();

    recordVariationQualitiesAndClear(source, result, result.getSequenceVariationsList());

    final boolean entryMatchingReverseStrand = source.getMatchingReverseStrand();
    Alignments.RelatedAlignmentEntry link = pairLinks.code(source.hasPairAlignmentLink(), source,
            source.getPairAlignmentLink());
    if (link == null) {
        result.clearPairAlignmentLink();
    } else {
        result.setPairAlignmentLink(link);
    }

    link = forwardSpliceLinks.code(source.hasSplicedForwardAlignmentLink(), source,
            source.getSplicedForwardAlignmentLink());
    if (link == null) {
        result.clearSplicedForwardAlignmentLink();
    } else {
        result.setSplicedForwardAlignmentLink(link);
    }

    link = backwardSpliceLinks.code(source.hasSplicedBackwardAlignmentLink(), source,
            source.getSplicedBackwardAlignmentLink());
    if (link == null) {
        result.clearSplicedBackwardAlignmentLink();
    } else {
        result.setSplicedBackwardAlignmentLink(link);
    }
    if (source.hasReadQualityScores()) {

        final ByteString quals = source.getReadQualityScores();
        final int size = quals.size();
        numReadQualityScores.add(size);
        for (int i = 0; i < size; i++) {
            allReadQualityScores.add(quals.byteAt(i));
            qualScoreIndex++;
        }
        result.clearReadQualityScores();
    } else {
        numReadQualityScores.add(0);
    }

    if (source.hasInsertSize()) {
        final int readPos = source.getPosition();

        final int matePos = source.getPairAlignmentLink().getPosition();
        final int length = source.getTargetAlignedLength();
        final int pos1 = source.getMatchingReverseStrand() ? length + readPos : readPos + 1;
        final int pos2 = EntryFlagHelper.isMateReverseStrand(source) ? length + matePos : matePos + 1;
        final int insertSize = source.getInsertSize();
        final int insertSizeDiff = Fast.int2nat(pos2 - pos1 - insertSize);
        // reverse:  insertSize= (pos2-pos1) - isd
        /*    if (insertSize != 0) {
        System.out.printf("insertSize %d length= %d %c %c readPos %d matePos %d  pos1 %d pos2 %d  pos2-pos1 %d matePos-readPos  %d  insertSizeDiff %d %n",
         source.getInsertSize(), length,
         source.getMatchingReverseStrand() ? '+' : '-',
         EntryFlagHelper.isMateReverseStrand(source) ? '+' : '-',
         readPos, matePos, pos1, pos2, pos2 - pos1, matePos - readPos, insertSizeDiff);
                
        }      */

        insertSizes.add(insertSizeDiff);

    } else {
        insertSizes.add(MISSING_VALUE);
    }
    result.clearInsertSize();
    // Fields above this line were removed before comparing source to the previous template.
    final Alignments.AlignmentEntry partial = result.clone().build();

    if (previousPartial != null && indexInReducedCollection >= 1 && fastEqualsEntry(previousPartial, partial)) {
        //   System.out.println("same");
        //  print(partial);
        final int m = multiplicities.get(indexInReducedCollection - 1);
        multiplicities.set(indexInReducedCollection - 1, m + 1);
        // do not add this one, we just increased the multiplicity of the previous one.
        countAggregatedWithMultiplicity++;
        //      System.out.printf("Returning for template match to previous, current queryIndex=%d%n",queryIndex);
        return null;
    } else {
        previousPartial = partial;
        multiplicityFieldsAllMissing &= !source.hasMultiplicity();
        multiplicities.add(Math.max(1, source.getMultiplicity()));
    }
    //System.out.printf("encoding query-index=%d varPositionIndex=%d %n",queryIndex, varPositionIndex);

    queryLengths.add(source.hasQueryLength() ? source.getQueryLength() : MISSING_VALUE);
    mappingQualities.add(source.hasMappingQuality() ? source.getMappingQuality() : MISSING_VALUE);
    matchingReverseStrand
            .add(source.hasMatchingReverseStrand() ? source.getMatchingReverseStrand() ? 1 : 0 : MISSING_VALUE);
    numberOfIndels.add(source.hasNumberOfIndels() ? source.getNumberOfIndels() : MISSING_VALUE);
    numberOfMismatches.add(source.hasNumberOfMismatches() ? source.getNumberOfMismatches() : MISSING_VALUE);
    queryAlignedLengths.add(source.hasQueryAlignedLength()
            ? modelQueryAlignedLength(source.getQueryAlignedLength(), source.getTargetAlignedLength())
            : MISSING_VALUE);
    targetAlignedLengths.add(source.hasTargetAlignedLength() ? source.getTargetAlignedLength() : MISSING_VALUE);
    fragmentIndices.add(source.hasFragmentIndex() ? source.getFragmentIndex() : MISSING_VALUE);
    variationCount.add(source.getSequenceVariationsCount());
    queryPositions.add(source.hasQueryPosition() ? source.getQueryPosition() : MISSING_VALUE);
    sampleIndices.add(source.hasSampleIndex() ? source.getSampleIndex() : MISSING_VALUE);
    readOriginIndices
            .add(source.hasReadOriginIndex() && storeReadOrigins ? source.getReadOriginIndex() : MISSING_VALUE);
    pairFlags.add(source.hasPairFlags() ? reduceSamFlags(source) : MISSING_VALUE);
    scores.add(source.hasScore() ? Float.floatToIntBits(source.getScore()) : MISSING_VALUE);

    result.clearQueryLength();
    result.clearMappingQuality();
    result.clearMatchingReverseStrand();
    result.clearMultiplicity();
    result.clearNumberOfIndels();
    result.clearNumberOfMismatches();
    result.clearQueryAlignedLength();
    result.clearTargetAlignedLength();

    result.clearQueryPosition();
    result.clearFragmentIndex();
    result.clearReadQualityScores();
    result.clearSampleIndex();
    result.clearReadOriginIndex();
    result.clearPairFlags();
    result.clearScore();
    boolean canFullyRemoveThisOne = true;
    boolean canFullyRemoveCollection = true;
    int seqVarIndex = 0;

    for (final Alignments.SequenceVariation seqVar : result.getSequenceVariationsList()) {
        assert seqVar.getPosition() >= 0 : String.format(
                "The following entry had a sequence variation with a negative position. This is not allowed since seqVar.positions must be >=0. %s ",
                source.toString());

        encodeVar(source.getMatchingReverseStrand(), source.getQueryLength(), seqVar);
        final Alignments.SequenceVariation.Builder varBuilder = Alignments.SequenceVariation.newBuilder(seqVar);
        varBuilder.clearPosition();
        varBuilder.clearFrom();
        varBuilder.clearTo();
        varBuilder.clearToQuality();
        varBuilder.clearReadIndex();
        if (!isEmpty(varBuilder.build())) {
            canFullyRemoveThisOne = false;
            canFullyRemoveCollection = false;
        }
        if (canFullyRemoveThisOne) {
            result.removeSequenceVariations(seqVarIndex);
            seqVarIndex--;
        }
        seqVarIndex++;
    }
    if (canFullyRemoveCollection) {
        result.clearSequenceVariations();
    }

    final Alignments.AlignmentEntry alignmentEntry = result.build();
    //       System.out.println(alignmentEntry);
    return alignmentEntry;
}

From source file:org.grails.datastore.mapping.engine.NativeEntryEntityPersister.java

protected boolean areEqual(Object oldValue, Object currentValue, String propName) {
    if (oldValue == currentValue) {
        return true;
    }//from ww w . j a  va  2 s . c  o m

    if (oldValue == null || currentValue == null) {
        return false;
    }

    if ("version".equals(propName)) {
        // special case where comparing int and long would fail artifically
        if (oldValue instanceof Number && currentValue instanceof Number) {
            oldValue = ((Number) oldValue).longValue();
            currentValue = ((Number) currentValue).longValue();
        } else {
            oldValue = oldValue.toString();
            currentValue = currentValue.toString();
        }
    }

    Class oldValueClass = oldValue.getClass();
    if (!oldValueClass.isArray()) {
        if (oldValue instanceof Float) {
            return Float.floatToIntBits((Float) oldValue) == Float.floatToIntBits((Float) currentValue);
        }
        if (oldValue instanceof Double) {
            return Double.doubleToLongBits((Double) oldValue) == Double.doubleToLongBits((Double) currentValue);
        }
        return oldValue.equals(currentValue);
    }

    // check arrays

    if (oldValue.getClass() != currentValue.getClass()) {
        // different dimension
        return false;
    }

    if (oldValue instanceof long[]) {
        return Arrays.equals((long[]) oldValue, (long[]) currentValue);
    }

    if (oldValue instanceof int[]) {
        return Arrays.equals((int[]) oldValue, (int[]) currentValue);
    }

    if (oldValue instanceof short[]) {
        return Arrays.equals((short[]) oldValue, (short[]) currentValue);
    }

    if (oldValue instanceof char[]) {
        return Arrays.equals((char[]) oldValue, (char[]) currentValue);
    }

    if (oldValue instanceof byte[]) {
        return Arrays.equals((byte[]) oldValue, (byte[]) currentValue);
    }

    if (oldValue instanceof double[]) {
        return Arrays.equals((double[]) oldValue, (double[]) currentValue);
    }

    if (oldValue instanceof float[]) {
        return Arrays.equals((float[]) oldValue, (float[]) currentValue);
    }

    if (oldValue instanceof boolean[]) {
        return Arrays.equals((boolean[]) oldValue, (boolean[]) currentValue);
    }

    return Arrays.equals((Object[]) oldValue, (Object[]) currentValue);
}

From source file:foodsimulationmodel.pathmapping.Route.java

/**
 * Returns: <code>Float.floatToIntBits((float)(this.origin.getX()+this.origin.getY()))</code>
 */// w  w w  . j  a  va2 s  . c  o m
@Override
public int hashCode() {
    return Float.floatToIntBits((float) (this.origin.x + this.origin.y));
}

From source file:gedi.util.ArrayUtils.java

private static <T> void sort2(float a[], T[] a2, int fromIndex, int toIndex) {
    final int NEG_ZERO_BITS = Float.floatToIntBits(-0.0f);
    /*// w w w .ja v  a  2  s . c o  m
     * The sort is done in three phases to avoid the expense of using
     * NaN and -0.0 aware comparisons during the main sort.
     */

    /*
     * Preprocessing phase:  Move any NaN's to end of array, count the
     * number of -0.0's, and turn them into 0.0's.
     */
    int numNegZeros = 0;
    int i = fromIndex, n = toIndex;
    while (i < n) {
        if (a[i] != a[i]) {
            float swap = a[i];
            a[i] = a[--n];
            a[n] = swap;
            T swap2 = a2[i];
            a2[i] = a2[n];
            a2[n] = swap2;
        } else {
            if (a[i] == 0 && Float.floatToIntBits(a[i]) == NEG_ZERO_BITS) {
                a[i] = 0.0f;
                numNegZeros++;
            }
            i++;
        }
    }

    // Main sort phase: quicksort everything but the NaN's
    sort1(a, a2, fromIndex, n - fromIndex);

    // Postprocessing phase: change 0.0's to -0.0's as required
    if (numNegZeros != 0) {
        int j = binarySearch0(a, fromIndex, n, 0.0f); // posn of ANY zero
        do {
            j--;
        } while (j >= 0 && a[j] == 0.0f);

        // j is now one less than the index of the FIRST zero
        for (int k = 0; k < numNegZeros; k++)
            a[++j] = -0.0f;
    }
}

From source file:gedi.util.ArrayUtils.java

private static int binarySearch0(float[] a, int fromIndex, int toIndex, float key) {
    int low = fromIndex;
    int high = toIndex - 1;

    while (low <= high) {
        int mid = (low + high) >>> 1;
        float midVal = a[mid];

        int cmp;/* ww  w.j  a va  2  s.  co m*/
        if (midVal < key) {
            cmp = -1; // Neither val is NaN, thisVal is smaller
        } else if (midVal > key) {
            cmp = 1; // Neither val is NaN, thisVal is larger
        } else {
            int midBits = Float.floatToIntBits(midVal);
            int keyBits = Float.floatToIntBits(key);
            cmp = (midBits == keyBits ? 0 : // Values are equal
                    (midBits < keyBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
                            1)); // (0.0, -0.0) or (NaN, !NaN)
        }

        if (cmp < 0)
            low = mid + 1;
        else if (cmp > 0)
            high = mid - 1;
        else
            return mid; // key found
    }
    return -(low + 1); // key not found.
}