国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

c#實現的LRU算法

2019-11-08 03:11:19
字體:
來源:轉載
供稿:網友
using System.Collections.Generic;using System.Threading;namespace Lru{    public class LRUCache<TKey, TValue>    {        const int DEFAULT_CAPACITY = 255;        int _capacity;        ReaderWriterLockSlim _locker;        IDictionary<TKey, TValue> _dictionary;        LinkedList<TKey> _linkedList;        public LRUCache() : this(DEFAULT_CAPACITY) { }        public LRUCache(int capacity)        {            _locker = new ReaderWriterLockSlim();            _capacity = capacity > 0 ? capacity : DEFAULT_CAPACITY;            _dictionary = new Dictionary<TKey, TValue>();            _linkedList = new LinkedList<TKey>();        }        public void Set(TKey key, TValue value)        {            _locker.EnterWriteLock();            try            {                _dictionary[key] = value;                _linkedList.Remove(key);                _linkedList.AddFirst(key);                if (_linkedList.Count > _capacity)                {                    _dictionary.Remove(_linkedList.Last.Value);                    _linkedList.RemoveLast();                }            }            finally { _locker.ExitWriteLock(); }        }        public bool TryGet(TKey key, out TValue value)        {            _locker.EnterUpgradeableReadLock();            try            {                bool b = _dictionary.TryGetValue(key, out value);                if (b)                {                    _locker.EnterWriteLock();                    try                    {                        _linkedList.Remove(key);                        _linkedList.AddFirst(key);                    }                    finally { _locker.ExitWriteLock(); }                }                return b;            }            catch { throw; }            finally { _locker.ExitUpgradeableReadLock(); }        }        public bool ContainsKey(TKey key)        {            _locker.EnterReadLock();            try            {                return _dictionary.ContainsKey(key);            }            finally { _locker.ExitReadLock(); }        }        public int Count        {            get            {                _locker.EnterReadLock();                try                {                    return _dictionary.Count;                }                finally { _locker.ExitReadLock(); }            }        }        public int Capacity        {            get            {                _locker.EnterReadLock();                try                {                    return _capacity;                }                finally { _locker.ExitReadLock(); }            }            set            {                _locker.EnterUpgradeableReadLock();                try                {                    if (value > 0 && _capacity != value)                    {                        _locker.EnterWriteLock();                        try                        {                            _capacity = value;                            while (_linkedList.Count > _capacity)                            {                                _linkedList.RemoveLast();                            }                        }                        finally { _locker.ExitWriteLock(); }                    }                }                finally { _locker.ExitUpgradeableReadLock(); }            }        }        public ICollection<TKey> Keys        {            get            {                _locker.EnterReadLock();                try                {                    return _dictionary.Keys;                }                finally { _locker.ExitReadLock(); }            }        }        public ICollection<TValue> Values        {            get            {                _locker.EnterReadLock();                try                {                    return _dictionary.Values;                }                finally { _locker.ExitReadLock(); }            }        }    }}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 长武县| 汽车| 咸阳市| 罗田县| 巴中市| 大安市| 武功县| 兴海县| 涟源市| 美姑县| 南川市| 三台县| 泗水县| 永州市| 饶平县| 江门市| 莲花县| 阜新| 丘北县| 荣昌县| 扶沟县| 阿拉尔市| 上蔡县| 嘉兴市| 梓潼县| 丹凤县| 安新县| 汝州市| 盐津县| 大竹县| 康马县| 贞丰县| 张家界市| 通城县| 通州市| 文水县| 汉源县| 隆林| 赤峰市| 龙泉市| 绍兴市|