P4C
The P4 Compiler
Loading...
Searching...
No Matches
ltbitmatrix.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef LIB_LTBITMATRIX_H_
18#define LIB_LTBITMATRIX_H_
19
20#include "bitvec.h"
21
22namespace P4 {
23
24/* A lower-triangular bit matrix, held in a bit vector */
25class LTBitMatrix : private bitvec {
26 public:
27 nonconst_bitref operator()(unsigned r, unsigned c) {
28 return r >= c ? bitvec::operator[]((r * r + r) / 2 + c) : end();
29 }
30 bool operator()(unsigned r, unsigned c) const {
31 return r >= c ? bitvec::operator[]((r * r + r) / 2 + c) : false;
32 }
33 unsigned size() const {
34 if (empty()) return 0;
35 unsigned m = *max();
36 unsigned r = 1;
37 while ((r * r + r) / 2 <= m) r++;
38 return r;
39 }
40 using bitvec::clear;
41 using bitvec::empty;
42 using bitvec::operator bool;
43
44 private:
45 template <class T>
46 class rowref {
47 friend class LTBitMatrix;
48 T &self;
49 unsigned row;
50 rowref(T &s, unsigned r) : self(s), row(r) {}
51
52 public:
53 rowref(const rowref &) = default;
54 rowref(rowref &&) = default;
55 explicit operator bool() const {
56 if (row < bits_per_unit)
57 return self.getrange((row * row + row) / 2, row + 1) != 0;
58 else
59 return self.getslice((row * row + row) / 2, row + 1) ? true : false;
60 }
61 operator bitvec() const { return self.getslice((row * row + row) / 2, row + 1); }
62 };
63 class nonconst_rowref : public rowref<LTBitMatrix> {
64 public:
65 friend class LTBitMatrix;
66 using rowref<LTBitMatrix>::rowref;
67 void operator|=(bitvec a) const {
68 for (size_t v : a) {
69 if (v > row) break;
70 self(row, v) = 1;
71 }
72 }
73 nonconst_bitref operator[](unsigned col) const { return self(row, col); }
74 };
75 class const_rowref : public rowref<const LTBitMatrix> {
76 public:
77 friend class LTBitMatrix;
78 using rowref<const LTBitMatrix>::rowref;
79 bool operator[](unsigned col) const { return self(row, col); }
80 };
81
82 public:
83 nonconst_rowref operator[](unsigned r) { return nonconst_rowref(*this, r); }
84 const_rowref operator[](unsigned r) const { return const_rowref(*this, r); }
85
86 bool operator==(const LTBitMatrix &a) const { return bitvec::operator==(a); }
87 bool operator!=(const LTBitMatrix &a) const { return bitvec::operator!=(a); }
88 friend bool operator>>(const char *p, LTBitMatrix &bm);
89};
90
91inline std::ostream &operator<<(std::ostream &out, const LTBitMatrix &bm) {
92 for (unsigned i = 1; i < bm.size(); i++) {
93 if (i > 1) out << ' ';
94 for (unsigned j = 0; j < i; j++) out << (bm[i][j] ? '1' : '0');
95 }
96 return out;
97}
98
99inline bool operator>>(const char *p, LTBitMatrix &bm) {
100 bitvec rv;
101 for (int i = 0; *p; ++p, ++i) switch (*p) {
102 case ' ':
103 --i;
104 break;
105 case '0':
106 break;
107 case '1':
108 rv[i] = 1;
109 break;
110 default:
111 return false;
112 }
113 bm.bitvec::operator=(rv);
114 return true;
115}
116
117} // namespace P4
118
119#endif /* LIB_LTBITMATRIX_H_ */
Definition ltbitmatrix.h:25
Definition bitvec.h:175
Definition bitvec.h:120
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24