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_WRITING_UTILS_H
18#define LATINIME_DYNAMIC_PT_WRITING_UTILS_H
19
20#include <cstddef>
21
22#include "defines.h"
23#include "dictionary/structure/pt_common/dynamic_pt_reading_utils.h"
24
25namespace latinime {
26
27class BufferWithExtendableBuffer;
28
29class DynamicPtWritingUtils {
30 public:
31    static const int NODE_FLAG_FIELD_SIZE;
32
33    static bool writeEmptyDictionary(BufferWithExtendableBuffer *const buffer, const int rootPos);
34
35    static bool writeForwardLinkPositionAndAdvancePosition(
36            BufferWithExtendableBuffer *const buffer, const int forwardLinkPos,
37            int *const forwardLinkFieldPos);
38
39    static bool writePtNodeArraySizeAndAdvancePosition(BufferWithExtendableBuffer *const buffer,
40            const size_t arraySize, int *const arraySizeFieldPos);
41
42    static bool writeFlags(BufferWithExtendableBuffer *const buffer,
43            const DynamicPtReadingUtils::NodeFlags nodeFlags,
44            const int nodeFlagsFieldPos) {
45        int writingPos = nodeFlagsFieldPos;
46        return writeFlagsAndAdvancePosition(buffer, nodeFlags, &writingPos);
47    }
48
49    static bool writeFlagsAndAdvancePosition(BufferWithExtendableBuffer *const buffer,
50            const DynamicPtReadingUtils::NodeFlags nodeFlags,
51            int *const nodeFlagsFieldPos);
52
53    static bool writeParentPosOffsetAndAdvancePosition(BufferWithExtendableBuffer *const buffer,
54            const int parentPosition, const int basePos, int *const parentPosFieldPos);
55
56    static bool writeCodePointsAndAdvancePosition(BufferWithExtendableBuffer *const buffer,
57            const int *const codePoints, const int codePointCount, int *const codePointFieldPos);
58
59    static bool writeChildrenPositionAndAdvancePosition(BufferWithExtendableBuffer *const buffer,
60            const int childrenPosition, int *const childrenPositionFieldPos);
61
62 private:
63    DISALLOW_IMPLICIT_CONSTRUCTORS(DynamicPtWritingUtils);
64
65    static const size_t MAX_PTNODE_ARRAY_SIZE_TO_USE_SMALL_SIZE_FIELD;
66    static const size_t MAX_PTNODE_ARRAY_SIZE;
67    static const int SMALL_PTNODE_ARRAY_SIZE_FIELD_SIZE;
68    static const int LARGE_PTNODE_ARRAY_SIZE_FIELD_SIZE;
69    static const int LARGE_PTNODE_ARRAY_SIZE_FIELD_SIZE_FLAG;
70    static const int DICT_OFFSET_FIELD_SIZE;
71    static const int MAX_DICT_OFFSET_VALUE;
72    static const int MIN_DICT_OFFSET_VALUE;
73    static const int DICT_OFFSET_NEGATIVE_FLAG;
74
75    static bool writeDictOffset(BufferWithExtendableBuffer *const buffer, const int targetPos,
76            const int basePos, int *const offsetFieldPos);
77};
78} // namespace latinime
79#endif /* LATINIME_DYNAMIC_PT_WRITING_UTILS_H */
80