From bf62dc9460408dc37324c03735ab13c2cdf45396 Mon Sep 17 00:00:00 2001 From: Kurt Partridge Date: Fri, 22 Mar 2013 14:13:03 -0700 Subject: 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 --- .../inputmethod/research/FixedLogBuffer.java | 26 ++++++++++++++-------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'java/src/com/android/inputmethod/research/FixedLogBuffer.java') diff --git a/java/src/com/android/inputmethod/research/FixedLogBuffer.java b/java/src/com/android/inputmethod/research/FixedLogBuffer.java index 78dc59562..641bf7eae 100644 --- a/java/src/com/android/inputmethod/research/FixedLogBuffer.java +++ b/java/src/com/android/inputmethod/research/FixedLogBuffer.java @@ -51,10 +51,6 @@ public class FixedLogBuffer extends LogBuffer { mNumActualWords = 0; } - protected int getNumActualWords() { - return mNumActualWords; - } - /** * Adds a new LogUnit to the front of the LIFO queue, evicting existing LogUnit's * (oldest first) if word capacity is reached. @@ -119,12 +115,24 @@ public class FixedLogBuffer extends LogBuffer { return logUnit; } - protected void shiftOutWords(final int numWords) { - final int targetNumWords = mNumActualWords - numWords; - final LinkedList logUnits = getLogUnits(); - while (mNumActualWords > targetNumWords && !logUnits.isEmpty()) { - shiftOut(); + /** + * Remove LogUnits from the front of the LogBuffer until {@code numWords} have been removed. + * + * If there are less than {@code numWords} word-containing {@link LogUnit}s, shifts out + * 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() { -- cgit v1.2.3-83-g751a