Merger
is a generic interface defining an iterator for merging two
ordered sequences.
GENERIC INTERFACEMerger (Elem);
WhereElem.T
is a reference type andElem
contains
PROCEDURE Compare(a, b: Elem.T): [-1..1];Compare
must be a total order. It may be declared with a parameter mode of eitherVALUE
orREADONLY
, but notVAR
.
TYPE T <: Public; Public = OBJECT METHODS next(VAR a, b: Elem.T): BOOLEAN RAISES ANY; compare(READONLY a, b: Elem.T): [-1..1]; getA(): Elem.T RAISES ANY := NIL; getB(): Elem.T RAISES ANY := NIL; END;A
Merger.T(Elem)
, or merger, is an iterator which yields successive
elements from a merge of two sorted input sequences. The merger
gets elements from the two input sequences by calling its methods
m.getA()
and m.getB()
. These methods are undefined by
default, and must be appropriately overridden through subtyping.
Each method must return the next element of its input sequence,
or NIL
if the sequence is exhausted. The elements so returned
must be ordered as defined by m.compare(a, b)
.
The default implementation of m.compare(a, b)
calls
Elem.Compare(a, b)
. It may be overridden if desired.
The elements of the merged sequence are obtained by calls to
m.next(a, b)
. This method returns FALSE
when the merged
sequence is exhausted; otherwise, it returns TRUE
. The VAR
parameters a
and/or b
are set to the next element of the merged
sequence, depending on which input sequence it came from. If the
elements from the two input sequences are equal according to
m.compare(a, b)
, then both a
and b
are set. Otherwise,
one of them is set to NIL
.
The RAISES
set for the next
method is the union of the RAISES
sets of getA
and getB
.
END Merger.