-
Notifications
You must be signed in to change notification settings - Fork 2
/
NestedStringDictionary.cs
240 lines (208 loc) · 8.22 KB
/
NestedStringDictionary.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
using System;
using System.Collections.Generic;
using System.Linq;
namespace ValidateFastaFile
{
/// <summary>
/// This class implements a dictionary where keys are strings and values are type T (for example string or integer)
/// Internally it uses a set of dictionaries to track the data, binning the data into separate dictionaries
/// based on the first few letters of the keys of an added key/value pair
/// </summary>
/// <typeparam name="T">Type for values</typeparam>
public class NestedStringDictionary<T>
{
private readonly Dictionary<string, Dictionary<string, T>> mData;
private readonly StringComparer mComparer;
/// <summary>
/// Number of items stored with Add()
/// </summary>
public int Count
{
get
{
return mData.Values.Sum(subDictionary => subDictionary.Count);
}
}
// ReSharper disable once UnusedMember.Global
/// <summary>
/// True when we are ignoring case for stored keys
/// </summary>
public bool IgnoreCase { get; }
/// <summary>
/// The number of characters at the start of keyStrings to use when adding items to NestedStringDictionary instances
/// </summary>
/// <remarks>
/// If this value is too short, all of the items added to the NestedStringDictionary instance
/// will be tracked by the same dictionary, which could result in a dictionary surpassing the 2 GB boundary
/// </remarks>
public byte SpannerCharLength { get; }
/// <summary>
/// Constructor
/// </summary>
/// <remarks>
/// If spannerCharLength is too small, all of the items added to this class instance using Add() will be
/// tracked by the same dictionary, which could result in a dictionary surpassing the 2 GB boundary
/// </remarks>
/// <param name="ignoreCaseForKeys">True to create case-insensitive dictionaries (and thus ignore differences between uppercase and lowercase letters)</param>
/// <param name="spannerCharLength"></param>
public NestedStringDictionary(bool ignoreCaseForKeys = false, byte spannerCharLength = 1)
{
IgnoreCase = ignoreCaseForKeys;
if (IgnoreCase)
{
mComparer = StringComparer.OrdinalIgnoreCase;
}
else
{
mComparer = StringComparer.Ordinal;
}
mData = new Dictionary<string, Dictionary<string, T>>(mComparer);
if (spannerCharLength < 1)
{
SpannerCharLength = 1;
}
else
{
SpannerCharLength = spannerCharLength;
}
}
/// <summary>
/// Store a key and its associated value
/// </summary>
/// <param name="key">String to store</param>
/// <param name="value">Value of type T</param>
/// <exception cref="System.ArgumentException">Thrown if the key has already been stored</exception>
public void Add(string key, T value)
{
var spannerKey = GetSpannerKey(key);
if (!mData.TryGetValue(spannerKey, out var subDictionary))
{
subDictionary = new Dictionary<string, T>(mComparer);
mData.Add(spannerKey, subDictionary);
}
subDictionary.Add(key, value);
}
/// <summary>
/// Remove the stored items
/// </summary>
public void Clear()
{
foreach (var item in mData)
{
item.Value.Clear();
}
mData.Clear();
}
/// <summary>
/// Check for the existence of a key
/// </summary>
/// <param name="key"></param>
/// <returns>True if the key exists, otherwise false</returns>
public bool ContainsKey(string key)
{
var spannerKey = GetSpannerKey(key);
if (mData.TryGetValue(spannerKey, out var subDictionary))
{
return subDictionary.ContainsKey(key);
}
return false;
}
/// <summary>
/// Return a string summarizing the number of items in the dictionary associated with each spanning key
/// </summary>
/// <remarks>
/// Example return strings:
/// 1 spanning key: 'a' with 1 item
/// 2 spanning keys: 'a' with 1 item and 'o' with 1 item
/// 3 spanning keys: including 'a' with 1 item, 'o' with 1 item, and 'p' with 1 item
/// 5 spanning keys: including 'a' with 2 items, 'p' with 2 items, and 'w' with 1 item
/// </remarks>
/// <returns>String description of the stored data</returns>
public string GetSizeSummary()
{
var summary = mData.Keys.Count + " spanning keys";
var keyNames = mData.Keys.ToList();
keyNames.Sort(mComparer);
if (keyNames.Count == 1)
{
summary = "1 spanning key: " +
GetSpanningKeyDescription(keyNames[0]);
}
else if (keyNames.Count == 2)
{
summary += ": " +
GetSpanningKeyDescription(keyNames[0]) + " and " +
GetSpanningKeyDescription(keyNames[1]);
}
else if (keyNames.Count > 2)
{
var midPoint = keyNames.Count / 2;
summary += ": including " +
GetSpanningKeyDescription(keyNames[0]) + ", " +
GetSpanningKeyDescription(keyNames[midPoint]) + ", and " +
GetSpanningKeyDescription(keyNames[keyNames.Count - 1]);
}
return summary;
}
private string GetSpanningKeyDescription(string keyName)
{
var keyDescription = "'" + keyName + "' with " + mData[keyName].Values.Count + " item";
if (mData[keyName].Values.Count == 1)
{
return keyDescription;
}
return keyDescription + "s";
}
// ReSharper disable once UnusedMember.Global
/// <summary>
/// Retrieve the dictionary associated with the given spanner key
/// </summary>
/// <param name="keyName"></param>
/// <returns>The dictionary, or nothing if the key is not found</returns>
public Dictionary<string, T> GetDictionaryForSpanningKey(string keyName)
{
if (mData.TryGetValue(keyName, out var subDictionary))
{
return subDictionary;
}
return null;
}
// ReSharper disable once UnusedMember.Global
/// <summary>
/// Retrieve the list of spanning keys in use
/// </summary>
/// <returns>List of keys</returns>
public List<string> GetSpanningKeys()
{
return mData.Keys.ToList();
}
/// <summary>
/// Try to get the value associated with the key
/// </summary>
/// <param name="key">Key to find</param>
/// <param name="value">Value found, or nothing if no match</param>
/// <returns>True if a match was found, otherwise nothing</returns>
public bool TryGetValue(string key, out T value)
{
var spannerKey = GetSpannerKey(key);
if (mData.TryGetValue(spannerKey, out var subDictionary))
{
return subDictionary.TryGetValue(key, out value);
}
value = default;
return false;
}
private string GetSpannerKey(string key)
{
if (key == null)
{
throw new ArgumentNullException(nameof(key), "Key cannot be null");
}
if (key.Length <= SpannerCharLength)
{
return key;
}
return key.Substring(0, SpannerCharLength);
}
}
}