2 * This mechanism prevents files from being included twice.
3 * Each file gets its own `id' (here `NUMBERLIKEARRAY').
4 * When `#include'd, this file checks whether its `id' has
5 * already been flagged. If not, it flags the `id' and
6 * loads the declarations.
8 #ifndef NUMBERLIKEARRAY
9 #define NUMBERLIKEARRAY
11 // An essential memory-management constant.
12 // I wish this were built into C++ just as it is in Java.
18 * A NumberlikeArray<Blk> object holds a dynamically
19 * allocated array of Blk. It provides certain basic
20 * memory management features needed by both BigUnsigned
21 * and BigUnsignedInABase, which are both derived from it.
23 * NumberlikeArray provides no information hiding, so make
24 * sure you know what you are doing if you use it directly.
25 * Classes derived from it will probably wish to pass on
26 * some members of NumberlikeArray to their clients while
27 * keeping some safe for themselves. These classes should
28 * use protected inheritance and manually make some members
29 * public with declarations like this:
32 * NumberlikeArray< whatever >::getLength;
36 class NumberlikeArray {
39 typedef unsigned int Index; // Type for the index of a block in the array
40 static const unsigned int N; // The number of bits in a block, defined below.
43 Index cap; // The current allocated capacity of this NumberlikeArray (in blocks)
44 Index len; // The actual length of the value stored in this NumberlikeArray (in blocks)
45 Blk *blk; // Dynamically allocated array of the blocks
48 * Change made on 2005.01.06:
50 * If a zero-length NumberlikeArray is desired, no array is actually allocated.
51 * Instead, `blk' is set to `NULL', and `cap' and `len' are zero as usual.
53 * `blk' is never dereferenced if the array has zero length. Furthermore,
54 * `delete NULL;' does nothing and causes no error. Therefore, we can use
55 * `NULL' as if it were a zero-length array from `new'.
57 * This is a great convenience because the only code that need be changed
58 * is the array allocation code. All other code will still work fine.
62 NumberlikeArray(Index c) : cap(c), len(0) { // Creates a NumberlikeArray with a capacity
63 blk = (cap > 0) ? (new Blk[cap]) : NULL;
65 void allocate(Index c); // Ensures the array has at least the indicated capacity, maybe discarding contents
66 void allocateAndCopy(Index c); // Ensures the array has at least the indicated capacity, preserving its contents
69 * Default constructor.
71 * If a class derived from NumberlikeArray knows at initializer time what size array
72 * it wants, it can call the first constructor listed above in an initializer.
74 * Otherwise, this default constructor will be implicitly invoked, pointing `blk' to
75 * `NULL', a fake zero-length block array. The derived class can allocate the desired
76 * array itself and overwrite `blk'; it need not `delete [] blk' first.
78 * This change fixes a memory leak reported by Milan Tomic on 2005.01.06.
79 * Integer-type-to-BigUnsigned (and BigInteger) conversion constructors have always
80 * allocated their own array of length 0 or 1 after seeing whether the input is zero.
81 * But when the NumberlikeArray transition occurred, these constructors contained an
82 * implicit initializer call to the old NumberlikeArray default constructor, which
83 * created a real `new'-allocated zero-length array. This array would then be lost,
84 * causing a small but annoying memory leak.
86 NumberlikeArray() : cap(0), len(0) {
89 NumberlikeArray(const NumberlikeArray<Blk> &x); // Copy constructor
90 void operator=(const NumberlikeArray<Blk> &x); // Assignment operator
91 NumberlikeArray(const Blk *b, Index l); // Constructor from an array of blocks
92 ~NumberlikeArray() { // Destructor
93 delete [] blk; // Does nothing and causes no error if `blk' is null.
97 // These accessors can be used to get the pieces of the value
98 Index getCapacity() const { return cap; }
99 Index getLength() const { return len; }
100 Blk getBlock(Index i) const { return blk[i]; };
101 bool isEmpty() const { return len == 0; }
103 // Equality comparison: checks if arrays have same length and matching values
104 // Derived classes may wish to override these if differing arrays can
105 // sometimes be considered equivalent.
106 bool operator ==(const NumberlikeArray<Blk> &x) const;
107 bool operator !=(const NumberlikeArray<Blk> &x) const { return !operator ==(x); }
112 * =================================
113 * BELOW THIS POINT are template definitions; above are declarations.
115 * Definitions would ordinarily belong in a file NumberlikeArray.cc so that they would
116 * be compiled once into NumberlikeArray.o and then linked.
118 * However, because of the way templates are usually implemented,
119 * template ``definitions'' are treated as declarations by the compiler.
120 * When someone uses an instance of the template, definitions are generated,
121 * and the linker is smart enough to toss duplicate definitions for the same
122 * instance generated by different files.
124 * Thus, the template ``definitions'' for NumberlikeArray must appear in this header file
125 * so other files including NumberlikeArray will be able to generate real definitions.
129 const unsigned int NumberlikeArray<Blk>::N = 8 * sizeof(Blk);
133 // This routine is called to ensure the array is at least a
134 // certain size before another value is written into it.
136 void NumberlikeArray<Blk>::allocate(Index c) {
137 // If the requested capacity is more than the current capacity...
139 // Delete the old number array
141 // Allocate the new array
147 // This routine is called to ensure the array is at least a
148 // certain size without losing its contents.
150 void NumberlikeArray<Blk>::allocateAndCopy(Index c) {
151 // If the requested capacity is more than the current capacity...
154 // Allocate the new number array
157 // Copy number blocks
159 for (i = 0; i < len; i++)
161 // Delete the old array
168 NumberlikeArray<Blk>::NumberlikeArray(const NumberlikeArray<Blk> &x) : len(x.len) {
174 for (i = 0; i < len; i++)
178 // Assignment operator
180 void NumberlikeArray<Blk>::operator=(const NumberlikeArray<Blk> &x) {
181 // Calls like a = a have no effect
186 // Expand array if necessary
188 // Copy number blocks
190 for (i = 0; i < len; i++)
194 // Constructor from an array of blocks
196 NumberlikeArray<Blk>::NumberlikeArray(const Blk *b, Index l) : cap(l), len(l) {
201 for (i = 0; i < len; i++)
207 // This uses == to compare Blks for equality.
208 // Therefore, Blks must have an == operator with the desired semantics.
210 bool NumberlikeArray<Blk>::operator ==(const NumberlikeArray<Blk> &x) const {
211 // Different lengths imply different objects.
215 // Compare matching blocks one by one.
217 for (i = 0; i < len; i++)
218 if (blk[i] != x.blk[i])
220 // If no blocks differed, the objects are equal.