-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIBTree.cs
More file actions
122 lines (104 loc) · 4 KB
/
IBTree.cs
File metadata and controls
122 lines (104 loc) · 4 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
using Optional;
namespace HeapsAndBTrees
{
public interface IBTree<TKey, TValue, TNode, TVirtual, TActual>
where TKey : IComparable
where TNode : INode<TKey>
where TActual : IActualNode<TNode, TKey>, TNode
where TVirtual : IVirtualNode<TActual, TNode, TKey>, TNode
{
public Option<TValue> Search(TKey key);
public void Insert(TKey key, TValue value);
public void Delete(TKey key);
public IEnumerable<(TKey, TValue)> Traverse();
public void Clear();
public delegate void OnRootChangedEvent(TActual root);
public event OnRootChangedEvent OnRootChanged;
}
public interface IDiagnostableBTree<TKey, TValue, TNode, TVirtual, TActual> : IBTree<TKey, TValue, TNode, TVirtual, TActual>
where TKey : IComparable
where TNode : INode<TKey>
where TActual : IActualNode<TNode, TKey>, TNode
where TVirtual : IVirtualNode<TActual, TNode, TKey>, TNode
{
public BTreeDiagnosticsData Retrieve(BTreeOperation operation);
public void Watch();
public void ResetWatch();
public void PrettyPrint();
}
public enum BTreeOperation
{
Search = 0,
Insert = 1,
Delete = 2
}
public record BTreeDiagnosticsData
{
public int Count;
public int Reads;
public int Writes;
public void Log()
{
Console.WriteLine($"Operations count: {Count}");
Console.WriteLine($"Reads: {Reads} Reads (Avg): {(double)Reads / Count}");
Console.WriteLine($"Writes: {Writes} Writes (Avg): {(double)Writes / Count}");
}
}
public abstract class DiagnostableBTreeBase<TKey, TValue, TNode, TVirtual, TActual> : IDiagnostableBTree<TKey, TValue, TNode, TVirtual, TActual>
where TKey : IComparable
where TNode : INode<TKey>
where TActual : IActualNode<TNode, TKey>, TNode
where TVirtual : IVirtualNode<TActual, TNode, TKey>, TNode
{
protected Dictionary<BTreeOperation, BTreeDiagnosticsData> _diagnosticsData = new Dictionary<BTreeOperation, BTreeDiagnosticsData>
{
{ BTreeOperation.Search, new BTreeDiagnosticsData() },
{ BTreeOperation.Insert, new BTreeDiagnosticsData() },
{ BTreeOperation.Delete, new BTreeDiagnosticsData() },
};
protected bool _watched;
public void Watch()
{
_watched = true;
}
public void ResetWatch()
{
_watched = false;
_diagnosticsData.Clear();
_diagnosticsData = new Dictionary<BTreeOperation, BTreeDiagnosticsData>
{
{ BTreeOperation.Search, new BTreeDiagnosticsData() },
{ BTreeOperation.Insert, new BTreeDiagnosticsData() },
{ BTreeOperation.Delete, new BTreeDiagnosticsData() },
};
}
protected virtual void DiskWrite(TNode node, BTreeOperation caller)
{
if (!_watched)
{
return;
}
_diagnosticsData[caller].Writes++;
}
protected virtual void DiskRead(TNode node, BTreeOperation caller)
{
if (!_watched)
{
return;
}
_diagnosticsData[caller].Reads++;
}
public BTreeDiagnosticsData Retrieve(BTreeOperation operation)
{
return _diagnosticsData.GetValueOrDefault(operation, new BTreeDiagnosticsData());
}
public abstract Option<TValue> Search(TKey key);
public abstract void Insert(TKey key, TValue value);
public abstract void Delete(TKey key);
public abstract void Clear();
public abstract IEnumerable<(TKey, TValue)> Traverse();
public event IBTree<TKey, TValue, TNode, TVirtual, TActual>.OnRootChangedEvent OnRootChanged;
protected void InvokeOnRootChanged(TActual node) => OnRootChanged?.Invoke(node);
public abstract void PrettyPrint();
}
}