Example usage for org.apache.mahout.collections Arithmetic log2

List of usage examples for org.apache.mahout.collections Arithmetic log2

Introduction

In this page you can find the example usage for org.apache.mahout.collections Arithmetic log2.

Prototype

public static double log2(double value) 

Source Link

Document

Returns log2value.

Usage

From source file:gov.vha.isaac.ochre.collections.uuidnidmap.UuidArrayList.java

License:Apache License

/**
 * Removes from the receiver all elements that are contained in the
 * specified list. Tests for identity./*from  w  w w  .  j a  va 2  s .c om*/
 * 
 * @param other
 *            the other list.
 * @return {@code true} if the receiver changed as a result of the
 *         call.
 */
@Override
public boolean removeAll(AbstractUuidList other) {
    // overridden for performance only.
    if (!(other instanceof UuidArrayList))
        return super.removeAll(other);

    /*
     * There are two possibilities to do the thing a) use other.indexOf(...)
     * b) sort other, then use other.binarySearch(...)
     * 
     * Let's try to figure out which one is faster. Let M=size,
     * N=other.size, then a) takes O(M*N) steps b) takes O(N*logN + M*logN)
     * steps (sorting is O(N*logN) and binarySearch is O(logN))
     * 
     * Hence, if N*logN + M*logN < M*N, we use b) otherwise we use a).
     */
    if (other.size() == 0) {
        return false;
    } // nothing to do
    int limit = other.size() - 1;
    int j = 0;
    long[] theElements = elements;
    int mySize = size();

    double N = (double) other.size();
    double M = (double) mySize;
    if ((N + M) * Arithmetic.log2(N) < M * N) {
        // it is faster to sort other before searching in it
        UuidArrayList sortedList = (UuidArrayList) other.clone();
        sortedList.quickSort();

        for (int i = 0; i < mySize; i++) {
            int msb = i * 2;
            int lsb = msb + 1;
            long[] key = new long[2];
            key[0] = theElements[msb];
            key[1] = theElements[lsb];
            if (sortedList.binarySearchFromTo(key, 0, limit) < 0) {
                theElements[2 * j] = theElements[2 * i];
                theElements[2 * j + 1] = theElements[2 * i + 1];
                j++;
            }
        }
    } else {
        // it is faster to search in other without sorting
        for (int i = 0; i < mySize; i++) {
            int msb = i * 2;
            int lsb = msb + 1;
            long[] key = new long[2];
            key[0] = theElements[msb];
            key[1] = theElements[lsb];
            if (other.indexOfFromTo(key, 0, limit) < 0) {
                theElements[2 * j] = theElements[2 * i];
                theElements[2 * j + 1] = theElements[2 * i + 1];
                j++;
            }
        }
    }

    boolean modified = (j != mySize);
    setSize(j);
    return modified;
}

From source file:gov.vha.isaac.ochre.collections.uuidnidmap.UuidArrayList.java

License:Apache License

/**
 * Retains (keeps) only the elements in the receiver that are contained in
 * the specified other list. In other words, removes from the receiver all
 * of its elements that are not contained in the specified other list.
 * /* ww w  . jav a 2s . c  o m*/
 * @param other
 *            the other list to test against.
 * @return {@code true} if the receiver changed as a result of the
 *         call.
 */
@Override
public boolean retainAll(AbstractUuidList other) {
    // overridden for performance only.
    if (!(other instanceof UuidArrayList))
        return super.retainAll(other);

    /*
     * There are two possibilities to do the thing a) use other.indexOf(...)
     * b) sort other, then use other.binarySearch(...)
     * 
     * Let's try to figure out which one is faster. Let M=size,
     * N=other.size, then a) takes O(M*N) steps b) takes O(N*logN + M*logN)
     * steps (sorting is O(N*logN) and binarySearch is O(logN))
     * 
     * Hence, if N*logN + M*logN < M*N, we use b) otherwise we use a).
     */
    int limit = other.size() - 1;
    int j = 0;
    long[] theElements = elements;
    int mySize = size();

    double N = (double) other.size();
    double M = (double) mySize;
    if ((N + M) * Arithmetic.log2(N) < M * N) {
        // it is faster to sort other before searching in it
        UuidArrayList sortedList = (UuidArrayList) other.clone();
        sortedList.quickSort();

        for (int i = 0; i < mySize; i++) {
            int msb = i * 2;
            int lsb = msb + 1;
            long[] key = new long[2];
            key[0] = theElements[msb];
            key[1] = theElements[lsb];
            if (sortedList.binarySearchFromTo(key, 0, limit) >= 0) {
                theElements[2 * j] = theElements[2 * i];
                theElements[2 * j + 1] = theElements[2 * i + 1];
                j++;
            }
        }
    } else {
        // it is faster to search in other without sorting
        for (int i = 0; i < mySize; i++) {
            int msb = i * 2;
            int lsb = msb + 1;
            long[] key = new long[2];
            key[0] = theElements[msb];
            key[1] = theElements[lsb];
            if (other.indexOfFromTo(key, 0, limit) >= 0) {
                theElements[2 * j] = theElements[2 * i];
                theElements[2 * j + 1] = theElements[2 * i + 1];
                j++;
            }
        }
    }

    boolean modified = (j != mySize);
    setSize(j);
    return modified;
}

