Example usage for android.util SparseArray get

List of usage examples for android.util SparseArray get

Introduction

In this page you can find the example usage for android.util SparseArray get.

Prototype

public E get(int key) 

Source Link

Document

Gets the Object mapped from the specified key, or null if no such mapping has been made.

Usage

From source file:Main.java

public static void scaleBitmaps(SparseArray<Bitmap> bitmapMap, float scale) {
    for (int i = 0; i < bitmapMap.size(); i++) {
        int key = bitmapMap.keyAt(i);
        Log.d("BitmapUtil", "scaleBitmaps: " + key);
        Bitmap bitmap = bitmapMap.get(key);
        bitmapMap.put(i, scaleBitmap(bitmap, scale));
    }/*from  ww w  .  ja v  a 2s .  c o m*/
}

From source file:android.support.v7.content.res.AppCompatResources.java

@Nullable
private static ColorStateList getCachedColorStateList(@NonNull Context context, @ColorRes int resId) {
    synchronized (sColorStateCacheLock) {
        final SparseArray<ColorStateListCacheEntry> entries = sColorStateCaches.get(context);
        if (entries != null && entries.size() > 0) {
            final ColorStateListCacheEntry entry = entries.get(resId);
            if (entry != null) {
                if (entry.configuration.equals(context.getResources().getConfiguration())) {
                    // If the current configuration matches the entry's, we can use it
                    return entry.value;
                } else {
                    // Otherwise we'll remove the entry
                    entries.remove(resId);
                }/*from  w ww . ja v a2 s  .c  o  m*/
            }
        }
    }
    return null;
}

From source file:Main.java

public static String mergeUri(String uri, Object[] args, SparseArray<String> paramKeys,
        Map<String, String> methodStaticUri, Map<String, String> globalParamKeys) {

    if (paramKeys != null && paramKeys.size() > 0 && args != null && args.length > 0) {
        for (int i = 0, key = paramKeys.keyAt(i); i < paramKeys.size(); key = paramKeys.keyAt(++i)) {
            uri = uri.replaceFirst(":" + paramKeys.get(key), String.valueOf(args[key]));
        }//from   w ww. j  a  va 2  s. c  o m
    }
    if (methodStaticUri != null && methodStaticUri.size() > 0) {
        for (Map.Entry<String, String> e : methodStaticUri.entrySet()) {
            uri = uri.replaceFirst(":" + e.getKey(), e.getValue());
        }
    }
    if (globalParamKeys != null && globalParamKeys.size() > 0) {
        for (Map.Entry<String, String> e : globalParamKeys.entrySet()) {
            uri = uri.replaceFirst(":" + e.getKey(), e.getValue());
        }
    }

    return uri;
}

From source file:Main.java

private static double getTheoreticalStudent(int n) {
    SparseArray<Double> theorStudent = new SparseArray<Double>();
    theorStudent.put(10, 2.2281389);/* w w  w . j  av  a  2s .c  om*/
    theorStudent.put(20, 2.0859634);
    theorStudent.put(30, 2.0422725);
    theorStudent.put(40, 2.0210754);
    theorStudent.put(50, 2.0085591);
    theorStudent.put(60, 2.0002978);
    theorStudent.put(70, 1.9944371);
    if (n >= 70)
        return theorStudent.get(70);
    else if (n >= 60)
        return theorStudent.get(60);
    else if (n >= 50)
        return theorStudent.get(50);
    else if (n >= 40)
        return theorStudent.get(40);
    else if (n >= 30)
        return theorStudent.get(30);
    else if (n >= 20)
        return theorStudent.get(20);
    else if (n >= 10)
        return theorStudent.get(10);
    else
        return theorStudent.get(10);
}

From source file:Main.java

