2 Copyright (c) 2002-2009 Tampere University.
4 This file is part of TTA-Based Codesign Environment (TCE).
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:
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
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.
27 * Inline implementation of BitMatrix class.
29 * @author Pekka Jääskeläinen 2007 (pekka.jaaskelainen-no.spam-tut.fi)
33 #include "BaseType.hh"
36 * Count of rows in the bit matrix.
38 * @return Count of rows.
41 BitMatrix::rowCount() const {
46 * Count of columns in the bit matrix.
48 * @return Count of columns.
51 BitMatrix::columnCount() const {
56 * Performs an AND operation between the matrices for all elements and
57 * returns true in case any result is nonzero.
59 * This is used to check for conflicts between two bit matrices, that is,
60 * to look for element in which both matrices have value '1'.
62 * @note Both matrices must be of same size. This is not checked by the
65 * @param another The bit matrix to AND with.
68 BitMatrix::conflictsWith(const BitMatrix& another) const {
69 for (int word = 0; word < rows_ * wordsPerRow_; ++word) {
71 matrix_[word] & another.matrix_[word];
79 * ORs the given matrix with this one.
81 * The resulting matrix has '1' in an element in case either the merged
82 * matrix or the original had '1' there.
84 * @param another The resource table of the merged.
87 BitMatrix::orWith(const BitMatrix& another) {
88 for (int word = 0; word < rows_ * wordsPerRow_; ++word) {
89 matrix_[word] |= another.matrix_[word];
94 * Sets the bit at given element.
96 * @param column The column.
98 * @param value The bit.
101 BitMatrix::setBit(int column, int row, bool value) {
103 const int wordPos = column / (sizeof(RowWord) * BYTE_BITWIDTH);
104 const int bitPos = column % (sizeof(RowWord) * BYTE_BITWIDTH);
105 const RowWord oldWord = matrix_[row * wordsPerRow_ + wordPos];
108 // set bit to 1 by ORing
109 RowWord mask = 1 << bitPos;
110 newWord = oldWord | mask;
112 // set the bit to 0 by ANDing with all ones except the bit to set
113 RowWord mask = 1 << bitPos;
115 newWord = oldWord & mask;
117 matrix_[row * wordsPerRow_ + wordPos] = newWord;
121 * Checks if the given bit is set.
123 * @param column The column.
124 * @param row The row.
125 * @return Value of the bit.
128 BitMatrix::bitAt(int column, int row) const {
130 const int wordPos = column / (sizeof(RowWord) * BYTE_BITWIDTH);
131 const int bitPos = column % (sizeof(RowWord) * BYTE_BITWIDTH);
132 const RowWord word = matrix_[row * wordsPerRow_ + wordPos];
133 // reset all bits except that one by ANDing
134 RowWord mask = 1 << bitPos;
135 return (word & mask) != 0;
139 * Shifts the matrix left one step.
142 BitMatrix::shiftLeft() {
144 const int shiftAmount = sizeof(RowWord)*BYTE_BITWIDTH - 1;
146 for (int col = 0; col < wordsPerRow_; ++col) {
147 for (int row = 0; row < rows_; ++row) {
148 RowWord word = matrix_[row*wordsPerRow_ + col];
150 // transfer the upper bit from the next column
151 if (col + 1 < wordsPerRow_) {
153 matrix_[row*wordsPerRow_ + col + 1] << shiftAmount;
156 matrix_[row*wordsPerRow_ + col] = word;