| 1 | /********************************************************************** | 
| 2 | *           Copyright (C) 2002-2003 by Gezelter's Group | 
| 3 | *This program is free software; you can redistribute it and/or modify | 
| 4 | *it under the terms of the GNU General Public License as published by | 
| 5 | *the Free Software Foundation version 2 of the License. | 
| 6 | * | 
| 7 | *This program is distributed in the hope that it will be useful, | 
| 8 | *but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 9 | *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
| 10 | *GNU General Public License for more details. | 
| 11 | * | 
| 12 | ************************************************************************ | 
| 13 | *Author: Teng Lin               Email:  tlin@nd.edu | 
| 14 | *Date: 08/13/2002               Version: 1.0 | 
| 15 | * | 
| 16 | ************************************************************************ | 
| 17 | *Description: | 
| 18 | * | 
| 19 | ***********************************************************************/ | 
| 20 | #include "bitvector.h" | 
| 21 | /*********************************************************************** | 
| 22 | * Class TBitVector | 
| 23 | ***********************************************************************/ | 
| 24 | // | 
| 25 | void TBitVector::Resize(int newSize) | 
| 26 | { | 
| 27 | if (newSize > Count()) | 
| 28 | { | 
| 29 | this->insert(this->end(), newSize-Count(), false); | 
| 30 | } | 
| 31 | else if (newSize < Count()) | 
| 32 | { | 
| 33 | this->erase(this->begin()+Count()-newSize, this->end()); | 
| 34 | } | 
| 35 | else | 
| 36 | { | 
| 37 | return; | 
| 38 | } | 
| 39 | } | 
| 40 |  | 
| 41 | bool TBitVector::IsEmpty() | 
| 42 | { | 
| 43 | return (Count() == 0); | 
| 44 | } | 
| 45 |  | 
| 46 | // bit operation | 
| 47 | void TBitVector::SetBitOn(int index) | 
| 48 | { | 
| 49 | (*this)[index] = true; | 
| 50 | } | 
| 51 |  | 
| 52 | void TBitVector::SetBitOff(int index) | 
| 53 | { | 
| 54 | (*this)[index] = false; | 
| 55 | } | 
| 56 |  | 
| 57 | void TBitVector::SetRangeOn(int begin, int end) | 
| 58 | { | 
| 59 | for(int i=begin; i<end; i++) | 
| 60 | SetBitOn(i); | 
| 61 | } | 
| 62 |  | 
| 63 |  | 
| 64 | void TBitVector::SetRangeOff(int begin, int end) | 
| 65 | { | 
| 66 | for(int i=begin; i<end; i++) | 
| 67 | SetBitOff(i); | 
| 68 | } | 
| 69 |  | 
| 70 | void TBitVector::SetAllOn() | 
| 71 | { | 
| 72 | SetRangeOn(0,Count()); | 
| 73 | } | 
| 74 |  | 
| 75 | void TBitVector::SetAllOff() | 
| 76 | { | 
| 77 | SetRangeOff(0,Count()); | 
| 78 | } | 
| 79 |  | 
| 80 | // | 
| 81 | bool TBitVector::IsBitOn(int index) | 
| 82 | { | 
| 83 | return (*this)[index]; | 
| 84 | } | 
| 85 |  | 
| 86 | // oveload operators | 
| 87 | TBitVector& TBitVector::operator =(const TBitVector &src) | 
| 88 | { | 
| 89 | int i; | 
| 90 | if (Count() < src.size()) | 
| 91 | { | 
| 92 | Resize(src.size()); | 
| 93 | } | 
| 94 |  | 
| 95 | for (i=0; i<src.size(); i++) | 
| 96 | { | 
| 97 | (*this)[i] = src[i]; | 
| 98 | } | 
| 99 |  | 
| 100 | for (; i<Count();i++) | 
| 101 | { | 
| 102 | (*this)[i] = false; | 
| 103 | } | 
| 104 |  | 
| 105 | return *this; | 
| 106 | } | 
| 107 |  | 
| 108 | TBitVector& TBitVector::operator &=(const TBitVector &src) | 
| 109 | { | 
| 110 | int i; | 
| 111 | if (Count() < src.size()) | 
| 112 | { | 
| 113 | Resize(src.size()); | 
| 114 | } | 
| 115 |  | 
| 116 | for (i=0; i<src.size(); i++) | 
| 117 | { | 
| 118 | (*this)[i] = (*this)[i] & src[i]; | 
| 119 | } | 
| 120 |  | 
| 121 | for (; i<Count();i++) | 
| 122 | { | 
| 123 | (*this)[i] = false; | 
| 124 | } | 
| 125 |  | 
| 126 | return *this; | 
| 127 | } | 
| 128 |  | 
| 129 | TBitVector& TBitVector::operator |=(const TBitVector &src) | 
| 130 | { | 
| 131 | int i; | 
| 132 | if (Count() < src.size()) | 
| 133 | { | 
| 134 | Resize(src.size()); | 
| 135 | } | 
| 136 |  | 
| 137 | for (i=0; i<src.size(); i++) | 
| 138 | { | 
| 139 | (*this)[i] = (*this)[i] | src[i]; | 
| 140 | } | 
| 141 |  | 
| 142 | return *this; | 
| 143 |  | 
| 144 | } | 
| 145 |  | 
| 146 | TBitVector& TBitVector::operator ^=(const TBitVector &src) | 
| 147 | { | 
| 148 | int i; | 
| 149 | if (Count() < src.size()) | 
| 150 | { | 
| 151 | Resize(src.size()); | 
| 152 | } | 
| 153 |  | 
| 154 | for (i=0; i<src.size(); i++) | 
| 155 | { | 
| 156 | (*this)[i] = (*this)[i] ^ src[i]; | 
| 157 | } | 
| 158 |  | 
| 159 | return *this; | 
| 160 | } | 
| 161 |  | 
| 162 | TBitVector& TBitVector::operator -=(const TBitVector &src) | 
| 163 | { | 
| 164 | TBitVector tmp_bv; | 
| 165 |  | 
| 166 | tmp_bv = (*this) ^ src; | 
| 167 | *this &= tmp_bv; | 
| 168 | return *this; | 
| 169 | } | 
| 170 |  | 
| 171 | TBitVector& TBitVector::operator |=(const int i) | 
| 172 | { | 
| 173 | SetBitOn(i); | 
| 174 | } | 
| 175 |  | 
| 176 | /*----------------------------------------------------------------------------*/ | 
| 177 | void TBitVector::FromIntVec(vector<int> &intVec) | 
| 178 | { | 
| 179 | int max; | 
| 180 | vector<int>::iterator i; | 
| 181 |  | 
| 182 |  | 
| 183 | for(i=intVec.begin(); i<intVec.end(); i++) | 
| 184 | { | 
| 185 | if (*i > max) | 
| 186 | max = *i; | 
| 187 | } | 
| 188 |  | 
| 189 | Resize(max); | 
| 190 | SetAllOff(); | 
| 191 |  | 
| 192 | for(i=intVec.begin(); i<intVec.end(); i++) | 
| 193 | { | 
| 194 | SetBitOn(*i); | 
| 195 | } | 
| 196 |  | 
| 197 | } | 
| 198 |  | 
| 199 | void TBitVector::ToIntvec(vector<int> &intVec) | 
| 200 | { | 
| 201 | intVec.clear(); | 
| 202 |  | 
| 203 | for(int i=0; i<Count(); i++) | 
| 204 | { | 
| 205 | if ((*this)[i]) | 
| 206 | intVec.push_back(i); | 
| 207 | } | 
| 208 | } | 
| 209 |  | 
| 210 | /*********************************************************************** | 
| 211 | * friend function of TBitVector | 
| 212 | ***********************************************************************/ | 
| 213 | TBitVector operator &(const TBitVector& bv1, const TBitVector& bv2) | 
| 214 | { | 
| 215 | TBitVector tmp_bv; | 
| 216 |  | 
| 217 | tmp_bv = bv1; | 
| 218 | tmp_bv &= bv2; | 
| 219 |  | 
| 220 | return tmp_bv; | 
| 221 | } | 
| 222 |  | 
| 223 | TBitVector operator |(const TBitVector& bv1, const TBitVector& bv2) | 
| 224 | { | 
| 225 | TBitVector tmp_bv; | 
| 226 |  | 
| 227 | tmp_bv = bv1; | 
| 228 | tmp_bv |= bv2; | 
| 229 |  | 
| 230 | return tmp_bv; | 
| 231 | } | 
| 232 |  | 
| 233 | TBitVector operator ^(const TBitVector& bv1, const TBitVector& bv2) | 
| 234 | { | 
| 235 | TBitVector tmp_bv; | 
| 236 |  | 
| 237 | tmp_bv = bv1; | 
| 238 | tmp_bv ^= bv2; | 
| 239 |  | 
| 240 | return tmp_bv; | 
| 241 | } | 
| 242 |  | 
| 243 | TBitVector operator -(const TBitVector& bv1, const TBitVector& bv2) | 
| 244 | { | 
| 245 | TBitVector tmp_bv; | 
| 246 |  | 
| 247 | tmp_bv = bv1 ^ bv2; | 
| 248 | tmp_bv &= bv1; | 
| 249 |  | 
| 250 | return tmp_bv; | 
| 251 |  | 
| 252 | } | 
| 253 |  | 
| 254 | bool operator ==(const TBitVector& bv1, const TBitVector& bv2) | 
| 255 | { | 
| 256 |  | 
| 257 | } |