| 1 | gezelter | 1390 | /* | 
| 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. Redistributions of source code must retain the above copyright | 
| 10 |  |  | *    notice, this list of conditions and the following disclaimer. | 
| 11 |  |  | * | 
| 12 |  |  | * 2. Redistributions in binary form must reproduce the above copyright | 
| 13 |  |  | *    notice, this list of conditions and the following disclaimer in the | 
| 14 |  |  | *    documentation and/or other materials provided with the | 
| 15 |  |  | *    distribution. | 
| 16 |  |  | * | 
| 17 |  |  | * This software is provided "AS IS," without a warranty of any | 
| 18 |  |  | * kind. All express or implied conditions, representations and | 
| 19 |  |  | * warranties, including any implied warranty of merchantability, | 
| 20 |  |  | * fitness for a particular purpose or non-infringement, are hereby | 
| 21 |  |  | * excluded.  The University of Notre Dame and its licensors shall not | 
| 22 |  |  | * be liable for any damages suffered by licensee as a result of | 
| 23 |  |  | * using, modifying or distributing the software or its | 
| 24 |  |  | * derivatives. In no event will the University of Notre Dame or its | 
| 25 |  |  | * licensors be liable for any lost revenue, profit or data, or for | 
| 26 |  |  | * direct, indirect, special, consequential, incidental or punitive | 
| 27 |  |  | * damages, however caused and regardless of the theory of liability, | 
| 28 |  |  | * arising out of the use of or inability to use software, even if the | 
| 29 |  |  | * University of Notre Dame has been advised of the possibility of | 
| 30 |  |  | * such damages. | 
| 31 |  |  | * | 
| 32 |  |  | * SUPPORT OPEN SCIENCE!  If you use OpenMD or its source code in your | 
| 33 |  |  | * research, please cite the appropriate papers when you publish your | 
| 34 |  |  | * work.  Good starting points are: | 
| 35 |  |  | * | 
| 36 |  |  | * [1]  Meineke, et al., J. Comp. Chem. 26, 252-271 (2005). | 
| 37 |  |  | * [2]  Fennell & Gezelter, J. Chem. Phys. 124, 234104 (2006). | 
| 38 | gezelter | 1879 | * [3]  Sun, Lin & Gezelter, J. Chem. Phys. 128, 234107 (2008). | 
| 39 | gezelter | 1782 | * [4]  Kuang & Gezelter,  J. Chem. Phys. 133, 164101 (2010). | 
| 40 |  |  | * [5]  Vardeman, Stocker & Gezelter, J. Chem. Theory Comput. 7, 834 (2011). | 
| 41 | gezelter | 1390 | */ | 
| 42 |  |  |  | 
| 43 |  |  | #ifndef UTILS_OPENMDBITSET_HPP | 
| 44 |  |  | #define UTILS_OPENMDBITSET_HPP | 
| 45 |  |  |  | 
| 46 |  |  | #include <iostream> | 
| 47 |  |  | #include <vector> | 
| 48 |  |  | namespace OpenMD { | 
| 49 |  |  |  | 
| 50 |  |  | /** | 
| 51 |  |  | * @class OpenMDBitSet OpenMDBitSet.hpp "OpenMDBitSet.hpp" | 
| 52 | gezelter | 1782 | * @brief OpenMDBitSet is a wrapper class of std::vector<bool> to act as a growable std::bitset | 
| 53 | gezelter | 1390 | */ | 
| 54 |  |  | class OpenMDBitSet { | 
| 55 |  |  | public: | 
| 56 |  |  | /** */ | 
| 57 |  |  | OpenMDBitSet() {} | 
| 58 |  |  | /** */ | 
| 59 |  |  | OpenMDBitSet(int nbits) : bitset_(nbits) {clearAll(); } | 
| 60 |  |  |  | 
| 61 |  |  | /** Returns the number of bits set to true in this OpenMDBitSet.  */ | 
| 62 |  |  | int countBits(); | 
| 63 |  |  |  | 
| 64 |  |  | /** Sets the bit at the specified index to to the complement of its current value. */ | 
| 65 |  |  | void flip(int bitIndex) {  bitset_[bitIndex] = !bitset_[bitIndex];  } | 
| 66 |  |  |  | 
| 67 |  |  | /** Sets each bit from the specified fromIndex(inclusive) to the specified toIndex(exclusive) to the complement of its current value. */ | 
| 68 |  |  | void flip(int fromIndex, int toIndex); | 
| 69 |  |  |  | 
| 70 |  |  | /** Sets each bit to the complement of its current value. */ | 
| 71 |  |  | void flip() { flip(0, size()); } | 
| 72 |  |  |  | 
| 73 |  |  | /** Returns the value of the bit with the specified index. */ | 
| 74 |  |  | bool get(int bitIndex) {  return bitset_[bitIndex];  } | 
| 75 |  |  |  | 
| 76 |  |  | /** Returns a new OpenMDBitSet composed of bits from this OpenMDBitSet from fromIndex(inclusive) to toIndex(exclusive). */ | 
| 77 |  |  | OpenMDBitSet get(int fromIndex, int toIndex); | 
| 78 |  |  |  | 
| 79 |  |  | /** Returns true if any bits are set to true */ | 
| 80 |  |  | bool any() {return !none(); } | 
| 81 |  |  |  | 
| 82 |  |  | /** Returns true if no bits are set to true */ | 
| 83 |  |  | bool none(); | 
| 84 |  |  |  | 
| 85 |  |  | int firstOffBit() const { return !bitset_[0] ? 0 : nextOffBit(0); } | 
| 86 |  |  |  | 
| 87 |  |  | /** Returns the index of the first bit that is set to false that occurs on or after the specified starting index.*/ | 
| 88 |  |  | int nextOffBit(int fromIndex) const; | 
| 89 |  |  |  | 
| 90 |  |  | int firstOnBit() const { return bitset_[0] ? 0 : nextOnBit(0); } | 
| 91 |  |  |  | 
| 92 |  |  | /** Returns the index of the first bit that is set to true that occurs on or after the specified starting index. */ | 
| 93 |  |  | int nextOnBit(int fromIndex) const; | 
| 94 |  |  |  | 
| 95 |  |  | /** Performs a logical AND of this target bit set with the argument bit set. */ | 
| 96 |  |  | void andOperator (const OpenMDBitSet& bs); | 
| 97 |  |  |  | 
| 98 |  |  | /** Performs a logical OR of this bit set with the bit set argument. */ | 
| 99 |  |  | void orOperator (const OpenMDBitSet& bs); | 
| 100 |  |  |  | 
| 101 |  |  | /** Performs a logical XOR of this bit set with the bit set argument. */ | 
| 102 |  |  | void xorOperator (const OpenMDBitSet& bs); | 
| 103 |  |  |  | 
| 104 |  |  | void setBitOn(int bitIndex) {  setBit(bitIndex, true);  } | 
| 105 |  |  |  | 
| 106 |  |  | void setBitOff(int bitIndex) {  setBit(bitIndex, false);  } | 
| 107 |  |  |  | 
| 108 |  |  | void setRangeOn(int fromIndex, int toIndex) {  setBits(fromIndex, toIndex, true);  } | 
| 109 |  |  |  | 
| 110 |  |  | void setRangeOff(int fromIndex, int toIndex) {  setBits(fromIndex, toIndex, false);  } | 
| 111 |  |  |  | 
| 112 |  |  | /** Sets all of the bits in this OpenMDBitSet to false. */ | 
| 113 |  |  | void clearAll() {  setRangeOff(0, size());  } | 
| 114 |  |  |  | 
| 115 |  |  | void setAll() {  setRangeOn(0, size());  } | 
| 116 |  |  |  | 
| 117 |  |  | /** Returns the number of bits of space actually in use by this OpenMDBitSet to represent bit values. */ | 
| 118 |  |  | int size() const {  return bitset_.size();  } | 
| 119 |  |  |  | 
| 120 |  |  | /** Changes the size of OpenMDBitSet*/ | 
| 121 |  |  | void resize(int nbits); | 
| 122 |  |  |  | 
| 123 |  |  | OpenMDBitSet& operator&= (const OpenMDBitSet &bs) {  andOperator (bs); return *this; } | 
| 124 |  |  | OpenMDBitSet& operator|= (const OpenMDBitSet &bs) { orOperator (bs); return *this; } | 
| 125 |  |  | OpenMDBitSet& operator^= (const OpenMDBitSet &bs) { xorOperator (bs); return *this; } | 
| 126 |  |  | OpenMDBitSet& operator-= (const OpenMDBitSet &bs) { | 
| 127 |  |  | OpenMDBitSet tmp = *this ^ bs; | 
| 128 |  |  | *this &= tmp; | 
| 129 |  |  | return *this; | 
| 130 |  |  | } | 
| 131 | gezelter | 1802 |  | 
| 132 |  |  | OpenMDBitSet parallelReduce(); | 
| 133 | gezelter | 1390 |  | 
| 134 |  |  | bool operator[] (int bitIndex)  const {  return bitset_[bitIndex];  } | 
| 135 |  |  | friend OpenMDBitSet operator| (const OpenMDBitSet& bs1, const OpenMDBitSet& bs2); | 
| 136 |  |  | friend OpenMDBitSet operator& (const OpenMDBitSet& bs1, const OpenMDBitSet& bs2); | 
| 137 |  |  | friend OpenMDBitSet operator^ (const OpenMDBitSet& bs1, const OpenMDBitSet& bs2); | 
| 138 |  |  | friend OpenMDBitSet operator- (const OpenMDBitSet& bs1, const OpenMDBitSet& bs2); | 
| 139 |  |  |  | 
| 140 |  |  | friend bool operator== (const OpenMDBitSet & bs1, const OpenMDBitSet &bs2); | 
| 141 |  |  |  | 
| 142 |  |  | //friend std::istream& operator>> ( std::istream&, const OpenMDBitSet& bs); | 
| 143 |  |  | friend std::ostream& operator<< ( std::ostream&, const OpenMDBitSet& bs) ; | 
| 144 |  |  |  | 
| 145 |  |  | private: | 
| 146 |  |  |  | 
| 147 |  |  | /** Sets the bit at the specified index to the specified value. */ | 
| 148 |  |  | void setBit(int bitIndex, bool value) { bitset_[bitIndex] = value; } | 
| 149 |  |  |  | 
| 150 |  |  | /** Sets the bits from the specified fromIndex(inclusive) to the specified toIndex(exclusive) to the specified value. */ | 
| 151 |  |  | void setBits(int fromIndex, int toIndex, bool value); | 
| 152 |  |  |  | 
| 153 | gezelter | 1782 | std::vector<bool> bitset_; | 
| 154 | gezelter | 1953 | }; | 
| 155 | gezelter | 1390 |  | 
| 156 |  |  | } | 
| 157 |  |  | #endif |