package lrs.visitor; import lrs.*; /** * Merges the sorted input list into the sorted host list. * In the end, host and the input list share the same nodes and are sorted. * @author Dung X. Nguyen - Copyright 2005 - All rights reserved. */ public class Merge implements IAlgo { public final static Merge Singleton = new Merge(); private Merge() { } /** * @param host is empty. * @param other [0] an LRStruct * @return null */ public Object emptyCase(LRStruct host, Object... other) { return host.execute (Share.Singleton, other[0]); } /** * @param host is not empty * @param other [0] an LRStruct * @return null */ public Object nonEmptyCase(LRStruct host, Object... other) { return ((LRStruct)other[0]).execute (new IAlgo() { public Object emptyCase(LRStruct h, Object... inp) { return h.execute (Share.Singleton, inp[0]); } /** * @param host a non-empty LRStruct * @param inp [0] a non-empty LRStruct. */ public Object nonEmptyCase(LRStruct h, Object... inp) { LRStruct inList = (LRStruct)inp[0]; int hFirst = (Integer)h.getFirst(); int inpFirst = (Integer)inList.getFirst(); if (hFirst <= inpFirst) { h.getRest().execute(this, inList); return inList.execute(Share.Singleton, h); } else { inList.getRest().execute (this, h); return h.execute(Share.Singleton, inList); } } }, host); } }