/**
 * Compare two dumps and get a list of all indices where
 * they differ from each other./* www.j a v  a 2  s  .c  o m*/
 * @param dump1 The first dump. The sector number is key and the
 * string array represents the blocks.
 * @param dump2 The second dump. The sector number is key and the
 * string array represents the blocks.
 * @return Indices where the two dumps differ. The key represents
 * the sector number. The first dimension of the value represents the
 * block number and the second is a list of indices where dump2 is
 * different from dump1. If the value is Integer[0][0] then the sector
 * exists only in dump1. If the value is Integer[1][0] then the sector
 * exists only in dump2.
 */
public static SparseArray<Integer[][]> diffIndices(SparseArray<String[]> dump1, SparseArray<String[]> dump2) {
    SparseArray<Integer[][]> ret = new SparseArray<Integer[][]>();
    // Walk through all sectors of dump1.
    for (int i = 0; i < dump1.size(); i++) {
        String[] sector1 = dump1.valueAt(i);
        int sectorNr = dump1.keyAt(i);
        String[] sector2 = dump2.get(sectorNr);

        // Check if dump2 has the current sector of dump1.
        if (sector2 == null) {
            ret.put(sectorNr, new Integer[0][0]);
            continue;
        }

        // Check the blocks.
        Integer[][] diffSector = new Integer[sector1.length][];
        // Walk through all blocks.
        for (int j = 0; j < sector1.length; j++) {
            ArrayList<Integer> diffIndices = new ArrayList<Integer>();
            // Walk through all symbols.
            for (int k = 0; k < sector1[j].length(); k++) {
                if (sector1[j].charAt(k) != sector2[j].charAt(k)) {
                    // Found different symbol at index k.
                    diffIndices.add(k);
                }
            }
            if (diffIndices.size() == 0) {
                // Block was identical.
                diffSector[j] = new Integer[0];
            } else {
                diffSector[j] = diffIndices.toArray(new Integer[diffIndices.size()]);
            }
        }
        ret.put(sectorNr, diffSector);
    }

    // Are there sectors that occur only in dump2?
    for (int i = 0; i < dump2.size(); i++) {
        int sectorNr = dump2.keyAt(i);
        if (dump1.get(sectorNr) == null) {
            // Sector only exists in dump2.
            ret.put(sectorNr, new Integer[1][0]);
        }
    }

    return ret;
}

From source file:edu.umich.eecs.rtcl.lp_doctor.utilities.MathTools.java

public static boolean isDistanceIncreased(SparseArray<Double> mobility, SparseArray<Integer> appHistogram,
        int currentPlace) {

    double[] expected = new double[mobility.size()];
    long[] observed = new long[mobility.size()];
    long[] toBeObserved = new long[mobility.size()];

    //we need the total number of visits
    int totalVisits = 0;
    for (int index = 0; index < mobility.size(); index++) {
        int placeID = mobility.keyAt(index);
        double probability = mobility.get(placeID);
        int numVisits = appHistogram.get(placeID, 0);// no visits if place not in histogram
        expected[index] = probability;/*from  w  ww .  j a  va2s  . c  o m*/
        observed[index] = numVisits;
        toBeObserved[index] = numVisits;
        if (placeID == currentPlace) {
            toBeObserved[index]++; //to be observed?
        }
        totalVisits += numVisits; // num visits is per place Id in mobility pattern

    }

    if (totalVisits == 0) {
        //no location access recorded, information leak is inevitable from location leak
        return false;
    }
    double KLValueOld = 0;
    double KLValueNew = 0;
    //what happens if totalVisits is 0?
    //also, what happens id the obsPr is 0?

    for (int i = 0; i < expected.length; i++) {
        double expPr = expected[i]; //larger than 0 by definition
        double obsPr = totalVisits > 0 ? observed[i] * 1.0 / totalVisits : 0;
        double obsPrNew = toBeObserved[i] * 1.0 / (totalVisits + 1);

        KLValueOld += obsPr <= 1e-6 ? 0 : obsPr * Math.log(obsPr / expPr);
        KLValueNew += obsPrNew <= 1e-6 ? 0 : obsPrNew * Math.log(obsPrNew / expPr);

        Util.Log(Util.SESSION_TAG, "exp:\t" + expPr + "\tobs:\t" + obsPr + "temp:\t"
                + (obsPr <= 1e-6 ? 0 : obsPr * Math.log(obsPr / expPr)));
    }

    //Util.Log(Util.SESSION_TAG,KLValueOld+"\t"+expected+"\t"+observed);
    //Util.Log(Util.SESSION_TAG,KLValueNew+"\t"+expected+"\t"+toBeObserved);
    return KLValueNew > KLValueOld;
}

