1 #ifndef NUMBERLIKEARRAY_H
2 #define NUMBERLIKEARRAY_H
4 // Make sure we have NULL.
9 /* A NumberlikeArray<Blk> object holds a heap-allocated array of Blk with a
10 * length and a capacity and provides basic memory management features.
11 * BigUnsigned and BigUnsignedInABase both subclass it.
13 * NumberlikeArray provides no information hiding. Subclasses should use
14 * nonpublic inheritance and manually expose members as desired using
15 * declarations like this:
18 * NumberlikeArray< the-type-argument >::getLength;
21 class NumberlikeArray {
24 // Type for the index of a block in the array
25 typedef unsigned int Index;
26 // The number of bits in a block, defined below.
27 static const unsigned int N;
29 // The current allocated capacity of this NumberlikeArray (in blocks)
31 // The actual length of the value stored in this NumberlikeArray (in blocks)
33 // Heap-allocated array of the blocks (can be NULL if len == 0)
36 // Constructs a ``zero'' NumberlikeArray with the given capacity.
37 NumberlikeArray(Index c) : cap(c), len(0) {
38 blk = (cap > 0) ? (new Blk[cap]) : NULL;
41 /* Constructs a zero NumberlikeArray without allocating a backing array.
42 * A subclass that doesn't know the needed capacity at initialization
43 * time can use this constructor and then overwrite blk without first
45 NumberlikeArray() : cap(0), len(0) {
49 // Destructor. Note that `delete NULL' is a no-op.
54 /* Ensures that the array has at least the requested capacity; may
55 * destroy the contents. */
56 void allocate(Index c);
58 /* Ensures that the array has at least the requested capacity; does not
59 * destroy the contents. */
60 void allocateAndCopy(Index c);
63 NumberlikeArray(const NumberlikeArray<Blk> &x);
65 // Assignment operator
66 void operator=(const NumberlikeArray<Blk> &x);
68 // Constructor that copies from a given array of blocks
69 NumberlikeArray(const Blk *b, Index blen);
72 Index getCapacity() const { return cap; }
73 Index getLength() const { return len; }
74 Blk getBlock(Index i) const { return blk[i]; }
75 bool isEmpty() const { return len == 0; }
77 /* Equality comparison: checks if both objects have the same length and
78 * equal (==) array elements to that length. Subclasses may wish to
80 bool operator ==(const NumberlikeArray<Blk> &x) const;
82 bool operator !=(const NumberlikeArray<Blk> &x) const {
83 return !operator ==(x);
87 /* BEGIN TEMPLATE DEFINITIONS. They are present here so that source files that
88 * include this header file can generate the necessary real definitions. */
91 const unsigned int NumberlikeArray<Blk>::N = 8 * sizeof(Blk);
94 void NumberlikeArray<Blk>::allocate(Index c) {
95 // If the requested capacity is more than the current capacity...
97 // Delete the old number array
99 // Allocate the new array
106 void NumberlikeArray<Blk>::allocateAndCopy(Index c) {
107 // If the requested capacity is more than the current capacity...
110 // Allocate the new number array
113 // Copy number blocks
115 for (i = 0; i < len; i++)
117 // Delete the old array
123 NumberlikeArray<Blk>::NumberlikeArray(const NumberlikeArray<Blk> &x)
130 for (i = 0; i < len; i++)
135 void NumberlikeArray<Blk>::operator=(const NumberlikeArray<Blk> &x) {
136 /* Calls like a = a have no effect; catch them before the aliasing
137 * causes a problem */
142 // Expand array if necessary
144 // Copy number blocks
146 for (i = 0; i < len; i++)
151 NumberlikeArray<Blk>::NumberlikeArray(const Blk *b, Index blen)
152 : cap(blen), len(blen) {
157 for (i = 0; i < len; i++)
162 bool NumberlikeArray<Blk>::operator ==(const NumberlikeArray<Blk> &x) const {
164 // Definitely unequal.
167 // Compare corresponding blocks one by one.
169 for (i = 0; i < len; i++)
170 if (blk[i] != x.blk[i])
172 // No blocks differed, so the objects are equal.