OpenASIP
2.0
src
tools
BitMatrix.cc
Go to the documentation of this file.
1
/*
2
Copyright (c) 2002-2009 Tampere University.
3
4
This file is part of TTA-Based Codesign Environment (TCE).
5
6
Permission is hereby granted, free of charge, to any person obtaining a
7
copy of this software and associated documentation files (the "Software"),
8
to deal in the Software without restriction, including without limitation
9
the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
and/or sell copies of the Software, and to permit persons to whom the
11
Software is furnished to do so, subject to the following conditions:
12
13
The above copyright notice and this permission notice shall be included in
14
all copies or substantial portions of the Software.
15
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
DEALINGS IN THE SOFTWARE.
23
*/
24
/**
25
* @file BitMatrix.cc
26
*
27
* Implementation of BitMatrix class.
28
*
29
* @author Pekka Jääskeläinen 2007 (pekka.jaaskelainen-no.spam-tut.fi)
30
* @note rating: red
31
*/
32
33
#include <sstream>
34
#include <cmath>
35
36
#include "
BitMatrix.hh
"
37
#include "
BaseType.hh
"
38
39
/**
40
* Builds a bit matrix of given size and default content.
41
*
42
* @param width The width of the matrix.
43
* @param height The height of the matrix.
44
* @param initialValue The initial value for elements.
45
*/
46
BitMatrix::BitMatrix
(
int
width,
int
height,
bool
initialValue) :
47
rows_(height), columns_(width), wordsPerRow_(
48
static_cast<int>(
49
std::ceil(columns_ / float(sizeof(
RowWord
) *
BYTE_BITWIDTH
)))) {
50
51
matrix_
=
new
RowWord
[
rows_
*
wordsPerRow_
];
52
53
RowWord
word = 0;
54
if
(initialValue) {
55
word = ~word;
56
}
57
for
(
int
row = 0; row <
rows_
; ++row) {
58
for
(
int
col = 0; col <
wordsPerRow_
; ++col) {
59
matrix_
[row *
wordsPerRow_
+ col] = word;
60
}
61
}
62
}
63
64
/**
65
* Frees the bit matrix content storage.
66
*/
67
BitMatrix::~BitMatrix
() {
68
delete
[]
matrix_
;
69
matrix_
= NULL;
70
}
71
72
/**
73
* Copy constructor.
74
*/
75
BitMatrix::BitMatrix
(
const
BitMatrix
& another) :
76
rows_(another.rows_), columns_(another.columns_),
77
wordsPerRow_(another.wordsPerRow_) {
78
79
matrix_
=
new
RowWord
[
rows_
*
wordsPerRow_
];
80
81
for
(
int
row = 0; row <
rows_
; ++row) {
82
for
(
int
col = 0; col <
wordsPerRow_
; ++col) {
83
matrix_
[row *
wordsPerRow_
+ col] =
84
another.
matrix_
[row *
wordsPerRow_
+ col];
85
}
86
}
87
}
88
89
/**
90
* Sets all bits to zero.
91
*/
92
void
93
BitMatrix::setAllToZero
() {
94
95
for
(
int
row = 0; row <
rows_
; ++row) {
96
for
(
int
col = 0; col <
wordsPerRow_
; ++col) {
97
matrix_
[row *
wordsPerRow_
+ col] = 0;
98
}
99
}
100
}
101
102
/**
103
* Sets all bits to one.
104
*/
105
void
106
BitMatrix::setAllToOne
() {
107
108
for
(
int
row = 0; row <
rows_
; ++row) {
109
for
(
int
col = 0; col <
wordsPerRow_
; ++col) {
110
matrix_
[row *
wordsPerRow_
+ col] = ~0;
111
}
112
}
113
}
114
115
116
/**
117
* ASCII representation of the bit matrix.
118
*
119
* For debugging and testing.
120
*
121
* @return A string representation of the matrix.
122
*/
123
std::string
124
BitMatrix::toString
()
const
{
125
std::ostringstream s;
126
for
(
int
row = 0; row <
rows_
; ++row) {
127
for
(
int
col = 0; col <
columns_
; ++col) {
128
s <<
bitAt
(col, row) <<
" "
;
129
}
130
s << std::endl;
131
}
132
return
s.str();
133
}
134
135
/**
136
* Compares two bit matrices..
137
*
138
* @param other The RT to compare to.
139
* @return True in case all values in both reservation tables are equal and
140
* the dimensions are the same.
141
*/
142
bool
143
BitMatrix::operator==
(
const
BitMatrix
& other)
const
{
144
145
if
(other.
rows_
!=
rows_
|| other.
columns_
!=
columns_
)
146
return
false
;
147
148
for
(
int
row = 0; row <
rows_
; ++row) {
149
for
(
int
col = 0; col <
columns_
; ++col) {
150
if
(other.
bitAt
(col, row) !=
bitAt
(col, row))
151
return
false
;
152
}
153
}
154
return
true
;
155
}
156
157
/**
158
* Returns a textual description of the matrix suitable to be used
159
* as a dot edge/node label.
160
*
161
* @return A string describing the matrix.
162
*/
163
std::string
164
BitMatrix::toDotString
()
const
{
165
166
std::string theString =
""
;
167
for
(
int
row = 0; row <
rows_
; ++row) {
168
for
(
int
column = 0; column <
columns_
;
169
++column) {
170
if
(
bitAt
(column, row))
171
theString +=
"1 "
;
172
else
173
theString +=
"0 "
;
174
}
175
theString +=
"\\n"
;
176
}
177
return
theString;
178
}
179
180
/**
181
* Returns true in case the given matrix is "smaller" than this one.
182
*
183
* The one that has the first non-equal non-zero matrix element is considered
184
* larger. That is, the matrix is considered as a bit string. This is an
185
* arbitrary ordering constraint to make it possible to store BitMatrices
186
* in sets.
187
*
188
* @param rightHand The right hand side of the comparison.
189
* @return True in case the the right hand is considered larger.
190
*/
191
bool
192
BitMatrix::operator<
(
const
BitMatrix
& rightHand)
const
{
193
194
for
(
int
row = 0; row <
rows_
; ++row) {
195
for
(
int
column = 0; column <
columns_
; ++column) {
196
bool
left =
bitAt
(column, row);
197
bool
right = rightHand.
bitAt
(column, row);
198
if
(left != right)
199
return
static_cast<
int
>
(left) <
static_cast<
int
>
(right);
200
}
201
}
202
// they are equal
203
return
false
;
204
}
BaseType.hh
BitMatrix
Definition:
BitMatrix.hh:49
BitMatrix::toDotString
std::string toDotString() const
Definition:
BitMatrix.cc:164
BitMatrix::toString
std::string toString() const
Definition:
BitMatrix.cc:124
BitMatrix::setAllToZero
void setAllToZero()
Definition:
BitMatrix.cc:93
BitMatrix::columns_
const int columns_
Definition:
BitMatrix.hh:79
BitMatrix::operator<
bool operator<(const BitMatrix &rightHand) const
Definition:
BitMatrix.cc:192
BitMatrix::operator==
bool operator==(const BitMatrix &other) const
Definition:
BitMatrix.cc:143
BitMatrix::wordsPerRow_
const int wordsPerRow_
Definition:
BitMatrix.hh:81
BitMatrix::rows_
const int rows_
Definition:
BitMatrix.hh:77
BitMatrix::BitMatrix
BitMatrix(int width, int height, bool initialValue)
Definition:
BitMatrix.cc:46
BitMatrix::~BitMatrix
virtual ~BitMatrix()
Definition:
BitMatrix.cc:67
BitMatrix::matrix_
RowWord * matrix_
Definition:
BitMatrix.hh:75
BitMatrix.hh
BYTE_BITWIDTH
const Byte BYTE_BITWIDTH
Definition:
BaseType.hh:136
BitMatrix::RowWord
unsigned RowWord
Definition:
BitMatrix.hh:73
BitMatrix::bitAt
bool bitAt(int column, int row) const
BitMatrix::setAllToOne
void setAllToOne()
Definition:
BitMatrix.cc:106
Generated by
1.8.17