Add isDeletedGroup.

Change-Id: I83f09c068868e5e6e1b46f494a6ef957f0b466d8
main
Yuichiro Hanada 2012-10-03 17:23:39 +09:00 committed by Android (Google) Code Review
parent 7223cc2ef1
commit a853356b82
3 changed files with 29 additions and 10 deletions

View File

@ -95,7 +95,9 @@ public final class BinaryDictIOUtils {
final boolean isMovedGroup = BinaryDictInputOutput.isMovedGroup(info.mFlags,
formatOptions);
if (!isMovedGroup
final boolean isDeletedGroup = BinaryDictInputOutput.isDeletedGroup(info.mFlags,
formatOptions);
if (!isMovedGroup && !isDeletedGroup
&& info.mFrequency != FusionDictionary.CharGroup.NOT_A_TERMINAL) {// found word
words.put(info.mOriginalAddress, new String(pushedChars, 0, index));
frequencies.put(info.mOriginalAddress, info.mFrequency);
@ -179,10 +181,13 @@ public final class BinaryDictIOUtils {
final int charGroupPos = buffer.position();
final CharGroupInfo currentInfo = BinaryDictInputOutput.readCharGroup(buffer,
buffer.position(), header.mFormatOptions);
if (BinaryDictInputOutput.isMovedGroup(currentInfo.mFlags,
header.mFormatOptions)) {
continue;
}
final boolean isMovedGroup =
BinaryDictInputOutput.isMovedGroup(currentInfo.mFlags,
header.mFormatOptions);
final boolean isDeletedGroup =
BinaryDictInputOutput.isDeletedGroup(currentInfo.mFlags,
header.mFormatOptions);
if (isMovedGroup) continue;
boolean same = true;
for (int p = 0, j = word.offsetByCodePoints(0, wordPos);
p < currentInfo.mCharacters.length;
@ -197,7 +202,8 @@ public final class BinaryDictIOUtils {
if (same) {
// found the group matches the word.
if (wordPos + currentInfo.mCharacters.length == wordLen) {
if (currentInfo.mFrequency == CharGroup.NOT_A_TERMINAL) {
if (currentInfo.mFrequency == CharGroup.NOT_A_TERMINAL
|| isDeletedGroup) {
return FormatSpec.NOT_VALID_WORD;
} else {
return charGroupPos;
@ -233,6 +239,10 @@ public final class BinaryDictIOUtils {
return FormatSpec.NOT_VALID_WORD;
}
private static int markAsDeleted(final int flags) {
return (flags & (~FormatSpec.MASK_GROUP_ADDRESS_TYPE)) | FormatSpec.FLAG_IS_DELETED;
}
/**
* Delete the word from the binary file.
*
@ -250,9 +260,8 @@ public final class BinaryDictIOUtils {
buffer.position(wordPosition);
final int flags = buffer.readUnsignedByte();
final int newFlags = flags ^ FormatSpec.FLAG_IS_TERMINAL;
buffer.position(wordPosition);
buffer.put((byte)newFlags);
buffer.put((byte)markAsDeleted(flags));
}
/**
@ -302,7 +311,7 @@ public final class BinaryDictIOUtils {
}
/**
* Update a parent address in a CharGroup that is addressed by groupOriginAddress.
* Update a parent address in a CharGroup that is referred to by groupOriginAddress.
*
* @param buffer the buffer to write.
* @param groupOriginAddress the address of the group.
@ -380,7 +389,7 @@ public final class BinaryDictIOUtils {
final int flags = buffer.readUnsignedByte();
final int parentAddress = BinaryDictInputOutput.readParentAddress(buffer, formatOptions);
skipString(buffer, (flags & FormatSpec.FLAG_HAS_MULTIPLE_CHARS) != 0);
if ((FormatSpec.FLAG_IS_TERMINAL) != 0) buffer.readUnsignedByte();
if ((flags & FormatSpec.FLAG_IS_TERMINAL) != 0) buffer.readUnsignedByte();
final int childrenOffset = newChildrenAddress == FormatSpec.NO_CHILDREN_ADDRESS
? FormatSpec.NO_CHILDREN_ADDRESS : newChildrenAddress - buffer.position();
writeSInt24ToBuffer(buffer, childrenOffset);

View File

@ -401,6 +401,14 @@ public final class BinaryDictInputOutput {
return options.mSupportsDynamicUpdate && ((flags & FormatSpec.FLAG_IS_MOVED) == 1);
}
/**
* Helper method to check whether the group is deleted.
*/
public static boolean isDeletedGroup(final int flags, final FormatOptions formatOptions) {
return formatOptions.mSupportsDynamicUpdate
&& ((flags & FormatSpec.MASK_GROUP_ADDRESS_TYPE) == FormatSpec.FLAG_IS_DELETED);
}
/**
* Helper method to check whether the dictionary can be updated dynamically.
*/

View File

@ -63,6 +63,7 @@ public final class FormatSpec {
* | This must be the same as FLAG_GROUP_ADDRESS_TYPE_THREEBYTES
* | 01 = yes : FLAG_IS_MOVED
* | the new address is stored in the same place as the parent address
* | is deleted? 10 = yes : FLAG_IS_DELETED
* | has several chars ? 1 bit, 1 = yes, 0 = no : FLAG_HAS_MULTIPLE_CHARS
* | has a terminal ? 1 bit, 1 = yes, 0 = no : FLAG_IS_TERMINAL
* | has shortcut targets ? 1 bit, 1 = yes, 0 = no : FLAG_HAS_SHORTCUT_TARGETS
@ -190,6 +191,7 @@ public final class FormatSpec {
static final int FIXED_BIT_OF_DYNAMIC_UPDATE_MOVE = 0x40;
static final int FLAG_IS_MOVED = 0x00 | FIXED_BIT_OF_DYNAMIC_UPDATE_MOVE;
static final int FLAG_IS_NOT_MOVED = 0x80 | FIXED_BIT_OF_DYNAMIC_UPDATE_MOVE;
static final int FLAG_IS_DELETED = 0x80;
static final int FLAG_ATTRIBUTE_HAS_NEXT = 0x80;
static final int FLAG_ATTRIBUTE_OFFSET_NEGATIVE = 0x40;