template <typename Object, typename Sequence> void Merger<Object, Sequence>:: // generic merge function merger(const Sequence& A, const Sequence& B, Sequence& C) { iterA = A.elements(); // A's elements iterB = B.elements(); // B's elements bool aExists = getFromA(); // test for more in A bool bExists = getFromB(); // test for more in B while (aExists && bExists) { // main merging loop if (a < b) { aIsLess(a, C); aExists = getFromA(); } // a is smaller else if (a == b) { // both are equal bothEqual(a, b, C); aExists = getFromA(); bExists = getFromB(); } else { bIsLess(b, C); bExists = getFromB(); } // b is smaller } // copy remainders while (aExists) { aIsLess(a, C); aExists = getFromA(); } while (bExists) { bIsLess(b, C); bExists = getFromB(); } }