001: /*=============================================================================
002: * Copyright Texas Instruments 2003. All Rights Reserved.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: * $ProjectHeader: OSCRIPT 0.155 Fri, 20 Dec 2002 18:34:22 -0800 rclark $
019: */
020:
021: package oscript.util;
022:
023: import java.util.Vector;
024: import java.util.Iterator;
025:
026: import oscript.exceptions.*;
027:
028: /**
029: * This utility class provides a more Hashtable-like interface to
030: * {@link SymbolTable}, which normally maps a symbol to a table index.
031: * <p>
032: * Threading note: this class is not synchronized, but is designed to
033: * save to read from multiple threads, while write from a single thread
034: * context (at a time).
035: *
036: * @author Rob Clark (rob@ti.com)
037: * @version 0.0
038: * @see SymbolTable
039: */
040: public class SymbolMap {
041: private final SymbolTable table;
042: private Object[] values;
043:
044: /**
045: * Class Constructor
046: */
047: public SymbolMap() {
048: this (new OpenHashSymbolTable());
049: }
050:
051: /**
052: * Class Constructor
053: *
054: * @param table the underlying table data structure
055: */
056: public SymbolMap(SymbolTable table) {
057: this .table = table;
058: this .values = new Object[(table.size() > 0) ? table.size() : 10];
059: }
060:
061: /**
062: * Get a mapping
063: */
064: public final Object get(int id) {
065: int idx = table.get(id);
066: if ((idx == -1) || (idx >= values.length))
067: return null;
068: return values[idx];
069: }
070:
071: /**
072: * Put a new mapping in the table
073: */
074: public final Object put(int id, Object val) {
075: int idx = table.create(id);
076:
077: // grow if needed:
078: if (idx >= values.length) {
079: Object[] newValues = new Object[values.length * 2];
080: System.arraycopy(values, 0, newValues, 0, values.length);
081: values = newValues;
082: }
083:
084: Object ret = values[idx];
085: values[idx] = val;
086: return ret;
087: }
088:
089: /**
090: * Return an iterator of keys into the table. Each key is boxed
091: * in an {@link Integer}.
092: */
093: public Iterator keys() {
094: return table.symbols();
095: }
096: }
097:
098: /*
099: * Local Variables:
100: * tab-width: 2
101: * indent-tabs-mode: nil
102: * mode: java
103: * c-indentation-style: java
104: * c-basic-offset: 2
105: * eval: (c-set-offset 'substatement-open '0)
106: * eval: (c-set-offset 'case-label '+)
107: * eval: (c-set-offset 'inclass '+)
108: * eval: (c-set-offset 'inline-open '0)
109: * End:
110: */
|