模仿st_table写的StTable类

    读ruby hacking guide,其中专门辟了一个章节介绍了st.c中的st_table,这个数据结构也就是类似java中的HashMap,基本原理是利用数组存储,数组的每一个元素是一个单向链表,链表中再存储具体的元素,如下图所示的结构

   ruby中利用这个结构来存储对象变量、类方法、常量、全局变量等信息,在c ruby中,方法、变量都是用一个整型作为键值来存储在st_table中,因此这个数据结构对于以整性为键的map类型来说速度非常不错(我没有测试内存的占用情况)。
源码如下:
java 代码
 
  1. //接口,用于定义hash函数  
  2. //HashFunction.java  
  3. public interface HashFunction<T> {  
  4.    public int hash(T key);  
  5. }  

链表元素类:
java 代码
 
  1. public class StTableEntry<T, V> {  
  2.     protected int hash; //hash值  
  3.   
  4.     protected T key;   //键  
  5.   
  6.     protected V value; //存储值  
  7.   
  8.     protected StTableEntry<T, V> next; //下一节点  
  9.   
  10.     public StTableEntry() {  
  11.   
  12.     }  
  13.   
  14.     public StTableEntry(int hash, T key, V value, StTableEntry<T, V> next) {  
  15.         super();  
  16.         this.hash = hash;  
  17.         this.key = key;  
  18.         this.value = value;  
  19.         this.next = next;  
  20.     }  
  21.   
  22.     public int getHash() {  
  23.         return hash;  
  24.     }  
  25.   
  26.     public void setHash(int hash) {  
  27.         this.hash = hash;  
  28.     }  
  29.   
  30.     public T getKey() {  
  31.         return key;  
  32.     }  
  33.   
  34.     public void setKey(T key) {  
  35.         this.key = key;  
  36.     }  
  37.   
  38.     public StTableEntry<T, V> getNext() {  
  39.         return next;  
  40.     }  
  41.   
  42.     public void setNext(StTableEntry<T, V> next) {  
  43.         this.next = next;  
  44.     }  
  45.   
  46.     public V getValue() {  
  47.         return value;  
  48.     }  
  49.   
  50.     public void setValue(V value) {  
  51.         this.value = value;  
  52.     }  
  53.   
  54. }  

