哈希表

  1 #include<iostream>  
  2 #include<vector>  
  3 #include<string>  
  4 using namespace std;
  5 template<class K, class V>
  6 struct Node
  7 {
  8     K _key;
  9     V _value;
 10     Node*_next;
 11     Node(const K& key, const V& value)
 12         :_key(key)
 13         ,_value(value)
 14         ,_next(NULL)
 15     {}
 16 };
 17 template<class T>
 18 struct Hasher
 19 {
 20     size_t operator() (const T& key)
 21     {
 22         return key;
 23     }
 24 };
 25 
 26 template<>
 27 struct Hasher<string>
 28 {
 29     size_t operator() (const string& s)
 30     {
 31         const char* str = s.c_str();
 32 
 33         unsigned int seed = 131; // 31 131 1313 13131 131313  
 34         unsigned int hash = 0;
 35         while (*str)
 36         {
 37             hash = hash * seed + (*str++);
 38         }
 39 
 40         return (hash & 0x7FFFFFFF);
 41     }
 42 };
 43 
 44 const int _PrimeSize = 28;
 45 static const unsigned long _PrimeList[_PrimeSize] =
 46 {
 47     53ul, 97ul, 193ul, 389ul, 769ul,
 48     1543ul, 3079ul, 6151ul, 12289ul, 24593ul,
 49     49157ul, 98317ul, 196613ul, 393241ul, 786433ul,
 50     1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,
 51     50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,
 52     1610612741ul, 3221225473ul, 4294967291ul
 53 };
 54 template<class K, class V, class HashFunc = Hasher<K>>
 55 class HashTable
 56 {
 57 public:
 58     HashTable(size_t capacity)
 59     {
 60         _size = 0;
 61         _table.reserve(capacity);
 62         _table.assign(capacity, NULL);
 63     }
 64     void Insert(const K& key, const V& value)//放入数据  
 65     {
 66         ExpandCapacity();
 67 
 68         int pos = _HashFunc(key, _table.size());
 69         Node<K, V>*begin = _table[pos];
 70         while (begin != NULL)
 71         {
 72             if (begin->_key == key)
 73             {
 74                 return;
 75             }
 76             begin = begin->_next;
 77         }
 78         Node<K, V>*tmp = new Node<K, V>(key, value);
 79         tmp->_next = _table[pos];
 80         _table[pos] = tmp;
 81         ++_size;
 82     }
 83     void Delete(const int&key)//删除结点  
 84     {
 85         size_t pos = (size_t)key % (size_t)_table.capacity();
 86         Node*begin = _table[pos];
 87         Node*prev = NULL;
 88         while (begin)
 89         {
 90             if (begin->_key == key)
 91             {
 92                 if (begin == _table[pos])
 93                 {
 94                     _table[pos] = _table[pos]->_next;
 95                 }
 96                 else
 97                 {
 98                     prev->_next = begin->_next;
 99                 }
100                 delete begin;
101                 return;
102             }
103             prev = begin;
104             begin = begin->_next;
105         }
106     }
107     void Print()
108     {
109         for (int i = 24; i <(int)_table.capacity(); i++)
110         {
111             Node<K, V>*begin = _table[i];
112             while (begin != NULL)
113             {
114                 printf("pos[%d]:", i);
115                 cout << "(" << begin->_key << "," << begin->_value << ")";
116                 cout << "->";
117                 begin = begin->_next;
118             }
119             cout << "NULL" << endl;
120         }
121     }
122 
123 protected:
124     void ExpandCapacity()//实现扩容  
125     {
126         if (_size >= (size_t)_table.capacity())
127         {
128             int NewCapacity = CapacityNum(_size);
129             vector<Node<K, V> *>tmp;
130             tmp.reserve(NewCapacity);
131             tmp.assign(NewCapacity, NULL);
132             for (size_t i = 0; i < (size_t)_table.capacity(); i++)
133             {
134                 while (_table[i] != NULL)
135                 {
136                     //摘节点  
137                     Node<K, V>* head = _table[i];
138                     _table[i] = _table[i]->_next;
139 
140                     //放节点  
141                     int pos = _HashFunc((head->_key), NewCapacity);
142                     head->_next = tmp[pos];
143                     tmp[pos] = head;
144                 }
145             }
146             _table.swap(tmp);
147         }
148     }
149     size_t CapacityNum(size_t num) //得到应该扩展到的容量值  
150     {
151         for (int i = 0; i < _PrimeSize; i++)
152         {
153             if (_PrimeList[i]>num)
154             {
155                 return _PrimeList[i];
156             }
157         }
158         return _PrimeList[_PrimeSize - 1];
159     }
160 
161     int _HashFunc(const K& key, size_t capacity)
162     {
163         HashFunc hashFunc; // operator()  
164         return hashFunc(key) % _table.capacity();
165 
166     }
167 private:
168     vector<Node<K, V>*>_table;
169     size_t _size;
170 };
171 int main()
172 {
173     system("pause");
174     return 0;
175 }

 

转载于:https://www.cnblogs.com/yuanshuang/p/5409248.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值