Skip to content

Commit fb9cd77

Browse files
sync back library
1 parent cdc89e1 commit fb9cd77

31 files changed

Lines changed: 137 additions & 185 deletions

ExtendedSystemObjects/BiMap.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* PROJECT: ExtendedSystemObjects
44
* FILE: BiMap.cs
55
* PURPOSE: Bi-directional map implementation that allows for efficient lookups in both directions.
6-
* PROGRAMER: Peter Geinitz (Wayfarer)
6+
* PROGRAMMER: Peter Geinitz (Wayfarer)
77
*/
88

99
// ReSharper disable MemberCanBeInternal
@@ -31,12 +31,12 @@ public sealed class BiMap<T> : IReadOnlyCollection<KeyValuePair<T, T>> where T :
3131
/// <summary>
3232
/// The forward
3333
/// </summary>
34-
private readonly Dictionary<T, T> _forward = new();
34+
private readonly Dictionary<T, T> _forward;
3535

3636
/// <summary>
3737
/// The reverse
3838
/// </summary>
39-
private readonly Dictionary<T, T> _reverse = new();
39+
private readonly Dictionary<T, T> _reverse;
4040

4141
/// <inheritdoc />
4242
/// <summary>
@@ -80,9 +80,7 @@ public IEnumerator<KeyValuePair<T, T>> GetEnumerator()
8080
/// <summary>
8181
/// Initializes a new instance of the <see cref="T:ExtendedSystemObjects.BiMap`1" /> class.
8282
/// </summary>
83-
public BiMap() : this(0)
84-
{
85-
}
83+
public BiMap() : this(0) { }
8684

8785
/// <summary>
8886
/// Initializes a new instance of the <see cref="BiMap{T}"/> class.
@@ -195,4 +193,4 @@ public void Clear()
195193
}
196194
}
197195
}
198-
}
196+
}

ExtendedSystemObjects/CategorizedDictionary.cs

Lines changed: 28 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*
22
* COPYRIGHT: See COPYING in the top level directory
3-
* PROJECT: ExtendedSystemObjects
4-
* FILE: CategorizedDictionary.cs
5-
* PURPOSE: Extended Dictionary with a Category.
6-
* PROGRAMER: Peter Geinitz (Wayfarer)
3+
* PROJECT: ExtendedSystemObjects
4+
* FILE: CategorizedDictionary.cs
5+
* PURPOSE: Extended Dictionary with a Category.
6+
* PROGRAMMER: Peter Geinitz (Wayfarer)
77
*/
88

99
// ReSharper disable UnusedMethodReturnValue.Global
@@ -18,13 +18,14 @@
1818

1919
namespace ExtendedSystemObjects
2020
{
21+
/// <inheritdoc />
2122
/// <summary>
2223
/// A thread-safe dictionary that associates each key with a category.
2324
/// Provides fast lookups by key or by category.
2425
/// </summary>
2526
/// <typeparam name="TK">Type of dictionary keys.</typeparam>
2627
/// <typeparam name="TV">Type of dictionary values.</typeparam>
27-
/// <seealso cref="System.Collections.Generic.IEnumerable&lt;(TK Key, System.String Category, TV Value)&gt;" />
28+
/// <seealso cref="!:System.Collections.Generic.IEnumerable&lt;(TK Key, System.String Category, TV Value)&gt;" />
2829
[Serializable]
2930
public sealed class CategorizedDictionary<TK, TV> : IEnumerable<(TK Key, string Category, TV Value)>
3031
where TK : notnull
@@ -44,7 +45,7 @@ public sealed class CategorizedDictionary<TK, TV> : IEnumerable<(TK Key, string
4445
/// </summary>
4546
/// <param name="category">The category.</param>
4647
/// <returns>string Empty if category was empty.</returns>
47-
private static string NormalizeCategory(string category) => category ?? string.Empty;
48+
private static string NormalizeCategory(string? category) => category ?? string.Empty;
4849

4950
/// <summary>
5051
/// Lock for thread-safety.
@@ -72,14 +73,8 @@ public int Count
7273
get
7374
{
7475
_lock.EnterReadLock();
75-
try
76-
{
77-
return _data.Count;
78-
}
79-
finally
80-
{
81-
_lock.ExitReadLock();
82-
}
76+
try { return _data.Count; }
77+
finally { _lock.ExitReadLock(); }
8378
}
8479
}
8580

@@ -104,10 +99,7 @@ public TV this[TK key]
10499

105100
throw new KeyNotFoundException($"Key '{key}' not found.");
106101
}
107-
finally
108-
{
109-
_lock.ExitReadLock();
110-
}
102+
finally { _lock.ExitReadLock(); }
111103
}
112104
set
113105
{
@@ -123,10 +115,7 @@ public TV this[TK key]
123115
AddInternal(string.Empty, key, value);
124116
}
125117
}
126-
finally
127-
{
128-
_lock.ExitWriteLock();
129-
}
118+
finally { _lock.ExitWriteLock(); }
130119
}
131120
}
132121

