bitVector.hpp

Go to the documentation of this file.
00001 /* Copyright 1994, LongView Technologies L.L.C. $Revision: 1.9 $ */
00002 /* Copyright (c) 2006, Sun Microsystems, Inc.
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 
00006 following conditions are met:
00007 
00008     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00009     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 
00010           disclaimer in the documentation and/or other materials provided with the distribution.
00011     * Neither the name of Sun Microsystems nor the names of its contributors may be used to endorse or promote products derived 
00012           from this software without specific prior written permission.
00013 
00014 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 
00015 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
00016 THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
00017 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00018 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
00019 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
00020 
00021 
00022 */
00023 
00024 // General bit vectors; space is allocated for maxLength bits, but only
00025 // the portion up to length bits is used.
00026 // SimpleBitVector: simple version that fits into a word (for speed)
00027 
00028 # ifdef DELTA_COMPILER
00029 
00030   typedef void (*intDoFn)(int i);
00031   
00032   // a pseudo-class -- int(this) actually holds the bits, so there's no
00033   // allocation
00034   class SimpleBitVector : public ValueObj {
00035     int bits;
00036    public:
00037     SimpleBitVector(int b = 0) { bits = b; } 
00038 
00039     SimpleBitVector allocate(int l) {
00040       assert(l >= 0 && l < BitsPerWord, "need longer bit vector");
00041       return SimpleBitVector(addNth(bits, l));
00042     }
00043 
00044     SimpleBitVector deallocate(int l) {
00045       assert(l >= 0 && l < BitsPerWord, "need longer bit vector");
00046       return SimpleBitVector(subNth(bits, l));
00047     }
00048 
00049     bool isAllocated(int l) {
00050       assert(l >= 0 && l < BitsPerWord, "need longer bit vector");
00051       return isSet(bits, l);
00052     }
00053     bool isEmpty() { return bits == 0; }
00054   };
00055 
00056   class BitVector:  public PrintableResourceObj {
00057    protected:
00058     int maxLength;              // max # bits
00059     int length;                 // number of bits, not words
00060     int* bits;                  // array containing the bits
00061     
00062     int  indexFromNumber(int i) { return i >> LogBitsPerWord; }
00063     int offsetFromNumber(int i) { return lowerBits(i, LogBitsPerWord); }
00064     
00065     bool getBitInWord(int i, int o) { return isSet(bits[i], o); }
00066     void setBitInWord(int i, int o) {       setNth(bits[i], o); }
00067     void clearBitInWord(int i, int o) {   clearNth(bits[i], o); }
00068     
00069     int bitsLength(int l) { return indexFromNumber(l - 1) + 1; }
00070     
00071     int* createBitString(int l) {
00072       int blen = bitsLength(l);
00073       int* bs = NEW_RESOURCE_ARRAY( int, blen);
00074       set_words(bs, blen, 0);
00075       return bs; }
00076     int* copyBitString(int len) {
00077       assert(len >= maxLength, "can't shorten");
00078       int blen = bitsLength(len);
00079       int* bs = NEW_RESOURCE_ARRAY( int, blen);
00080       int blength = bitsLength(maxLength);
00081       copy_words(bits, bs, blength);
00082       if (blength < blen) set_words(bs + blength, blen - blength, 0);
00083       return bs; }
00084     
00085   public:
00086     BitVector(int l) {
00087       assert(l > 0, "should have some length");
00088       length = maxLength = l; bits = createBitString(l); }
00089     
00090   protected:
00091     BitVector(int l, int ml, int* bs) {
00092       maxLength = ml; length = l; bits = bs; }
00093     
00094   public:
00095     BitVector* copy(int len) {
00096       return new BitVector(length, len, copyBitString(len)); }
00097     
00098     bool includes(int i) {
00099       assert(this, "shouldn't be a null pointer");
00100       assert(i >= 0 && i < length, "not in range");
00101       bool b = getBitInWord(indexFromNumber(i), offsetFromNumber(i));
00102       return b; }
00103     void add(int i) {
00104       assert(this, "shouldn't be a null pointer");
00105       assert(i >= 0 && i < length, "not in range");
00106       setBitInWord(indexFromNumber(i), offsetFromNumber(i)); }
00107     void addFromTo(int first, int last);        // set bits [first..last]
00108     void remove(int i) {
00109       assert(this, "shouldn't be a null pointer");
00110       assert(i >= 0 && i < length, "not in range");
00111       clearBitInWord(indexFromNumber(i), offsetFromNumber(i)); }
00112     void removeFromTo(int first, int last);     // clear bits [first..last]
00113 
00114     // union/intersect return true if receiver has changed
00115     bool unionWith(BitVector* other);           // this |= other
00116     bool intersectWith(BitVector* other);       // this &= other
00117     bool isDisjointFrom(BitVector* other);      // (this & other) == {}
00118 
00119     void doForAllOnes(intDoFn f);  // call f for all 1 bits
00120 
00121     void setLength(int l)       { assert(l < maxLength, "too big"); length = l; }
00122     void clear()                { set_words(bits, bitsLength(length), 0); }
00123     
00124     void print_short();
00125     void print();
00126 
00127     friend class LongRegisterMask;
00128     friend int findFirstUnused(LongRegisterMask** strings, int len, int start);
00129   };
00130   
00131 # endif

Generated on Mon Oct 9 13:37:03 2006 for Strongtalk VM by  doxygen 1.4.7