5 #include <hilti/ast/operators/common.h> 6 #include <hilti/ast/types/bool.h> 7 #include <hilti/ast/types/integer.h> 8 #include <hilti/ast/types/vector.h> 9 #include <hilti/ast/types/void.h> 10 #include <hilti/base/util.h> 15 STANDARD_OPERATOR_1(vector::iterator, Deref, operator_::dereferencedType(0),
16 type::constant(type::vector::Iterator(type::Wildcard())),
17 "Returns the vector element that the iterator refers to.");
18 STANDARD_OPERATOR_1(vector::iterator, IncrPostfix, operator_::sameTypeAs(0,
"iterator<vector<*>>"),
19 type::vector::Iterator(type::Wildcard()),
20 "Advances the iterator by one vector element, returning the previous position.");
21 STANDARD_OPERATOR_1(vector::iterator, IncrPrefix, operator_::sameTypeAs(0,
"iterator<vector<*>>"),
22 type::vector::Iterator(type::Wildcard()),
23 "Advances the iterator by one vector element, returning the new position.");
24 STANDARD_OPERATOR_2(vector::iterator, Equal, type::Bool(), type::constant(type::vector::Iterator(type::Wildcard())),
25 operator_::sameTypeAs(0,
"iterator<vector<*>>"),
26 "Returns true if two vector iterators refer to the same location.");
27 STANDARD_OPERATOR_2(vector::iterator, Unequal, type::Bool(), type::constant(type::vector::Iterator(type::Wildcard())),
28 operator_::sameTypeAs(0,
"iterator<vector<*>>"),
29 "Returns true if two vector iterators refer to different locations.");
31 STANDARD_OPERATOR_1(vector, Size, type::UnsignedInteger(64), type::constant(type::Vector(type::Wildcard())),
32 "Returns the number of elements a vector contains.");
33 STANDARD_OPERATOR_2(vector, Equal, type::Bool(), type::constant(type::Vector(type::Wildcard())),
34 operator_::sameTypeAs(0,
"vector<*>"),
"Compares two vectors element-wise.");
35 STANDARD_OPERATOR_2x_low_prio(vector, IndexConst, Index, operator_::constantElementType(0),
36 type::constant(type::Vector(type::Wildcard())), type::UnsignedInteger(64),
37 "Returns the vector element at the given index.");
38 STANDARD_OPERATOR_2x_lhs(vector, IndexNonConst, Index, operator_::elementType(0), type::Vector(type::Wildcard()),
39 type::UnsignedInteger(64),
"Returns the vector element at the given index.");
40 STANDARD_OPERATOR_2(vector, Unequal, type::Bool(), type::constant(type::Vector(type::Wildcard())),
41 operator_::sameTypeAs(0,
"vector<*>"),
"Compares two vectors element-wise.");
43 STANDARD_OPERATOR_2(vector, Sum, operator_::sameTypeAs(0,
"vector<*>"), type::Vector(type::Wildcard()),
44 operator_::sameTypeAs(0,
"vector<*>"),
"Returns the concatenation of two vectors.")
45 STANDARD_OPERATOR_2(vector, SumAssign, operator_::sameTypeAs(0, "vector<*>"), type::Vector(type::Wildcard()),
46 operator_::sameTypeAs(0, "vector<*>"), "Concatenates another vector to the vector.")
48 BEGIN_METHOD(vector, Assign)
49 auto signature()
const {
50 return Signature{.self = type::Vector(type::Wildcard()),
51 .result = type::void_,
53 .args = {{.id =
"i", .type = type::UnsignedInteger(64)}, {.id =
"x", .type = type::Any()}},
55 Assigns *x* to the *i*th element of the vector. If the vector contains less 56 than *i* elements a sufficient number of default-initialized elements is added 57 to carry out the assignment. 62 BEGIN_METHOD(vector, PushBack) 63 auto signature()
const {
64 return Signature{.self = type::Vector(type::Wildcard()),
65 .result = type::void_,
67 .args = {{.id =
"x", .type = type::Any()}},
69 Appends *x* to the end of the vector. 74 BEGIN_METHOD(vector, PopBack) 75 auto signature()
const {
76 return Signature{.self = type::Vector(type::Wildcard()),
77 .result = type::void_,
81 Removes the last element from the vector, which must be non-empty. 86 BEGIN_METHOD(vector, Front) 87 auto signature()
const {
88 return Signature{.self = type::constant(type::Vector(type::Wildcard())),
89 .result = operator_::constantElementType(0),
93 Returns the first element of the vector. It throws an exception if the vector is 100 BEGIN_METHOD(vector, Back) 101 auto signature()
const {
102 return Signature{.self = type::constant(type::Vector(type::Wildcard())),
103 .result = operator_::constantElementType(0),
107 Returns the last element of the vector. It throws an exception if the vector is 113 BEGIN_METHOD(vector, Reserve) 114 auto signature()
const {
115 return Signature{.self = type::Vector(type::Wildcard()),
116 .result = type::void_,
118 .args = {{.id =
"n", .type = type::constant(type::UnsignedInteger(64))}},
120 Reserves space for at least *n* elements. This operation does not change the 121 vector in any observable way but provides a hint about the size that will be 127 BEGIN_METHOD(vector, Resize) 128 auto signature()
const {
129 return Signature{.self = type::Vector(type::Wildcard()),
130 .result = type::void_,
132 .args = {{.id =
"n", .type = type::constant(type::UnsignedInteger(64))}},
134 Resizes the vector to hold exactly *n* elements. If *n* is larger than the 135 current size, the new slots are filled with default values. If *n* is smaller 136 than the current size, the excessive elements are removed. 141 BEGIN_METHOD(vector, At) 142 auto signature()
const {
143 return Signature{.self = type::constant(type::Vector(type::Wildcard())),
144 .result = operator_::iteratorType(0,
true),
146 .args = {{.id =
"i", .type = type::UnsignedInteger(64)}},
148 Returns an iterator referring to the element at vector index *i*. 153 BEGIN_METHOD(vector, SubRange) 154 auto signature()
const {
155 return Signature{.self = type::constant(type::Vector(type::Wildcard())),
156 .result = operator_::sameTypeAs(0,
"vector<*>"),
158 .args = {{.id =
"begin", .type = type::UnsignedInteger(64)},
159 {.id =
"end", .type = type::UnsignedInteger(64)}},
161 Extracts a subsequence of vector elements spanning from index *begin* 162 to (but not including) index *end*. 167 BEGIN_METHOD(vector, SubEnd) 168 auto signature()
const {
169 return Signature{.self = type::constant(type::Vector(type::Wildcard())),
170 .result = operator_::sameTypeAs(0,
"vector<*>"),
172 .args = {{.id =
"end", .type = type::UnsignedInteger(64)}},
174 Extracts a subsequence of vector elements spanning from the beginning 175 to (but not including) the index *end* as a new vector.