00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
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);
00125 last = i;
00126 }
00127 } else {
00128 if (last >= 0) lprintf("..%ld", i - 1);
00129 last = -1;
00130 }
00131 }
00132 if (last >= 0) lprintf("..%ld", i - 1);
00133 lprintf(" }");
00134 }
00135
00136 # endif