1 21 package au.id.jericho.lib.html; 22 23 import java.util.*; 24 25 28 final class IntStringHashMap { 29 private static final int DEFAULT_INITIAL_CAPACITY=15; 30 private static final float DEFAULT_LOAD_FACTOR=0.75f; 31 private transient Entry[] entries; private transient int size; 33 private int threshold; 34 private float loadFactor; 35 private int bitmask; 37 public IntStringHashMap(int initialCapacity, final float loadFactor) { 38 this.loadFactor=loadFactor; 39 int capacity=1; 40 while (capacity<initialCapacity) capacity<<=1; 41 threshold=(int)(capacity*loadFactor); 42 entries=new Entry[capacity]; 43 bitmask=capacity-1; 44 } 45 46 public IntStringHashMap(final int initialCapacity) { 47 this(initialCapacity,DEFAULT_LOAD_FACTOR); 48 } 49 50 public IntStringHashMap() { 51 this(DEFAULT_INITIAL_CAPACITY,DEFAULT_LOAD_FACTOR); 52 } 53 54 public int size() { 55 return size; 56 } 57 58 public boolean isEmpty() { 59 return size==0; 60 } 61 62 private int getIndex(final int key) { 63 return key&bitmask; } 65 66 public String get(final int key) { 67 Entry entry=entries[getIndex(key)]; 68 while (entry!=null) { 69 if (key==entry.key) return entry.value; 70 entry=entry.next; 71 } 72 return null; 73 } 74 75 private Entry getEntry(final int key) { 76 Entry entry=entries[getIndex(key)]; 77 while (entry!=null && key!=entry.key) entry=entry.next; 78 return entry; 79 } 80 81 public boolean containsKey(final int key) { 82 return getEntry(key)!=null; 83 } 84 85 public String put(final int key, final String value) { 86 final int index=getIndex(key); 87 for (Entry entry=entries[index]; entry!= null; entry=entry.next) { 88 if (key==entry.key) { 89 final String oldValue=entry.value; 90 entry.value=value; 91 return oldValue; 92 } 93 } 94 entries[index]=new Entry(key,value,entries[index]); 95 if (size++>=threshold) increaseCapacity(); 96 return null; 97 } 98 99 private void increaseCapacity() { 100 final int oldCapacity=entries.length; 101 final Entry[] oldEntries=entries; 102 entries=new Entry[oldCapacity<<1]; 103 bitmask=entries.length-1; 104 for (int i=0; i<oldCapacity; i++) { 105 Entry entry=oldEntries[i]; 106 while (entry!=null) { 107 final Entry next=entry.next; 108 final int index=getIndex(entry.key); 109 entry.next=entries[index]; 110 entries[index]=entry; 111 entry=next; 112 } 113 } 114 threshold=(int)(entries.length*loadFactor); 115 } 116 117 public String remove(final int key) { 118 final int index=getIndex(key); 119 Entry previous=null; 120 for (Entry entry=entries[index]; entry!=null; entry=(previous=entry).next) { 121 if (key==entry.key) { 122 if (previous==null) 123 entries[index]=entry.next; 124 else 125 previous.next=entry.next; 126 size--; 127 return entry.value; 128 } 129 } 130 return null; 131 } 132 133 public void clear() { 134 for (int i=bitmask; i>=0; i--) entries[i]=null; 135 size=0; 136 } 137 138 public boolean containsValue(final String value) { 139 if (value==null) { 140 for (int i=bitmask; i>=0; i--) 141 for (Entry entry=entries[i]; entry!=null; entry=entry.next) 142 if (entry.value==null) return true; 143 } else { 144 for (int i=bitmask; i>=0; i--) 145 for (Entry entry=entries[i]; entry!=null; entry=entry.next) 146 if (value.equals(entry.value)) return true; 147 } 148 return false; 149 } 150 151 private static final class Entry { 152 final int key; 153 String value; 154 Entry next; 155 156 public Entry(final int key, final String value, final Entry next) { 157 this.key=key; 158 this.value=value; 159 this.next=next; 160 } 161 } 162 } 163 | Popular Tags |