ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/OpenMD/trunk/src/utils/BitSet.hpp
Revision: 507
Committed: Fri Apr 15 22:04:00 2005 UTC (20 years ago) by gezelter
File size: 6423 byte(s)
Log Message:
xemacs has been drafted to perform our indentation services

File Contents

# User Rev Content
1 tim 277 /*
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 296 #include <iostream>
46 tim 277 #include <vector>
47     namespace oopse {
48    
49 gezelter 507 /**
50     * @class BitSet BitSet.hpp "BitSet.hpp"
51     * @brief BitSet is a wrapper class of std::vector<char> to act as a growable std::bitset
52     */
53     class BitSet {
54     public:
55     /** */
56     BitSet() {}
57     /** */
58     BitSet(int nbits) : bitset_(nbits) {clearAll(); }
59 tim 277
60 gezelter 507 /** Returns the number of bits set to true in this BitSet. */
61     int countBits();
62 tim 277
63 gezelter 507 /** Sets the bit at the specified index to to the complement of its current value. */
64     void flip(int bitIndex) { bitset_[bitIndex] = !bitset_[bitIndex]; }
65 tim 277
66 gezelter 507 /** Sets each bit from the specified fromIndex(inclusive) to the specified toIndex(exclusive) to the complement of its current value. */
67     void flip(int fromIndex, int toIndex);
68 tim 277
69 gezelter 507 /** Sets each bit to the complement of its current value. */
70     void flip() { flip(0, size()); }
71 tim 277
72 gezelter 507 /** Returns the value of the bit with the specified index. */
73     bool get(int bitIndex) { return bitset_[bitIndex]; }
74 tim 277
75 gezelter 507 /** Returns a new BitSet composed of bits from this BitSet from fromIndex(inclusive) to toIndex(exclusive). */
76     BitSet get(int fromIndex, int toIndex);
77 tim 277
78 gezelter 507 /** Returns true if any bits are set to true */
79     bool any() {return !none(); }
80 tim 278
81 gezelter 507 /** Returns true if no bits are set to true */
82     bool none();
83 tim 312
84 gezelter 507 int firstOffBit() const { return !bitset_[0] ? 0 : nextOffBit(0); }
85 tim 278
86 gezelter 507 /** Returns the index of the first bit that is set to false that occurs on or after the specified starting index.*/
87     int nextOffBit(int fromIndex) const;
88 tim 312
89 gezelter 507 int firstOnBit() const { return bitset_[0] ? 0 : nextOnBit(0); }
90 tim 312
91 gezelter 507 /** Returns the index of the first bit that is set to true that occurs on or after the specified starting index. */
92     int nextOnBit(int fromIndex) const;
93 tim 277
94 gezelter 507 /** Performs a logical AND of this target bit set with the argument bit set. */
95     void andOperator (const BitSet& bs);
96 tim 278
97 gezelter 507 /** Performs a logical OR of this bit set with the bit set argument. */
98     void orOperator (const BitSet& bs);
99 tim 277
100 gezelter 507 /** Performs a logical XOR of this bit set with the bit set argument. */
101     void xorOperator (const BitSet& bs);
102 tim 277
103 gezelter 507 void setBitOn(int bitIndex) { setBit(bitIndex, true); }
104 tim 277
105 gezelter 507 void setBitOff(int bitIndex) { setBit(bitIndex, false); }
106 tim 277
107 gezelter 507 void setRangeOn(int fromIndex, int toIndex) { setBits(fromIndex, toIndex, true); }
108 tim 277
109 gezelter 507 void setRangeOff(int fromIndex, int toIndex) { setBits(fromIndex, toIndex, false); }
110 tim 277
111 gezelter 507 /** Sets all of the bits in this BitSet to false. */
112     void clearAll() { setRangeOff(0, size()); }
113 tim 286
114 gezelter 507 void setAll() { setRangeOn(0, size()); }
115 tim 277
116 gezelter 507 /** Returns the number of bits of space actually in use by this BitSet to represent bit values. */
117     int size() const { return bitset_.size(); }
118 tim 277
119 gezelter 507 /** Changes the size of BitSet*/
120     void resize(int nbits);
121 tim 277
122 gezelter 507 BitSet& operator&= (const BitSet &bs) { andOperator (bs); return *this; }
123     BitSet& operator|= (const BitSet &bs) { orOperator (bs); return *this; }
124     BitSet& operator^= (const BitSet &bs) { xorOperator (bs); return *this; }
125     BitSet& operator-= (const BitSet &bs) {
126     BitSet tmp = *this ^ bs;
127     *this &= tmp;
128     return *this;
129     }
130 tim 353
131 gezelter 507 bool operator[] (int bitIndex) const { return bitset_[bitIndex]; }
132     friend BitSet operator| (const BitSet& bs1, const BitSet& bs2);
133     friend BitSet operator& (const BitSet& bs1, const BitSet& bs2);
134     friend BitSet operator^ (const BitSet& bs1, const BitSet& bs2);
135     friend BitSet operator- (const BitSet& bs1, const BitSet& bs2);
136 tim 353
137 gezelter 507 friend bool operator== (const BitSet & bs1, const BitSet &bs2);
138 tim 277
139 gezelter 507 //friend std::istream& operator>> ( std::istream&, const BitSet& bs);
140     friend std::ostream& operator<< ( std::ostream&, const BitSet& bs) ;
141 tim 277
142 gezelter 507 private:
143 tim 277
144 gezelter 507 /** Sets the bit at the specified index to the specified value. */
145     void setBit(int bitIndex, bool value) { bitset_[bitIndex] = value; }
146 tim 277
147 gezelter 507 /** Sets the bits from the specified fromIndex(inclusive) to the specified toIndex(exclusive) to the specified value. */
148     void setBits(int fromIndex, int toIndex, bool value);
149 tim 277
150 gezelter 507 std::vector<char> bitset_;
151     };
152 tim 277
153 tim 278
154 tim 277 }
155     #endif