Path: blob/master/src/java.base/share/classes/sun/text/IntHashtable.java
41152 views
/*1* Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26* (C) Copyright Taligent, Inc. 1996,1997 - All Rights Reserved27* (C) Copyright IBM Corp. 1996, 1997 - All Rights Reserved28*/2930package sun.text;3132/** Simple internal class for doing hash mapping. Much, much faster than the33* standard Hashtable for integer to integer mappings,34* and doesn't require object creation.<br>35* If a key is not found, the defaultValue is returned.36* Note: the keys are limited to values above Integer.MIN_VALUE+1.<br>37*/38public final class IntHashtable {3940public IntHashtable () {41initialize(3);42}4344public IntHashtable (int initialSize) {45initialize(leastGreaterPrimeIndex((int)(initialSize/HIGH_WATER_FACTOR)));46}4748public int size() {49return count;50}5152public boolean isEmpty() {53return count == 0;54}5556public void put(int key, int value) {57if (count > highWaterMark) {58rehash();59}60int index = find(key);61if (keyList[index] <= MAX_UNUSED) { // deleted or empty62keyList[index] = key;63++count;64}65values[index] = value; // reset value66}6768public int get(int key) {69return values[find(key)];70}7172public void remove(int key) {73int index = find(key);74if (keyList[index] > MAX_UNUSED) { // neither deleted nor empty75keyList[index] = DELETED; // set to deleted76values[index] = defaultValue; // set to default77--count;78if (count < lowWaterMark) {79rehash();80}81}82}8384public int getDefaultValue() {85return defaultValue;86}8788public void setDefaultValue(int newValue) {89defaultValue = newValue;90rehash();91}9293public boolean equals (Object that) {94if (that.getClass() != this.getClass()) return false;9596IntHashtable other = (IntHashtable) that;97if (other.size() != count || other.defaultValue != defaultValue) {98return false;99}100for (int i = 0; i < keyList.length; ++i) {101int key = keyList[i];102if (key > MAX_UNUSED && other.get(key) != values[i])103return false;104}105return true;106}107108public int hashCode() {109// NOTE: This function isn't actually used anywhere in this package, but it's here110// in case this class is ever used to make sure we uphold the invariants about111// hashCode() and equals()112113// WARNING: This function hasn't undergone rigorous testing to make sure it actually114// gives good distribution. We've eyeballed the results, and they appear okay, but115// you copy this algorithm (or these seed and multiplier values) at your own risk.116// --rtg 8/17/99117118int result = 465; // an arbitrary seed value119int scrambler = 1362796821; // an arbitrary multiplier.120for (int i = 0; i < keyList.length; ++i) {121// this line just scrambles the bits as each value is added into the122// has value. This helps to make sure we affect all the bits and that123// the same values in a different order will produce a different hash value124result = result * scrambler + 1;125result += keyList[i];126}127for (int i = 0; i < values.length; ++i) {128result = result * scrambler + 1;129result += values[i];130}131return result;132}133134public Object clone ()135throws CloneNotSupportedException {136IntHashtable result = (IntHashtable) super.clone();137values = values.clone();138keyList = keyList.clone();139return result;140}141142// =======================PRIVATES============================143private int defaultValue = 0;144145// the tables have to have prime-number lengths. Rather than compute146// primes, we just keep a table, with the current index we are using.147private int primeIndex;148149// highWaterFactor determines the maximum number of elements before150// a rehash. Can be tuned for different performance/storage characteristics.151private static final float HIGH_WATER_FACTOR = 0.4F;152private int highWaterMark;153154// lowWaterFactor determines the minimum number of elements before155// a rehash. Can be tuned for different performance/storage characteristics.156private static final float LOW_WATER_FACTOR = 0.0F;157private int lowWaterMark;158159private int count;160161// we use two arrays to minimize allocations162private int[] values;163private int[] keyList;164165private static final int EMPTY = Integer.MIN_VALUE;166private static final int DELETED = EMPTY + 1;167private static final int MAX_UNUSED = DELETED;168169private void initialize (int primeIndex) {170if (primeIndex < 0) {171primeIndex = 0;172} else if (primeIndex >= PRIMES.length) {173System.out.println("TOO BIG");174primeIndex = PRIMES.length - 1;175// throw new java.util.IllegalArgumentError();176}177this.primeIndex = primeIndex;178int initialSize = PRIMES[primeIndex];179values = new int[initialSize];180keyList = new int[initialSize];181for (int i = 0; i < initialSize; ++i) {182keyList[i] = EMPTY;183values[i] = defaultValue;184}185count = 0;186lowWaterMark = (int)(initialSize * LOW_WATER_FACTOR);187highWaterMark = (int)(initialSize * HIGH_WATER_FACTOR);188}189190private void rehash() {191int[] oldValues = values;192int[] oldkeyList = keyList;193int newPrimeIndex = primeIndex;194if (count > highWaterMark) {195++newPrimeIndex;196} else if (count < lowWaterMark) {197newPrimeIndex -= 2;198}199initialize(newPrimeIndex);200for (int i = oldValues.length - 1; i >= 0; --i) {201int key = oldkeyList[i];202if (key > MAX_UNUSED) {203putInternal(key, oldValues[i]);204}205}206}207208public void putInternal (int key, int value) {209int index = find(key);210if (keyList[index] < MAX_UNUSED) { // deleted or empty211keyList[index] = key;212++count;213}214values[index] = value; // reset value215}216217private int find (int key) {218if (key <= MAX_UNUSED)219throw new IllegalArgumentException("key can't be less than 0xFFFFFFFE");220int firstDeleted = -1; // assume invalid index221int index = (key ^ 0x4000000) % keyList.length;222if (index < 0) index = -index; // positive only223int jump = 0; // lazy evaluate224while (true) {225int tableHash = keyList[index];226if (tableHash == key) { // quick check227return index;228} else if (tableHash > MAX_UNUSED) { // neither correct nor unused229// ignore230} else if (tableHash == EMPTY) { // empty, end o' the line231if (firstDeleted >= 0) {232index = firstDeleted; // reset if had deleted slot233}234return index;235} else if (firstDeleted < 0) { // remember first deleted236firstDeleted = index;237}238if (jump == 0) { // lazy compute jump239jump = (key % (keyList.length - 1));240if (jump < 0) jump = -jump;241++jump;242}243244index = (index + jump) % keyList.length;245if (index == firstDeleted) {246// We've searched all entries for the given key.247return index;248}249}250}251252private static int leastGreaterPrimeIndex(int source) {253int i;254for (i = 0; i < PRIMES.length; ++i) {255if (source < PRIMES[i]) {256break;257}258}259return (i == 0) ? 0 : (i - 1);260}261262// This list is the result of buildList below. Can be tuned for different263// performance/storage characteristics.264private static final int[] PRIMES = {26517, 37, 67, 131, 257,266521, 1031, 2053, 4099, 8209, 16411, 32771, 65537,267131101, 262147, 524309, 1048583, 2097169, 4194319, 8388617, 16777259,26833554467, 67108879, 134217757, 268435459, 536870923, 1073741827, 2147483647269};270}271272273