ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-2.0/src/utils/BitSet.hpp
Revision: 1980
Committed: Mon Feb 7 19:13:43 2005 UTC (19 years, 5 months ago) by tim
File size: 6445 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     #ifndef UTILS_BITSET_HPP
43     #define UTILS_BITSET_HPP
44    
45 tim 1980 #include <iostream>
46 tim 1962 #include <functional>
47 tim 1961 #include <vector>
48     namespace oopse {
49    
50     /**
51     * @class BitSet BitSet.hpp "BitSet.hpp"
52 tim 1962 * @brief BitSet is a wrapper class of std::vector<char> to act as a growable std::bitset
53 tim 1961 */
54     class BitSet {
55     public:
56     /** */
57     BitSet() {}
58     /** */
59 tim 1974 BitSet(int nbits) : bitset_(nbits) {clearAll(); }
60 tim 1961
61     /** Returns the number of bits set to true in this BitSet. */
62     int countBits();
63    
64     /** Sets the bit at the specified index to to the complement of its current value. */
65     void flip(int bitIndex) { bitset_[bitIndex] = !bitset_[bitIndex]; }
66    
67     /** Sets each bit from the specified fromIndex(inclusive) to the specified toIndex(exclusive) to the complement of its current value. */
68     void flip(int fromIndex, int toIndex);
69    
70     /** Sets each bit to the complement of its current value. */
71     void flip() { flip(0, size()); }
72    
73     /** Returns the value of the bit with the specified index. */
74     bool get(int bitIndex) { return bitset_[bitIndex]; }
75    
76     /** Returns a new BitSet composed of bits from this BitSet from fromIndex(inclusive) to toIndex(exclusive). */
77     BitSet get(int fromIndex, int toIndex);
78    
79 tim 1962 /** Returns true if any bits are set to true */
80     bool any() {return !none(); }
81    
82     /** Returns true if no bits are set to true */
83     bool none();
84    
85 tim 1961 /** Returns the index of the first bit that is set to false that occurs on or after the specified starting index.*/
86 tim 1980 int nextOffBit(int fromIndex) const;
87 tim 1961
88     /** Returns the index of the first bit that is set to true that occurs on or after the specified starting index. */
89 tim 1980 int nextOnBit(int fromIndex) const;
90 tim 1961
91     /** Performs a logical AND of this target bit set with the argument bit set. */
92 tim 1962 void andOperator (const BitSet& bs);
93    
94 tim 1961 /** Performs a logical OR of this bit set with the bit set argument. */
95 tim 1962 void orOperator (const BitSet& bs);
96 tim 1961
97     /** Performs a logical XOR of this bit set with the bit set argument. */
98 tim 1962 void xorOperator (const BitSet& bs);
99 tim 1961
100     void setBitOn(int bitIndex) { setBit(bitIndex, true); }
101    
102     void setBitOff(int bitIndex) { setBit(bitIndex, false); }
103    
104     void setRangeOn(int fromIndex, int toIndex) { setBits(fromIndex, toIndex, true); }
105    
106     void setRangeOff(int fromIndex, int toIndex) { setBits(fromIndex, toIndex, false); }
107    
108     /** Sets all of the bits in this BitSet to false. */
109 tim 1970 void clearAll() { setRangeOff(0, size()); }
110    
111     void setAll() { setRangeOn(0, size()); }
112 tim 1961
113     /** Returns the number of bits of space actually in use by this BitSet to represent bit values. */
114 tim 1962 int size() const { return bitset_.size(); }
115 tim 1961
116     /** Changes the size of BitSet*/
117 tim 1970 void resize(int nbits);
118 tim 1961
119 tim 1962 BitSet& operator&= (const BitSet &bs) { andOperator (bs); return *this; }
120     BitSet& operator|= (const BitSet &bs) { orOperator (bs); return *this; }
121     BitSet& operator^= (const BitSet &bs) { xorOperator (bs); return *this; }
122 tim 1980 bool operator[] (int bitIndex) const { return bitset_[bitIndex]; }
123 tim 1962 friend BitSet operator| (const BitSet& bs1, const BitSet& bs2);
124     friend BitSet operator& (const BitSet& bs1, const BitSet& bs2);
125     friend BitSet operator^ (const BitSet& bs1, const BitSet& bs2);
126 tim 1961 friend bool operator== (const BitSet & bs1, const BitSet &bs2);
127    
128 tim 1962 friend std::istream& operator>> ( std::istream&, const BitSet& bs);
129 tim 1961 friend std::ostream& operator<< ( std::ostream&, const BitSet& bs) ;
130    
131     private:
132    
133     /** Sets the bit at the specified index to the specified value. */
134     void setBit(int bitIndex, bool value) { bitset_[bitIndex] = value; }
135    
136     /** Sets the bits from the specified fromIndex(inclusive) to the specified toIndex(exclusive) to the specified value. */
137 tim 1962 void setBits(int fromIndex, int toIndex, bool value);
138 tim 1961
139     std::vector<char> bitset_;
140     };
141    
142 tim 1962 template<typename T>
143     struct logical_xor :public std::binary_function<T, T, bool> {
144     double operator()(T x, T y) { return x ^ y; }
145     };
146    
147 tim 1961 }
148     #endif