Old snapshot `BigIntegerLibrary-2005.01.11.devel'; see the ChangeLog file.
authorMatt McCutchen <hashproduct@gmail.com>
Sat, 27 Jan 2007 21:06:16 +0000 (16:06 -0500)
committerMatt McCutchen <hashproduct@gmail.com>
Sat, 27 Jan 2007 21:06:16 +0000 (16:06 -0500)
BigInteger.cc
BigUnsigned.cc
BigUnsignedInABase.cc
NumberlikeArray.hh
sample.cc

index 69d2dd7..471d056 100644 (file)
@@ -67,7 +67,7 @@ BigInteger::BigInteger(unsigned long x) {
                sign = zero; // NumberlikeArray did the rest
        else {
                cap = 1;
-               blk = new Blk[1];
+               blk2 = new Blk[1];
                sign = positive;
                len = 1;
                blk[0] = Blk(x);
@@ -77,13 +77,13 @@ BigInteger::BigInteger(unsigned long x) {
 BigInteger::BigInteger(long x) {
        if (x > 0) {
                cap = 1;
-               blk = new Blk[1];
+               blk2 = new Blk[1];
                sign = positive;
                len = 1;
                blk[0] = Blk(x);
        } else if (x < 0) {
                cap = 1;
-               blk = new Blk[1];
+               blk2 = new Blk[1];
                sign = negative;
                len = 1;
                blk[0] = Blk(-x);
@@ -96,7 +96,7 @@ BigInteger::BigInteger(unsigned int x) {
                sign = zero;
        else {
                cap = 1;
-               blk = new Blk[1];
+               blk2 = new Blk[1];
                sign = positive;
                len = 1;
                blk[0] = Blk(x);
@@ -106,13 +106,13 @@ BigInteger::BigInteger(unsigned int x) {
 BigInteger::BigInteger(int x) {
        if (x > 0) {
                cap = 1;
-               blk = new Blk[1];
+               blk2 = new Blk[1];
                sign = positive;
                len = 1;
                blk[0] = Blk(x);
        } else if (x < 0) {
                cap = 1;
-               blk = new Blk[1];
+               blk2 = new Blk[1];
                sign = negative;
                len = 1;
                blk[0] = Blk(-x);
@@ -125,7 +125,7 @@ BigInteger::BigInteger(unsigned short x) {
                sign = zero;
        else {
                cap = 1;
-               blk = new Blk[1];
+               blk2 = new Blk[1];
                sign = positive;
                len = 1;
                blk[0] = Blk(x);
@@ -135,13 +135,13 @@ BigInteger::BigInteger(unsigned short x) {
 BigInteger::BigInteger(short x) {
        if (x > 0) {
                cap = 1;
-               blk = new Blk[1];
+               blk2 = new Blk[1];
                sign = positive;
                len = 1;
                blk[0] = Blk(x);
        } else if (x < 0) {
                cap = 1;
-               blk = new Blk[1];
+               blk2 = new Blk[1];
                sign = negative;
                len = 1;
                blk[0] = Blk(-x);
index 4074822..83e725d 100644 (file)
@@ -20,7 +20,7 @@
 * Since 2005.01.06, NumberlikeArray uses `NULL' rather
 * than a real array if one of zero length is needed.
 * These constructors implicitly call NumberlikeArray's
