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