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