P4C
The P4 Compiler
Loading...
Searching...
No Matches
backends/tofino/bf-asm/map.h
1
17
18#ifndef BACKENDS_TOFINO_BF_ASM_MAP_H_
19#define BACKENDS_TOFINO_BF_ASM_MAP_H_
20
21#include <map>
22
23template <class K, class T, class V, class Comp, class Alloc>
24inline V get(const std::map<K, V, Comp, Alloc> &m, T key, V def = V()) {
25 auto it = m.find(key);
26 if (it != m.end()) return it->second;
27 return def;
28}
29
30template <class K, class T, class V, class Comp, class Alloc>
31inline V *getref(std::map<K, V, Comp, Alloc> &m, T key) {
32 auto it = m.find(key);
33 if (it != m.end()) return &it->second;
34 return 0;
35}
36
37template <class K, class T, class V, class Comp, class Alloc>
38inline const V *getref(const std::map<K, V, Comp, Alloc> &m, T key) {
39 auto it = m.find(key);
40 if (it != m.end()) return &it->second;
41 return 0;
42}
43
44template <class K, class T, class V, class Comp, class Alloc>
45inline V get(const std::map<K, V, Comp, Alloc> *m, T key, V def = V()) {
46 return m ? get(*m, key, def) : def;
47}
48
49template <class K, class T, class V, class Comp, class Alloc>
50inline V *getref(std::map<K, V, Comp, Alloc> *m, T key) {
51 return m ? getref(*m, key) : 0;
52}
53
54template <class K, class T, class V, class Comp, class Alloc>
55inline const V *getref(const std::map<K, V, Comp, Alloc> *m, T key) {
56 return m ? getref(*m, key) : 0;
57}
58
59/* iterate over the keys in a map */
60template <class PairIter>
61struct IterKeys {
62 class iterator
63 : public std::iterator<typename std::iterator_traits<PairIter>::iterator_category,
64 typename std::iterator_traits<PairIter>::value_type,
65 typename std::iterator_traits<PairIter>::difference_type,
66 typename std::iterator_traits<PairIter>::pointer,
67 typename std::iterator_traits<PairIter>::reference> {
68 PairIter it;
69
70 public:
71 iterator() {}
72 explicit iterator(PairIter i) : it(i) {}
73 iterator &operator=(PairIter i) {
74 it = i;
75 return *this;
76 }
77 iterator &operator++() {
78 ++it;
79 return *this;
80 }
81 iterator &operator--() {
82 --it;
83 return *this;
84 }
85 iterator operator++(int) {
86 auto copy = *this;
87 ++it;
88 return copy;
89 }
90 iterator operator--(int) {
91 auto copy = *this;
92 --it;
93 return copy;
94 }
95 bool operator==(const iterator &i) const { return it == i.it; }
96 bool operator!=(const iterator &i) const { return it != i.it; }
97 decltype(*&it->first) operator*() const { return it->first; }
98 decltype(&it->first) operator->() const { return &it->first; }
99 } b, e;
100
101 template <class U>
102 IterKeys(U &map) : b(map.begin()), e(map.end()) {} // NOLINT(runtime/explicit)
103 IterKeys(PairIter b, PairIter e) : b(b), e(e) {}
104 iterator begin() const { return b; }
105 iterator end() const { return e; }
106
107 protected:
108 IterKeys() {}
109};
110
111template <class Map>
112struct IterKeysCopy : IterKeys<typename Map::const_iterator> {
113 Map m;
114 explicit IterKeysCopy(Map &&map) : m(std::move(map)) {
115 // move the map into this object, then setup the iterators
116 this->b = m.begin();
117 this->e = m.end();
118 }
119};
120
121template <class Map>
124}
125
126template <class Map>
129}
130
131template <class Map>
132IterKeysCopy<Map> Keys(Map &&m) {
133 return IterKeysCopy<Map>(std::move(m));
134}
135
136template <class PairIter>
137IterKeys<PairIter> Keys(std::pair<PairIter, PairIter> range) {
138 return IterKeys<PairIter>(range.first, range.second);
139}
140
141/* iterate over the values in a map */
142template <class PairIter>
143struct IterValues {
144 class iterator {
145 public:
146 using iterator_category = typename std::iterator_traits<PairIter>::iterator_category;
147 using value_type = typename std::iterator_traits<PairIter>::value_type;
148 using difference_type = typename std::iterator_traits<PairIter>::difference_type;
149 using pointer = typename std::iterator_traits<PairIter>::pointer;
150 using reference = typename std::iterator_traits<PairIter>::reference;
151 PairIter it;
152
153 public:
154 iterator() {}
155 explicit iterator(PairIter i) : it(i) {}
156 iterator &operator=(PairIter i) {
157 it = i;
158 return *this;
159 }
160 iterator &operator++() {
161 ++it;
162 return *this;
163 }
164 iterator &operator--() {
165 --it;
166 return *this;
167 }
168 iterator operator++(int) {
169 auto copy = *this;
170 ++it;
171 return copy;
172 }
173 iterator operator--(int) {
174 auto copy = *this;
175 --it;
176 return copy;
177 }
178 bool operator==(const iterator &i) const { return it == i.it; }
179 bool operator!=(const iterator &i) const { return it != i.it; }
180 decltype(*&it->second) operator*() const { return it->second; }
181 decltype(&it->second) operator->() const { return &it->second; }
182 } b, e;
183
184 template <class U>
185 IterValues(U &map) : b(map.begin()), e(map.end()) {} // NOLINT(runtime/explicit)
186 IterValues(PairIter b, PairIter e) : b(b), e(e) {}
187 iterator begin() const { return b; }
188 iterator end() const { return e; }
189
190 protected:
191 IterValues() {}
192};
193
194template <class Map>
195struct IterValuesCopy : IterValues<typename Map::const_iterator> {
196 Map m;
197 explicit IterValuesCopy(Map &&map) : m(std::move(map)) {
198 // move the map into this object, then setup the iterators
199 this->b = m.begin();
200 this->e = m.end();
201 }
202};
203
204template <class Map>
207}
208
209template <class Map>
210IterValues<typename Map::const_iterator> Values(const Map &m) {
212}
213
214template <class Map>
215IterValuesCopy<Map> Values(Map &&m) {
216 return IterValuesCopy<Map>(std::move(m));
217}
218
219template <class PairIter>
220IterValues<PairIter> Values(std::pair<PairIter, PairIter> range) {
221 return IterValues<PairIter>(range.first, range.second);
222}
223
224/* iterate over the values for a single key in a multimap */
225template <class M>
226class MapForKey {
227 M &map;
228 typename M::key_type key;
229 class iterator {
230 const MapForKey &self;
231 decltype(map.begin()) it;
232
233 public:
234 iterator(const MapForKey &s, decltype(map.begin()) i) : self(s), it(i) {}
235 iterator &operator++() {
236 if (++it != self.map.end() && it->first != self.key) it = self.map.end();
237 return *this;
238 }
239 bool operator==(const iterator &i) const { return it == i.it; }
240 bool operator!=(const iterator &i) const { return it != i.it; }
241 decltype(*&it->second) operator*() const { return it->second; }
242 decltype(&it->second) operator->() const { return &it->second; }
243 };
244
245 public:
246 MapForKey(M &m, typename M::key_type k) : map(m), key(k) {}
247 iterator begin() const { return iterator(*this, map.find(key)); }
248 iterator end() const { return iterator(*this, map.end()); }
249};
250
251template <class M>
252MapForKey<M> ValuesForKey(M &m, typename M::key_type k) {
253 return MapForKey<M>(m, k);
254}
255
256#endif /* BACKENDS_TOFINO_BF_ASM_MAP_H_ */
Definition backends/tofino/bf-asm/map.h:67
Definition backends/tofino/bf-asm/map.h:144
Definition backends/tofino/bf-asm/map.h:226
Definition backends/tofino/bf-asm/map.h:112
Definition backends/tofino/bf-asm/map.h:61
Definition backends/tofino/bf-asm/map.h:195
Definition backends/tofino/bf-asm/map.h:143