emul/compact/src/main/java/java/util/IdentityHashMap.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 07 Sep 2013 13:51:24 +0200
branchjdk7-b147
changeset 1258 724f3e1ea53e
permissions -rw-r--r--
Additional set of classes to make porting of lookup library more easier
jaroslav@1258
     1
/*
jaroslav@1258
     2
 * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
jaroslav@1258
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@1258
     4
 *
jaroslav@1258
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@1258
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@1258
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@1258
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@1258
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@1258
    10
 *
jaroslav@1258
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@1258
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@1258
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@1258
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@1258
    15
 * accompanied this code).
jaroslav@1258
    16
 *
jaroslav@1258
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@1258
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@1258
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@1258
    20
 *
jaroslav@1258
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@1258
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@1258
    23
 * questions.
jaroslav@1258
    24
 */
jaroslav@1258
    25
jaroslav@1258
    26
package java.util;
jaroslav@1258
    27
import java.io.*;
jaroslav@1258
    28
jaroslav@1258
    29
/**
jaroslav@1258
    30
 * This class implements the <tt>Map</tt> interface with a hash table, using
jaroslav@1258
    31
 * reference-equality in place of object-equality when comparing keys (and
jaroslav@1258
    32
 * values).  In other words, in an <tt>IdentityHashMap</tt>, two keys
jaroslav@1258
    33
 * <tt>k1</tt> and <tt>k2</tt> are considered equal if and only if
jaroslav@1258
    34
 * <tt>(k1==k2)</tt>.  (In normal <tt>Map</tt> implementations (like
jaroslav@1258
    35
 * <tt>HashMap</tt>) two keys <tt>k1</tt> and <tt>k2</tt> are considered equal
jaroslav@1258
    36
 * if and only if <tt>(k1==null ? k2==null : k1.equals(k2))</tt>.)
jaroslav@1258
    37
 *
jaroslav@1258
    38
 * <p><b>This class is <i>not</i> a general-purpose <tt>Map</tt>
jaroslav@1258
    39
 * implementation!  While this class implements the <tt>Map</tt> interface, it
jaroslav@1258
    40
 * intentionally violates <tt>Map's</tt> general contract, which mandates the
jaroslav@1258
    41
 * use of the <tt>equals</tt> method when comparing objects.  This class is
jaroslav@1258
    42
 * designed for use only in the rare cases wherein reference-equality
jaroslav@1258
    43
 * semantics are required.</b>
jaroslav@1258
    44
 *
jaroslav@1258
    45
 * <p>A typical use of this class is <i>topology-preserving object graph
jaroslav@1258
    46
 * transformations</i>, such as serialization or deep-copying.  To perform such
jaroslav@1258
    47
 * a transformation, a program must maintain a "node table" that keeps track
jaroslav@1258
    48
 * of all the object references that have already been processed.  The node
jaroslav@1258
    49
 * table must not equate distinct objects even if they happen to be equal.
jaroslav@1258
    50
 * Another typical use of this class is to maintain <i>proxy objects</i>.  For
jaroslav@1258
    51
 * example, a debugging facility might wish to maintain a proxy object for
jaroslav@1258
    52
 * each object in the program being debugged.
jaroslav@1258
    53
 *
jaroslav@1258
    54
 * <p>This class provides all of the optional map operations, and permits
jaroslav@1258
    55
 * <tt>null</tt> values and the <tt>null</tt> key.  This class makes no
jaroslav@1258
    56
 * guarantees as to the order of the map; in particular, it does not guarantee
jaroslav@1258
    57
 * that the order will remain constant over time.
jaroslav@1258
    58
 *
jaroslav@1258
    59
 * <p>This class provides constant-time performance for the basic
jaroslav@1258
    60
 * operations (<tt>get</tt> and <tt>put</tt>), assuming the system
jaroslav@1258
    61
 * identity hash function ({@link System#identityHashCode(Object)})
jaroslav@1258
    62
 * disperses elements properly among the buckets.
jaroslav@1258
    63
 *
jaroslav@1258
    64
 * <p>This class has one tuning parameter (which affects performance but not
jaroslav@1258
    65
 * semantics): <i>expected maximum size</i>.  This parameter is the maximum
jaroslav@1258
    66
 * number of key-value mappings that the map is expected to hold.  Internally,
jaroslav@1258
    67
 * this parameter is used to determine the number of buckets initially
jaroslav@1258
    68
 * comprising the hash table.  The precise relationship between the expected
jaroslav@1258
    69
 * maximum size and the number of buckets is unspecified.
jaroslav@1258
    70
 *
jaroslav@1258
    71
 * <p>If the size of the map (the number of key-value mappings) sufficiently
jaroslav@1258
    72
 * exceeds the expected maximum size, the number of buckets is increased
jaroslav@1258
    73
 * Increasing the number of buckets ("rehashing") may be fairly expensive, so
jaroslav@1258
    74
 * it pays to create identity hash maps with a sufficiently large expected
jaroslav@1258
    75
 * maximum size.  On the other hand, iteration over collection views requires
jaroslav@1258
    76
 * time proportional to the number of buckets in the hash table, so it
jaroslav@1258
    77
 * pays not to set the expected maximum size too high if you are especially
jaroslav@1258
    78
 * concerned with iteration performance or memory usage.
jaroslav@1258
    79
 *
jaroslav@1258
    80
 * <p><strong>Note that this implementation is not synchronized.</strong>
jaroslav@1258
    81
 * If multiple threads access an identity hash map concurrently, and at
jaroslav@1258
    82
 * least one of the threads modifies the map structurally, it <i>must</i>
jaroslav@1258
    83
 * be synchronized externally.  (A structural modification is any operation
jaroslav@1258
    84
 * that adds or deletes one or more mappings; merely changing the value
jaroslav@1258
    85
 * associated with a key that an instance already contains is not a
jaroslav@1258
    86
 * structural modification.)  This is typically accomplished by
jaroslav@1258
    87
 * synchronizing on some object that naturally encapsulates the map.
jaroslav@1258
    88
 *
jaroslav@1258
    89
 * If no such object exists, the map should be "wrapped" using the
jaroslav@1258
    90
 * {@link Collections#synchronizedMap Collections.synchronizedMap}
jaroslav@1258
    91
 * method.  This is best done at creation time, to prevent accidental
jaroslav@1258
    92
 * unsynchronized access to the map:<pre>
jaroslav@1258
    93
 *   Map m = Collections.synchronizedMap(new IdentityHashMap(...));</pre>
jaroslav@1258
    94
 *
jaroslav@1258
    95
 * <p>The iterators returned by the <tt>iterator</tt> method of the
jaroslav@1258
    96
 * collections returned by all of this class's "collection view
jaroslav@1258
    97
 * methods" are <i>fail-fast</i>: if the map is structurally modified
jaroslav@1258
    98
 * at any time after the iterator is created, in any way except
jaroslav@1258
    99
 * through the iterator's own <tt>remove</tt> method, the iterator
jaroslav@1258
   100
 * will throw a {@link ConcurrentModificationException}.  Thus, in the
jaroslav@1258
   101
 * face of concurrent modification, the iterator fails quickly and
jaroslav@1258
   102
 * cleanly, rather than risking arbitrary, non-deterministic behavior
jaroslav@1258
   103
 * at an undetermined time in the future.
jaroslav@1258
   104
 *
jaroslav@1258
   105
 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
jaroslav@1258
   106
 * as it is, generally speaking, impossible to make any hard guarantees in the
jaroslav@1258
   107
 * presence of unsynchronized concurrent modification.  Fail-fast iterators
jaroslav@1258
   108
 * throw <tt>ConcurrentModificationException</tt> on a best-effort basis.
jaroslav@1258
   109
 * Therefore, it would be wrong to write a program that depended on this
jaroslav@1258
   110
 * exception for its correctness: <i>fail-fast iterators should be used only
jaroslav@1258
   111
 * to detect bugs.</i>
jaroslav@1258
   112
 *
jaroslav@1258
   113
 * <p>Implementation note: This is a simple <i>linear-probe</i> hash table,
jaroslav@1258
   114
 * as described for example in texts by Sedgewick and Knuth.  The array
jaroslav@1258
   115
 * alternates holding keys and values.  (This has better locality for large
jaroslav@1258
   116
 * tables than does using separate arrays.)  For many JRE implementations
jaroslav@1258
   117
 * and operation mixes, this class will yield better performance than
jaroslav@1258
   118
 * {@link HashMap} (which uses <i>chaining</i> rather than linear-probing).
jaroslav@1258
   119
 *
jaroslav@1258
   120
 * <p>This class is a member of the
jaroslav@1258
   121
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
jaroslav@1258
   122
 * Java Collections Framework</a>.
jaroslav@1258
   123
 *
jaroslav@1258
   124
 * @see     System#identityHashCode(Object)
jaroslav@1258
   125
 * @see     Object#hashCode()
jaroslav@1258
   126
 * @see     Collection
jaroslav@1258
   127
 * @see     Map
jaroslav@1258
   128
 * @see     HashMap
jaroslav@1258
   129
 * @see     TreeMap
jaroslav@1258
   130
 * @author  Doug Lea and Josh Bloch
jaroslav@1258
   131
 * @since   1.4
jaroslav@1258
   132
 */
jaroslav@1258
   133
jaroslav@1258
   134
public class IdentityHashMap<K,V>
jaroslav@1258
   135
    extends AbstractMap<K,V>
jaroslav@1258
   136
    implements Map<K,V>, java.io.Serializable, Cloneable
