|
| 1 | +// Copyright 2019 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
1 | 15 | #include "common/operators.h" |
2 | 16 |
|
3 | | -#include <map> |
4 | | -#include <memory> |
5 | 17 | #include <string> |
6 | 18 |
|
| 19 | +#include "absl/container/flat_hash_map.h" |
| 20 | +#include "absl/strings/string_view.h" |
| 21 | +#include "absl/types/optional.h" |
| 22 | + |
7 | 23 | #undef IN |
8 | 24 |
|
9 | | -namespace google { |
10 | | -namespace api { |
11 | | -namespace expr { |
12 | | -namespace common { |
| 25 | +namespace google::api::expr::common { |
13 | 26 |
|
14 | 27 | namespace { |
15 | 28 | // These functions provide access to reverse mappings for operators. |
16 | 29 | // Functions generally map from text expression to Expr representation, |
17 | 30 | // e.g., from "&&" to "_&&_". Reverse operators provides a mapping from |
18 | 31 | // Expr to textual mapping, e.g., from "_&&_" to "&&". |
19 | 32 |
|
20 | | -const std::map<std::string, std::string>& UnaryOperators() { |
21 | | - static std::shared_ptr<std::map<std::string, std::string>> unaries_map = |
22 | | - [&]() { |
23 | | - auto u = std::make_shared<std::map<std::string, std::string>>( |
24 | | - std::map<std::string, std::string>{ |
25 | | - {CelOperator::NEGATE, "-"}, {CelOperator::LOGICAL_NOT, "!"}}); |
26 | | - return u; |
27 | | - }(); |
| 33 | +const absl::flat_hash_map<std::string, std::string>& UnaryOperators() { |
| 34 | + static auto* unaries_map = new absl::flat_hash_map<std::string, std::string>{ |
| 35 | + {CelOperator::NEGATE, "-"}, {CelOperator::LOGICAL_NOT, "!"}}; |
28 | 36 | return *unaries_map; |
29 | 37 | } |
30 | 38 |
|
31 | | -const std::map<std::string, std::string>& BinaryOperators() { |
32 | | - static std::shared_ptr<std::map<std::string, std::string>> binops_map = |
33 | | - [&]() { |
34 | | - auto c = std::make_shared<std::map<std::string, std::string>>( |
35 | | - std::map<std::string, std::string>{ |
36 | | - {CelOperator::LOGICAL_OR, "||"}, |
37 | | - {CelOperator::LOGICAL_AND, "&&"}, |
38 | | - {CelOperator::LESS_EQUALS, "<="}, |
39 | | - {CelOperator::LESS, "<"}, |
40 | | - {CelOperator::GREATER_EQUALS, ">="}, |
41 | | - {CelOperator::GREATER, ">"}, |
42 | | - {CelOperator::EQUALS, "=="}, |
43 | | - {CelOperator::NOT_EQUALS, "!="}, |
44 | | - {CelOperator::IN_DEPRECATED, "in"}, |
45 | | - {CelOperator::IN, "in"}, |
46 | | - {CelOperator::ADD, "+"}, |
47 | | - {CelOperator::SUBTRACT, "-"}, |
48 | | - {CelOperator::MULTIPLY, "*"}, |
49 | | - {CelOperator::DIVIDE, "/"}, |
50 | | - {CelOperator::MODULO, "%"}}); |
51 | | - return c; |
52 | | - }(); |
| 39 | +const absl::flat_hash_map<std::string, std::string>& BinaryOperators() { |
| 40 | + static auto* binops_map = new absl::flat_hash_map<std::string, std::string>{ |
| 41 | + {CelOperator::LOGICAL_OR, "||"}, |
| 42 | + {CelOperator::LOGICAL_AND, "&&"}, |
| 43 | + {CelOperator::LESS_EQUALS, "<="}, |
| 44 | + {CelOperator::LESS, "<"}, |
| 45 | + {CelOperator::GREATER_EQUALS, ">="}, |
| 46 | + {CelOperator::GREATER, ">"}, |
| 47 | + {CelOperator::EQUALS, "=="}, |
| 48 | + {CelOperator::NOT_EQUALS, "!="}, |
| 49 | + {CelOperator::IN_DEPRECATED, "in"}, |
| 50 | + {CelOperator::IN, "in"}, |
| 51 | + {CelOperator::ADD, "+"}, |
| 52 | + {CelOperator::SUBTRACT, "-"}, |
| 53 | + {CelOperator::MULTIPLY, "*"}, |
| 54 | + {CelOperator::DIVIDE, "/"}, |
| 55 | + {CelOperator::MODULO, "%"}}; |
53 | 56 | return *binops_map; |
54 | 57 | } |
55 | 58 |
|
56 | | -const std::map<std::string, std::string>& ReverseOperators() { |
57 | | - static std::shared_ptr<std::map<std::string, std::string>> operators_map = |
58 | | - [&]() { |
59 | | - auto c = std::make_shared<std::map<std::string, std::string>>( |
60 | | - std::map<std::string, std::string>{ |
61 | | - {"+", CelOperator::ADD}, |
62 | | - {"-", CelOperator::SUBTRACT}, |
63 | | - {"*", CelOperator::MULTIPLY}, |
64 | | - {"/", CelOperator::DIVIDE}, |
65 | | - {"%", CelOperator::MODULO}, |
66 | | - {"==", CelOperator::EQUALS}, |
67 | | - {"!=", CelOperator::NOT_EQUALS}, |
68 | | - {">", CelOperator::GREATER}, |
69 | | - {">=", CelOperator::GREATER_EQUALS}, |
70 | | - {"<", CelOperator::LESS}, |
71 | | - {"<=", CelOperator::LESS_EQUALS}, |
72 | | - {"&&", CelOperator::LOGICAL_AND}, |
73 | | - {"!", CelOperator::LOGICAL_NOT}, |
74 | | - {"||", CelOperator::LOGICAL_OR}, |
75 | | - {"in", CelOperator::IN}, |
76 | | - }); |
77 | | - return c; |
78 | | - }(); |
| 59 | +const absl::flat_hash_map<std::string, std::string>& ReverseOperators() { |
| 60 | + static auto* operators_map = |
| 61 | + new absl::flat_hash_map<std::string, std::string>{ |
| 62 | + {"+", CelOperator::ADD}, |
| 63 | + {"-", CelOperator::SUBTRACT}, |
| 64 | + {"*", CelOperator::MULTIPLY}, |
| 65 | + {"/", CelOperator::DIVIDE}, |
| 66 | + {"%", CelOperator::MODULO}, |
| 67 | + {"==", CelOperator::EQUALS}, |
| 68 | + {"!=", CelOperator::NOT_EQUALS}, |
| 69 | + {">", CelOperator::GREATER}, |
| 70 | + {">=", CelOperator::GREATER_EQUALS}, |
| 71 | + {"<", CelOperator::LESS}, |
| 72 | + {"<=", CelOperator::LESS_EQUALS}, |
| 73 | + {"&&", CelOperator::LOGICAL_AND}, |
| 74 | + {"!", CelOperator::LOGICAL_NOT}, |
| 75 | + {"||", CelOperator::LOGICAL_OR}, |
| 76 | + {"in", CelOperator::IN}, |
| 77 | + }; |
79 | 78 | return *operators_map; |
80 | 79 | } |
81 | 80 |
|
82 | | -const std::map<std::string, std::string>& Operators() { |
83 | | - static std::shared_ptr<std::map<std::string, std::string>> operators_map = |
84 | | - [&]() { |
85 | | - auto c = std::make_shared<std::map<std::string, std::string>>( |
86 | | - std::map<std::string, std::string>{ |
87 | | - {CelOperator::ADD, "+"}, |
88 | | - {CelOperator::SUBTRACT, "-"}, |
89 | | - {CelOperator::MULTIPLY, "*"}, |
90 | | - {CelOperator::DIVIDE, "/"}, |
91 | | - {CelOperator::MODULO, "%"}, |
92 | | - {CelOperator::EQUALS, "=="}, |
93 | | - {CelOperator::NOT_EQUALS, "!="}, |
94 | | - {CelOperator::GREATER, ">"}, |
95 | | - {CelOperator::GREATER_EQUALS, ">="}, |
96 | | - {CelOperator::LESS, "<"}, |
97 | | - {CelOperator::LESS_EQUALS, "<="}, |
98 | | - {CelOperator::LOGICAL_AND, "&&"}, |
99 | | - {CelOperator::LOGICAL_NOT, "!"}, |
100 | | - {CelOperator::LOGICAL_OR, "||"}, |
101 | | - {CelOperator::IN, "in"}, |
102 | | - {CelOperator::IN_DEPRECATED, "in"}, |
103 | | - {CelOperator::NEGATE, "-"}}); |
104 | | - return c; |
105 | | - }(); |
| 81 | +const absl::flat_hash_map<std::string, std::string>& Operators() { |
| 82 | + static auto* operators_map = |
| 83 | + new absl::flat_hash_map<std::string, std::string>{ |
| 84 | + {CelOperator::ADD, "+"}, |
| 85 | + {CelOperator::SUBTRACT, "-"}, |
| 86 | + {CelOperator::MULTIPLY, "*"}, |
| 87 | + {CelOperator::DIVIDE, "/"}, |
| 88 | + {CelOperator::MODULO, "%"}, |
| 89 | + {CelOperator::EQUALS, "=="}, |
| 90 | + {CelOperator::NOT_EQUALS, "!="}, |
| 91 | + {CelOperator::GREATER, ">"}, |
| 92 | + {CelOperator::GREATER_EQUALS, ">="}, |
| 93 | + {CelOperator::LESS, "<"}, |
| 94 | + {CelOperator::LESS_EQUALS, "<="}, |
| 95 | + {CelOperator::LOGICAL_AND, "&&"}, |
| 96 | + {CelOperator::LOGICAL_NOT, "!"}, |
| 97 | + {CelOperator::LOGICAL_OR, "||"}, |
| 98 | + {CelOperator::IN, "in"}, |
| 99 | + {CelOperator::IN_DEPRECATED, "in"}, |
| 100 | + {CelOperator::NEGATE, "-"}}; |
106 | 101 | return *operators_map; |
107 | 102 | } |
108 | 103 |
|
109 | 104 | // precedence of the operator, where the higher value means higher. |
110 | | -const std::map<std::string, int>& Precedences() { |
111 | | - static std::shared_ptr<std::map<std::string, int>> precedence_map = [&]() { |
112 | | - auto c = std::make_shared<std::map<std::string, int>>( |
113 | | - std::map<std::string, int>{{CelOperator::CONDITIONAL, 8}, |
| 105 | +const absl::flat_hash_map<std::string, int>& Precedences() { |
| 106 | + static auto* precedence_map = new absl::flat_hash_map<std::string, int>{ |
| 107 | + {CelOperator::CONDITIONAL, 8}, |
114 | 108 |
|
115 | | - {CelOperator::LOGICAL_OR, 7}, |
| 109 | + {CelOperator::LOGICAL_OR, 7}, |
116 | 110 |
|
117 | | - {CelOperator::LOGICAL_AND, 6}, |
| 111 | + {CelOperator::LOGICAL_AND, 6}, |
118 | 112 |
|
119 | | - {CelOperator::EQUALS, 5}, |
120 | | - {CelOperator::GREATER, 5}, |
121 | | - {CelOperator::GREATER_EQUALS, 5}, |
122 | | - {CelOperator::IN, 5}, |
123 | | - {CelOperator::LESS, 5}, |
124 | | - {CelOperator::LESS_EQUALS, 5}, |
125 | | - {CelOperator::NOT_EQUALS, 5}, |
126 | | - {CelOperator::IN_DEPRECATED, 5}, |
| 113 | + {CelOperator::EQUALS, 5}, |
| 114 | + {CelOperator::GREATER, 5}, |
| 115 | + {CelOperator::GREATER_EQUALS, 5}, |
| 116 | + {CelOperator::IN, 5}, |
| 117 | + {CelOperator::LESS, 5}, |
| 118 | + {CelOperator::LESS_EQUALS, 5}, |
| 119 | + {CelOperator::NOT_EQUALS, 5}, |
| 120 | + {CelOperator::IN_DEPRECATED, 5}, |
127 | 121 |
|
128 | | - {CelOperator::ADD, 4}, |
129 | | - {CelOperator::SUBTRACT, 4}, |
| 122 | + {CelOperator::ADD, 4}, |
| 123 | + {CelOperator::SUBTRACT, 4}, |
130 | 124 |
|
131 | | - {CelOperator::DIVIDE, 3}, |
132 | | - {CelOperator::MODULO, 3}, |
133 | | - {CelOperator::MULTIPLY, 3}, |
| 125 | + {CelOperator::DIVIDE, 3}, |
| 126 | + {CelOperator::MODULO, 3}, |
| 127 | + {CelOperator::MULTIPLY, 3}, |
134 | 128 |
|
135 | | - {CelOperator::LOGICAL_NOT, 2}, |
136 | | - {CelOperator::NEGATE, 2}, |
| 129 | + {CelOperator::LOGICAL_NOT, 2}, |
| 130 | + {CelOperator::NEGATE, 2}, |
137 | 131 |
|
138 | | - {CelOperator::INDEX, 1}}); |
139 | | - return c; |
140 | | - }(); |
| 132 | + {CelOperator::INDEX, 1}}; |
141 | 133 | return *precedence_map; |
142 | 134 | } |
143 | 135 |
|
@@ -238,7 +230,4 @@ bool IsOperatorLeftRecursive(const std::string& op) { |
238 | 230 | return op != CelOperator::LOGICAL_AND && op != CelOperator::LOGICAL_OR; |
239 | 231 | } |
240 | 232 |
|
241 | | -} // namespace common |
242 | | -} // namespace expr |
243 | | -} // namespace api |
244 | | -} // namespace google |
| 233 | +} // namespace google::api::expr::common |
0 commit comments