[Avida-SVN] r3322 - development/source/tools
blwalker at myxo.css.msu.edu
blwalker at myxo.css.msu.edu
Wed Jun 17 09:45:45 PDT 2009
Author: blwalker
Date: 2009-06-17 12:45:45 -0400 (Wed, 17 Jun 2009)
New Revision: 3322
Modified:
development/source/tools/cBitArray.cc
development/source/tools/cBitArray.h
Log:
Adds increment to cBitArray, with operator++, in both pre and postfix varieties. Unit tests, too.
Modified: development/source/tools/cBitArray.cc
===================================================================
--- development/source/tools/cBitArray.cc 2009-06-16 20:03:30 UTC (rev 3321)
+++ development/source/tools/cBitArray.cc 2009-06-17 16:45:45 UTC (rev 3322)
@@ -282,10 +282,26 @@
assert(false); // Should never get here.
}
+void cRawBitArray::INCREMENT(const int num_bits)
+{
+ const int num_fields = GetNumFields(num_bits);
+ int i = 0;
+ for (i = 0; i < num_fields; i++) {
+ bit_fields[i]++;
+ if (bit_fields[i] != 0) { break; } // no overflow, do not need to increment higher fields
+ }
+
+ // if highest bit field was incremented, mask out any unused portions of the field so as not to confuse CountBits
+ if (i == num_fields - 1) {
+ unsigned int shift_mask = 0xffffffff >> 32 - (num_bits % 32);
+ bit_fields[num_fields - 1] &= shift_mask;
+ }
+}
+
void cRawBitArray::NOT(const cRawBitArray & array1, const int num_bits)
{
ResizeSloppy(num_bits);
@@ -391,8 +407,15 @@
SHIFT(num_bits, shift_size);
}
+void cRawBitArray::INCREMENT(const cRawBitArray & array1, const int num_bits)
+{
+ Copy(array1, num_bits);
+ INCREMENT(num_bits);
+}
+
+
std::ostream & operator << (std::ostream & out, const cBitArray & bit_array)
{
bit_array.Print(out);
@@ -650,6 +673,31 @@
passed = false;
cerr << "ERROR in ShiftRight across multiple fields!" << endl;
}
+
+ // INCREMENT
+
+ cRawBitArray bit_array10(1);
+
+ bit_array10.INCREMENT(1);
+ if (bit_array10.GetBit(0) != true || bit_array10.CountBits(1) != 1) {
+ passed = false;
+ cerr << "ERROR in INCREMENT operation!" << endl;
+ }
+
+ bit_array10.INCREMENT(1);
+ if (bit_array10.GetBit(0) != false || bit_array10.CountBits(1) != 0) {
+ passed = false;
+ cerr << "ERROR in INCREMENT overflowing last bit field!" << endl;
+ }
+
+ cRawBitArray bit_array11(33);
+ for (int i = 0; i < 32; i++) { bit_array11.SetBit(i, true); }
+
+ bit_array11.INCREMENT(33);
+ if (bit_array11.GetBit(32) != 1 || bit_array11.CountBits(33) != 1) {
+ passed = false;
+ cerr << "ERROR in INCREMENT across fields!" << endl;
+ }
// bit_array4.Print(70);
// bit_array5.Print(70);
@@ -699,6 +747,16 @@
cerr << "ERROR: Chained bitwise operators failed for cBitArray" << endl;
}
+ if ((++(~ba & ~ba2)).CountBits() != 30) {
+ passed = false;
+ cerr << "ERROR: prefix ++ failed for cBitArray" << endl;
+ }
+
+ if (((~ba & ~ba2)++).CountBits() != 31) {
+ passed = false;
+ cerr << "ERROR: postfix ++ failed for cBitArray" << endl;
+ }
+
cout << ba << " " << ba.CountBits() << endl;
cout << ba2 << " " << ba2.CountBits() << endl;
cout << (~ba & ~ba2) << " " << (~ba & ~ba2).CountBits() << endl;
Modified: development/source/tools/cBitArray.h
===================================================================
--- development/source/tools/cBitArray.h 2009-06-16 20:03:30 UTC (rev 3321)
+++ development/source/tools/cBitArray.h 2009-06-17 16:45:45 UTC (rev 3322)
@@ -64,6 +64,9 @@
// const cBitArray & EQUSELF(const cBitArray & array2)
// const cBitArray & SHIFTSELF(const int shift_size) const
+// Arithmetic:
+// cBitArray INCREMENTSELF()
+
// Operator overloads:
// cBitArray operator~() const
// cBitArray operator&(const cBitArray & ar2) const
@@ -76,6 +79,8 @@
// const cBitArray & operator^=(const cBitArray & ar2)
// const cBitArray & operator>>=(const int)
// const cBitArray & operator<<=(const int)
+// cBitArray & operator++() // prefix ++
+// cBitArray & operator++(int) // postfix ++
@@ -208,6 +213,7 @@
void XOR(const cRawBitArray & array2, const int num_bits);
void EQU(const cRawBitArray & array2, const int num_bits);
void SHIFT(const int num_bits, const int shift_size); // positive numbers for left and negative for right (0 does nothing)
+ void INCREMENT(const int num_bits);
// Fast bool operators where we load all of the inputs and store the
// results here.
@@ -225,6 +231,7 @@
void EQU(const cRawBitArray & array1, const cRawBitArray & array2,
const int num_bits);
void SHIFT(const cRawBitArray & array1, const int num_bits, const int shift_size);
+ void INCREMENT(const cRawBitArray & array1, const int num_bits); // implemented for completeness, but unused by cBitArray
};
class cBitArray {
@@ -412,6 +419,11 @@
return *this;
}
+ cBitArray & INCREMENTSELF() {
+ bit_array.INCREMENT(array_size);
+ return *this;
+ }
+
// Operator overloads...
cBitArray operator~() const { return NOT(); }
@@ -425,6 +437,8 @@
const cBitArray & operator^=(const cBitArray & ar2) { return XORSELF(ar2); }
const cBitArray & operator<<=(const int shift_size) { return SHIFTSELF(shift_size); }
const cBitArray & operator>>=(const int shift_size) { return SHIFTSELF(-shift_size); }
+ cBitArray & operator++() { return INCREMENTSELF(); } // prefix ++
+ cBitArray operator++(int) { cBitArray ans = *this; operator++(); return ans;} // postfix ++
};
More information about the Avida-cvs
mailing list