00001 /************************************************************************** 00002 *** 00003 *** Copyright (c) 1995-2000 Regents of the University of California, 00004 *** Andrew E. Caldwell, Andrew B. Kahng and Igor L. Markov 00005 *** Copyright (c) 2000-2004 Regents of the University of Michigan, 00006 *** Saurabh N. Adya, Jarrod A. Roy and Igor L. Markov 00007 *** 00008 *** Contact author(s): abk@cs.ucsd.edu, imarkov@umich.edu 00009 *** Original Affiliation: UCLA, Computer Science Department, 00010 *** Los Angeles, CA 90095-1596 USA 00011 *** 00012 *** Permission is hereby granted, free of charge, to any person obtaining 00013 *** a copy of this software and associated documentation files (the 00014 *** "Software"), to deal in the Software without restriction, including 00015 *** without limitation 00016 *** the rights to use, copy, modify, merge, publish, distribute, sublicense, 00017 *** and/or sell copies of the Software, and to permit persons to whom the 00018 *** Software is furnished to do so, subject to the following conditions: 00019 *** 00020 *** The above copyright notice and this permission notice shall be included 00021 *** in all copies or substantial portions of the Software. 00022 *** 00023 *** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00024 *** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 00025 *** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00026 *** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 00027 *** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT 00028 *** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR 00029 *** THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00030 *** 00031 *** 00032 ***************************************************************************/ 00033 00034 00035 00036 00037 00038 00039 00040 00041 00042 00043 00044 00046 // 020811 ilm ported to g++ 3.0 00047 00048 #ifndef _BITBOARDP_H_ 00049 #define _BITBOARDP_H_ 00050 00051 #include <iostream> 00052 #include <ostream> 00053 00054 #include <vector> 00055 using namespace std; 00056 //#include "ABKCommon/abkassert.h" 00057 //#include "ABKCommon/abkio.h" 00058 //#include "ABKCommon/abkcommon.h" 00059 00060 //: bits can be set, and then all set bits can be erased in O(#bits) 00061 // time bits can not be unset one by one in O(1) time each (search required) 00062 // SAMPLE USAGE: mark visited nodes in graph traversals when 00063 // multiple traversals are performed 00064 namespace parquetfp 00065 { 00066 class BitBoard 00067 { 00068 std::vector<unsigned> _bitIndicesSet; 00069 vector< bool > _boardSpace; 00070 //bit_vector _boardSpace; 00071 00072 public: 00073 00074 BitBoard(unsigned size): _boardSpace(size,false) {} 00075 const std::vector<unsigned>& getIndicesOfSetBits() const 00076 { return _bitIndicesSet; } 00077 //To get the Indices of Bits which have been set to 1; 00078 const vector<bool>& getBoardSpace() const 00079 { return _boardSpace; } 00080 //To get the bit sequence; 00081 unsigned getSize() const { return _boardSpace.size(); } 00082 //To get the size of the bitBoard; 00083 unsigned getNumBitsSet() const { return _bitIndicesSet.size(); } 00084 //To get the number of bits which have been set to 1; 00085 bool isBitSet(unsigned idx) const 00086 //To tell whether the given bit is set to 1; 00087 { 00088 /* abkassert(idx<getSize()," Index out of range"); */ 00089 return _boardSpace[idx]; 00090 } 00091 void setBit (unsigned idx) 00092 //To set the given bit to 1; 00093 { 00094 /* abkassert(idx<getSize()," Index out of range"); */ 00095 if ( !_boardSpace[idx] ) 00096 { _boardSpace[idx]=true; _bitIndicesSet.push_back(idx); } 00097 } 00098 void reset() 00099 { 00100 _bitIndicesSet.clear(); 00101 std::fill(_boardSpace.begin(),_boardSpace.end(), false); 00102 } 00103 // Both functions reset and clear are to set all bits to 0; 00104 // difference is reset() takes O(size) time 00105 // but clear() takes O(# bits which are true) time 00106 00107 void clear() 00108 { 00109 for(std::vector<unsigned>::iterator it= _bitIndicesSet.begin(); 00110 it!=_bitIndicesSet.end(); it++) 00111 _boardSpace[*it]=false; 00112 _bitIndicesSet.clear(); 00113 } 00114 00115 void reset(unsigned newSize) 00116 { 00117 signed sizeDif = newSize-_boardSpace.size(); 00118 if (sizeDif<=0) clear(); 00119 else 00120 { 00121 _bitIndicesSet.clear(); 00122 _boardSpace.clear(); 00123 _boardSpace.insert(_boardSpace.end(),newSize,false); 00124 } 00125 } 00126 00127 friend ostream& operator<<(ostream& os, const BitBoard& bb); 00128 }; 00129 } 00130 00131 #endif