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 |
|
|
* [3] Sun, Lin & Gezelter, J. Chem. Phys. 128, 24107 (2008). |
39 |
|
|
* [4] Vardeman & Gezelter, in progress (2009). |
40 |
|
|
*/ |
41 |
|
|
|
42 |
|
|
#ifndef UTILS_OPENMDBITSET_HPP |
43 |
|
|
#define UTILS_OPENMDBITSET_HPP |
44 |
|
|
|
45 |
|
|
#include <iostream> |
46 |
|
|
#include <vector> |
47 |
|
|
namespace OpenMD { |
48 |
|
|
|
49 |
|
|
/** |
50 |
|
|
* @class OpenMDBitSet OpenMDBitSet.hpp "OpenMDBitSet.hpp" |
51 |
|
|
* @brief OpenMDBitSet is a wrapper class of std::vector<char> to act as a growable std::bitset |
52 |
|
|
*/ |
53 |
|
|
class OpenMDBitSet { |
54 |
|
|
public: |
55 |
|
|
/** */ |
56 |
|
|
OpenMDBitSet() {} |
57 |
|
|
/** */ |
58 |
|
|
OpenMDBitSet(int nbits) : bitset_(nbits) {clearAll(); } |
59 |
|
|
|
60 |
|
|
/** Returns the number of bits set to true in this OpenMDBitSet. */ |
61 |
|
|
int countBits(); |
62 |
|
|
|
63 |
|
|
/** Sets the bit at the specified index to to the complement of its current value. */ |
64 |
|
|
void flip(int bitIndex) { bitset_[bitIndex] = !bitset_[bitIndex]; } |
65 |
|
|
|
66 |
|
|
/** Sets each bit from the specified fromIndex(inclusive) to the specified toIndex(exclusive) to the complement of its current value. */ |
67 |
|
|
void flip(int fromIndex, int toIndex); |
68 |
|
|
|
69 |
|
|
/** Sets each bit to the complement of its current value. */ |
70 |
|
|
void flip() { flip(0, size()); } |
71 |
|
|
|
72 |
|
|
/** Returns the value of the bit with the specified index. */ |
73 |
|
|
bool get(int bitIndex) { return bitset_[bitIndex]; } |
74 |
|
|
|
75 |
|
|
/** Returns a new OpenMDBitSet composed of bits from this OpenMDBitSet from fromIndex(inclusive) to toIndex(exclusive). */ |
76 |
|
|
OpenMDBitSet get(int fromIndex, int toIndex); |
77 |
|
|
|
78 |
|
|
/** Returns true if any bits are set to true */ |
79 |
|
|
bool any() {return !none(); } |
80 |
|
|
|
81 |
|
|
/** Returns true if no bits are set to true */ |
82 |
|
|
bool none(); |
83 |
|
|
|
84 |
|
|
int firstOffBit() const { return !bitset_[0] ? 0 : nextOffBit(0); } |
85 |
|
|
|
86 |
|
|
/** Returns the index of the first bit that is set to false that occurs on or after the specified starting index.*/ |
87 |
|
|
int nextOffBit(int fromIndex) const; |
88 |
|
|
|
89 |
|
|
int firstOnBit() const { return bitset_[0] ? 0 : nextOnBit(0); } |
90 |
|
|
|
91 |
|
|
/** Returns the index of the first bit that is set to true that occurs on or after the specified starting index. */ |
92 |
|
|
int nextOnBit(int fromIndex) const; |
93 |
|
|
|
94 |
|
|
/** Performs a logical AND of this target bit set with the argument bit set. */ |
95 |
|
|
void andOperator (const OpenMDBitSet& bs); |
96 |
|
|
|
97 |
|
|
/** Performs a logical OR of this bit set with the bit set argument. */ |
98 |
|
|
void orOperator (const OpenMDBitSet& bs); |
99 |
|
|
|
100 |
|
|
/** Performs a logical XOR of this bit set with the bit set argument. */ |
101 |
|
|
void xorOperator (const OpenMDBitSet& bs); |
102 |
|
|
|
103 |
|
|
void setBitOn(int bitIndex) { setBit(bitIndex, true); } |
104 |
|
|
|
105 |
|
|
void setBitOff(int bitIndex) { setBit(bitIndex, false); } |
106 |
|
|
|
107 |
|
|
void setRangeOn(int fromIndex, int toIndex) { setBits(fromIndex, toIndex, true); } |
108 |
|
|
|
109 |
|
|
void setRangeOff(int fromIndex, int toIndex) { setBits(fromIndex, toIndex, false); } |
110 |
|
|
|
111 |
|
|
/** Sets all of the bits in this OpenMDBitSet to false. */ |
112 |
|
|
void clearAll() { setRangeOff(0, size()); } |
113 |
|
|
|
114 |
|
|
void setAll() { setRangeOn(0, size()); } |
115 |
|
|
|
116 |
|
|
/** Returns the number of bits of space actually in use by this OpenMDBitSet to represent bit values. */ |
117 |
|
|
int size() const { return bitset_.size(); } |
118 |
|
|
|
119 |
|
|
/** Changes the size of OpenMDBitSet*/ |
120 |
|
|
void resize(int nbits); |
121 |
|
|
|
122 |
|
|
OpenMDBitSet& operator&= (const OpenMDBitSet &bs) { andOperator (bs); return *this; } |
123 |
|
|
OpenMDBitSet& operator|= (const OpenMDBitSet &bs) { orOperator (bs); return *this; } |
124 |
|
|
OpenMDBitSet& operator^= (const OpenMDBitSet &bs) { xorOperator (bs); return *this; } |
125 |
|
|
OpenMDBitSet& operator-= (const OpenMDBitSet &bs) { |
126 |
|
|
OpenMDBitSet tmp = *this ^ bs; |
127 |
|
|
*this &= tmp; |
128 |
|
|
return *this; |
129 |
|
|
} |
130 |
|
|
|
131 |
|
|
bool operator[] (int bitIndex) const { return bitset_[bitIndex]; } |
132 |
|
|
friend OpenMDBitSet operator| (const OpenMDBitSet& bs1, const OpenMDBitSet& bs2); |
133 |
|
|
friend OpenMDBitSet operator& (const OpenMDBitSet& bs1, const OpenMDBitSet& bs2); |
134 |
|
|
friend OpenMDBitSet operator^ (const OpenMDBitSet& bs1, const OpenMDBitSet& bs2); |
135 |
|
|
friend OpenMDBitSet operator- (const OpenMDBitSet& bs1, const OpenMDBitSet& bs2); |
136 |
|
|
|
137 |
|
|
friend bool operator== (const OpenMDBitSet & bs1, const OpenMDBitSet &bs2); |
138 |
|
|
|
139 |
|
|
//friend std::istream& operator>> ( std::istream&, const OpenMDBitSet& bs); |
140 |
|
|
friend std::ostream& operator<< ( std::ostream&, const OpenMDBitSet& bs) ; |
141 |
|
|
|
142 |
|
|
private: |
143 |
|
|
|
144 |
|
|
/** Sets the bit at the specified index to the specified value. */ |
145 |
|
|
void setBit(int bitIndex, bool value) { bitset_[bitIndex] = value; } |
146 |
|
|
|
147 |
|
|
/** Sets the bits from the specified fromIndex(inclusive) to the specified toIndex(exclusive) to the specified value. */ |
148 |
|
|
void setBits(int fromIndex, int toIndex, bool value); |
149 |
|
|
|
150 |
|
|
std::vector<char> bitset_; |
151 |
|
|
}; |
152 |
|
|
|
153 |
|
|
|
154 |
|
|
} |
155 |
|
|
#endif |