1 /*
2 * Copyright (C) 2018 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 // Common definitions used in the grammar system.
18
19 #ifndef LIBTEXTCLASSIFIER_UTILS_GRAMMAR_TYPES_H_
20 #define LIBTEXTCLASSIFIER_UTILS_GRAMMAR_TYPES_H_
21
22 #include "utils/base/integral_types.h"
23 #include "utils/base/logging.h"
24
25 namespace libtextclassifier3::grammar {
26
27 // A nonterminal identifier.
28 typedef uint32 Nonterm;
29
30 // This special Nonterm value is never used as a real Nonterm, but used as
31 // a standin of an unassigned or unspecified nonterminal.
32 const Nonterm kUnassignedNonterm = 0;
33
34 typedef int32 CallbackId; // `kNoCallback` is reserved for "no callback"
35 enum class DefaultCallback : CallbackId {
36 kSetType = -1,
37 kAssertion = -2,
38 kMapping = -3,
39 kExclusion = -4,
40 kRootRule = 1,
41 kSemanticExpression = 2,
42 };
43
44 // Special CallbackId indicating that there's no callback associated with a
45 // rule.
46 const int32 kNoCallback = 0;
47
48 // A pair of nonterminals.
49 using TwoNonterms = std::pair<Nonterm, Nonterm>;
50
hash_int32(uint32 a)51 static uint32 hash_int32(uint32 a) {
52 a = (a ^ 61) ^ (a >> 16);
53 a = a + (a << 3);
54 a = a ^ (a >> 4);
55 a = a * 0x27d4eb2d;
56 a = a ^ (a >> 15);
57 return a;
58 }
59
60 struct BinaryRuleHasher {
operatorBinaryRuleHasher61 inline uint64 operator()(const TwoNonterms& x) const {
62 // the hash_int32 maps a int to a random int, then treat two ints as a
63 // rational number, then use cantor pairing function to calculate the
64 // order of rational number.
65 uint32 t1 = hash_int32(x.first);
66 uint32 t2 = hash_int32(x.second);
67 uint64 t = t1 + t2;
68 t *= (t + 1);
69 t >>= 1;
70 return t + t1;
71 }
72 };
73
74 } // namespace libtextclassifier3::grammar
75
76 #endif // LIBTEXTCLASSIFIER_UTILS_GRAMMAR_TYPES_H_
77