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 {
13 namespace operator_ {
14 
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.");
30 
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(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.");
42 
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.")
47 
48 BEGIN_METHOD(vector, Assign)
49  auto signature() const {
50  return Signature{.self = type::Vector(type::Wildcard()),
51  .result = type::void_,
52  .id = "assign",
53  .args = {{.id = "i", .type = type::UnsignedInteger(64)}, {.id = "x", .type = type::Any()}},
54  .doc = R"(
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.
58 )"};
59  }
60 END_METHOD
61 
62 BEGIN_METHOD(vector, PushBack)
63  auto signature() const {
64  return Signature{.self = type::Vector(type::Wildcard()),
65  .result = type::void_,
66  .id = "push_back",
67  .args = {{.id = "x", .type = type::Any()}},
68  .doc = R"(
69 Appends *x* to the end of the vector.
70 )"};
71  }
72 END_METHOD
73 
74 BEGIN_METHOD(vector, PopBack)
75  auto signature() const {
76  return Signature{.self = type::Vector(type::Wildcard()),
77  .result = type::void_,
78  .id = "pop_back",
79  .args = {},
80  .doc = R"(
81 Removes the last element from the vector, which must be non-empty.
82 )"};
83  }
84 END_METHOD
85 
86 BEGIN_METHOD(vector, Front)
87  auto signature() const {
88  return Signature{.self = type::constant(type::Vector(type::Wildcard())),
89  .result = operator_::constantElementType(0),
90  .id = "front",
91  .args = {},
92  .doc = R"(
93 Returns the first element of the vector. It throws an exception if the vector is
94 empty.
95 )"};
96  }
97 END_METHOD
98 
99 
100 BEGIN_METHOD(vector, Back)
101  auto signature() const {
102  return Signature{.self = type::constant(type::Vector(type::Wildcard())),
103  .result = operator_::constantElementType(0),
104  .id = "back",
105  .args = {},
106  .doc = R"(
107 Returns the last element of the vector. It throws an exception if the vector is
108 empty.
109 )"};
110  }
111 END_METHOD
112 
113 BEGIN_METHOD(vector, Reserve)
114  auto signature() const {
115  return Signature{.self = type::Vector(type::Wildcard()),
116  .result = type::void_,
117  .id = "reserve",
118  .args = {{.id = "n", .type = type::constant(type::UnsignedInteger(64))}},
119  .doc = R"(
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
122 needed.
123 )"};
124  }
125 END_METHOD
126 
127 BEGIN_METHOD(vector, Resize)
128  auto signature() const {
129  return Signature{.self = type::Vector(type::Wildcard()),
130  .result = type::void_,
131  .id = "resize",
132  .args = {{.id = "n", .type = type::constant(type::UnsignedInteger(64))}},
133  .doc = R"(
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.
137 )"};
138  }
139 END_METHOD
140 
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),
145  .id = "at",
146  .args = {{.id = "i", .type = type::UnsignedInteger(64)}},
147  .doc = R"(
148 Returns an iterator referring to the element at vector index *i*.
149 )"};
150  }
151 END_METHOD
152 
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<*>"),
157  .id = "sub",
158  .args = {{.id = "begin", .type = type::UnsignedInteger(64)},
159  {.id = "end", .type = type::UnsignedInteger(64)}},
160  .doc = R"(
161 Extracts a subsequence of vector elements spanning from index *begin*
162 to (but not including) index *end*.
163 )"};
164  }
165 END_METHOD
166 
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<*>"),
171  .id = "sub",
172  .args = {{.id = "end", .type = type::UnsignedInteger(64)}},
173  .doc = R"(
174 Extracts a subsequence of vector elements spanning from the beginning
175 to (but not including) the index *end* as a new vector.
176 )"};
177  }
178 END_METHOD
179 
180 } // namespace operator_
181 
182 } // namespace hilti