Fix bug in counting words between samples

Previously MainLogBuffer#shiftOutWords() assumed it wouldn't be called if
mNumWordsUntilSafeToSample was 0.  This relaxes this assumption (which is in fact
false in the current code).

Change-Id: I8723248095e84a0d9d6f4639b4742cc7dda9716b
main
Kurt Partridge 2013-03-22 14:13:03 -07:00
parent 3970352ea9
commit bf62dc9460
2 changed files with 23 additions and 20 deletions

View File

@ -51,10 +51,6 @@ public class FixedLogBuffer extends LogBuffer {
mNumActualWords = 0; mNumActualWords = 0;
} }
protected int getNumActualWords() {
return mNumActualWords;
}
/** /**
* Adds a new LogUnit to the front of the LIFO queue, evicting existing LogUnit's * Adds a new LogUnit to the front of the LIFO queue, evicting existing LogUnit's
* (oldest first) if word capacity is reached. * (oldest first) if word capacity is reached.
@ -119,13 +115,25 @@ public class FixedLogBuffer extends LogBuffer {
return logUnit; return logUnit;
} }
protected void shiftOutWords(final int numWords) { /**
final int targetNumWords = mNumActualWords - numWords; * Remove LogUnits from the front of the LogBuffer until {@code numWords} have been removed.
final LinkedList<LogUnit> logUnits = getLogUnits(); *
while (mNumActualWords > targetNumWords && !logUnits.isEmpty()) { * If there are less than {@code numWords} word-containing {@link LogUnit}s, shifts out
shiftOut(); * all {@code LogUnit}s in the buffer.
*
* @param numWords the number of word-containing {@link LogUnit}s to shift out
* @return the number of actual {@code LogUnit}s shifted out
*/
protected int shiftOutWords(final int numWords) {
int numWordContainingLogUnitsShiftedOut = 0;
for (LogUnit logUnit = shiftOut(); logUnit != null
&& numWordContainingLogUnitsShiftedOut < numWords; logUnit = shiftOut()) {
if (logUnit.hasWord()) {
numWordContainingLogUnitsShiftedOut++;
} }
} }
return numWordContainingLogUnitsShiftedOut;
}
public void shiftOutAll() { public void shiftOutAll() {
final LinkedList<LogUnit> logUnits = getLogUnits(); final LinkedList<LogUnit> logUnits = getLogUnits();

View File

@ -25,7 +25,6 @@ import com.android.inputmethod.latin.define.ProductionFlag;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Random;
/** /**
* MainLogBuffer is a FixedLogBuffer that tracks the state of LogUnits to make privacy guarantees. * MainLogBuffer is a FixedLogBuffer that tracks the state of LogUnits to make privacy guarantees.
@ -100,10 +99,6 @@ public abstract class MainLogBuffer extends FixedLogBuffer {
return mSuggest.getMainDictionary(); return mSuggest.getMainDictionary();
} }
public void resetWordCounter() {
mNumWordsUntilSafeToSample = mNumWordsBetweenNGrams;
}
public void setIsStopping() { public void setIsStopping() {
mIsStopping = true; mIsStopping = true;
} }
@ -201,7 +196,7 @@ public abstract class MainLogBuffer extends FixedLogBuffer {
// Good n-gram at the front of the buffer. Publish it, disclosing details. // Good n-gram at the front of the buffer. Publish it, disclosing details.
publish(logUnits, true /* canIncludePrivateData */); publish(logUnits, true /* canIncludePrivateData */);
shiftOutWords(N_GRAM_SIZE); shiftOutWords(N_GRAM_SIZE);
resetWordCounter(); mNumWordsUntilSafeToSample = mNumWordsBetweenNGrams;
} else { } else {
// No good n-gram at front, and buffer is full. Shift out the first word (or if there // No good n-gram at front, and buffer is full. Shift out the first word (or if there
// is none, the existing logUnits). // is none, the existing logUnits).
@ -224,13 +219,13 @@ public abstract class MainLogBuffer extends FixedLogBuffer {
final boolean canIncludePrivateData); final boolean canIncludePrivateData);
@Override @Override
protected void shiftOutWords(final int numWords) { protected int shiftOutWords(final int numWords) {
final int oldNumActualWords = getNumActualWords(); final int numWordContainingLogUnitsShiftedOut = super.shiftOutWords(numWords);
super.shiftOutWords(numWords); mNumWordsUntilSafeToSample = Math.max(0, mNumWordsUntilSafeToSample
final int numWordsShifted = oldNumActualWords - getNumActualWords(); - numWordContainingLogUnitsShiftedOut);
mNumWordsUntilSafeToSample -= numWordsShifted;
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "wordsUntilSafeToSample now at " + mNumWordsUntilSafeToSample); Log.d(TAG, "wordsUntilSafeToSample now at " + mNumWordsUntilSafeToSample);
} }
return numWordContainingLogUnitsShiftedOut;
} }
} }