diff options
Diffstat (limited to 'java/src')
3 files changed, 100 insertions, 51 deletions
diff --git a/java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java b/java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java index 29e7c28f3..a54344ab5 100644 --- a/java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java +++ b/java/src/com/android/inputmethod/latin/makedict/BinaryDictInputOutput.java @@ -389,7 +389,7 @@ public final class BinaryDictInputOutput { * @param node the node to compute the maximum size of. * @param options file format options. */ - private static void setNodeMaximumSize(final Node node, final FormatOptions options) { + private static void calculateNodeMaximumSize(final Node node, final FormatOptions options) { int size = getGroupCountSize(node); for (CharGroup g : node.mData) { final int groupSize = getCharGroupMaximumSize(g, options); @@ -548,17 +548,17 @@ public final class BinaryDictInputOutput { boolean changed = false; int size = getGroupCountSize(node); for (CharGroup group : node.mData) { - if (group.mCachedAddress != node.mCachedAddress + size) { + if (group.mCachedAddress != node.mCachedAddressBeforeUpdate + size) { changed = true; - group.mCachedAddress = node.mCachedAddress + size; + group.mCachedAddress = node.mCachedAddressBeforeUpdate + size; } int groupSize = getGroupHeaderSize(group, formatOptions); if (group.isTerminal()) groupSize += FormatSpec.GROUP_FREQUENCY_SIZE; if (null == group.mChildren && formatOptions.mSupportsDynamicUpdate) { groupSize += FormatSpec.SIGNED_CHILDREN_ADDRESS_SIZE; } else if (null != group.mChildren) { - final int offsetBasePoint = groupSize + node.mCachedAddress + size; - final int offset = group.mChildren.mCachedAddress - offsetBasePoint; + final int offsetBasePoint = groupSize + node.mCachedAddressBeforeUpdate + size; + final int offset = group.mChildren.mCachedAddressBeforeUpdate - offsetBasePoint; if (formatOptions.mSupportsDynamicUpdate) { groupSize += FormatSpec.SIGNED_CHILDREN_ADDRESS_SIZE; } else { @@ -568,7 +568,7 @@ public final class BinaryDictInputOutput { groupSize += getShortcutListSize(group.mShortcutTargets); if (null != group.mBigrams) { for (WeightedString bigram : group.mBigrams) { - final int offsetBasePoint = groupSize + node.mCachedAddress + size + final int offsetBasePoint = groupSize + node.mCachedAddressBeforeUpdate + size + FormatSpec.GROUP_FLAGS_SIZE; final int addressOfBigram = findAddressOfWord(dict, bigram.mWord); final int offset = addressOfBigram - offsetBasePoint; @@ -589,20 +589,48 @@ public final class BinaryDictInputOutput { } /** - * Computes the byte size of a list of nodes and updates each node cached position. + * Initializes the cached addresses of nodes from their size. * * @param flatNodes the array of nodes. * @param formatOptions file format options. * @return the byte size of the entire stack. */ - private static int stackNodes(final ArrayList<Node> flatNodes, + private static int initializeNodesCachedAddresses(final ArrayList<Node> flatNodes, final FormatOptions formatOptions) { int nodeOffset = 0; - for (Node n : flatNodes) { - n.mCachedAddress = nodeOffset; + for (final Node n : flatNodes) { + n.mCachedAddressBeforeUpdate = nodeOffset; + int groupCountSize = getGroupCountSize(n); + int groupOffset = 0; + for (final CharGroup g : n.mData) { + g.mCachedAddress = groupCountSize + nodeOffset + groupOffset; + groupOffset += g.mCachedSize; + } + final int nodeSize = groupCountSize + groupOffset + + (formatOptions.mSupportsDynamicUpdate + ? FormatSpec.FORWARD_LINK_ADDRESS_SIZE : 0); + nodeOffset += n.mCachedSize; + } + return nodeOffset; + } + + /** + * Updates the cached addresses of nodes after recomputing their new positions. + * + * @param flatNodes the array of nodes. + * @param formatOptions file format options. + * @return the byte size of the entire stack. + */ + private static int updateNodeCachedAddresses(final ArrayList<Node> flatNodes, + final FormatOptions formatOptions) { + int nodeOffset = 0; + for (final Node n : flatNodes) { + n.mCachedAddressBeforeUpdate = n.mCachedAddressAfterUpdate; int groupCountSize = getGroupCountSize(n); int groupOffset = 0; - for (CharGroup g : n.mData) { + for (final CharGroup g : n.mData) { + // TODO: just copy cached address after update into cached address before update + // when the two fields are separated. g.mCachedAddress = groupCountSize + nodeOffset + groupOffset; groupOffset += g.mCachedSize; } @@ -610,8 +638,13 @@ public final class BinaryDictInputOutput { + (formatOptions.mSupportsDynamicUpdate ? FormatSpec.FORWARD_LINK_ADDRESS_SIZE : 0); if (nodeSize != n.mCachedSize) { + // TODO: remove this test when the addresses are separated throw new RuntimeException("Bug : Stored and computed node size differ"); } + if (nodeOffset != n.mCachedAddressAfterUpdate) { + // TODO: remove this test when the code is well tested + throw new RuntimeException("Bug : Stored and computed node address differ"); + } nodeOffset += n.mCachedSize; } return nodeOffset; @@ -627,11 +660,13 @@ public final class BinaryDictInputOutput { */ private static void computeParentAddresses(final ArrayList<Node> flatNodes) { for (final Node node : flatNodes) { - for (CharGroup group : node.mData) { + for (final CharGroup group : node.mData) { if (null != group.mChildren) { - // assign my address to children's parent address + // Assign my address to children's parent address + // Here BeforeUpdate and AfterUpdate addresses have the same value, so it + // does not matter which we use. group.mChildren.mCachedParentAddress = group.mCachedAddress - - group.mChildren.mCachedAddress; + - group.mChildren.mCachedAddressAfterUpdate; } } } @@ -654,9 +689,9 @@ public final class BinaryDictInputOutput { */ private static ArrayList<Node> computeAddresses(final FusionDictionary dict, final ArrayList<Node> flatNodes, final FormatOptions formatOptions) { - // First get the worst sizes and offsets - for (Node n : flatNodes) setNodeMaximumSize(n, formatOptions); - final int offset = stackNodes(flatNodes, formatOptions); + // First get the worst possible sizes and offsets + for (final Node n : flatNodes) calculateNodeMaximumSize(n, formatOptions); + final int offset = initializeNodesCachedAddresses(flatNodes, formatOptions); MakedictLog.i("Compressing the array addresses. Original size : " + offset); MakedictLog.i("(Recursively seen size : " + offset + ")"); @@ -665,14 +700,17 @@ public final class BinaryDictInputOutput { boolean changesDone = false; do { changesDone = false; - for (Node n : flatNodes) { + int nodeStartOffset = 0; + for (final Node n : flatNodes) { + n.mCachedAddressAfterUpdate = nodeStartOffset; final int oldNodeSize = n.mCachedSize; final boolean changed = computeActualNodeSize(n, dict, formatOptions); final int newNodeSize = n.mCachedSize; if (oldNodeSize < newNodeSize) throw new RuntimeException("Increased size ?!"); + nodeStartOffset += newNodeSize; changesDone |= changed; } - stackNodes(flatNodes, formatOptions); + updateNodeCachedAddresses(flatNodes, formatOptions); ++passes; if (passes > MAX_PASSES) throw new RuntimeException("Too many passes - probably a bug"); } while (changesDone); @@ -683,7 +721,7 @@ public final class BinaryDictInputOutput { final Node lastNode = flatNodes.get(flatNodes.size() - 1); MakedictLog.i("Compression complete in " + passes + " passes."); MakedictLog.i("After address compression : " - + (lastNode.mCachedAddress + lastNode.mCachedSize)); + + (lastNode.mCachedAddressAfterUpdate + lastNode.mCachedSize)); return flatNodes; } @@ -701,10 +739,12 @@ public final class BinaryDictInputOutput { private static void checkFlatNodeArray(final ArrayList<Node> array) { int offset = 0; int index = 0; - for (Node n : array) { - if (n.mCachedAddress != offset) { + for (final Node n : array) { + // BeforeUpdate and AfterUpdate addresses are the same here, so it does not matter + // which we use. + if (n.mCachedAddressAfterUpdate != offset) { throw new RuntimeException("Wrong address for node " + index - + " : expected " + offset + ", got " + n.mCachedAddress); + + " : expected " + offset + ", got " + n.mCachedAddressAfterUpdate); } ++index; offset += n.mCachedSize; @@ -946,7 +986,7 @@ public final class BinaryDictInputOutput { private static int writePlacedNode(final FusionDictionary dict, byte[] buffer, final Node node, final FormatOptions formatOptions) { // TODO: Make the code in common with BinaryDictIOUtils#writeCharGroup - int index = node.mCachedAddress; + int index = node.mCachedAddressAfterUpdate; final int groupCount = node.mData.size(); final int countSize = getGroupCountSize(node); @@ -977,7 +1017,7 @@ public final class BinaryDictInputOutput { if (group.mFrequency >= 0) groupAddress += FormatSpec.GROUP_FREQUENCY_SIZE; final int childrenOffset = null == group.mChildren ? FormatSpec.NO_CHILDREN_ADDRESS - : group.mChildren.mCachedAddress - groupAddress; + : group.mChildren.mCachedAddressAfterUpdate - groupAddress; byte flags = makeCharGroupFlags(group, groupAddress, childrenOffset, formatOptions); buffer[index++] = flags; @@ -985,7 +1025,7 @@ public final class BinaryDictInputOutput { index = writeParentAddress(buffer, index, parentAddress, formatOptions); } else { index = writeParentAddress(buffer, index, - parentAddress + (node.mCachedAddress - group.mCachedAddress), + parentAddress + (node.mCachedAddressAfterUpdate - group.mCachedAddress), formatOptions); } @@ -1055,9 +1095,9 @@ public final class BinaryDictInputOutput { = FormatSpec.NO_FORWARD_LINK_ADDRESS; index += FormatSpec.FORWARD_LINK_ADDRESS_SIZE; } - if (index != node.mCachedAddress + node.mCachedSize) throw new RuntimeException( + if (index != node.mCachedAddressAfterUpdate + node.mCachedSize) throw new RuntimeException( "Not the same size : written " - + (index - node.mCachedAddress) + " bytes out of a node that should have " + + (index - node.mCachedAddressAfterUpdate) + " bytes from a node that should have " + node.mCachedSize + " bytes"); return index; } @@ -1077,25 +1117,27 @@ public final class BinaryDictInputOutput { int charGroups = 0; int maxGroups = 0; int maxRuns = 0; - for (Node n : nodes) { + for (final Node n : nodes) { if (maxGroups < n.mData.size()) maxGroups = n.mData.size(); - for (CharGroup cg : n.mData) { + for (final CharGroup cg : n.mData) { ++charGroups; if (cg.mChars.length > maxRuns) maxRuns = cg.mChars.length; if (cg.mFrequency >= 0) { - if (n.mCachedAddress < firstTerminalAddress) - firstTerminalAddress = n.mCachedAddress; - if (n.mCachedAddress > lastTerminalAddress) - lastTerminalAddress = n.mCachedAddress; + if (n.mCachedAddressAfterUpdate < firstTerminalAddress) + firstTerminalAddress = n.mCachedAddressAfterUpdate; + if (n.mCachedAddressAfterUpdate > lastTerminalAddress) + lastTerminalAddress = n.mCachedAddressAfterUpdate; } } - if (n.mCachedAddress + n.mCachedSize > size) size = n.mCachedAddress + n.mCachedSize; + if (n.mCachedAddressAfterUpdate + n.mCachedSize > size) { + size = n.mCachedAddressAfterUpdate + n.mCachedSize; + } } final int[] groupCounts = new int[maxGroups + 1]; final int[] runCounts = new int[maxRuns + 1]; - for (Node n : nodes) { + for (final Node n : nodes) { ++groupCounts[n.mData.size()]; - for (CharGroup cg : n.mData) { + for (final CharGroup cg : n.mData) { ++runCounts[cg.mChars.length]; } } @@ -1205,7 +1247,7 @@ public final class BinaryDictInputOutput { // Create a buffer that matches the final dictionary size. final Node lastNode = flatNodes.get(flatNodes.size() - 1); - final int bufferSize = lastNode.mCachedAddress + lastNode.mCachedSize; + final int bufferSize = lastNode.mCachedAddressAfterUpdate + lastNode.mCachedSize; final byte[] buffer = new byte[bufferSize]; int index = 0; @@ -1584,8 +1626,9 @@ public final class BinaryDictInputOutput { buffer.position() != FormatSpec.NO_FORWARD_LINK_ADDRESS); final Node node = new Node(nodeContents); - node.mCachedAddress = nodeOrigin; - reverseNodeMap.put(node.mCachedAddress, node); + node.mCachedAddressBeforeUpdate = nodeOrigin; + node.mCachedAddressAfterUpdate = nodeOrigin; + reverseNodeMap.put(node.mCachedAddressAfterUpdate, node); return node; } diff --git a/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java b/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java index 17d281518..5530b7a74 100644 --- a/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java +++ b/java/src/com/android/inputmethod/latin/makedict/FusionDictionary.java @@ -46,7 +46,13 @@ public final class FusionDictionary implements Iterable<Word> { ArrayList<CharGroup> mData; // To help with binary generation int mCachedSize = Integer.MIN_VALUE; - int mCachedAddress = Integer.MIN_VALUE; + // mCachedAddressBefore/AfterUpdate are helpers for binary dictionary generation. They + // always hold the same value except between dictionary address compression, during which + // the update process needs to know about both values at the same time. Updating will + // update the AfterUpdate value, and the code will move them to BeforeUpdate before + // the next update pass. + int mCachedAddressBeforeUpdate = Integer.MIN_VALUE; + int mCachedAddressAfterUpdate = Integer.MIN_VALUE; int mCachedParentAddress = 0; public Node() { diff --git a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionarySettings.java b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionarySettings.java index 50dda9663..64f860e7d 100644 --- a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionarySettings.java +++ b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionarySettings.java @@ -1,17 +1,17 @@ -/** - * Copyright (C) 2013 Google Inc. +/* + * Copyright (C) 2013 The Android Open Source Project * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not - * use this file except in compliance with the License. You may obtain a copy - * of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.android.inputmethod.latin.userdictionary; |