1 package com.icl.saxon.sort; 2 3 6 12 13 public class HashMap { 14 15 String [] strings; 16 int size; 17 18 public HashMap(int size) { 19 strings = new String [size]; 20 this.size = size; 21 } 22 23 public void set(String s) { 24 int hash = (hashCode(s) & 0x7fffffff) % size; 25 while(true) { 26 if (strings[hash]==null) { 27 strings[hash] = s; 28 return; 29 } 30 if (strings[hash].equalsIgnoreCase(s)) { 31 return; 32 } 33 hash = (hash + 1) % size; 34 } 35 } 36 37 public boolean get(String s) { 38 int hash = (hashCode(s) & 0x7fffffff) % size; 39 while(true) { 40 if (strings[hash]==null) { 41 return false; 42 } 43 if (strings[hash].equalsIgnoreCase(s)) { 44 return true; 45 } 46 hash = (hash + 1) % size; 47 } 48 } 49 50 private int hashCode(String s) { 51 int hash = 0; 54 int limit = s.length(); 55 if (limit>24) limit = 24; 56 for (int i=0; i<limit; i++) { 57 hash = (hash<<1) + (s.charAt(i) & 0xdf); 58 } 59 return hash; 60 } 61 } 62 63 | Popular Tags |