- 浏览: 2551363 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Interview(7)Map and Hash Table
Map - (key, value)
java.util.Map interface
public class MapDLNode implements Map {
private List list;
private EqualityTester tester;
public MapDLNode(){ this(new EqualityTesterDefault()); }
public MapDLNode(EqualityTester tester){ this(list = new ListDLNode(); tester = tester); }
public int getSize() { return list.getSize(); }
public boolean isEmpty() { return list.isEmptyy(); }
public Object get(Object key){
Iterator p = list.positions();
while(p.hasNext()){
Position pos = (Position) p.getNext();
Entry entry = (EntryDefault) pos.getElem();
if( tester.isEqualTo(entry.getKey(), key)){
return entry.getValue();
}
}
return null;
}
public Object put(Object key, Object value){
Iterator p = list.positions();
while (p.hasNext()){ //loop all the elements
Position pos = (Position) p.getNext();
Entry entry = (EntryDefault) pos.getElem();
if( tester.isEqualTo(entry.getKey(), key)){
Object oldValue = entry.getValue();
list.replace(pos, new EntryDefault(key, value));
return oldValue;
}
}
list.insertFirst(new EntryDefault(key, value));
return null;
}
public Object remove(Object key){
Iterator p = list.positions();
while (p.hasNext()){
Position pos = (Position) p.getNext();
Entry entry = (EntryDefault) pos.getElem();
if(tester.isEqualTo(entry.getKey, key)){
Object oldValue = entry.getValue();
list.remove(pos);
return oldValue;
}
}
return null;
}
public Iterator entries() { return new IteratorElement(list); }
}
get(key), put(key, value), remove(key) - O(n), system will scan all the elements in these methods.
Hash Table
Bucket Array, array[0.. N-1]
Convert the key to 0 - N-1 ——> Hash Function
Hash Code —> hashCode() —> 32 int
static int hashCode(long l){
return (int) ((l>>32) + (int)l; )
}
Convert 2^32 = 4G = 0..N-1
|i| mod N = 0 .. N-1
MAD - Multiply Add Divide | a x i + b| mod N
No matter how we improve the Hash Function or Solution, we still have the conflicts.
Solution to the Conflict - Separate Chaining
All the Value are List similar to below, or empty, or single value
Solution to the Conflict - Collision Pool
Two Bucket, one for normal items, one for conflict items
public class MapHashTable implements Map {
private Map[] a; // bucket array
private int n;
private final double maxLemda = 0.75;
private int size;
private EqualityTester t;
public MapHashTable(){
this(0, new EqualityTesterDefault());
}
public MapHashTable(int n, EqualityTester t){
t = t;
n = p(n); //less than n
a = new Map[n];
for(int i = 0;i< n;i++){
a[i] = new MapDLNode(t); //use MapDLNode for in bucket array
}
size = 0;
}
private int h(Object key) { return key.hashCode() % N; } //hash code the key and mod N
private static boolean prime(int n){ ..snip… } //check prime number
private static int p(int n) { …snip.. } //return the prime number bigger than n
public int getSize() { return size; }
public boolean isEmpty(){ return 0 == size; }
public Object get(Object key){
return a[h(key)].get(key);
}
public Object put(Object key, Object value) {
Object oldValue = A[h(key)].put(key, value);
if(null == oldValue){
size++; //new item, update size
if(size > N * maxLemda){
rehash();
}
}
return oldValue;
}
public Object remove(Object key) {
Object oldValue = A[h(key)].remove(key);
if( null != oldValue) { size- -; }
return oldValue;
}
public Iterator entries(){ //combine all the entries together
List l = new ListDLNode();
for(int i = 0;i<N;i++){
Iterator it = a[i].entries();
while(it.hasNext()){
l.insertLast(it.getNext());
}
}
return new InteratorElement(l);
}
private void rehash(){
Iterator it = this.entries();
N = p(N<<1);
A = new Map[N]; //double the N
for(int i = 0;i<N;i++){
A[i] = new MapDLNode(T);
}
while(it.hasNext()) {
Entry e = (Entry)it.getNext();
Object k = e.getKey();
Object v = e.getValue();
a[h(k)].put(k, v); //re-insert everything
}
}
}
Dictionary
References:
Map - (key, value)
java.util.Map interface
public class MapDLNode implements Map {
private List list;
private EqualityTester tester;
public MapDLNode(){ this(new EqualityTesterDefault()); }
public MapDLNode(EqualityTester tester){ this(list = new ListDLNode(); tester = tester); }
public int getSize() { return list.getSize(); }
public boolean isEmpty() { return list.isEmptyy(); }
public Object get(Object key){
Iterator p = list.positions();
while(p.hasNext()){
Position pos = (Position) p.getNext();
Entry entry = (EntryDefault) pos.getElem();
if( tester.isEqualTo(entry.getKey(), key)){
return entry.getValue();
}
}
return null;
}
public Object put(Object key, Object value){
Iterator p = list.positions();
while (p.hasNext()){ //loop all the elements
Position pos = (Position) p.getNext();
Entry entry = (EntryDefault) pos.getElem();
if( tester.isEqualTo(entry.getKey(), key)){
Object oldValue = entry.getValue();
list.replace(pos, new EntryDefault(key, value));
return oldValue;
}
}
list.insertFirst(new EntryDefault(key, value));
return null;
}
public Object remove(Object key){
Iterator p = list.positions();
while (p.hasNext()){
Position pos = (Position) p.getNext();
Entry entry = (EntryDefault) pos.getElem();
if(tester.isEqualTo(entry.getKey, key)){
Object oldValue = entry.getValue();
list.remove(pos);
return oldValue;
}
}
return null;
}
public Iterator entries() { return new IteratorElement(list); }
}
get(key), put(key, value), remove(key) - O(n), system will scan all the elements in these methods.
Hash Table
Bucket Array, array[0.. N-1]
Convert the key to 0 - N-1 ——> Hash Function
Hash Code —> hashCode() —> 32 int
static int hashCode(long l){
return (int) ((l>>32) + (int)l; )
}
Convert 2^32 = 4G = 0..N-1
|i| mod N = 0 .. N-1
MAD - Multiply Add Divide | a x i + b| mod N
No matter how we improve the Hash Function or Solution, we still have the conflicts.
Solution to the Conflict - Separate Chaining
All the Value are List similar to below, or empty, or single value
Solution to the Conflict - Collision Pool
Two Bucket, one for normal items, one for conflict items
public class MapHashTable implements Map {
private Map[] a; // bucket array
private int n;
private final double maxLemda = 0.75;
private int size;
private EqualityTester t;
public MapHashTable(){
this(0, new EqualityTesterDefault());
}
public MapHashTable(int n, EqualityTester t){
t = t;
n = p(n); //less than n
a = new Map[n];
for(int i = 0;i< n;i++){
a[i] = new MapDLNode(t); //use MapDLNode for in bucket array
}
size = 0;
}
private int h(Object key) { return key.hashCode() % N; } //hash code the key and mod N
private static boolean prime(int n){ ..snip… } //check prime number
private static int p(int n) { …snip.. } //return the prime number bigger than n
public int getSize() { return size; }
public boolean isEmpty(){ return 0 == size; }
public Object get(Object key){
return a[h(key)].get(key);
}
public Object put(Object key, Object value) {
Object oldValue = A[h(key)].put(key, value);
if(null == oldValue){
size++; //new item, update size
if(size > N * maxLemda){
rehash();
}
}
return oldValue;
}
public Object remove(Object key) {
Object oldValue = A[h(key)].remove(key);
if( null != oldValue) { size- -; }
return oldValue;
}
public Iterator entries(){ //combine all the entries together
List l = new ListDLNode();
for(int i = 0;i<N;i++){
Iterator it = a[i].entries();
while(it.hasNext()){
l.insertLast(it.getNext());
}
}
return new InteratorElement(l);
}
private void rehash(){
Iterator it = this.entries();
N = p(N<<1);
A = new Map[N]; //double the N
for(int i = 0;i<N;i++){
A[i] = new MapDLNode(T);
}
while(it.hasNext()) {
Entry e = (Entry)it.getNext();
Object k = e.getKey();
Object v = e.getValue();
a[h(k)].put(k, v); //re-insert everything
}
}
}
Dictionary
References:
发表评论
-
Update Site will come soon
2021-06-02 04:10 1677I am still keep notes my tech n ... -
Stop Update Here
2020-04-28 09:00 315I will stop update here, and mo ... -
NodeJS12 and Zlib
2020-04-01 07:44 475NodeJS12 and Zlib It works as ... -
Docker Swarm 2020(2)Docker Swarm and Portainer
2020-03-31 23:18 367Docker Swarm 2020(2)Docker Swar ... -
Docker Swarm 2020(1)Simply Install and Use Swarm
2020-03-31 07:58 368Docker Swarm 2020(1)Simply Inst ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 336Traefik 2020(1)Introduction and ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 430Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 435Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 373Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 454VPN Server 2020(2)Docker on Cen ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 384Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 477NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 422Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 337Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 246GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 450GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 326GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 313Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 317Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 292Serverless with NodeJS and Tenc ...
相关推荐
代码重点是hash_table,附加std::map与其做对比,实现的是一条sql语句:select c_nationkey, c_mktsegment, count(*), max(c_acctbal) from aaa_customer_1g group by c_nationkey, c_mktsegment order by c_...
Search function: This function searches the key specified as the parameter in the hash table. If exist, return true, else return false. Note to use the eq which is a member of HashSet to compare ...
Sorting Hash Table Increase Sorting Searching Elementd in hash table BSTree
hash_map
本文档收集了前端大厂最新面试题的 Hash Table 相关知识点,涵盖 Hash Table 的基本概念、 ハッシュ関数、 Collision 解决方法、Hash Table 的实现和应用等方面。 一、Hash Table 基本概念 * Hash Table 是一种...
这就引出了哈希表(hash table)的概念——一种能够实现近似常数时间复杂度O(1)操作的数据结构。在C++ STL中,`hash_map`正是用来实现这种高效键值对存储的数据结构。 #### 1. 数据结构:hash_map原理 `hash_map`的...
方便地查看文件的MD5/SHA1值 – HashTab给文件属性窗口添加校验值!
"FPGA-based hash table"是指利用FPGA的特性实现的哈希表数据结构。哈希表是一种高效的数据存储和检索结构,它通过将关键字映射到数组的特定位置来快速查找、插入和删除数据。 哈希表的核心是哈希函数,它将输入的...
根据提供的文件信息,我们可以深入分析C语言中哈希表(Hash Table)的实现方式与具体细节。本篇文章将从以下几个方面展开讨论: 1. **哈希表的基本概念** 2. **哈希函数的设计** 3. **哈希表的创建与管理** 4. **...
标题中的"a-similar-map-simple-hash-table.rar_Table"指的是一个使用C语言实现的类似于地图的数据结构,这个数据结构是一个简单的哈希表。哈希表是一种高效的数据存储方式,它通过哈希函数将键(key)映射到数组的...
7. **哈希表相关概念**: - 映射是一种关联数据结构,将键(key)映射到值(value)。 - 哈希表是一种实现映射的高效结构,通过哈希函数将键转换为哈希码,再用桶数组存储。 - 桶数组是一系列固定大小的数组,...
在IT领域,哈希表(Hash Table)是一种高效的数据结构,用于存储和检索键值对。标题中的"u_hash_table.rar_Table For Two"暗示我们关注的是一个特定的哈希表实现,可能是为处理两个相关对象设计的。描述中的"compare...
this is a hash table using hash function
7. WeakHashMap WeakHashMap是一种特殊的Map实现,它使用弱引用作为键,当键不再被引用时,即使在Map中,也会被垃圾回收器清除,从而释放内存资源。 总结来说,选择哪种容器取决于具体的需求:如果需要有序的元素...
哈希表(Hash Table)是一种数据结构,它通过关联键(Key)与值(Value)实现高效的数据存储和检索。在“所有哈希ids_hash_Table_”这个主题中,我们聚焦于利用哈希表来创建作弊表,这可能是为了在游戏中快速查找...
哈希映射(Hash Map)是一种常见的数据结构,它提供了键值对(Key-Value Pair)的快速存储和检索功能。在C++中,STL(Standard Template Library)提供了一个名为`std::unordered_map`的容器,它是基于哈希表实现的...
7. **其他操作**:`hash_map`还提供了删除元素、查找元素、更新元素等操作,这些操作通常在`std::unordered_map`(C++11引入的替代`hash_map`的标准容器)中也是一样的。 总的来说,`hash_map`在Linux环境下提供了...
哈希表,也被称为Hash Map,是计算机科学中一种高效的数据结构,用于存储键值对。它通过一种称为哈希函数的过程将键映射到数组的特定位置,从而实现快速的查找、插入和删除操作。在哈希表中,查找时间通常接近常数...
"kernel_list_and_hash_table.tar.gz"这个压缩包聚焦于Linux内核中的两种关键数据结构:链表和散列表,以及它们如何被用于实现内核功能。下面我们将详细探讨这两种数据结构及其在Linux内核中的应用。 首先,链表是...
GLib哈希表还提供了`g_hash_table_remove()`、`g_hash_table_steal()`等方法来删除或移除键值对,以及`g_hash_table_contains()`检查键是否存在。 4. **遍历哈希表** GLib提供迭代器来遍历哈希表的所有键值对,如`...