| 47 | 
  | 
#include "utils/Algorithm.hpp" | 
| 48 | 
  | 
 | 
| 49 | 
  | 
namespace oopse { | 
| 50 | 
< | 
int BitSet::countBits() { | 
| 50 | 
> | 
  int BitSet::countBits() { | 
| 51 | 
  | 
#ifdef __RWSTD     | 
| 52 | 
  | 
    //For the compiler(Sun, MSVC6.0) binding with RougeWave STL Library, we need to use old-style | 
| 53 | 
  | 
    // std::count which is error-prone. | 
| 57 | 
  | 
#else | 
| 58 | 
  | 
    return std::count(bitset_.begin(), bitset_.end(), true); | 
| 59 | 
  | 
#endif | 
| 60 | 
< | 
} | 
| 60 | 
> | 
  } | 
| 61 | 
  | 
 | 
| 62 | 
< | 
void BitSet::flip(int fromIndex, int toIndex) { | 
| 62 | 
> | 
  void BitSet::flip(int fromIndex, int toIndex) { | 
| 63 | 
  | 
    assert(fromIndex <= toIndex); | 
| 64 | 
  | 
    assert(fromIndex >=0); | 
| 65 | 
  | 
    assert(toIndex <= size()); | 
| 68 | 
  | 
 | 
| 69 | 
  | 
    std::transform(first, last, first, std::logical_not<bool>()); | 
| 70 | 
  | 
         | 
| 71 | 
< | 
} | 
| 71 | 
> | 
  } | 
| 72 | 
  | 
 | 
| 73 | 
< | 
BitSet BitSet::get(int fromIndex, int toIndex) { | 
| 73 | 
> | 
  BitSet BitSet::get(int fromIndex, int toIndex) { | 
| 74 | 
  | 
    assert(fromIndex <= toIndex); | 
| 75 | 
  | 
    assert(fromIndex >=0); | 
| 76 | 
  | 
    assert(toIndex <= size()); | 
| 80 | 
  | 
    BitSet result; | 
| 81 | 
  | 
    std::copy(first, last, std::back_inserter(result.bitset_)); | 
| 82 | 
  | 
    return result; | 
| 83 | 
< | 
} | 
| 83 | 
> | 
  } | 
| 84 | 
  | 
 | 
| 85 | 
< | 
bool BitSet::none() { | 
| 85 | 
> | 
  bool BitSet::none() { | 
| 86 | 
  | 
    std::vector<char>::iterator i = std::find(bitset_.begin(), bitset_.end(), true); | 
| 87 | 
  | 
    return i == bitset_.end() ? true : false; | 
| 88 | 
< | 
} | 
| 88 | 
> | 
  } | 
| 89 | 
  | 
     | 
| 90 | 
< | 
int BitSet::nextOffBit(int fromIndex) const { | 
| 90 | 
> | 
  int BitSet::nextOffBit(int fromIndex) const { | 
| 91 | 
  | 
    if (fromIndex <= -1) { | 
| 92 | 
< | 
        //in case -1 or other negative number is passed to this function | 
| 93 | 
< | 
        return -1; | 
| 92 | 
> | 
      //in case -1 or other negative number is passed to this function | 
| 93 | 
> | 
      return -1; | 
| 94 | 
  | 
    } | 
| 95 | 
  | 
     | 
| 96 | 
  | 
    ++fromIndex; | 
| 97 | 
  | 
    while (fromIndex < size()) { | 
| 98 | 
< | 
        if (!bitset_[fromIndex]) { | 
| 99 | 
< | 
            return fromIndex; | 
| 100 | 
< | 
        } | 
| 101 | 
< | 
        ++fromIndex; | 
| 98 | 
> | 
      if (!bitset_[fromIndex]) { | 
| 99 | 
> | 
        return fromIndex; | 
| 100 | 
> | 
      } | 
| 101 | 
> | 
      ++fromIndex; | 
| 102 | 
  | 
    } | 
| 103 | 
  | 
 | 
| 104 | 
  | 
    return -1; | 
| 105 | 
< | 
} | 
| 105 | 
> | 
  } | 
| 106 | 
  | 
 | 
| 107 | 
< | 
int BitSet::nextOnBit(int fromIndex) const { | 
| 107 | 
> | 
  int BitSet::nextOnBit(int fromIndex) const { | 
| 108 | 
  | 
    if (fromIndex <= -1) { | 
| 109 | 
< | 
        //in case -1 or other negative number is passed to this function | 
| 110 | 
< | 
        return -1; | 
| 109 | 
> | 
      //in case -1 or other negative number is passed to this function | 
| 110 | 
> | 
      return -1; | 
| 111 | 
  | 
    } | 
| 112 | 
  | 
 | 
| 113 | 
  | 
    ++fromIndex; | 
| 114 | 
  | 
    while (fromIndex < size()) { | 
| 115 | 
< | 
        if (bitset_[fromIndex]) { | 
| 116 | 
< | 
            return fromIndex; | 
| 117 | 
< | 
        } | 
| 118 | 
< | 
        ++fromIndex; | 
| 115 | 
> | 
      if (bitset_[fromIndex]) { | 
| 116 | 
> | 
        return fromIndex; | 
| 117 | 
> | 
      } | 
| 118 | 
> | 
      ++fromIndex; | 
| 119 | 
  | 
    } | 
| 120 | 
  | 
 | 
| 121 | 
  | 
    return -1; | 
| 122 | 
< | 
} | 
| 122 | 
> | 
  } | 
| 123 | 
  | 
 | 
| 124 | 
< | 
void BitSet::andOperator (const BitSet& bs) { | 
| 124 | 
> | 
  void BitSet::andOperator (const BitSet& bs) { | 
| 125 | 
  | 
    assert(size() == bs.size()); | 
| 126 | 
  | 
 | 
| 127 | 
  | 
    std::transform(bs.bitset_.begin(), bs.bitset_.end(), bitset_.begin(), bitset_.begin(), std::logical_and<bool>()); | 
| 128 | 
< | 
} | 
| 128 | 
> | 
  } | 
| 129 | 
  | 
 | 
| 130 | 
< | 
void BitSet::orOperator (const BitSet& bs) { | 
| 130 | 
> | 
  void BitSet::orOperator (const BitSet& bs) { | 
| 131 | 
  | 
    assert(size() == bs.size()); | 
| 132 | 
  | 
    std::transform(bs.bitset_.begin(), bs.bitset_.end(), bitset_.begin(), bitset_.begin(), std::logical_or<bool>());     | 
| 133 | 
< | 
} | 
| 133 | 
> | 
  } | 
| 134 | 
  | 
 | 
| 135 | 
< | 
void BitSet::xorOperator (const BitSet& bs) { | 
| 135 | 
> | 
  void BitSet::xorOperator (const BitSet& bs) { | 
| 136 | 
  | 
    assert(size() == bs.size()); | 
| 137 | 
  | 
    std::transform(bs.bitset_.begin(), bs.bitset_.end(), bitset_.begin(), bitset_.begin(), oopse::logical_xor<bool>());         | 
| 138 | 
< | 
} | 
| 138 | 
> | 
  } | 
| 139 | 
  | 
    | 
| 140 | 
< | 
void BitSet::setBits(int fromIndex, int toIndex, bool value) { | 
| 140 | 
> | 
  void BitSet::setBits(int fromIndex, int toIndex, bool value) { | 
| 141 | 
  | 
    assert(fromIndex <= toIndex); | 
| 142 | 
  | 
    assert(fromIndex >=0); | 
| 143 | 
  | 
    assert(toIndex <= size()); | 
| 144 | 
  | 
    std::vector<char>::iterator first = bitset_.begin() + fromIndex; | 
| 145 | 
  | 
    std::vector<char>::iterator last = bitset_.begin() + toIndex; | 
| 146 | 
  | 
    std::fill(first, last, value); | 
| 147 | 
< | 
} | 
| 147 | 
> | 
  } | 
| 148 | 
  | 
 | 
| 149 | 
< | 
void BitSet::resize(int nbits) { | 
| 149 | 
> | 
  void BitSet::resize(int nbits) { | 
| 150 | 
  | 
    int oldSize = size(); | 
| 151 | 
  | 
    bitset_.resize(nbits); | 
| 152 | 
  | 
    if (nbits > oldSize) { | 
| 153 | 
< | 
        std::fill(bitset_.begin()+oldSize, bitset_.end(), false); | 
| 153 | 
> | 
      std::fill(bitset_.begin()+oldSize, bitset_.end(), false); | 
| 154 | 
  | 
    } | 
| 155 | 
< | 
} | 
| 155 | 
> | 
  } | 
| 156 | 
  | 
 | 
| 157 | 
< | 
BitSet operator| (const BitSet& bs1, const BitSet& bs2) { | 
| 157 | 
> | 
  BitSet operator| (const BitSet& bs1, const BitSet& bs2) { | 
| 158 | 
  | 
    assert(bs1.size() == bs2.size()); | 
| 159 | 
  | 
 | 
| 160 | 
  | 
    BitSet result(bs1); | 
| 161 | 
  | 
    result |= bs2; | 
| 162 | 
  | 
    return result; | 
| 163 | 
< | 
} | 
| 163 | 
> | 
  } | 
| 164 | 
  | 
 | 
| 165 | 
< | 
BitSet operator& (const BitSet& bs1, const BitSet& bs2) { | 
| 165 | 
> | 
  BitSet operator& (const BitSet& bs1, const BitSet& bs2) { | 
| 166 | 
  | 
    assert(bs1.size() == bs2.size()); | 
| 167 | 
  | 
 | 
| 168 | 
  | 
    BitSet result(bs1); | 
| 169 | 
  | 
    result &= bs2; | 
| 170 | 
  | 
    return result; | 
| 171 | 
< | 
} | 
| 171 | 
> | 
  } | 
| 172 | 
  | 
 | 
| 173 | 
< | 
BitSet operator^ (const BitSet& bs1, const BitSet& bs2) { | 
| 173 | 
> | 
  BitSet operator^ (const BitSet& bs1, const BitSet& bs2) { | 
| 174 | 
  | 
    assert(bs1.size() == bs2.size()); | 
| 175 | 
  | 
 | 
| 176 | 
  | 
    BitSet result(bs1); | 
| 177 | 
  | 
    result ^= bs2; | 
| 178 | 
  | 
    return result; | 
| 179 | 
< | 
} | 
| 179 | 
> | 
  } | 
| 180 | 
  | 
 | 
| 181 | 
< | 
BitSet operator- (const BitSet& bs1, const BitSet& bs2) { | 
| 181 | 
> | 
  BitSet operator- (const BitSet& bs1, const BitSet& bs2) { | 
| 182 | 
  | 
    assert(bs1.size() == bs2.size()); | 
| 183 | 
  | 
 | 
| 184 | 
  | 
    BitSet result(bs1); | 
| 185 | 
  | 
    result -= bs2; | 
| 186 | 
  | 
    return result; | 
| 187 | 
< | 
} | 
| 187 | 
> | 
  } | 
| 188 | 
  | 
 | 
| 189 | 
< | 
bool operator== (const BitSet & bs1, const BitSet &bs2) { | 
| 189 | 
> | 
  bool operator== (const BitSet & bs1, const BitSet &bs2) { | 
| 190 | 
  | 
    assert(bs1.size() == bs2.size()); | 
| 191 | 
  | 
    return std::equal(bs1.bitset_.begin(), bs1.bitset_.end(), bs2.bitset_.begin()); | 
| 192 | 
< | 
} | 
| 192 | 
> | 
  } | 
| 193 | 
  | 
 | 
| 194 | 
< | 
//std::istream& operator>> ( std::istream& is, const BitSet& bs) { | 
| 195 | 
< | 
// | 
| 196 | 
< | 
//    return is; | 
| 197 | 
< | 
//} | 
| 194 | 
> | 
  //std::istream& operator>> ( std::istream& is, const BitSet& bs) { | 
| 195 | 
> | 
  // | 
| 196 | 
> | 
  //    return is; | 
| 197 | 
> | 
  //} | 
| 198 | 
  | 
 | 
| 199 | 
< | 
std::ostream& operator<< ( std::ostream& os, const BitSet& bs) { | 
| 199 | 
> | 
  std::ostream& operator<< ( std::ostream& os, const BitSet& bs) { | 
| 200 | 
  | 
    for (int i = 0; i < bs.bitset_.size(); ++i) { | 
| 201 | 
< | 
        std::string val = bs[i] ? "true" : "false"; | 
| 202 | 
< | 
        os << "BitSet[" << i <<"] = " << val << std::endl;  | 
| 201 | 
> | 
      std::string val = bs[i] ? "true" : "false"; | 
| 202 | 
> | 
      os << "BitSet[" << i <<"] = " << val << std::endl;  | 
| 203 | 
  | 
    } | 
| 204 | 
  | 
     | 
| 205 | 
  | 
    return os; | 
| 206 | 
< | 
} | 
| 206 | 
> | 
  } | 
| 207 | 
  | 
 | 
| 208 | 
  | 
} |