package edu.vt.marian.search; import java.io.*; import java.util.*; import edu.vt.marian.common.*; /** A Vector-based cache for any WtdObjSet or WtdObjSet-like object. @author Robert France
JDK Version : 1.1.5
@see WtdObjSet
@see VectorWtdObjSetEnum
*/
public class VectorWtdObjSet implements WtdObjSet
{
protected Debug debug; // The eternal Debug object.
protected Vector v; // Used to store WtdObjs, and only WtdObjs.
public class Enum implements WtdObjSetEnumeration
{
protected int nextElt;
public Enum()
{
nextElt = 0;
}
public synchronized Object nextElement()
{
if ( nextElt >= v.size() )
throw( new NoSuchElementException() );
return( v.elementAt(nextElt++) );
}
public boolean hasMoreElements()
{
return( nextElt < v.size() );
}
/**
Skip forward a certain number of elements.
@param k How many elements to skip.
@exception NoSuchElementException
*/
public synchronized void skip(int k)
{
if ( nextElt+k >= v.size() )
throw( new NoSuchElementException() );
nextElt += k;
}
/**
Copy a certain number of elements into a bag.
@param num How many elements to copy.
@param sampleBag The WtdObjBag to add elements to.
@return OK There were Num elements left, and they all were copied well.
NOTE: We cannot use Vector.contains() (or even indexOf()) here
because Vectors compare objects by identity, not equality.
*/
public Weight isElt(FullID id)
{
Enumeration elts = v.elements();
try
{
while( true )
{
WtdObj w = (WtdObj) elts.nextElement();
if ( id.equals(w) )
return( w.getWeight() );
}
} catch( NoSuchElementException e )
{
return( Weight.bottomWt );
}
}
/**
Return the exact number of elements in this set (may be costly).
*/
public int exactSize()
{
return( v.size() );
}
/**
Return the approximate number of elements in this set (can be cheap
and dirty).
*/
public int approxSize()
{
return( v.size() );
}
/**
Return the maximum number of elements in this set (can be cheap
and dirty).
*/
public int maxSize()
{
return( v.size() );
}
/**
Return a human-readable string for this entire set (may be large).
*/
public String toString()
{
StringBuffer sb = new StringBuffer(v.size()*30 + 2);
sb.append('{');
Enumeration elts = v.elements();
try
{
WtdObj w = (WtdObj) elts.nextElement();
sb.append(w.toString());
while( true )
{
w = (WtdObj) elts.nextElement();
sb.append(',');
sb.append(w.toString());
}
} catch( NoSuchElementException e ) { }
sb.append('}');
return( sb.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());
}
}
}
sb.append('}');
return( sb.toString() );
}
};
anything else Problems.
@exception NoSuchElementException
*/
public synchronized int sample(int num, WtdObjBag sampleBag)
{
int i;
WtdObj w;
int err;
for (i=0; i
anything else Problems.
@exception NoSuchElementException
*/
public synchronized int sampleToWt(Weight minWt, WtdObjBag sampleBag)
{
WtdObj w;
int err;
while (true)
{
if ( nextElt >= v.size() )
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 );
}
/**
Return exact number of elements remaining in the parent set.
@return The exact number of elements still to be enumerated.
*/
public int exactNumRemaining()
{
return( v.size() - nextElt );
}
/**
Return approximate number of elements left.
@return The approximate number of elements still to be enumerated.
*/
public int approxNumRemaining()
{
return( v.size() - nextElt );
}
/**
Return maximum number of elements left.
@return The maximum number of elements still to be enumerated.
*/
public int maxNumRemaining()
{
return( v.size() - nextElt );
}
}
public VectorWtdObjSet(Debug d)
{
debug = d;
v = new Vector();
}
public VectorWtdObjSet(int expectedSize, Debug d)
{
debug = d;
v = new Vector(expectedSize);
}
public int add(WtdObj w)
{
try
{
WtdObj lastElt = (WtdObj) v.lastElement();
if ( w.compare(lastElt) == Weight.LOWER )
{
int i;
for (i=0; i