by Marko Riedel
Combinatorics problems often require arbitrary precision arithmetic; this is the case e.g. with the graph enumeration problem that we present in the next recipe. We need an efficient Objective C wrapper class that encapsulates the GMP integer data structure. This is what GMPInt is all about.
The class GMPInt is a wrapper class whose sole instance variable holds a GMP Integer.
The class knows how to manufacture objects for the values zero and one and it can compute the factorial of an unsigned long int.
There are two initializers: we may initialize from a signed long integer or from a GMP integer.
The basic operations of arithmetic are implemented. All four use the receiver as the first operand, the argument as the second and return a GMPInt with the value of the result.
GMPInt instances can raise themselves to a power given by an unsigned long integer.
It is important that a GMPInt instance be able to represent itself as a string. This is what stringValue: does; we also implement description so that the %@ works e.g. in calls to NSLog.
Two commonly used predicates test for the receiver’s value being zero or one.
The arithmetic methods need to get at the GMP integer value of their second argument; we provide a method for this purpose. We also implement dealloc, so that we can free GMP integers, i.e. ‘‘mpz_t’’ values, that are no longer used.
The implementation is straightforward. The methods zeroObj and oneObj simply invoke the initializer with the appropriate argument.
The class method for the factorial of an unsigned long integer allocates a GMPInt, obtains a pointer to its value, initializes the value (an mpz_t), calls GMP to compute the result and returns the GMPInt.
The two initializers initialize and set the instance variable value from their arguments and put the object into an autorelease pool. This is important, because a computation may temporarily allocate a lot of integers and the space they occupy will not be released if they are not put in an autorelease pool. We’ll see more about garbage collection later on.
The method add allocates a GMPInt, initializes its value, stores the sum in this value and returns the GMPInt.
Subtraction only differs in the mpz method that is called.
The structure stays the same for multiplication.
Division is like multiplication, except that we must choose from several mpz calls according to the rounding style that is to be used and whether we need the quotient, the remainder or both. We choose “truncate” for our rounding style and we do not compute the remainder.
Exponentiation differs from the preceding methods in that there is no invocation of valPtr, because we may use the argument as is.
The method stringValue returns a string that is a decimal representation of the receiver. It uses mpz_sizeinbase to determine the number of digits and takes a possible sign and the terminating null byte into account. It invokes mpgz_get_str to write the decimal representation into a buffer it has allocated for this purpose and then produces a NSString object from the contents of the buffer, freeing it after the string object is obtained. The method description simply returns the string value of the receiver.
The predicates that test for zero and one, respectively, work by calling the mpz_comp_ui with the appropriate arguments.
The method valPtr is used by the arithmetic methods and returns the address of the mpz_t value in the receiver.
The implementation of GMPInt concludes with the method dealloc, whose purpose is to free the space that GMP has allocated for the value of the receiver, since it is no longer needed.