package lrs.visitor; import lrs.*; /** * Uses reverse accumulation to remove duplicates from the host list. * @author Dung X. Nguyen - Copyright 2005 - All rights reserved. */ public class RemDupFwd implements IAlgo { public final static RemDupFwd Singleton = new RemDupFwd(); private RemDupFwd() { } /** */ public Object emptyCase(LRStruct host, Object... nu) { return host; } /** */ public Object nonEmptyCase(final LRStruct host, Object... nu) { final LRStruct seenSoFar = new LRStruct(); seenSoFar.insertFront(host.getFirst()); return host.getRest().execute(new IAlgo() { public Object emptyCase(LRStruct h, Object... nu) { return host; } /** */ public Object nonEmptyCase(LRStruct h, Object... nu) { // if h.first has been seen, remove it from h and recurs: if ((Boolean)seenSoFar.execute(Contain.Singleton, h.getFirst())) { h.removeFront(); return h.execute(this); } // else add h.first to the seenSoFar list and recurs on h.rest: else { seenSoFar.insertFront(h.getFirst()); return h.getRest().execute(this); } } }); } }