40class IndexedVector :
public Vector<T> {
45 void insertInMap(
const T *a) {
48 auto name = decl->getName().name;
49 auto [it, inserted] = declarations.emplace(name, decl);
52 ::P4::error(ErrorType::ERR_DUPLICATE,
"%1%: Duplicates declaration %2%", a, it->second);
55 void removeFromMap(
const T *a) {
56 if (a ==
nullptr)
return;
58 if (decl ==
nullptr)
return;
59 cstring name = decl->getName().name;
60 auto it = declarations.find(name);
61 if (it == declarations.end()) BUG(
"%1% does not exist", a);
62 declarations.erase(it);
66 using Vector<T>::begin;
69 IndexedVector() =
default;
70 IndexedVector(
const IndexedVector &) =
default;
71 IndexedVector(IndexedVector &&) =
default;
72 IndexedVector(std::initializer_list<const T *> a) : Vector<T>(a) {
73 for (
auto el : *
this) insertInMap(el);
75 IndexedVector &operator=(
const IndexedVector &) =
default;
76 IndexedVector &operator=(IndexedVector &&) =
default;
77 explicit IndexedVector(
const T *a) { push_back(a); }
79 insert(Vector<T>::end(), a.begin(), a.end());
81 explicit IndexedVector(
const Vector<T> &a) { insert(Vector<T>::end(), a.begin(), a.end()); }
82 template <
typename It>
83 explicit IndexedVector(It start, It end) {
84 insert(Vector<T>::end(), start, end);
89 IR::Vector<T>::clear();
95 using iterator =
typename Vector<T>::iterator;
96 using const_iterator =
typename Vector<T>::const_iterator;
99 auto it = declarations.find(name);
100 if (it == declarations.end())
return nullptr;
103 const IDeclaration *getDeclaration(std::string_view name)
const {
104 auto it = declarations.find(name);
105 if (it == declarations.end())
return nullptr;
109 const U *getDeclaration(
cstring name)
const {
110 auto it = declarations.find(name);
111 if (it == declarations.end())
return nullptr;
112 return it->second->template
to<U>();
115 const U *getDeclaration(std::string_view name)
const {
116 auto it = declarations.find(name);
117 if (it == declarations.end())
return nullptr;
118 return it->second->template
to<U>();
121 return Util::enumerate(Values(declarations));
123 iterator erase(iterator from, iterator
to) {
124 for (
auto it = from; it !=
to; ++it) {
127 return Vector<T>::erase(from,
to);
129 iterator erase(iterator i) {
131 return Vector<T>::erase(i);
133 template <
typename ForwardIter>
134 iterator insert(iterator i, ForwardIter b, ForwardIter e) {
135 for (
auto it = b; it != e; ++it) insertInMap(*it);
136 return Vector<T>::insert(i, b, e);
138 iterator replace(iterator i,
const T *v) {
144 template <
typename Container>
145 iterator append(
const Container &toAppend) {
146 return insert(Vector<T>::end(), toAppend.begin(), toAppend.end());
148 template <
typename Container>
149 iterator prepend(
const Container &toAppend) {
150 return insert(Vector<T>::begin(), toAppend.begin(), toAppend.end());
152 iterator insert(iterator i,
const T *v) {
154 return Vector<T>::insert(i, v);
156 template <
class... Args>
157 void emplace_back(Args &&...args) {
158 auto el =
new T(std::forward<Args>(args)...);
161 bool removeByName(
cstring name) {
162 for (
auto it = begin(); it != end(); ++it) {
164 if (decl !=
nullptr && decl->getName() == name) {
171 void push_back(T *a) {
173 Vector<T>::push_back(a);
176 void push_back(
const T *a) {
178 Vector<T>::push_back(a);
182 if (Vector<T>::empty()) BUG(
"pop_back from empty IndexedVector");
183 auto last = Vector<T>::back();
185 Vector<T>::pop_back();
188 void push_back(U &a) {
189 Vector<T>::push_back(a);
193 IRNODE_SUBCLASS(IndexedVector)
194 IRNODE_DECLARE_APPLY_OVERLOAD(IndexedVector)
195 bool operator==(
const Node &a)
const override {
return a == *
this; }
196 bool operator==(
const Vector<T> &a)
const override {
return a == *
this; }
197 bool operator==(
const IndexedVector &a)
const override {
198 return Vector<T>::operator==(
static_cast<const Vector<T> &
>(a));
215 cstring node_type_name()
const override {
216 return "IndexedVector<" + T::static_type_name() +
">";
218 static cstring static_type_name() {
return "IndexedVector<" + T::static_type_name() +
">"; }
219 void visit_children(Visitor &v,
const char *name)
override;
220 void visit_children(Visitor &v,
const char *name)
const override;
223 static IndexedVector<T> *fromJSON(
JSONLoader &json);
224 void validate()
const override {
226 for (
auto el : *
this) {
229 auto it = declarations.find(decl->getName());
230 BUG_CHECK(it != declarations.end() && it->second->getNode() == el->getNode(),
231 "invalid element %1%", el);
235 DECLARE_TYPEINFO_WITH_DISCRIMINATOR(IndexedVector<T>, NodeDiscriminator::IndexedVectorT, T,