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(std::move(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()); }
85 IR::Vector<T>::clear();
91 typedef typename Vector<T>::iterator iterator;
94 auto it = declarations.find(name);
95 if (it == declarations.end())
return nullptr;
98 const IDeclaration *getDeclaration(std::string_view name)
const {
99 auto it = declarations.find(name);
100 if (it == declarations.end())
return nullptr;
104 const U *getDeclaration(
cstring name)
const {
105 auto it = declarations.find(name);
106 if (it == declarations.end())
return nullptr;
107 return it->second->template
to<U>();
110 const U *getDeclaration(std::string_view name)
const {
111 auto it = declarations.find(name);
112 if (it == declarations.end())
return nullptr;
113 return it->second->template
to<U>();
116 return Util::enumerate(Values(declarations));
118 iterator erase(iterator from, iterator
to) {
119 for (
auto it = from; it !=
to; ++it) {
122 return Vector<T>::erase(from,
to);
124 iterator erase(iterator i) {
126 return Vector<T>::erase(i);
128 template <
typename ForwardIter>
129 iterator insert(iterator i, ForwardIter b, ForwardIter e) {
130 for (
auto it = b; it != e; ++it) insertInMap(*it);
131 return Vector<T>::insert(i, b, e);
133 iterator replace(iterator i,
const T *v) {
139 template <
typename Container>
140 iterator append(
const Container &toAppend) {
141 return insert(Vector<T>::end(), toAppend.begin(), toAppend.end());
143 template <
typename Container>
144 iterator prepend(
const Container &toAppend) {
145 return insert(Vector<T>::begin(), toAppend.begin(), toAppend.end());
147 iterator insert(iterator i,
const T *v) {
149 return Vector<T>::insert(i, v);
151 template <
class... Args>
152 void emplace_back(Args &&...args) {
153 auto el =
new T(std::forward<Args>(args)...);
156 bool removeByName(
cstring name) {
157 for (
auto it = begin(); it != end(); ++it) {
159 if (decl !=
nullptr && decl->getName() == name) {
166 void push_back(T *a) {
168 Vector<T>::push_back(a);
171 void push_back(
const T *a) {
173 Vector<T>::push_back(a);
177 if (Vector<T>::empty()) BUG(
"pop_back from empty IndexedVector");
178 auto last = Vector<T>::back();
180 Vector<T>::pop_back();
183 void push_back(U &a) {
184 Vector<T>::push_back(a);
188 IRNODE_SUBCLASS(IndexedVector)
189 IRNODE_DECLARE_APPLY_OVERLOAD(IndexedVector)
190 bool operator==(
const Node &a)
const override {
return a == *
this; }
191 bool operator==(
const Vector<T> &a)
const override {
return a == *
this; }
192 bool operator==(
const IndexedVector &a)
const override {
193 return Vector<T>::operator==(
static_cast<const Vector<T> &
>(a));
210 cstring node_type_name()
const override {
211 return "IndexedVector<" + T::static_type_name() +
">";
213 static cstring static_type_name() {
return "IndexedVector<" + T::static_type_name() +
">"; }
214 void visit_children(Visitor &v)
override;
215 void visit_children(Visitor &v)
const override;
218 static IndexedVector<T> *fromJSON(
JSONLoader &json);
219 void validate()
const override {
221 for (
auto el : *
this) {
224 auto it = declarations.find(decl->getName());
225 BUG_CHECK(it != declarations.end() && it->second->getNode() == el->getNode(),
226 "invalid element %1%", el);
230 DECLARE_TYPEINFO_WITH_DISCRIMINATOR(IndexedVector<T>, NodeDiscriminator::IndexedVectorT, T,