/** * Fills all keys contained in the receiver into the specified list. Fills the list, starting at * index 0. After this call returns the specified list has a new size that equals * <tt>this.size()</tt>. Iteration order is guaranteed to be <i>identical</i> to the order used by * method {@link #forEachKey(IntProcedure)}. * * <p>This method can be used to iterate over the keys of the receiver. * * @param list the list to be filled, can have any size. */ public void keys(IntArrayList list) { list.setSize(distinct); int[] elements = list.elements(); int[] tab = table; byte[] stat = state; int j = 0; for (int i = tab.length; i-- > 0; ) { if (stat[i] == FULL) elements[j++] = tab[i]; } }
/** Stable median calculation. */ public double calcStableMedian(double lowerFrac, double upperFrac, IntArrayList list) { // This works for any size! list.trimToSize(); Arrays.sort(list.elements()); int sum = 0; int cnt = 0; for (int i = (int) (lowerFrac * list.size()); i < (int) (list.size() * upperFrac); i++) { sum += list.get(i); cnt++; } return (double) sum / cnt; // below is the original. it Relies on bucket-sorting. It should be perfectly possible to write // a linear-time stable median // based on the normal linear-time median algorithm. This is needed to handle non-8bit images. // Value might be off a bit but not much (need to think of indexing), and it doesn't matter for // this application /* int numbins=66000; int[] elem=list.elements(); int numElem=list.size(); //Calculate histogram int[] histogram=new int[numbins]; //int[] elem=curBgOutside.elements(); //int numElem=curBgOutside.size(); for(int i=0;i<numElem;i++) histogram[elem[i]]++; int jumpElem=0; int lowerIndex=(int)(numElem*lowerFrac); int upperIndex=(int)(numElem*upperFrac); int sum=0; int cnt=0; for(int i=0;i<numbins;i++) { int thisNum=histogram[i]; int take=Math.min(upperIndex,jumpElem+thisNum)-Math.max(lowerIndex, jumpElem); if(take>0) { sum+=take*i; cnt+=take; } jumpElem+=thisNum; } return (double)sum/cnt; */ }