ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-3.0/src/utils/BitSet.cpp
Revision: 1980
Committed: Mon Feb 7 19:13:43 2005 UTC (19 years, 5 months ago) by tim
File size: 5750 byte(s)
Log Message:
minor change in Bitset

File Contents

# User Rev Content
1 tim 1961 /*
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. Acknowledgement of the program authors must be made in any
10     * publication of scientific results based in part on use of the
11     * program. An acceptable form of acknowledgement is citation of
12     * the article in which the program was described (Matthew
13     * A. Meineke, Charles F. Vardeman II, Teng Lin, Christopher
14     * J. Fennell and J. Daniel Gezelter, "OOPSE: An Object-Oriented
15     * Parallel Simulation Engine for Molecular Dynamics,"
16     * J. Comput. Chem. 26, pp. 252-271 (2005))
17     *
18     * 2. Redistributions of source code must retain the above copyright
19     * notice, this list of conditions and the following disclaimer.
20     *
21     * 3. Redistributions in binary form must reproduce the above copyright
22     * notice, this list of conditions and the following disclaimer in the
23     * documentation and/or other materials provided with the
24     * distribution.
25     *
26     * This software is provided "AS IS," without a warranty of any
27     * kind. All express or implied conditions, representations and
28     * warranties, including any implied warranty of merchantability,
29     * fitness for a particular purpose or non-infringement, are hereby
30     * excluded. The University of Notre Dame and its licensors shall not
31     * be liable for any damages suffered by licensee as a result of
32     * using, modifying or distributing the software or its
33     * derivatives. In no event will the University of Notre Dame or its
34     * licensors be liable for any lost revenue, profit or data, or for
35     * direct, indirect, special, consequential, incidental or punitive
36     * damages, however caused and regardless of the theory of liability,
37     * arising out of the use of or inability to use software, even if the
38     * University of Notre Dame has been advised of the possibility of
39     * such damages.
40     */
41    
42     #include "utils/BitSet.hpp"
43     #include <algorithm>
44 tim 1962 #include <cassert>
45 tim 1980 #include <string>
46 tim 1961 namespace oopse {
47     int BitSet::countBits() {
48 tim 1962 return std::count(bitset_.begin(), bitset_.end(), true);
49 tim 1961 }
50    
51     void BitSet::flip(int fromIndex, int toIndex) {
52     assert(fromIndex <= toIndex);
53     assert(fromIndex >=0);
54     assert(toIndex <= size());
55     std::vector<char>::iterator first = bitset_.begin() + fromIndex;
56     std::vector<char>::iterator last = bitset_.begin() + toIndex;
57    
58     std::transform(first, last, first, std::logical_not<bool>());
59    
60     }
61    
62     BitSet BitSet::get(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     BitSet result;
70     std::copy(first, last, std::back_inserter(result.bitset_));
71     return result;
72     }
73    
74 tim 1962 bool BitSet::none() {
75 tim 1961 std::vector<char>::iterator i = std::find(bitset_.begin(), bitset_.end(), true);
76     return i == bitset_.end() ? true : false;
77     }
78    
79 tim 1980 int BitSet::nextOffBit(int fromIndex) const {
80 tim 1962 ++fromIndex;
81     while (fromIndex < size()) {
82     if (!bitset_[fromIndex]) {
83     return fromIndex;
84     }
85     ++fromIndex;
86     }
87 tim 1961
88 tim 1962 return -1;
89 tim 1961 }
90    
91 tim 1980 int BitSet::nextOnBit(int fromIndex) const {
92 tim 1962 ++fromIndex;
93     while (fromIndex < size()) {
94     if (bitset_[fromIndex]) {
95     return fromIndex;
96     }
97     ++fromIndex;
98     }
99 tim 1961
100 tim 1962 return -1;
101     }
102    
103     void BitSet::andOperator (const BitSet& bs) {
104 tim 1961 assert(size() == bs.size());
105    
106     std::transform(bs.bitset_.begin(), bs.bitset_.end(), bitset_.begin(), bitset_.begin(), std::logical_and<bool>());
107     }
108    
109 tim 1962 void BitSet::orOperator (const BitSet& bs) {
110 tim 1961 assert(size() == bs.size());
111     std::transform(bs.bitset_.begin(), bs.bitset_.end(), bitset_.begin(), bitset_.begin(), std::logical_or<bool>());
112     }
113    
114 tim 1962 void BitSet::xorOperator (const BitSet& bs) {
115 tim 1961 assert(size() == bs.size());
116     std::transform(bs.bitset_.begin(), bs.bitset_.end(), bitset_.begin(), bitset_.begin(), oopse::logical_xor<bool>());
117     }
118    
119     void BitSet::setBits(int fromIndex, int toIndex, bool value) {
120     assert(fromIndex <= toIndex);
121     assert(fromIndex >=0);
122     assert(toIndex <= size());
123     std::vector<char>::iterator first = bitset_.begin() + fromIndex;
124     std::vector<char>::iterator last = bitset_.begin() + toIndex;
125     std::fill(first, last, value);
126     }
127    
128 tim 1970 void BitSet::resize(int nbits) {
129     int oldSize = size();
130     bitset_.resize(nbits);
131     if (nbits > oldSize) {
132     std::fill(bitset_.begin()+oldSize, bitset_.begin()+nbits+1, false);
133     }
134     }
135    
136 tim 1962 BitSet operator| (const BitSet& bs1, const BitSet& bs2) {
137 tim 1961 assert(bs1.size() == bs2.size());
138    
139     BitSet result(bs1);
140     result |= bs2;
141     return result;
142     }
143    
144 tim 1962 BitSet operator& (const BitSet& bs1, const BitSet& bs2) {
145 tim 1961 assert(bs1.size() == bs2.size());
146    
147     BitSet result(bs1);
148     result &= bs2;
149     return result;
150     }
151    
152 tim 1962 BitSet operator^ (const BitSet& bs1, const BitSet& bs2) {
153 tim 1961 assert(bs1.size() == bs2.size());
154    
155     BitSet result(bs1);
156     result ^= bs2;
157     return result;
158     }
159    
160     bool operator== (const BitSet & bs1, const BitSet &bs2) {
161     assert(bs1.size() == bs2.size());
162     return std::equal(bs1.bitset_.begin(), bs1.bitset_.end(), bs2.bitset_.begin());
163     }
164    
165 tim 1962 std::istream& operator>> ( std::istream& is, const BitSet& bs) {
166 tim 1961
167 tim 1962 return is;
168 tim 1961 }
169    
170     std::ostream& operator<< ( std::ostream& os, const BitSet& bs) {
171 tim 1980 for (int i = 0; i < bs.bitset_.size(); ++i) {
172     std::string val = bs[i] ? "true" : "false";
173     os << "BitSet[" << i <<"] = " << val << std::endl;
174     }
175    
176 tim 1962 return os;
177 tim 1961 }
178    
179     }