• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 };
42 
43 // Special CallbackId indicating that there's no callback associated with a
44 // rule.
45 const int32 kNoCallback = 0;
46 
47 // A pair of nonterminals.
48 using TwoNonterms = std::pair<Nonterm, Nonterm>;
49 
hash_int32(uint32 a)50 static uint32 hash_int32(uint32 a) {
51   a = (a ^ 61) ^ (a >> 16);
52   a = a + (a << 3);
53   a = a ^ (a >> 4);
54   a = a * 0x27d4eb2d;
55   a = a ^ (a >> 15);
56   return a;
57 }
58 
59 struct BinaryRuleHasher {
operatorBinaryRuleHasher60   inline uint64 operator()(const TwoNonterms& x) const {
61     // the hash_int32 maps a int to a random int, then treat two ints as a
62     // rational number, then use cantor pairing function to calculate the
63     // order of rational number.
64     uint32 t1 = hash_int32(x.first);
65     uint32 t2 = hash_int32(x.second);
66     uint64 t = t1 + t2;
67     t *= (t + 1);
68     t >>= 1;
69     return t + t1;
70   }
71 };
72 
73 }  // namespace libtextclassifier3::grammar
74 
75 #endif  // LIBTEXTCLASSIFIER_UTILS_GRAMMAR_TYPES_H_
76