dic_traverse_session.cpp revision b68e73448104714e8f12f89a1e00fb10b5fd14c4
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "suggest/core/session/dic_traverse_session.h"
18
19#include "defines.h"
20#include "dictionary.h"
21#include "dic_traverse_wrapper.h"
22#include "jni.h"
23#include "suggest/core/dicnode/dic_node_utils.h"
24
25namespace latinime {
26
27const int DicTraverseSession::CACHE_START_INPUT_LENGTH_THRESHOLD = 20;
28
29// A factory method for DicTraverseSession
30static void *getSessionInstance(JNIEnv *env, jstring localeStr) {
31    return new DicTraverseSession(env, localeStr);
32}
33
34// TODO: Pass "DicTraverseSession *traverseSession" when the source code structure settles down.
35static void initSessionInstance(void *traverseSession, const Dictionary *const dictionary,
36        const int *prevWord, const int prevWordLength) {
37    if (traverseSession) {
38        DicTraverseSession *tSession = static_cast<DicTraverseSession *>(traverseSession);
39        tSession->init(dictionary, prevWord, prevWordLength);
40    }
41}
42
43// TODO: Pass "DicTraverseSession *traverseSession" when the source code structure settles down.
44static void releaseSessionInstance(void *traverseSession) {
45    delete static_cast<DicTraverseSession *>(traverseSession);
46}
47
48// An ad-hoc internal class to register the factory method defined above
49class TraverseSessionFactoryRegisterer {
50 public:
51    TraverseSessionFactoryRegisterer() {
52        DicTraverseWrapper::setTraverseSessionFactoryMethod(getSessionInstance);
53        DicTraverseWrapper::setTraverseSessionInitMethod(initSessionInstance);
54        DicTraverseWrapper::setTraverseSessionReleaseMethod(releaseSessionInstance);
55    }
56 private:
57    DISALLOW_COPY_AND_ASSIGN(TraverseSessionFactoryRegisterer);
58};
59
60// To invoke the TraverseSessionFactoryRegisterer constructor in the global constructor.
61static TraverseSessionFactoryRegisterer traverseSessionFactoryRegisterer;
62
63void DicTraverseSession::init(const Dictionary *const dictionary, const int *prevWord,
64        int prevWordLength) {
65    mDictionary = dictionary;
66    if (!prevWord) {
67        mPrevWordPos = NOT_VALID_WORD;
68        return;
69    }
70    mPrevWordPos = DicNodeUtils::getWordPos(dictionary->getOffsetDict(), prevWord, prevWordLength);
71}
72
73void DicTraverseSession::setupForGetSuggestions(const ProximityInfo *pInfo,
74        const int *inputCodePoints, const int inputSize, const int *const inputXs,
75        const int *const inputYs, const int *const times, const int *const pointerIds,
76        const float maxSpatialDistance, const int maxPointerCount) {
77    mProximityInfo = pInfo;
78    mMaxPointerCount = maxPointerCount;
79    initializeProximityInfoStates(inputCodePoints, inputXs, inputYs, times, pointerIds, inputSize,
80            maxSpatialDistance, maxPointerCount);
81}
82
83const uint8_t *DicTraverseSession::getOffsetDict() const {
84    return mDictionary->getOffsetDict();
85}
86
87void DicTraverseSession::resetCache(const int nextActiveCacheSize, const int maxWords) {
88    mDicNodesCache.reset(nextActiveCacheSize, maxWords);
89    mBigramCacheMap.clear();
90    mPartiallyCommited = false;
91}
92
93void DicTraverseSession::initializeProximityInfoStates(const int *const inputCodePoints,
94        const int *const inputXs, const int *const inputYs, const int *const times,
95        const int *const pointerIds, const int inputSize, const float maxSpatialDistance,
96        const int maxPointerCount) {
97    ASSERT(1 <= maxPointerCount && maxPointerCount <= MAX_POINTER_COUNT_G);
98    mInputSize = 0;
99    for (int i = 0; i < maxPointerCount; ++i) {
100        mProximityInfoStates[i].initInputParams(i, maxSpatialDistance, getProximityInfo(),
101                inputCodePoints, inputSize, inputXs, inputYs, times, pointerIds,
102                maxPointerCount == MAX_POINTER_COUNT_G
103                /* TODO: this is a hack. fix proximity info state */);
104        mInputSize += mProximityInfoStates[i].size();
105    }
106}
107} // namespace latinime
108