Skip to content

Commit 8004619

Browse files
author
LoneWandererProductions
committed
finish up documentation
1 parent 53d20ab commit 8004619

2 files changed

Lines changed: 104 additions & 9 deletions

File tree

ExtendedSystemObjects/MultiArray.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,30 @@
22
* COPYRIGHT: See COPYING in the top level directory
33
* PROJECT: ExtendedSystemObjects
44
* FILE: ExtendedSystemObjects/MultiArray.cs
5-
* PURPOSE: Some Extensions for Arrays, all generic
6-
* PROGRAMER: Peter Geinitz (Wayfarer)
5+
* PURPOSE: Utility extensions for 2D and jagged arrays, with focus on performance and unsafe access.
6+
* PROGRAMMER: Peter Geinitz (Wayfarer)
7+
*
8+
* DESCRIPTION:
9+
* This file contains a set of static helper methods to simplify working with multi-dimensional arrays.
10+
* It includes operations like deep cloning, row/column swapping, equality checks, span conversion,
11+
* and conversions between jagged and rectangular arrays using unsafe code for maximum performance.
712
*/
813

914
using System;
1015
using System.Text;
1116

1217
namespace ExtendedSystemObjects
1318
{
19+
/// <summary>
20+
/// Provides utility extensions for 2D arrays (`[,]`) and jagged arrays (`[][]`),
21+
/// including efficient operations such as swapping rows/columns, deep copying, comparing,
22+
/// and converting to spans or between formats.
23+
/// Designed for use with unmanaged types to leverage unsafe memory access for performance.
24+
/// </summary>
25+
/// <remarks>
26+
/// All methods are `static` and operate on arrays passed as parameters.
27+
/// Unsafe context is used in several methods to improve performance via pointer access.
28+
/// </remarks>
1429
public static class MultiArray
1530
{
1631
/// <summary>

ExtendedSystemObjects/SortedKvStore.cs

Lines changed: 87 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
// ReSharper disable MemberCanBePrivate.Global
1+
/*
2+
* COPYRIGHT: See COPYING in the top level directory
3+
* PROJECT: ExtendedSystemObjects
4+
* FILE: ExtendedSystemObjects/SortedKvStore.cs
5+
* PURPOSE: Represents a sorted key-value store with integer keys and integer values. Key must be unique. Occubied internally manages how to handle deletions.
6+
* PROGRAMER: Peter Geinitz (Wayfarer)
7+
*/
8+
9+
// ReSharper disable MemberCanBePrivate.Global
210
// ReSharper disable UnusedType.Global
311

412
using System;
@@ -8,29 +16,49 @@
816
namespace ExtendedSystemObjects
917
{
1018
/// <summary>
11-
///
19+
/// Represents a sorted key-value store with integer keys and integer values.
20+
/// Keys are kept sorted internally to allow efficient binary search operations.
21+
/// The class supports insertion, removal, and lookup operations with dynamic storage growth.
1222
/// </summary>
1323
/// <seealso cref="System.IDisposable" />
1424
public sealed class SortedKvStore : IDisposable
1525
{
26+
/// <summary>
27+
/// The keys
28+
/// </summary>
1629
private readonly IntArray _keys;
17-
private readonly IntArray _occupied; // 0/1 flags
30+
31+
/// <summary>
32+
/// The occupied Array, 0/1 flags
33+
/// </summary>
34+
private readonly IntArray _occupied;
35+
36+
/// <summary>
37+
/// The values
38+
/// </summary>
1839
private readonly IntArray _values;
1940

41+
/// <summary>
42+
/// Initializes a new instance of the <see cref="SortedKvStore"/> class with a specified initial capacity.
43+
/// </summary>
44+
/// <param name="initialCapacity">The initial capacity of the store.</param>
2045
public SortedKvStore(int initialCapacity = 16)
2146
{
2247
_keys = new IntArray(initialCapacity);
2348
_values = new IntArray(initialCapacity);
2449
_occupied = new IntArray(initialCapacity);
2550
}
2651

52+
/// <summary>
53+
/// Gets the number of active (occupied) key-value pairs stored.
54+
/// </summary>
2755
public int Count { get; private set; }
2856

2957
/// <summary>
30-
/// Gets the keys.
58+
/// Gets an enumerable collection of all keys currently in the store.
3159
/// </summary>
3260
/// <value>
33-
/// The keys.
61+
/// The keys of the key-value pairs.
3462
/// </value>
3563
public IEnumerable<int> Keys
3664
{
@@ -44,6 +72,19 @@ public IEnumerable<int> Keys
4472
}
4573
}
4674

75+
/// <summary>
76+
/// Gets or sets the value associated with the specified key.
77+
/// If the key does not exist on get, a <see cref="KeyNotFoundException" /> is thrown.
78+
/// If the key exists on set, its value is updated; otherwise, the key-value pair is added.
79+
/// </summary>
80+
/// <value>
81+
/// The <see cref="System.Int32"/>.
82+
/// </value>
83+
/// <param name="key">The key to locate or add.</param>
84+
/// <returns>
85+
/// The value associated with the specified key.
86+
/// </returns>
87+
/// <exception cref="System.Collections.Generic.KeyNotFoundException">Key {key} not found.</exception>
4788
public int this[int key]
4889
{
4990
get
@@ -59,6 +100,11 @@ public int this[int key]
59100
}
60101
}
61102

103+
/// <summary>
104+
/// Adds a new key-value pair to the store, or updates the value if the key already exists.
105+
/// </summary>
106+
/// <param name="key">The key to add or update.</param>
107+
/// <param name="value">The value associated with the key.</param>
62108
public void Add(int key, int value)
63109
{
64110
var idx = BinarySearch(key);
@@ -90,6 +136,12 @@ public void Add(int key, int value)
90136
Count++;
91137
}
92138

139+
/// <summary>
140+
/// Tries to get the value associated with the specified key.
141+
/// </summary>
142+
/// <param name="key">The key to locate.</param>
143+
/// <param name="value">When this method returns, contains the value associated with the key, if found; otherwise, the default value.</param>
144+
/// <returns><c>true</c> if the key was found; otherwise, <c>false</c>.</returns>
93145
public bool TryGet(int key, out int value)
94146
{
95147
int left = 0, right = Count - 1;
@@ -126,6 +178,10 @@ public bool TryGet(int key, out int value)
126178
return false;
127179
}
128180

181+
/// <summary>
182+
/// Marks the specified key as removed. The item is not physically removed until <see cref="Compact" /> is called.
183+
/// </summary>
184+
/// <param name="key">The key to remove.</param>
129185
public void Remove(int key)
130186
{
131187
var idx = BinarySearch(key);
@@ -135,6 +191,12 @@ public void Remove(int key)
135191
}
136192
}
137193

194+
/// <summary>
195+
/// Tries to remove the specified key.
196+
/// </summary>
197+
/// <param name="key">The key to remove.</param>
198+
/// <param name="index">When this method returns, contains the index of the removed key if successful; otherwise, -1.</param>
199+
/// <returns><c>true</c> if the key was removed; otherwise, <c>false</c>.</returns>
138200
public bool TryRemove(int key, out int index)
139201
{
140202
index = BinarySearch(key);
@@ -148,6 +210,11 @@ public bool TryRemove(int key, out int index)
148210
return false;
149211
}
150212

213+
/// <summary>
214+
/// Removes multiple keys in one batch. Uses optimized path if the input span is sorted.
215+
/// Keys are marked as unoccupied but not physically removed.
216+
/// </summary>
217+
/// <param name="keysToRemove">A span of keys to remove.</param>
151218
public void RemoveMany(ReadOnlySpan<int> keysToRemove)
152219
{
153220
if (keysToRemove.Length == 0)
@@ -211,6 +278,10 @@ public void RemoveMany(ReadOnlySpan<int> keysToRemove)
211278
}
212279
}
213280

