| 1 | tim | 1961 | /* | 
| 2 |  |  | * Copyright (c) 2005 The University of Notre Dame. All Rights Reserved. | 
| 3 |  |  | * | 
| 4 |  |  | * The University of Notre Dame grants you ("Licensee") a | 
| 5 |  |  | * non-exclusive, royalty free, license to use, modify and | 
| 6 |  |  | * redistribute this software in source and binary code form, provided | 
| 7 |  |  | * that the following conditions are met: | 
| 8 |  |  | * | 
| 9 |  |  | * 1. Acknowledgement of the program authors must be made in any | 
| 10 |  |  | *    publication of scientific results based in part on use of the | 
| 11 |  |  | *    program.  An acceptable form of acknowledgement is citation of | 
| 12 |  |  | *    the article in which the program was described (Matthew | 
| 13 |  |  | *    A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher | 
| 14 |  |  | *    J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented | 
| 15 |  |  | *    Parallel Simulation Engine for Molecular Dynamics," | 
| 16 |  |  | *    J. Comput. Chem. 26, pp. 252-271 (2005)) | 
| 17 |  |  | * | 
| 18 |  |  | * 2. Redistributions of source code must retain the above copyright | 
| 19 |  |  | *    notice, this list of conditions and the following disclaimer. | 
| 20 |  |  | * | 
| 21 |  |  | * 3. Redistributions in binary form must reproduce the above copyright | 
| 22 |  |  | *    notice, this list of conditions and the following disclaimer in the | 
| 23 |  |  | *    documentation and/or other materials provided with the | 
| 24 |  |  | *    distribution. | 
| 25 |  |  | * | 
| 26 |  |  | * This software is provided "AS IS," without a warranty of any | 
| 27 |  |  | * kind. All express or implied conditions, representations and | 
| 28 |  |  | * warranties, including any implied warranty of merchantability, | 
| 29 |  |  | * fitness for a particular purpose or non-infringement, are hereby | 
| 30 |  |  | * excluded.  The University of Notre Dame and its licensors shall not | 
| 31 |  |  | * be liable for any damages suffered by licensee as a result of | 
| 32 |  |  | * using, modifying or distributing the software or its | 
| 33 |  |  | * derivatives. In no event will the University of Notre Dame or its | 
| 34 |  |  | * licensors be liable for any lost revenue, profit or data, or for | 
| 35 |  |  | * direct, indirect, special, consequential, incidental or punitive | 
| 36 |  |  | * damages, however caused and regardless of the theory of liability, | 
| 37 |  |  | * arising out of the use of or inability to use software, even if the | 
| 38 |  |  | * University of Notre Dame has been advised of the possibility of | 
| 39 |  |  | * such damages. | 
| 40 |  |  | */ | 
| 41 |  |  |  | 
| 42 |  |  | #include <algorithm> | 
| 43 | tim | 1962 | #include <cassert> | 
| 44 | tim | 1980 | #include <string> | 
| 45 | tim | 2008 |  | 
| 46 |  |  | #include "utils/BitSet.hpp" | 
| 47 |  |  | #include "utils/Algorithm.hpp" | 
| 48 |  |  |  | 
| 49 | tim | 1961 | namespace oopse { | 
| 50 |  |  | int BitSet::countBits() { | 
| 51 | tim | 1962 | return std::count(bitset_.begin(), bitset_.end(), true); | 
| 52 | tim | 1961 | } | 
| 53 |  |  |  | 
| 54 |  |  | void BitSet::flip(int fromIndex, int toIndex) { | 
| 55 |  |  | assert(fromIndex <= toIndex); | 
| 56 |  |  | assert(fromIndex >=0); | 
| 57 |  |  | assert(toIndex <= size()); | 
| 58 |  |  | std::vector<char>::iterator first = bitset_.begin() + fromIndex; | 
| 59 |  |  | std::vector<char>::iterator last = bitset_.begin() + toIndex; | 
| 60 |  |  |  | 
| 61 |  |  | std::transform(first, last, first, std::logical_not<bool>()); | 
| 62 |  |  |  | 
| 63 |  |  | } | 
| 64 |  |  |  | 
| 65 |  |  | BitSet BitSet::get(int fromIndex, int toIndex) { | 
| 66 |  |  | assert(fromIndex <= toIndex); | 
| 67 |  |  | assert(fromIndex >=0); | 
| 68 |  |  | assert(toIndex <= size()); | 
| 69 |  |  | std::vector<char>::iterator first = bitset_.begin() + fromIndex; | 
| 70 |  |  | std::vector<char>::iterator last = bitset_.begin() + toIndex; | 
| 71 |  |  |  | 
| 72 |  |  | BitSet result; | 
| 73 |  |  | std::copy(first, last, std::back_inserter(result.bitset_)); | 
| 74 |  |  | return result; | 
| 75 |  |  | } | 
| 76 |  |  |  | 
| 77 | tim | 1962 | bool BitSet::none() { | 
| 78 | tim | 1961 | std::vector<char>::iterator i = std::find(bitset_.begin(), bitset_.end(), true); | 
| 79 |  |  | return i == bitset_.end() ? true : false; | 
| 80 |  |  | } | 
| 81 |  |  |  | 
| 82 | tim | 1980 | int BitSet::nextOffBit(int fromIndex) const { | 
| 83 | tim | 1962 | ++fromIndex; | 
| 84 |  |  | while (fromIndex < size()) { | 
| 85 |  |  | if (!bitset_[fromIndex]) { | 
| 86 |  |  | return fromIndex; | 
| 87 |  |  | } | 
| 88 |  |  | ++fromIndex; | 
| 89 |  |  | } | 
| 90 | tim | 1961 |  | 
| 91 | tim | 1962 | return -1; | 
| 92 | tim | 1961 | } | 
| 93 |  |  |  | 
| 94 | tim | 1980 | int BitSet::nextOnBit(int fromIndex) const { | 
| 95 | tim | 1962 | ++fromIndex; | 
| 96 |  |  | while (fromIndex < size()) { | 
| 97 |  |  | if (bitset_[fromIndex]) { | 
| 98 |  |  | return fromIndex; | 
| 99 |  |  | } | 
| 100 |  |  | ++fromIndex; | 
| 101 |  |  | } | 
| 102 | tim | 1961 |  | 
| 103 | tim | 1962 | return -1; | 
| 104 |  |  | } | 
| 105 |  |  |  | 
| 106 |  |  | void BitSet::andOperator (const BitSet& bs) { | 
| 107 | tim | 1961 | assert(size() == bs.size()); | 
| 108 |  |  |  | 
| 109 |  |  | std::transform(bs.bitset_.begin(), bs.bitset_.end(), bitset_.begin(), bitset_.begin(), std::logical_and<bool>()); | 
| 110 |  |  | } | 
| 111 |  |  |  | 
| 112 | tim | 1962 | void BitSet::orOperator (const BitSet& bs) { | 
| 113 | tim | 1961 | assert(size() == bs.size()); | 
| 114 |  |  | std::transform(bs.bitset_.begin(), bs.bitset_.end(), bitset_.begin(), bitset_.begin(), std::logical_or<bool>()); | 
| 115 |  |  | } | 
| 116 |  |  |  | 
| 117 | tim | 1962 | void BitSet::xorOperator (const BitSet& bs) { | 
| 118 | tim | 1961 | assert(size() == bs.size()); | 
| 119 |  |  | std::transform(bs.bitset_.begin(), bs.bitset_.end(), bitset_.begin(), bitset_.begin(), oopse::logical_xor<bool>()); | 
| 120 |  |  | } | 
| 121 |  |  |  | 
| 122 |  |  | void BitSet::setBits(int fromIndex, int toIndex, bool value) { | 
| 123 |  |  | assert(fromIndex <= toIndex); | 
| 124 |  |  | assert(fromIndex >=0); | 
| 125 |  |  | assert(toIndex <= size()); | 
| 126 |  |  | std::vector<char>::iterator first = bitset_.begin() + fromIndex; | 
| 127 |  |  | std::vector<char>::iterator last = bitset_.begin() + toIndex; | 
| 128 |  |  | std::fill(first, last, value); | 
| 129 |  |  | } | 
| 130 |  |  |  | 
| 131 | tim | 1970 | void BitSet::resize(int nbits) { | 
| 132 |  |  | int oldSize = size(); | 
| 133 |  |  | bitset_.resize(nbits); | 
| 134 |  |  | if (nbits > oldSize) { | 
| 135 |  |  | std::fill(bitset_.begin()+oldSize, bitset_.begin()+nbits+1, false); | 
| 136 |  |  | } | 
| 137 |  |  | } | 
| 138 |  |  |  | 
| 139 | tim | 1962 | BitSet operator| (const BitSet& bs1, const BitSet& bs2) { | 
| 140 | tim | 1961 | assert(bs1.size() == bs2.size()); | 
| 141 |  |  |  | 
| 142 |  |  | BitSet result(bs1); | 
| 143 |  |  | result |= bs2; | 
| 144 |  |  | return result; | 
| 145 |  |  | } | 
| 146 |  |  |  | 
| 147 | tim | 1962 | BitSet operator& (const BitSet& bs1, const BitSet& bs2) { | 
| 148 | tim | 1961 | assert(bs1.size() == bs2.size()); | 
| 149 |  |  |  | 
| 150 |  |  | BitSet result(bs1); | 
| 151 |  |  | result &= bs2; | 
| 152 |  |  | return result; | 
| 153 |  |  | } | 
| 154 |  |  |  | 
| 155 | tim | 1962 | BitSet operator^ (const BitSet& bs1, const BitSet& bs2) { | 
| 156 | tim | 1961 | assert(bs1.size() == bs2.size()); | 
| 157 |  |  |  | 
| 158 |  |  | BitSet result(bs1); | 
| 159 |  |  | result ^= bs2; | 
| 160 |  |  | return result; | 
| 161 |  |  | } | 
| 162 |  |  |  | 
| 163 |  |  | bool operator== (const BitSet & bs1, const BitSet &bs2) { | 
| 164 |  |  | assert(bs1.size() == bs2.size()); | 
| 165 |  |  | return std::equal(bs1.bitset_.begin(), bs1.bitset_.end(), bs2.bitset_.begin()); | 
| 166 |  |  | } | 
| 167 |  |  |  | 
| 168 | tim | 1962 | std::istream& operator>> ( std::istream& is, const BitSet& bs) { | 
| 169 | tim | 1961 |  | 
| 170 | tim | 1962 | return is; | 
| 171 | tim | 1961 | } | 
| 172 |  |  |  | 
| 173 |  |  | std::ostream& operator<< ( std::ostream& os, const BitSet& bs) { | 
| 174 | tim | 1980 | for (int i = 0; i < bs.bitset_.size(); ++i) { | 
| 175 |  |  | std::string val = bs[i] ? "true" : "false"; | 
| 176 |  |  | os << "BitSet[" << i <<"] = " << val << std::endl; | 
| 177 |  |  | } | 
| 178 |  |  |  | 
| 179 | tim | 1962 | return os; | 
| 180 | tim | 1961 | } | 
| 181 |  |  |  | 
| 182 |  |  | } |