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_BIGRAM_LIST_READ_WRITE_UTILS_H
18 #define LATINIME_BIGRAM_LIST_READ_WRITE_UTILS_H
19 
20 #include <cstdint>
21 #include <cstdlib>
22 
23 #include "defines.h"
24 #include "utils/byte_array_view.h"
25 
26 namespace latinime {
27 
28 class BufferWithExtendableBuffer;
29 
30 class BigramListReadWriteUtils {
31 public:
32    typedef uint8_t BigramFlags;
33 
34    static bool getBigramEntryPropertiesAndAdvancePosition(const ReadOnlyByteArrayView buffer,
35            BigramFlags *const outBigramFlags, int *const outTargetPtNodePos,
36            int *const bigramEntryPos);
37 
getProbabilityFromFlags(const BigramFlags flags)38    static AK_FORCE_INLINE int getProbabilityFromFlags(const BigramFlags flags) {
39        return flags & MASK_ATTRIBUTE_PROBABILITY;
40    }
41 
hasNext(const BigramFlags flags)42    static AK_FORCE_INLINE bool hasNext(const BigramFlags flags) {
43        return (flags & FLAG_ATTRIBUTE_HAS_NEXT) != 0;
44    }
45 
46    // Bigrams reading methods
47    static bool skipExistingBigrams(const ReadOnlyByteArrayView buffer, int *const bigramListPos);
48 
49 private:
50    DISALLOW_IMPLICIT_CONSTRUCTORS(BigramListReadWriteUtils);
51 
52    static const BigramFlags MASK_ATTRIBUTE_ADDRESS_TYPE;
53    static const BigramFlags FLAG_ATTRIBUTE_ADDRESS_TYPE_ONEBYTE;
54    static const BigramFlags FLAG_ATTRIBUTE_ADDRESS_TYPE_TWOBYTES;
55    static const BigramFlags FLAG_ATTRIBUTE_ADDRESS_TYPE_THREEBYTES;
56    static const BigramFlags FLAG_ATTRIBUTE_OFFSET_NEGATIVE;
57    static const BigramFlags FLAG_ATTRIBUTE_HAS_NEXT;
58    static const BigramFlags MASK_ATTRIBUTE_PROBABILITY;
59 
isOffsetNegative(const BigramFlags flags)60    static AK_FORCE_INLINE bool isOffsetNegative(const BigramFlags flags) {
61        return (flags & FLAG_ATTRIBUTE_OFFSET_NEGATIVE) != 0;
62    }
63 
64    static int getBigramAddressAndAdvancePosition(const ReadOnlyByteArrayView buffer,
65            const BigramFlags flags, int *const pos);
66 };
67 } // namespace latinime
68 #endif // LATINIME_BIGRAM_LIST_READ_WRITE_UTILS_H
69