From source file:edu.umich.eecs.rtcl.lp_doctor.utilities.MathTools.java

private static double getPValue(SparseArray<Double> mobility, SparseArray<Integer> appHistogram,
        int currentPlace) {

    double[] expected = new double[mobility.size()];
    long[] observed = new long[mobility.size()];
    long[] toBeObserved = new long[mobility.size()];

    //nothing there, for bootstrapping
    if (mobility.size() < 2) {
        return 1;
    }/* ww w  .j a va2 s  . c o m*/

    for (int index = 0; index < mobility.size(); index++) {
        int placeID = mobility.keyAt(index);
        double probability = mobility.get(placeID);
        int numVisits = appHistogram.get(placeID, 0);// no visits if place not in histogram
        expected[index] = probability;
        observed[index] = numVisits;
        toBeObserved[index] = numVisits;
        if (placeID == currentPlace) {
            toBeObserved[index]++; //to be observed?
        }
        Util.Log(Util.SESSION_TAG, "place:\t" + placeID + "\texp:\t" + probability + "\tobs:\t" + numVisits);
    }
    double pValueOld = new ChiSquareTest().chiSquareTest(expected, observed);
    double pValueNew = new ChiSquareTest().chiSquareTest(expected, toBeObserved); //automatic normalization

    Util.Log(Util.SESSION_TAG, pValueOld + "\t" + Arrays.toString(expected) + "\t" + Arrays.toString(observed));
    Util.Log(Util.SESSION_TAG, pValueNew + "\t" + Arrays.toString(expected) + "\t" + Arrays.toString(observed));
    return pValueNew;
}

From source file:com.facebook.litho.ComponentsPools.java

@ThreadSafe(enableChecks = false)
static void release(Context context, ComponentLifecycle lifecycle, Object mountContent) {
    if (context instanceof ComponentContext) {
        context = ((ComponentContext) context).getBaseContext();

        if (context instanceof ComponentContext) {
            throw new IllegalStateException("Double wrapped ComponentContext.");
        }//from   w w w . j  a va  2  s .  c o  m
    }

    RecyclePool pool = null;

    synchronized (mountContentLock) {
        SparseArray<RecyclePool> poolsArray = sMountContentPoolsByContext.get(context);
        if (poolsArray != null) {
            pool = poolsArray.get(lifecycle.getId());
            if (pool == null) {
                pool = new RecyclePool("MountContent - " + lifecycle.getClass().getSimpleName(),
                        lifecycle.poolSize(), true);
                poolsArray.put(lifecycle.getId(), pool);
            }
        }

        if (pool != null) {
            pool.release(mountContent);
        }
    }
}

From source file:Main.java

/**
 * @return theoretical fishers table/*www .j a v  a 2  s  .c o  m*/
 * table realized by SparseArray, in connection with better performance than HashMap
 */
