Make writePlacedNode write the linked-list node.

Change-Id: I60feda815ea08cf73300fccca1ae12b97550f116
main
Yuichiro Hanada 2012-09-18 15:28:05 +09:00
parent 061d225fb1
commit bf45dc4860
2 changed files with 30 additions and 7 deletions

View File

@ -370,6 +370,9 @@ public class BinaryDictInputOutput {
g.mCachedSize = groupSize; g.mCachedSize = groupSize;
size += groupSize; size += groupSize;
} }
if (options.mHasLinkedListNode) {
size += FormatSpec.FORWARD_LINK_ADDRESS_SIZE;
}
node.mCachedSize = size; node.mCachedSize = size;
} }
@ -521,6 +524,9 @@ public class BinaryDictInputOutput {
group.mCachedSize = groupSize; group.mCachedSize = groupSize;
size += groupSize; size += groupSize;
} }
if (formatOptions.mHasLinkedListNode) {
size += FormatSpec.FORWARD_LINK_ADDRESS_SIZE;
}
if (node.mCachedSize != size) { if (node.mCachedSize != size) {
node.mCachedSize = size; node.mCachedSize = size;
changed = true; changed = true;
@ -532,9 +538,11 @@ public class BinaryDictInputOutput {
* Computes the byte size of a list of nodes and updates each node cached position. * Computes the byte size of a list of nodes and updates each node cached position.
* *
* @param flatNodes the array of nodes. * @param flatNodes the array of nodes.
* @param formatOptions file format options.
* @return the byte size of the entire stack. * @return the byte size of the entire stack.
*/ */
private static int stackNodes(final ArrayList<Node> flatNodes) { private static int stackNodes(final ArrayList<Node> flatNodes,
final FormatOptions formatOptions) {
int nodeOffset = 0; int nodeOffset = 0;
for (Node n : flatNodes) { for (Node n : flatNodes) {
n.mCachedAddress = nodeOffset; n.mCachedAddress = nodeOffset;
@ -544,7 +552,9 @@ public class BinaryDictInputOutput {
g.mCachedAddress = groupCountSize + nodeOffset + groupOffset; g.mCachedAddress = groupCountSize + nodeOffset + groupOffset;
groupOffset += g.mCachedSize; groupOffset += g.mCachedSize;
} }
if (groupOffset + groupCountSize != n.mCachedSize) { final int nodeSize = groupCountSize + groupOffset
+ (formatOptions.mHasLinkedListNode ? FormatSpec.FORWARD_LINK_ADDRESS_SIZE : 0);
if (nodeSize != n.mCachedSize) {
throw new RuntimeException("Bug : Stored and computed node size differ"); throw new RuntimeException("Bug : Stored and computed node size differ");
} }
nodeOffset += n.mCachedSize; nodeOffset += n.mCachedSize;
@ -571,7 +581,7 @@ public class BinaryDictInputOutput {
final ArrayList<Node> flatNodes, final FormatOptions formatOptions) { final ArrayList<Node> flatNodes, final FormatOptions formatOptions) {
// First get the worst sizes and offsets // First get the worst sizes and offsets
for (Node n : flatNodes) setNodeMaximumSize(n, formatOptions); for (Node n : flatNodes) setNodeMaximumSize(n, formatOptions);
final int offset = stackNodes(flatNodes); final int offset = stackNodes(flatNodes, formatOptions);
MakedictLog.i("Compressing the array addresses. Original size : " + offset); MakedictLog.i("Compressing the array addresses. Original size : " + offset);
MakedictLog.i("(Recursively seen size : " + offset + ")"); MakedictLog.i("(Recursively seen size : " + offset + ")");
@ -587,7 +597,7 @@ public class BinaryDictInputOutput {
if (oldNodeSize < newNodeSize) throw new RuntimeException("Increased size ?!"); if (oldNodeSize < newNodeSize) throw new RuntimeException("Increased size ?!");
changesDone |= changed; changesDone |= changed;
} }
stackNodes(flatNodes); stackNodes(flatNodes, formatOptions);
++passes; ++passes;
if (passes > MAX_PASSES) throw new RuntimeException("Too many passes - probably a bug"); if (passes > MAX_PASSES) throw new RuntimeException("Too many passes - probably a bug");
} while (changesDone); } while (changesDone);
@ -910,6 +920,11 @@ public class BinaryDictInputOutput {
} }
} }
if (formatOptions.mHasLinkedListNode) {
buffer[index] = buffer[index + 1] = buffer[index + 2]
= FormatSpec.NO_FORWARD_LINK_ADDRESS;
index += FormatSpec.FORWARD_LINK_ADDRESS_SIZE;
}
if (index != node.mCachedAddress + node.mCachedSize) throw new RuntimeException( if (index != node.mCachedAddress + node.mCachedSize) throw new RuntimeException(
"Not the same size : written " "Not the same size : written "
+ (index - node.mCachedAddress) + " bytes out of a node that should have " + (index - node.mCachedAddress) + " bytes out of a node that should have "

View File

@ -65,9 +65,12 @@ public class BinaryDictIOTests extends AndroidTestCase {
private static final FormatSpec.FormatOptions VERSION2 = new FormatSpec.FormatOptions(2); private static final FormatSpec.FormatOptions VERSION2 = new FormatSpec.FormatOptions(2);
private static final FormatSpec.FormatOptions VERSION3_WITHOUT_PARENTADDRESS = private static final FormatSpec.FormatOptions VERSION3_WITHOUT_PARENTADDRESS =
new FormatSpec.FormatOptions(3, false); new FormatSpec.FormatOptions(3, false /* hasParentAddress */);
private static final FormatSpec.FormatOptions VERSION3_WITH_PARENTADDRESS = private static final FormatSpec.FormatOptions VERSION3_WITH_PARENTADDRESS =
new FormatSpec.FormatOptions(3, true); new FormatSpec.FormatOptions(3, true /* hasParentAddress */);
private static final FormatSpec.FormatOptions VERSION3_WITH_LINKEDLIST_NODE =
new FormatSpec.FormatOptions(3, true /* hasParentAddress */,
true /* hasLinkedListNode */);
private static final String[] CHARACTERS = { private static final String[] CHARACTERS = {
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
@ -236,7 +239,8 @@ public class BinaryDictIOTests extends AndroidTestCase {
String result = " : buffer type = " String result = " : buffer type = "
+ ((bufferType == USE_BYTE_BUFFER) ? "byte buffer" : "byte array"); + ((bufferType == USE_BYTE_BUFFER) ? "byte buffer" : "byte array");
result += " : version = " + formatOptions.mVersion; result += " : version = " + formatOptions.mVersion;
return result + ", hasParentAddress = " + formatOptions.mHasParentAddress; return result + ", hasParentAddress = " + formatOptions.mHasParentAddress
+ ", hasLinkedListNode = " + formatOptions.mHasLinkedListNode;
} }
// Tests for readDictionaryBinary and writeDictionaryBinary // Tests for readDictionaryBinary and writeDictionaryBinary
@ -305,6 +309,7 @@ public class BinaryDictIOTests extends AndroidTestCase {
runReadAndWriteTests(results, USE_BYTE_BUFFER, VERSION2); runReadAndWriteTests(results, USE_BYTE_BUFFER, VERSION2);
runReadAndWriteTests(results, USE_BYTE_BUFFER, VERSION3_WITHOUT_PARENTADDRESS); runReadAndWriteTests(results, USE_BYTE_BUFFER, VERSION3_WITHOUT_PARENTADDRESS);
runReadAndWriteTests(results, USE_BYTE_BUFFER, VERSION3_WITH_PARENTADDRESS); runReadAndWriteTests(results, USE_BYTE_BUFFER, VERSION3_WITH_PARENTADDRESS);
runReadAndWriteTests(results, USE_BYTE_BUFFER, VERSION3_WITH_LINKEDLIST_NODE);
for (final String result : results) { for (final String result : results) {
Log.d(TAG, result); Log.d(TAG, result);
@ -317,6 +322,7 @@ public class BinaryDictIOTests extends AndroidTestCase {
runReadAndWriteTests(results, USE_BYTE_ARRAY, VERSION2); runReadAndWriteTests(results, USE_BYTE_ARRAY, VERSION2);
runReadAndWriteTests(results, USE_BYTE_ARRAY, VERSION3_WITHOUT_PARENTADDRESS); runReadAndWriteTests(results, USE_BYTE_ARRAY, VERSION3_WITHOUT_PARENTADDRESS);
runReadAndWriteTests(results, USE_BYTE_ARRAY, VERSION3_WITH_PARENTADDRESS); runReadAndWriteTests(results, USE_BYTE_ARRAY, VERSION3_WITH_PARENTADDRESS);
runReadAndWriteTests(results, USE_BYTE_ARRAY, VERSION3_WITH_LINKEDLIST_NODE);
for (final String result : results) { for (final String result : results) {
Log.d(TAG, result); Log.d(TAG, result);
@ -450,6 +456,7 @@ public class BinaryDictIOTests extends AndroidTestCase {
runReadUnigramsAndBigramsTests(results, USE_BYTE_BUFFER, VERSION2); runReadUnigramsAndBigramsTests(results, USE_BYTE_BUFFER, VERSION2);
runReadUnigramsAndBigramsTests(results, USE_BYTE_BUFFER, VERSION3_WITHOUT_PARENTADDRESS); runReadUnigramsAndBigramsTests(results, USE_BYTE_BUFFER, VERSION3_WITHOUT_PARENTADDRESS);
runReadUnigramsAndBigramsTests(results, USE_BYTE_BUFFER, VERSION3_WITH_PARENTADDRESS); runReadUnigramsAndBigramsTests(results, USE_BYTE_BUFFER, VERSION3_WITH_PARENTADDRESS);
runReadUnigramsAndBigramsTests(results, USE_BYTE_BUFFER, VERSION3_WITH_LINKEDLIST_NODE);
for (final String result : results) { for (final String result : results) {
Log.d(TAG, result); Log.d(TAG, result);
@ -462,6 +469,7 @@ public class BinaryDictIOTests extends AndroidTestCase {
runReadUnigramsAndBigramsTests(results, USE_BYTE_ARRAY, VERSION2); runReadUnigramsAndBigramsTests(results, USE_BYTE_ARRAY, VERSION2);
runReadUnigramsAndBigramsTests(results, USE_BYTE_ARRAY, VERSION3_WITHOUT_PARENTADDRESS); runReadUnigramsAndBigramsTests(results, USE_BYTE_ARRAY, VERSION3_WITHOUT_PARENTADDRESS);
runReadUnigramsAndBigramsTests(results, USE_BYTE_ARRAY, VERSION3_WITH_PARENTADDRESS); runReadUnigramsAndBigramsTests(results, USE_BYTE_ARRAY, VERSION3_WITH_PARENTADDRESS);
runReadUnigramsAndBigramsTests(results, USE_BYTE_ARRAY, VERSION3_WITH_LINKEDLIST_NODE);
for (final String result : results) { for (final String result : results) {
Log.d(TAG, result); Log.d(TAG, result);