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_DYNAMIC_PT_READING_UTILS_H
18#define LATINIME_DYNAMIC_PT_READING_UTILS_H
19
20#include <cstdint>
21
22#include "defines.h"
23
24namespace latinime {
25
26class DynamicPtReadingUtils {
27 public:
28    typedef uint8_t NodeFlags;
29
30    static const int DICT_OFFSET_INVALID;
31    static const int DICT_OFFSET_ZERO_OFFSET;
32
33    static int getForwardLinkPosition(const uint8_t *const buffer, const int pos);
34
35    static AK_FORCE_INLINE bool isValidForwardLinkPosition(const int forwardLinkAddress) {
36        return forwardLinkAddress != 0;
37    }
38
39    static int getParentPtNodePosOffsetAndAdvancePosition(const uint8_t *const buffer,
40            int *const pos);
41
42    static int getParentPtNodePos(const int parentOffset, const int ptNodePos);
43
44    static int readChildrenPositionAndAdvancePosition(const uint8_t *const buffer, int *const pos);
45
46    /**
47     * Node Flags
48     */
49    static AK_FORCE_INLINE bool isMoved(const NodeFlags flags) {
50        return FLAG_IS_MOVED == (MASK_MOVED & flags);
51    }
52
53    static AK_FORCE_INLINE bool isDeleted(const NodeFlags flags) {
54        return FLAG_IS_DELETED == (MASK_MOVED & flags);
55    }
56
57    static AK_FORCE_INLINE bool willBecomeNonTerminal(const NodeFlags flags) {
58        return FLAG_WILL_BECOME_NON_TERMINAL == (MASK_MOVED & flags);
59    }
60
61    static AK_FORCE_INLINE NodeFlags updateAndGetFlags(const NodeFlags originalFlags,
62            const bool isMoved, const bool isDeleted, const bool willBecomeNonTerminal) {
63        NodeFlags flags = originalFlags;
64        flags = willBecomeNonTerminal ?
65                ((flags & (~MASK_MOVED)) | FLAG_WILL_BECOME_NON_TERMINAL) : flags;
66        flags = isMoved ? ((flags & (~MASK_MOVED)) | FLAG_IS_MOVED) : flags;
67        flags = isDeleted ? ((flags & (~MASK_MOVED)) | FLAG_IS_DELETED) : flags;
68        flags = (!isMoved && !isDeleted && !willBecomeNonTerminal) ?
69                ((flags & (~MASK_MOVED)) | FLAG_IS_NOT_MOVED) : flags;
70        return flags;
71    }
72
73 private:
74    DISALLOW_IMPLICIT_CONSTRUCTORS(DynamicPtReadingUtils);
75
76    static const NodeFlags MASK_MOVED;
77    static const NodeFlags FLAG_IS_NOT_MOVED;
78    static const NodeFlags FLAG_IS_MOVED;
79    static const NodeFlags FLAG_IS_DELETED;
80    static const NodeFlags FLAG_WILL_BECOME_NON_TERMINAL;
81};
82} // namespace latinime
83#endif /* LATINIME_DYNAMIC_PT_READING_UTILS_H */
84