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