python利用双向链表和map实现LFU算法
双向链表链接:
https://blog.csdn.net/weixin_41978134/article/details/106143560
"""
@File : LFU.py
@Author: wangjiahui
@Date : 2020/5/17 17:02
@Desc : LRU算法
"""
from Computer_Organization.node1 import DoubleLinkedList, Node
class LFUNode(Node):
def __init__(self, key, value):
# 频率属性
self.freq = 0
super(LFUNode, self).__init__(key, value)
class LFUCache(object):
def __init__(self, capacity):
self.capacity = capacity
self.size = 0
self.map = {}
# key: 频率, value: 频率对应的双向链表
self.freq_map = {}
# 更新节点频率的操作
def __update_freq(self, node):
freq = node.freq
# 删除
node = self.freq_map[freq].remove(node)
if self.freq_map[freq].size == 0:
del self.freq_map[freq]
# 更新
freq += 1
node.freq = freq
if freq not in self.freq_map:
self.freq_map[freq] = DoubleLinkedList()
self.freq_map[freq].append(node)
def get(self, key):
if key not in self.map:
return -1
node = self.map[key]
self.__update_freq(node)
return node.value
def put(self, key, value):
if self.capacity == 0:
return
# 缓存命中情况
if key in self.map:
node = self.map.get(key)
node.value = value
self.__update_freq(node)
# 缓存没有命中情况
else:
if self.size == self.capacity:
# min_freq = min(self.freq_map.keys())
min_freq = min(self.freq_map)
node = self.freq_map[min_freq].pop()
del self.map[node.key]
self.size -= 1
node = LFUNode(key, value)
node.freq = 1
self.map[key] = node
if node.freq not in self.freq_map:
self.freq_map[node.freq] = DoubleLinkedList()
node = self.freq_map[node.freq].append(node)
self.size += 1
def print(self):
print("================================")
for k, v in self.freq_map.items():
print('Freq = %d' % k)
self.freq_map[k].print()
print("================================")
print()
if __name__ == '__main__':
cache = LFUCache(4)
cache.put(1, 1)
cache.print()
cache.put(2, 2)
cache.print()
print(cache.get(1))
cache.put(3, 3)
cache.print()
print(cache.get(2))
cache.print()
print(cache.get(3))
cache.print()
cache.put(4, 4)
cache.print()
print(cache.get(1))
cache.print()
print(cache.get(3))
cache.print()
print(cache.get(4))
结果
================================
Freq = 1
{1: 1}
================================
================================
Freq = 1
{1: 1}<=>{2: 2}
================================
1
================================
Freq = 1
{2: 2}<=>{3: 3}
Freq = 2
{1: 1}
================================
2
================================
Freq = 1
{3: 3}
Freq = 2
{1: 1}<=>{2: 2}
================================
3
================================
Freq = 2
{1: 1}<=>{2: 2}<=>{3: 3}
================================
================================
Freq = 2
{1: 1}<=>{2: 2}<=>{3: 3}
Freq = 1
{4: 4}
================================
1
================================
Freq = 2
{2: 2}<=>{3: 3}
Freq = 1
{4: 4}
Freq = 3
{1: 1}
================================
3
================================
Freq = 2
{2: 2}
Freq = 1
{4: 4}
Freq = 3
{1: 1}<=>{3: 3}
================================
4