• 哈希表查找
    • By C++
    • By Golang

    哈希表查找

    By C++

    1. #include <stdio.h>
    2. #define MaxSize 100 /*哈希表最大长度*/
    3. typedef int KeyType;
    4. typedef struct
    5. {
    6. KeyType key; /*关键字值*/
    7. int si; /*探查次数*/
    8. } HashTable;
    9. void CreateHT(HashTable ht[],KeyType a[],int n,int m,int p) /*构造哈希表*/
    10. {
    11. int i,d,cnt;
    12. for (i=0;i<m;i++) /*置初值*/
    13. {
    14. ht[i].key=0;
    15. ht[i].si=0;
    16. }
    17. for (i=0;i<n;i++)
    18. {
    19. cnt=1; /*累计探查次数*/
    20. d=a[i]%p;
    21. if (ht[d].key==0)
    22. {
    23. ht[d].key=a[i];
    24. ht[d].si=cnt;
    25. }
    26. else
    27. {
    28. do /*处理冲突*/
    29. {
    30. d=(d+1)%m;
    31. cnt++;
    32. } while (ht[d].key!=0);
    33. ht[d].key=a[i];
    34. ht[d].si=cnt;
    35. }
    36. }
    37. }
    38. void DispHT(HashTable ht[],int n,int m) /*输出哈希表*/
    39. {
    40. int i;
    41. double avg;
    42. printf("i: ");
    43. for (i=0;i<m;i++)
    44. printf("%-3d",i);
    45. printf("\n");
    46. printf("key:");
    47. for (i=0;i<m;i++)
    48. printf("%-3d",ht[i].key);
    49. printf("\n");
    50. printf("si: ");
    51. for (i=0;i<m;i++)
    52. printf("%-3d",ht[i].si);
    53. printf("\n");
    54. avg=0;
    55. for (i=0;i<m;i++)
    56. avg+=ht[i].si;
    57. avg=avg/n;
    58. printf("平均查找长度:ASL(%d)=%g\n",n,avg);
    59. }
    60. void main()
    61. {
    62. HashTable ht[MaxSize];
    63. KeyType a[]={19,1,23,14,55,20,84,27,68,11,10,77};
    64. int n=12,m=19,p=13;
    65. CreateHT(ht,a,n,m,p);
    66. DispHT(ht,n,m);
    67. }

    By Golang

    1. // Package hashtable creates a ValueHashtable data structure for the Item type
    2. package hashtable
    3. import (
    4. "fmt"
    5. "sync"
    6. )
    7. // Key the key of the dictionary
    8. type Key interface{}
    9. // Value the content of the dictionary
    10. type Value interface{}
    11. // ValueHashtable the set of Items
    12. type ValueHashtable struct {
    13. items map[int]Value
    14. lock sync.RWMutex
    15. }
    16. // the hash() private function uses the famous Horner's method
    17. // to generate a hash of a string with O(n) complexity
    18. func hash(k Key) int {
    19. key := fmt.Sprintf("%s", k)
    20. h := 0
    21. for i := 0; i < len(key); i++ {
    22. h = 31*h + int(key[i])
    23. }
    24. return h
    25. }
    26. // Put item with value v and key k into the hashtable
    27. func (ht *ValueHashtable) Put(k Key, v Value) {
    28. ht.lock.Lock()
    29. defer ht.lock.Unlock()
    30. i := hash(k)
    31. if ht.items == nil {
    32. ht.items = make(map[int]Value)
    33. }
    34. ht.items[i] = v
    35. }
    36. // Remove item with key k from hashtable
    37. func (ht *ValueHashtable) Remove(k Key) {
    38. ht.lock.Lock()
    39. defer ht.lock.Unlock()
    40. i := hash(k)
    41. delete(ht.items, i)
    42. }
    43. // Get item with key k from the hashtable
    44. func (ht *ValueHashtable) Get(k Key) Value {
    45. ht.lock.RLock()
    46. defer ht.lock.RUnlock()
    47. i := hash(k)
    48. return ht.items[i]
    49. }
    50. // Size returns the number of the hashtable elements
    51. func (ht *ValueHashtable) Size() int {
    52. ht.lock.RLock()
    53. defer ht.lock.RUnlock()
    54. return len(ht.items)
    55. }