Thu, 12 Oct 2017 19:44:07 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | * |
aoqi@0 | 25 | * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. |
aoqi@0 | 26 | */ |
aoqi@0 | 27 | |
aoqi@0 | 28 | package com.sun.xml.internal.fastinfoset.util; |
aoqi@0 | 29 | |
aoqi@0 | 30 | import com.sun.xml.internal.fastinfoset.CommonResourceBundle; |
aoqi@0 | 31 | |
aoqi@0 | 32 | public class StringIntMap extends KeyIntMap { |
aoqi@0 | 33 | protected static final Entry NULL_ENTRY = new Entry(null, 0, -1, null); |
aoqi@0 | 34 | |
aoqi@0 | 35 | protected StringIntMap _readOnlyMap; |
aoqi@0 | 36 | |
aoqi@0 | 37 | protected static class Entry extends BaseEntry { |
aoqi@0 | 38 | final String _key; |
aoqi@0 | 39 | Entry _next; |
aoqi@0 | 40 | |
aoqi@0 | 41 | public Entry(String key, int hash, int value, Entry next) { |
aoqi@0 | 42 | super(hash, value); |
aoqi@0 | 43 | _key = key; |
aoqi@0 | 44 | _next = next; |
aoqi@0 | 45 | } |
aoqi@0 | 46 | } |
aoqi@0 | 47 | |
aoqi@0 | 48 | protected Entry _lastEntry = NULL_ENTRY; |
aoqi@0 | 49 | |
aoqi@0 | 50 | protected Entry[] _table; |
aoqi@0 | 51 | |
aoqi@0 | 52 | protected int _index; |
aoqi@0 | 53 | |
aoqi@0 | 54 | // Total character count of Map |
aoqi@0 | 55 | protected int _totalCharacterCount; |
aoqi@0 | 56 | |
aoqi@0 | 57 | public StringIntMap(int initialCapacity, float loadFactor) { |
aoqi@0 | 58 | super(initialCapacity, loadFactor); |
aoqi@0 | 59 | |
aoqi@0 | 60 | _table = new Entry[_capacity]; |
aoqi@0 | 61 | } |
aoqi@0 | 62 | |
aoqi@0 | 63 | public StringIntMap(int initialCapacity) { |
aoqi@0 | 64 | this(initialCapacity, DEFAULT_LOAD_FACTOR); |
aoqi@0 | 65 | } |
aoqi@0 | 66 | |
aoqi@0 | 67 | public StringIntMap() { |
aoqi@0 | 68 | this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR); |
aoqi@0 | 69 | } |
aoqi@0 | 70 | |
aoqi@0 | 71 | public void clear() { |
aoqi@0 | 72 | for (int i = 0; i < _table.length; i++) { |
aoqi@0 | 73 | _table[i] = null; |
aoqi@0 | 74 | } |
aoqi@0 | 75 | _lastEntry = NULL_ENTRY; |
aoqi@0 | 76 | _size = 0; |
aoqi@0 | 77 | _index = _readOnlyMapSize; |
aoqi@0 | 78 | _totalCharacterCount = 0; |
aoqi@0 | 79 | } |
aoqi@0 | 80 | |
aoqi@0 | 81 | public void setReadOnlyMap(KeyIntMap readOnlyMap, boolean clear) { |
aoqi@0 | 82 | if (!(readOnlyMap instanceof StringIntMap)) { |
aoqi@0 | 83 | throw new IllegalArgumentException(CommonResourceBundle.getInstance(). |
aoqi@0 | 84 | getString("message.illegalClass", new Object[]{readOnlyMap})); |
aoqi@0 | 85 | } |
aoqi@0 | 86 | |
aoqi@0 | 87 | setReadOnlyMap((StringIntMap)readOnlyMap, clear); |
aoqi@0 | 88 | } |
aoqi@0 | 89 | |
aoqi@0 | 90 | public final void setReadOnlyMap(StringIntMap readOnlyMap, boolean clear) { |
aoqi@0 | 91 | _readOnlyMap = readOnlyMap; |
aoqi@0 | 92 | if (_readOnlyMap != null) { |
aoqi@0 | 93 | _readOnlyMapSize = _readOnlyMap.size(); |
aoqi@0 | 94 | _index = _size + _readOnlyMapSize; |
aoqi@0 | 95 | |
aoqi@0 | 96 | if (clear) { |
aoqi@0 | 97 | clear(); |
aoqi@0 | 98 | } |
aoqi@0 | 99 | } else { |
aoqi@0 | 100 | _readOnlyMapSize = 0; |
aoqi@0 | 101 | _index = _size; |
aoqi@0 | 102 | } |
aoqi@0 | 103 | } |
aoqi@0 | 104 | |
aoqi@0 | 105 | public final int getNextIndex() { |
aoqi@0 | 106 | return _index++; |
aoqi@0 | 107 | } |
aoqi@0 | 108 | |
aoqi@0 | 109 | public final int getIndex() { |
aoqi@0 | 110 | return _index; |
aoqi@0 | 111 | } |
aoqi@0 | 112 | |
aoqi@0 | 113 | public final int obtainIndex(String key) { |
aoqi@0 | 114 | final int hash = hashHash(key.hashCode()); |
aoqi@0 | 115 | |
aoqi@0 | 116 | if (_readOnlyMap != null) { |
aoqi@0 | 117 | final int index = _readOnlyMap.get(key, hash); |
aoqi@0 | 118 | if (index != -1) { |
aoqi@0 | 119 | return index; |
aoqi@0 | 120 | } |
aoqi@0 | 121 | } |
aoqi@0 | 122 | |
aoqi@0 | 123 | final int tableIndex = indexFor(hash, _table.length); |
aoqi@0 | 124 | for (Entry e = _table[tableIndex]; e != null; e = e._next) { |
aoqi@0 | 125 | if (e._hash == hash && eq(key, e._key)) { |
aoqi@0 | 126 | return e._value; |
aoqi@0 | 127 | } |
aoqi@0 | 128 | } |
aoqi@0 | 129 | |
aoqi@0 | 130 | addEntry(key, hash, tableIndex); |
aoqi@0 | 131 | return NOT_PRESENT; |
aoqi@0 | 132 | } |
aoqi@0 | 133 | |
aoqi@0 | 134 | public final void add(String key) { |
aoqi@0 | 135 | final int hash = hashHash(key.hashCode()); |
aoqi@0 | 136 | final int tableIndex = indexFor(hash, _table.length); |
aoqi@0 | 137 | addEntry(key, hash, tableIndex); |
aoqi@0 | 138 | } |
aoqi@0 | 139 | |
aoqi@0 | 140 | public final int get(String key) { |
aoqi@0 | 141 | if (key == _lastEntry._key) |
aoqi@0 | 142 | return _lastEntry._value; |
aoqi@0 | 143 | |
aoqi@0 | 144 | return get(key, hashHash(key.hashCode())); |
aoqi@0 | 145 | } |
aoqi@0 | 146 | |
aoqi@0 | 147 | public final int getTotalCharacterCount() { |
aoqi@0 | 148 | return _totalCharacterCount; |
aoqi@0 | 149 | } |
aoqi@0 | 150 | |
aoqi@0 | 151 | private final int get(String key, int hash) { |
aoqi@0 | 152 | if (_readOnlyMap != null) { |
aoqi@0 | 153 | final int i = _readOnlyMap.get(key, hash); |
aoqi@0 | 154 | if (i != -1) { |
aoqi@0 | 155 | return i; |
aoqi@0 | 156 | } |
aoqi@0 | 157 | } |
aoqi@0 | 158 | |
aoqi@0 | 159 | final int tableIndex = indexFor(hash, _table.length); |
aoqi@0 | 160 | for (Entry e = _table[tableIndex]; e != null; e = e._next) { |
aoqi@0 | 161 | if (e._hash == hash && eq(key, e._key)) { |
aoqi@0 | 162 | _lastEntry = e; |
aoqi@0 | 163 | return e._value; |
aoqi@0 | 164 | } |
aoqi@0 | 165 | } |
aoqi@0 | 166 | |
aoqi@0 | 167 | return NOT_PRESENT; |
aoqi@0 | 168 | } |
aoqi@0 | 169 | |
aoqi@0 | 170 | |
aoqi@0 | 171 | private final void addEntry(String key, int hash, int bucketIndex) { |
aoqi@0 | 172 | Entry e = _table[bucketIndex]; |
aoqi@0 | 173 | _table[bucketIndex] = new Entry(key, hash, _index++, e); |
aoqi@0 | 174 | _totalCharacterCount += key.length(); |
aoqi@0 | 175 | if (_size++ >= _threshold) { |
aoqi@0 | 176 | resize(2 * _table.length); |
aoqi@0 | 177 | } |
aoqi@0 | 178 | } |
aoqi@0 | 179 | |
aoqi@0 | 180 | protected final void resize(int newCapacity) { |
aoqi@0 | 181 | _capacity = newCapacity; |
aoqi@0 | 182 | Entry[] oldTable = _table; |
aoqi@0 | 183 | int oldCapacity = oldTable.length; |
aoqi@0 | 184 | if (oldCapacity == MAXIMUM_CAPACITY) { |
aoqi@0 | 185 | _threshold = Integer.MAX_VALUE; |
aoqi@0 | 186 | return; |
aoqi@0 | 187 | } |
aoqi@0 | 188 | |
aoqi@0 | 189 | Entry[] newTable = new Entry[_capacity]; |
aoqi@0 | 190 | transfer(newTable); |
aoqi@0 | 191 | _table = newTable; |
aoqi@0 | 192 | _threshold = (int)(_capacity * _loadFactor); |
aoqi@0 | 193 | } |
aoqi@0 | 194 | |
aoqi@0 | 195 | private final void transfer(Entry[] newTable) { |
aoqi@0 | 196 | Entry[] src = _table; |
aoqi@0 | 197 | int newCapacity = newTable.length; |
aoqi@0 | 198 | for (int j = 0; j < src.length; j++) { |
aoqi@0 | 199 | Entry e = src[j]; |
aoqi@0 | 200 | if (e != null) { |
aoqi@0 | 201 | src[j] = null; |
aoqi@0 | 202 | do { |
aoqi@0 | 203 | Entry next = e._next; |
aoqi@0 | 204 | int i = indexFor(e._hash, newCapacity); |
aoqi@0 | 205 | e._next = newTable[i]; |
aoqi@0 | 206 | newTable[i] = e; |
aoqi@0 | 207 | e = next; |
aoqi@0 | 208 | } while (e != null); |
aoqi@0 | 209 | } |
aoqi@0 | 210 | } |
aoqi@0 | 211 | } |
aoqi@0 | 212 | |
aoqi@0 | 213 | private final boolean eq(String x, String y) { |
aoqi@0 | 214 | return x == y || x.equals(y); |
aoqi@0 | 215 | } |
aoqi@0 | 216 | } |