jaroslav@1258
   137
{
jaroslav@1258
   138
    /**
jaroslav@1258
   139
     * The initial capacity used by the no-args constructor.
jaroslav@1258
   140
     * MUST be a power of two.  The value 32 corresponds to the
jaroslav@1258
   141
     * (specified) expected maximum size of 21, given a load factor
jaroslav@1258
   142
     * of 2/3.
jaroslav@1258
   143
     */
jaroslav@1258
   144
    private static final int DEFAULT_CAPACITY = 32;
jaroslav@1258
   145
jaroslav@1258
   146
    /**
jaroslav@1258
   147
     * The minimum capacity, used if a lower value is implicitly specified
jaroslav@1258
   148
     * by either of the constructors with arguments.  The value 4 corresponds
jaroslav@1258
   149
     * to an expected maximum size of 2, given a load factor of 2/3.
jaroslav@1258
   150
     * MUST be a power of two.
jaroslav@1258
   151
     */
jaroslav@1258
   152
    private static final int MINIMUM_CAPACITY = 4;
jaroslav@1258
   153
jaroslav@1258
   154
    /**
jaroslav@1258
   155
     * The maximum capacity, used if a higher value is implicitly specified
jaroslav@1258
   156
     * by either of the constructors with arguments.
jaroslav@1258
   157
     * MUST be a power of two <= 1<<29.
jaroslav@1258
   158
     */
jaroslav@1258
   159
    private static final int MAXIMUM_CAPACITY = 1 << 29;
jaroslav@1258
   160
jaroslav@1258
   161
    /**
jaroslav@1258
   162
     * The table, resized as necessary. Length MUST always be a power of two.
jaroslav@1258
   163
     */
jaroslav@1258
   164
    private transient Object[] table;
jaroslav@1258
   165
jaroslav@1258
   166
    /**
jaroslav@1258
   167
     * The number of key-value mappings contained in this identity hash map.
jaroslav@1258
   168
     *
jaroslav@1258
   169
     * @serial
jaroslav@1258
   170
     */
jaroslav@1258
   171
    private int size;
jaroslav@1258
   172
jaroslav@1258
   173
    /**
jaroslav@1258
   174
     * The number of modifications, to support fast-fail iterators
jaroslav@1258
   175
     */
jaroslav@1258
   176
    private transient int modCount;
jaroslav@1258
   177
jaroslav@1258
   178
    /**
jaroslav@1258
   179
     * The next size value at which to resize (capacity * load factor).
jaroslav@1258
   180
     */
jaroslav@1258
   181
    private transient int threshold;
jaroslav@1258
   182
jaroslav@1258
   183
    /**
jaroslav@1258
   184
     * Value representing null keys inside tables.
jaroslav@1258
   185
     */
jaroslav@1258
   186
    private static final Object NULL_KEY = new Object();
jaroslav@1258
   187
jaroslav@1258
   188
    /**
jaroslav@1258
   189
     * Use NULL_KEY for key if it is null.
jaroslav@1258
   190
     */
jaroslav@1258
   191
    private static Object maskNull(Object key) {
jaroslav@1258
   192
        return (key == null ? NULL_KEY : key);
jaroslav@1258
   193
    }
jaroslav@1258
   194
jaroslav@1258
   195
    /**
jaroslav@1258
   196
     * Returns internal representation of null key back to caller as null.
jaroslav@1258
   197
     */
jaroslav@1258
   198
    private static Object unmaskNull(Object key) {
jaroslav@1258
   199
        return (key == NULL_KEY ? null : key);
jaroslav@1258
   200
    }
jaroslav@1258
   201
jaroslav@1258
   202
    /**
jaroslav@1258
   203
     * Constructs a new, empty identity hash map with a default expected
jaroslav@1258
   204
     * maximum size (21).
jaroslav@1258
   205
     */
jaroslav@1258
   206
    public IdentityHashMap() {
jaroslav@1258
   207
        init(DEFAULT_CAPACITY);
jaroslav@1258
   208
    }
jaroslav@1258
   209
jaroslav@1258
   210
    /**
jaroslav@1258
   211
     * Constructs a new, empty map with the specified expected maximum size.
jaroslav@1258
   212
     * Putting more than the expected number of key-value mappings into
jaroslav@1258
   213
     * the map may cause the internal data structure to grow, which may be
jaroslav@1258
   214
     * somewhat time-consuming.
jaroslav@1258
   215
     *
jaroslav@1258
   216
     * @param expectedMaxSize the expected maximum size of the map
jaroslav@1258
   217
     * @throws IllegalArgumentException if <tt>expectedMaxSize</tt> is negative
jaroslav@1258
   218
     */
jaroslav@1258
   219
    public IdentityHashMap(int expectedMaxSize) {
jaroslav@1258
   220
        if (expectedMaxSize < 0)
jaroslav@1258
   221
            throw new IllegalArgumentException("expectedMaxSize is negative: "
jaroslav@1258
   222
                                               + expectedMaxSize);
jaroslav@1258
   223
        init(capacity(expectedMaxSize));
jaroslav@1258
   224
    }
jaroslav@1258
   225
jaroslav@1258
   226
    /**
jaroslav@1258
   227
     * Returns the appropriate capacity for the specified expected maximum
jaroslav@1258
   228
     * size.  Returns the smallest power of two between MINIMUM_CAPACITY
jaroslav@1258
   229
     * and MAXIMUM_CAPACITY, inclusive, that is greater than
jaroslav@1258
   230
     * (3 * expectedMaxSize)/2, if such a number exists.  Otherwise
jaroslav@1258
   231
     * returns MAXIMUM_CAPACITY.  If (3 * expectedMaxSize)/2 is negative, it
jaroslav@1258
   232
     * is assumed that overflow has occurred, and MAXIMUM_CAPACITY is returned.
jaroslav@1258
   233
     */
jaroslav@1258
   234
    private int capacity(int expectedMaxSize) {
jaroslav@1258
   235
        // Compute min capacity for expectedMaxSize given a load factor of 2/3
jaroslav@1258
   236
        int minCapacity = (3 * expectedMaxSize)/2;
jaroslav@1258
   237
jaroslav@1258
   238
        // Compute the appropriate capacity
jaroslav@1258
   239
        int result;
jaroslav@1258
   240
        if (minCapacity > MAXIMUM_CAPACITY || minCapacity < 0) {
jaroslav@1258
   241
            result = MAXIMUM_CAPACITY;
jaroslav@1258
   242
        } else {
jaroslav@1258
   243
            result = MINIMUM_CAPACITY;
jaroslav@1258
   244
            while (result < minCapacity)
jaroslav@1258
   245
                result <<= 1;
jaroslav@1258
   246
        }
jaroslav@1258
   247
        return result;
jaroslav@1258
   248
    }
jaroslav@1258
   249
jaroslav@1258
   250
    /**
jaroslav@1258
   251
     * Initializes object to be an empty map with the specified initial
jaroslav@1258
   252
     * capacity, which is assumed to be a power of two between
jaroslav@1258
   253
     * MINIMUM_CAPACITY and MAXIMUM_CAPACITY inclusive.
jaroslav@1258
   254
     */
jaroslav@1258
   255
    private void init(int initCapacity) {
jaroslav@1258
   256
        // assert (initCapacity & -initCapacity) == initCapacity; // power of 2
jaroslav@1258
   257
        // assert initCapacity >= MINIMUM_CAPACITY;
jaroslav@1258
   258
        // assert initCapacity <= MAXIMUM_CAPACITY;
jaroslav@1258
   259
jaroslav@1258
   260
        threshold = (initCapacity * 2)/3;
jaroslav@1258
   261
        table = new Object[2 * initCapacity];
jaroslav@1258
   262
    }
jaroslav@1258
   263
jaroslav@1258
   264
    /**
jaroslav@1258
   265
     * Constructs a new identity hash map containing the keys-value mappings
jaroslav@1258
   266
     * in the specified map.
jaroslav@1258
   267
     *
jaroslav@1258
   268
     * @param m the map whose mappings are to be placed into this map
jaroslav@1258
   269
     * @throws NullPointerException if the specified map is null
jaroslav@1258
   270
     */
jaroslav@1258
   271
    public IdentityHashMap(Map<? extends K, ? extends V> m) {
jaroslav@1258
   272
        // Allow for a bit of growth
jaroslav@1258
   273
        this((int) ((1 + m.size()) * 1.1));
jaroslav@1258
   274
        putAll(m);
jaroslav@1258
   275
    }
jaroslav@1258
   276
jaroslav@1258
   277
    /**
jaroslav@1258
   278
     * Returns the number of key-value mappings in this identity hash map.
jaroslav@1258
   279
     *
jaroslav@1258
   280
     * @return the number of key-value mappings in this map
jaroslav@1258
   281
     */
jaroslav@1258
   282
    public int size() {
jaroslav@1258
   283
        return size;
jaroslav@1258
   284
    }
jaroslav@1258
   285
jaroslav@1258
   286
    /**
jaroslav@1258
   287
     * Returns <tt>true</tt> if this identity hash map contains no key-value
jaroslav@1258
   288
     * mappings.
jaroslav@1258
   289
     *
jaroslav@1258
   290
     * @return <tt>true</tt> if this identity hash map contains no key-value
jaroslav@1258
   291
     *         mappings
jaroslav@1258
   292
     */
jaroslav@1258
   293
    public boolean isEmpty() {
jaroslav@1258
   294
        return size == 0;
jaroslav@1258
   295
    }
jaroslav@1258
   296
jaroslav@1258
   297
    /**
jaroslav@1258
   298
     * Returns index for Object x.
jaroslav@1258
   299
     */
jaroslav@1258
   300
    private static int hash(Object x, int length) {
jaroslav@1258
   301
        int h = System.identityHashCode(x);
jaroslav@1258
   302
        // Multiply by -127, and left-shift to use least bit as part of hash
jaroslav@1258
   303
        return ((h << 1) - (h << 8)) & (length - 1);
jaroslav@1258
   304
    }
jaroslav@1258
   305
jaroslav@1258
   306
    /**
jaroslav@1258
   307
     * Circularly traverses table of size len.
jaroslav@1258
   308
     */
jaroslav@1258
   309
    private static int nextKeyIndex(int i, int len) {
jaroslav@1258
   310
        return (i + 2 < len ? i + 2 : 0);
jaroslav@1258
   311
    }
jaroslav@1258
   312
jaroslav@1258
   313
    /**
jaroslav@1258
   314
     * Returns the value to which the specified key is mapped,
jaroslav@1258
   315
     * or {@code null} if this map contains no mapping for the key.
jaroslav@1258
   316
     *
jaroslav@1258
   317
     * <p>More formally, if this map contains a mapping from a key
jaroslav@1258
   318
     * {@code k} to a value {@code v} such that {@code (key == k)},
jaroslav@1258
   319
     * then this method returns {@code v}; otherwise it returns
jaroslav@1258
   320
     * {@code null}.  (There can be at most one such mapping.)
jaroslav@1258
   321
     *
jaroslav@1258
   322
     * <p>A return value of {@code null} does not <i>necessarily</i>
jaroslav@1258
   323
     * indicate that the map contains no mapping for the key; it's also
jaroslav@1258
   324
     * possible that the map explicitly maps the key to {@code null}.
jaroslav@1258
   325
     * The {@link #containsKey containsKey} operation may be used to
jaroslav@1258
   326
     * distinguish these two cases.
jaroslav@1258
   327
     *
jaroslav@1258
   328
     * @see #put(Object, Object)
jaroslav@1258
   329
     */
jaroslav@1258
   330
    public V get(Object key) {
jaroslav@1258
   331
        Object k = maskNull(key);
jaroslav@1258
   332
        Object[] tab = table;
jaroslav@1258
   333
        int len = tab.length;
jaroslav@1258
   334
        int i = hash(k, len);
jaroslav@1258
   335
        while (true) {
jaroslav@1258
   336
            Object item = tab[i];
jaroslav@1258
   337
            if (item == k)
jaroslav@1258
   338
                return (V) tab[i + 1];
jaroslav@1258
   339
            if (item == null)
jaroslav@1258
   340
                return null;
jaroslav@1258
   341
            i = nextKeyIndex(i, len);
jaroslav@1258
   342
        }
jaroslav@1258
   343
    }
jaroslav@1258
   344
jaroslav@1258
   345
    /**
jaroslav@1258
   346
     * Tests whether the specified object reference is a key in this identity
jaroslav@1258
   347
     * hash map.
jaroslav@1258
   348
     *
jaroslav@1258
   349
     * @param   key   possible key
jaroslav@1258
   350
     * @return  <code>true</code> if the specified object reference is a key
jaroslav@1258
   351
     *          in this map
jaroslav@1258
   352
     * @see     #containsValue(Object)
jaroslav@1258
   353
     */
jaroslav@1258
   354
    public boolean containsKey(Object key) {
jaroslav@1258
   355
        Object k = maskNull(key);
jaroslav@1258
   356
        Object[] tab = table;
jaroslav@1258
   357
        int len = tab.length;
jaroslav@1258
   358
        int i = hash(k, len);
jaroslav@1258
   359
        while (true) {
jaroslav@1258
   360
            Object item = tab[i];
jaroslav@1258
   361
            if (item == k)
jaroslav@1258
   362
                return true;
jaroslav@1258
   363
            if (item == null)
jaroslav@1258
   364
                return false;
jaroslav@1258
   365
            i = nextKeyIndex(i, len);
jaroslav@1258
   366
        }
jaroslav@1258
   367
    }
jaroslav@1258
   368
jaroslav@1258
   369
    /**
jaroslav@1258
   370
     * Tests whether the specified object reference is a value in this identity
jaroslav@1258
   371
     * hash map.
jaroslav@1258
   372
     *
jaroslav@1258
   373
     * @param value value whose presence in this map is to be tested
jaroslav@1258
   374
     * @return <tt>true</tt> if this map maps one or more keys to the
jaroslav@1258
   375
     *         specified object reference
jaroslav@1258
   376
     * @see     #containsKey(Object)
jaroslav@1258
   377
     */
jaroslav@1258
   378
    public boolean containsValue(Object value) {
jaroslav@1258
   379
        Object[] tab = table;
jaroslav@1258
   380
        for (int i = 1; i < tab.length; i += 2)
jaroslav@1258
   381
            if (tab[i] == value && tab[i - 1] != null)
jaroslav@1258
   382
                return true;
jaroslav@1258
   383
jaroslav@1258
   384
        return false;
jaroslav@1258
   385
    }
jaroslav@1258
   386
jaroslav@1258
   387
    /**
jaroslav@1258
   388
     * Tests if the specified key-value mapping is in the map.
jaroslav@1258
   389
     *
jaroslav@1258
   390
     * @param   key   possible key
jaroslav@1258
   391
     * @param   value possible value
jaroslav@1258
   392
     * @return  <code>true</code> if and only if the specified key-value
jaroslav@1258
   393
     *          mapping is in the map
jaroslav@1258
   394
     */
jaroslav@1258
   395
    private boolean containsMapping(Object key, Object value) {
jaroslav@1258
   396
        Object k = maskNull(key);
jaroslav@1258
   397
        Object[] tab = table;
jaroslav@1258
   398
        int len = tab.length;
jaroslav@1258
   399
        int i = hash(k, len);
jaroslav@1258
   400
        while (true) {
jaroslav@1258
   401
            Object item = tab[i];
jaroslav@1258
   402
            if (item == k)
jaroslav@1258
   403
                return tab[i + 1] == value;
jaroslav@1258
   404
            if (item == null)
jaroslav@1258
   405
                return false;
jaroslav@1258
   406
            i = nextKeyIndex(i, len);
jaroslav@1258
   407
        }
jaroslav@1258
   408
    }
jaroslav@1258
   409
jaroslav@1258
   410
    /**
jaroslav@1258
   411
     * Associates the specified value with the specified key in this identity
jaroslav@1258
   412
     * hash map.  If the map previously contained a mapping for the key, the
jaroslav@1258
   413
     * old value is replaced.
jaroslav@1258
   414
     *
jaroslav@1258
   415
     * @param key the key with which the specified value is to be associated
jaroslav@1258
   416
     * @param value the value to be associated with the specified key
jaroslav@1258
   417
     * @return the previous value associated with <tt>key</tt>, or
jaroslav@1258
   418
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
jaroslav@1258
   419
     *         (A <tt>null</tt> return can also indicate that the map
jaroslav@1258
   420
     *         previously associated <tt>null</tt> with <tt>key</tt>.)
jaroslav@1258
   421
     * @see     Object#equals(Object)
jaroslav@1258
   422
     * @see     #get(Object)
jaroslav@1258
   423
     * @see     #containsKey(Object)
jaroslav@1258
   424
     */
jaroslav@1258
   425
    public V put(K key, V value) {
jaroslav@1258
   426
        Object k = maskNull(key);
jaroslav@1258
   427
        Object[] tab = table;
jaroslav@1258
   428
        int len = tab.length;
jaroslav@1258
   429
        int i = hash(k, len);
jaroslav@1258
   430
jaroslav@1258
   431
        Object item;
jaroslav@1258
   432
        while ( (item = tab[i]) != null) {
jaroslav@1258
   433
            if (item == k) {
jaroslav@1258
   434
                V oldValue = (V) tab[i + 1];
jaroslav@1258
   435
                tab[i + 1] = value;
jaroslav@1258
   436
                return oldValue;
jaroslav@1258
   437
            }
jaroslav@1258
   438
            i = nextKeyIndex(i, len);
jaroslav@1258
   439
        }
jaroslav@1258
   440
jaroslav@1258
   441
        modCount++;
jaroslav@1258
   442
        tab[i] = k;
jaroslav@1258
   443
        tab[i + 1] = value;
jaroslav@1258
   444
        if (++size >= threshold)
jaroslav@1258
   445
            resize(len); // len == 2 * current capacity.
jaroslav@1258
   446
        return null;
jaroslav@1258
   447
    }
jaroslav@1258
   448
jaroslav@1258
   449
    /**
jaroslav@1258
   450
     * Resize the table to hold given capacity.
jaroslav@1258
   451
     *
jaroslav@1258
   452
     * @param newCapacity the new capacity, must be a power of two.
jaroslav@1258
   453
     */
jaroslav@1258
   454
    private void resize(int newCapacity) {
jaroslav@1258
   455
        // assert (newCapacity & -newCapacity) == newCapacity; // power of 2
jaroslav@1258
   456
        int newLength = newCapacity * 2;
jaroslav@1258
   457
jaroslav@1258
   458
        Object[] oldTable = table;
jaroslav@1258
   459
        int oldLength = oldTable.length;
jaroslav@1258
   460
        if (oldLength == 2*MAXIMUM_CAPACITY) { // can't expand any further
jaroslav@1258
   461
            if (threshold == MAXIMUM_CAPACITY-1)
jaroslav@1258
   462
                throw new IllegalStateException("Capacity exhausted.");
jaroslav@1258
   463
            threshold = MAXIMUM_CAPACITY-1;  // Gigantic map!
jaroslav@1258
   464
            return;
jaroslav@1258
   465
        }
jaroslav@1258
   466
        if (oldLength >= newLength)
jaroslav@1258
   467
            return;
jaroslav@1258
   468
jaroslav@1258
   469
        Object[] newTable = new Object[newLength];
jaroslav@1258
   470
        threshold = newLength / 3;
jaroslav@1258
   471
jaroslav@1258
   472
        for (int j = 0; j < oldLength; j += 2) {
jaroslav@1258
   473
            Object key = oldTable[j];
jaroslav@1258
   474
            if (key != null) {
jaroslav@1258
   475
                Object value = oldTable[j+1];
jaroslav@1258
   476
                oldTable[j] = null;
jaroslav@1258
   477
                oldTable[j+1] = null;
jaroslav@1258
   478
                int i = hash(key, newLength);
jaroslav@1258
   479
                while (newTable[i] != null)
jaroslav@1258
   480
                    i = nextKeyIndex(i, newLength);
jaroslav@1258
   481
                newTable[i] = key;
jaroslav@1258
   482
                newTable[i + 1] = value;
jaroslav@1258
   483
            }
jaroslav@1258
   484
        }
jaroslav@1258
   485
        table = newTable;
jaroslav@1258
   486
    }
jaroslav@1258
   487
jaroslav@1258
   488
    /**
jaroslav@1258
   489
     * Copies all of the mappings from the specified map to this map.
jaroslav@1258
   490
     * These mappings will replace any mappings that this map had for
jaroslav@1258
   491
     * any of the keys currently in the specified map.
jaroslav@1258
   492
     *
jaroslav@1258
   493
     * @param m mappings to be stored in this map
jaroslav@1258
   494
     * @throws NullPointerException if the specified map is null
jaroslav@1258
   495
     */
jaroslav@1258
   496
    public void putAll(Map<? extends K, ? extends V> m) {
jaroslav@1258
   497
        int n = m.size();
jaroslav@1258
   498
        if (n == 0)
jaroslav@1258
   499
            return;
jaroslav@1258
   500
        if (n > threshold) // conservatively pre-expand
jaroslav@1258
   501
            resize(capacity(n));
jaroslav@1258
   502
jaroslav@1258
   503
        for (Entry<? extends K, ? extends V> e : m.entrySet())
jaroslav@1258
   504
            put(e.getKey(), e.getValue());
jaroslav@1258
   505
    }
jaroslav@1258
   506
jaroslav@1258
   507
    /**
jaroslav@1258
   508
     * Removes the mapping for this key from this map if present.
jaroslav@1258
   509
     *
jaroslav@1258
   510
     * @param key key whose mapping is to be removed from the map
jaroslav@1258
   511
     * @return the previous value associated with <tt>key</tt>, or
jaroslav@1258
   512
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
jaroslav@1258
   513
     *         (A <tt>null</tt> return can also indicate that the map
jaroslav@1258
   514
     *         previously associated <tt>null</tt> with <tt>key</tt>.)
jaroslav@1258
   515
     */
jaroslav@1258
   516
    public V remove(Object key) {
jaroslav@1258
   517
        Object k = maskNull(key);
jaroslav@1258
   518
        Object[] tab = table;
jaroslav@1258
   519
        int len = tab.length;
jaroslav@1258
   520
        int i = hash(k, len);
jaroslav@1258
   521
jaroslav@1258
   522
        while (true) {
jaroslav@1258
   523
            Object item = tab[i];
jaroslav@1258
   524
            if (item == k) {
jaroslav@1258
   525
                modCount++;
jaroslav@1258
   526
                size--;
jaroslav@1258
   527
                V oldValue = (V) tab[i + 1];
jaroslav@1258
   528
                tab[i + 1] = null;
jaroslav@1258
   529
                tab[i] = null;
jaroslav@1258
   530
                closeDeletion(i);
jaroslav@1258
   531
                return oldValue;
jaroslav@1258
   532
            }
jaroslav@1258
   533
            if (item == null)
jaroslav@1258
   534
                return null;
jaroslav@1258
   535
            i = nextKeyIndex(i, len);
jaroslav@1258
   536
        }
jaroslav@1258
   537
jaroslav@1258
   538
    }
jaroslav@1258
   539
jaroslav@1258
   540
    /**
jaroslav@1258
   541
     * Removes the specified key-value mapping from the map if it is present.
jaroslav@1258
   542
     *
jaroslav@1258
   543
     * @param   key   possible key
jaroslav@1258
   544
     * @param   value possible value
jaroslav@1258
   545
     * @return  <code>true</code> if and only if the specified key-value
jaroslav@1258
   546
     *          mapping was in the map
jaroslav@1258
   547
     */
jaroslav@1258
   548
    private boolean removeMapping(Object key, Object value) {
jaroslav@1258
   549
        Object k = maskNull(key);
jaroslav@1258
   550
        Object[] tab = table;
jaroslav@1258
   551
        int len = tab.length;
jaroslav@1258
   552
        int i = hash(k, len);
jaroslav@1258
   553
jaroslav@1258
   554
        while (true) {
jaroslav@1258
   555
            Object item = tab[i];
jaroslav@1258
   556
            if (item == k) {
jaroslav@1258
   557
                if (tab[i + 1] != value)
jaroslav@1258
   558
                    return false;
jaroslav@1258
   559
                modCount++;
jaroslav@1258
   560
                size--;
jaroslav@1258
   561
                tab[i] = null;
jaroslav@1258
   562
                tab[i + 1] = null;
jaroslav@1258
   563
                closeDeletion(i);
jaroslav@1258
   564
                return true;
jaroslav@1258
   565
            }
jaroslav@1258
   566
            if (item == null)
jaroslav@1258
   567
                return false;
jaroslav@1258
   568
            i = nextKeyIndex(i, len);
jaroslav@1258
   569
        }
jaroslav@1258
   570
    }
jaroslav@1258
   571
jaroslav@1258
   572
    /**
jaroslav@1258
   573
     * Rehash all possibly-colliding entries following a
jaroslav@1258
   574
     * deletion. This preserves the linear-probe
jaroslav@1258
   575
     * collision properties required by get, put, etc.
jaroslav@1258
   576
     *
jaroslav@1258
   577
     * @param d the index of a newly empty deleted slot
jaroslav@1258
   578
     */
jaroslav@1258
   579
    private void closeDeletion(int d) {
jaroslav@1258
   580
        // Adapted from Knuth Section 6.4 Algorithm R
jaroslav@1258
   581
        Object[] tab = table;
jaroslav@1258
   582
        int len = tab.length;
jaroslav@1258
   583
jaroslav@1258
   584
        // Look for items to swap into newly vacated slot
jaroslav@1258
   585
        // starting at index immediately following deletion,
jaroslav@1258
   586
        // and continuing until a null slot is seen, indicating
jaroslav@1258
   587
        // the end of a run of possibly-colliding keys.
jaroslav@1258
   588
        Object item;
jaroslav@1258
   589
        for (int i = nextKeyIndex(d, len); (item = tab[i]) != null;
jaroslav@1258
   590
             i = nextKeyIndex(i, len) ) {
jaroslav@1258
   591
            // The following test triggers if the item at slot i (which
jaroslav@1258
   592
            // hashes to be at slot r) should take the spot vacated by d.
jaroslav@1258
   593
            // If so, we swap it in, and then continue with d now at the
jaroslav@1258
   594
            // newly vacated i.  This process will terminate when we hit
jaroslav@1258
   595
            // the null slot at the end of this run.
jaroslav@1258
   596
            // The test is messy because we are using a circular table.
jaroslav@1258
   597
            int r = hash(item, len);
jaroslav@1258
   598
            if ((i < r && (r <= d || d <= i)) || (r <= d && d <= i)) {
jaroslav@1258
   599
                tab[d] = item;
jaroslav@1258
   600
                tab[d + 1] = tab[i + 1];
jaroslav@1258
   601
                tab[i] = null;
jaroslav@1258
   602
                tab[i + 1] = null;
jaroslav@1258
   603
                d = i;
jaroslav@1258
   604
            }
jaroslav@1258
   605
        }
jaroslav@1258
   606
    }
jaroslav@1258
   607
jaroslav@1258
   608
    /**
jaroslav@1258
   609
     * Removes all of the mappings from this map.
jaroslav@1258
   610
     * The map will be empty after this call returns.
jaroslav@1258
   611
     */
jaroslav@1258
   612
    public void clear() {
jaroslav@1258
   613
        modCount++;
jaroslav@1258
   614
        Object[] tab = table;
jaroslav@1258
   615
        for (int i = 0; i < tab.length; i++)
jaroslav@1258
   616
            tab[i] = null;
jaroslav@1258
   617
        size = 0;
jaroslav@1258
   618
    }
jaroslav@1258
   619
jaroslav@1258
   620
    /**
jaroslav@1258
   621
     * Compares the specified object with this map for equality.  Returns
jaroslav@1258
   622
     * <tt>true</tt> if the given object is also a map and the two maps
jaroslav@1258
   623
     * represent identical object-reference mappings.  More formally, this
jaroslav@1258
   624
     * map is equal to another map <tt>m</tt> if and only if
jaroslav@1258
   625
     * <tt>this.entrySet().equals(m.entrySet())</tt>.
jaroslav@1258
   626
     *
jaroslav@1258
   627
     * <p><b>Owing to the reference-equality-based semantics of this map it is
jaroslav@1258
   628
     * possible that the symmetry and transitivity requirements of the
jaroslav@1258
   629
     * <tt>Object.equals</tt> contract may be violated if this map is compared
jaroslav@1258
   630
     * to a normal map.  However, the <tt>Object.equals</tt> contract is
jaroslav@1258
   631
     * guaranteed to hold among <tt>IdentityHashMap</tt> instances.</b>
jaroslav@1258
   632
     *
jaroslav@1258
   633
     * @param  o object to be compared for equality with this map
jaroslav@1258
   634
     * @return <tt>true</tt> if the specified object is equal to this map
jaroslav@1258
   635
     * @see Object#equals(Object)
jaroslav@1258
   636
     */
jaroslav@1258
   637
    public boolean equals(Object o) {
jaroslav@1258
   638
        if (o == this) {
jaroslav@1258
   639
            return true;
jaroslav@1258
   640
        } else if (o instanceof IdentityHashMap) {
jaroslav@1258
   641
            IdentityHashMap m = (IdentityHashMap) o;
jaroslav@1258
   642
            if (m.size() != size)
jaroslav@1258
   643
                return false;
jaroslav@1258
   644
jaroslav@1258
   645
            Object[] tab = m.table;
jaroslav@1258
   646
            for (int i = 0; i < tab.length; i+=2) {
jaroslav@1258
   647
                Object k = tab[i];
jaroslav@1258
   648
                if (k != null && !containsMapping(k, tab[i + 1]))
jaroslav@1258
   649
                    return false;
jaroslav@1258
   650
            }
jaroslav@1258
   651
            return true;
jaroslav@1258
   652
        } else if (o instanceof Map) {
jaroslav@1258
   653
            Map m = (Map)o;
jaroslav@1258
   654
            return entrySet().equals(m.entrySet());
jaroslav@1258
   655
        } else {
jaroslav@1258
   656
            return false;  // o is not a Map
jaroslav@1258
   657
        }
jaroslav@1258
   658
    }
jaroslav@1258
   659
jaroslav@1258
   660
    /**
jaroslav@1258
   661
     * Returns the hash code value for this map.  The hash code of a map is
jaroslav@1258
   662
     * defined to be the sum of the hash codes of each entry in the map's
jaroslav@1258
   663
     * <tt>entrySet()</tt> view.  This ensures that <tt>m1.equals(m2)</tt>
jaroslav@1258
   664
     * implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two
jaroslav@1258
   665
     * <tt>IdentityHashMap</tt> instances <tt>m1</tt> and <tt>m2</tt>, as
jaroslav@1258
   666
     * required by the general contract of {@link Object#hashCode}.
jaroslav@1258
   667
     *
jaroslav@1258
   668
     * <p><b>Owing to the reference-equality-based semantics of the
jaroslav@1258
   669
     * <tt>Map.Entry</tt> instances in the set returned by this map's
jaroslav@1258
   670
     * <tt>entrySet</tt> method, it is possible that the contractual
jaroslav@1258
   671
     * requirement of <tt>Object.hashCode</tt> mentioned in the previous
jaroslav@1258
   672
     * paragraph will be violated if one of the two objects being compared is
jaroslav@1258
   673
     * an <tt>IdentityHashMap</tt> instance and the other is a normal map.</b>
jaroslav@1258
   674
     *
jaroslav@1258
   675
     * @return the hash code value for this map
jaroslav@1258
   676
     * @see Object#equals(Object)
jaroslav@1258
   677
     * @see #equals(Object)
jaroslav@1258
   678
     */
jaroslav@1258
   679
    public int hashCode() {
jaroslav@1258
   680
        int result = 0;
jaroslav@1258
   681
        Object[] tab = table;
jaroslav@1258
   682
        for (int i = 0; i < tab.length; i +=2) {
jaroslav@1258
   683
            Object key = tab[i];
jaroslav@1258
   684
            if (key != null) {
jaroslav@1258
   685
                Object k = unmaskNull(key);
jaroslav@1258
   686
                result += System.identityHashCode(k) ^
jaroslav@1258
   687
                          System.identityHashCode(tab[i + 1]);
jaroslav@1258
   688
            }
jaroslav@1258
   689
        }
jaroslav@1258
   690
        return result;
jaroslav@1258
   691
    }
jaroslav@1258
   692
jaroslav@1258
   693
    /**
jaroslav@1258
   694
     * Returns a shallow copy of this identity hash map: the keys and values
jaroslav@1258
   695
     * themselves are not cloned.
jaroslav@1258
   696
     *
jaroslav@1258
   697
     * @return a shallow copy of this map
jaroslav@1258
   698
     */
jaroslav@1258
   699
    public Object clone() {
jaroslav@1258
   700
        try {
jaroslav@1258
   701
            IdentityHashMap<K,V> m = (IdentityHashMap<K,V>) super.clone();
jaroslav@1258
   702
            m.entrySet = null;
jaroslav@1258
   703
            m.table = table.clone();
jaroslav@1258
   704
            return m;
jaroslav@1258
   705
        } catch (CloneNotSupportedException e) {
jaroslav@1258
   706
            throw new InternalError();
jaroslav@1258
   707
        }
jaroslav@1258
   708
    }
jaroslav@1258
   709
jaroslav@1258
   710
    private abstract class IdentityHashMapIterator<T> implements Iterator<T> {
jaroslav@1258
   711
        int index = (size != 0 ? 0 : table.length); // current slot.
jaroslav@1258
   712
        int expectedModCount = modCount; // to support fast-fail
jaroslav@1258
   713
        int lastReturnedIndex = -1;      // to allow remove()
jaroslav@1258
   714
        boolean indexValid; // To avoid unnecessary next computation
jaroslav@1258
   715
        Object[] traversalTable = table; // reference to main table or copy
jaroslav@1258
   716
jaroslav@1258
   717
        public boolean hasNext() {
jaroslav@1258
   718
            Object[] tab = traversalTable;
jaroslav@1258
   719
            for (int i = index; i < tab.length; i+=2) {
jaroslav@1258
   720
                Object key = tab[i];
jaroslav@1258
   721
                if (key != null) {
jaroslav@1258
   722
                    index = i;
jaroslav@1258
   723
                    return indexValid = true;
jaroslav@1258
   724
                }
jaroslav@1258
   725
            }
jaroslav@1258
   726
            index = tab.length;
jaroslav@1258
   727
            return false;
jaroslav@1258
   728
        }
jaroslav@1258
   729
jaroslav@1258
   730
        protected int nextIndex() {
jaroslav@1258
   731
            if (modCount != expectedModCount)
jaroslav@1258
   732
                throw new ConcurrentModificationException();
jaroslav@1258
   733
            if (!indexValid && !hasNext())
jaroslav@1258
   734
                throw new NoSuchElementException();
jaroslav@1258
   735
jaroslav@1258
   736
            indexValid = false;
jaroslav@1258
   737
            lastReturnedIndex = index;
jaroslav@1258
   738
            index += 2;
jaroslav@1258
   739
            return lastReturnedIndex;
jaroslav@1258
   740
        }
jaroslav@1258
   741
jaroslav@1258
   742
        public void remove() {
jaroslav@1258
   743
            if (lastReturnedIndex == -1)
jaroslav@1258
   744
                throw new IllegalStateException();
jaroslav@1258
   745
            if (modCount != expectedModCount)
jaroslav@1258
   746
                throw new ConcurrentModificationException();
jaroslav@1258
   747
jaroslav@1258
   748
            expectedModCount = ++modCount;
jaroslav@1258
   749
            int deletedSlot = lastReturnedIndex;
jaroslav@1258
   750
            lastReturnedIndex = -1;
jaroslav@1258
   751
            // back up index to revisit new contents after deletion
jaroslav@1258
   752
            index = deletedSlot;
jaroslav@1258
   753
            indexValid = false;
jaroslav@1258
   754
jaroslav@1258
   755
            // Removal code proceeds as in closeDeletion except that
jaroslav@1258
   756
            // it must catch the rare case where an element already
jaroslav@1258
   757
            // seen is swapped into a vacant slot that will be later
jaroslav@1258
   758
            // traversed by this iterator. We cannot allow future
jaroslav@1258
   759
            // next() calls to return it again.  The likelihood of
jaroslav@1258
   760
            // this occurring under 2/3 load factor is very slim, but
jaroslav@1258
   761
            // when it does happen, we must make a copy of the rest of
jaroslav@1258
   762
            // the table to use for the rest of the traversal. Since
jaroslav@1258
   763
            // this can only happen when we are near the end of the table,
jaroslav@1258
   764
            // even in these rare cases, this is not very expensive in
jaroslav@1258
   765
            // time or space.
jaroslav@1258
   766
jaroslav@1258
   767
            Object[] tab = traversalTable;
jaroslav@1258
   768
            int len = tab.length;
jaroslav@1258
   769
jaroslav@1258
   770
            int d = deletedSlot;
jaroslav@1258
   771
            K key = (K) tab[d];
jaroslav@1258
   772
            tab[d] = null;        // vacate the slot
jaroslav@1258
   773
            tab[d + 1] = null;
jaroslav@1258
   774
jaroslav@1258
   775
            // If traversing a copy, remove in real table.
jaroslav@1258
   776
            // We can skip gap-closure on copy.
jaroslav@1258
   777
            if (tab != IdentityHashMap.this.table) {
jaroslav@1258
   778
                IdentityHashMap.this.remove(key);
jaroslav@1258
   779
                expectedModCount = modCount;
jaroslav@1258
   780
                return;
jaroslav@1258
   781
            }
jaroslav@1258
   782
jaroslav@1258
   783
            size--;
jaroslav@1258
   784
jaroslav@1258
   785
            Object item;
jaroslav@1258
   786
            for (int i = nextKeyIndex(d, len); (item = tab[i]) != null;
jaroslav@1258
   787
                 i = nextKeyIndex(i, len)) {
jaroslav@1258
   788
                int r = hash(item, len);
jaroslav@1258
   789
                // See closeDeletion for explanation of this conditional
jaroslav@1258
   790
                if ((i < r && (r <= d || d <= i)) ||
jaroslav@1258
   791
                    (r <= d && d <= i)) {
jaroslav@1258
   792
jaroslav@1258
   793
                    // If we are about to swap an already-seen element
jaroslav@1258
   794
                    // into a slot that may later be returned by next(),
jaroslav@1258
   795
                    // then clone the rest of table for use in future
jaroslav@1258
   796
                    // next() calls. It is OK that our copy will have
jaroslav@1258
   797
                    // a gap in the "wrong" place, since it will never
jaroslav@1258
   798
                    // be used for searching anyway.
jaroslav@1258
   799
jaroslav@1258
   800
                    if (i < deletedSlot && d >= deletedSlot &&
jaroslav@1258
   801
                        traversalTable == IdentityHashMap.this.table) {
jaroslav@1258
   802
                        int remaining = len - deletedSlot;
jaroslav@1258
   803
                        Object[] newTable = new Object[remaining];
jaroslav@1258
   804
                        System.arraycopy(tab, deletedSlot,
jaroslav@1258
   805
                                         newTable, 0, remaining);
jaroslav@1258
   806
                        traversalTable = newTable;
jaroslav@1258
   807
                        index = 0;
jaroslav@1258
   808
                    }
jaroslav@1258
   809
jaroslav@1258
   810
                    tab[d] = item;
jaroslav@1258
   811
                    tab[d + 1] = tab[i + 1];
jaroslav@1258
   812
                    tab[i] = null;
jaroslav@1258
   813
                    tab[i + 1] = null;
jaroslav@1258
   814
                    d = i;
jaroslav@1258
   815
                }
jaroslav@1258
   816
            }
jaroslav@1258
   817
        }
jaroslav@1258
   818
    }
jaroslav@1258
   819
jaroslav@1258
   820
    private class KeyIterator extends IdentityHashMapIterator<K> {
jaroslav@1258
   821
        public K next() {
jaroslav@1258
   822
            return (K) unmaskNull(traversalTable[nextIndex()]);
jaroslav@1258
   823
        }
jaroslav@1258
   824
    }
jaroslav@1258
   825
jaroslav@1258
   826
    private class ValueIterator extends IdentityHashMapIterator<V> {
jaroslav@1258
   827
        public V next() {
jaroslav@1258
   828
            return (V) traversalTable[nextIndex() + 1];
jaroslav@1258
   829
        }
jaroslav@1258
   830
    }
jaroslav@1258
   831
jaroslav@1258
   832
    private class EntryIterator
jaroslav@1258
   833
        extends IdentityHashMapIterator<Map.Entry<K,V>>
jaroslav@1258
   834
    {
jaroslav@1258
   835
        private Entry lastReturnedEntry = null;
jaroslav@1258
   836
jaroslav@1258
   837
        public Map.Entry<K,V> next() {
jaroslav@1258
   838
            lastReturnedEntry = new Entry(nextIndex());
jaroslav@1258
   839
            return lastReturnedEntry;
jaroslav@1258
   840
        }
jaroslav@1258
   841
jaroslav@1258
   842
        public void remove() {
jaroslav@1258
   843
            lastReturnedIndex =
jaroslav@1258
   844
                ((null == lastReturnedEntry) ? -1 : lastReturnedEntry.index);
jaroslav@1258
   845
            super.remove();
jaroslav@1258
   846
            lastReturnedEntry.index = lastReturnedIndex;
jaroslav@1258
   847
            lastReturnedEntry = null;
jaroslav@1258
   848
        }
jaroslav@1258
   849
jaroslav@1258
   850
        private class Entry implements Map.Entry<K,V> {
jaroslav@1258
   851
            private int index;
jaroslav@1258
   852
jaroslav@1258
   853
            private Entry(int index) {
jaroslav@1258
   854
                this.index = index;
jaroslav@1258
   855
            }
jaroslav@1258
   856
jaroslav@1258
   857
            public K getKey() {
jaroslav@1258
   858
                checkIndexForEntryUse();
jaroslav@1258
   859
                return (K) unmaskNull(traversalTable[index]);
jaroslav@1258
   860
            }
jaroslav@1258
   861
jaroslav@1258
   862
            public V getValue() {
jaroslav@1258
   863
                checkIndexForEntryUse();
jaroslav@1258
   864
                return (V) traversalTable[index+1];
jaroslav@1258
   865
            }
jaroslav@1258
   866
jaroslav@1258
   867
            public V setValue(V value) {
jaroslav@1258
   868
                checkIndexForEntryUse();
jaroslav@1258
   869
                V oldValue = (V) traversalTable[index+1];
jaroslav@1258
   870
                traversalTable[index+1] = value;
jaroslav@1258
   871
                // if shadowing, force into main table
jaroslav@1258
   872
                if (traversalTable != IdentityHashMap.this.table)
jaroslav@1258
   873
                    put((K) traversalTable[index], value);
jaroslav@1258
   874
                return oldValue;
jaroslav@1258
   875
            }
jaroslav@1258
   876
jaroslav@1258
   877
            public boolean equals(Object o) {
jaroslav@1258
   878
                if (index < 0)
jaroslav@1258
   879
                    return super.equals(o);
jaroslav@1258
   880
jaroslav@1258
   881
                if (!(o instanceof Map.Entry))
jaroslav@1258
   882
                    return false;
jaroslav@1258
   883
                Map.Entry e = (Map.Entry)o;
jaroslav@1258
   884
                return (e.getKey() == unmaskNull(traversalTable[index]) &&
jaroslav@1258
   885
                       e.getValue() == traversalTable[index+1]);
jaroslav@1258
   886
            }
jaroslav@1258
   887
jaroslav@1258
   888
            public int hashCode() {
jaroslav@1258
   889
                if (lastReturnedIndex < 0)
jaroslav@1258
   890
                    return super.hashCode();
jaroslav@1258
   891
jaroslav@1258
   892
                return (System.identityHashCode(unmaskNull(traversalTable[index])) ^
jaroslav@1258
   893
                       System.identityHashCode(traversalTable[index+1]));
jaroslav@1258
   894
            }
jaroslav@1258
   895
jaroslav@1258
   896
            public String toString() {
jaroslav@1258
   897
                if (index < 0)
jaroslav@1258
   898
                    return super.toString();
jaroslav@1258
   899
jaroslav@1258
   900
                return (unmaskNull(traversalTable[index]) + "="
jaroslav@1258
   901
                        + traversalTable[index+1]);
jaroslav@1258
   902
            }
jaroslav@1258
   903
jaroslav@1258
   904
            private void checkIndexForEntryUse() {
jaroslav@1258
   905
                if (index < 0)
jaroslav@1258
   906
                    throw new IllegalStateException("Entry was removed");
jaroslav@1258
   907
            }
jaroslav@1258
   908
        }
jaroslav@1258
   909
    }
jaroslav@1258
   910
jaroslav@1258
   911
    // Views
jaroslav@1258
   912
jaroslav@1258
   913
    /**
jaroslav@1258
   914
     * This field is initialized to contain an instance of the entry set
jaroslav@1258
   915
     * view the first time this view is requested.  The view is stateless,
jaroslav@1258
   916
     * so there's no reason to create more than one.
jaroslav@1258
   917
     */
jaroslav@1258
   918
    private transient Set<Map.Entry<K,V>> entrySet = null;
jaroslav@1258
   919
jaroslav@1258
   920
    /**
jaroslav@1258
   921
     * Returns an identity-based set view of the keys contained in this map.
jaroslav@1258
   922
     * The set is backed by the map, so changes to the map are reflected in
jaroslav@1258
   923
     * the set, and vice-versa.  If the map is modified while an iteration
jaroslav@1258
   924
     * over the set is in progress, the results of the iteration are
jaroslav@1258
   925
     * undefined.  The set supports element removal, which removes the
jaroslav@1258
   926
     * corresponding mapping from the map, via the <tt>Iterator.remove</tt>,
jaroslav@1258
   927
     * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt>, and
jaroslav@1258
   928
     * <tt>clear</tt> methods.  It does not support the <tt>add</tt> or
jaroslav@1258
   929
     * <tt>addAll</tt> methods.
jaroslav@1258
   930
     *
jaroslav@1258
   931
     * <p><b>While the object returned by this method implements the
jaroslav@1258
   932
     * <tt>Set</tt> interface, it does <i>not</i> obey <tt>Set's</tt> general
jaroslav@1258
   933
     * contract.  Like its backing map, the set returned by this method
jaroslav@1258
   934
     * defines element equality as reference-equality rather than
jaroslav@1258
   935
     * object-equality.  This affects the behavior of its <tt>contains</tt>,
jaroslav@1258
   936
     * <tt>remove</tt>, <tt>containsAll</tt>, <tt>equals</tt>, and
jaroslav@1258
   937
     * <tt>hashCode</tt> methods.</b>
jaroslav@1258
   938
     *
jaroslav@1258
   939
     * <p><b>The <tt>equals</tt> method of the returned set returns <tt>true</tt>
jaroslav@1258
   940
     * only if the specified object is a set containing exactly the same
jaroslav@1258
   941
     * object references as the returned set.  The symmetry and transitivity
jaroslav@1258
   942
     * requirements of the <tt>Object.equals</tt> contract may be violated if
jaroslav@1258
   943
     * the set returned by this method is compared to a normal set.  However,
jaroslav@1258
   944
     * the <tt>Object.equals</tt> contract is guaranteed to hold among sets
jaroslav@1258
   945
     * returned by this method.</b>
jaroslav@1258
   946
     *
jaroslav@1258
   947
     * <p>The <tt>hashCode</tt> method of the returned set returns the sum of
jaroslav@1258
   948
     * the <i>identity hashcodes</i> of the elements in the set, rather than
jaroslav@1258
   949
     * the sum of their hashcodes.  This is mandated by the change in the
jaroslav@1258
   950
     * semantics of the <tt>equals</tt> method, in order to enforce the
jaroslav@1258
   951
     * general contract of the <tt>Object.hashCode</tt> method among sets
jaroslav@1258
   952
     * returned by this method.
jaroslav@1258
   953
     *
jaroslav@1258
   954
     * @return an identity-based set view of the keys contained in this map
jaroslav@1258
   955
     * @see Object#equals(Object)
jaroslav@1258
   956
     * @see System#identityHashCode(Object)
jaroslav@1258
   957
     */
jaroslav@1258
   958
    public Set<K> keySet() {
jaroslav@1258
   959
        Set<K> ks = keySet;
jaroslav@1258
   960
        if (ks != null)
jaroslav@1258
   961
            return ks;
jaroslav@1258
   962
        else
jaroslav@1258
   963
            return keySet = new KeySet();
jaroslav@1258
   964
    }
jaroslav@1258
   965
jaroslav@1258
   966
    private class KeySet extends AbstractSet<K> {
jaroslav@1258
   967
        public Iterator<K> iterator() {
jaroslav@1258
   968
            return new KeyIterator();
jaroslav@1258
   969
        }
jaroslav@1258
   970
        public int size() {
jaroslav@1258
   971
            return size;
jaroslav@1258
   972
        }
jaroslav@1258
   973
        public boolean contains(Object o) {
jaroslav@1258
   974
            return containsKey(o);
jaroslav@1258
   975
        }
jaroslav@1258
   976
        public boolean remove(Object o) {
jaroslav@1258
   977
            int oldSize = size;
jaroslav@1258
   978
            IdentityHashMap.this.remove(o);
jaroslav@1258
   979
            return size != oldSize;
jaroslav@1258
   980
        }
jaroslav@1258
   981
        /*
jaroslav@1258
   982
         * Must revert from AbstractSet's impl to AbstractCollection's, as
jaroslav@1258
   983
         * the former contains an optimization that results in incorrect
jaroslav@1258
   984
         * behavior when c is a smaller "normal" (non-identity-based) Set.
jaroslav@1258
   985
         */
jaroslav@1258
   986
        public boolean removeAll(Collection<?> c) {
jaroslav@1258
   987
            boolean modified = false;
jaroslav@1258
   988
            for (Iterator<K> i = iterator(); i.hasNext(); ) {
jaroslav@1258
   989
                if (c.contains(i.next())) {
jaroslav@1258
   990
                    i.remove();
jaroslav@1258
   991
                    modified = true;
jaroslav@1258
   992
                }
jaroslav@1258
   993
            }
jaroslav@1258
   994
            return modified;
jaroslav@1258
   995
        }
jaroslav@1258
   996
        public void clear() {
jaroslav@1258
   997
            IdentityHashMap.this.clear();
jaroslav@1258
   998
        }
jaroslav@1258
   999
        public int hashCode() {
jaroslav@1258
  1000
            int result = 0;
jaroslav@1258
  1001
            for (K key : this)
jaroslav@1258
  1002
                result += System.identityHashCode(key);
jaroslav@1258
  1003
            return result;
jaroslav@1258
  1004
        }
jaroslav@1258
  1005
    }
jaroslav@1258
  1006
jaroslav@1258
  1007
    /**
jaroslav@1258
  1008
     * Returns a {@link Collection} view of the values contained in this map.
jaroslav@1258
  1009
     * The collection is backed by the map, so changes to the map are
jaroslav@1258
  1010
     * reflected in the collection, and vice-versa.  If the map is
jaroslav@1258
  1011
     * modified while an iteration over the collection is in progress,
jaroslav@1258
  1012
     * the results of the iteration are undefined.  The collection
jaroslav@1258
  1013
     * supports element removal, which removes the corresponding
jaroslav@1258
  1014
     * mapping from the map, via the <tt>Iterator.remove</tt>,
jaroslav@1258
  1015
     * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
jaroslav@1258
  1016
     * <tt>retainAll</tt> and <tt>clear</tt> methods.  It does not
jaroslav@1258
  1017
     * support the <tt>add</tt> or <tt>addAll</tt> methods.
jaroslav@1258
  1018
     *
jaroslav@1258
  1019
     * <p><b>While the object returned by this method implements the
jaroslav@1258
  1020
     * <tt>Collection</tt> interface, it does <i>not</i> obey
jaroslav@1258
  1021
     * <tt>Collection's</tt> general contract.  Like its backing map,
jaroslav@1258
  1022
     * the collection returned by this method defines element equality as
jaroslav@1258
  1023
     * reference-equality rather than object-equality.  This affects the
jaroslav@1258
  1024
     * behavior of its <tt>contains</tt>, <tt>remove</tt> and
jaroslav@1258
  1025
     * <tt>containsAll</tt> methods.</b>
jaroslav@1258
  1026
     */
jaroslav@1258
  1027
    public Collection<V> values() {
jaroslav@1258
  1028
        Collection<V> vs = values;
jaroslav@1258
  1029
        if (vs != null)
jaroslav@1258
  1030
            return vs;
jaroslav@1258
  1031
        else
jaroslav@1258
  1032
            return values = new Values();
jaroslav@1258
  1033
    }
jaroslav@1258
  1034
jaroslav@1258
  1035
    private class Values extends AbstractCollection<V> {
jaroslav@1258
  1036
        public Iterator<V> iterator() {
jaroslav@1258
  1037
            return new ValueIterator();
jaroslav@1258
  1038
        }
jaroslav@1258
  1039
        public int size() {
jaroslav@1258
  1040
            return size;
jaroslav@1258
  1041
        }
jaroslav@1258
  1042
        public boolean contains(Object o) {
jaroslav@1258
  1043
            return containsValue(o);
jaroslav@1258
  1044
        }
jaroslav@1258
  1045
        public boolean remove(Object o) {
jaroslav@1258
  1046
            for (Iterator<V> i = iterator(); i.hasNext(); ) {
jaroslav@1258
  1047
                if (i.next() == o) {
jaroslav@1258
  1048
                    i.remove();
jaroslav@1258
  1049
                    return true;
jaroslav@1258
  1050
                }
jaroslav@1258
  1051
            }
jaroslav@1258
  1052
            return false;
jaroslav@1258
  1053
        }
jaroslav@1258
  1054
        public void clear() {
jaroslav@1258
  1055
            IdentityHashMap.this.clear();
jaroslav@1258
  1056
        }
jaroslav@1258
  1057
    }
jaroslav@1258
  1058
jaroslav@1258
  1059
    /**
jaroslav@1258
  1060
     * Returns a {@link Set} view of the mappings contained in this map.
jaroslav@1258
  1061
     * Each element in the returned set is a reference-equality-based
jaroslav@1258
  1062
     * <tt>Map.Entry</tt>.  The set is backed by the map, so changes
jaroslav@1258
  1063
     * to the map are reflected in the set, and vice-versa.  If the
jaroslav@1258
  1064
     * map is modified while an iteration over the set is in progress,
jaroslav@1258
  1065
     * the results of the iteration are undefined.  The set supports
jaroslav@1258
  1066
     * element removal, which removes the corresponding mapping from
jaroslav@1258
  1067
     * the map, via the <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
jaroslav@1258
  1068
     * <tt>removeAll</tt>, <tt>retainAll</tt> and <tt>clear</tt>
jaroslav@1258
  1069
     * methods.  It does not support the <tt>add</tt> or
jaroslav@1258
  1070
     * <tt>addAll</tt> methods.
jaroslav@1258
  1071
     *
jaroslav@1258
  1072
     * <p>Like the backing map, the <tt>Map.Entry</tt> objects in the set
jaroslav@1258
  1073
     * returned by this method define key and value equality as
jaroslav@1258
  1074
     * reference-equality rather than object-equality.  This affects the
jaroslav@1258
  1075
     * behavior of the <tt>equals</tt> and <tt>hashCode</tt> methods of these
jaroslav@1258
  1076
     * <tt>Map.Entry</tt> objects.  A reference-equality based <tt>Map.Entry
jaroslav@1258
  1077
     * e</tt> is equal to an object <tt>o</tt> if and only if <tt>o</tt> is a
jaroslav@1258
  1078
     * <tt>Map.Entry</tt> and <tt>e.getKey()==o.getKey() &amp;&amp;
jaroslav@1258
  1079
     * e.getValue()==o.getValue()</tt>.  To accommodate these equals
jaroslav@1258
  1080
     * semantics, the <tt>hashCode</tt> method returns
jaroslav@1258
  1081
     * <tt>System.identityHashCode(e.getKey()) ^
jaroslav@1258
  1082
     * System.identityHashCode(e.getValue())</tt>.
jaroslav@1258
  1083
     *
jaroslav@1258
  1084
     * <p><b>Owing to the reference-equality-based semantics of the
jaroslav@1258
  1085
     * <tt>Map.Entry</tt> instances in the set returned by this method,
jaroslav@1258
  1086
     * it is possible that the symmetry and transitivity requirements of
jaroslav@1258
  1087
     * the {@link Object#equals(Object)} contract may be violated if any of
jaroslav@1258
  1088
     * the entries in the set is compared to a normal map entry, or if
jaroslav@1258
  1089
     * the set returned by this method is compared to a set of normal map
jaroslav@1258
  1090
     * entries (such as would be returned by a call to this method on a normal
jaroslav@1258
  1091
     * map).  However, the <tt>Object.equals</tt> contract is guaranteed to
jaroslav@1258
  1092
     * hold among identity-based map entries, and among sets of such entries.
jaroslav@1258
  1093
     * </b>
jaroslav@1258
  1094
     *
jaroslav@1258
  1095
     * @return a set view of the identity-mappings contained in this map
jaroslav@1258
  1096
     */
jaroslav@1258
  1097
    public Set<Map.Entry<K,V>> entrySet() {
jaroslav@1258
  1098
        Set<Map.Entry<K,V>> es = entrySet;
jaroslav@1258
  1099
        if (es != null)
jaroslav@1258
  1100
            return es;
jaroslav@1258
  1101
        else
jaroslav@1258
  1102
            return entrySet = new EntrySet();
jaroslav@1258
  1103
    }
jaroslav@1258
  1104
jaroslav@1258
  1105
    private class EntrySet extends AbstractSet<Map.Entry<K,V>> {
jaroslav@1258
  1106
        public Iterator<Map.Entry<K,V>> iterator() {
jaroslav@1258
  1107
            return new EntryIterator();
jaroslav@1258
  1108
        }
jaroslav@1258
  1109
        public boolean contains(Object o) {
jaroslav@1258
  1110
            if (!(o instanceof Map.Entry))
jaroslav@1258
  1111
                return false;
jaroslav@1258
  1112
            Map.Entry entry = (Map.Entry)o;
jaroslav@1258
  1113
            return containsMapping(entry.getKey(), entry.getValue());
jaroslav@1258
  1114
        }
jaroslav@1258
  1115
        public boolean remove(Object o) {
jaroslav@1258
  1116
            if (!(o instanceof Map.Entry))
jaroslav@1258
  1117
                return false;
jaroslav@1258
  1118
            Map.Entry entry = (Map.Entry)o;
jaroslav@1258
  1119
            return removeMapping(entry.getKey(), entry.getValue());
jaroslav@1258
  1120
        }
jaroslav@1258
  1121
        public int size() {
jaroslav@1258
  1122
            return size;
jaroslav@1258
  1123
        }
jaroslav@1258
  1124
        public void clear() {
jaroslav@1258
  1125
            IdentityHashMap.this.clear();
jaroslav@1258
  1126
        }
jaroslav@1258
  1127
        /*
jaroslav@1258
  1128
         * Must revert from AbstractSet's impl to AbstractCollection's, as
jaroslav@1258
  1129
         * the former contains an optimization that results in incorrect
jaroslav@1258
  1130
         * behavior when c is a smaller "normal" (non-identity-based) Set.
jaroslav@1258
  1131
         */
jaroslav@1258
  1132
        public boolean removeAll(Collection<?> c) {
jaroslav@1258
  1133
            boolean modified = false;
jaroslav@1258
  1134
            for (Iterator<Map.Entry<K,V>> i = iterator(); i.hasNext(); ) {
jaroslav@1258
  1135
                if (c.contains(i.next())) {
jaroslav@1258
  1136
                    i.remove();
jaroslav@1258
  1137
                    modified = true;
jaroslav@1258
  1138
                }
jaroslav@1258
  1139
            }
jaroslav@1258
  1140
            return modified;
jaroslav@1258
  1141
        }
jaroslav@1258
  1142
jaroslav@1258
  1143
        public Object[] toArray() {
jaroslav@1258
  1144
            int size = size();
jaroslav@1258
  1145
            Object[] result = new Object[size];
jaroslav@1258
  1146
            Iterator<Map.Entry<K,V>> it = iterator();
jaroslav@1258
  1147
            for (int i = 0; i < size; i++)
jaroslav@1258
  1148
                result[i] = new AbstractMap.SimpleEntry<>(it.next());
jaroslav@1258
  1149
            return result;
jaroslav@1258
  1150
        }
jaroslav@1258
  1151
jaroslav@1258
  1152
        @SuppressWarnings("unchecked")
jaroslav@1258
  1153
        public <T> T[] toArray(T[] a) {
jaroslav@1258
  1154
            int size = size();
jaroslav@1258
  1155
            if (a.length < size)
jaroslav@1258
  1156
                a = (T[])java.lang.reflect.Array
jaroslav@1258
  1157
                    .newInstance(a.getClass().getComponentType(), size);
jaroslav@1258
  1158
            Iterator<Map.Entry<K,V>> it = iterator();
jaroslav@1258
  1159
            for (int i = 0; i < size; i++)
jaroslav@1258
  1160
                a[i] = (T) new AbstractMap.SimpleEntry<>(it.next());
jaroslav@1258
  1161
            if (a.length > size)
jaroslav@1258
  1162
                a[size] = null;
jaroslav@1258
  1163
            return a;
jaroslav@1258
  1164
        }
jaroslav@1258
  1165
    }
jaroslav@1258
  1166
jaroslav@1258
  1167
jaroslav@1258
  1168
    private static final long serialVersionUID = 8188218128353913216L;
jaroslav@1258
  1169
jaroslav@1258
  1170
    /**
jaroslav@1258
  1171
     * Save the state of the <tt>IdentityHashMap</tt> instance to a stream
jaroslav@1258
  1172
     * (i.e., serialize it).
jaroslav@1258
  1173
     *
jaroslav@1258
  1174
     * @serialData The <i>size</i> of the HashMap (the number of key-value
jaroslav@1258
  1175
     *          mappings) (<tt>int</tt>), followed by the key (Object) and
jaroslav@1258
  1176
     *          value (Object) for each key-value mapping represented by the
jaroslav@1258
  1177
     *          IdentityHashMap.  The key-value mappings are emitted in no
jaroslav@1258
  1178
     *          particular order.
jaroslav@1258
  1179
     */
jaroslav@1258
  1180
    private void writeObject(java.io.ObjectOutputStream s)
jaroslav@1258
  1181
        throws java.io.IOException  {
jaroslav@1258
  1182
        // Write out and any hidden stuff
jaroslav@1258
  1183
        s.defaultWriteObject();
jaroslav@1258
  1184
jaroslav@1258
  1185
        // Write out size (number of Mappings)
jaroslav@1258
  1186
        s.writeInt(size);
jaroslav@1258
  1187
jaroslav@1258
  1188
        // Write out keys and values (alternating)
jaroslav@1258
  1189
        Object[] tab = table;
jaroslav@1258
  1190
        for (int i = 0; i < tab.length; i += 2) {
jaroslav@1258
  1191
            Object key = tab[i];
jaroslav@1258
  1192
            if (key != null) {
jaroslav@1258
  1193
                s.writeObject(unmaskNull(key));
jaroslav@1258
  1194
                s.writeObject(tab[i + 1]);
jaroslav@1258
  1195
            }
jaroslav@1258
  1196
        }
jaroslav@1258
  1197
    }
jaroslav@1258
  1198
jaroslav@1258
  1199
    /**
jaroslav@1258
  1200
     * Reconstitute the <tt>IdentityHashMap</tt> instance from a stream (i.e.,
jaroslav@1258
  1201
     * deserialize it).
jaroslav@1258
  1202
     */
jaroslav@1258
  1203
    private void readObject(java.io.ObjectInputStream s)
jaroslav@1258
  1204
        throws java.io.IOException, ClassNotFoundException  {
jaroslav@1258
  1205
        // Read in any hidden stuff
jaroslav@1258
  1206
        s.defaultReadObject();
jaroslav@1258
  1207
jaroslav@1258
  1208
        // Read in size (number of Mappings)
jaroslav@1258
  1209
        int size = s.readInt();
jaroslav@1258
  1210
jaroslav@1258
  1211
        // Allow for 33% growth (i.e., capacity is >= 2* size()).
jaroslav@1258
  1212
        init(capacity((size*4)/3));
jaroslav@1258
  1213
jaroslav@1258
  1214
        // Read the keys and values, and put the mappings in the table
jaroslav@1258
  1215
        for (int i=0; i<size; i++) {
jaroslav@1258
  1216
            K key = (K) s.readObject();
jaroslav@1258
  1217
            V value = (V) s.readObject();
jaroslav@1258
  1218
            putForCreate(key, value);
jaroslav@1258
  1219
        }
jaroslav@1258
  1220
    }
jaroslav@1258
  1221
jaroslav@1258
  1222
    /**
jaroslav@1258
  1223
     * The put method for readObject.  It does not resize the table,
jaroslav@1258
  1224
     * update modCount, etc.
jaroslav@1258
  1225
     */
jaroslav@1258
  1226
    private void putForCreate(K key, V value)
jaroslav@1258
  1227
        throws IOException
jaroslav@1258
  1228
    {
jaroslav@1258
  1229
        K k = (K)maskNull(key);
jaroslav@1258
  1230
        Object[] tab = table;
jaroslav@1258
  1231
        int len = tab.length;
jaroslav@1258
  1232
        int i = hash(k, len);
jaroslav@1258
  1233
jaroslav@1258
  1234
        Object item;
jaroslav@1258
  1235
        while ( (item = tab[i]) != null) {
jaroslav@1258
  1236
            if (item == k)
jaroslav@1258
  1237
                throw new java.io.StreamCorruptedException();
jaroslav@1258
  1238
            i = nextKeyIndex(i, len);
jaroslav@1258
  1239
        }
jaroslav@1258
  1240
        tab[i] = k;
jaroslav@1258
  1241
        tab[i + 1] = value;
jaroslav@1258
  1242
    }
jaroslav@1258
  1243
}