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