001: /*
002: * Fast Infoset ver. 0.1 software ("Software")
003: *
004: * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * Software is licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License. You may
008: * obtain a copy of the License at:
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations.
016: *
017: * Sun supports and benefits from the global community of open source
018: * developers, and thanks the community for its important contributions and
019: * open standards-based technology, which Sun has adopted into many of its
020: * products.
021: *
022: * Please note that portions of Software may be provided with notices and
023: * open source licenses from such communities and third parties that govern the
024: * use of those portions, and any licenses granted hereunder do not alter any
025: * rights and obligations you may have under such open source licenses,
026: * however, the disclaimer of warranty and limitation of liability provisions
027: * in this License will apply to all Software in this distribution.
028: *
029: * You acknowledge that the Software is not designed, licensed or intended
030: * for use in the design, construction, operation or maintenance of any nuclear
031: * facility.
032: *
033: * Apache License
034: * Version 2.0, January 2004
035: * http://www.apache.org/licenses/
036: *
037: */
038:
039: package com.sun.xml.fastinfoset.util;
040:
041: import com.sun.xml.fastinfoset.CommonResourceBundle;
042:
043: public abstract class KeyIntMap {
044: public static final int NOT_PRESENT = -1;
045:
046: /**
047: * The default initial capacity - MUST be a power of two.
048: */
049: static final int DEFAULT_INITIAL_CAPACITY = 16;
050:
051: /**
052: * The maximum capacity, used if a higher value is implicitly specified
053: * by either of the constructors with arguments.
054: * MUST be a power of two <= 1<<30.
055: */
056: static final int MAXIMUM_CAPACITY = 1 << 20;
057:
058: /**
059: * The load factor used when none specified in constructor.
060: **/
061: static final float DEFAULT_LOAD_FACTOR = 0.75f;
062:
063: int _readOnlyMapSize;
064:
065: /**
066: * The number of key-value mappings contained in this identity hash map.
067: */
068: int _size;
069:
070: int _capacity;
071:
072: /**
073: * The next size value at which to resize (capacity * load factor).
074: */
075: int _threshold;
076:
077: /**
078: * The load factor for the hash table.
079: */
080: final float _loadFactor;
081:
082: static class BaseEntry {
083: final int _hash;
084: final int _value;
085:
086: public BaseEntry(int hash, int value) {
087: _hash = hash;
088: _value = value;
089: }
090: }
091:
092: public KeyIntMap(int initialCapacity, float loadFactor) {
093: if (initialCapacity < 0)
094: throw new IllegalArgumentException(CommonResourceBundle
095: .getInstance().getString(
096: "message.illegalInitialCapacity",
097: new Object[] { Integer
098: .valueOf(initialCapacity) }));
099: if (initialCapacity > MAXIMUM_CAPACITY)
100: initialCapacity = MAXIMUM_CAPACITY;
101: if (loadFactor <= 0 || Float.isNaN(loadFactor))
102: throw new IllegalArgumentException(CommonResourceBundle
103: .getInstance().getString(
104: "message.illegalLoadFactor",
105: new Object[] { Float.valueOf(loadFactor) }));
106:
107: // Find a power of 2 >= initialCapacity
108: if (initialCapacity != DEFAULT_INITIAL_CAPACITY) {
109: _capacity = 1;
110: while (_capacity < initialCapacity)
111: _capacity <<= 1;
112:
113: _loadFactor = loadFactor;
114: _threshold = (int) (_capacity * _loadFactor);
115: } else {
116: _capacity = DEFAULT_INITIAL_CAPACITY;
117: _loadFactor = DEFAULT_LOAD_FACTOR;
118: _threshold = (int) (DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
119: }
120: }
121:
122: public KeyIntMap(int initialCapacity) {
123: this (initialCapacity, DEFAULT_LOAD_FACTOR);
124: }
125:
126: public KeyIntMap() {
127: _capacity = DEFAULT_INITIAL_CAPACITY;
128: _loadFactor = DEFAULT_LOAD_FACTOR;
129: _threshold = (int) (DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
130: }
131:
132: public final int size() {
133: return _size + _readOnlyMapSize;
134: }
135:
136: public abstract void clear();
137:
138: public abstract void setReadOnlyMap(KeyIntMap readOnlyMap,
139: boolean clear);
140:
141: public static final int hashHash(int h) {
142: h += ~(h << 9);
143: h ^= (h >>> 14);
144: h += (h << 4);
145: h ^= (h >>> 10);
146: return h;
147: }
148:
149: public static final int indexFor(int h, int length) {
150: return h & (length - 1);
151: }
152:
153: }
|