From source file:sh.isaac.api.collections.uuidnidmap.UuidArrayList.java

License:Apache License

/**
 * Removes from the receiver all elements that are contained in the
 * specified list. Tests for identity.//  w  ww  .jav  a  2  s  .c o m
 *
 * @param other
 *            the other list.
 * @return {@code true} if the receiver changed as a result of the
 *         call.
 */
@Override
public boolean removeAll(AbstractUuidList other) {
    // overridden for performance only.
    if (!(other instanceof UuidArrayList)) {
        return super.removeAll(other);
    }

    /*
     * There are two possibilities to do the thing a) use other.indexOf(...)
     * b) sort other, then use other.binarySearch(...)
     *
     * Let's try to figure out which one is faster. Let M=size,
     * N=other.size, then a) takes O(M*N) steps b) takes O(N*logN + M*logN)
     * steps (sorting is O(N*logN) and binarySearch is O(logN))
     *
     * Hence, if N*logN + M*logN < M*N, we use b) otherwise we use a).
     */
    if (other.size() == 0) {
        return false;
    } // nothing to do

    final int limit = other.size() - 1;
    int j = 0;
    final long[] theElements = this.elements;
    final int mySize = size();
    final double N = other.size();
    final double M = mySize;

    if ((N + M) * Arithmetic.log2(N) < M * N) {
        // it is faster to sort other before searching in it
        final UuidArrayList sortedList = (UuidArrayList) other.clone();

        sortedList.quickSort();

        for (int i = 0; i < mySize; i++) {
            final int msb = i * 2;
            final int lsb = msb + 1;
            final long[] key = new long[2];

            key[0] = theElements[msb];
            key[1] = theElements[lsb];

            if (sortedList.binarySearchFromTo(key, 0, limit) < 0) {
                theElements[2 * j] = theElements[2 * i];
                theElements[2 * j + 1] = theElements[2 * i + 1];
                j++;
            }
        }
    } else {
        // it is faster to search in other without sorting
        for (int i = 0; i < mySize; i++) {
            final int msb = i * 2;
            final int lsb = msb + 1;
            final long[] key = new long[2];

            key[0] = theElements[msb];
            key[1] = theElements[lsb];

            if (other.indexOfFromTo(key, 0, limit) < 0) {
                theElements[2 * j] = theElements[2 * i];
                theElements[2 * j + 1] = theElements[2 * i + 1];
                j++;
            }
        }
    }

    final boolean modified = (j != mySize);

    setSize(j);
    return modified;
}

From source file:sh.isaac.api.collections.uuidnidmap.UuidArrayList.java

License:Apache License

/**
 * Retains (keeps) only the elements in the receiver that are contained in
 * the specified other list. In other words, removes from the receiver all
 * of its elements that are not contained in the specified other list.
 *
 * @param other/*from w w w  .  j ava2  s .  c o  m*/
 *            the other list to test against.
 * @return {@code true} if the receiver changed as a result of the
 *         call.
 */
@Override
public boolean retainAll(AbstractUuidList other) {
    // overridden for performance only.
    if (!(other instanceof UuidArrayList)) {
        return super.retainAll(other);
    }

    /*
     * There are two possibilities to do the thing a) use other.indexOf(...)
     * b) sort other, then use other.binarySearch(...)
     *
     * Let's try to figure out which one is faster. Let M=size,
     * N=other.size, then a) takes O(M*N) steps b) takes O(N*logN + M*logN)
     * steps (sorting is O(N*logN) and binarySearch is O(logN))
     *
     * Hence, if N*logN + M*logN < M*N, we use b) otherwise we use a).
     */
    final int limit = other.size() - 1;
    int j = 0;
    final long[] theElements = this.elements;
    final int mySize = size();
    final double N = other.size();
    final double M = mySize;

    if ((N + M) * Arithmetic.log2(N) < M * N) {
        // it is faster to sort other before searching in it
        final UuidArrayList sortedList = (UuidArrayList) other.clone();

        sortedList.quickSort();

        for (int i = 0; i < mySize; i++) {
            final int msb = i * 2;
            final int lsb = msb + 1;
            final long[] key = new long[2];

            key[0] = theElements[msb];
            key[1] = theElements[lsb];

            if (sortedList.binarySearchFromTo(key, 0, limit) >= 0) {
                theElements[2 * j] = theElements[2 * i];
                theElements[2 * j + 1] = theElements[2 * i + 1];
                j++;
            }
        }
    } else {
        // it is faster to search in other without sorting
        for (int i = 0; i < mySize; i++) {
            final int msb = i * 2;
            final int lsb = msb + 1;
            final long[] key = new long[2];

            key[0] = theElements[msb];
            key[1] = theElements[lsb];

            if (other.indexOfFromTo(key, 0, limit) >= 0) {
                theElements[2 * j] = theElements[2 * i];
                theElements[2 * j + 1] = theElements[2 * i + 1];
                j++;
            }
        }
    }

    final boolean modified = (j != mySize);

    setSize(j);
    return modified;
}