11/*
2- * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
@@ -74,20 +74,20 @@ public IntHashTable(int capacity) {
7474 * @param key The object whose hash code is to be computed.
7575 * @return zero if the object is null, otherwise the identityHashCode
7676 */
77- public int hash (Object key ) {
77+ protected int hash (Object key ) {
7878 return System .identityHashCode (key );
7979 }
8080
8181 /**
8282 * Find either the index of a key's value, or the index of an available space.
8383 *
84- * @param key The key to whose value you want to find.
85- * @param hash The hash code of this key.
84+ * @param key The key to whose index you want to find.
8685 * @return Either the index of the key's value, or an index pointing to
8786 * unoccupied space.
8887 */
89- public int lookup (Object key , int hash ) {
88+ protected int lookup (Object key ) {
9089 Object node ;
90+ int hash = hash (key );
9191 int hash1 = hash ^ (hash >>> 15 );
9292 int hash2 = (hash ^ (hash << 6 )) | 1 ; //ensure coprimeness
9393 int deleted = -1 ;
@@ -103,24 +103,14 @@ public int lookup(Object key, int hash) {
103103 }
104104
105105 /**
106- * Lookup a given key's value in the hash table.
107- *
108- * @param key The key whose value you want to find.
109- * @return Either the index of the key's value, or an index pointing to
110- * unoccupied space.
111- */
112- public int lookup (Object key ) {
113- return lookup (key , hash (key ));
114- }
115-
116- /**
117- * Return the value stored at the specified index in the table.
106+ * Return the value to which the specified key is mapped.
118107 *
119- * @param index The index to inspect, as returned from {@link #lookup}
120- * @return A non-negative integer if the index contains a non-null
121- * value, or -1 if it does .
108+ * @param key The key to whose value you want to find.
109+ * @return A non-negative integer if the value is found.
110+ * Otherwise, it is -1 .
122111 */
123- public int getFromIndex (int index ) {
112+ public int get (Object key ) {
113+ int index = lookup (key );
124114 Object node = objs [index ];
125115 return node == null || node == DELETED ? -1 : ints [index ];
126116 }
@@ -130,12 +120,11 @@ public int getFromIndex(int index) {
130120 *
131121 * @param key key with which the specified value is to be associated.
132122 * @param value value to be associated with the specified key.
133- * @param index the index at which to place this binding, as returned
134- * from {@link #lookup}.
135123 * @return previous value associated with specified key, or -1 if there was
136124 * no mapping for key.
137125 */
138- public int putAtIndex (Object key , int value , int index ) {
126+ public int put (Object key , int value ) {
127+ int index = lookup (key );
139128 Object old = objs [index ];
140129 if (old == null || old == DELETED ) {
141130 objs [index ] = key ;
@@ -152,6 +141,13 @@ public int putAtIndex(Object key, int value, int index) {
152141 }
153142 }
154143
144+ /**
145+ * Remove the mapping(key and value) of the specified key.
146+ *
147+ * @param key the key to whose value you want to remove.
148+ * @return the removed value associated with the specified key,
149+ * or -1 if there was no mapping for the specified key.
150+ */
155151 public int remove (Object key ) {
156152 int index = lookup (key );
157153 Object old = objs [index ];
@@ -169,20 +165,16 @@ public int remove(Object key) {
169165 protected void rehash () {
170166 Object [] oldObjsTable = objs ;
171167 int [] oldIntsTable = ints ;
172- int oldCapacity = oldObjsTable .length ;
173- int newCapacity = oldCapacity << 1 ;
174- Object [] newObjTable = new Object [newCapacity ];
175- int [] newIntTable = new int [newCapacity ];
176- int newMask = newCapacity - 1 ;
177- objs = newObjTable ;
178- ints = newIntTable ;
179- mask = newMask ;
168+ int newCapacity = oldObjsTable .length << 1 ;
169+ objs = new Object [newCapacity ];
170+ ints = new int [newCapacity ];
171+ mask = newCapacity - 1 ;
180172 num_bindings = 0 ; // this is recomputed below
181173 Object key ;
182174 for (int i = oldIntsTable .length ; --i >= 0 ;) {
183175 key = oldObjsTable [i ];
184176 if (key != null && key != DELETED )
185- putAtIndex (key , oldIntsTable [i ], lookup ( key , hash ( key )) );
177+ put (key , oldIntsTable [i ]);
186178 }
187179 }
188180
0 commit comments