281+
/// <summary>
282+
/// Physically removes all unoccupied entries to compact the underlying arrays.
283+
/// Reduces memory usage and improves lookup performance.
284+
/// </summary>
214285
public void Compact()
215286
{
216287
var occSpan = _occupied.AsSpan()[..Count];
@@ -245,7 +316,7 @@ public void Compact()
245316
}
246317

247318
/// <summary>
248-
/// Clears this instance.
319+
/// Removes all entries from the store.
249320
/// </summary>
250321
public void Clear()
251322
{
@@ -272,6 +343,9 @@ public void Dispose()
272343
_occupied.Dispose();
273344
}
274345

346+
/// <summary>
347+
/// Ensures the underlying arrays have sufficient capacity to hold at least one more entry.
348+
/// </summary>
275349
private void EnsureCapacity()
276350
{
277351
// Delegate to IntArray.EnsureCapacity, using Count + 1 since we add one item
@@ -280,10 +354,16 @@ private void EnsureCapacity()
280354
_occupied.EnsureCapacity(Count + 1);
281355
}
282356

357+
/// <summary>
358+
/// Performs a binary search for the specified key.
359+
/// </summary>
360+
/// <param name="key">The key to locate.</param>
361+
/// <returns>
362+
/// The index of the key if found; otherwise, the bitwise complement of the index at which the key should be inserted.
363+
/// </returns>
283364
private int BinarySearch(int key)
284365
{
285366
return Utility.BinarySearch(_keys.AsSpan(), Count, key);
286367
}
287-
288368
}
289369
}

0 commit comments

Comments
 (0)