|
| 1 | +/* |
| 2 | +****************************************************************************** |
| 3 | +* Copyright (c) 2016 Tencent Inc. All rights reserved |
| 4 | + |
| 5 | +* Date: 2016-10-30 |
| 6 | +* Description: 哈希树 - 质数分辨定理实现 |
| 7 | +****************************************************************************** |
| 8 | +*/ |
| 9 | + |
| 10 | +template <class KeyType, class ValueType, int SIZE = 32> |
| 11 | +class HashTree |
| 12 | +{ |
| 13 | + struct Node |
| 14 | + { |
| 15 | + Node() : occupied_(false), child_({0}) {} |
| 16 | + KeyType key_; |
| 17 | + ValueType value_; |
| 18 | + bool occupied_; |
| 19 | + Node *child_[SIZE]; |
| 20 | + }; |
| 21 | + |
| 22 | +public: |
| 23 | + HashTree() |
| 24 | + { |
| 25 | + max_level_ = 0; |
| 26 | + prime_ = {0}; |
| 27 | + |
| 28 | + for (int i = 2; i <= SIZE; ++i) |
| 29 | + { |
| 30 | + if (IsPrime(i)) |
| 31 | + { |
| 32 | + prime_[++max_level_] = i; |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * 插入函数 |
| 39 | + * @param key 键 |
| 40 | + * @param value 值 |
| 41 | + * @return 成功 true, 失败 false |
| 42 | + */ |
| 43 | + bool Insert(const KeyType &key, const ValueType &value) |
| 44 | + { |
| 45 | + return Insert(&root_, 1, key, value); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * 查找函数 |
| 50 | + * @param key 键 |
| 51 | + * @return 指向value的指针, 找不到时为NULL |
| 52 | + */ |
| 53 | + const ValueType *Find(const KeyType &key) |
| 54 | + { |
| 55 | + return Find(&root_, 1, key); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * 删除函数 |
| 60 | + * @param key 键 |
| 61 | + * @return 任何情况下都返回true |
| 62 | + */ |
| 63 | + bool Delete(const KeyType &key) |
| 64 | + { |
| 65 | + return Delete(&root_, 1, key); |
| 66 | + } |
| 67 | + |
| 68 | +private: |
| 69 | + bool Insert(Node *node, int level, const KeyType &key, const ValueType &value) |
| 70 | + { |
| 71 | + if (!node->occupied_) |
| 72 | + { |
| 73 | + node->key_ = key; |
| 74 | + node->value_ = value; |
| 75 | + node->occupied_ = true; |
| 76 | + return true; |
| 77 | + } |
| 78 | + |
| 79 | + if (level > max_level_) |
| 80 | + { |
| 81 | + return false; |
| 82 | + } |
| 83 | + |
| 84 | + int idx = key % prime_[level]; |
| 85 | + |
| 86 | + if (!node->child_[idx]) |
| 87 | + { |
| 88 | + node->child_[idx] = new Node; |
| 89 | + } |
| 90 | + |
| 91 | + return Insert(node->child_[idx], ++level, key, value); |
| 92 | + } |
| 93 | + |
| 94 | + const ValueType *Find(Node *node, int level, const KeyType &key) |
| 95 | + { |
| 96 | + if (node->occupied_) |
| 97 | + { |
| 98 | + if (node->key_ == key) |
| 99 | + { |
| 100 | + return &node->value_; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + int idx = key % prime_[level]; |
| 105 | + if (!node->child_[idx]) |
| 106 | + { |
| 107 | + return 0; |
| 108 | + } |
| 109 | + |
| 110 | + return Find(node->child_[idx], ++level, key); |
| 111 | + } |
| 112 | + |
| 113 | + bool Delete(Node *node, int level, const KeyType &key) |
| 114 | + { |
| 115 | + if (node->occupied_) |
| 116 | + { |
| 117 | + if (node->key_ == key) |
| 118 | + { |
| 119 | + node->occupied_ = false; |
| 120 | + return true; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + int idx = key % prime_[level]; |
| 125 | + if (!node->child_[idx]) |
| 126 | + { |
| 127 | + return true; |
| 128 | + } |
| 129 | + |
| 130 | + return Delete(node->child_[idx], ++level, key); |
| 131 | + } |
| 132 | +public: |
| 133 | + bool IsPrime(int n) |
| 134 | + { |
| 135 | + for (int i = 2; (i * i) < (n + 1); ++i) |
| 136 | + { |
| 137 | + if (!(n % i)) |
| 138 | + { |
| 139 | + return false; |
| 140 | + } |
| 141 | + } |
| 142 | + return true; |
| 143 | + } |
| 144 | + |
| 145 | +private: |
| 146 | + Node root_; |
| 147 | + int max_level_; |
| 148 | + int prime_[SIZE+1]; |
| 149 | +}; |
| 150 | + |
| 151 | + |
| 152 | +#include <iostream> |
| 153 | + |
| 154 | +int main() |
| 155 | +{ |
| 156 | + HashTree<int, std::string> tree; |
| 157 | + |
| 158 | + tree.Insert(1111, "AAA"); |
| 159 | + tree.Insert(2222, "BBB"); |
| 160 | + |
| 161 | + const std::string *value; |
| 162 | + |
| 163 | + value = tree.Find(2222); |
| 164 | + std::cout << (value ? *value : "NULL") << std::endl; |
| 165 | + |
| 166 | + tree.Delete(2222); |
| 167 | + |
| 168 | + value = tree.Find(2222); |
| 169 | + std::cout << (value ? *value : "NULL") << std::endl; |
| 170 | + |
| 171 | + std::cout << tree.IsPrime(3); |
| 172 | + |
| 173 | + return 0; |
| 174 | +} |
0 commit comments