@@ -146,14 +135,8 @@ public TV this[TK key]
146135
public void Add(string category, TK key, TV value)
147136
{
148137
_lock.EnterWriteLock();
149-
try
150-
{
151-
AddInternal(NormalizeCategory(category), key, value);
152-
}
153-
finally
154-
{
155-
_lock.ExitWriteLock();
156-
}
138+
try { AddInternal(NormalizeCategory(category), key, value); }
139+
finally { _lock.ExitWriteLock(); }
157140
}
158141

159142
/// <summary>
@@ -201,10 +184,7 @@ public bool Remove(TK key)
201184

202185
return true;
203186
}
204-
finally
205-
{
206-
_lock.ExitWriteLock();
207-
}
187+
finally { _lock.ExitWriteLock(); }
208188
}
209189

210190
/// <summary>
@@ -217,14 +197,8 @@ public bool Remove(TK key)
217197
public bool ContainsKey(TK key)
218198
{
219199
_lock.EnterReadLock();
220-
try
221-
{
222-
return _data.ContainsKey(key);
223-
}
224-
finally
225-
{
226-
_lock.ExitReadLock();
227-
}
200+
try { return _data.ContainsKey(key); }
201+
finally { _lock.ExitReadLock(); }
228202
}
229203

230204
/// <summary>
@@ -248,10 +222,7 @@ public bool TryGetValue(TK key, out TV value)
248222
value = default;
249223
return false;
250224
}
251-
finally
252-
{
253-
_lock.ExitReadLock();
254-
}
225+
finally { _lock.ExitReadLock(); }
255226
}
256227

257228
/// <summary>
@@ -275,10 +246,7 @@ public bool TryGetCategory(TK key, out string? category)
275246
category = null;
276247
return false;
277248
}
278-
finally
279-
{
280-
_lock.ExitReadLock();
281-
}
249+
finally { _lock.ExitReadLock(); }
282250
}
283251

284252
/// <summary>
@@ -297,10 +265,7 @@ public string GetCategory(TK key)
297265

298266
throw new KeyNotFoundException();
299267
}
300-
finally
301-
{
302-
_lock.ExitReadLock();
303-
}
268+
finally { _lock.ExitReadLock(); }
304269
}
305270

306271
/// <summary>
@@ -344,10 +309,7 @@ public bool SetCategory(TK key, string newCategory)
344309

345310
return true;
346311
}
347-
finally
348-
{
349-
_lock.ExitWriteLock();
350-
}
312+
finally { _lock.ExitWriteLock(); }
351313
}
352314

353315
/// <summary>
@@ -362,10 +324,7 @@ public IEnumerable<string> GetCategories()
362324
// Fix: Must snapshot keys inside the lock to allow safe iteration outside
363325
return new List<string>(_categories.Keys);
364326
}
365-
finally
366-
{
367-
_lock.ExitReadLock();
368-
}
327+
finally { _lock.ExitReadLock(); }
369328
}
370329

371330
/// <summary>
@@ -387,10 +346,7 @@ public IEnumerable<TK> GetKeys(string category)
387346

388347
return Array.Empty<TK>();
389348
}
390-
finally
391-
{
392-
_lock.ExitReadLock();
393-
}
349+
finally { _lock.ExitReadLock(); }
394350
}
395351

396352
/// <summary>
@@ -407,10 +363,7 @@ public IEnumerable<TK> GetKeys()
407363
// Fix: Must snapshot keys inside the lock
408364
return new List<TK>(_data.Keys);
409365
}
410-
finally
411-
{
412-
_lock.ExitReadLock();
413-
}
366+
finally { _lock.ExitReadLock(); }
414367
}
415368

416369
/// <summary>
@@ -428,10 +381,7 @@ public IEnumerable<TK> GetKeys()
428381

429382
return null;
430383
}
431-
finally
432-
{
433-
_lock.ExitReadLock();
434-
}
384+
finally { _lock.ExitReadLock(); }
435385
}
436386

437387

@@ -454,10 +404,7 @@ public Dictionary<TK, TV> GetCategory(string category)
454404
foreach (var key in keys) dict[key] = _data[key].Value;
455405
return dict;
456406
}
457-
finally
458-
{
459-
_lock.ExitReadLock();
460-
}
407+
finally { _lock.ExitReadLock(); }
461408
}
462409

