65087e0b8ffddf169f4dc0f19009c0226d5eacec
[IRC.git] / Robust / src / Benchmarks / SingleTM / Genome / Hashtable.java
1 public class Hashtable {
2     List buckets[];
3     int numBucket;
4     int size;
5     int resizeRatio;
6     int growthFactor;
7     
8     public Hashtable (int initNumBucket, int resizeRatio, int growthFactor) {
9       allocBuckets(initNumBucket);
10       numBucket = initNumBucket;
11       size = 0;
12       resizeRatio = ((resizeRatio < 0) ? 3 : resizeRatio);
13       growthFactor = ((growthFactor < 0) ? 3 : growthFactor);
14     }
15     
16     public boolean TMhashtable_insert (ByteString keyPtr, ByteString dataPtr) {
17       int i = keyPtr.hashCode() % numBucket;
18
19       Pair findPair = new Pair();
20       findPair.firstPtr = keyPtr;
21       Pair pairPtr = buckets[(int)i].find(findPair);
22       if (pairPtr != null) {
23           return false;
24       }
25
26       Pair insertPtr = new Pair(keyPtr, dataPtr);
27
28       /* Add new entry  */
29       if (buckets[(int)i].insert(insertPtr) == false) {
30           return false;
31       }
32
33       size++;
34
35       return true;
36     }
37     
38     void allocBuckets (int numBucket) {
39       int i;
40       /* Allocate bucket: extra bucket is dummy for easier iterator code */
41       buckets = new List[numBucket+1];
42       
43       for (i = 0; i < (numBucket + 1); i++) {
44           List chainPtr = new List();
45           buckets[(int)i] = chainPtr;
46       }
47     }
48     
49     int hashSegment (ByteString str) {
50       int hash = 0;
51
52       int index = 0;
53       /* Note: Do not change this hashing scheme */
54       for(index = 0; index < str.length(); index++) {
55         char c = str.byteAt(index);
56         hash = c + (hash << 6) + (hash << 16) - hash;
57       }
58   
59       if(hash < 0) hash *= -1;
60
61       return hash;
62     }
63 }