/
smwwiki
/
OneScript_fork
Обзор
Документация
Войти
/
smwwiki
/
OneScript_fork
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
src/OneScript.Core/Commons/LruCache.cs
72 строки
2 KB
EvilBeaver
Поисправлял немного предупреждений от анализатора
18 сен 2022, 09:55
18 сен 2022, 09:55
fd2865d
Код
Авторство
О чём код?
/*---------------------------------------------------------- This Source Code Form is subject to the terms of the Mozilla Public License, v.2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. ----------------------------------------------------------*/ using System; using System.Collections.Generic; namespace OneScript.Commons { public class LruCache<TKey, TValue> { private readonly int _capacity; private readonly Dictionary<TKey, LinkedListNode<CacheItem>> _index = new Dictionary<TKey, LinkedListNode<CacheItem>>(); private readonly LinkedList<CacheItem> _list = new LinkedList<CacheItem>(); public LruCache(int capacity) { _capacity = capacity; } public bool IsEmpty() => _index.Count == 0; public void Clear() { _index.Clear(); _list.Clear(); } public TValue GetOrAdd(TKey key, Func<TKey, TValue> factory) { if (_index.TryGetValue(key, out var listNode)) { _list.Remove(listNode); _list.AddFirst(listNode); return listNode.Value.Value; } if (_index.Count == _capacity) { var keyOfOld = _list.Last.Value.Key; _index.Remove(keyOfOld); _list.RemoveLast(); } var newItem = _list.AddFirst(CacheItem.Create(key, factory(key))); _index[key] = newItem; return newItem.Value.Value; } private class CacheItem { public static CacheItem Create(TKey key, TValue value) => new CacheItem(key, value); private CacheItem(TKey key, TValue value) { Key = key; Value = value; } public TKey Key { get; } public TValue Value { get; } } } }