private static Double getTheoreticalFisher(int n) {
    SparseArray<Double> theorFisher = new SparseArray<Double>();
    theorFisher.put(1, 12.706);
    theorFisher.put(2, 4.3027);
    theorFisher.put(3, 3.1825);
    theorFisher.put(4, 2.7764);
    theorFisher.put(5, 2.5706);
    theorFisher.put(6, 2.4469);
    theorFisher.put(7, 2.3646);
    theorFisher.put(8, 2.3060);
    theorFisher.put(9, 2.2622);
    theorFisher.put(10, 2.2281);
    theorFisher.put(11, 2.2010);
    theorFisher.put(12, 2.1788);
    theorFisher.put(13, 2.1604);
    theorFisher.put(14, 2.1448);
    theorFisher.put(15, 2.1315);
    theorFisher.put(16, 2.1199);
    theorFisher.put(17, 2.1098);
    theorFisher.put(18, 2.1009);
    theorFisher.put(19, 2.0930);
    theorFisher.put(20, 2.0860);
    theorFisher.put(30, 2.0423);
    theorFisher.put(40, 2.0211);
    theorFisher.put(60, 2.0003);
    if (n >= 60) {
        return theorFisher.get(60);
    } else if (n >= 40) {
        return theorFisher.get(40);
    } else if (n >= 30) {
        return theorFisher.get(30);
    } else if (n >= 20) {
        return theorFisher.get(20);
    } else {
        return theorFisher.get(n);
    }
}

From source file:Main.java

/**
 * This picks a dominant color, looking for high-saturation, high-value, repeated hues.
 * @param bitmap The bitmap to scan//from w  w w.ja  v  a  2s .c  om
 * @param samples The approximate max number of samples to use.
 */
static int findDominantColorByHue(Bitmap bitmap, int samples) {
    final int height = bitmap.getHeight();
    final int width = bitmap.getWidth();
    int sampleStride = (int) Math.sqrt((height * width) / samples);
    if (sampleStride < 1) {
        sampleStride = 1;
    }

    // This is an out-param, for getting the hsv values for an rgb
    float[] hsv = new float[3];

    // First get the best hue, by creating a histogram over 360 hue buckets,
    // where each pixel contributes a score weighted by saturation, value, and alpha.
    float[] hueScoreHistogram = new float[360];
    float highScore = -1;
    int bestHue = -1;

    for (int y = 0; y < height; y += sampleStride) {
        for (int x = 0; x < width; x += sampleStride) {
            int argb = bitmap.getPixel(x, y);
            int alpha = 0xFF & (argb >> 24);
            if (alpha < 0x80) {
                // Drop mostly-transparent pixels.
                continue;
            }
            // Remove the alpha channel.
            int rgb = argb | 0xFF000000;
            Color.colorToHSV(rgb, hsv);
            // Bucket colors by the 360 integer hues.
            int hue = (int) hsv[0];
            if (hue < 0 || hue >= hueScoreHistogram.length) {
                // Defensively avoid array bounds violations.
                continue;
            }
            float score = hsv[1] * hsv[2];
            hueScoreHistogram[hue] += score;
            if (hueScoreHistogram[hue] > highScore) {
                highScore = hueScoreHistogram[hue];
                bestHue = hue;
            }
        }
    }

    SparseArray<Float> rgbScores = new SparseArray<Float>();
    int bestColor = 0xff000000;
    highScore = -1;
    // Go back over the RGB colors that match the winning hue,
    // creating a histogram of weighted s*v scores, for up to 100*100 [s,v] buckets.
    // The highest-scoring RGB color wins.
    for (int y = 0; y < height; y += sampleStride) {
        for (int x = 0; x < width; x += sampleStride) {
            int rgb = bitmap.getPixel(x, y) | 0xff000000;
            Color.colorToHSV(rgb, hsv);
            int hue = (int) hsv[0];
            if (hue == bestHue) {
                float s = hsv[1];
                float v = hsv[2];
                int bucket = (int) (s * 100) + (int) (v * 10000);
                // Score by cumulative saturation * value.
                float score = s * v;
                Float oldTotal = rgbScores.get(bucket);
                float newTotal = oldTotal == null ? score : oldTotal + score;
                rgbScores.put(bucket, newTotal);
                if (newTotal > highScore) {
                    highScore = newTotal;
                    // All the colors in the winning bucket are very similar. Last in wins.
                    bestColor = rgb;
                }
            }
        }
    }
    return bestColor;
}