/* * TclList.java * * Copyright (c) 1997 Sun Microsystems, Inc. * * See the file "license.terms" for information on usage and * redistribution of this file, and for a DISCLAIMER OF ALL * WARRANTIES. * * RCS: @(#) $Id: TclList.java,v 1.1.1.1 1998/10/14 21:09:20 cvsadmin Exp $ * */ package tcl.lang; import java.util.*; /** * This class implements the list object type in Tcl. */ public class TclList extends InternalRep { /** * Internal representation of a list value. */ private Vector vector; /** * Create a new empty Tcl List. */ private TclList() { vector = new Vector(); } /** * Create a new empty Tcl List, with the vector pre-allocated to * the given size. * * @param size the number of slots pre-allocated in the vector. */ private TclList(int size) { vector = new Vector(size); } /** * Called to free any storage for the type's internal rep. * @param obj the TclObject that contains this internalRep. */ protected void dispose() { int size = vector.size(); for (int i=0; i * * The objects referenced by the returned array should be treated * as readonly and their ref counts are _not_ incremented; the * caller must do that if it holds on to a reference. * * @param interp the current interpreter. * @param tobj the list to sort. * @return a TclObject array of the elements in a list object. * @exception TclException if tobj is not a valid list. */ public static TclObject[] getElements(Interp interp, TclObject tobj) throws TclException { setListFromAny(interp, tobj); TclList tlist = (TclList)tobj.getInternalRep(); int size = tlist.vector.size(); TclObject objArray[] = new TclObject[size]; for (int i=0; i= tlist.vector.size()) { return null; } else { return (TclObject)tlist.vector.elementAt(index); } } /** * This procedure inserts the elements in elements[] into the list at * the given index. If tobj is not a list object, an attempt will * be made to convert it to a list. * * @param interp current interpreter. * @param tobj the TclObject to use as a list. * @param index the starting index of the insertion operation. <=0 means * the beginning of the list. >= TclList.getLength(tobj) means * the end of the list. * @param elements the element(s) to insert. * @param from insert elements starting from elements[from] (inclusive) * @param to insert elements up to elements[to] (inclusive) * @exception TclException if tobj is not a valid list. */ static final void insert(Interp interp, TclObject tobj, int index, TclObject elements[], int from, int to) throws TclException { replace(interp, tobj, index, 0, elements, from, to); } /** * This procedure replaces zero or more elements of the list * referenced by tobj with the objects from an TclObject array. * If tobj is not a list object, an attempt will be made to * convert it to a list. * * @param interp current interpreter. * @param tobj the TclObject to use as a list. * @param index the starting index of the replace operation. <=0 means * the beginning of the list. >= TclList.getLength(tobj) means * the end of the list. * @param count the number of elements to delete from the list. <=0 means * no elements should be deleted and the operation is equivalent to * an insertion operation. * @param elements the element(s) to insert. * @param from insert elements starting from elements[from] (inclusive) * @param to insert elements up to elements[to] (inclusive) * @exception TclException if tobj is not a valid list. */ public static final void replace(Interp interp, TclObject tobj, int index, int count, TclObject elements[], int from, int to) throws TclException { setListFromAny(interp, tobj); tobj.invalidateStringRep(); TclList tlist = (TclList)tobj.getInternalRep(); int size = tlist.vector.size(); int i; if (index >= size) { /* * Append to the end of the list. There is no need for deleting * elements. */ index = size; } else { if (index < 0) { index = 0; } if (count > size - index) { count = size - index; } for (i=0; i