Add makeCharGroupFlags.

Change-Id: Id2c580f21b77f66a97c5fbdf4542fdafe6c43614
main
Yuichiro Hanada 2012-10-03 12:34:52 +09:00
parent 8e01cc768d
commit 4ad4ff618f
2 changed files with 59 additions and 46 deletions

View File

@ -720,51 +720,58 @@ public final class BinaryDictInputOutput {
return 3; return 3;
} }
/**
* Makes the flag value for a char group.
*
* @param hasMultipleChars whether the group has multiple chars.
* @param isTerminal whether the group is terminal.
* @param childrenAddressSize the size of a children address.
* @param hasShortcuts whether the group has shortcuts.
* @param hasBigrams whether the group has bigrams.
* @param isNotAWord whether the group is not a word.
* @param isBlackListEntry whether the group is a blacklist entry.
* @param formatOptions file format options.
* @return the flags
*/
static int makeCharGroupFlags(final boolean hasMultipleChars, final boolean isTerminal,
final int childrenAddressSize, final boolean hasShortcuts, final boolean hasBigrams,
final boolean isNotAWord, final boolean isBlackListEntry,
final FormatOptions formatOptions) {
byte flags = 0;
if (hasMultipleChars) flags |= FormatSpec.FLAG_HAS_MULTIPLE_CHARS;
if (isTerminal) flags |= FormatSpec.FLAG_IS_TERMINAL;
if (formatOptions.mSupportsDynamicUpdate) {
flags |= FormatSpec.FLAG_IS_NOT_MOVED;
} else if (true) {
switch (childrenAddressSize) {
case 1:
flags |= FormatSpec.FLAG_GROUP_ADDRESS_TYPE_ONEBYTE;
break;
case 2:
flags |= FormatSpec.FLAG_GROUP_ADDRESS_TYPE_TWOBYTES;
break;
case 3:
flags |= FormatSpec.FLAG_GROUP_ADDRESS_TYPE_THREEBYTES;
break;
case 0:
flags |= FormatSpec.FLAG_GROUP_ADDRESS_TYPE_NOADDRESS;
break;
default:
throw new RuntimeException("Node with a strange address");
}
}
if (hasShortcuts) flags |= FormatSpec.FLAG_HAS_SHORTCUT_TARGETS;
if (hasBigrams) flags |= FormatSpec.FLAG_HAS_BIGRAMS;
if (isNotAWord) flags |= FormatSpec.FLAG_IS_NOT_A_WORD;
if (isBlackListEntry) flags |= FormatSpec.FLAG_IS_BLACKLISTED;
return flags;
}
private static byte makeCharGroupFlags(final CharGroup group, final int groupAddress, private static byte makeCharGroupFlags(final CharGroup group, final int groupAddress,
final int childrenOffset, final FormatOptions formatOptions) { final int childrenOffset, final FormatOptions formatOptions) {
byte flags = 0; return (byte) makeCharGroupFlags(group.mChars.length > 1, group.mFrequency >= 0,
if (group.mChars.length > 1) flags |= FormatSpec.FLAG_HAS_MULTIPLE_CHARS; getByteSize(childrenOffset), group.mShortcutTargets != null, group.mBigrams != null,
if (group.mFrequency >= 0) { group.mIsNotAWord, group.mIsBlacklistEntry, formatOptions);
flags |= FormatSpec.FLAG_IS_TERMINAL;
}
if (null != group.mChildren) {
final int byteSize = formatOptions.mSupportsDynamicUpdate
? FormatSpec.SIGNED_CHILDREN_ADDRESS_SIZE : getByteSize(childrenOffset);
switch (byteSize) {
case 1:
flags |= FormatSpec.FLAG_GROUP_ADDRESS_TYPE_ONEBYTE;
break;
case 2:
flags |= FormatSpec.FLAG_GROUP_ADDRESS_TYPE_TWOBYTES;
break;
case 3:
flags |= FormatSpec.FLAG_GROUP_ADDRESS_TYPE_THREEBYTES;
break;
default:
throw new RuntimeException("Node with a strange address");
}
} else if (formatOptions.mSupportsDynamicUpdate) {
flags |= FormatSpec.FLAG_GROUP_ADDRESS_TYPE_THREEBYTES;
}
if (null != group.mShortcutTargets) {
if (DBG && 0 == group.mShortcutTargets.size()) {
throw new RuntimeException("0-sized shortcut list must be null");
}
flags |= FormatSpec.FLAG_HAS_SHORTCUT_TARGETS;
}
if (null != group.mBigrams) {
if (DBG && 0 == group.mBigrams.size()) {
throw new RuntimeException("0-sized bigram list must be null");
}
flags |= FormatSpec.FLAG_HAS_BIGRAMS;
}
if (group.mIsNotAWord) {
flags |= FormatSpec.FLAG_IS_NOT_A_WORD;
}
if (group.mIsBlacklistEntry) {
flags |= FormatSpec.FLAG_IS_BLACKLISTED;
}
return flags;
} }
/** /**

View File

@ -59,8 +59,9 @@ public final class FormatSpec {
* l | 10 = 2 bytes : FLAG_GROUP_ADDRESS_TYPE_TWOBYTES * l | 10 = 2 bytes : FLAG_GROUP_ADDRESS_TYPE_TWOBYTES
* a | 11 = 3 bytes : FLAG_GROUP_ADDRESS_TYPE_THREEBYTES * a | 11 = 3 bytes : FLAG_GROUP_ADDRESS_TYPE_THREEBYTES
* g | ELSE * g | ELSE
* s | is moved ? 2 bits, 11 = no * s | is moved ? 2 bits, 11 = no : FLAG_IS_NOT_MOVED
* | 01 = yes * | 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 * | the new address is stored in the same place as the parent address
* | has several chars ? 1 bit, 1 = yes, 0 = no : FLAG_HAS_MULTIPLE_CHARS * | 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 a terminal ? 1 bit, 1 = yes, 0 = no : FLAG_IS_TERMINAL
@ -170,6 +171,7 @@ public final class FormatSpec {
static final int PARENT_ADDRESS_SIZE = 3; static final int PARENT_ADDRESS_SIZE = 3;
static final int FORWARD_LINK_ADDRESS_SIZE = 3; static final int FORWARD_LINK_ADDRESS_SIZE = 3;
// These flags are used only in the static dictionary.
static final int MASK_GROUP_ADDRESS_TYPE = 0xC0; static final int MASK_GROUP_ADDRESS_TYPE = 0xC0;
static final int FLAG_GROUP_ADDRESS_TYPE_NOADDRESS = 0x00; static final int FLAG_GROUP_ADDRESS_TYPE_NOADDRESS = 0x00;
static final int FLAG_GROUP_ADDRESS_TYPE_ONEBYTE = 0x40; static final int FLAG_GROUP_ADDRESS_TYPE_ONEBYTE = 0x40;
@ -183,7 +185,11 @@ public final class FormatSpec {
static final int FLAG_HAS_BIGRAMS = 0x04; static final int FLAG_HAS_BIGRAMS = 0x04;
static final int FLAG_IS_NOT_A_WORD = 0x02; static final int FLAG_IS_NOT_A_WORD = 0x02;
static final int FLAG_IS_BLACKLISTED = 0x01; static final int FLAG_IS_BLACKLISTED = 0x01;
static final int FLAG_IS_MOVED = 0x40;
// These flags are used only in the dynamic dictionary.
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_ATTRIBUTE_HAS_NEXT = 0x80; static final int FLAG_ATTRIBUTE_HAS_NEXT = 0x80;
static final int FLAG_ATTRIBUTE_OFFSET_NEGATIVE = 0x40; static final int FLAG_ATTRIBUTE_OFFSET_NEGATIVE = 0x40;