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

Commit fd10db04 authored by Keisuke Kuroyanagi's avatar Keisuke Kuroyanagi
Browse files

Move shortcut iteration methods to poilcy.

Bug: 6669677
Change-Id: I6e535ce7962de8e153a52fd1085fe961649ffe16
parent 1b7c1cd5
Loading
Loading
Loading
Loading
+8 −12
Original line number Diff line number Diff line
@@ -20,7 +20,6 @@
#include <stdint.h>

#include "defines.h"
#include "suggest/core/dictionary/binary_dictionary_info.h"
#include "suggest/core/dictionary/byte_array_utils.h"

namespace latinime {
@@ -47,16 +46,14 @@ class BinaryDictionaryTerminalAttributesReadingUtils {
    // This method returns the size of the shortcut list region excluding the shortcut list size
    // field at the beginning.
    static AK_FORCE_INLINE int getShortcutListSizeAndForwardPointer(
            const BinaryDictionaryInfo *const binaryDictionaryInfo, int *const pos) {
            const uint8_t *const dictRoot, int *const pos) {
        // readUint16andAdvancePosition() returns an offset *including* the uint16 field itself.
        return ByteArrayUtils::readUint16AndAdvancePosition(
                binaryDictionaryInfo->getDictRoot(), pos) - SHORTCUT_LIST_SIZE_FIELD_SIZE;
        return ByteArrayUtils::readUint16AndAdvancePosition(dictRoot, pos)
                - SHORTCUT_LIST_SIZE_FIELD_SIZE;
    }

    static AK_FORCE_INLINE void skipShortcuts(
            const BinaryDictionaryInfo *const binaryDictionaryInfo, int *const pos) {
        const int shortcutListSize = getShortcutListSizeAndForwardPointer(
                binaryDictionaryInfo, pos);
    static AK_FORCE_INLINE void skipShortcuts(const uint8_t *const dictRoot, int *const pos) {
        const int shortcutListSize = getShortcutListSizeAndForwardPointer(dictRoot, pos);
        *pos += shortcutListSize;
    }

@@ -65,10 +62,9 @@ class BinaryDictionaryTerminalAttributesReadingUtils {
    }

    static AK_FORCE_INLINE int readShortcutTarget(
            const BinaryDictionaryInfo *const binaryDictionaryInfo, const int maxLength,
            int *const outWord, int *const pos) {
        return ByteArrayUtils::readStringAndAdvancePosition(
                binaryDictionaryInfo->getDictRoot(), maxLength, outWord, pos);
            const uint8_t *const dictRoot, const int maxLength,  int *const outWord,
            int *const pos) {
        return ByteArrayUtils::readStringAndAdvancePosition(dictRoot, maxLength, outWord, pos);
    }

 private:
+12 −25
Original line number Diff line number Diff line
@@ -19,8 +19,7 @@

#include <stdint.h>

#include "suggest/core/dictionary/binary_dictionary_info.h"
#include "suggest/core/dictionary/binary_dictionary_terminal_attributes_reading_utils.h"
#include "suggest/core/policy/dictionary_shortcuts_structure_policy.h"

namespace latinime {

@@ -33,9 +32,9 @@ class TerminalAttributes {
 public:
    class ShortcutIterator {
     public:
        ShortcutIterator(const BinaryDictionaryInfo *const binaryDictionaryInfo,
        ShortcutIterator(const DictionaryShortcutsStructurePolicy *const shortcutStructurePolicy,
                const int shortcutPos, const bool hasShortcutList)
                : mBinaryDictionaryInfo(binaryDictionaryInfo), mPos(shortcutPos),
                : mShortcutStructurePolicy(shortcutStructurePolicy), mPos(shortcutPos),
                  mHasNextShortcutTarget(hasShortcutList) {}

        inline bool hasNextShortcutTarget() const {
@@ -47,46 +46,34 @@ class TerminalAttributes {
        AK_FORCE_INLINE void nextShortcutTarget(
                const int maxDepth, int *const outTarget, int *const outTargetLength,
                bool *const outIsWhitelist) {
            const BinaryDictionaryTerminalAttributesReadingUtils::ShortcutFlags flags =
                    BinaryDictionaryTerminalAttributesReadingUtils::getFlagsAndForwardPointer(
                            mBinaryDictionaryInfo->getDictRoot(), &mPos);
            mHasNextShortcutTarget =
                    BinaryDictionaryTerminalAttributesReadingUtils::hasNext(flags);
            if (outIsWhitelist) {
                *outIsWhitelist =
                        BinaryDictionaryTerminalAttributesReadingUtils::isWhitelist(flags);
            }
            if (outTargetLength) {
                *outTargetLength =
                        BinaryDictionaryTerminalAttributesReadingUtils::readShortcutTarget(
                                mBinaryDictionaryInfo, maxDepth, outTarget, &mPos);
            }
            mShortcutStructurePolicy->getNextShortcut(maxDepth, outTarget, outTargetLength,
                    outIsWhitelist, &mHasNextShortcutTarget, &mPos);
        }

     private:
        const BinaryDictionaryInfo *const mBinaryDictionaryInfo;
        const DictionaryShortcutsStructurePolicy *const mShortcutStructurePolicy;
        int mPos;
        bool mHasNextShortcutTarget;
    };

    TerminalAttributes(const BinaryDictionaryInfo *const binaryDictionaryInfo,
    TerminalAttributes(const DictionaryShortcutsStructurePolicy *const shortcutStructurePolicy,
            const int shortcutPos)
            : mBinaryDictionaryInfo(binaryDictionaryInfo), mShortcutListSizePos(shortcutPos) {}
            : mShortcutStructurePolicy(shortcutStructurePolicy),
              mShortcutListSizePos(shortcutPos) {}

    inline ShortcutIterator getShortcutIterator() const {
        int shortcutPos = mShortcutListSizePos;
        const bool hasShortcutList = shortcutPos != NOT_A_DICT_POS;
        if (hasShortcutList) {
            BinaryDictionaryTerminalAttributesReadingUtils::getShortcutListSizeAndForwardPointer(
                    mBinaryDictionaryInfo, &shortcutPos);
            shortcutPos = mShortcutStructurePolicy->getStartPos(shortcutPos);
        }
        // shortcutPos is never used if hasShortcutList is false.
        return ShortcutIterator(mBinaryDictionaryInfo, shortcutPos, hasShortcutList);
        return ShortcutIterator(mShortcutStructurePolicy, shortcutPos, hasShortcutList);
    }

 private:
    DISALLOW_IMPLICIT_CONSTRUCTORS(TerminalAttributes);
    const BinaryDictionaryInfo *const mBinaryDictionaryInfo;
    const DictionaryShortcutsStructurePolicy *const mShortcutStructurePolicy;
    const int mShortcutListSizePos;
};
} // namespace latinime
+46 −0
Original line number Diff line number Diff line
/*
 * 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
 *
 *     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.
 */

#ifndef LATINIME_DICTIONARY_SHORTCUTS_STRUCTURE_POLICY_H
#define LATINIME_DICTIONARY_SHORTCUTS_STRUCTURE_POLICY_H

#include "defines.h"

namespace latinime {

/*
 * This class abstracts structure of shortcuts.
 */
class DictionaryShortcutsStructurePolicy {
 public:
    virtual ~DictionaryShortcutsStructurePolicy() {}

    virtual int getStartPos(const int pos) const = 0;

    virtual void getNextShortcut(const int maxCodePointCount, int *const outCodePoint,
            int *const outCodePointCount, bool *const outIsWhitelist, bool *const outHasNext,
            int *const pos) const = 0;

    virtual void skipAllShortcuts(int *const pos) const = 0;

 protected:
    DictionaryShortcutsStructurePolicy() {}

 private:
    DISALLOW_COPY_AND_ASSIGN(DictionaryShortcutsStructurePolicy);
};
} // namespace latinime
#endif /* LATINIME_DICTIONARY_SHORTCUTS_STRUCTURE_POLICY_H */
+3 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ namespace latinime {
class DicNode;
class DicNodeVector;
class DictionaryBigramsStructurePolicy;
class DictionaryShortcutsStructurePolicy;

/*
 * This class abstracts structure of dictionaries.
@@ -66,6 +67,8 @@ class DictionaryStructureWithBufferPolicy {

    virtual const DictionaryBigramsStructurePolicy *getBigramsStructurePolicy() const = 0;

    virtual const DictionaryShortcutsStructurePolicy *getShortcutsStructurePolicy() const = 0;

 protected:
    DictionaryStructureWithBufferPolicy() {}

+6 −5
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@
#include "suggest/core/dicnode/dic_node.h"
#include "suggest/core/dicnode/dic_node_priority_queue.h"
#include "suggest/core/dicnode/dic_node_vector.h"
// TODO: Use DictionaryStructurePolicy instead of BinaryDictionaryInfo.
#include "suggest/core/dictionary/binary_dictionary_info.h"
#include "suggest/core/dictionary/dictionary.h"
#include "suggest/core/dictionary/digraph_utils.h"
@@ -211,11 +212,11 @@ int Suggest::outputSuggestions(DicTraverseSession *traverseSession, int *frequen
        }

        if (!terminalDicNode->hasMultipleWords()) {
            const BinaryDictionaryInfo *const binaryDictionaryInfo =
                    traverseSession->getBinaryDictionaryInfo();
            const TerminalAttributes terminalAttributes(traverseSession->getBinaryDictionaryInfo(),
                    binaryDictionaryInfo->getStructurePolicy()->getShortcutPositionOfNode(
                            terminalDicNode->getPos()));
            const DictionaryStructureWithBufferPolicy *const structurePolicy =
                    traverseSession->getBinaryDictionaryInfo()->getStructurePolicy();
            const TerminalAttributes terminalAttributes(
                    structurePolicy->getShortcutsStructurePolicy(),
                    structurePolicy->getShortcutPositionOfNode(terminalDicNode->getPos()));
            // Shortcut is not supported for multiple words suggestions.
            // TODO: Check shortcuts during traversal for multiple words suggestions.
            const bool sameAsTyped = TRAVERSAL->sameAsTyped(traverseSession, terminalDicNode);
Loading