001: //
002: // This file is part of the prose package.
003: //
004: // The contents of this file are subject to the Mozilla Public License
005: // Version 1.1 (the "License"); you may not use this file except in
006: // compliance with the License. You may obtain a copy of the License at
007: // http://www.mozilla.org/MPL/
008: //
009: // Software distributed under the License is distributed on an "AS IS" basis,
010: // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
011: // for the specific language governing rights and limitations under the
012: // License.
013: //
014: // The Original Code is prose.
015: //
016: // Contributor(s):
017: // $Id$
018: // =====================================================================
019: //
020: // (history at end)
021: //
022:
023: package ch.ethz.inf.iks.jvmai.jvmdi;
024:
025: /**
026: * Utility for managing unique ids.
027: *
028: * TODO
029: *
030: * Implemented without the target array to allow a more efficient implementation
031: * with typed arrays instead of arrays of anonymous objects which must be casted.
032: * May be implemented with generics in JDK 1.5.
033: *
034: * @author Angela Nicoara
035: * @author Gerald Linhofer
036: * @version $Revision$
037: *
038: */
039: class UniqueIdArrayManager {
040:
041: // Stores released ids in a stack manner (first in - last out).
042: private int releasedStack[];
043: // Number of entries in releasedStack (biggest valid index is releasedSize-1).
044: private int releasedSize;
045: // Next id, which will be used when releasedStack is empty.
046: private int idMax;
047:
048: /**
049: * Creates an UniqueIdArrayManager
050: */
051: public UniqueIdArrayManager() {
052: releasedStack = new int[1024];
053: releasedSize = 0;
054: idMax = 0;
055: }
056:
057: /**
058: * Returns a new (or recycled) unique id.
059: * @return unique id
060: */
061: public int newId() {
062: int result;
063: if (0 < releasedSize) {
064: releasedSize--;
065: result = releasedStack[releasedSize];
066: } else {
067: result = idMax;
068: idMax++;
069: }
070: return result;
071: }
072:
073: /**
074: * Releases an id.
075: * @param id that will be released.
076: */
077: public void releaseId(int id) {
078: if (0 > id || idMax <= id)
079: throw new RuntimeException( /*TODO*/);
080:
081: int releasedLength = releasedStack.length;
082: if (releasedLength <= releasedSize) {
083: int newStack[] = new int[2 * releasedLength];
084: System.arraycopy(releasedStack, 0, newStack, 0,
085: releasedLength);
086: releasedStack = newStack;
087: }
088:
089: releasedStack[releasedSize] = id;
090: releasedSize++;
091: }
092:
093: /**
094: * Resets this object to is initial state.
095: */
096: public void reset() {
097: releasedStack = new int[1024];
098: releasedSize = 0;
099: idMax = 0;
100: }
101:
102: }
103:
104: //======================================================================
105: //
106: //$Log$
107: //
|