BitVector.C 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 libraries 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. #include "BitVector.h"
  25. #include "Debug.h"
  26. #include "StyleSheetExceptions.h"
  27. #define wordWithMSBSet (0x1 << (WORD_SIZE-1))
  28. #define BIT_TEST(x, y) ( ((x) & (y)) == (y) )
  29. #define RESET_BIT(x, y) x &= (~(y))
  30. #define SET_BIT(x, y) x |= (y)
  31. unsigned int posRecord::operator ==(const posRecord&)
  32. {
  33. MESSAGE(cerr, "posRecord::operator ==() should not be called");
  34. throw(CASTBEEXCEPT badEvaluationException());
  35. return 0;
  36. }
  37. BitVector::BitVector(int bits, unsigned int initValue) :
  38. f_bits(bits), f_words(bits/WORD_SIZE+1), f_positionArray(0)
  39. {
  40. f_array = new unsigned int[f_words];
  41. setAllBitsTo(initValue);
  42. }
  43. void BitVector::setAllBitsTo(unsigned int initValue)
  44. {
  45. unsigned int fill = ( initValue == 0 ) ? 0x0 : ~0x0;
  46. for ( unsigned int i=0; i<f_words; i++ )
  47. f_array[i]=fill;
  48. }
  49. BitVector::~BitVector()
  50. {
  51. delete [] f_array;
  52. delete f_positionArray;
  53. }
  54. void BitVector::setTo(BitVector& v)
  55. {
  56. if ( f_words != v.f_words ) {
  57. delete [] f_array;
  58. f_array = new unsigned int[v.f_words];
  59. }
  60. f_bits = v.f_bits;
  61. f_words = v.f_words;
  62. for ( unsigned int i=0; i<f_words; i++ )
  63. f_array[i]=v.f_array[i];
  64. }
  65. void BitVector::setBitTo(int i, unsigned int x)
  66. {
  67. unsigned int wordIndex = i / WORD_SIZE;
  68. unsigned int bitIndex = i % WORD_SIZE;
  69. if ( x == 1 ) {
  70. if ( wordIndex < f_words - 1 )
  71. SET_BIT(f_array[wordIndex], (0x1 << bitIndex));
  72. else
  73. SET_BIT(f_array[wordIndex],
  74. (0x1 << (WORD_SIZE - f_bits % WORD_SIZE + bitIndex))
  75. );
  76. } else {
  77. if ( wordIndex < f_words - 1 )
  78. RESET_BIT(f_array[wordIndex], (0x1 << bitIndex));
  79. else
  80. RESET_BIT(f_array[wordIndex],
  81. (0x1 << (WORD_SIZE - f_bits % WORD_SIZE + bitIndex))
  82. );
  83. }
  84. }
  85. void BitVector::recordPositions(unsigned int PTPos, unsigned int BITPos)
  86. {
  87. if ( f_positionArray == 0 )
  88. f_positionArray = new positionArrayT;
  89. f_positionArray -> append(posRecord(PTPos, BITPos));
  90. }
  91. unsigned int BitVector::getBit(int i)
  92. {
  93. unsigned int wordIndex = i / WORD_SIZE;
  94. unsigned int bitIndex = i % WORD_SIZE;
  95. if ( wordIndex < f_words - 1 )
  96. return BIT_TEST((int)f_array[wordIndex], (0x1 << bitIndex)) ? 1 : 0;
  97. else
  98. return BIT_TEST((int)f_array[wordIndex],
  99. (0x1 << (WORD_SIZE - f_bits % WORD_SIZE + bitIndex))
  100. ) ? 1 : 0;
  101. }
  102. BitVector& BitVector::operator &=(BitVector& b)
  103. {
  104. for ( unsigned int i=0; i<f_words; i++ )
  105. f_array[i] &= b.f_array[i];
  106. return *this;
  107. }
  108. BitVector& BitVector::operator ^=(BitVector& b)
  109. {
  110. for ( unsigned int i=0; i<f_words; i++ )
  111. f_array[i] ^= b.f_array[i];
  112. return *this;
  113. }
  114. BitVector& BitVector::operator |=(BitVector& b)
  115. {
  116. for ( unsigned int i=0; i<f_words; i++ )
  117. f_array[i] |= b.f_array[i];
  118. return *this;
  119. }
  120. BitVector& BitVector::shiftRightOneBit()
  121. {
  122. unsigned int msb = 0;
  123. unsigned int lsb = 0;
  124. for ( unsigned int i=0; i<f_words; i++ ) {
  125. lsb = ( BIT_TEST(f_array[i], 0x1) ) ? 0x1 : 0x0;
  126. f_array[i] = f_array[i] >> 1;
  127. f_array[i] |= msb;
  128. msb = lsb;
  129. }
  130. SET_BIT(f_array[0], wordWithMSBSet);
  131. return *this;
  132. }
  133. BitVector& BitVector::shiftLeftOneBit()
  134. {
  135. unsigned int msb = 0;
  136. unsigned int lsb = 0;
  137. for ( int i=f_words-1; i>=0; i++ ) {
  138. msb = (BIT_TEST((int)f_array[i], wordWithMSBSet)) ? wordWithMSBSet : 0x0;
  139. f_array[i] = f_array[i] << 1;
  140. f_array[i] |= lsb;
  141. lsb = msb;
  142. }
  143. return *this;
  144. }
  145. ostream& operator<<(ostream& out, BitVector& bv)
  146. {
  147. for ( int i=bv.f_bits-1; i>=0; i-- ) {
  148. out << bv.getBit(i) ;
  149. }
  150. out << "\n";
  151. posRecord x;
  152. if ( bv.f_positionArray ) {
  153. positionArrayIteratorT l_positionNext(*bv.f_positionArray);
  154. while (++l_positionNext) {
  155. x = l_positionNext.key();
  156. out << x.pathTermPos << "." << x.bitPos << " ";
  157. }
  158. }
  159. out << "\n";
  160. return out;
  161. }