⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 indexedunorderset.java

📁 JAVA3D矩陈的相关类
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * $RCSfile: IndexedUnorderSet.java,v $ * * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. * * Use is subject to license terms. * * $Revision: 1.6 $ * $Date: 2007/04/12 17:34:05 $ * $State: Exp $ */package javax.media.j3d;/** * A strongly type indexed unorder set. * All operations remove(IndexedObject, ListType), add(IndexedObject, ListType), * contains(IndexedObject, ListType) etc. take O(1) time. * The class is designed to optimize speed. So many reductance * procedures call and range check as found in ArrayList are  * removed. * * <p> * Use the following code to iterate through an array. * * <pre> *  IndexedUnorderSet  IUset = *      new IndexedUnorderSet(YourClass.class, listType); *  // add element here * *  YourClass[] arr = (YourClass []) IUset.toArray(); *  int size = IUset.arraySize(); *  for (int i=0; i < size; i++) { *      YourClass obj = arr[i]; *      .... *  } * </pre> * * <p> * Note: * <ul> * 1) The array return is a copied of internal array.<br> * 2) Don't use arr.length , use IUset.arraySize();<br> * 3) IndexedObject contains an array of listIndex, the number of * array elements depends on the number of different types of  * IndexedUnorderSet that use it.<br> * 4) No need to do casting for individual element as in ArrayList.<br> * 5) IndexedUnorderSet is thread safe.<br> * 6) Object implement this interface MUST initialize the index to -1.<br> * </ul> * * <p> * Limitation: * <ul> *       1) Order of IndexedObject in list is not important<br> *       2) Can't modify the clone() copy.<br> *       3) IndexedObject can't be null<br> * </ul> */class IndexedUnorderSet implements Cloneable, java.io.Serializable  {    // XXXX: set to false when release    final static boolean debug = false;    /**     * The array buffer into which the elements of the ArrayList are stored.     * The capacity of the ArrayList is the length of this array buffer.     *     * It is non-private to enable compiler do inlining for get(),     * set(), remove() when -O flag turn on.     */    transient IndexedObject elementData[];    /**     * Clone copy of elementData return by toArray(true);     */    transient Object cloneData[];    // size of the above clone objec.    transient int cloneSize;    transient boolean isDirty = true;    /**     * Component Type of individual array element entry     */     Class componentType;    /**     * The size of the ArrayList (the number of elements it contains).     *     * We make it non-private to enable compiler do inlining for     * getSize() when -O flag turn on.     */    int size;    int listType;    // Current VirtualUniverse using this structure    VirtualUniverse univ;    /**     * Constructs an empty list with the specified initial capacity.     * and the class data Type     *     * @param   initialCapacity   the initial capacity of the list.     * @param   componentType     class type of element in the list.     */     IndexedUnorderSet(int initialCapacity, Class componentType,		       int listType, VirtualUniverse univ) {	this.componentType = componentType;	this.elementData = (IndexedObject[])java.lang.reflect.Array.newInstance(						componentType, initialCapacity);	this.listType = listType;	this.univ = univ;    }    /**     * Constructs an empty list.     * @param   componentType     class type of element in the list.     */     IndexedUnorderSet(Class componentType, int listType,		       VirtualUniverse univ) {	this(10, componentType, listType, univ);     }    /**     * Constructs an empty list with the specified initial capacity.     *     * @param   initialCapacity   the initial capacity of the list.     */     IndexedUnorderSet(int initialCapacity, int listType,		       VirtualUniverse univ) {	 this(initialCapacity, IndexedObject.class, listType, univ);     }    /**     * Constructs an empty list.     * @param listType default to Object.     */     IndexedUnorderSet(int listType, VirtualUniverse univ) {	this(10, IndexedObject.class, listType, univ);    }    /**     * Initialize all indexes to -1     */    final static void init(IndexedObject obj, int len) {	obj.listIdx = new int[3][];	obj.listIdx[0] = new int[len];	obj.listIdx[1] = new int[len];	obj.listIdx[2] = new int[1];	for (int i=0; i < len; i++) {	    obj.listIdx[0][i] = -1;	    obj.listIdx[1][i] = -1;	}		// Just want to set both RenderMolecule idx	// and BehaviorRetained idx to 0 by default	// It is OK without the following lines	if (obj instanceof SceneGraphObjectRetained) {	    // setlive() will change this back to 0	    obj.listIdx[2][0] = 1;	} else {	    obj.listIdx[2][0] = 0;	}    }    /**     * Returns the number of elements in this list.     *     * @return  the number of elements in this list.     */    final int size() {	return size;    }      /**     * Returns the size of entry use in toArray() number of elements      * in this list.     *     * @return  the number of elements in this list.     */    final int arraySize() {	return cloneSize;    }    /**     * Tests if this list has no elements.     *     * @return  <tt>true</tt> if this list has no elements;     *          <tt>false</tt> otherwise.     */    final boolean isEmpty() {	return size == 0;    }    /**     * Returns <tt>true</tt> if this list contains the specified element.     *     * @param o element whose presence in this List is to be tested.     */    synchronized final boolean contains(IndexedObject o) {	return (o.listIdx[o.getIdxUsed(univ)][listType] >= 0);    }    /**     * Searches for the last occurence of the given argument, testing      * for equality using the <tt>equals</tt> method.      *     * @param   o   an object.     * @return  the index of the first occurrence of the argument in this     *          list; returns <tt>-1</tt> if the object is not found.     * @see     Object#equals(Object)     */    synchronized final int indexOf(IndexedObject o) {	return o.listIdx[o.getIdxUsed(univ)][listType];    }    /**     * Returns a shallow copy of this <tt>ArrayList</tt> instance.  (The     * elements themselves are not copied.)     *     * @return  a clone of this <tt>ArrayList</tt> instance.     */    synchronized protected final Object clone() {	try { 	    IndexedUnorderSet v = (IndexedUnorderSet)super.clone();	    v.elementData =  (IndexedObject[])java.lang.reflect.Array.newInstance(						   componentType, size);	    System.arraycopy(elementData, 0, v.elementData, 0, size);	    isDirty = true; // can't use the old cloneData reference	    return v;	} catch (CloneNotSupportedException e) { 	    // this shouldn't happen, since we are Cloneable	    throw new InternalError();	}    }    /**     * Returns an array containing all of the elements in this list.     * The size of the array may longer than the actual size. Use     * arraySize() to retrieve the size.      * The array return is a copied of internal array. if copy     * is true.     *     * @return an array containing all of the elements in this list     */    synchronized final Object[] toArray(boolean copy) {	if (copy) {	    if (isDirty) {		if ((cloneData == null) || cloneData.length < size) {		    cloneData = (Object[])java.lang.reflect.Array.newInstance(									      componentType, size);		}		System.arraycopy(elementData, 0, cloneData, 0, size);		cloneSize = size;		isDirty = false;	    }	    return cloneData;	} else {	    cloneSize = size;	    return elementData;	}     }    /**     * Returns an array containing all of the elements in this list.     * The size of the array may longer than the actual size. Use     * arraySize() to retrieve the size.      * The array return is a copied of internal array. So another     * thread can continue add/delete the current list. However,     * it should be noticed that two call to toArray() may return     * the same copy.     *     * @return an array containing all of the elements in this list     */    synchronized final Object[] toArray() {	return toArray(true);    }    /**     * Returns an array containing elements starting from startElement     * all of the elements in this list. A new array of exact size      * is always allocated.     *     * @param startElement starting element to copy      *     * @return an array containing elements starting from     *         startElement, null if element not found.     *     */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -