-
- 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:
- *
- * If a zero-length NumberlikeArray is desired, no array is actually allocated.
- * Instead, `blk' is set to `NULL', and `cap' and `len' are zero as usual.
- *
- * `blk' is never dereferenced if the array has zero length. Furthermore,
- * `delete NULL;' does nothing and causes no error. Therefore, we can use
- * `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.
- */
-
- // MANAGEMENT
- 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
-
- /*
- * Default constructor.
- *
- * If a class derived from NumberlikeArray knows at initializer time what size array
- * it wants, it can call the first constructor listed above in an initializer.
- *
- * Otherwise, this default constructor will be implicitly invoked, pointing `blk' to
- * `NULL', a fake zero-length block array. The derived class can allocate the desired
- * array itself and overwrite `blk'; it need not `delete [] blk' first.
- *
- * This change fixes a memory leak reported by Milan Tomic on 2005.01.06.
- * Integer-type-to-BigUnsigned (and BigInteger) conversion constructors have always
- * allocated their own array of length 0 or 1 after seeing whether the input is zero.
- * But when the NumberlikeArray transition occurred, these constructors contained an
- * implicit initializer call to the old NumberlikeArray default constructor, which
- * 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;