forked from google/cel-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction_descriptor.cc
More file actions
98 lines (88 loc) · 2.72 KB
/
function_descriptor.cc
File metadata and controls
98 lines (88 loc) · 2.72 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
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "common/function_descriptor.h"
#include <algorithm>
#include <cstddef>
#include "absl/base/macros.h"
#include "absl/types/span.h"
#include "common/kind.h"
namespace cel {
bool FunctionDescriptor::ShapeMatches(bool receiver_style,
absl::Span<const Kind> types) const {
if (this->receiver_style() != receiver_style) {
return false;
}
if (this->types().size() != types.size()) {
return false;
}
for (size_t i = 0; i < this->types().size(); i++) {
Kind this_type = this->types()[i];
Kind other_type = types[i];
if (this_type != Kind::kAny && other_type != Kind::kAny &&
this_type != other_type) {
return false;
}
}
return true;
}
bool FunctionDescriptor::operator==(const FunctionDescriptor& other) const {
return impl_.get() == other.impl_.get() ||
(name() == other.name() &&
receiver_style() == other.receiver_style() &&
types().size() == other.types().size() &&
std::equal(types().begin(), types().end(), other.types().begin()));
}
bool FunctionDescriptor::operator<(const FunctionDescriptor& other) const {
if (impl_.get() == other.impl_.get()) {
return false;
}
if (name() < other.name()) {
return true;
}
if (name() != other.name()) {
return false;
}
if (receiver_style() < other.receiver_style()) {
return true;
}
if (receiver_style() != other.receiver_style()) {
return false;
}
auto lhs_begin = types().begin();
auto lhs_end = types().end();
auto rhs_begin = other.types().begin();
auto rhs_end = other.types().end();
while (lhs_begin != lhs_end && rhs_begin != rhs_end) {
if (*lhs_begin < *rhs_begin) {
return true;
}
if (!(*lhs_begin == *rhs_begin)) {
return false;
}
lhs_begin++;
rhs_begin++;
}
if (lhs_begin == lhs_end && rhs_begin == rhs_end) {
// Neither has any elements left, they are equal.
return false;
}
if (lhs_begin == lhs_end) {
// Left has no more elements. Right is greater.
return true;
}
// Right has no more elements. Left is greater.
ABSL_ASSERT(rhs_begin == rhs_end);
return false;
}
} // namespace cel