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 "words_priority_queue.h" 18 19namespace latinime { 20 21int WordsPriorityQueue::outputSuggestions(const int *before, const int beforeLength, 22 int *frequencies, int *outputCodePoints, int* outputTypes) { 23 mHighestSuggestedWord = 0; 24 const int size = min(MAX_WORDS, static_cast<int>(mSuggestions.size())); 25 SuggestedWord *swBuffer[size]; 26 int index = size - 1; 27 while (!mSuggestions.empty() && index >= 0) { 28 SuggestedWord *sw = mSuggestions.top(); 29 if (DEBUG_WORDS_PRIORITY_QUEUE) { 30 AKLOGI("dump word. %d", sw->mScore); 31 DUMP_WORD(sw->mWord, sw->mWordLength); 32 } 33 swBuffer[index] = sw; 34 mSuggestions.pop(); 35 --index; 36 } 37 if (size >= 2) { 38 SuggestedWord *nsMaxSw = 0; 39 int maxIndex = 0; 40 float maxNs = 0; 41 for (int i = 0; i < size; ++i) { 42 SuggestedWord *tempSw = swBuffer[i]; 43 if (!tempSw) { 44 continue; 45 } 46 const float tempNs = getNormalizedScore(tempSw, before, beforeLength, 0, 0, 0); 47 if (tempNs >= maxNs) { 48 maxNs = tempNs; 49 maxIndex = i; 50 nsMaxSw = tempSw; 51 } 52 } 53 if (maxIndex > 0 && nsMaxSw) { 54 memmove(&swBuffer[1], &swBuffer[0], maxIndex * sizeof(swBuffer[0])); 55 swBuffer[0] = nsMaxSw; 56 } 57 } 58 for (int i = 0; i < size; ++i) { 59 SuggestedWord *sw = swBuffer[i]; 60 if (!sw) { 61 AKLOGE("SuggestedWord is null %d", i); 62 continue; 63 } 64 const int wordLength = sw->mWordLength; 65 int *targetAddress = outputCodePoints + i * MAX_WORD_LENGTH; 66 frequencies[i] = sw->mScore; 67 outputTypes[i] = sw->mType; 68 memcpy(targetAddress, sw->mWord, wordLength * sizeof(targetAddress[0])); 69 if (wordLength < MAX_WORD_LENGTH) { 70 targetAddress[wordLength] = 0; 71 } 72 sw->mUsed = false; 73 } 74 return size; 75} 76} // namespace latinime 77