P4C
The P4 Compiler
Loading...
Searching...
No Matches
ltbitmatrix.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_LTBITMATRIX_H_
9#define LIB_LTBITMATRIX_H_
10
11#include "bitvec.h"
12
13namespace P4 {
14
15/* A lower-triangular bit matrix, held in a bit vector */
16class LTBitMatrix : private bitvec {
17 public:
18 nonconst_bitref operator()(unsigned r, unsigned c) {
19 return r >= c ? bitvec::operator[]((r * r + r) / 2 + c) : end();
20 }
21 bool operator()(unsigned r, unsigned c) const {
22 return r >= c ? bitvec::operator[]((r * r + r) / 2 + c) : false;
23 }
24 unsigned size() const {
25 if (empty()) return 0;
26 unsigned m = *max();
27 unsigned r = 1;
28 while ((r * r + r) / 2 <= m) r++;
29 return r;
30 }
31 using bitvec::clear;
32 using bitvec::empty;
33 using bitvec::operator bool;
34
35 private:
36 template <class T>
37 class rowref {
38 friend class LTBitMatrix;
39 T &self;
40 unsigned row;
41 rowref(T &s, unsigned r) : self(s), row(r) {}
42
43 public:
44 rowref(const rowref &) = default;
45 rowref(rowref &&) = default;
46 explicit operator bool() const {
47 if (row < bits_per_unit)
48 return self.getrange((row * row + row) / 2, row + 1) != 0;
49 else
50 return self.getslice((row * row + row) / 2, row + 1) ? true : false;
51 }
52 operator bitvec() const { return self.getslice((row * row + row) / 2, row + 1); }
53 };
54 class nonconst_rowref : public rowref<LTBitMatrix> {
55 public:
56 friend class LTBitMatrix;
57 using rowref<LTBitMatrix>::rowref;
58 void operator|=(bitvec a) const {
59 for (size_t v : a) {
60 if (v > row) break;
61 self(row, v) = 1;
62 }
63 }
64 nonconst_bitref operator[](unsigned col) const { return self(row, col); }
65 };
66 class const_rowref : public rowref<const LTBitMatrix> {
67 public:
68 friend class LTBitMatrix;
69 using rowref<const LTBitMatrix>::rowref;
70 bool operator[](unsigned col) const { return self(row, col); }
71 };
72
73 public:
74 nonconst_rowref operator[](unsigned r) { return nonconst_rowref(*this, r); }
75 const_rowref operator[](unsigned r) const { return const_rowref(*this, r); }
76
77 bool operator==(const LTBitMatrix &a) const { return bitvec::operator==(a); }
78 bool operator!=(const LTBitMatrix &a) const { return bitvec::operator!=(a); }
79 friend bool operator>>(const char *p, LTBitMatrix &bm);
80};
81
82inline std::ostream &operator<<(std::ostream &out, const LTBitMatrix &bm) {
83 for (unsigned i = 1; i < bm.size(); i++) {
84 if (i > 1) out << ' ';
85 for (unsigned j = 0; j < i; j++) out << (bm[i][j] ? '1' : '0');
86 }
87 return out;
88}
89
90inline bool operator>>(const char *p, LTBitMatrix &bm) {
91 bitvec rv;
92 for (int i = 0; *p; ++p, ++i) switch (*p) {
93 case ' ':
94 --i;
95 break;
96 case '0':
97 break;
98 case '1':
99 rv[i] = 1;
100 break;
101 default:
102 return false;
103 }
104 bm.bitvec::operator=(rv);
105 return true;
106}
107
108} // namespace P4
109
110#endif /* LIB_LTBITMATRIX_H_ */
Definition ltbitmatrix.h:16
Definition bitvec.h:166
Definition bitvec.h:111
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13