Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
algorithms-in-python/other/lfu_cache.py at Write-for-current-Python · coderjack/algorithms-in-python · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
coderjack
/
algorithms-in-python
Public
forked from
TheAlgorithms/Python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
Write-for-current-Python
Breadcrumbs
algorithms-in-python
/
other
/
lfu_cache.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
187 lines (150 loc) · 5.29 KB
Write-for-current-Python
Breadcrumbs
algorithms-in-python
/
other
/
lfu_cache.py
Copy path
Top
File metadata and controls
Code
Blame
187 lines (150 loc) · 5.29 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from
typing
import
Callable
,
Optional
class
DoubleLinkedListNode
:
"""
Double Linked List Node built specifically for LFU Cache
"""
def
__init__
(
self
,
key
:
int
,
val
:
int
):
self
.
key
=
key
self
.
val
=
val
self
.
freq
=
0
self
.
next
=
None
self
.
prev
=
None
class
DoubleLinkedList
:
"""
Double Linked List built specifically for LFU Cache
"""
def
__init__
(
self
):
self
.
head
=
DoubleLinkedListNode
(
None
,
None
)
self
.
rear
=
DoubleLinkedListNode
(
None
,
None
)
self
.
head
.
next
,
self
.
rear
.
prev
=
self
.
rear
,
self
.
head
def
add
(
self
,
node
:
DoubleLinkedListNode
)
->
None
:
"""
Adds the given node at the head of the list and shifting it to proper position
"""
temp
=
self
.
rear
.
prev
self
.
rear
.
prev
,
node
.
next
=
node
,
self
.
rear
temp
.
next
,
node
.
prev
=
node
,
temp
node
.
freq
+=
1
self
.
_position_node
(
node
)
def
_position_node
(
self
,
node
:
DoubleLinkedListNode
)
->
None
:
while
node
.
prev
.
key
and
node
.
prev
.
freq
>
node
.
freq
:
node1
,
node2
=
node
,
node
.
prev
node1
.
prev
,
node2
.
next
=
node2
.
prev
,
node1
.
prev
node1
.
next
,
node2
.
prev
=
node2
,
node1
def
remove
(
self
,
node
:
DoubleLinkedListNode
)
->
DoubleLinkedListNode
:
"""
Removes and returns the given node from the list
"""
temp_last
,
temp_next
=
node
.
prev
,
node
.
next
node
.
prev
,
node
.
next
=
None
,
None
temp_last
.
next
,
temp_next
.
prev
=
temp_next
,
temp_last
return
node
class
LFUCache
:
"""
LFU Cache to store a given capacity of data. Can be used as a stand-alone object
or as a function decorator.
>>> cache = LFUCache(2)
>>> cache.set(1, 1)
>>> cache.set(2, 2)
>>> cache.get(1)
1
>>> cache.set(3, 3)
>>> cache.get(2) # None is returned
>>> cache.set(4, 4)
>>> cache.get(1) # None is returned
>>> cache.get(3)
3
>>> cache.get(4)
4
>>> cache
CacheInfo(hits=3, misses=2, capacity=2, current_size=2)
>>> @LFUCache.decorator(100)
... def fib(num):
... if num in (1, 2):
... return 1
... return fib(num - 1) + fib(num - 2)
>>> for i in range(1, 101):
... res = fib(i)
>>> fib.cache_info()
CacheInfo(hits=196, misses=100, capacity=100, current_size=100)
"""
# class variable to map the decorator functions to their respective instance
decorator_function_to_instance_map
=
{}
def
__init__
(
self
,
capacity
:
int
):
self
.
list
=
DoubleLinkedList
()
self
.
capacity
=
capacity
self
.
num_keys
=
0
self
.
hits
=
0
self
.
miss
=
0
self
.
cache
=
{}
def
__repr__
(
self
)
->
str
:
"""
Return the details for the cache instance
[hits, misses, capacity, current_size]
"""
return
(
f"CacheInfo(hits=
{
self
.
hits
}
, misses=
{
self
.
miss
}
, "
f"capacity=
{
self
.
capacity
}
, current_size=
{
self
.
num_keys
}
)"
)
def
__contains__
(
self
,
key
:
int
)
->
bool
:
"""
>>> cache = LFUCache(1)
>>> 1 in cache
False
>>> cache.set(1, 1)
>>> 1 in cache
True
"""
return
key
in
self
.
cache
def
get
(
self
,
key
:
int
)
->
Optional
[
int
]:
"""
Returns the value for the input key and updates the Double Linked List. Returns
None if key is not present in cache
"""
if
key
in
self
.
cache
:
self
.
hits
+=
1
self
.
list
.
add
(
self
.
list
.
remove
(
self
.
cache
[
key
]))
return
self
.
cache
[
key
].
val
self
.
miss
+=
1
return
None
def
set
(
self
,
key
:
int
,
value
:
int
)
->
None
:
"""
Sets the value for the input key and updates the Double Linked List
"""
if
key
not
in
self
.
cache
:
if
self
.
num_keys
>=
self
.
capacity
:
key_to_delete
=
self
.
list
.
head
.
next
.
key
self
.
list
.
remove
(
self
.
cache
[
key_to_delete
])
del
self
.
cache
[
key_to_delete
]
self
.
num_keys
-=
1
self
.
cache
[
key
]
=
DoubleLinkedListNode
(
key
,
value
)
self
.
list
.
add
(
self
.
cache
[
key
])
self
.
num_keys
+=
1
else
:
node
=
self
.
list
.
remove
(
self
.
cache
[
key
])
node
.
val
=
value
self
.
list
.
add
(
node
)
@
staticmethod
def
decorator
(
size
:
int
=
128
):
"""
Decorator version of LFU Cache
"""
def
cache_decorator_inner
(
func
:
Callable
):
def
cache_decorator_wrapper
(
*
args
,
**
kwargs
):
if
func
not
in
LFUCache
.
decorator_function_to_instance_map
:
LFUCache
.
decorator_function_to_instance_map
[
func
]
=
LFUCache
(
size
)
result
=
LFUCache
.
decorator_function_to_instance_map
[
func
].
get
(
args
[
0
])
if
result
is
None
:
result
=
func
(
*
args
,
**
kwargs
)
LFUCache
.
decorator_function_to_instance_map
[
func
].
set
(
args
[
0
],
result
)
return
result
def
cache_info
():
return
LFUCache
.
decorator_function_to_instance_map
[
func
]
cache_decorator_wrapper
.
cache_info
=
cache_info
return
cache_decorator_wrapper
return
cache_decorator_inner
if
__name__
==
"__main__"
:
import
doctest
doctest
.
testmod
()
You can’t perform that action at this time.