Skip to content

Commit b6f8fd3

Browse files
authored
Switches StoragePair to Tuples (#78)
Needed a custom comparer to make it work.
1 parent fcdb50f commit b6f8fd3

File tree

2 files changed

+32
-39
lines changed

2 files changed

+32
-39
lines changed

src/FRC.NetworkTables/Storage.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,6 @@ namespace NetworkTables
1313
{
1414
internal partial class Storage : IDisposable
1515
{
16-
internal struct StoragePair : IComparable<StoragePair>
17-
{
18-
public string First { get; }
19-
public Value Second { get; }
20-
21-
public StoragePair(string first, Value second)
22-
{
23-
First = first;
24-
Second = second;
25-
}
26-
27-
public int CompareTo(StoragePair other)
28-
{
29-
return string.Compare(First, other.First, StringComparison.Ordinal);
30-
}
31-
}
32-
3316
private static Storage s_instance;
3417

3518
public static Storage Instance

src/FRC.NetworkTables/StorageSaving.cs

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,17 @@ namespace NetworkTables
1212
{
1313
internal partial class Storage
1414
{
15-
private bool GetPersistentEntries(bool periodic, List<StoragePair> entries)
15+
private class StoragePairComparer : IComparer<(string key, Value value)>
16+
{
17+
public int Compare((string key, Value value) x, (string key, Value value) y)
18+
{
19+
return string.Compare(x.key, y.key, StringComparison.Ordinal);
20+
}
21+
}
22+
23+
private readonly static StoragePairComparer s_storageComparer = new StoragePairComparer();
24+
25+
private bool GetPersistentEntries(bool periodic, List<(string key, Value value)> entries)
1626
{
1727
using (m_monitor.Enter())
1828
{
@@ -22,19 +32,19 @@ private bool GetPersistentEntries(bool periodic, List<StoragePair> entries)
2232
{
2333
Entry entry = i.Value;
2434
if (!entry.IsPersistent()) continue;
25-
entries.Add(new StoragePair(i.Key, entry.Value));
35+
entries.Add((i.Key, entry.Value));
2636
}
2737
}
28-
entries.Sort();
38+
entries.Sort(s_storageComparer);
2939
return true;
3040
}
3141

32-
private static async Task SavePersistentImpl(StreamWriter stream, IEnumerable<StoragePair> entries)
42+
private static async Task SavePersistentImpl(StreamWriter stream, IEnumerable<(string key, Value value)> entries)
3343
{
3444
await stream.WriteAsync("[NetworkTables Storage 3.0]\n").ConfigureAwait(false);
3545
foreach (var i in entries)
3646
{
37-
var v = i.Second;
47+
var v = i.value;
3848
if (v == null) continue;
3949
switch (v.Type)
4050
{
@@ -63,7 +73,7 @@ private static async Task SavePersistentImpl(StreamWriter stream, IEnumerable<St
6373
continue;
6474
}
6575

66-
await WriteStringAsync(stream, i.First).ConfigureAwait(false);
76+
await WriteStringAsync(stream, i.key).ConfigureAwait(false);
6777

6878
await stream.WriteAsync('=').ConfigureAwait(false);
6979

@@ -260,7 +270,7 @@ private static void UnescapeString(string source, out string dest)
260270

261271
public void SavePersistent(Stream stream, bool periodic)
262272
{
263-
List<StoragePair> entries = new List<StoragePair>();
273+
List<(string key, Value value)> entries = new List<(string key, Value value)>();
264274
if (!GetPersistentEntries(periodic, entries)) return;
265275
StreamWriter w = new StreamWriter(stream);
266276
Task task = SavePersistentImpl(w, entries);
@@ -281,7 +291,7 @@ public string SavePersistent(string filename, bool periodic)
281291
bak += ".bak";
282292

283293
//Get entries before creating files
284-
List<StoragePair> entries = new List<StoragePair>();
294+
List<(string key, Value value)> entries = new List<(string key, Value value)>();
285295
if (!GetPersistentEntries(periodic, entries)) return null;
286296

287297

@@ -349,7 +359,7 @@ public async Task<string> SavePersistentAsync(string filename, bool periodic)
349359
bak += ".bak";
350360

351361
//Get entries before creating files
352-
List<StoragePair> entries = new List<StoragePair>();
362+
List<(string key, Value value)> entries = new List<(string key, Value value)>();
353363
if (!GetPersistentEntries(periodic, entries)) return null;
354364

355365
string err = null;
@@ -523,7 +533,7 @@ public async Task<bool> LoadPersistentAsync(Stream stream, Action<int, string> w
523533
{
524534
int lineNum = 1;
525535

526-
List<StoragePair> entries = new List<StoragePair>();
536+
List<(string key, Value value)> entries = new List<(string key, Value value)>();
527537

528538
List<bool> boolArray = new List<bool>();
529539
List<double> doubleArray = new List<double>();
@@ -657,7 +667,7 @@ public async Task<bool> LoadPersistentAsync(Stream stream, Action<int, string> w
657667
}
658668
if (name.Length != 0 && value != null)
659669
{
660-
entries.Add(new StoragePair(name, value));
670+
entries.Add((name, value));
661671
}
662672

663673
}
@@ -670,13 +680,13 @@ public async Task<bool> LoadPersistentAsync(Stream stream, Action<int, string> w
670680
monitor = await m_monitor.EnterAsync().ConfigureAwait(false);
671681
foreach (var i in entries)
672682
{
673-
if (!m_entries.TryGetValue(i.First, out Entry entry))
683+
if (!m_entries.TryGetValue(i.key, out Entry entry))
674684
{
675-
entry = new Entry(i.First);
676-
m_entries.Add(i.First, entry);
685+
entry = new Entry(i.key);
686+
m_entries.Add(i.key, entry);
677687
}
678688
var oldValue = entry.Value;
679-
entry.Value = i.Second;
689+
entry.Value = i.value;
680690
bool wasPersist = entry.IsPersistent();
681691
if (!wasPersist) entry.Flags |= EntryFlags.Persistent;
682692

@@ -691,28 +701,28 @@ public async Task<bool> LoadPersistentAsync(Stream stream, Action<int, string> w
691701
{
692702
if (oldValue != null)
693703
{
694-
m_notifier.NotifyEntry(i.First, i.Second, (NotifyFlags.NotifyNew | NotifyFlags.NotifyLocal));
704+
m_notifier.NotifyEntry(i.key, i.value, (NotifyFlags.NotifyNew | NotifyFlags.NotifyLocal));
695705
}
696-
else if (oldValue != i.Second)
706+
else if (oldValue != i.value)
697707
{
698708
NotifyFlags notifyFlags = NotifyFlags.NotifyUpdate | NotifyFlags.NotifyLocal;
699709
if (!wasPersist) notifyFlags |= NotifyFlags.NotifyFlagsChanged;
700-
m_notifier.NotifyEntry(i.First, i.Second, notifyFlags);
710+
m_notifier.NotifyEntry(i.key, i.value, notifyFlags);
701711
}
702712
}
703713

704714
if (m_queueOutgoing == null) continue;
705715
++entry.SeqNum;
706716

707-
if (oldValue == null || oldValue.Type != i.Second.Type)
717+
if (oldValue == null || oldValue.Type != i.value.Type)
708718
{
709-
msgs.Add(Message.EntryAssign(i.First, entry.Id, entry.SeqNum.Value, i.Second, entry.Flags));
719+
msgs.Add(Message.EntryAssign(i.key, entry.Id, entry.SeqNum.Value, i.value, entry.Flags));
710720
}
711721
else if (entry.Id != 0xffff)
712722
{
713-
if (oldValue != i.Second)
723+
if (oldValue != i.value)
714724
{
715-
msgs.Add(Message.EntryUpdate(entry.Id, entry.SeqNum.Value, i.Second));
725+
msgs.Add(Message.EntryUpdate(entry.Id, entry.SeqNum.Value, i.value));
716726
}
717727
if (!wasPersist)
718728
msgs.Add(Message.FlagsUpdate(entry.Id, entry.Flags));

0 commit comments

Comments
 (0)