我是靠谱客的博主 坚定雨,这篇文章主要介绍LFU算法2,代码,现在分享给大家,希望可以做个参考。

1,基本原理

LFU(Least Frequently Used)算法根据数据的历史访问频率来淘汰数据,其核心思想是“如果数据过去被访问多次,那么将来被访问的频率也更高”。

LFU的每个数据块都有一个引用计数,所有数据块按照引用计数排序,具有相同引用计数的数据块则按照时间排序。

具体实现如下:

 

1. 新加入数据插入到队列尾部(因为引用计数为1);

2. 队列中的数据被访问后,引用计数增加,队列重新排序;

3. 当需要淘汰数据时,将已经排序的列表最后的数据块删除。

2,代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/************************************************************************* > File Name: Lfu.cpp > Author:zhangtx > Mail: zhangtx@jinher.com > Created Time: 2018年03月30日 星期五 15时50分24秒 ************************************************************************/ #include <map> #include <iostream> #include <stdio.h> #include <stdlib.h> #include <sys/time.h> using namespace std; class LFUCache { public: /* * @param capacity: An integer */LFUCache(int capacity) { // do intialization if necessary m_capacity=capacity; m_count = 0; } void remove_least_used_key() { map<int,int>::iterator iterBegin=m_countMap.begin(); map<int,int>::iterator pos =iterBegin; for(;iterBegin!=m_countMap.end();iterBegin++) { if (pos->second>iterBegin->second) { pos=iterBegin; } else if (pos->second==iterBegin->second) { if (m_keytotime[pos->first]>m_keytotime[iterBegin->first]) { pos=iterBegin; } } } cout<<endl<<"remove "<<pos->first<<endl; m_dataMap.erase(m_dataMap.find(pos->first)); m_keytotime.erase(m_keytotime.find(pos->first)); m_countMap.erase(pos); m_count--; } /* * @param key: An integer * @param value: An integer * @return: nothing */ void set(int key, int value) { // write your code here if (m_countMap.find(key) != m_countMap.end()) { m_countMap[key]=m_countMap[key]+1; m_dataMap[key]=value; } else { if ((m_count>=m_capacity)) { remove_least_used_key(); } m_countMap[key]=1; m_dataMap[key]=value; m_count++; } struct timezone tz; struct timeval tv; gettimeofday (&tv , &tz); m_keytotime[key]=tv.tv_sec*1000000+tv.tv_usec; } /* * @param key: An integer * @return: An integer */ int get(int key) { // write your code here if (m_dataMap.find(key)!=m_dataMap.end()) {
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
struct timezone tz; struct timeval tv; gettimeofday (&tv , &tz); m_keytotime[key]=tv.tv_sec*1000000+tv.tv_usec; m_countMap[key]=m_countMap[key]+1; return m_dataMap[key]; } else return -1; } private: int m_capacity; int m_count; map<int,int> m_dataMap; map<int,int> m_countMap; map<int,long> m_keytotime; }; int main(int argc,char *argv[]) { cout<<endl; cout<<endl; LFUCache *lfu=new LFUCache(3); lfu->set(1,10); lfu->set(2,20); lfu->set(3,30); cout<<lfu->get(1)<<" "; lfu->set(4, 40); cout<<lfu->get(4)<<" "; cout<<lfu->get(3)<<" "; cout<<lfu->get(2)<<" "; cout<<lfu->get(1)<<" "; lfu->set(5, 50); cout<<lfu->get(1)<<" "; cout<<lfu->get(2)<<" "; cout<<lfu->get(3)<<" "; cout<<lfu->get(4)<<" "; cout<<lfu->get(5)<<" "; delete lfu; cout<<endl; cout<<endl; cout<<endl; }

lfu

最后

以上就是坚定雨最近收集整理的关于LFU算法2,代码的全部内容,更多相关LFU算法2内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(85)

评论列表共有 0 条评论

立即
投稿
返回
顶部