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
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() );
}
};