python利用双向链表和map实现LFU算法

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值