本哈希表类以及相关类属于作者智能内核项目的一个组成部分。为了实现计算机智能化, 需要在超巨量的空间内快速地存储、检索、匹配和修改数据,传统的哈希表算法根本不适用, 而且在很多应用场合需要一种按整数方式自然排序的哈希树容器,但可惜数据结构理论和 实践中都没有。因此作者一直在思索构造适用于这一目的的算法和容器。
我相信,人的大脑神经网络中一定存在一种类似的容器,不同的是基于某种多进制分叉运算的, 而不是基于二进制分叉运算的。
此实现版本是基于java语言的,稍后有时间的话,将实现一个c++语言版本的。若有读者花时间 实现了的话,可将之共享出来,请发邮件给我:iamkarl@163.com
以下是Map的put模块代码示例:
V put(K key, V value) {
KeyValue<K, V> pair = new KeyValue<K, V>(key, value);
int code = pair.hashCode();
//System.out.println("put:"+code);
int hash = ((code>>24) & 0xff);
if(table[hash]==null){
table[hash] = pair;
return null;
}else{
if(table[hash] instanceof KeyValue ){
KeyValue<K, V> pair1 = (KeyValue<K, V>) table[hash];
MyMapUnit<K, V> mmu = new MyMapUnit<K, V>();];//新建包含256个插槽的数组对象
mmu.put(pair1, pair1.hashCode(), 1);
mmu.put(pair, code, 1);
table[hash] = mmu;
return null;
}
MyMapUnit<K, V> mmu = (MyMapUnit<K, V>)table[hash];
return mmu.put(pair, code, 1);
}
}
V put(KeyValue<K, V> pair, int code, int index) {
if(index >= LAYER_LIMIT){
return endPut( pair,code);
}
//int hash = toHash(code, index);
int hash = 0;
switch(index){
case 0:hash = ((code>>24) & 0xff);break;
case 1:hash = ((code>>16) & 0xff);break;
case 2:hash = ((code>>8) & 0xff);break;
case 3:hash = (code & 0xff);
}
if(table[hash]==null){
table[hash] = pair;
return null;
}else{
//index++;
if(table[hash] instanceof KeyValue ){
KeyValue<K, V> pair1 = (KeyValue<K, V>) table[hash];
MyMapUnit<K, V> mmu = new MyMapUnit<K, V>();
mmu.put(pair1, pair1.hashCode(), index+1);
table[hash] = mmu;
return mmu.put(pair, code, index+1);
}
//MyMapUnit<K, V> mmu = (MyMapUnit<K, V>)table[hash];
return ( (MyMapUnit<K, V>)table[hash] ).put(pair, code, index+1);
}
}
private V endPut( KeyValue<K, V> pair,int code) {
//int hash = toHash(code, LAYER_LIMIT);
int hash = (code & 0xff);
if(table[hash]==null){
table[hash] = pair;
return null;
}else{
if(table[hash] instanceof KeyValue ){
KeyValue<K, V> pair1 = (KeyValue<K, V>) table[hash];
if (pair.equals(pair1)) {
V oldValue = pair1.value;
table[hash] = pair;
return oldValue;// 已有同key元素,执行更新操作
}
HashMap<String, KeyValue<K, V>> map = new HashMap<String, KeyValue<K, V>>();
map.put(pair1.key.toString(), pair1);
map.put(pair.key.toString(), pair);
table[hash] = map;
return null;
}
conflictCount++;
KeyValue<K, V> obj = ( (HashMap<String, KeyValue<K, V>>) table[hash] ).put(pair.key.toString(), pair);
if(obj==null) return null;
return obj.value;
}
}