mirror of
https://github.com/bringout/oca-ocb-core.git
synced 2026-04-20 08:52:08 +02:00
18.0 vanilla
This commit is contained in:
parent
d72e748793
commit
0a7ae8db93
337 changed files with 399651 additions and 232598 deletions
|
|
@ -1,57 +1,60 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import collections
|
||||
import threading
|
||||
import typing
|
||||
from collections.abc import Iterable, Iterator, MutableMapping
|
||||
|
||||
from .func import locked
|
||||
|
||||
__all__ = ['LRU']
|
||||
|
||||
class LRU(object):
|
||||
K = typing.TypeVar('K')
|
||||
V = typing.TypeVar('V')
|
||||
|
||||
|
||||
class LRU(MutableMapping[K, V], typing.Generic[K, V]):
|
||||
"""
|
||||
Implementation of a length-limited O(1) LRU map.
|
||||
|
||||
Original Copyright 2003 Josiah Carlson, later rebuilt on OrderedDict.
|
||||
Original Copyright 2003 Josiah Carlson, later rebuilt on OrderedDict and added typing.
|
||||
"""
|
||||
def __init__(self, count, pairs=()):
|
||||
def __init__(self, count: int, pairs: Iterable[tuple[K, V]] = ()):
|
||||
self._lock = threading.RLock()
|
||||
self.count = max(count, 1)
|
||||
self.d = collections.OrderedDict()
|
||||
self.d: collections.OrderedDict[K, V] = collections.OrderedDict()
|
||||
for key, value in pairs:
|
||||
self[key] = value
|
||||
|
||||
@locked
|
||||
def __contains__(self, obj):
|
||||
def __contains__(self, obj: K) -> bool:
|
||||
return obj in self.d
|
||||
|
||||
def get(self, obj, val=None):
|
||||
try:
|
||||
return self[obj]
|
||||
except KeyError:
|
||||
return val
|
||||
|
||||
@locked
|
||||
def __getitem__(self, obj):
|
||||
def __getitem__(self, obj: K) -> V:
|
||||
a = self.d[obj]
|
||||
self.d.move_to_end(obj, last=False)
|
||||
return a
|
||||
|
||||
@locked
|
||||
def __setitem__(self, obj, val):
|
||||
def __setitem__(self, obj: K, val: V):
|
||||
self.d[obj] = val
|
||||
self.d.move_to_end(obj, last=False)
|
||||
while len(self.d) > self.count:
|
||||
self.d.popitem(last=True)
|
||||
|
||||
@locked
|
||||
def __delitem__(self, obj):
|
||||
def __delitem__(self, obj: K):
|
||||
del self.d[obj]
|
||||
|
||||
@locked
|
||||
def __len__(self):
|
||||
def __len__(self) -> int:
|
||||
return len(self.d)
|
||||
|
||||
@locked
|
||||
def pop(self,key):
|
||||
def __iter__(self) -> Iterator[K]:
|
||||
return iter(self.d)
|
||||
|
||||
@locked
|
||||
def pop(self, key: K) -> V:
|
||||
return self.d.pop(key)
|
||||
|
||||
@locked
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue