bitVector.cpp

Go to the documentation of this file.
00001 /* Copyright 1994, LongView Technologies L.L.C. $Revision: 1.3 $ */
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 # include "incls/_precompiled.incl"
00025 
00026 # ifdef DELTA_COMPILER
00027 
00028 # include "incls/_bitVector.cpp.incl"
00029 
00030   bool BitVector::unionWith(BitVector* other) {
00031     while (length < other->length) bits[length++] = 0;
00032     assert(length <= maxLength, "grew too much");
00033     bool changed = false;
00034     for (int i = indexFromNumber(other->length-1); i >= 0; i--) {
00035       int old = bits[i];
00036       bits[i] |= other->bits[i];
00037       changed |= (old != bits[i]);
00038     }
00039     return changed;
00040   }
00041 
00042   bool BitVector::intersectWith(BitVector* other) {
00043     bool changed = false;
00044     for (int i = indexFromNumber(min(length, other->length)-1); i >= 0; i--) {
00045       int old = bits[i];
00046       bits[i] &= other->bits[i];
00047       changed |= (old != bits[i]);
00048     }
00049     return changed;
00050   }
00051 
00052   bool BitVector::isDisjointFrom(BitVector* other) {
00053     for (int i = indexFromNumber(min(length, other->length)-1); i >= 0; i--) {
00054       if ((bits[i] & other->bits[i]) != 0) return false;
00055     }
00056     return true;
00057   }
00058 
00059   void BitVector::addFromTo(int first, int last) {
00060     // mark bits [first..last]
00061     assert(first >= 0 && first < length, "wrong index");
00062     assert(last >= 0 && last < length, "wrong index");
00063     int startIndex = indexFromNumber(first);
00064     int   endIndex = indexFromNumber(last);
00065     if (startIndex == endIndex) {
00066       assert(last - first < BitsPerWord, "oops");
00067       int mask = nthMask(last - first + 1);
00068       bits[startIndex] |= mask << offsetFromNumber(first);
00069     } else {
00070       bits[startIndex] |= AllBits << offsetFromNumber(first);
00071       for (int i = startIndex + 1; i < endIndex; i++) bits[i] = AllBits;
00072       bits[endIndex] |= nthMask(offsetFromNumber(last) + 1);
00073     }
00074 #   ifdef ASSERT
00075       for (int i = first; i <= last; i++)
00076         assert(includes(i), "bit should be set");
00077 #   endif
00078   }
00079 
00080 # ifdef UNUSED
00081   void BitVector::removeFromTo(int first, int last) {
00082     assert(first >= 0 && first < length, "wrong index");
00083     assert(last >= 0 && last < length, "wrong index");
00084     int startIndex = indexFromNumber(first);
00085     int   endIndex = indexFromNumber(last);
00086     if (startIndex == endIndex) {
00087       assert(last - first < BitsPerWord, "oops");
00088       int mask = ~nthMask(last - first + 1);
00089       bits[startIndex] &= mask << offsetFromNumber(first);
00090     } else {
00091       bits[startIndex] &= ~(AllBits << offsetFromNumber(first));
00092       for (int i = startIndex + 1; i < endIndex; i++) bits[i] = 0;
00093       bits[endIndex] &= ~nthMask(offsetFromNumber(last) + 1);
00094     }
00095 #   ifdef ASSERT
00096       for (int i = first; i <= last; i++)
00097         assert(!includes(i), "bit shouldn't be set");
00098 #   endif
00099   }
00100 # endif
00101 
00102   void BitVector::print_short() { lprintf("BitVector %#lx", this); }
00103   
00104   void BitVector::doForAllOnes(intDoFn f) {
00105     for (int i = indexFromNumber(length-1); i >= 0; i--) {
00106       int b = bits[i];
00107       for (int j = 0; j < BitsPerWord; j++) {
00108         if (isSet(b, j)) {
00109           f(i * BitsPerWord + j);
00110           clearNth(b, j);
00111           if (!b) break;
00112         }
00113       }
00114     }
00115   }
00116   
00117   void BitVector::print() {
00118     print_short();
00119     lprintf(": {");
00120     int last = -1;
00121     for (int i = 0; i < length; i ++) {
00122       if (includes(i)) {
00123         if (last < 0) {
00124           lprintf(" %ld", i);   // first bit after string of 0s
00125           last = i;
00126         }
00127       } else {
00128         if (last >= 0) lprintf("..%ld", i - 1); // ended a group
00129         last = -1;
00130       }
00131     }
00132     if (last >= 0) lprintf("..%ld", i - 1);
00133     lprintf(" }");
00134   }
00135   
00136 # endif

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