Spicy
vector.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
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>
11 
12 namespace hilti::operator_ {
13 
14 STANDARD_OPERATOR_1(vector::iterator, Deref, operator_::dereferencedType(0),
15  type::constant(type::vector::Iterator(type::Wildcard())),
16  "Returns the vector element that the iterator refers to.");
17 STANDARD_OPERATOR_1(vector::iterator, IncrPostfix, operator_::sameTypeAs(0, "iterator<vector<*>>"),
18  type::vector::Iterator(type::Wildcard()),
19  "Advances the iterator by one vector element, returning the previous position.");
20 STANDARD_OPERATOR_1(vector::iterator, IncrPrefix, operator_::sameTypeAs(0, "iterator<vector<*>>"),
21  type::vector::Iterator(type::Wildcard()),
22  "Advances the iterator by one vector element, returning the new position.");
23 STANDARD_OPERATOR_2(vector::iterator, Equal, type::Bool(), type::constant(type::vector::Iterator(type::Wildcard())),
24  operator_::sameTypeAs(0, "iterator<vector<*>>"),
25  "Returns true if two vector iterators refer to the same location.");
26 STANDARD_OPERATOR_2(vector::iterator, Unequal, type::Bool(), type::constant(type::vector::Iterator(type::Wildcard())),
27  operator_::sameTypeAs(0, "iterator<vector<*>>"),
28  "Returns true if two vector iterators refer to different locations.");
29 
30 STANDARD_OPERATOR_1(vector, Size, type::UnsignedInteger(64), type::constant(type::Vector(type::Wildcard())),
31  "Returns the number of elements a vector contains.");
32 STANDARD_OPERATOR_2(vector, Equal, type::Bool(), type::constant(type::Vector(type::Wildcard())),
33  operator_::sameTypeAs(0, "vector<*>"), "Compares two vectors element-wise.");
34 STANDARD_OPERATOR_2x_low_prio(vector, IndexConst, Index, operator_::constantElementType(0),
35  type::constant(type::Vector(type::Wildcard())), type::UnsignedInteger(64),
36  "Returns the vector element at the given index.");
37 STANDARD_OPERATOR_2x_lhs(vector, IndexNonConst, Index, operator_::elementType(0), type::Vector(type::Wildcard()),
38  type::UnsignedInteger(64), "Returns the vector element at the given index.");
39 STANDARD_OPERATOR_2(vector, Unequal, type::Bool(), type::constant(type::Vector(type::Wildcard())),
40  operator_::sameTypeAs(0, "vector<*>"), "Compares two vectors element-wise.");
41 
42 STANDARD_OPERATOR_2(vector, Sum, operator_::sameTypeAs(0, "vector<*>"), type::Vector(type::Wildcard()),
43  operator_::sameTypeAs(0, "vector<*>"), "Returns the concatenation of two vectors.")
44 STANDARD_OPERATOR_2(vector, SumAssign, operator_::sameTypeAs(0, "vector<*>"), type::Vector(type::Wildcard()),
45  operator_::sameTypeAs(0, "vector<*>"), "Concatenates another vector to the vector.")
46 
47 BEGIN_METHOD(vector, Assign)
48  const auto& signature() const {
49  static auto _signature = Signature{.self = type::Vector(type::Wildcard()),
50  .result = type::void_,
51  .id = "assign",
52  .args = {{"i", type::UnsignedInteger(64)}, {"x", type::Any()}},
53  .doc = R"(
54 Assigns *x* to the *i*th element of the vector. If the vector contains less
55 than *i* elements a sufficient number of default-initialized elements is added
56 to carry out the assignment.
57 )"};
58  return _signature;
59  }
60 END_METHOD
61 
62 BEGIN_METHOD(vector, PushBack)
63  const auto& signature() const {
64  static auto _signature = Signature{.self = type::Vector(type::Wildcard()),
65  .result = type::void_,
66  .id = "push_back",
67  .args = {{"x", type::Any()}},
68  .doc = R"(
69 Appends *x* to the end of the vector.
70 )"};
71  return _signature;
72  }
73 END_METHOD
74 
75 BEGIN_METHOD(vector, PopBack)
76  const auto& signature() const {
77  static auto _signature = Signature{.self = type::Vector(type::Wildcard()),
78  .result = type::void_,
79  .id = "pop_back",
80  .args = {},
81  .doc = R"(
82 Removes the last element from the vector, which must be non-empty.
83 )"};
84  return _signature;
85  }
86 END_METHOD
87 
88 BEGIN_METHOD(vector, Front)
89  const auto& signature() const {
90  static auto _signature = Signature{.self = type::constant(type::Vector(type::Wildcard())),
91  .result = operator_::constantElementType(0),
92  .id = "front",
93  .args = {},
94  .doc = R"(
95 Returns the first element of the vector. It throws an exception if the vector is
96 empty.
97 )"};
98  return _signature;
99  }
100 END_METHOD
101 
102 
103 BEGIN_METHOD(vector, Back)
104  const auto& signature() const {
105  static auto _signature = Signature{.self = type::constant(type::Vector(type::Wildcard())),
106  .result = operator_::constantElementType(0),
107  .id = "back",
108  .args = {},
109  .doc = R"(
110 Returns the last element of the vector. It throws an exception if the vector is
111 empty.
112 )"};
113  return _signature;
114  }
115 END_METHOD
116 
117 BEGIN_METHOD(vector, Reserve)
118  const auto& signature() const {
119  static auto _signature = Signature{.self = type::Vector(type::Wildcard()),
120  .result = type::void_,
121  .id = "reserve",
122  .args = {{"n", type::constant(type::UnsignedInteger(64))}},
123  .doc = R"(
124 Reserves space for at least *n* elements. This operation does not change the
125 vector in any observable way but provides a hint about the size that will be
126 needed.
127 )"};
128  return _signature;
129  }
130 END_METHOD
131 
132 BEGIN_METHOD(vector, Resize)
133  const auto& signature() const {
134  static auto _signature = Signature{.self = type::Vector(type::Wildcard()),
135  .result = type::void_,
136  .id = "resize",
137  .args = {{"n", type::constant(type::UnsignedInteger(64))}},
138  .doc = R"(
139 Resizes the vector to hold exactly *n* elements. If *n* is larger than the
140 current size, the new slots are filled with default values. If *n* is smaller
141 than the current size, the excessive elements are removed.
142 )"};
143  return _signature;
144  }
145 END_METHOD
146 
147 BEGIN_METHOD(vector, At)
148  const auto& signature() const {
149  static auto _signature = Signature{.self = type::constant(type::Vector(type::Wildcard())),
150  .result = operator_::iteratorType(0, true),
151  .id = "at",
152  .args = {{"i", type::UnsignedInteger(64)}},
153  .doc = R"(
154 Returns an iterator referring to the element at vector index *i*.
155 )"};
156  return _signature;
157  }
158 END_METHOD
159 
160 BEGIN_METHOD(vector, SubRange)
161  const auto& signature() const {
162  static auto _signature =
163  Signature{.self = type::constant(type::Vector(type::Wildcard())),
164  .result = operator_::sameTypeAs(0, "vector<*>"),
165  .id = "sub",
166  .args = {{"begin", type::UnsignedInteger(64)}, {"end", type::UnsignedInteger(64)}},
167  .doc = R"(
168 Extracts a subsequence of vector elements spanning from index *begin*
169 to (but not including) index *end*.
170 )"};
171  return _signature;
172  }
173 END_METHOD
174 
175 BEGIN_METHOD(vector, SubEnd)
176  const auto& signature() const {
177  static auto _signature = Signature{.self = type::constant(type::Vector(type::Wildcard())),
178  .result = operator_::sameTypeAs(0, "vector<*>"),
179  .id = "sub",
180  .args = {{"end", type::UnsignedInteger(64)}},
181  .doc = R"(
182 Extracts a subsequence of vector elements spanning from the beginning
183 to (but not including) the index *end* as a new vector.
184 )"};
185  return _signature;
186  }
187 END_METHOD
188 
189 } // namespace hilti::operator_
Definition: operator-registry.h:15