2f313d36a367752cb1925bf3b514cc5c11ce343b tdreszer Mon Jan 28 11:45:16 2013 -0800 Added local memeory support for bits and a couple of routines to query lm size. diff --git src/lib/bits.c src/lib/bits.c index 9695ead..3148370 100644 --- src/lib/bits.c +++ src/lib/bits.c @@ -65,30 +65,60 @@ Bits *bitClone(Bits* orig, int bitCount) /* Clone bits. */ { int byteCount = ((bitCount+7)>>3); Bits* bits = needLargeZeroedMem(byteCount); memcpy(bits, orig, byteCount); return bits; } void bitFree(Bits **pB) /* Free bits. */ { freez(pB); } +Bits *lmBitAlloc(struct lm *lm,int bitCount) +// Allocate bits. Optionally supply local memory. +{ +int byteCount = ((bitCount+7)>>3); +return lmAlloc(lm,byteCount); +} + +Bits *lmBitRealloc(struct lm *lm,Bits *b, int bitCount, int newBitCount) +// Resize a bit array. If b is null, allocate a new array. Optionally use local memory. +{ +int byteCount = ((bitCount+7)>>3); +int newByteCount = ((newBitCount+7)>>3); +return lmAllocMoreMem(lm, b ,byteCount, newByteCount); +} + +Bits *lmBitClone(struct lm *lm,Bits* orig, int bitCount) +// Clone bits. Optionally use local memory. +{ +int byteCount = ((bitCount+7)>>3); +Bits* bits = lmAlloc(lm,byteCount); +memcpy(bits, orig, byteCount); +return bits; +} + +void lmBitFree(struct lm *lm,Bits **pB) +// Free bits. If allocated from local memory, this does nothing. +{ +*pB = NULL; // Just zero pointer +} + void bitSetOne(Bits *b, int bitIx) /* Set a single bit. */ { b[bitIx>>3] |= oneBit[bitIx&7]; } void bitClearOne(Bits *b, int bitIx) /* Clear a single bit. */ { b[bitIx>>3] &= ~oneBit[bitIx&7]; } void bitSetRange(Bits *b, int startIx, int bitCount) /* Set a range of bits. */ { @@ -219,51 +249,90 @@ b[i] = 0x00; b[endByte] &= ~rightMask[endBits]; } void bitAnd(Bits *a, Bits *b, int bitCount) /* And two bitmaps. Put result in a. */ { int byteCount = ((bitCount+7)>>3); while (--byteCount >= 0) { *a = (*a & *b++); a++; } } +int bitAndCount(Bits *a, Bits *b, int bitCount) +// Without altering 2 bitmaps, count the AND bits. +{ +int byteCount = ((bitCount+7)>>3); +int count = 0; +if (!inittedBitsInByte) + bitsInByteInit(); +while (--byteCount >= 0) + count += bitsInByte[(*a++ & *b++)]; + +return count; +} + void bitOr(Bits *a, Bits *b, int bitCount) /* Or two bitmaps. Put result in a. */ { int byteCount = ((bitCount+7)>>3); while (--byteCount >= 0) { *a = (*a | *b++); a++; } } +int bitOrCount(Bits *a, Bits *b, int bitCount) +// Without altering 2 bitmaps, count the OR'd bits. +{ +int byteCount = ((bitCount+7)>>3); +int count = 0; +if (!inittedBitsInByte) + bitsInByteInit(); +while (--byteCount >= 0) + count += bitsInByte[(*a++ | *b++)]; + +return count; +} + void bitXor(Bits *a, Bits *b, int bitCount) { int byteCount = ((bitCount+7)>>3); while (--byteCount >= 0) { *a = (*a ^ *b++); a++; } } +int bitXorCount(Bits *a, Bits *b, int bitCount) +// Without altering 2 bitmaps, count the XOR'd bits. +{ +int byteCount = ((bitCount+7)>>3); +int count = 0; +if (!inittedBitsInByte) + bitsInByteInit(); +while (--byteCount >= 0) + count += bitsInByte[(*a++ ^ *b++)]; + +return count; +} + void bitNot(Bits *a, int bitCount) /* Flip all bits in a. */ { int byteCount = ((bitCount+7)>>3); while (--byteCount >= 0) { *a = ~*a; a++; } } void bitPrint(Bits *a, int startIx, int bitCount, FILE* out) /* Print part or all of bit map as a string of 0s and 1s. Mostly useful for * debugging */ {