Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9e0bb2f0 authored by Keisuke Kuroyanagi's avatar Keisuke Kuroyanagi Committed by Android (Google) Code Review
Browse files

Merge "Move header reading methods to policyimpl."

parents f27297d2 76e579c7
Loading
Loading
Loading
Loading
+5 −13
Original line number Diff line number Diff line
@@ -16,23 +16,15 @@

#include "suggest/core/dictionary/binary_dictionary_header.h"

#include "defines.h"

namespace latinime {

const char *const BinaryDictionaryHeader::MULTIPLE_WORDS_DEMOTION_RATE_KEY =
const char *const HeaderPolicy::MULTIPLE_WORDS_DEMOTION_RATE_KEY =
        "MULTIPLE_WORDS_DEMOTION_RATE";
const float BinaryDictionaryHeader::DEFAULT_MULTI_WORD_COST_MULTIPLIER = 1.0f;
const float BinaryDictionaryHeader::MULTI_WORD_COST_MULTIPLIER_SCALE = 100.0f;

BinaryDictionaryHeader::BinaryDictionaryHeader(const uint8_t *const dictBuf)
        : mDictBuf(dictBuf),
          mDictionaryFlags(BinaryDictionaryHeaderReadingUtils::getFlags(mDictBuf)),
          mSize(BinaryDictionaryHeaderReadingUtils::getHeaderSize(mDictBuf)),
          mMultiWordCostMultiplier(readMultiWordCostMultiplier()) {}
const float HeaderPolicy::DEFAULT_MULTI_WORD_COST_MULTIPLIER = 1.0f;
const float HeaderPolicy::MULTI_WORD_COST_MULTIPLIER_SCALE = 100.0f;

float BinaryDictionaryHeader::readMultiWordCostMultiplier() const {
    const int headerValue = BinaryDictionaryHeaderReadingUtils::readHeaderValueInt(
float HeaderPolicy::readMultiWordCostMultiplier() const {
    const int headerValue = HeaderReadingUtils::readHeaderValueInt(
            mDictBuf, MULTIPLE_WORDS_DEMOTION_RATE_KEY);
    if (headerValue == S_INT_MIN) {
        // not found
+19 −16
Original line number Diff line number Diff line
@@ -14,39 +14,41 @@
 * limitations under the License.
 */

#ifndef LATINIME_BINARY_DICTIONARY_HEADER_H
#define LATINIME_BINARY_DICTIONARY_HEADER_H
#ifndef LATINIME_HEADER_POLICY_H
#define LATINIME_HEADER_POLICY_H

#include <stdint.h>

#include "defines.h"
#include "suggest/core/policy/dictionary_header_structure_policy.h"
#include "suggest/core/dictionary/binary_dictionary_header_reading_utils.h"

namespace latinime {

/**
 * This class abstracts dictionary header structures and provide interface to access dictionary
 * header information.
 */
// TODO:: Move header classes to policyimpl.
class BinaryDictionaryHeader {
// TODO: Move to policyimpl.
class HeaderPolicy : public DictionaryHeaderStructurePolicy {
 public:
    explicit BinaryDictionaryHeader(const uint8_t *const dictBuf);
    explicit HeaderPolicy(const uint8_t *const dictBuf)
            : mDictBuf(dictBuf), mDictionaryFlags(HeaderReadingUtils::getFlags(dictBuf)),
              mSize(HeaderReadingUtils::getHeaderSize(dictBuf)),
              mMultiWordCostMultiplier(readMultiWordCostMultiplier()) {}

    ~HeaderPolicy() {}

    AK_FORCE_INLINE int getSize() const {
        return mSize;
    }

    AK_FORCE_INLINE bool supportsDynamicUpdate() const {
        return BinaryDictionaryHeaderReadingUtils::supportsDynamicUpdate(mDictionaryFlags);
        return HeaderReadingUtils::supportsDynamicUpdate(mDictionaryFlags);
    }

    AK_FORCE_INLINE bool requiresGermanUmlautProcessing() const {
        return BinaryDictionaryHeaderReadingUtils::requiresGermanUmlautProcessing(mDictionaryFlags);
        return HeaderReadingUtils::requiresGermanUmlautProcessing(mDictionaryFlags);
    }

    AK_FORCE_INLINE bool requiresFrenchLigatureProcessing() const {
        return BinaryDictionaryHeaderReadingUtils::requiresFrenchLigatureProcessing(
        return HeaderReadingUtils::requiresFrenchLigatureProcessing(
                mDictionaryFlags);
    }

@@ -61,7 +63,7 @@ class BinaryDictionaryHeader {
            outValue[0] = '\0';
            return;
        }
        if (!BinaryDictionaryHeaderReadingUtils::readHeaderValue(mDictBuf,
        if (!HeaderReadingUtils::readHeaderValue(mDictBuf,
                key, outValue, outValueSize)) {
            outValue[0] = '?';
            outValue[1] = '\0';
@@ -69,18 +71,19 @@ class BinaryDictionaryHeader {
    }

 private:
    DISALLOW_COPY_AND_ASSIGN(BinaryDictionaryHeader);
    DISALLOW_IMPLICIT_CONSTRUCTORS(HeaderPolicy);

    static const char *const MULTIPLE_WORDS_DEMOTION_RATE_KEY;
    static const float DEFAULT_MULTI_WORD_COST_MULTIPLIER;
    static const float MULTI_WORD_COST_MULTIPLIER_SCALE;

    const uint8_t *const mDictBuf;
    const BinaryDictionaryHeaderReadingUtils::DictionaryFlags mDictionaryFlags;
    const HeaderReadingUtils::DictionaryFlags mDictionaryFlags;
    const int mSize;
    const float mMultiWordCostMultiplier;

    float readMultiWordCostMultiplier() const;
};

} // namespace latinime
#endif // LATINIME_BINARY_DICTIONARY_HEADER_H
#endif /* LATINIME_HEADER_POLICY_H */
+25 −24
Original line number Diff line number Diff line
@@ -24,39 +24,40 @@

namespace latinime {

const int BinaryDictionaryHeaderReadingUtils::MAX_OPTION_KEY_LENGTH = 256;
const int BinaryDictionaryHeaderReadingUtils::VERSION_2_HEADER_MAGIC_NUMBER_SIZE = 4;
const int BinaryDictionaryHeaderReadingUtils::VERSION_2_HEADER_DICTIONARY_VERSION_SIZE = 2;
const int BinaryDictionaryHeaderReadingUtils::VERSION_2_HEADER_FLAG_SIZE = 2;
const int BinaryDictionaryHeaderReadingUtils::VERSION_2_HEADER_SIZE_FIELD_SIZE = 4;
const int HeaderReadingUtils::MAX_OPTION_KEY_LENGTH = 256;

const BinaryDictionaryHeaderReadingUtils::DictionaryFlags
        BinaryDictionaryHeaderReadingUtils::NO_FLAGS = 0;
const int HeaderReadingUtils::HEADER_MAGIC_NUMBER_SIZE = 4;
const int HeaderReadingUtils::HEADER_DICTIONARY_VERSION_SIZE = 2;
const int HeaderReadingUtils::HEADER_FLAG_SIZE = 2;
const int HeaderReadingUtils::HEADER_SIZE_FIELD_SIZE = 4;

const HeaderReadingUtils::DictionaryFlags
        HeaderReadingUtils::NO_FLAGS = 0;
// Flags for special processing
// Those *must* match the flags in makedict (BinaryDictInputOutput#*_PROCESSING_FLAG) or
// something very bad (like, the apocalypse) will happen. Please update both at the same time.
const BinaryDictionaryHeaderReadingUtils::DictionaryFlags
        BinaryDictionaryHeaderReadingUtils::GERMAN_UMLAUT_PROCESSING_FLAG = 0x1;
const BinaryDictionaryHeaderReadingUtils::DictionaryFlags
        BinaryDictionaryHeaderReadingUtils::SUPPORTS_DYNAMIC_UPDATE_FLAG = 0x2;
const BinaryDictionaryHeaderReadingUtils::DictionaryFlags
        BinaryDictionaryHeaderReadingUtils::FRENCH_LIGATURE_PROCESSING_FLAG = 0x4;
const HeaderReadingUtils::DictionaryFlags
        HeaderReadingUtils::GERMAN_UMLAUT_PROCESSING_FLAG = 0x1;
const HeaderReadingUtils::DictionaryFlags
        HeaderReadingUtils::SUPPORTS_DYNAMIC_UPDATE_FLAG = 0x2;
const HeaderReadingUtils::DictionaryFlags
        HeaderReadingUtils::FRENCH_LIGATURE_PROCESSING_FLAG = 0x4;

/* static */ int BinaryDictionaryHeaderReadingUtils::getHeaderSize(const uint8_t *const dictBuf) {
/* static */ int HeaderReadingUtils::getHeaderSize(const uint8_t *const dictBuf) {
    // See the format of the header in the comment in
    // BinaryDictionaryFormatUtils::detectFormatVersion()
    return ByteArrayUtils::readUint32(dictBuf, VERSION_2_HEADER_MAGIC_NUMBER_SIZE
            + VERSION_2_HEADER_DICTIONARY_VERSION_SIZE + VERSION_2_HEADER_FLAG_SIZE);
    return ByteArrayUtils::readUint32(dictBuf, HEADER_MAGIC_NUMBER_SIZE
            + HEADER_DICTIONARY_VERSION_SIZE + HEADER_FLAG_SIZE);
}

/* static */ BinaryDictionaryHeaderReadingUtils::DictionaryFlags
        BinaryDictionaryHeaderReadingUtils::getFlags(const uint8_t *const dictBuf) {
    return ByteArrayUtils::readUint16(dictBuf, VERSION_2_HEADER_MAGIC_NUMBER_SIZE
            + VERSION_2_HEADER_DICTIONARY_VERSION_SIZE);
/* static */ HeaderReadingUtils::DictionaryFlags
        HeaderReadingUtils::getFlags(const uint8_t *const dictBuf) {
    return ByteArrayUtils::readUint16(dictBuf,
            HEADER_MAGIC_NUMBER_SIZE + HEADER_DICTIONARY_VERSION_SIZE);
}

// Returns if the key is found or not and reads the found value into outValue.
/* static */ bool BinaryDictionaryHeaderReadingUtils::readHeaderValue(const uint8_t *const dictBuf,
/* static */ bool HeaderReadingUtils::readHeaderValue(const uint8_t *const dictBuf,
        const char *const key, int *outValue, const int outValueSize) {
    if (outValueSize <= 0) {
        return false;
@@ -71,8 +72,8 @@ const BinaryDictionaryHeaderReadingUtils::DictionaryFlags
        if(ByteArrayUtils::compareStringInBufferWithCharArray(
                dictBuf, key, headerSize - pos, &pos) == 0) {
            // The key was found.
            const int length = ByteArrayUtils::readStringAndAdvancePosition(
                    dictBuf, outValueSize, outValue, &pos);
            const int length = ByteArrayUtils::readStringAndAdvancePosition(dictBuf, outValueSize,
                    outValue, &pos);
            // Add a 0 terminator to the string.
            outValue[length < outValueSize ? length : outValueSize - 1] = '\0';
            return true;
@@ -83,7 +84,7 @@ const BinaryDictionaryHeaderReadingUtils::DictionaryFlags
    return false;
}

/* static */ int BinaryDictionaryHeaderReadingUtils::readHeaderValueInt(
/* static */ int HeaderReadingUtils::readHeaderValueInt(
        const uint8_t *const dictBuf, const char *const key) {
    const int bufferSize = LARGEST_INT_DIGIT_COUNT;
    int intBuffer[bufferSize];
+12 −12
Original line number Diff line number Diff line
@@ -14,8 +14,8 @@
 * limitations under the License.
 */

#ifndef LATINIME_DICTIONARY_HEADER_READING_UTILS_H
#define LATINIME_DICTIONARY_HEADER_READING_UTILS_H
#ifndef LATINIME_HEADER_READING_UTILS_H
#define LATINIME_HEADER_READING_UTILS_H

#include <stdint.h>

@@ -23,8 +23,8 @@

namespace latinime {

// TODO:: Move header classes to policyimpl.
class BinaryDictionaryHeaderReadingUtils {
// TODO: Move to policyimpl.
class HeaderReadingUtils {
 public:
    typedef uint16_t DictionaryFlags;

@@ -47,8 +47,8 @@ class BinaryDictionaryHeaderReadingUtils {
    }

    static AK_FORCE_INLINE int getHeaderOptionsPosition() {
        return VERSION_2_HEADER_MAGIC_NUMBER_SIZE + VERSION_2_HEADER_DICTIONARY_VERSION_SIZE
                + VERSION_2_HEADER_FLAG_SIZE + VERSION_2_HEADER_SIZE_FIELD_SIZE;
        return HEADER_MAGIC_NUMBER_SIZE + HEADER_DICTIONARY_VERSION_SIZE + HEADER_FLAG_SIZE
                + HEADER_SIZE_FIELD_SIZE;
    }

    static bool readHeaderValue(const uint8_t *const dictBuf,
@@ -57,12 +57,12 @@ class BinaryDictionaryHeaderReadingUtils {
    static int readHeaderValueInt(const uint8_t *const dictBuf, const char *const key);

 private:
    DISALLOW_IMPLICIT_CONSTRUCTORS(BinaryDictionaryHeaderReadingUtils);
    DISALLOW_IMPLICIT_CONSTRUCTORS(HeaderReadingUtils);

    static const int VERSION_2_HEADER_MAGIC_NUMBER_SIZE;
    static const int VERSION_2_HEADER_DICTIONARY_VERSION_SIZE;
    static const int VERSION_2_HEADER_FLAG_SIZE;
    static const int VERSION_2_HEADER_SIZE_FIELD_SIZE;
    static const int HEADER_MAGIC_NUMBER_SIZE;
    static const int HEADER_DICTIONARY_VERSION_SIZE;
    static const int HEADER_FLAG_SIZE;
    static const int HEADER_SIZE_FIELD_SIZE;

    static const DictionaryFlags NO_FLAGS;
    // Flags for special processing
@@ -74,4 +74,4 @@ class BinaryDictionaryHeaderReadingUtils {
    static const DictionaryFlags CONTAINS_BIGRAMS_FLAG;
};
}
#endif /* LATINIME_DICTIONARY_HEADER_READING_UTILS_H */
#endif /* LATINIME_HEADER_READING_UTILS_H */
+2 −38
Original line number Diff line number Diff line
@@ -20,21 +20,15 @@
#include <stdint.h>

#include "defines.h"
#include "jni.h"
#include "suggest/core/dictionary/binary_dictionary_header.h"
#include "utils/log_utils.h"

namespace latinime {

class BinaryDictionaryInfo {
 public:
     AK_FORCE_INLINE BinaryDictionaryInfo(JNIEnv *env, const uint8_t *const dictBuf,
     AK_FORCE_INLINE BinaryDictionaryInfo(const uint8_t *const dictBuf,
            const int dictSize, const int mmapFd, const int dictBufOffset, const bool isUpdatable)
            : mDictBuf(dictBuf), mDictSize(dictSize), mMmapFd(mmapFd),
              mDictBufOffset(dictBufOffset), mIsUpdatable(isUpdatable),
              mDictionaryHeader(dictBuf) {
        logDictionaryInfo(env);
    }
              mDictBufOffset(dictBufOffset), mIsUpdatable(isUpdatable) {}

    ~BinaryDictionaryInfo() {}

@@ -68,36 +62,6 @@ class BinaryDictionaryInfo {
    const int mMmapFd;
    const int mDictBufOffset;
    const bool mIsUpdatable;
    // TODO: Move BinaryDictionaryHeader to policyimpl and introduce dedicated API to the
    // DictionaryStructureWithBufferPolicy.
    const BinaryDictionaryHeader mDictionaryHeader;

    AK_FORCE_INLINE void logDictionaryInfo(JNIEnv *const env) const {
        const int BUFFER_SIZE = 16;
        int dictionaryIdCodePointBuffer[BUFFER_SIZE];
        int versionStringCodePointBuffer[BUFFER_SIZE];
        int dateStringCodePointBuffer[BUFFER_SIZE];
        mDictionaryHeader.readHeaderValueOrQuestionMark("dictionary",
                dictionaryIdCodePointBuffer, BUFFER_SIZE);
        mDictionaryHeader.readHeaderValueOrQuestionMark("version",
                versionStringCodePointBuffer, BUFFER_SIZE);
        mDictionaryHeader.readHeaderValueOrQuestionMark("date",
                dateStringCodePointBuffer, BUFFER_SIZE);

        char dictionaryIdCharBuffer[BUFFER_SIZE];
        char versionStringCharBuffer[BUFFER_SIZE];
        char dateStringCharBuffer[BUFFER_SIZE];
        intArrayToCharArray(dictionaryIdCodePointBuffer, BUFFER_SIZE,
                dictionaryIdCharBuffer, BUFFER_SIZE);
        intArrayToCharArray(versionStringCodePointBuffer, BUFFER_SIZE,
                versionStringCharBuffer, BUFFER_SIZE);
        intArrayToCharArray(dateStringCodePointBuffer, BUFFER_SIZE,
                dateStringCharBuffer, BUFFER_SIZE);

        LogUtils::logToJava(env,
                "Dictionary info: dictionary = %s ; version = %s ; date = %s ; filesize = %i",
                dictionaryIdCharBuffer, versionStringCharBuffer, dateStringCharBuffer, mDictSize);
    }
};
}
#endif /* LATINIME_BINARY_DICTIONARY_INFO_H */
Loading