The file compare.h contains a few function declarations pertaining to the computation of the comparison predicate described in Compare. Some of the work is done by static functions in compare.c that are not recommended entry points to the library.
This function should be called once before the first call to
avm_comparison
, as it initializes some necessary internal data structures.
This function can be used to check for memory leaks, by detecting unreclaimed storage at the end of a run. The data structures relevant to comparison that could be reported as unreclaimed are known as “decision” nodes, but these should always be handled properly by the library without intervention. If
avm_count_lists
is also being used, the call to this function must precede it.
This function takes a list operand representing a pair of trees and returns a list representing the logical value of their equality. If the operand is
NULL
, a message of invalid comparison is returned and the*
fault is set to a non-zero value. If thehead
of the operand is unequal to thetail
, aNULL
value is returned. If they are equal, a list is returned whosehead
andtail
are bothNULL
. The equality in question is structural rather than pointer equality.The list operand to this function may be modified by this function, but not in a way that should make any difference to a client program. If two lists are found to be equal, or if even two sublists are found to be equal in the course of the comparison, one of them is deallocated and made to point to the other. This action saves memory and may make subsequent comparisons faster. However, it could disrupt client programs that happen to be holding stale list pointers.
As of
avram
version 0.6.0, a logical field calleddiscontiguous
has been added to thenode
record type declared inlists.h
, which is checked by the comparison function. If a list node has itsdiscontiguous
field set to a non-zero value, and if it also has a non-nullvalue
field, then it won't be deallocated in the course of comparison even if it is found to be equal to something else. This feature can be used by client modules to create lists in which value fields refer to data structures that are meant to exist independently of them. See mpfr.c for an example.This function is likely to have better performance and memory usage than a naive implementation of comparison, for the above reasons and also because of optimizations pertaining to comparison of lists representing characters. Moreover, it is not subject to stack overflow exceptions because it is not written in a recursive style.