-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLruDictionary.cs
More file actions
174 lines (103 loc) · 4.23 KB
/
LruDictionary.cs
File metadata and controls
174 lines (103 loc) · 4.23 KB
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace Gsemac.Collections {
public class LruDictionary<TKey, TValue> :
IDictionary<TKey, TValue> {
// Public members
public int Capacity { get; set; }
public ICollection<TKey> Keys => new LazyReadOnlyCollection<TKey>(values.Select(i => i.Key));
public ICollection<TValue> Values => new LazyReadOnlyCollection<TValue>(values.Select(i => i.Value));
public int Count => values.Count();
public bool IsReadOnly => false;
public TValue this[TKey key] {
get => GetValue(key);
set => SetValue(key, value, replace: true);
}
public LruDictionary(int capacity) {
Capacity = capacity;
}
public bool ContainsKey(TKey key) {
return nodeDict.ContainsKey(key);
}
public void Add(TKey key, TValue value) {
SetValue(key, value, replace: false);
}
public bool Remove(TKey key) {
if (nodeDict.TryGetValue(key, out LinkedListNode<KeyValuePair<TKey, TValue>> node)) {
values.Remove(node);
nodeDict.Remove(key);
return true;
}
else {
return false;
}
}
public bool TryGetValue(TKey key, out TValue value) {
if (nodeDict.TryGetValue(key, out LinkedListNode<KeyValuePair<TKey, TValue>> node)) {
value = node.Value.Value;
SetMostRecent(node);
return true;
}
else {
value = default;
return false;
}
}
public void Add(KeyValuePair<TKey, TValue> item) {
SetValue(item.Key, item.Value, replace: false);
}
public void Clear() {
values.Clear();
nodeDict.Clear();
}
public bool Contains(KeyValuePair<TKey, TValue> item) {
return nodeDict.ContainsKey(item.Key);
}
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) {
values.Select(i => new KeyValuePair<TKey, TValue>(i.Key, i.Value)).ToArray()
.CopyTo(array, arrayIndex);
}
public bool Remove(KeyValuePair<TKey, TValue> item) {
return Remove(item.Key);
}
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() {
return values.Select(i => new KeyValuePair<TKey, TValue>(i.Key, i.Value))
.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
// Private members
private readonly LinkedList<KeyValuePair<TKey, TValue>> values = new LinkedList<KeyValuePair<TKey, TValue>>();
private readonly IDictionary<TKey, LinkedListNode<KeyValuePair<TKey, TValue>>> nodeDict = new Dictionary<TKey, LinkedListNode<KeyValuePair<TKey, TValue>>>();
private void SetMostRecent(LinkedListNode<KeyValuePair<TKey, TValue>> node) {
values.Remove(node);
values.AddFirst(node);
}
private void EvictOldest() {
if (values.Any()) {
LinkedListNode<KeyValuePair<TKey, TValue>> nodeToRemove = values.Last;
bool removedFromDict = nodeDict.Remove(nodeToRemove.Value.Key);
Debug.Assert(removedFromDict);
values.Remove(nodeToRemove);
}
}
private TValue GetValue(TKey key) {
if (TryGetValue(key, out TValue value))
return value;
else
throw new KeyNotFoundException(Properties.ExceptionMessages.KeyNotFound);
}
private void SetValue(TKey key, TValue value, bool replace) {
if (!Remove(key) && Count >= Capacity)
EvictOldest();
if (!replace && ContainsKey(key))
throw new ArgumentException(Properties.ExceptionMessages.KeyAlreadyExists, nameof(key));
nodeDict[key] = values.AddFirst(new KeyValuePair<TKey, TValue>(key, value));
Debug.Assert(Count <= Capacity);
}
}
}