forked from pythonnet/pythonnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFifoDictionary.cs
More file actions
62 lines (49 loc) · 1.65 KB
/
FifoDictionary.cs
File metadata and controls
62 lines (49 loc) · 1.65 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
using System;
namespace Python.Runtime
{
using System.Collections.Generic;
public class FifoDictionary<TKey, TValue>
{
private readonly Dictionary<TKey, int> _innerDictionary;
private readonly KeyValuePair<TKey,TValue>[] _fifoList;
private bool _hasEmptySlots = true;
public FifoDictionary(int capacity)
{
if (capacity <= 0)
{
throw new ArgumentOutOfRangeException(nameof(capacity), "Capacity should be non-zero positive.");
}
_innerDictionary = new Dictionary<TKey, int>(capacity);
_fifoList = new KeyValuePair<TKey, TValue>[capacity];
Capacity = capacity;
}
public bool TryGetValue(TKey key, out TValue value)
{
int index;
if (_innerDictionary.TryGetValue(key, out index))
{
value =_fifoList[index].Value;
return true;
}
value = default(TValue);
return false;
}
public void AddUnsafe(TKey key, TValue value)
{
if (!_hasEmptySlots)
{
_innerDictionary.Remove(_fifoList[NextSlotToAdd].Key);
}
_innerDictionary.Add(key, NextSlotToAdd);
_fifoList[NextSlotToAdd] = new KeyValuePair<TKey, TValue>(key, value);
NextSlotToAdd++;
if (NextSlotToAdd >= Capacity)
{
_hasEmptySlots = false;
NextSlotToAdd = 0;
}
}
public int NextSlotToAdd { get; private set; }
public int Capacity { get; }
}
}