/proc/self/cwd/eval/public/activation.cc
Line | Count | Source |
1 | | #include "eval/public/activation.h" |
2 | | |
3 | | #include <algorithm> |
4 | | #include <memory> |
5 | | #include <utility> |
6 | | #include <vector> |
7 | | |
8 | | #include "absl/status/status.h" |
9 | | #include "absl/strings/string_view.h" |
10 | | #include "eval/public/cel_function.h" |
11 | | |
12 | | namespace google { |
13 | | namespace api { |
14 | | namespace expr { |
15 | | namespace runtime { |
16 | | |
17 | | absl::optional<CelValue> Activation::FindValue(absl::string_view name, |
18 | 55.1k | google::protobuf::Arena* arena) const { |
19 | 55.1k | auto entry = value_map_.find(name); |
20 | | |
21 | | // No entry found. |
22 | 55.1k | if (entry == value_map_.end()) { |
23 | 55.1k | return {}; |
24 | 55.1k | } |
25 | | |
26 | 0 | return entry->second.RetrieveValue(arena); |
27 | 55.1k | } |
28 | | |
29 | 0 | absl::Status Activation::InsertFunction(std::unique_ptr<CelFunction> function) { |
30 | 0 | auto& overloads = function_map_[function->descriptor().name()]; |
31 | 0 | for (const auto& overload : overloads) { |
32 | 0 | if (overload->descriptor().ShapeMatches(function->descriptor())) { |
33 | 0 | return absl::InvalidArgumentError( |
34 | 0 | "Function with same shape already defined in activation"); |
35 | 0 | } |
36 | 0 | } |
37 | 0 | overloads.emplace_back(std::move(function)); |
38 | 0 | return absl::OkStatus(); |
39 | 0 | } |
40 | | |
41 | | std::vector<const CelFunction*> Activation::FindFunctionOverloads( |
42 | 0 | absl::string_view name) const { |
43 | 0 | const auto map_entry = function_map_.find(name); |
44 | 0 | std::vector<const CelFunction*> overloads; |
45 | 0 | if (map_entry == function_map_.end()) { |
46 | 0 | return overloads; |
47 | 0 | } |
48 | 0 | overloads.resize(map_entry->second.size()); |
49 | 0 | std::transform(map_entry->second.begin(), map_entry->second.end(), |
50 | 0 | overloads.begin(), |
51 | 0 | [](const auto& func) { return func.get(); }); |
52 | 0 | return overloads; |
53 | 0 | } |
54 | | |
55 | | bool Activation::RemoveFunctionEntries( |
56 | 0 | const CelFunctionDescriptor& descriptor) { |
57 | 0 | auto map_entry = function_map_.find(descriptor.name()); |
58 | 0 | if (map_entry == function_map_.end()) { |
59 | 0 | return false; |
60 | 0 | } |
61 | 0 | std::vector<std::unique_ptr<CelFunction>>& overloads = map_entry->second; |
62 | 0 | bool funcs_removed = false; |
63 | 0 | auto func_iter = overloads.begin(); |
64 | 0 | while (func_iter != overloads.end()) { |
65 | 0 | if (descriptor.ShapeMatches(func_iter->get()->descriptor())) { |
66 | 0 | func_iter = overloads.erase(func_iter); |
67 | 0 | funcs_removed = true; |
68 | 0 | } else { |
69 | 0 | ++func_iter; |
70 | 0 | } |
71 | 0 | } |
72 | |
|
73 | 0 | if (overloads.empty()) { |
74 | 0 | function_map_.erase(map_entry); |
75 | 0 | } |
76 | |
|
77 | 0 | return funcs_removed; |
78 | 0 | } |
79 | | |
80 | 0 | void Activation::InsertValue(absl::string_view name, const CelValue& value) { |
81 | 0 | value_map_.try_emplace(name, ValueEntry(value)); |
82 | 0 | } |
83 | | |
84 | | void Activation::InsertValueProducer( |
85 | 0 | absl::string_view name, std::unique_ptr<CelValueProducer> value_producer) { |
86 | 0 | value_map_.try_emplace(name, ValueEntry(std::move(value_producer))); |
87 | 0 | } |
88 | | |
89 | 0 | bool Activation::RemoveValueEntry(absl::string_view name) { |
90 | 0 | return value_map_.erase(name); |
91 | 0 | } |
92 | | |
93 | 0 | bool Activation::ClearValueEntry(absl::string_view name) { |
94 | 0 | auto entry = value_map_.find(name); |
95 | | |
96 | | // No entry found. |
97 | 0 | if (entry == value_map_.end()) { |
98 | 0 | return false; |
99 | 0 | } |
100 | | |
101 | 0 | return entry->second.ClearValue(); |
102 | 0 | } |
103 | | |
104 | 0 | int Activation::ClearCachedValues() { |
105 | 0 | int n = 0; |
106 | 0 | for (auto& entry : value_map_) { |
107 | 0 | if (entry.second.HasProducer()) { |
108 | 0 | if (entry.second.ClearValue()) { |
109 | 0 | n++; |
110 | 0 | } |
111 | 0 | } |
112 | 0 | } |
113 | 0 | return n; |
114 | 0 | } |
115 | | |
116 | | } // namespace runtime |
117 | | } // namespace expr |
118 | | } // namespace api |
119 | | } // namespace google |