-* default constructor, which sets `blk = NULL, cap = len = 0'.
+* default constructor, which sets `blk2 = NULL, cap = len = 0'.
 * So if the input number is zero, they can just return.
 * See remarks in `NumberlikeArray.hh'.
 */
@@ -30,7 +30,7 @@ BigUnsigned::BigUnsigned(unsigned long x) {
                ; // NumberlikeArray already did all the work
        else {
                cap = 1;
-               blk = new Blk[1];
+               blk2 = new Blk[1];
                len = 1;
                blk[0] = Blk(x);
        }
@@ -41,7 +41,7 @@ BigUnsigned::BigUnsigned(long x) {
                ;
        else if (x > 0) {
                cap = 1;
-               blk = new Blk[1];
+               blk2 = new Blk[1];
                len = 1;
                blk[0] = Blk(x);
        } else
@@ -53,7 +53,7 @@ BigUnsigned::BigUnsigned(unsigned int x) {
                ;
        else {
                cap = 1;
-               blk = new Blk[1];
+               blk2 = new Blk[1];
                len = 1;
                blk[0] = Blk(x);
        }
@@ -64,7 +64,7 @@ BigUnsigned::BigUnsigned(int x) {
                ;
        else if (x > 0) {
                cap = 1;
-               blk = new Blk[1];
+               blk2 = new Blk[1];
                len = 1;
                blk[0] = Blk(x);
        } else
@@ -76,7 +76,7 @@ BigUnsigned::BigUnsigned(unsigned short x) {
                ;
        else {
                cap = 1;
-               blk = new Blk[1];
+               blk2 = new Blk[1];
                len = 1;
                blk[0] = Blk(x);
        }
@@ -87,7 +87,7 @@ BigUnsigned::BigUnsigned(short x) {
                ;
        else if (x > 0) {
                cap = 1;
-               blk = new Blk[1];
+               blk2 = new Blk[1];
                len = 1;
                blk[0] = Blk(x);
        } else
index 7745adb..22fca51 100644 (file)
@@ -13,6 +13,7 @@
 */
 
 #include "BigUnsignedInABase.hh"
+#include <iostream>
 
 namespace {
        unsigned int bitLen(unsigned int x) {
@@ -27,7 +28,9 @@ namespace {
                return (a + b - 1) / b;
        }
 }
-
+       /*std::cout << "((( BigUnsigned ==> BigUnsignedInABase\n";
+       std::cout << "[ Parameter BigUnsigned @ " << (void *)(NumberlikeArray<BigUnsigned::Blk> *)(&x)
+               << ",\nresulting BigUnsignedInABase @ " << (void *)(NumberlikeArray<Digit> *)(this) << "]" << std::endl;*/
 BigUnsignedInABase::BigUnsignedInABase(const BigUnsigned &x, Base base) {
 
        // Check the base
@@ -59,6 +62,7 @@ BigUnsignedInABase::BigUnsignedInABase(const BigUnsigned &x, Base base) {
        
        // Save the actual length.
        len = digitNum;
+       /*std::cout << "BigUnsigned ==> BigUnsignedInABase )))\n";*/
 }
 
 BigUnsignedInABase::operator BigUnsigned() const {
@@ -80,9 +84,7 @@ BigUnsignedInABase::BigUnsignedInABase(const std::string &s, Base base) {
        // This pattern is seldom seen in C++, but the analogous ``this.'' is common in Java.
        this->base = base;
        
-       // `s.length()' is a `size_t', while `len' is a `NumberlikeArray::Index',
-       // also known as an `unsigned int'.  Some compilers warn without this cast.
-       len = Index(s.length());
+       len = s.length();
        allocate(len);
        
        Index digitNum, symbolNumInString;
@@ -102,6 +104,7 @@ BigUnsignedInABase::BigUnsignedInABase(const std::string &s, Base base) {
 }
 
 BigUnsignedInABase::operator std::string() const {
+       //std::cout << "((( BigUnsignedInABase ==> std::string\n";
        if (base > 36)
                throw "BigUnsignedInABase ==> std::string: The default string conversion routines use the symbol set 0-9, A-Z and therefore support only up to base 36.  You tried a conversion with a base over 36; write your own string conversion routine.";
        if (len == 0)
@@ -119,5 +122,6 @@ BigUnsignedInABase::operator std::string() const {
        }
        std::string s2(s);
        delete s;
+       //std::cout << "BigUnsignedInABase ==> std::string )))\n";
        return s2;
 }
index 3b018a4..0744d5d 100644 (file)
@@ -37,6 +37,9 @@
 *     NumberlikeArray< whatever >::getLength;
 */
 
