1 /*
2  * Copyright (C) 2013 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 #ifndef LATINIME_SHORTCUT_LIST_READING_UTILS_H
18 #define LATINIME_SHORTCUT_LIST_READING_UTILS_H
19 
20 #include <cstdint>
21 
22 #include "defines.h"
23 #include "utils/byte_array_view.h"
24 
25 namespace latinime {
26 
27 class ShortcutListReadingUtils {
28  public:
29     typedef uint8_t ShortcutFlags;
30 
31     static ShortcutFlags getFlagsAndForwardPointer(const ReadOnlyByteArrayView buffer,
32             int *const pos);
33 
getProbabilityFromFlags(const ShortcutFlags flags)34     static AK_FORCE_INLINE int getProbabilityFromFlags(const ShortcutFlags flags) {
35         return flags & MASK_ATTRIBUTE_PROBABILITY;
36     }
37 
hasNext(const ShortcutFlags flags)38     static AK_FORCE_INLINE bool hasNext(const ShortcutFlags flags) {
39         return (flags & FLAG_ATTRIBUTE_HAS_NEXT) != 0;
40     }
41 
42     // This method returns the size of the shortcut list region excluding the shortcut list size
43     // field at the beginning.
44     static int getShortcutListSizeAndForwardPointer(const ReadOnlyByteArrayView buffer,
45             int *const pos);
46 
getShortcutListSizeFieldSize()47     static AK_FORCE_INLINE int getShortcutListSizeFieldSize() {
48         return SHORTCUT_LIST_SIZE_FIELD_SIZE;
49     }
50 
skipShortcuts(const ReadOnlyByteArrayView buffer,int * const pos)51     static AK_FORCE_INLINE void skipShortcuts(const ReadOnlyByteArrayView buffer, int *const pos) {
52         const int shortcutListSize = getShortcutListSizeAndForwardPointer(buffer, pos);
53         *pos += shortcutListSize;
54     }
55 
isWhitelist(const ShortcutFlags flags)56     static AK_FORCE_INLINE bool isWhitelist(const ShortcutFlags flags) {
57         return getProbabilityFromFlags(flags) == WHITELIST_SHORTCUT_PROBABILITY;
58     }
59 
60     static int readShortcutTarget(const ReadOnlyByteArrayView buffer, const int maxLength,
61             int *const outWord, int *const pos);
62 
63  private:
64     DISALLOW_IMPLICIT_CONSTRUCTORS(ShortcutListReadingUtils);
65 
66     static const ShortcutFlags FLAG_ATTRIBUTE_HAS_NEXT;
67     static const ShortcutFlags MASK_ATTRIBUTE_PROBABILITY;
68     static const int SHORTCUT_LIST_SIZE_FIELD_SIZE;
69     static const int WHITELIST_SHORTCUT_PROBABILITY;
70 };
71 } // namespace latinime
72 #endif // LATINIME_SHORTCUT_LIST_READING_UTILS_H
73