package edu.vt.marian.search; import java.io.*; import java.util.*; import edu.vt.marian.common.*; /** A Vector-based cache for any Enumeration. Given any class that implements Enumeration and returns elements in Weight order, construct the Weighted Object Set behavior on top of it.

NOTE: No checking is currently done to ensure that the base Enumeration is (a) returning unique objects or (b) returning them in non-increasing Weight order. Use this baby only very carefully. @author Robert France

JDK Version : 1.1.5 @see WtdObjSet @see VectorWtdObjSet @see java.util.Enumeration */ public class EnumCacheWtdObjSet extends VectorWtdObjSet implements WtdObjSet { protected Enumeration base; /** An Enumeration over an Enumeration -- to be precise, one of many possible Weighted Object Set Enumerations that share a single underlying simple Enumeration.

JDK Version : 1.1.5 @author Robert France @see WtdObjSetEnumeration */ public class Enum implements WtdObjSetEnumeration { protected int nextElt; public Enum() { nextElt = 0; } public synchronized Object nextElement() throws NoSuchElementException { if ( nextElt >= v.size() ) { Object next = base.nextElement(); v.addElement(next); } return( v.elementAt(nextElt++) ); } public boolean hasMoreElements() { return( (nextElt < v.size()) || base.hasMoreElements() ); } public synchronized void skip(int k) { nextElt += k; while ( nextElt >= v.size() ) { Object next = base.nextElement(); v.addElement(next); } } public synchronized int sample(int num, WtdObjBag sampleBag) { int i; WtdObj w; int err; for (i=0; i= v.size() ) { try { Object next = base.nextElement(); v.addElement(next); } catch( NoSuchElementException e) { break; } } w = (WtdObj) v.elementAt(nextElt++); if ( (err = sampleBag.add(w)) != ReturnCodes.OK) return( err ); } return( ReturnCodes.OK ); } public synchronized int sampleToWt(Weight minWt, WtdObjBag sampleBag) { WtdObj w; int err; while (true) { if ( nextElt >= v.size() ) { try { Object next = base.nextElement(); v.addElement(next); } catch( NoSuchElementException e) { break; } } w = (WtdObj) v.elementAt(nextElt); if ( minWt.compare(w.getWeight()) == Weight.HIGHER ) break; nextElt++; if ( (err = sampleBag.add(w)) != ReturnCodes.OK) return( err ); } return( ReturnCodes.OK ); } public int exactNumRemaining() { try { while( true ) { Object next = base.nextElement(); v.addElement(next); } } catch( NoSuchElementException e) { } return( v.size() - nextElt ); } public int approxNumRemaining() { return( exactNumRemaining() ); } public int maxNumRemaining() { return( exactNumRemaining() ); } } // end EnumCacheWtdObjSet.Enum public EnumCacheWtdObjSet(Enumeration baseEnum, Debug d) { super(d); if (baseEnum == null) { EmptyWtdObjSet baseSet = new EmptyWtdObjSet(d); base = baseSet.elements(); } else { base = baseEnum; } } /** Create an Enumeration for this set. */ public WtdObjSetEnumeration elements() { return( new Enum() ); } /** Is this set empty? @return true / false */ public boolean isEmpty() { return( v.isEmpty() && ! base.hasMoreElements() ); } /** Is a given ID an element of this set? @param id The (ID of the) element to be tested. @return null -- the element is not in the set.
any valid Weight -- the element is in the set with that Weight. */ public Weight isElt(FullID id) { while( base.hasMoreElements() ) { v.addElement( base.nextElement() ); } return( super.isElt(id) ); } /** Return the exact number of elements in this set (may be costly). */ public int exactSize() { try { while( true ) { Object next = base.nextElement(); v.addElement(next); } } catch( NoSuchElementException e) { } return( v.size() ); } /** Return the approximate number of elements in this set (can be cheap and dirty). */ public int approxSize() { return( exactSize() ); } /** Return the maximum number of elements in this set (can be cheap and dirty). */ public int maxSize() { return( exactSize() ); } /** Return a human-readable string for this entire set (may be large). */ public String toString() { while( base.hasMoreElements() ) { v.addElement( base.nextElement() ); } return( super.toString() ); } /** Return a short human-readable string that quickly describes this set. */ public String profile() { StringBuffer sb = new StringBuffer(100); sb.append('{'); if ( v.size() > 0 ) { WtdObj w = (WtdObj) v.firstElement(); sb.append(w.toString()); if ( v.size() > 1 ) { w = (WtdObj) v.elementAt(1); sb.append(','); sb.append(w.toString()); if ( v.size() > 2 ) { sb.append(','); if ( v.size() > 3 ) { sb.append("...[" + (v.size()-3) + "]...,"); } w = (WtdObj) v.lastElement(); sb.append(w.toString()); } } } if ( base.hasMoreElements() ) { sb.append(" / ..."); } sb.append('}'); return( sb.toString() ); } };