完整的StTable实现,没有实现remove,有兴趣的话自己实现一下:
java 代码
 
  1. public final class StTable<T, V> {  
  2.     private HashFunction<T> hashFunction;  
  3.   
  4.     private int num_bins;  
  5.   
  6.     int num_entries;  
  7.   
  8.     StTableEntry<T, V>[] bins;  
  9.   
  10.     public static int DEFAULT_SIZE = 10;  
  11.   
  12.     private static int DEFAULT_MAX_DENSITY = 5;  
  13.   
  14.     private static int DEFAULT_MIN_SIZE = 8;  
  15.   
  16.     private static long primes[] = { 8 + 316 + 332 + 564 + 3128 + 3,  
  17.             256 + 27512 + 91024 + 92048 + 54096 + 38192 + 27,  
  18.             16384 + 4332768 + 365536 + 45131072 + 29262144 + 3,  
  19.             524288 + 211048576 + 72097152 + 174194304 + 158388608 + 9,  
  20.             16777216 + 4333554432 + 3567108864 + 15134217728 + 29,  
  21.             268435456 + 3536870912 + 111073741824 + 850 };  
  22.   
  23.     public StTable(HashFunction<T> hashFunction) {  
  24.         this.hashFunction = hashFunction;  
  25.         this.num_bins = DEFAULT_SIZE;  
  26.         this.num_entries = 0;  
  27.         this.bins = (StTableEntry<T, V>[]) new StTableEntry[this.num_bins];  
  28.     }  
  29.   
  30.     public StTable(HashFunction<T> hashFunction, int size) {  
  31.         this.hashFunction = hashFunction;  
  32.         if (size == 0)  
  33.             throw new IllegalArgumentException(  
  34.                     "The size could not less than zero:" + size);  
  35.         this.num_bins = size;  
  36.         this.num_entries = 0;  
  37.         this.bins = (StTableEntry<T, V>[]) new StTableEntry[this.num_bins];  
  38.     }  
  39.   
  40.     private long newSize(int size) {  
  41.   
  42.         for (int i = 0, newsize = DEFAULT_MIN_SIZE; i < primes.length; i++, newsize <<= 1) {  
  43.             if (newsize > size)  
  44.                 return primes[i];  
  45.         }  
  46.         /* Ran out of polynomials */  
  47.         return -1/* should raise exception */  
  48.     }  
  49.   
  50.     public V get(T key) {  
  51.         int hash_val = doHash(key);  
  52.         StTableEntry<T, V> entry = findEntry(hash_val, key);  
  53.         if (entry == null)  
  54.             return null;  
  55.         else  
  56.             return entry.getValue();  
  57.     }  
  58.   
  59.     public V put(T key, V value) {  
  60.         int hash_val = doHash(key);  
  61.         StTableEntry<T, V> entry = findEntry(hash_val, key);  
  62.         if (entry == null) {  
  63.             // 未有键值,直接添加  
  64.             addDirect(key, value);  
  65.             return value;  
  66.         } else {  
  67.             V v = entry.value;  
  68.             entry.value = value;  
  69.             return v;  
  70.         }  
  71.     }  
  72.   
  73.     // hash函数,调用hashFunction的hash方法  
  74.     private int doHash(T key) {  
  75.         if (hashFunction.hash(key) < 0)  
  76.             throw new IllegalArgumentException(  
  77.                     "hash value could not less than zero:"  
  78.                             + hashFunction.hash(key));  
  79.         return hashFunction.hash(key);  
  80.     }  
  81.   
  82.     // 过于拥挤,重新分布  
  83.     public void reHash() {  
  84.         int new_size = (int) newSize(this.num_bins);  
  85.         StTableEntry<T, V>[] new_bins = (StTableEntry<T, V>[]) new StTableEntry[new_size];  
  86.         for (int i = 0; i < this.num_bins; i++) {  
  87.             StTableEntry<T, V> entry = this.bins[i];  
  88.             while (entry != null) {  
  89.                 StTableEntry<T, V> next = entry.next;  
  90.                 int hash_val = entry.hash % new_size;  
  91.                 entry.next = new_bins[hash_val];  
  92.                 new_bins[hash_val] = entry;  
  93.                 entry = next;  
  94.             }  
  95.         }  
  96.         this.bins = null//gc  
  97.         this.num_bins = new_size;  
  98.         this.bins = new_bins;  
  99.   
  100.     }  
  101.   
  102.     private void addDirect(T key, V value) {  
  103.         int hash_val = doHash(key);  
  104.         int bin_pos = hash_val % this.num_bins;  
  105.         if ((this.num_entries / this.num_bins) > DEFAULT_MAX_DENSITY) {  
  106.             reHash();  
  107.             bin_pos = hash_val % this.num_bins;  
  108.         }  
  109.         StTableEntry<T, V> entry = new StTableEntry<T, V>();  
  110.         entry.setHash(hash_val);  
  111.         entry.setKey(key);  
  112.         entry.setValue(value);  
  113.         entry.setNext(this.bins[bin_pos]);  
  114.         this.bins[bin_pos] = entry;  
  115.         this.num_entries++;  
  116.     }  
  117.   
  118.     private StTableEntry<T, V> findEntry(int hash_val, T key) {  
  119.         int bin_pos = hash_val % this.num_bins;  
  120.         StTableEntry<T, V> entry = this.bins[bin_pos];  
  121.         if (entryNotEqual(entry, key, hash_val)) {  
  122.             entry = entry.next;  
  123.             while (entryNotEqual(entry, key, hash_val)) {  
  124.                 entry = entry.next;  
  125.             }  
  126.         }  
  127.         return entry;  
  128.     }  
  129.   
  130.     // 判断元素是否相同  
  131.     private boolean entryNotEqual(StTableEntry<T, V> entry, T key, int hash_val) {  
  132.         return entry != null  
  133.                 && (entry.getHash() != hash_val || (!key.equals(entry.getKey())));  
  134.     }  
  135.   
  136. }  

   单元测试类就不列了,给一个与HashMap的简单性能对比,以整型为键,显然StTable快多了,对于字符串型,关键是HashFunction的定义,我直接调用String的hashCode方法,不知道有没有其他更好的方法让元素分布的更均匀些:
java 代码
 
  1. import java.util.HashMap;  
  2. import java.util.Map;  
  3.   
  4. public class Benchmark {  
  5.     public static void main(String args[]) {  
  6.         long map_cost = testStringMap();  
  7.         long table_cost = testStringTable();  
  8.         if (map_cost <= table_cost)  
  9.             System.out.println("map is faster than table ");  
  10.         else  
  11.             System.out.println("table is faster than map ");  
  12.   
  13.         map_cost = testIntegerMap();  
  14.         table_cost = testIntegerTable();  
  15.         if (map_cost <= table_cost)  
  16.             System.out.println("map is faster than table ");  
  17.         else  
  18.             System.out.println("table is faster than map ");  
  19.     }  
  20.   
  21.     public static long testIntegerMap() {  
  22.         Map<Integer, Integer> map = new HashMap<Integer, Integer>();  
  23.         long start = System.nanoTime();  
  24.         for (int i = 0; i < 10000; i++)  
  25.             map.put(i, i);  
  26.         long result = 0;  
  27.         for (int i = 0; i < 10000; i++)  
  28.             result += map.get(i);  
  29.         long end = System.nanoTime();  
  30.         System.out.println("result:" + result);  
  31.         System.out.println("map:" + (end - start));  
  32.         return (end - start);  
  33.     }  
  34.   
  35.     public static long testIntegerTable() {  
  36.         HashFunction<Integer> intHash = new HashFunction<Integer>() {  
  37.             public int hash(Integer key) {  
  38.                 return key;  
  39.             }  
  40.         };  
  41.         StTable<Integer, Integer> table = new StTable<Integer, Integer>(intHash);  
  42.         long start = System.nanoTime();  
  43.         for (int i = 0; i < 10000; i++)  
  44.             table.put(i, i);  
  45.         long result = 0;  
  46.         for (int i = 0; i < 10000; i++)  
  47.             result += table.get(i);  
  48.         long end = System.nanoTime();  
  49.         System.out.println("result:" + result);  
  50.         System.out.println("table:" + (end - start));  
  51.         return (end - start);  
  52.     }  
  53.   
  54.     public static long testStringMap() {  
  55.         Map<String, String> map = new HashMap<String, String>();  
  56.         long start = System.nanoTime();  
  57.         for (int i = 0; i < 10000; i++)  
  58.             map.put(String.valueOf(i), String.valueOf(i));  
  59.         long result = 0;  
  60.         for (int i = 0; i < 10000; i++)  
  61.             result += Integer.parseInt(map.get(String.valueOf(i)));  
  62.         long end = System.nanoTime();  
  63.         System.out.println("result:" + result);  
  64.         System.out.println("map:" + (end - start));  
  65.         return (end - start);  
  66.     }  
  67.   
  68.     public static long testStringTable() {  
  69.         HashFunction<String> intHash = new HashFunction<String>() {  
  70.             int i = 0;  
  71.             public int hash(String key) {  
  72.                 int hashCode = key.hashCode();  
  73.                 return hashCode < 0 ? -hashCode : hashCode;  
  74.             }  
  75.         };  
  76.         StTable<String, String> table = new StTable<String, String>(intHash);  
  77.         long start = System.nanoTime();  
  78.         for (int i = 0; i < 10000; i++)  
  79.             table.put(String.valueOf(i), String.valueOf(i));  
  80.         long result = 0;  
  81.         for (int i = 0; i < 10000; i++)  
  82.             result += Integer.parseInt(table.get(String.valueOf(i)));  
  83.         long end = System.nanoTime();  
  84.         System.out.println("result:" + result);  
  85.         System.out.println("table:" + (end - start));  
  86.         return (end - start);  
  87.     }  
  88.   
  89. }  

结果为:
result:49995000
map:55501468
result:49995000
table:60999652
map is faster than table

 
result:49995000
map:44634444
result:49995000
table:26209477
table is faster than map



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值