From 9c2a96aa6cb6d8c1f7a559dbd7051302cfc6150b Mon Sep 17 00:00:00 2001
From: Jean Chalard <jchalard@google.com>
Date: Tue, 17 Apr 2012 11:38:44 +0900
Subject: [PATCH] Preparatory refactoring

Split out getting the pointer to the bigrams to a separate
function. This is a preparative change to bug#6313806

Change-Id: Ieb2e306a1151cd95dc1a16793c8dc2f7fed8b654
---
 native/jni/src/bigram_dictionary.cpp | 32 +++++++++++++++++-----------
 native/jni/src/bigram_dictionary.h   |  4 ++++
 2 files changed, 23 insertions(+), 13 deletions(-)

diff --git a/native/jni/src/bigram_dictionary.cpp b/native/jni/src/bigram_dictionary.cpp
index 926a0d44e9..87ca1e9edc 100644
--- a/native/jni/src/bigram_dictionary.cpp
+++ b/native/jni/src/bigram_dictionary.cpp
@@ -108,19 +108,7 @@ int BigramDictionary::getBigrams(unsigned short *prevWord, int prevWordLength, i
     mMaxBigrams = maxBigrams;
 
     const uint8_t* const root = DICT;
-    int pos = BinaryFormat::getTerminalPosition(root, prevWord, prevWordLength);
-
-    if (NOT_VALID_WORD == pos) return 0;
-    const int flags = BinaryFormat::getFlagsAndForwardPointer(root, &pos);
-    if (0 == (flags & UnigramDictionary::FLAG_HAS_BIGRAMS)) return 0;
-    if (0 == (flags & UnigramDictionary::FLAG_HAS_MULTIPLE_CHARS)) {
-        BinaryFormat::getCharCodeAndForwardPointer(root, &pos);
-    } else {
-        pos = BinaryFormat::skipOtherCharacters(root, pos);
-    }
-    pos = BinaryFormat::skipChildrenPosition(flags, pos);
-    pos = BinaryFormat::skipFrequency(flags, pos);
-    pos = BinaryFormat::skipShortcuts(root, flags, pos);
+    int pos = getBigramListForWord(root, prevWord, prevWordLength);
     int bigramFlags;
     int bigramCount = 0;
     do {
@@ -142,6 +130,24 @@ int BigramDictionary::getBigrams(unsigned short *prevWord, int prevWordLength, i
     return bigramCount;
 }
 
+int BigramDictionary::getBigramListForWord(const uint8_t* const root,
+        const unsigned short *prevWord, const int prevWordLength) {
+    int pos = BinaryFormat::getTerminalPosition(root, prevWord, prevWordLength);
+
+    if (NOT_VALID_WORD == pos) return 0;
+    const int flags = BinaryFormat::getFlagsAndForwardPointer(root, &pos);
+    if (0 == (flags & UnigramDictionary::FLAG_HAS_BIGRAMS)) return 0;
+    if (0 == (flags & UnigramDictionary::FLAG_HAS_MULTIPLE_CHARS)) {
+        BinaryFormat::getCharCodeAndForwardPointer(root, &pos);
+    } else {
+        pos = BinaryFormat::skipOtherCharacters(root, pos);
+    }
+    pos = BinaryFormat::skipChildrenPosition(flags, pos);
+    pos = BinaryFormat::skipFrequency(flags, pos);
+    pos = BinaryFormat::skipShortcuts(root, flags, pos);
+    return pos;
+}
+
 bool BigramDictionary::checkFirstCharacter(unsigned short *word) {
     // Checks whether this word starts with same character or neighboring characters of
     // what user typed.
diff --git a/native/jni/src/bigram_dictionary.h b/native/jni/src/bigram_dictionary.h
index af89e32553..1612131c46 100644
--- a/native/jni/src/bigram_dictionary.h
+++ b/native/jni/src/bigram_dictionary.h
@@ -17,6 +17,8 @@
 #ifndef LATINIME_BIGRAM_DICTIONARY_H
 #define LATINIME_BIGRAM_DICTIONARY_H
 
+#include <stdint.h>
+
 namespace latinime {
 
 class Dictionary;
@@ -25,6 +27,8 @@ class BigramDictionary {
     BigramDictionary(const unsigned char *dict, int maxWordLength, Dictionary *parentDictionary);
     int getBigrams(unsigned short *word, int length, int *codes, int codesSize,
             unsigned short *outWords, int *frequencies, int maxWordLength, int maxBigrams);
+    int getBigramListForWord(const uint8_t* const root,
+        const unsigned short *prevWord, const int prevWordLength);
     ~BigramDictionary();
  private:
     bool addWordBigram(unsigned short *word, int length, int frequency);
-- 
GitLab