Disable all code related to libXp
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / StyleSheet / BitVector.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
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)
10  * any later version.
11  *
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
16  * details.
17  *
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
22  */
23 // $TOG: BitVector.C /main/4 1998/04/17 11:47:51 mgreess $
24
25
26 #include "BitVector.h"
27 #include "Debug.h"
28 #include "StyleSheetExceptions.h"
29    
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)
34
35 unsigned int posRecord::operator ==(const posRecord&)
36 {
37    MESSAGE(cerr, "posRecord::operator ==() should not be called");
38    throw(CASTBEEXCEPT badEvaluationException());
39    return 0;
40 }
41
42
43 BitVector::BitVector(int bits, unsigned int initValue) :
44    f_bits(bits), f_words(bits/WORD_SIZE+1), f_positionArray(0)
45 {
46    f_array = new unsigned int[f_words];
47    setAllBitsTo(initValue);
48 }
49
50 void BitVector::setAllBitsTo(unsigned int initValue) 
51 {
52    unsigned int fill = ( initValue == 0 ) ? 0x0 : ~0x0;
53
54    for ( unsigned int i=0; i<f_words; i++ )
55      f_array[i]=fill;
56 }
57
58 BitVector::~BitVector()
59 {
60    delete f_array;
61    delete f_positionArray;
62 }
63
64 void BitVector::setTo(BitVector& v)
65 {
66    if ( f_words != v.f_words ) {
67      delete f_array;
68      f_array = new unsigned int[v.f_words];
69    }
70    f_bits = v.f_bits;
71    f_words = v.f_words;
72
73    for ( unsigned int i=0; i<f_words; i++ )
74      f_array[i]=v.f_array[i];
75 }
76
77 void BitVector::setBitTo(int i, unsigned int x)
78 {
79    unsigned int wordIndex = i / WORD_SIZE;
80    unsigned int bitIndex = i % WORD_SIZE;
81
82    if ( x == 1 ) {
83       if ( wordIndex < f_words - 1 ) 
84          SET_BIT(f_array[wordIndex], (0x1 << bitIndex));
85       else
86          SET_BIT(f_array[wordIndex], 
87                  (0x1 << (WORD_SIZE - f_bits % WORD_SIZE + bitIndex))
88                 );
89    } else {
90       if ( wordIndex < f_words - 1 ) 
91          RESET_BIT(f_array[wordIndex], (0x1 << bitIndex));
92       else
93          RESET_BIT(f_array[wordIndex], 
94                  (0x1 << (WORD_SIZE - f_bits % WORD_SIZE + bitIndex))
95                 );
96     }
97 }
98
99 void BitVector::recordPositions(unsigned int PTPos, unsigned int BITPos)
100 {
101    if ( f_positionArray == 0 )
102       f_positionArray = new positionArrayT;
103
104    f_positionArray -> append(posRecord(PTPos, BITPos));
105 }
106
107 unsigned int BitVector::getBit(int i)
108 {
109    unsigned int wordIndex = i / WORD_SIZE;
110    unsigned int bitIndex = i % WORD_SIZE;
111
112    if ( wordIndex < f_words - 1 ) 
113          return BIT_TEST((int)f_array[wordIndex], (0x1 << bitIndex)) ? 1 : 0;
114    else
115          return BIT_TEST((int)f_array[wordIndex],
116                  (0x1 << (WORD_SIZE - f_bits % WORD_SIZE + bitIndex))
117                         ) ? 1 : 0;
118 }
119
120 BitVector& BitVector::operator &=(BitVector& b)
121 {
122    for ( unsigned int i=0; i<f_words; i++ )
123      f_array[i] &= b.f_array[i];
124
125    return *this;
126 }
127
128 BitVector& BitVector::operator ^=(BitVector& b)
129 {
130    for ( unsigned int i=0; i<f_words; i++ )
131      f_array[i] ^= b.f_array[i];
132
133    return *this;
134 }
135
136 BitVector& BitVector::operator |=(BitVector& b)
137 {
138    for ( unsigned int i=0; i<f_words; i++ )
139      f_array[i] |= b.f_array[i];
140
141    return *this;
142 }
143
144 BitVector& BitVector::shiftRightOneBit()
145 {
146    unsigned int msb = 0;
147    unsigned int lsb = 0;
148
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;   
152      f_array[i] |= msb;
153      msb = lsb;
154    }
155
156    SET_BIT(f_array[0], wordWithMSBSet);
157
158    return *this;
159 }
160
161 BitVector& BitVector::shiftLeftOneBit()
162 {
163    unsigned int msb = 0;
164    unsigned int lsb = 0;
165
166
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;   
170      f_array[i] |= lsb;
171      lsb = msb;
172    }
173
174    return *this;
175 }
176
177 ostream& operator<<(ostream& out, BitVector& bv)
178 {
179    for ( int i=bv.f_bits-1; i>=0; i-- ) {
180       out << bv.getBit(i) ;
181    }
182    out << "\n";
183
184    posRecord x;
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 << " ";
190       }
191    }
192
193    out << "\n";
194
195    return out;
196 }