Skip to content

Commit f0801e6

Browse files
cleanup extended objects
1 parent d52ff49 commit f0801e6

15 files changed

Lines changed: 51 additions & 45 deletions

ExtendedSystemObjects/BiMap.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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>

ExtendedSystemObjects/CategorizedDictionary.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@
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]
29-
public sealed class CategorizedDictionary<TK, TV> : IEnumerable<(TK Key, string Category, TV Value)>
30+
public sealed class CategorizedDictionary<TK, TV> : IEnumerable<(TK Key, string Category, TV Value)> where TK : notnull
3031
{
3132
/// <summary>
3233
/// Internal storage mapping keys to (Category, Value) pairs.
@@ -43,7 +44,7 @@ public sealed class CategorizedDictionary<TK, TV> : IEnumerable<(TK Key, string
4344
/// </summary>
4445
/// <param name="category">The category.</param>
4546
/// <returns>string Empty if category was empty.</returns>
46-
private static string NormalizeCategory(string category) => category ?? string.Empty;
47+
private static string NormalizeCategory(string? category) => category ?? string.Empty;
4748

4849
/// <summary>
4950
/// Lock for thread-safety.

ExtendedSystemObjects/ExtendedDictionary.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

@@ -296,7 +296,7 @@ public static Dictionary<TKey, TValue> GetDictionaryByValues<TKey, TValue>(this
296296
/// <typeparam name="TValue">Internal Value</typeparam>
297297
/// <param name="dic">Internal Target Dictionary</param>
298298
/// <returns>Clone of the Input Dictionary</returns>
299-
public static Dictionary<TKey, TValue>? Clone<TKey, TValue>(this IDictionary<TKey, TValue> dic)
299+
public static Dictionary<TKey, TValue>? Clone<TKey, TValue>(this IDictionary<TKey, TValue>? dic)
300300
where TKey : notnull
301301
{
302302
return dic?.ToDictionary(dctClone => dctClone.Key, dctClone => dctClone.Value);
@@ -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
}

ExtendedSystemObjects/ExtendedList.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public static void RemoveAtFast<TValue>(this List<TValue> list, int index)
118118
/// <typeparam name="TValue">The type of the value.</typeparam>
119119
/// <param name="lst">The List.</param>
120120
/// <returns>A Dictionary from a list with int as the key</returns>
121-
public static Dictionary<int, TValue>? ToDictionary<TValue>(this IEnumerable<TValue> lst)
121+
public static Dictionary<int, TValue>? ToDictionary<TValue>(this IEnumerable<TValue>? lst)
122122
{
123123
var index = 0;
124124
return lst?.ToDictionary(_ => index++);

ExtendedSystemObjects/FastLinq.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public static bool AnyFast<T>(this List<T>? list)
178178
/// <param name="array">The array.</param>
179179
/// <returns>True if the collection contains at least one element; otherwise false.</returns>
180180
[MethodImpl(MethodImplOptions.AggressiveInlining)]
181-
public static bool AnyFast<T>(this T[] array)
181+
public static bool AnyFast<T>(this T[]? array)
182182
{
183183
return array != null && array.Length != 0;
184184
}

ExtendedSystemObjects/ImmutableLookupMapUnmanaged.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
* PROGRAMMER: Peter Geinitz (Wayfarer)
88
*/
99

10+
// ReSharper disable MemberCanBeInternal
11+
// ReSharper disable MemberCanBePrivate.Global
12+
1013
using System;
1114
using System.Collections;
1215
using System.Collections.Generic;
@@ -122,12 +125,12 @@ public ImmutableLookupMapUnmanaged(IDictionary<TKey, TValue> data)
122125
/// </summary>
123126
public void Dispose()
124127
{
125-
if(_disposed) return;
128+
if (_disposed) return;
129+
126130
_entries.Dispose();
127131
_disposed = true;
128132
}
129133

130-
/// <inheritdoc />
131134
/// <summary>
132135
/// Gets the value associated with the specified key.
133136
/// </summary>
@@ -219,18 +222,24 @@ IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.
219222
return GetEnumerator();
220223
}
221224

225+
/// <inheritdoc />
222226
/// <summary>
223227
/// High performance enumerator for iterating over the entries in the lookup map. Uses unsafe code and pointer arithmetic for maximum speed.
224228
/// </summary>
225-
/// <seealso cref="System.IDisposable" />
226-
/// <seealso cref="System.Collections.Generic.IEnumerable&lt;System.Collections.Generic.KeyValuePair&lt;TKey, TValue&gt;&gt;" />
227-
public unsafe struct Enumerator : IEnumerator<KeyValuePair<TKey, TValue>>
229+
/// <seealso cref="T:System.IDisposable" />
230+
/// <seealso cref="!:System.Collections.Generic.IEnumerable&lt;System.Collections.Generic.KeyValuePair&lt;TKey, TValue&gt;&gt;" />
231+
public struct Enumerator : IEnumerator<KeyValuePair<TKey, TValue>>
228232
{
229233
private readonly Entry* _entries;
230234
private readonly int _capacity;
231235
private int _index;
232236
private KeyValuePair<TKey, TValue> _current;
233237

238+
/// <summary>
239+
/// Initializes a new instance of the <see cref="Enumerator"/> struct.
240+
/// </summary>
241+
/// <param name="entries">The entries.</param>
242+
/// <param name="capacity">The capacity.</param>
234243
[MethodImpl(MethodImplOptions.AggressiveInlining)]
235244
internal Enumerator(Entry* entries, int capacity)
236245
{
@@ -244,7 +253,6 @@ internal Enumerator(Entry* entries, int capacity)
244253
[MethodImpl(MethodImplOptions.AggressiveInlining)]
245254
public bool MoveNext()
246255
{
247-
// Nutzt superschnelle Pointer-Arithmetik statt des Array-Indexers
248256
while (++_index < _capacity)
249257
{
250258
Entry* entry = _entries + _index;
@@ -254,6 +262,7 @@ public bool MoveNext()
254262
return true;
255263
}
256264
}
265+
257266
return false;
258267
}
259268

@@ -267,6 +276,7 @@ public readonly KeyValuePair<TKey, TValue> Current
267276
/// <inheritdoc />
268277
readonly object IEnumerator.Current => Current;
269278

279+
/// <inheritdoc />
270280
/// <summary>
271281
/// Sets the enumerator to its initial position, which is before the first element in the collection.
272282
/// </summary>
@@ -276,6 +286,7 @@ public void Reset()
276286
_current = default;
277287
}
278288

289+
/// <inheritdoc />
279290
/// <summary>
280291
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
281292
/// </summary>

ExtendedSystemObjects/MemoryVault.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ private void EnsureNotDisposed()
406406
/// Decrements the memory.
407407
/// </summary>
408408
/// <param name="item">The item.</param>
409-
private void DecrementMemory(VaultItem<TU> item)
409+
private void DecrementMemory(VaultItem<TU?> item)
410410
{
411411
var size = item.DataSize + (item.Description?.Length * 2 ?? 0);
412412
// Add additional metadata estimate if it exists

ExtendedSystemObjects/UnmanagedArray.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ namespace ExtendedSystemObjects
2727
/// <seealso cref="T:System.IDisposable" />
2828
public sealed unsafe class UnmanagedArray<T> : IUnmanagedArray<T>, IEnumerable<T> where T : unmanaged
2929
{
30-
/// <summary>
31-
/// The buffer
32-
/// </summary>
33-
private IntPtr _buffer;
34-
3530
/// <summary>
3631
/// Check if we disposed the object
3732
/// </summary>
@@ -60,7 +55,7 @@ public UnmanagedArray(int size)
6055
Length = size;
6156

6257
_ptr = (T*)UnmanagedMemoryHelper.Allocate<T>(size);
63-
UnmanagedMemoryHelper.Clear<T>(_ptr, size);
58+
UnmanagedMemoryHelper.Clear(_ptr, size);
6459
}
6560

6661
/// <summary>
@@ -197,11 +192,11 @@ public void Resize(int newSize)
197192

198193
if (newSize == Capacity) return;
199194

200-
_ptr = UnmanagedMemoryHelper.Reallocate<T>(_ptr, newSize);
195+
_ptr = UnmanagedMemoryHelper.Reallocate(_ptr, newSize);
201196

202197
if (newSize > Capacity)
203198
{
204-
UnmanagedMemoryHelper.Clear<T>(_ptr + Capacity, newSize - Capacity);
199+
UnmanagedMemoryHelper.Clear(_ptr + Capacity, newSize - Capacity);
205200
}
206201

207202
Capacity = newSize;
@@ -219,7 +214,7 @@ public void Resize(int newSize)
219214
public void Clear()
220215
{
221216
EnsureNotDisposed();
222-
UnmanagedMemoryHelper.Clear<T>(_ptr, Length);
217+
UnmanagedMemoryHelper.Clear(_ptr, Length);
223218
}
224219

225220
/// <inheritdoc />

ExtendedSystemObjects/UnmanagedIntArray.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,14 @@ public void Resize(int newSize)
149149
}
150150

151151
// 1. Reallocate directly into the typed pointer. No more IntPtr middleman.
152-
_ptr = UnmanagedMemoryHelper.Reallocate<int>(_ptr, newSize);
152+
_ptr = UnmanagedMemoryHelper.Reallocate(_ptr, newSize);
153153

154154
// If growing, clear the newly allocated portion
155155
if (newSize > Capacity)
156156
{
157157
// 2. Clean pointer arithmetic. '_ptr + Capacity' automatically moves the pointer
158158
// by the correct byte offset because '_ptr' is strongly typed as int*.
159-
UnmanagedMemoryHelper.Clear<int>(_ptr + Capacity, newSize - Capacity);
159+
UnmanagedMemoryHelper.Clear(_ptr + Capacity, newSize - Capacity);
160160
}
161161

162162
Capacity = newSize;
@@ -174,7 +174,7 @@ public void Resize(int newSize)
174174
public void Clear()
175175
{
176176
// No casting, no IntPtr, just pure native clearing.
177-
UnmanagedMemoryHelper.Clear<int>(_ptr, Length);
177+
UnmanagedMemoryHelper.Clear(_ptr, Length);
178178
}
179179

180180
/// <inheritdoc />

ExtendedSystemObjects/UnmanagedIntList.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public void Clear()
203203
Length = 0;
204204

205205
// Clear the entire allocated capacity, not just Length items
206-
UnmanagedMemoryHelper.Clear<int>(_ptr, Capacity);
206+
UnmanagedMemoryHelper.Clear(_ptr, Capacity);
207207
}
208208

209209
/// <inheritdoc />
@@ -425,7 +425,7 @@ public void TrimExcess()
425425
return;
426426
}
427427

428-
_ptr = UnmanagedMemoryHelper.Reallocate<int>(_ptr, Length);
428+
_ptr = UnmanagedMemoryHelper.Reallocate(_ptr, Length);
429429
Capacity = Length;
430430
}
431431

@@ -566,7 +566,7 @@ private void EnsureCapacity(int min)
566566
newCapacity = min;
567567
}
568568

569-
_ptr = UnmanagedMemoryHelper.Reallocate<int>(_ptr, newCapacity);
569+
_ptr = UnmanagedMemoryHelper.Reallocate(_ptr, newCapacity);
570570
Capacity = newCapacity;
571571
}
572572
}

0 commit comments

Comments
 (0)