X-Git-Url: https://mattmccutchen.net/bigint/bigint.git/blobdiff_plain/e257a1b25b880dc6246189e7ede1d0ea3db6337d..5ff40cf5d6e822051da902b041ae7ae8f545123e:/NumberlikeArray.hh diff --git a/NumberlikeArray.hh b/NumberlikeArray.hh index 0744d5d..d884d37 100644 --- a/NumberlikeArray.hh +++ b/NumberlikeArray.hh @@ -1,6 +1,5 @@ /* * Matt McCutchen's Big Integer Library -* http://mysite.verizon.net/mccutchen/bigint/ */ /* @@ -37,58 +36,18 @@ * NumberlikeArray< whatever >::getLength; */ -/*debug*/ -#include - template class NumberlikeArray { public: - + typedef unsigned int Index; // Type for the index of a block in the array static const unsigned int N; // The number of bits in a block, defined below. - + // 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 *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; - + Blk *blk; // Dynamically allocated array of the blocks + /* * Change made on 2005.01.06: * @@ -100,16 +59,16 @@ class NumberlikeArray { * `NULL' as if it were a zero-length array from `new'. * * This is a great convenience because the only code that need be changed - * is the array allocation code. All other code will still work file. + * is the array allocation code. All other code will still work fine. */ - + // MANAGEMENT - NumberlikeArray(Index c) : cap(c), len(0), blk(this) { // Creates a NumberlikeArray with a capacity - blk2 = (cap > 0) ? (new Blk[cap]) : NULL; + NumberlikeArray(Index c) : cap(c), len(0) { // Creates a NumberlikeArray with a capacity + blk = (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 - + /* * Default constructor. * @@ -128,32 +87,33 @@ 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(this) { - blk2 = NULL; + NumberlikeArray() : cap(0), len(0) { + blk = NULL; } NumberlikeArray(const NumberlikeArray &x); // Copy constructor void operator=(const NumberlikeArray &x); // Assignment operator NumberlikeArray(const Blk *b, Index l); // Constructor from an array of blocks ~NumberlikeArray() { // Destructor - delete [] blk2; // Does nothing and causes no error if `blk' is null. + delete [] blk; // Does nothing and causes no error if `blk' is null. } - + // PICKING APART // These accessors can be used to get the pieces of the value Index getCapacity() const { return cap; } Index getLength() const { return len; } Blk getBlock(Index i) const { return blk[i]; }; bool isEmpty() const { return len == 0; } - + // Equality comparison: checks if arrays have same length and matching values // Derived classes may wish to override these if differing arrays can // sometimes be considered equivalent. bool operator ==(const NumberlikeArray &x) const; - bool operator !=(const NumberlikeArray &x) const; - + bool operator !=(const NumberlikeArray &x) const { return !operator ==(x); } + }; /* +* ================================= * BELOW THIS POINT are template definitions; above are declarations. * * Definitions would ordinarily belong in a file NumberlikeArray.cc so that they would @@ -169,9 +129,6 @@ class NumberlikeArray { * so other files including NumberlikeArray will be able to generate real definitions. */ -template -Blk NumberlikeArray::x = 0; - template const unsigned int NumberlikeArray::N = 8 * sizeof(Blk); @@ -184,10 +141,10 @@ void NumberlikeArray::allocate(Index c) { // If the requested capacity is more than the current capacity... if (c > cap) { // Delete the old number array - delete [] blk2; + delete [] blk; // Allocate the new array cap = c; - blk2 = new Blk[cap]; + blk = new Blk[cap]; } } @@ -197,10 +154,10 @@ template void NumberlikeArray::allocateAndCopy(Index c) { // If the requested capacity is more than the current capacity... if (c > cap) { - Blk *oldBlk = blk2; + Blk *oldBlk = blk; // Allocate the new number array cap = c; - blk2 = new Blk[cap]; + blk = new Blk[cap]; // Copy number blocks Index i; for (i = 0; i < len; i++) @@ -212,10 +169,10 @@ void NumberlikeArray::allocateAndCopy(Index c) { // Copy constructor template -NumberlikeArray::NumberlikeArray(const NumberlikeArray &x) : len(x.len), blk(this) { +NumberlikeArray::NumberlikeArray(const NumberlikeArray &x) : len(x.len) { // Create array cap = len; - blk2 = new Blk[cap]; + blk = new Blk[cap]; // Copy blocks Index i; for (i = 0; i < len; i++) @@ -240,9 +197,9 @@ void NumberlikeArray::operator=(const NumberlikeArray &x) { // Constructor from an array of blocks template -NumberlikeArray::NumberlikeArray(const Blk *b, Index l) : cap(l), len(l), blk(this) { +NumberlikeArray::NumberlikeArray(const Blk *b, Index l) : cap(l), len(l) { // Create array - blk2 = new Blk[cap]; + blk = new Blk[cap]; // Copy blocks Index i; for (i = 0; i < len; i++)