31class IndexedVector :
public Vector<T> {
36 void insertInMap(
const T *a) {
39 auto name = decl->getName().name;
40 auto [it, inserted] = declarations.emplace(name, decl);
43 ::P4::error(ErrorType::ERR_DUPLICATE,
"%1%: Duplicates declaration %2%", a, it->second);
46 void removeFromMap(
const T *a) {
47 if (a ==
nullptr)
return;
49 if (decl ==
nullptr)
return;
50 cstring name = decl->getName().name;
51 auto it = declarations.find(name);
52 if (it == declarations.end()) BUG(
"%1% does not exist", a);
53 declarations.erase(it);
57 using Vector<T>::begin;
60 IndexedVector() =
default;
61 IndexedVector(
const IndexedVector &) =
default;
62 IndexedVector(IndexedVector &&) =
default;
63 IndexedVector(std::initializer_list<const T *> a) : Vector<T>(a) {
64 for (
auto el : *
this) insertInMap(el);
66 IndexedVector &operator=(
const IndexedVector &) =
default;
67 IndexedVector &operator=(IndexedVector &&) =
default;
68 explicit IndexedVector(
const T *a) { push_back(a); }
70 insert(Vector<T>::end(), a.begin(), a.end());
72 explicit IndexedVector(
const Vector<T> &a) { insert(Vector<T>::end(), a.begin(), a.end()); }
73 template <
typename It>
74 explicit IndexedVector(It start, It end) {
75 insert(Vector<T>::end(), start, end);
80 IR::Vector<T>::clear();
86 using iterator =
typename Vector<T>::iterator;
87 using const_iterator =
typename Vector<T>::const_iterator;
90 auto it = declarations.find(name);
91 if (it == declarations.end())
return nullptr;
94 const IDeclaration *getDeclaration(std::string_view name)
const {
95 auto it = declarations.find(name);
96 if (it == declarations.end())
return nullptr;
100 const U *getDeclaration(
cstring name)
const {
101 auto it = declarations.find(name);
102 if (it == declarations.end())
return nullptr;
103 return it->second->template
to<U>();
106 const U *getDeclaration(std::string_view name)
const {
107 auto it = declarations.find(name);
108 if (it == declarations.end())
return nullptr;
109 return it->second->template
to<U>();
112 return Util::enumerate(Values(declarations));
114 iterator erase(iterator from, iterator
to) {
115 for (
auto it = from; it !=
to; ++it) {
118 return Vector<T>::erase(from,
to);
120 iterator erase(iterator i) {
122 return Vector<T>::erase(i);
124 template <
typename ForwardIter>
125 iterator insert(iterator i, ForwardIter b, ForwardIter e) {
126 for (
auto it = b; it != e; ++it) insertInMap(*it);
127 return Vector<T>::insert(i, b, e);
129 iterator replace(iterator i,
const T *v) {
135 template <
typename Container>
136 iterator append(
const Container &toAppend) {
137 return insert(Vector<T>::end(), toAppend.begin(), toAppend.end());
139 template <
typename Container>
140 iterator prepend(
const Container &toAppend) {
141 return insert(Vector<T>::begin(), toAppend.begin(), toAppend.end());
143 iterator insert(iterator i,
const T *v) {
145 return Vector<T>::insert(i, v);
147 template <
class... Args>
148 void emplace_back(Args &&...args) {
149 auto el =
new T(std::forward<Args>(args)...);
152 bool removeByName(
cstring name) {
153 for (
auto it = begin(); it != end(); ++it) {
155 if (decl !=
nullptr && decl->getName() == name) {
162 void push_back(T *a) {
164 Vector<T>::push_back(a);
167 void push_back(
const T *a) {
169 Vector<T>::push_back(a);
173 if (Vector<T>::empty()) BUG(
"pop_back from empty IndexedVector");
174 auto last = Vector<T>::back();
176 Vector<T>::pop_back();
179 void push_back(U &a) {
180 Vector<T>::push_back(a);
184 IRNODE_SUBCLASS(IndexedVector)
185 IRNODE_DECLARE_APPLY_OVERLOAD(IndexedVector)
186 bool operator==(
const Node &a)
const override {
return a == *
this; }
187 bool operator==(
const Vector<T> &a)
const override {
return a == *
this; }
188 bool operator==(
const IndexedVector &a)
const override {
189 return Vector<T>::operator==(
static_cast<const Vector<T> &
>(a));
206 cstring node_type_name()
const override {
207 return "IndexedVector<" + T::static_type_name() +
">";
209 static cstring static_type_name() {
return "IndexedVector<" + T::static_type_name() +
">"; }
210 void visit_children(Visitor &v,
const char *name)
override;
211 void visit_children(Visitor &v,
const char *name)
const override;
215 void validate()
const override {
217 for (
auto el : *
this) {
220 auto it = declarations.find(decl->getName());
221 BUG_CHECK(it != declarations.end() && it->second->getNode() == el->getNode(),
222 "invalid element %1%", el);
226 DECLARE_TYPEINFO_WITH_DISCRIMINATOR(IndexedVector<T>, NodeDiscriminator::IndexedVectorT, T,