1 18 package org.apache.batik.css.engine.value; 19 20 27 public class StringMap { 28 29 32 protected final static int INITIAL_CAPACITY = 11; 33 34 37 protected Entry[] table; 38 39 42 protected int count; 43 44 47 public StringMap() { 48 table = new Entry[INITIAL_CAPACITY]; 49 } 50 51 55 public StringMap(StringMap t) { 56 count = t.count; 57 table = new Entry[t.table.length]; 58 for (int i = 0; i < table.length; i++) { 59 Entry e = t.table[i]; 60 Entry n = null; 61 if (e != null) { 62 n = new Entry(e.hash, e.key, e.value, null); 63 table[i] = n; 64 e = e.next; 65 while (e != null) { 66 n.next = new Entry(e.hash, e.key, e.value, null); 67 n = n.next; 68 e = e.next; 69 } 70 } 71 } 72 } 73 74 78 public Object get(String key) { 79 int hash = key.hashCode() & 0x7FFFFFFF; 80 int index = hash % table.length; 81 82 for (Entry e = table[index]; e != null; e = e.next) { 83 if ((e.hash == hash) && e.key == key) { 84 return e.value; 85 } 86 } 87 return null; 88 } 89 90 94 public Object put(String key, Object value) { 95 int hash = key.hashCode() & 0x7FFFFFFF; 96 int index = hash % table.length; 97 98 for (Entry e = table[index]; e != null; e = e.next) { 99 if ((e.hash == hash) && e.key == key) { 100 Object old = e.value; 101 e.value = value; 102 return old; 103 } 104 } 105 106 int len = table.length; 108 if (count++ >= (len * 3) >>> 2) { 109 rehash(); 110 index = hash % table.length; 111 } 112 113 Entry e = new Entry(hash, key, value, table[index]); 114 table[index] = e; 115 return null; 116 } 117 118 121 protected void rehash () { 122 Entry[] oldTable = table; 123 124 table = new Entry[oldTable.length * 2 + 1]; 125 126 for (int i = oldTable.length-1; i >= 0; i--) { 127 for (Entry old = oldTable[i]; old != null;) { 128 Entry e = old; 129 old = old.next; 130 131 int index = e.hash % table.length; 132 e.next = table[index]; 133 table[index] = e; 134 } 135 } 136 } 137 138 141 protected static class Entry { 142 145 public int hash; 146 147 150 public String key; 151 152 155 public Object value; 156 157 160 public Entry next; 161 162 165 public Entry(int hash, String key, Object value, Entry next) { 166 this.hash = hash; 167 this.key = key; 168 this.value = value; 169 this.next = next; 170 } 171 } 172 } 173 | Popular Tags |