github.com/keysonZZZ/kmg@v0.0.0-20151121023212-05317bfd7d39/kmgRpc/kmgRpcJava/java/src/com/google/gson/internal/LinkedHashTreeMap.java (about)

     1  /*
     2   * Copyright (C) 2010 The Android Open Source Project
     3   * Copyright (C) 2012 Google Inc.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package com.google.gson.internal;
    19  
    20  import java.io.ObjectStreamException;
    21  import java.io.Serializable;
    22  import java.util.AbstractMap;
    23  import java.util.AbstractSet;
    24  import java.util.Arrays;
    25  import java.util.Comparator;
    26  import java.util.ConcurrentModificationException;
    27  import java.util.Iterator;
    28  import java.util.LinkedHashMap;
    29  import java.util.NoSuchElementException;
    30  import java.util.Set;
    31  
    32  /**
    33   * A map of comparable keys to values. Unlike {@code TreeMap}, this class uses
    34   * insertion order for iteration order. Comparison order is only used as an
    35   * optimization for efficient insertion and removal.
    36   *
    37   * <p>This implementation was derived from Android 4.1's TreeMap and
    38   * LinkedHashMap classes.
    39   */
    40  public final class LinkedHashTreeMap<K, V> extends AbstractMap<K, V> implements Serializable {
    41    @SuppressWarnings({ "unchecked", "rawtypes" }) // to avoid Comparable<Comparable<Comparable<...>>>
    42    private static final Comparator<Comparable> NATURAL_ORDER = new Comparator<Comparable>() {
    43      public int compare(Comparable a, Comparable b) {
    44        return a.compareTo(b);
    45      }
    46    };
    47  
    48    Comparator<? super K> comparator;
    49    Node<K, V>[] table;
    50    final Node<K, V> header;
    51    int size = 0;
    52    int modCount = 0;
    53    int threshold;
    54  
    55    /**
    56     * Create a natural order, empty tree map whose keys must be mutually
    57     * comparable and non-null.
    58     */
    59    @SuppressWarnings("unchecked") // unsafe! this assumes K is comparable
    60    public LinkedHashTreeMap() {
    61      this((Comparator<? super K>) NATURAL_ORDER);
    62    }
    63  
    64    /**
    65     * Create a tree map ordered by {@code comparator}. This map's keys may only
    66     * be null if {@code comparator} permits.
    67     *
    68     * @param comparator the comparator to order elements with, or {@code null} to
    69     *     use the natural ordering.
    70     */
    71    @SuppressWarnings({ "unchecked", "rawtypes" }) // unsafe! if comparator is null, this assumes K is comparable
    72    public LinkedHashTreeMap(Comparator<? super K> comparator) {
    73      this.comparator = comparator != null
    74          ? comparator
    75          : (Comparator) NATURAL_ORDER;
    76      this.header = new Node<K, V>();
    77      this.table = new Node[16]; // TODO: sizing/resizing policies
    78      this.threshold = (table.length / 2) + (table.length / 4); // 3/4 capacity
    79    }
    80  
    81    @Override public int size() {
    82      return size;
    83    }
    84  
    85    @Override public V get(Object key) {
    86      Node<K, V> node = findByObject(key);
    87      return node != null ? node.value : null;
    88    }
    89  
    90    @Override public boolean containsKey(Object key) {
    91      return findByObject(key) != null;
    92    }
    93  
    94    @Override public V put(K key, V value) {
    95      if (key == null) {
    96        throw new NullPointerException("key == null");
    97      }
    98      Node<K, V> created = find(key, true);
    99      V result = created.value;
   100      created.value = value;
   101      return result;
   102    }
   103  
   104    @Override public void clear() {
   105      Arrays.fill(table, null);
   106      size = 0;
   107      modCount++;
   108  
   109      // Clear all links to help GC
   110      Node<K, V> header = this.header;
   111      for (Node<K, V> e = header.next; e != header; ) {
   112        Node<K, V> next = e.next;
   113        e.next = e.prev = null;
   114        e = next;
   115      }
   116  
   117      header.next = header.prev = header;
   118    }
   119  
   120    @Override public V remove(Object key) {
   121      Node<K, V> node = removeInternalByKey(key);
   122      return node != null ? node.value : null;
   123    }
   124  
   125    /**
   126     * Returns the node at or adjacent to the given key, creating it if requested.
   127     *
   128     * @throws ClassCastException if {@code key} and the tree's keys aren't
   129     *     mutually comparable.
   130     */
   131    Node<K, V> find(K key, boolean create) {
   132      Comparator<? super K> comparator = this.comparator;
   133      Node<K, V>[] table = this.table;
   134      int hash = secondaryHash(key.hashCode());
   135      int index = hash & (table.length - 1);
   136      Node<K, V> nearest = table[index];
   137      int comparison = 0;
   138  
   139      if (nearest != null) {
   140        // Micro-optimization: avoid polymorphic calls to Comparator.compare().
   141        @SuppressWarnings("unchecked") // Throws a ClassCastException below if there's trouble.
   142        Comparable<Object> comparableKey = (comparator == NATURAL_ORDER)
   143            ? (Comparable<Object>) key
   144            : null;
   145  
   146        while (true) {
   147          comparison = (comparableKey != null)
   148              ? comparableKey.compareTo(nearest.key)
   149              : comparator.compare(key, nearest.key);
   150  
   151          // We found the requested key.
   152          if (comparison == 0) {
   153            return nearest;
   154          }
   155  
   156          // If it exists, the key is in a subtree. Go deeper.
   157          Node<K, V> child = (comparison < 0) ? nearest.left : nearest.right;
   158          if (child == null) {
   159            break;
   160          }
   161  
   162          nearest = child;
   163        }
   164      }
   165  
   166      // The key doesn't exist in this tree.
   167      if (!create) {
   168        return null;
   169      }
   170  
   171      // Create the node and add it to the tree or the table.
   172      Node<K, V> header = this.header;
   173      Node<K, V> created;
   174      if (nearest == null) {
   175        // Check that the value is comparable if we didn't do any comparisons.
   176        if (comparator == NATURAL_ORDER && !(key instanceof Comparable)) {
   177          throw new ClassCastException(key.getClass().getName() + " is not Comparable");
   178        }
   179        created = new Node<K, V>(nearest, key, hash, header, header.prev);
   180        table[index] = created;
   181      } else {
   182        created = new Node<K, V>(nearest, key, hash, header, header.prev);
   183        if (comparison < 0) { // nearest.key is higher
   184          nearest.left = created;
   185        } else { // comparison > 0, nearest.key is lower
   186          nearest.right = created;
   187        }
   188        rebalance(nearest, true);
   189      }
   190  
   191      if (size++ > threshold) {
   192        doubleCapacity();
   193      }
   194      modCount++;
   195  
   196      return created;
   197    }
   198  
   199    @SuppressWarnings("unchecked")
   200    Node<K, V> findByObject(Object key) {
   201      try {
   202        return key != null ? find((K) key, false) : null;
   203      } catch (ClassCastException e) {
   204        return null;
   205      }
   206    }
   207  
   208    /**
   209     * Returns this map's entry that has the same key and value as {@code
   210     * entry}, or null if this map has no such entry.
   211     *
   212     * <p>This method uses the comparator for key equality rather than {@code
   213     * equals}. If this map's comparator isn't consistent with equals (such as
   214     * {@code String.CASE_INSENSITIVE_ORDER}), then {@code remove()} and {@code
   215     * contains()} will violate the collections API.
   216     */
   217    Node<K, V> findByEntry(Entry<?, ?> entry) {
   218      Node<K, V> mine = findByObject(entry.getKey());
   219      boolean valuesEqual = mine != null && equal(mine.value, entry.getValue());
   220      return valuesEqual ? mine : null;
   221    }
   222  
   223    private boolean equal(Object a, Object b) {
   224      return a == b || (a != null && a.equals(b));
   225    }
   226  
   227    /**
   228     * Applies a supplemental hash function to a given hashCode, which defends
   229     * against poor quality hash functions. This is critical because HashMap
   230     * uses power-of-two length hash tables, that otherwise encounter collisions
   231     * for hashCodes that do not differ in lower or upper bits.
   232     */
   233    private static int secondaryHash(int h) {
   234      // Doug Lea's supplemental hash function
   235      h ^= (h >>> 20) ^ (h >>> 12);
   236      return h ^ (h >>> 7) ^ (h >>> 4);
   237    }
   238  
   239    /**
   240     * Removes {@code node} from this tree, rearranging the tree's structure as
   241     * necessary.
   242     *
   243     * @param unlink true to also unlink this node from the iteration linked list.
   244     */
   245    void removeInternal(Node<K, V> node, boolean unlink) {
   246      if (unlink) {
   247        node.prev.next = node.next;
   248        node.next.prev = node.prev;
   249        node.next = node.prev = null; // Help the GC (for performance)
   250      }
   251  
   252      Node<K, V> left = node.left;
   253      Node<K, V> right = node.right;
   254      Node<K, V> originalParent = node.parent;
   255      if (left != null && right != null) {
   256  
   257        /*
   258         * To remove a node with both left and right subtrees, move an
   259         * adjacent node from one of those subtrees into this node's place.
   260         *
   261         * Removing the adjacent node may change this node's subtrees. This
   262         * node may no longer have two subtrees once the adjacent node is
   263         * gone!
   264         */
   265  
   266        Node<K, V> adjacent = (left.height > right.height) ? left.last() : right.first();
   267        removeInternal(adjacent, false); // takes care of rebalance and size--
   268  
   269        int leftHeight = 0;
   270        left = node.left;
   271        if (left != null) {
   272          leftHeight = left.height;
   273          adjacent.left = left;
   274          left.parent = adjacent;
   275          node.left = null;
   276        }
   277        int rightHeight = 0;
   278        right = node.right;
   279        if (right != null) {
   280          rightHeight = right.height;
   281          adjacent.right = right;
   282          right.parent = adjacent;
   283          node.right = null;
   284        }
   285        adjacent.height = Math.max(leftHeight, rightHeight) + 1;
   286        replaceInParent(node, adjacent);
   287        return;
   288      } else if (left != null) {
   289        replaceInParent(node, left);
   290        node.left = null;
   291      } else if (right != null) {
   292        replaceInParent(node, right);
   293        node.right = null;
   294      } else {
   295        replaceInParent(node, null);
   296      }
   297  
   298      rebalance(originalParent, false);
   299      size--;
   300      modCount++;
   301    }
   302  
   303    Node<K, V> removeInternalByKey(Object key) {
   304      Node<K, V> node = findByObject(key);
   305      if (node != null) {
   306        removeInternal(node, true);
   307      }
   308      return node;
   309    }
   310  
   311    private void replaceInParent(Node<K, V> node, Node<K, V> replacement) {
   312      Node<K, V> parent = node.parent;
   313      node.parent = null;
   314      if (replacement != null) {
   315        replacement.parent = parent;
   316      }
   317  
   318      if (parent != null) {
   319        if (parent.left == node) {
   320          parent.left = replacement;
   321        } else {
   322          assert (parent.right == node);
   323          parent.right = replacement;
   324        }
   325      } else {
   326        int index = node.hash & (table.length - 1);
   327        table[index] = replacement;
   328      }
   329    }
   330  
   331    /**
   332     * Rebalances the tree by making any AVL rotations necessary between the
   333     * newly-unbalanced node and the tree's root.
   334     *
   335     * @param insert true if the node was unbalanced by an insert; false if it
   336     *     was by a removal.
   337     */
   338    private void rebalance(Node<K, V> unbalanced, boolean insert) {
   339      for (Node<K, V> node = unbalanced; node != null; node = node.parent) {
   340        Node<K, V> left = node.left;
   341        Node<K, V> right = node.right;
   342        int leftHeight = left != null ? left.height : 0;
   343        int rightHeight = right != null ? right.height : 0;
   344  
   345        int delta = leftHeight - rightHeight;
   346        if (delta == -2) {
   347          Node<K, V> rightLeft = right.left;
   348          Node<K, V> rightRight = right.right;
   349          int rightRightHeight = rightRight != null ? rightRight.height : 0;
   350          int rightLeftHeight = rightLeft != null ? rightLeft.height : 0;
   351  
   352          int rightDelta = rightLeftHeight - rightRightHeight;
   353          if (rightDelta == -1 || (rightDelta == 0 && !insert)) {
   354            rotateLeft(node); // AVL right right
   355          } else {
   356            assert (rightDelta == 1);
   357            rotateRight(right); // AVL right left
   358            rotateLeft(node);
   359          }
   360          if (insert) {
   361            break; // no further rotations will be necessary
   362          }
   363  
   364        } else if (delta == 2) {
   365          Node<K, V> leftLeft = left.left;
   366          Node<K, V> leftRight = left.right;
   367          int leftRightHeight = leftRight != null ? leftRight.height : 0;
   368          int leftLeftHeight = leftLeft != null ? leftLeft.height : 0;
   369  
   370          int leftDelta = leftLeftHeight - leftRightHeight;
   371          if (leftDelta == 1 || (leftDelta == 0 && !insert)) {
   372            rotateRight(node); // AVL left left
   373          } else {
   374            assert (leftDelta == -1);
   375            rotateLeft(left); // AVL left right
   376            rotateRight(node);
   377          }
   378          if (insert) {
   379            break; // no further rotations will be necessary
   380          }
   381  
   382        } else if (delta == 0) {
   383          node.height = leftHeight + 1; // leftHeight == rightHeight
   384          if (insert) {
   385            break; // the insert caused balance, so rebalancing is done!
   386          }
   387  
   388        } else {
   389          assert (delta == -1 || delta == 1);
   390          node.height = Math.max(leftHeight, rightHeight) + 1;
   391          if (!insert) {
   392            break; // the height hasn't changed, so rebalancing is done!
   393          }
   394        }
   395      }
   396    }
   397  
   398    /**
   399     * Rotates the subtree so that its root's right child is the new root.
   400     */
   401    private void rotateLeft(Node<K, V> root) {
   402      Node<K, V> left = root.left;
   403      Node<K, V> pivot = root.right;
   404      Node<K, V> pivotLeft = pivot.left;
   405      Node<K, V> pivotRight = pivot.right;
   406  
   407      // move the pivot's left child to the root's right
   408      root.right = pivotLeft;
   409      if (pivotLeft != null) {
   410        pivotLeft.parent = root;
   411      }
   412  
   413      replaceInParent(root, pivot);
   414  
   415      // move the root to the pivot's left
   416      pivot.left = root;
   417      root.parent = pivot;
   418  
   419      // fix heights
   420      root.height = Math.max(left != null ? left.height : 0,
   421          pivotLeft != null ? pivotLeft.height : 0) + 1;
   422      pivot.height = Math.max(root.height,
   423          pivotRight != null ? pivotRight.height : 0) + 1;
   424    }
   425  
   426    /**
   427     * Rotates the subtree so that its root's left child is the new root.
   428     */
   429    private void rotateRight(Node<K, V> root) {
   430      Node<K, V> pivot = root.left;
   431      Node<K, V> right = root.right;
   432      Node<K, V> pivotLeft = pivot.left;
   433      Node<K, V> pivotRight = pivot.right;
   434  
   435      // move the pivot's right child to the root's left
   436      root.left = pivotRight;
   437      if (pivotRight != null) {
   438        pivotRight.parent = root;
   439      }
   440  
   441      replaceInParent(root, pivot);
   442  
   443      // move the root to the pivot's right
   444      pivot.right = root;
   445      root.parent = pivot;
   446  
   447      // fixup heights
   448      root.height = Math.max(right != null ? right.height : 0,
   449          pivotRight != null ? pivotRight.height : 0) + 1;
   450      pivot.height = Math.max(root.height,
   451          pivotLeft != null ? pivotLeft.height : 0) + 1;
   452    }
   453  
   454    private EntrySet entrySet;
   455    private KeySet keySet;
   456  
   457    @Override public Set<Entry<K, V>> entrySet() {
   458      EntrySet result = entrySet;
   459      return result != null ? result : (entrySet = new EntrySet());
   460    }
   461  
   462    @Override public Set<K> keySet() {
   463      KeySet result = keySet;
   464      return result != null ? result : (keySet = new KeySet());
   465    }
   466  
   467    static final class Node<K, V> implements Entry<K, V> {
   468      Node<K, V> parent;
   469      Node<K, V> left;
   470      Node<K, V> right;
   471      Node<K, V> next;
   472      Node<K, V> prev;
   473      final K key;
   474      final int hash;
   475      V value;
   476      int height;
   477  
   478      /** Create the header entry */
   479      Node() {
   480        key = null;
   481        hash = -1;
   482        next = prev = this;
   483      }
   484  
   485      /** Create a regular entry */
   486      Node(Node<K, V> parent, K key, int hash, Node<K, V> next, Node<K, V> prev) {
   487        this.parent = parent;
   488        this.key = key;
   489        this.hash = hash;
   490        this.height = 1;
   491        this.next = next;
   492        this.prev = prev;
   493        prev.next = this;
   494        next.prev = this;
   495      }
   496  
   497      public K getKey() {
   498        return key;
   499      }
   500  
   501      public V getValue() {
   502        return value;
   503      }
   504  
   505      public V setValue(V value) {
   506        V oldValue = this.value;
   507        this.value = value;
   508        return oldValue;
   509      }
   510  
   511      @SuppressWarnings("rawtypes")
   512      @Override public boolean equals(Object o) {
   513        if (o instanceof Entry) {
   514          Entry other = (Entry) o;
   515          return (key == null ? other.getKey() == null : key.equals(other.getKey()))
   516              && (value == null ? other.getValue() == null : value.equals(other.getValue()));
   517        }
   518        return false;
   519      }
   520  
   521      @Override public int hashCode() {
   522        return (key == null ? 0 : key.hashCode())
   523            ^ (value == null ? 0 : value.hashCode());
   524      }
   525  
   526      @Override public String toString() {
   527        return key + "=" + value;
   528      }
   529  
   530      /**
   531       * Returns the first node in this subtree.
   532       */
   533      public Node<K, V> first() {
   534        Node<K, V> node = this;
   535        Node<K, V> child = node.left;
   536        while (child != null) {
   537          node = child;
   538          child = node.left;
   539        }
   540        return node;
   541      }
   542  
   543      /**
   544       * Returns the last node in this subtree.
   545       */
   546      public Node<K, V> last() {
   547        Node<K, V> node = this;
   548        Node<K, V> child = node.right;
   549        while (child != null) {
   550          node = child;
   551          child = node.right;
   552        }
   553        return node;
   554      }
   555    }
   556  
   557    private void doubleCapacity() {
   558      table = doubleCapacity(table);
   559      threshold = (table.length / 2) + (table.length / 4); // 3/4 capacity
   560    }
   561  
   562    /**
   563     * Returns a new array containing the same nodes as {@code oldTable}, but with
   564     * twice as many trees, each of (approximately) half the previous size.
   565     */
   566    static <K, V> Node<K, V>[] doubleCapacity(Node<K, V>[] oldTable) {
   567      // TODO: don't do anything if we're already at MAX_CAPACITY
   568      int oldCapacity = oldTable.length;
   569      @SuppressWarnings("unchecked") // Arrays and generics don't get along.
   570      Node<K, V>[] newTable = new Node[oldCapacity * 2];
   571      AvlIterator<K, V> iterator = new AvlIterator<K, V>();
   572      AvlBuilder<K, V> leftBuilder = new AvlBuilder<K, V>();
   573      AvlBuilder<K, V> rightBuilder = new AvlBuilder<K, V>();
   574  
   575      // Split each tree into two trees.
   576      for (int i = 0; i < oldCapacity; i++) {
   577        Node<K, V> root = oldTable[i];
   578        if (root == null) {
   579          continue;
   580        }
   581  
   582        // Compute the sizes of the left and right trees.
   583        iterator.reset(root);
   584        int leftSize = 0;
   585        int rightSize = 0;
   586        for (Node<K, V> node; (node = iterator.next()) != null; ) {
   587          if ((node.hash & oldCapacity) == 0) {
   588            leftSize++;
   589          } else {
   590            rightSize++;
   591          }
   592        }
   593  
   594        // Split the tree into two.
   595        leftBuilder.reset(leftSize);
   596        rightBuilder.reset(rightSize);
   597        iterator.reset(root);
   598        for (Node<K, V> node; (node = iterator.next()) != null; ) {
   599          if ((node.hash & oldCapacity) == 0) {
   600            leftBuilder.add(node);
   601          } else {
   602            rightBuilder.add(node);
   603          }
   604        }
   605  
   606        // Populate the enlarged array with these new roots.
   607        newTable[i] = leftSize > 0 ? leftBuilder.root() : null;
   608        newTable[i + oldCapacity] = rightSize > 0 ? rightBuilder.root() : null;
   609      }
   610      return newTable;
   611    }
   612  
   613    /**
   614     * Walks an AVL tree in iteration order. Once a node has been returned, its
   615     * left, right and parent links are <strong>no longer used</strong>. For this
   616     * reason it is safe to transform these links as you walk a tree.
   617     *
   618     * <p><strong>Warning:</strong> this iterator is destructive. It clears the
   619     * parent node of all nodes in the tree. It is an error to make a partial
   620     * iteration of a tree.
   621     */
   622    static class AvlIterator<K, V> {
   623      /** This stack is a singly linked list, linked by the 'parent' field. */
   624      private Node<K, V> stackTop;
   625  
   626      void reset(Node<K, V> root) {
   627        Node<K, V> stackTop = null;
   628        for (Node<K, V> n = root; n != null; n = n.left) {
   629          n.parent = stackTop;
   630          stackTop = n; // Stack push.
   631        }
   632        this.stackTop = stackTop;
   633      }
   634  
   635      public Node<K, V> next() {
   636        Node<K, V> stackTop = this.stackTop;
   637        if (stackTop == null) {
   638          return null;
   639        }
   640        Node<K, V> result = stackTop;
   641        stackTop = result.parent;
   642        result.parent = null;
   643        for (Node<K, V> n = result.right; n != null; n = n.left) {
   644          n.parent = stackTop;
   645          stackTop = n; // Stack push.
   646        }
   647        this.stackTop = stackTop;
   648        return result;
   649      }
   650    }
   651  
   652    /**
   653     * Builds AVL trees of a predetermined size by accepting nodes of increasing
   654     * value. To use:
   655     * <ol>
   656     *   <li>Call {@link #reset} to initialize the target size <i>size</i>.
   657     *   <li>Call {@link #add} <i>size</i> times with increasing values.
   658     *   <li>Call {@link #root} to get the root of the balanced tree.
   659     * </ol>
   660     *
   661     * <p>The returned tree will satisfy the AVL constraint: for every node
   662     * <i>N</i>, the height of <i>N.left</i> and <i>N.right</i> is different by at
   663     * most 1. It accomplishes this by omitting deepest-level leaf nodes when
   664     * building trees whose size isn't a power of 2 minus 1.
   665     *
   666     * <p>Unlike rebuilding a tree from scratch, this approach requires no value
   667     * comparisons. Using this class to create a tree of size <i>S</i> is
   668     * {@code O(S)}.
   669     */
   670    final static class AvlBuilder<K, V> {
   671      /** This stack is a singly linked list, linked by the 'parent' field. */
   672      private Node<K, V> stack;
   673      private int leavesToSkip;
   674      private int leavesSkipped;
   675      private int size;
   676  
   677      void reset(int targetSize) {
   678        // compute the target tree size. This is a power of 2 minus one, like 15 or 31.
   679        int treeCapacity = Integer.highestOneBit(targetSize) * 2 - 1;
   680        leavesToSkip = treeCapacity - targetSize;
   681        size = 0;
   682        leavesSkipped = 0;
   683        stack = null;
   684      }
   685  
   686      void add(Node<K, V> node) {
   687        node.left = node.parent = node.right = null;
   688        node.height = 1;
   689  
   690        // Skip a leaf if necessary.
   691        if (leavesToSkip > 0 && (size & 1) == 0) {
   692          size++;
   693          leavesToSkip--;
   694          leavesSkipped++;
   695        }
   696  
   697        node.parent = stack;
   698        stack = node; // Stack push.
   699        size++;
   700  
   701        // Skip a leaf if necessary.
   702        if (leavesToSkip > 0 && (size & 1) == 0) {
   703          size++;
   704          leavesToSkip--;
   705          leavesSkipped++;
   706        }
   707  
   708        /*
   709         * Combine 3 nodes into subtrees whenever the size is one less than a
   710         * multiple of 4. For example we combine the nodes A, B, C into a
   711         * 3-element tree with B as the root.
   712         *
   713         * Combine two subtrees and a spare single value whenever the size is one
   714         * less than a multiple of 8. For example at 8 we may combine subtrees
   715         * (A B C) and (E F G) with D as the root to form ((A B C) D (E F G)).
   716         *
   717         * Just as we combine single nodes when size nears a multiple of 4, and
   718         * 3-element trees when size nears a multiple of 8, we combine subtrees of
   719         * size (N-1) whenever the total size is 2N-1 whenever N is a power of 2.
   720         */
   721        for (int scale = 4; (size & scale - 1) == scale - 1; scale *= 2) {
   722          if (leavesSkipped == 0) {
   723            // Pop right, center and left, then make center the top of the stack.
   724            Node<K, V> right = stack;
   725            Node<K, V> center = right.parent;
   726            Node<K, V> left = center.parent;
   727            center.parent = left.parent;
   728            stack = center;
   729            // Construct a tree.
   730            center.left = left;
   731            center.right = right;
   732            center.height = right.height + 1;
   733            left.parent = center;
   734            right.parent = center;
   735          } else if (leavesSkipped == 1) {
   736            // Pop right and center, then make center the top of the stack.
   737            Node<K, V> right = stack;
   738            Node<K, V> center = right.parent;
   739            stack = center;
   740            // Construct a tree with no left child.
   741            center.right = right;
   742            center.height = right.height + 1;
   743            right.parent = center;
   744            leavesSkipped = 0;
   745          } else if (leavesSkipped == 2) {
   746            leavesSkipped = 0;
   747          }
   748        }
   749      }
   750  
   751      Node<K, V> root() {
   752        Node<K, V> stackTop = this.stack;
   753        if (stackTop.parent != null) {
   754          throw new IllegalStateException();
   755        }
   756        return stackTop;
   757      }
   758    }
   759  
   760    private abstract class LinkedTreeMapIterator<T> implements Iterator<T> {
   761      Node<K, V> next = header.next;
   762      Node<K, V> lastReturned = null;
   763      int expectedModCount = modCount;
   764  
   765      public final boolean hasNext() {
   766        return next != header;
   767      }
   768  
   769      final Node<K, V> nextNode() {
   770        Node<K, V> e = next;
   771        if (e == header) {
   772          throw new NoSuchElementException();
   773        }
   774        if (modCount != expectedModCount) {
   775          throw new ConcurrentModificationException();
   776        }
   777        next = e.next;
   778        return lastReturned = e;
   779      }
   780  
   781      public final void remove() {
   782        if (lastReturned == null) {
   783          throw new IllegalStateException();
   784        }
   785        removeInternal(lastReturned, true);
   786        lastReturned = null;
   787        expectedModCount = modCount;
   788      }
   789    }
   790  
   791    final class EntrySet extends AbstractSet<Entry<K, V>> {
   792      @Override public int size() {
   793        return size;
   794      }
   795  
   796      @Override public Iterator<Entry<K, V>> iterator() {
   797        return new LinkedTreeMapIterator<Entry<K, V>>() {
   798          public Entry<K, V> next() {
   799            return nextNode();
   800          }
   801        };
   802      }
   803  
   804      @Override public boolean contains(Object o) {
   805        return o instanceof Entry && findByEntry((Entry<?, ?>) o) != null;
   806      }
   807  
   808      @Override public boolean remove(Object o) {
   809        if (!(o instanceof Entry)) {
   810          return false;
   811        }
   812  
   813        Node<K, V> node = findByEntry((Entry<?, ?>) o);
   814        if (node == null) {
   815          return false;
   816        }
   817        removeInternal(node, true);
   818        return true;
   819      }
   820  
   821      @Override public void clear() {
   822        LinkedHashTreeMap.this.clear();
   823      }
   824    }
   825  
   826    final class KeySet extends AbstractSet<K> {
   827      @Override public int size() {
   828        return size;
   829      }
   830  
   831      @Override public Iterator<K> iterator() {
   832        return new LinkedTreeMapIterator<K>() {
   833          public K next() {
   834            return nextNode().key;
   835          }
   836        };
   837      }
   838  
   839      @Override public boolean contains(Object o) {
   840        return containsKey(o);
   841      }
   842  
   843      @Override public boolean remove(Object key) {
   844        return removeInternalByKey(key) != null;
   845      }
   846  
   847      @Override public void clear() {
   848        LinkedHashTreeMap.this.clear();
   849      }
   850    }
   851  
   852    /**
   853     * If somebody is unlucky enough to have to serialize one of these, serialize
   854     * it as a LinkedHashMap so that they won't need Gson on the other side to
   855     * deserialize it. Using serialization defeats our DoS defence, so most apps
   856     * shouldn't use it.
   857     */
   858    private Object writeReplace() throws ObjectStreamException {
   859      return new LinkedHashMap<K, V>(this);
   860    }
   861  }