P4C
The P4 Compiler
Loading...
Searching...
No Matches
algorithm.h
1/*
2 * SPDX-FileCopyrightText: 2013 Barefoot Networks, Inc.
3 * Copyright 2013-present Barefoot Networks, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef LIB_ALGORITHM_H_
9#define LIB_ALGORITHM_H_
10
11#include <algorithm>
12#include <set>
13
14// Round up x/y, for integers
15#define ROUNDUP(x, y) (((x) + (y) - 1) / (y))
16// Elements in an array
17#define ELEMENTS(a) (sizeof(a) / sizeof(a[0]))
18
19/* these should all be in <algorithm>, but are missing... */
20namespace P4 {
21
22template <class C, class T>
23inline bool contains(C &c, const T &val) {
24 return std::find(c.begin(), c.end(), val) != c.end();
25}
26
27template <class C, class Pred>
28inline bool contains_if(C &c, Pred pred) {
29 return std::find_if(c.begin(), c.end(), pred) != c.end();
30}
31
32template <class C, class Pred>
33inline void erase_if(C &c, Pred pred) {
34 for (auto it = c.begin(); it != c.end();) {
35 if (pred(*it))
36 it = c.erase(it);
37 else
38 ++it;
39 }
40}
41
42template <class C, class Pred>
43inline void remove_if(C &c, Pred pred) {
44 c.erase(std::remove_if(c.begin(), c.end(), pred), c.end());
45}
46
47template <class C, class T>
48inline typename C::iterator find(C &c, const T &val) {
49 return std::find(c.begin(), c.end(), val);
50}
51
52using std::max_element;
53using std::min_element;
54
55template <class C>
56inline typename C::const_iterator min_element(const C &c) {
57 return min_element(c.begin(), c.end());
58}
59template <class C, class Compare>
60inline typename C::const_iterator min_element(const C &c, Compare comp) {
61 return min_element(c.begin(), c.end(), comp);
62}
63
64template <class C>
65inline typename C::const_iterator max_element(const C &c) {
66 return max_element(c.begin(), c.end());
67}
68template <class C, class Compare>
69inline typename C::const_iterator max_element(const C &c, Compare comp) {
70 return max_element(c.begin(), c.end(), comp);
71}
72
73template <class Iter, class Fn>
74inline Fn for_each(std::pair<Iter, Iter> range, Fn fn) {
75 return std::for_each(range.first, range.second, fn);
76}
77
78template <class Iter>
79Iter begin(std::pair<Iter, Iter> pr) {
80 return pr.first;
81}
82template <class Iter>
83Iter end(std::pair<Iter, Iter> pr) {
84 return pr.second;
85}
86
87} // namespace P4
88
89#endif /* LIB_ALGORITHM_H_ */
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13