1 |
/* |
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 |
#ifndef UTILS_BITSET_HPP |
43 |
#define UTILS_BITSET_HPP |
44 |
|
45 |
#include <vector> |
46 |
namespace oopse { |
47 |
|
48 |
/** |
49 |
* @class BitSet BitSet.hpp "BitSet.hpp" |
50 |
* @brief BitSet is a wrapper class of std::vector<char> |
51 |
*/ |
52 |
class BitSet { |
53 |
public: |
54 |
/** */ |
55 |
BitSet() {} |
56 |
/** */ |
57 |
BitSet(int nbits) { bitset_.resize(nbits); } |
58 |
|
59 |
/** Returns the number of bits set to true in this BitSet. */ |
60 |
int countBits(); |
61 |
|
62 |
/** Sets the bit at the specified index to to the complement of its current value. */ |
63 |
void flip(int bitIndex) { bitset_[bitIndex] = !bitset_[bitIndex]; } |
64 |
|
65 |
/** Sets each bit from the specified fromIndex(inclusive) to the specified toIndex(exclusive) to the complement of its current value. */ |
66 |
void flip(int fromIndex, int toIndex); |
67 |
|
68 |
/** Sets each bit to the complement of its current value. */ |
69 |
void flip() { flip(0, size()); } |
70 |
|
71 |
/** Returns the value of the bit with the specified index. */ |
72 |
bool get(int bitIndex) { return bitset_[bitIndex]; } |
73 |
|
74 |
/** Returns a new BitSet composed of bits from this BitSet from fromIndex(inclusive) to toIndex(exclusive). */ |
75 |
BitSet get(int fromIndex, int toIndex); |
76 |
|
77 |
/** Returns true if this BitSet contains no bits that are set to true. */ |
78 |
bool isEmpty(); |
79 |
|
80 |
/** Returns the index of the first bit that is set to false that occurs on or after the specified starting index.*/ |
81 |
int nextOffBit(int fromIndex); |
82 |
|
83 |
/** Returns the index of the first bit that is set to true that occurs on or after the specified starting index. */ |
84 |
int nextOnBit(int fromIndex); |
85 |
|
86 |
/** Performs a logical AND of this target bit set with the argument bit set. */ |
87 |
void and(const BitSet& bs); |
88 |
|
89 |
/** Clears all of the bits in this BitSet whose corresponding bit is set in the specified BitSet. */ |
90 |
void andNot(const BitSet& bs); |
91 |
|
92 |
/** Performs a logical OR of this bit set with the bit set argument. */ |
93 |
void or(const BitSet& bs); |
94 |
|
95 |
/** Performs a logical XOR of this bit set with the bit set argument. */ |
96 |
void xor(const BitSet& bs); |
97 |
|
98 |
void setBitOn(int bitIndex) { setBit(bitIndex, true); } |
99 |
|
100 |
void setBitOff(int bitIndex) { setBit(bitIndex, false); } |
101 |
|
102 |
void setRangeOn(int fromIndex, int toIndex) { setBits(fromIndex, toIndex, true); } |
103 |
|
104 |
void setRangeOff(int fromIndex, int toIndex) { setBits(fromIndex, toIndex, false); } |
105 |
|
106 |
/** Sets all of the bits in this BitSet to false. */ |
107 |
void clear() { setRangeOff(0, size()); } |
108 |
|
109 |
/** Returns the number of bits of space actually in use by this BitSet to represent bit values. */ |
110 |
int size() { return bitset_.size(); } |
111 |
|
112 |
/** Changes the size of BitSet*/ |
113 |
bool resize(int nbits) { bitset_.resize(); } |
114 |
|
115 |
BitSet& operator&= (const BitSet &bs) { and(bs); return *this; } |
116 |
BitSet& operator|= (const BitSet &bs) { or(bs); return *this; } |
117 |
BitSet& operator^= (const BitSet &bs) { xor(bs); return *this; } |
118 |
bool operator[] (int bitIndex) { return bitset_[bitIndex]; } |
119 |
|
120 |
friend BitSet operator| (BitSet& bs1, BitSet& bs2); |
121 |
friend BitSet operator& (BitSet& bs1, BitSet& bs2); |
122 |
friend BitSet operator^ (BitSet& bs1, BitSet& bs2); |
123 |
friend bool operator== (const BitSet & bs1, const BitSet &bs2); |
124 |
|
125 |
friend std::istream& operator>> ( std::istream&, BitSet& bs); |
126 |
friend std::ostream& operator<< ( std::ostream&, const BitSet& bs) ; |
127 |
|
128 |
private: |
129 |
|
130 |
/** Sets the bit at the specified index to the specified value. */ |
131 |
void setBit(int bitIndex, bool value) { bitset_[bitIndex] = value; } |
132 |
|
133 |
/** Sets the bits from the specified fromIndex(inclusive) to the specified toIndex(exclusive) to the specified value. */ |
134 |
void setBits(int fromIndex, int toIndex, bool value) {} |
135 |
|
136 |
std::vector<char> bitset_; |
137 |
}; |
138 |
|
139 |
} |
140 |
#endif |