If you think the Android project Jello listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package com.atteo.jello.space;
/*fromwww.java2s.com*/import java.util.ArrayList;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.name.Named;
@Singleton
publicclass VanillaHistogram implements NextFitHistogram {
privatefinalshort classSize;
privatefinal ArrayList<Integer> witnesses;
privatefinal ArrayList<Integer> counts;
privateint count;
privatefinalint histogramClasses;
@Inject
public VanillaHistogram(@Named("pageSize") finalshort pageSize,
@Named("nextFitHistogramClasses") int histogramClasses) {
if (pageSize % histogramClasses != 0)
thrownew IllegalArgumentException(
"histogramClasses should divide pageSize");
classSize = (short) (pageSize / histogramClasses);
histogramClasses++; // empty pages have a separate class
this.histogramClasses = histogramClasses;
count = 0;
witnesses = new ArrayList<Integer>();
counts = new ArrayList<Integer>();
for (int i = 0; i < histogramClasses; i++) {
witnesses.add(-1);
counts.add(0);
}
}
publicshort getClassSize() {
return classSize;
}
publicint getWitness(finalshort freeSpace) {
int loc = classFor(freeSpace);
loc++;
if (loc == histogramClasses)
loc = histogramClasses - 1;
boolean found = false;
while (loc < histogramClasses) {
if (counts.get(loc) > 0) {
found = true;
finalint w = witnesses.get(loc);
if (w != -1)
return w;
}
loc++;
}
if (found == false)
return NextFitHistogram.NO_PAGE;
return NextFitHistogram.NO_WITNESS;
}
publicvoid update(finalint id, finalshort previousFreeSpace,
finalshort freeSpace) {
if (previousFreeSpace != -1) {
finalint loc = classFor(previousFreeSpace);
counts.set(loc, counts.get(loc) - 1);
if (witnesses.get(loc) == id)
witnesses.set(loc, -1);
count--;
}
if (freeSpace == -1)
return;
finalint loc = classFor(freeSpace);
counts.set(loc, counts.get(loc) + 1);
witnesses.set(loc, id);
count++;
}
privateint classFor(finalint freeSpace) {
return freeSpace / classSize;
}
}