463410
/// <summary>
@@ -471,10 +418,7 @@ public void Clear()
471418
_data.Clear();
472419
_categories.Clear();
473420
}
474-
finally
475-
{
476-
_lock.ExitWriteLock();
477-
}
421+
finally { _lock.ExitWriteLock(); }
478422
}
479423

480424
/// <summary>
@@ -494,10 +438,7 @@ public void Clear()
494438
foreach (var kvp in _data)
495439
snapshot.Add((kvp.Key, kvp.Value.Category, kvp.Value.Value));
496440
}
497-
finally
498-
{
499-
_lock.ExitReadLock();
500-
}
441+
finally { _lock.ExitReadLock(); }
501442

502443
// Iterate outside the lock to avoid deadlocks or contention
503444
foreach (var item in snapshot) yield return item;
@@ -511,4 +452,4 @@ public void Clear()
511452
/// </returns>
512453
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
513454
}
514-
}
455+
}

ExtendedSystemObjects/ExtendedDictionary.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* PROJECT: ExtendedSystemObjects
44
* FILE: DictionaryExtensions.cs
55
* PURPOSE: Helper class that extends the already versatile Dictionary, most operations are not thread safe, so beware.
6-
* PROGRAMER: Peter Geinitz (Wayfarer)
6+
* PROGRAMMER: Peter Geinitz (Wayfarer)
77
*/
88

99
// ReSharper disable UnusedMethodReturnValue.Global
@@ -276,7 +276,7 @@ public static List<TKey> GetKeysByValue<TKey, TValue>(this IDictionary<TKey, TVa
276276
/// </returns>
277277
/// <exception cref="ValueNotFoundException"><paramref name="dic" /> value not found.</exception>
278278
public static Dictionary<TKey, TValue> GetDictionaryByValues<TKey, TValue>(this IDictionary<TKey, TValue> dic,
279-
IEnumerable<TKey> value)
279+
IEnumerable<TKey> value) where TKey : notnull
280280
{
281281
var collection = value.Where(dic.ContainsKey).ToDictionary(key => key, key => dic[key]);
282282

@@ -337,7 +337,7 @@ public static void Swap<TKey, TValue>(this IDictionary<TKey, TValue> dic, TKey i
337337
/// <returns>
338338
/// [true] if success, else [false], Reduces Dictionary, first Element will be removed until it empty
339339
/// </returns>
340-
public static bool Reduce<TKey, TValue>(this Dictionary<TKey, TValue> dic)
340+
public static bool Reduce<TKey, TValue>(this Dictionary<TKey, TValue> dic) where TKey : notnull
341341
{
342342
return !dic.IsNullOrEmpty() && dic.Remove(dic.Keys.First());
343343
}
@@ -365,4 +365,4 @@ public static List<TValue> ToListId<TId, TValue>(this Dictionary<TId, TValue> di
365365
return lst;
366366
}
367367
}
368-
}
368+
}

ExtendedSystemObjects/ExtendedDouble.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* PROJECT: ExtendedSystemObjects
44
* FILE: ExtendedDouble.cs
55
* PURPOSE: Some Extensions for double
6-
* PROGRAMER: Peter Geinitz (Wayfarer)
6+
* PROGRAMMER: Peter Geinitz (Wayfarer)
77
*/
88

99
// ReSharper disable UnusedMember.Global
@@ -45,4 +45,4 @@ public static bool IsEqualTo(this double first, double second)
4545
return Math.Abs(first - second) < double.Epsilon;
4646
}
4747
}
48-
}
48+
}

ExtendedSystemObjects/ExtendedInt.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* PROJECT: ExtendedSystemObjects
44
* FILE: ExtendedInt.cs
55
* PURPOSE: Some Extensions for int
6-
* PROGRAMER: Peter Geinitz (Wayfarer)
6+
* PROGRAMMER: Peter Geinitz (Wayfarer)
77
*/
88

99
// ReSharper disable UnusedMember.Global
@@ -90,4 +90,4 @@ public static int RoundToInt(this float value)
9090
return (int)(value + 0.5f); // round up for positive values
9191
}
9292
}
93-
}
93+
}

ExtendedSystemObjects/ExtendedList.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* PROJECT: ExtendedSystemObjects
44
* FILE: ExtendedList.cs
55
* PURPOSE: Generic System Functions for Lists, most operations are not thread safe, so beware.
6-
* PROGRAMER: Peter Geinitz (Wayfarer)
6+
* PROGRAMMER: Peter Geinitz (Wayfarer)
77
*/
88

99
// ReSharper disable UnusedMember.Global
@@ -349,4 +349,4 @@ public static Dictionary<TId, TValue> ToDictionaryId<TValue, TId>(this IEnumerab
349349
return dct;
350350
}
351351
}
352-
}
352+
}

0 commit comments

Comments
 (0)