+/*debug*/
+#include <iostream>
+
 template <class Blk>
 class NumberlikeArray {
        public:
@@ -47,7 +50,44 @@ class NumberlikeArray {
        // FIELDS
        Index cap; // The current allocated capacity of this NumberlikeArray (in blocks)
        Index len; // The actual length of the value stored in this NumberlikeArray (in blocks)
-       Blk *blk; // Dynamically allocated array of the blocks
+       Blk *blk2; // Dynamically allocated array of the blocks
+       
+       static Blk x; // trash that [] can return for out-of-range requests
+       
+       void dump() const {
+               std::cout << "Dumping NumberlikeArray @ " << (void *)(this) << '\n';
+               std::cout << "Length " << (len) << ", capacity " << (cap) << '\n';
+               for (unsigned int i = 0; i < len; i++) {
+                       std::cout << "Block " << i << ":" << blk2[i] << '\n';
+               }
+       }
+       
+       struct BoundsCheckingBlk {
+               const NumberlikeArray *na;
+               BoundsCheckingBlk(NumberlikeArray *na) {
+                       this->na = na;
+               }
+               Blk & operator [](Index index) const {
+                       if (index >= na->len) {
+                               std::cout << "== Out-of-bounds access to block " << index << ".  Affected NumberlikeArray: ==\n";
+                               na->dump();
+                               std::cout << "== End of dump. ==" << std::endl;
+                               return x;
+                       } else
+                               return na->blk2[index];
+               } // dangerous because it allows ``always writable'', but OK for now
+               /*const Blk & operator [](Index index) const {
+                       if (index >= na->len)
+                               std::cout << "OUT OF BOUNDS!  Length " << (na->len) << ", accessed " << index << std::endl;
+                       else
+                               return na->blk[index];
+               }*/
+               /*operator Blk * () {
+                       return na->blk2;
+               }*/
+       };
+       
+       BoundsCheckingBlk blk;
        
        /*
        * Change made on 2005.01.06:
@@ -64,8 +104,8 @@ class NumberlikeArray {
        */
        
        // MANAGEMENT
-       NumberlikeArray(Index c) : cap(c), len(0) { // Creates a NumberlikeArray with a capacity
-               blk = (cap > 0) ? (new Blk[cap]) : NULL;
+       NumberlikeArray(Index c) : cap(c), len(0), blk(this) { // Creates a NumberlikeArray with a capacity
+               blk2 = (cap > 0) ? (new Blk[cap]) : NULL;
        }
        void allocate(Index c); // Ensures the array has at least the indicated capacity, maybe discarding contents
        void allocateAndCopy(Index c); // Ensures the array has at least the indicated capacity, preserving its contents
@@ -88,14 +128,14 @@ class NumberlikeArray {
        * created a real `new'-allocated zero-length array.  This array would then be lost,
        * causing a small but annoying memory leak.
        */
-       NumberlikeArray() : cap(0), len(0) {
-               blk = NULL;
+       NumberlikeArray() : cap(0), len(0), blk(this) {
+               blk2 = NULL;
        }
        NumberlikeArray(const NumberlikeArray<Blk> &x); // Copy constructor
        void operator=(const NumberlikeArray<Blk> &x); // Assignment operator
        NumberlikeArray(const Blk *b, Index l); // Constructor from an array of blocks
        ~NumberlikeArray() { // Destructor
-               delete [] blk; // Does nothing and causes no error if `blk' is null.
+               delete [] blk2; // Does nothing and causes no error if `blk' is null.
        }
        
        // PICKING APART
@@ -129,6 +169,9 @@ class NumberlikeArray {
 * so other files including NumberlikeArray will be able to generate real definitions.
 */
 
+template <class Blk>
+Blk NumberlikeArray<Blk>::x = 0;
+
 template <class Blk>
 const unsigned int NumberlikeArray<Blk>::N = 8 * sizeof(Blk);
 
@@ -141,10 +184,10 @@ void NumberlikeArray<Blk>::allocate(Index c) {
        // If the requested capacity is more than the current capacity...
        if (c > cap) {
                // Delete the old number array
-               delete [] blk;
+               delete [] blk2;
                // Allocate the new array
                cap = c;
-               blk = new Blk[cap];
+               blk2 = new Blk[cap];
        }
 }
 
@@ -154,10 +197,10 @@ template <class Blk>
 void NumberlikeArray<Blk>::allocateAndCopy(Index c) {
        // If the requested capacity is more than the current capacity...
        if (c > cap) {
-               Blk *oldBlk = blk;
+               Blk *oldBlk = blk2;
                // Allocate the new number array
                cap = c;
-               blk = new Blk[cap];
+               blk2 = new Blk[cap];
                // Copy number blocks
                Index i;
                for (i = 0; i < len; i++)
@@ -169,10 +212,10 @@ void NumberlikeArray<Blk>::allocateAndCopy(Index c) {
 
 // Copy constructor
 template <class Blk>
-NumberlikeArray<Blk>::NumberlikeArray(const NumberlikeArray<Blk> &x) : len(x.len) {
+NumberlikeArray<Blk>::NumberlikeArray(const NumberlikeArray<Blk> &x) : len(x.len), blk(this) {
        // Create array
        cap = len;
-       blk = new Blk[cap];
+       blk2 = new Blk[cap];
        // Copy blocks
        Index i;
        for (i = 0; i < len; i++)
@@ -197,9 +240,9 @@ void NumberlikeArray<Blk>::operator=(const NumberlikeArray<Blk> &x) {
 
 // Constructor from an array of blocks
 template <class Blk>
-NumberlikeArray<Blk>::NumberlikeArray(const Blk *b, Index l) : cap(l), len(l) {
+NumberlikeArray<Blk>::NumberlikeArray(const Blk *b, Index l) : cap(l), len(l), blk(this) {
        // Create array
-       blk = new Blk[cap];
+       blk2 = new Blk[cap];
        // Copy blocks
        Index i;
        for (i = 0; i < len; i++)
index 3779ef4..a9e4d46 100644 (file)
--- a/sample.cc
+++ b/sample.cc
@@ -42,20 +42,30 @@ int main() {
                
                BigInteger c(a); // Copy a BigInteger.
                
+               std::cout << "here 0" << std::endl;
+               
                BigInteger d(-314159265); // c is -314159265.  The `int' literal is converted to a BigInteger.
                
                // Ahem: that's too big to be an `int' literal (or even a `long' literal)!
                // Disillusion yourself now -- this won't compile.
                //BigInteger e(3141592653589793238462643383279);
                
+               std::cout << "here 1" << std::endl;
+               
                std::string s("3141592653589793238462643383279");
                BigInteger f = easyStringToBI(s);
                // Ah.  The string is converted to a BigInteger, and strings can be as long as you want.
                
+               std::cout << "here 2" << std::endl;
+               
                std::string s2 = easyBItoString(f); // You can convert the other way too.
                
+               std::cout << "here 3" << std::endl;
+               
                std::cout << f << std::endl; // f is stringified and send to std::cout.
                
+               std::cout << "here 4" << std::endl;
+               
                /*
                * Let's do some math!
                *
@@ -89,11 +99,15 @@ int main() {
                std::cout << (g + h) << '\n' << (g - h) << '\n' << (g * h)
                        << '\n' << (g / h) << '\n' << (g % h) << std::endl;
                
+               std::cout << "here 5" << std::endl;
+               
                BigInteger i(5), j(10), k;
                // These two lines do the same thing: k is set to a BigInteger containing 15.
                k = i + j;
                k.add(i, j);
                
+               std::cout << "here 6" << std::endl;
+               
                // Let's do some heavy lifting.
                std::cout << "Powers of 3" << std::endl;
                std::cout << "How many do you want?" << std::endl;