jaroslav@378: /** jaroslav@378: * Back 2 Browser Bytecode Translator jaroslav@378: * Copyright (C) 2012 Jaroslav Tulach jaroslav@378: * jaroslav@378: * This program is free software: you can redistribute it and/or modify jaroslav@378: * it under the terms of the GNU General Public License as published by jaroslav@378: * the Free Software Foundation, version 2 of the License. jaroslav@378: * jaroslav@378: * This program is distributed in the hope that it will be useful, jaroslav@378: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@378: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@378: * GNU General Public License for more details. jaroslav@378: * jaroslav@378: * You should have received a copy of the GNU General Public License jaroslav@378: * along with this program. Look for COPYING file in the top folder. jaroslav@378: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@149: */ jtulach@167: package org.apidesign.javap; jaroslav@149: jaroslav@149: /** A JavaScript optimized replacement for Hashtable. jaroslav@149: * jaroslav@149: * @author Jaroslav Tulach jaroslav@149: */ jaroslav@149: final class Hashtable { jaroslav@150: private Object[] keys; jaroslav@150: private Object[] values; jaroslav@149: jaroslav@149: Hashtable(int i) { jaroslav@150: this(); jaroslav@149: } jaroslav@149: jaroslav@149: Hashtable(int i, double d) { jaroslav@150: this(); jaroslav@149: } jaroslav@149: jaroslav@149: Hashtable() { jaroslav@149: } jaroslav@149: jaroslav@150: synchronized void put(Object key, Object val) { jaroslav@150: int[] where = { -1, -1 }; jaroslav@150: Object found = get(key, where); jaroslav@150: if (where[0] != -1) { jaroslav@150: // key exists jaroslav@150: values[where[0]] = val; jaroslav@150: } else { jaroslav@150: if (where[1] != -1) { jaroslav@150: // null found jaroslav@150: keys[where[1]] = key; jaroslav@150: values[where[1]] = val; jaroslav@150: } else { jaroslav@150: if (keys == null) { jaroslav@150: keys = new Object[11]; jaroslav@150: values = new Object[11]; jaroslav@150: keys[0] = key; jaroslav@150: values[0] = val; jaroslav@150: } else { jaroslav@150: Object[] newKeys = new Object[keys.length * 2]; jaroslav@150: Object[] newValues = new Object[values.length * 2]; jaroslav@150: for (int i = 0; i < keys.length; i++) { jaroslav@150: newKeys[i] = keys[i]; jaroslav@150: newValues[i] = values[i]; jaroslav@150: } jaroslav@150: newKeys[keys.length] = key; jaroslav@150: newValues[keys.length] = val; jaroslav@150: keys = newKeys; jaroslav@150: values = newValues; jaroslav@150: } jaroslav@150: } jaroslav@150: } jaroslav@149: } jaroslav@149: jaroslav@150: Object get(Object key) { jaroslav@150: return get(key, null); jaroslav@150: } jaroslav@150: private synchronized Object get(Object key, int[] foundAndNull) { jaroslav@150: if (keys == null) { jaroslav@150: return null; jaroslav@150: } jaroslav@150: for (int i = 0; i < keys.length; i++) { jaroslav@150: if (keys[i] == null) { jaroslav@150: if (foundAndNull != null) { jaroslav@150: foundAndNull[1] = i; jaroslav@150: } jaroslav@150: } else if (keys[i].equals(key)) { jaroslav@150: if (foundAndNull != null) { jaroslav@150: foundAndNull[0] = i; jaroslav@150: } jaroslav@150: return values[i]; jaroslav@150: } jaroslav@150: } jaroslav@150: return null; jaroslav@149: } jaroslav@149: jaroslav@149: }