View Javadoc
1   /*
2    * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   *
25   * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
26   */
27  
28  package com.sun.xml.internal.fastinfoset.util;
29  import com.sun.xml.internal.fastinfoset.CommonResourceBundle;
30  
31  public abstract class KeyIntMap {
32      public static final int NOT_PRESENT = -1;
33  
34      /**
35       * The default initial capacity - MUST be a power of two.
36       */
37      static final int DEFAULT_INITIAL_CAPACITY = 16;
38  
39      /**
40       * The maximum capacity, used if a higher value is implicitly specified
41       * by either of the constructors with arguments.
42       * MUST be a power of two <= 1<<30.
43       */
44      static final int MAXIMUM_CAPACITY = 1 << 20;
45  
46      /**
47       * The load factor used when none specified in constructor.
48       **/
49      static final float DEFAULT_LOAD_FACTOR = 0.75f;
50  
51      int _readOnlyMapSize;
52  
53      /**
54       * The number of key-value mappings contained in this identity hash map.
55       */
56      int _size;
57  
58      int _capacity;
59  
60      /**
61       * The next size value at which to resize (capacity * load factor).
62       */
63      int _threshold;
64  
65      /**
66       * The load factor for the hash table.
67       */
68      final float _loadFactor;
69  
70      static class BaseEntry {
71          final int _hash;
72          final int _value;
73  
74          public BaseEntry(int hash, int value) {
75              _hash = hash;
76              _value = value;
77          }
78      }
79  
80      public KeyIntMap(int initialCapacity, float loadFactor) {
81          if (initialCapacity < 0)
82              throw new IllegalArgumentException(CommonResourceBundle.getInstance().
83                      getString("message.illegalInitialCapacity", new Object[]{Integer.valueOf(initialCapacity)}));
84          if (initialCapacity > MAXIMUM_CAPACITY)
85              initialCapacity = MAXIMUM_CAPACITY;
86          if (loadFactor <= 0 || Float.isNaN(loadFactor))
87              throw new IllegalArgumentException(CommonResourceBundle.getInstance().
88                      getString("message.illegalLoadFactor", new Object[]{Float.valueOf(loadFactor)}));
89  
90          // Find a power of 2 >= initialCapacity
91          if (initialCapacity != DEFAULT_INITIAL_CAPACITY) {
92              _capacity = 1;
93              while (_capacity < initialCapacity)
94                  _capacity <<= 1;
95  
96              _loadFactor = loadFactor;
97              _threshold = (int)(_capacity * _loadFactor);
98          } else {
99              _capacity = DEFAULT_INITIAL_CAPACITY;
100             _loadFactor = DEFAULT_LOAD_FACTOR;
101             _threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
102         }
103     }
104 
105     public KeyIntMap(int initialCapacity) {
106         this(initialCapacity, DEFAULT_LOAD_FACTOR);
107     }
108 
109     public KeyIntMap() {
110         _capacity = DEFAULT_INITIAL_CAPACITY;
111         _loadFactor = DEFAULT_LOAD_FACTOR;
112         _threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
113     }
114 
115     public final int size() {
116         return _size + _readOnlyMapSize;
117     }
118 
119     public abstract void clear();
120 
121     public abstract void setReadOnlyMap(KeyIntMap readOnlyMap, boolean clear);
122 
123 
124     public static final int hashHash(int h) {
125         h += ~(h << 9);
126         h ^=  (h >>> 14);
127         h +=  (h << 4);
128         h ^=  (h >>> 10);
129         return h;
130     }
131 
132     public static final int indexFor(int h, int length) {
133         return h & (length-1);
134     }
135 
136 }