src/share/jaxws_classes/com/sun/xml/internal/fastinfoset/util/CharArrayIntMap.java

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 0
373ffda63c9a
permissions
-rw-r--r--

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 CharArrayIntMap extends KeyIntMap {
aoqi@0 33
aoqi@0 34 private CharArrayIntMap _readOnlyMap;
aoqi@0 35
aoqi@0 36 // Total character count of Map
aoqi@0 37 protected int _totalCharacterCount;
aoqi@0 38
aoqi@0 39 static class Entry extends BaseEntry {
aoqi@0 40 final char[] _ch;
aoqi@0 41 final int _start;
aoqi@0 42 final int _length;
aoqi@0 43 Entry _next;
aoqi@0 44
aoqi@0 45 public Entry(char[] ch, int start, int length, int hash, int value, Entry next) {
aoqi@0 46 super(hash, value);
aoqi@0 47 _ch = ch;
aoqi@0 48 _start = start;
aoqi@0 49 _length = length;
aoqi@0 50 _next = next;
aoqi@0 51 }
aoqi@0 52
aoqi@0 53 public final boolean equalsCharArray(char[] ch, int start, int length) {
aoqi@0 54 if (_length == length) {
aoqi@0 55 int n = _length;
aoqi@0 56 int i = _start;
aoqi@0 57 int j = start;
aoqi@0 58 while (n-- != 0) {
aoqi@0 59 if (_ch[i++] != ch[j++])
aoqi@0 60 return false;
aoqi@0 61 }
aoqi@0 62 return true;
aoqi@0 63 }
aoqi@0 64
aoqi@0 65 return false;
aoqi@0 66 }
aoqi@0 67
aoqi@0 68 }
aoqi@0 69
aoqi@0 70 private Entry[] _table;
aoqi@0 71
aoqi@0 72 public CharArrayIntMap(int initialCapacity, float loadFactor) {
aoqi@0 73 super(initialCapacity, loadFactor);
aoqi@0 74
aoqi@0 75 _table = new Entry[_capacity];
aoqi@0 76 }
aoqi@0 77
aoqi@0 78 public CharArrayIntMap(int initialCapacity) {
aoqi@0 79 this(initialCapacity, DEFAULT_LOAD_FACTOR);
aoqi@0 80 }
aoqi@0 81
aoqi@0 82 public CharArrayIntMap() {
aoqi@0 83 this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
aoqi@0 84 }
aoqi@0 85
aoqi@0 86 public final void clear() {
aoqi@0 87 for (int i = 0; i < _table.length; i++) {
aoqi@0 88 _table[i] = null;
aoqi@0 89 }
aoqi@0 90 _size = 0;
aoqi@0 91 _totalCharacterCount = 0;
aoqi@0 92 }
aoqi@0 93
aoqi@0 94 public final void setReadOnlyMap(KeyIntMap readOnlyMap, boolean clear) {
aoqi@0 95 if (!(readOnlyMap instanceof CharArrayIntMap)) {
aoqi@0 96 throw new IllegalArgumentException(CommonResourceBundle.getInstance().
aoqi@0 97 getString("message.illegalClass", new Object[]{readOnlyMap}));
aoqi@0 98 }
aoqi@0 99
aoqi@0 100 setReadOnlyMap((CharArrayIntMap)readOnlyMap, clear);
aoqi@0 101 }
aoqi@0 102
aoqi@0 103 public final void setReadOnlyMap(CharArrayIntMap readOnlyMap, boolean clear) {
aoqi@0 104 _readOnlyMap = readOnlyMap;
aoqi@0 105 if (_readOnlyMap != null) {
aoqi@0 106 _readOnlyMapSize = _readOnlyMap.size();
aoqi@0 107
aoqi@0 108 if (clear) {
aoqi@0 109 clear();
aoqi@0 110 }
aoqi@0 111 } else {
aoqi@0 112 _readOnlyMapSize = 0;
aoqi@0 113 }
aoqi@0 114 }
aoqi@0 115
aoqi@0 116 /**
aoqi@0 117 * Method returns an index of the passed character buffer in
aoqi@0 118 * <code>CharArrayIntMap</code>.
aoqi@0 119 *
aoqi@0 120 * @return index of character buffer in <code>CharArrayIntMap</code>,
aoqi@0 121 * otherwise NOT_PRESENT.
aoqi@0 122 */
aoqi@0 123 public final int get(char[] ch, int start, int length) {
aoqi@0 124 final int hash = hashHash(CharArray.hashCode(ch, start, length));
aoqi@0 125 return get(ch, start, length, hash);
aoqi@0 126 }
aoqi@0 127
aoqi@0 128 /**
aoqi@0 129 * Method returns an index of the passed character buffer in
aoqi@0 130 * <code>CharArrayIntMap</code>. If character buffer is not in
aoqi@0 131 * <code>CharArrayIntMap</code> - it will be added.
aoqi@0 132 *
aoqi@0 133 * @return index of character buffer in <code>CharArrayIntMap</code>, or
aoqi@0 134 * NOT_PRESENT if character buffer was just added.
aoqi@0 135 */
aoqi@0 136 public final int obtainIndex(char[] ch, int start, int length, boolean clone) {
aoqi@0 137 final int hash = hashHash(CharArray.hashCode(ch, start, length));
aoqi@0 138
aoqi@0 139 if (_readOnlyMap != null) {
aoqi@0 140 final int index = _readOnlyMap.get(ch, start, length, hash);
aoqi@0 141 if (index != -1) {
aoqi@0 142 return index;
aoqi@0 143 }
aoqi@0 144 }
aoqi@0 145
aoqi@0 146 final int tableIndex = indexFor(hash, _table.length);
aoqi@0 147 for (Entry e = _table[tableIndex]; e != null; e = e._next) {
aoqi@0 148 if (e._hash == hash && e.equalsCharArray(ch, start, length)) {
aoqi@0 149 return e._value;
aoqi@0 150 }
aoqi@0 151 }
aoqi@0 152
aoqi@0 153 if (clone) {
aoqi@0 154 char[] chClone = new char[length];
aoqi@0 155 System.arraycopy(ch, start, chClone, 0, length);
aoqi@0 156
aoqi@0 157 ch = chClone;
aoqi@0 158 start = 0;
aoqi@0 159 }
aoqi@0 160
aoqi@0 161 addEntry(ch, start, length, hash, _size + _readOnlyMapSize, tableIndex);
aoqi@0 162 return NOT_PRESENT;
aoqi@0 163 }
aoqi@0 164
aoqi@0 165 public final int getTotalCharacterCount() {
aoqi@0 166 return _totalCharacterCount;
aoqi@0 167 }
aoqi@0 168
aoqi@0 169 private final int get(char[] ch, int start, int length, int hash) {
aoqi@0 170 if (_readOnlyMap != null) {
aoqi@0 171 final int i = _readOnlyMap.get(ch, start, length, hash);
aoqi@0 172 if (i != -1) {
aoqi@0 173 return i;
aoqi@0 174 }
aoqi@0 175 }
aoqi@0 176
aoqi@0 177 final int tableIndex = indexFor(hash, _table.length);
aoqi@0 178 for (Entry e = _table[tableIndex]; e != null; e = e._next) {
aoqi@0 179 if (e._hash == hash && e.equalsCharArray(ch, start, length)) {
aoqi@0 180 return e._value;
aoqi@0 181 }
aoqi@0 182 }
aoqi@0 183
aoqi@0 184 return NOT_PRESENT;
aoqi@0 185 }
aoqi@0 186
aoqi@0 187 private final void addEntry(char[] ch, int start, int length, int hash, int value, int bucketIndex) {
aoqi@0 188 Entry e = _table[bucketIndex];
aoqi@0 189 _table[bucketIndex] = new Entry(ch, start, length, hash, value, e);
aoqi@0 190 _totalCharacterCount += length;
aoqi@0 191 if (_size++ >= _threshold) {
aoqi@0 192 resize(2 * _table.length);
aoqi@0 193 }
aoqi@0 194 }
aoqi@0 195
aoqi@0 196 private final void resize(int newCapacity) {
aoqi@0 197 _capacity = newCapacity;
aoqi@0 198 Entry[] oldTable = _table;
aoqi@0 199 int oldCapacity = oldTable.length;
aoqi@0 200 if (oldCapacity == MAXIMUM_CAPACITY) {
aoqi@0 201 _threshold = Integer.MAX_VALUE;
aoqi@0 202 return;
aoqi@0 203 }
aoqi@0 204
aoqi@0 205 Entry[] newTable = new Entry[_capacity];
aoqi@0 206 transfer(newTable);
aoqi@0 207 _table = newTable;
aoqi@0 208 _threshold = (int)(_capacity * _loadFactor);
aoqi@0 209 }
aoqi@0 210
aoqi@0 211 private final void transfer(Entry[] newTable) {
aoqi@0 212 Entry[] src = _table;
aoqi@0 213 int newCapacity = newTable.length;
aoqi@0 214 for (int j = 0; j < src.length; j++) {
aoqi@0 215 Entry e = src[j];
aoqi@0 216 if (e != null) {
aoqi@0 217 src[j] = null;
aoqi@0 218 do {
aoqi@0 219 Entry next = e._next;
aoqi@0 220 int i = indexFor(e._hash, newCapacity);
aoqi@0 221 e._next = newTable[i];
aoqi@0 222 newTable[i] = e;
aoqi@0 223 e = next;
aoqi@0 224 } while (e != null);
aoqi@0 225 }
aoqi@0 226 }
aoqi@0 227 }
aoqi@0 228 }

mercurial