forked from cel-expr/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunknown_function_result_set.h
More file actions
85 lines (67 loc) · 2.91 KB
/
Copy pathunknown_function_result_set.h
File metadata and controls
85 lines (67 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#ifndef THIRD_PARTY_CEL_CPP_EVAL_PUBLIC_UNKNOWN_FUNCTION_RESULT_SET_H_
#define THIRD_PARTY_CEL_CPP_EVAL_PUBLIC_UNKNOWN_FUNCTION_RESULT_SET_H_
#include <vector>
#include "google/api/expr/v1alpha1/syntax.pb.h"
#include "absl/container/btree_map.h"
#include "absl/container/btree_set.h"
#include "eval/public/cel_function.h"
namespace google {
namespace api {
namespace expr {
namespace runtime {
// Represents a function result that is unknown at the time of execution. This
// allows for lazy evaluation of expensive functions.
class UnknownFunctionResult {
public:
UnknownFunctionResult(const CelFunctionDescriptor& descriptor, int64_t expr_id,
const std::vector<CelValue>& arguments)
: descriptor_(descriptor), expr_id_(expr_id), arguments_(arguments) {}
// The descriptor of the called function that return Unknown.
const CelFunctionDescriptor& descriptor() const { return descriptor_; }
// The id of the |Expr| that triggered the function call step. Provided
// informationally -- if two different |Expr|s generate the same unknown call,
// they will be treated as the same unknown function result.
int64_t call_expr_id() const { return expr_id_; }
// The arguments of the function call that generated the unknown.
const std::vector<CelValue>& arguments() const { return arguments_; }
// Equality operator provided for testing. Compatible with set less-than
// comparator.
// Compares descriptor then arguments elementwise.
bool IsEqualTo(const UnknownFunctionResult& other) const;
private:
CelFunctionDescriptor descriptor_;
int64_t expr_id_;
std::vector<CelValue> arguments_;
};
// Comparator for set semantics.
struct UnknownFunctionComparator {
bool operator()(const UnknownFunctionResult*,
const UnknownFunctionResult*) const;
};
// Represents a collection of unknown function results at a particular point in
// execution. Execution should advance further if this set of unknowns are
// provided. It may not advance if only a subset are provided.
// Set semantics use |IsEqualTo()| defined on |UnknownFunctionResult|.
class UnknownFunctionResultSet {
public:
// Empty set
UnknownFunctionResultSet() {}
// Merge constructor -- effectively union(lhs, rhs).
UnknownFunctionResultSet(const UnknownFunctionResultSet& lhs,
const UnknownFunctionResultSet& rhs);
// Initialize with a single UnknownFunctionResult.
UnknownFunctionResultSet(const UnknownFunctionResult* initial)
: unknown_function_results_{initial} {}
using Container =
absl::btree_set<const UnknownFunctionResult*, UnknownFunctionComparator>;
const Container& unknown_function_results() const {
return unknown_function_results_;
}
private:
Container unknown_function_results_;
};
} // namespace runtime
} // namespace expr
} // namespace api
} // namespace google
#endif // THIRD_PARTY_CEL_CPP_EVAL_PUBLIC_UNKNOWN_FUNCTION_RESULT_SET_H_