-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathLRUCache.cs
154 lines (135 loc) · 5.07 KB
/
LRUCache.cs
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
/**
* This code is based on
* https://github.com/migueldeicaza/MonoTouch.Dialog/blob/e69b806efee4ead3c39926d5164c15ae90a92b7a/MonoTouch.Dialog/Utilities/LRUCache.cs
*
* Minor adaptations were made for compatibility with structs and classes which don't implement IDisposable
*/
//
// A simple LRU cache used for tracking the images
//
// Authors:
// Miguel de Icaza ([email protected])
//
// Copyright 2010 Miguel de Icaza
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
using System;
using System.Collections.Generic;
namespace MonoTouch.Dialog.Utilities
{
public class LRUCache<TKey, TValue>
{
Dictionary<TKey, LinkedListNode<TValue>> dict;
Dictionary<LinkedListNode<TValue>, TKey> revdict;
LinkedList<TValue> list;
int entryLimit, sizeLimit, currentSize;
Func<TValue, int> slotSizeFunc;
public LRUCache(int entryLimit) : this(entryLimit, 0, null)
{
}
public LRUCache(int entryLimit, int sizeLimit, Func<TValue, int> slotSizer)
{
list = new LinkedList<TValue>();
dict = new Dictionary<TKey, LinkedListNode<TValue>>();
revdict = new Dictionary<LinkedListNode<TValue>, TKey>();
if (sizeLimit != 0 && slotSizer == null)
throw new ArgumentNullException("If sizeLimit is set, the slotSizer must be provided");
this.entryLimit = entryLimit;
this.sizeLimit = sizeLimit;
slotSizeFunc = slotSizer;
}
void Evict()
{
var last = list.Last;
var key = revdict[last];
if (sizeLimit > 0)
{
int size = slotSizeFunc(last.Value);
currentSize -= size;
}
dict.Remove(key);
revdict.Remove(last);
list.RemoveLast();
(last.Value as IDisposable)?.Dispose();
}
public void Purge()
{
foreach (var element in list)
(element as IDisposable)?.Dispose();
dict.Clear();
revdict.Clear();
list.Clear();
currentSize = 0;
}
public TValue this[TKey key]
{
get
{
if (dict.TryGetValue(key, out LinkedListNode<TValue> node))
{
list.Remove(node);
list.AddFirst(node);
return node.Value;
}
return default;
}
set
{
int size = sizeLimit > 0 ? slotSizeFunc(value) : 0;
if (dict.TryGetValue(key, out LinkedListNode<TValue> node))
{
if (sizeLimit > 0 && node.Value != null)
{
int repSize = slotSizeFunc(node.Value);
currentSize -= repSize;
currentSize += size;
}
// If we already have a key, move it to the front
list.Remove(node);
list.AddFirst(node);
// Remove the old value
if (node.Value != null)
(node.Value as IDisposable)?.Dispose();
node.Value = value;
while (sizeLimit > 0 && currentSize > sizeLimit && list.Count > 1)
Evict();
return;
}
if (sizeLimit > 0)
{
while (sizeLimit > 0 && currentSize + size > sizeLimit && list.Count > 0)
Evict();
}
if (dict.Count >= entryLimit)
Evict();
// Adding new node
node = new LinkedListNode<TValue>(value);
list.AddFirst(node);
dict[key] = node;
revdict[node] = key;
currentSize += size;
}
}
public override string ToString()
{
return "LRUCache dict={0} revdict={1} list={2}";
}
}
}