2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 // $TOG: BitVector.C /main/4 1998/04/17 11:47:51 mgreess $
26 #include "BitVector.h"
28 #include "StyleSheetExceptions.h"
30 #define wordWithMSBSet (0x1 << (WORD_SIZE-1))
31 #define BIT_TEST(x, y) ( ((x) & (y)) == (y) )
32 #define RESET_BIT(x, y) x &= (~(y))
33 #define SET_BIT(x, y) x |= (y)
35 unsigned int posRecord::operator ==(const posRecord&)
37 MESSAGE(cerr, "posRecord::operator ==() should not be called");
38 throw(CASTBEEXCEPT badEvaluationException());
43 BitVector::BitVector(int bits, unsigned int initValue) :
44 f_bits(bits), f_words(bits/WORD_SIZE+1), f_positionArray(0)
46 f_array = new unsigned int[f_words];
47 setAllBitsTo(initValue);
50 void BitVector::setAllBitsTo(unsigned int initValue)
52 unsigned int fill = ( initValue == 0 ) ? 0x0 : ~0x0;
54 for ( unsigned int i=0; i<f_words; i++ )
58 BitVector::~BitVector()
61 delete f_positionArray;
64 void BitVector::setTo(BitVector& v)
66 if ( f_words != v.f_words ) {
68 f_array = new unsigned int[v.f_words];
73 for ( unsigned int i=0; i<f_words; i++ )
74 f_array[i]=v.f_array[i];
77 void BitVector::setBitTo(int i, unsigned int x)
79 unsigned int wordIndex = i / WORD_SIZE;
80 unsigned int bitIndex = i % WORD_SIZE;
83 if ( wordIndex < f_words - 1 )
84 SET_BIT(f_array[wordIndex], (0x1 << bitIndex));
86 SET_BIT(f_array[wordIndex],
87 (0x1 << (WORD_SIZE - f_bits % WORD_SIZE + bitIndex))
90 if ( wordIndex < f_words - 1 )
91 RESET_BIT(f_array[wordIndex], (0x1 << bitIndex));
93 RESET_BIT(f_array[wordIndex],
94 (0x1 << (WORD_SIZE - f_bits % WORD_SIZE + bitIndex))
99 void BitVector::recordPositions(unsigned int PTPos, unsigned int BITPos)
101 if ( f_positionArray == 0 )
102 f_positionArray = new positionArrayT;
104 f_positionArray -> append(posRecord(PTPos, BITPos));
107 unsigned int BitVector::getBit(int i)
109 unsigned int wordIndex = i / WORD_SIZE;
110 unsigned int bitIndex = i % WORD_SIZE;
112 if ( wordIndex < f_words - 1 )
113 return BIT_TEST((int)f_array[wordIndex], (0x1 << bitIndex)) ? 1 : 0;
115 return BIT_TEST((int)f_array[wordIndex],
116 (0x1 << (WORD_SIZE - f_bits % WORD_SIZE + bitIndex))
120 BitVector& BitVector::operator &=(BitVector& b)
122 for ( unsigned int i=0; i<f_words; i++ )
123 f_array[i] &= b.f_array[i];
128 BitVector& BitVector::operator ^=(BitVector& b)
130 for ( unsigned int i=0; i<f_words; i++ )
131 f_array[i] ^= b.f_array[i];
136 BitVector& BitVector::operator |=(BitVector& b)
138 for ( unsigned int i=0; i<f_words; i++ )
139 f_array[i] |= b.f_array[i];
144 BitVector& BitVector::shiftRightOneBit()
146 unsigned int msb = 0;
147 unsigned int lsb = 0;
149 for ( unsigned int i=0; i<f_words; i++ ) {
150 lsb = ( BIT_TEST(f_array[i], 0x1) ) ? 0x1 : 0x0;
151 f_array[i] = f_array[i] >> 1;
156 SET_BIT(f_array[0], wordWithMSBSet);
161 BitVector& BitVector::shiftLeftOneBit()
163 unsigned int msb = 0;
164 unsigned int lsb = 0;
167 for ( int i=f_words-1; i>=0; i++ ) {
168 msb = (BIT_TEST((int)f_array[i], wordWithMSBSet)) ? wordWithMSBSet : 0x0;
169 f_array[i] = f_array[i] << 1;
177 ostream& operator<<(ostream& out, BitVector& bv)
179 for ( int i=bv.f_bits-1; i>=0; i-- ) {
180 out << bv.getBit(i) ;
185 if ( bv.f_positionArray ) {
186 positionArrayIteratorT l_positionNext(*bv.f_positionArray);
187 while (++l_positionNext) {
188 x = l_positionNext.key();
189 out << x.pathTermPos << "." << x.bitPos << " ";