1 18 package org.apache.batik.css.engine; 19 20 27 public class StringIntMap { 28 29 32 protected Entry[] table; 33 34 37 protected int count; 38 39 43 public StringIntMap(int c) { 44 table = new Entry[((c * 3) >>> 2) + 1]; 45 } 46 47 51 public int get(String key) { 52 int hash = key.hashCode() & 0x7FFFFFFF; 53 int index = hash % table.length; 54 55 for (Entry e = table[index]; e != null; e = e.next) { 56 if ((e.hash == hash) && e.key.equals(key)) { 57 return e.value; 58 } 59 } 60 return -1; 61 } 62 63 66 public void put(String key, int value) { 67 int hash = key.hashCode() & 0x7FFFFFFF; 68 int index = hash % table.length; 69 70 for (Entry e = table[index]; e != null; e = e.next) { 71 if ((e.hash == hash) && e.key.equals(key)) { 72 e.value = value; 73 return; 74 } 75 } 76 77 int len = table.length; 79 if (count++ >= (len * 3) >>> 2) { 80 rehash(); 81 index = hash % table.length; 82 } 83 84 Entry e = new Entry(hash, key, value, table[index]); 85 table[index] = e; 86 } 87 88 91 protected void rehash () { 92 Entry[] oldTable = table; 93 94 table = new Entry[oldTable.length * 2 + 1]; 95 96 for (int i = oldTable.length-1; i >= 0; i--) { 97 for (Entry old = oldTable[i]; old != null;) { 98 Entry e = old; 99 old = old.next; 100 101 int index = e.hash % table.length; 102 e.next = table[index]; 103 table[index] = e; 104 } 105 } 106 } 107 108 111 protected static class Entry { 112 115 public int hash; 116 117 120 public String key; 121 122 125 public int value; 126 127 130 public Entry next; 131 132 135 public Entry(int hash, String key, int value, Entry next) { 136 this.hash = hash; 137 this.key = key; 138 this.value = value; 139 this.next = next; 140 } 141 } 142 } 143 | Popular Tags |