Package it.unich.jgmp
Here are some guidelines we have followed in developing the API for the JGMP library. We tried to adhere to Java conventions, while keeping methods discoverable by people who already know the GMP library. These rules may be overriden in specific cases.
Naming conventions: the base name
For each GMP function, a base name is determined by the original name as follows:
- the prefix (
mpf_,mpq_,mpz_orgmp_) is removed; the same happens to the components_si,_dand_str, if this does not clause signature clashes; - the component
_uiis kept to help the user distinguish unsigned long parameters; - the postfix
_p, which sometimes marks functions returning booleans, is either removed or replaced by the prefixiswhen this makes sense; - the rest of the name is transformed to camel case, by converting to uppercase the first letter after each underscore;
- name which could cause conflicts with Java reserved words are modified:
for example,
importandexportbecomebufferImportandbufferExportrespectively.
Naming conventions: general rules
Given a GMP function, the following rules dictate how to derive name and
signature for the corrisponding method in JGMP. Let
mptype be the family of the function, which may be
either mpf, mpq or mpz. The function becomes a method
of the corresponding MPF, MPQ or MPZ class. In
particular:
- the function
mptype_clearis only used internally; - the functions
mptype_initsandmptype_clearsare only used internally and are not exposed by the high-level classes; - if
baseNamebegins withrealloc2,setorswap, we create a method calledbaseNamewhich calls the original function, implicitly usingthisas the firstmptypeparameter; - if
baseNamebegins withinit, we create a side-effect free static method (see later); - for all the other functions:
- if the function has at least a non constant
mptype_tparameter, then we create a methodbaseNameAssignwhich calls the original function, implicitly usingthisas the first non-constantmptype_tparameter; - if the function has at least a non constant
mptype_tparameter and a constant one, we create a methodbaseNameAssignwhich calls the original function, implicitly usingthisas both the first non-constantmptype_tparameter and the most relevant constantmptype_tparameter, with the exception of a few cases where such as a method would not be particularly useful; - we create e side-effect free method called
baseName, with the exception of a few cases where such as a method would not be particularly useful.
- if the function has at least a non constant
this should be provided explicitly.
Other methods and constructors which conform to standard Java naming conventions might be provided.
Side-effect free methods
In order to simplify the development of code without side-effects, we enrich
the API provided by JGMP with side-effect free methods, which builds new
objects instead of modifying old ones.
First of all, we distinguish between input and output parameters for the GMP
function. Some parameters may have both an input and an output nature. The
side-effect free method takes all input parameters in its signature, with the
exception of the first input mptype_t parameter which
is mapped to this. If there are no input
mptype_t parameters, the method will be static. The
method creates new objects for the output parameters, eventually cloning the
ones also used as an input. After calling the GMP functions, the return value
and all the output parameters are returned by the method, eventually packed
in an org.javatuples.Tuple, from left to right
according to the function signature. Sometimes, when the first
mptype_t input parameter comes after other input
parameters, this procedure may lead to a signature clash. In this case, the
name of the method is changed into baseNameReverse.
Type mapping
The types of the formal parameters and return value of a GMP function are mapped to the types of the JGMP method as follows:
intandlong) Generally mapped to the respective Java types. This may cause truncation when the nativelongis only 32 bit. If anintis used to represent a boolean, then thebooleantype is used in JGMP.unsigned long,size_t,mp_bitcnt,mp_size_tandmp_exp_t) Mapped tolong. This may cause truncation when the native size of these types is only 32 bit. Note thatunsigned long,size_tandmp_bitcntare natively unsigned. Handle with care.mpz_t) Mapped toMPZ.mpq_t) Mapped toMPQ.mpf_t) Mapped toMPF.gmp_randstate_t) Mapped toRandState.const char*) Mapped toString.char*) All functions requiring a non-constchar*may be called with anullpointer. Since this is much easier to handle, we choose to always follow this pattern. Therefore, non-constantchar*are always removed from the input parameters. Whenchar*is used a return value, it is mapped to aString.void) If a method would returnvoid, we prefer to returnthisinstead, in order to ease chaining method calls.
-
ClassDescriptionThe allocation monitor keeps track of the amount of native memory allocated by GMP, and calls the Java garbage collector when it finds that too much native memory is being used.The custom allocator function.The custom deallocator.The custom reallocator.Collects global variables and static methods which do no fit in more specific classes.Multi-precision floating point numbers.Cleaning action for the
MPFclass.Multi-precision rational numbers.Cleaning action for theMPQclass.Multi-precision integer number.Cleaning action for theMPZclass.Result enumeration for themethod.invalid reference
isProbabPrimeCurrent state of a random number generator.Cleaning action for theRandStateclass.