You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: ExtendedSystemObjects/SortedKvStore.cs
+87-7Lines changed: 87 additions & 7 deletions
Original file line number
Diff line number
Diff 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
2
10
// ReSharper disable UnusedType.Global
3
11
4
12
usingSystem;
@@ -8,29 +16,49 @@
8
16
namespaceExtendedSystemObjects
9
17
{
10
18
/// <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.
12
22
/// </summary>
13
23
/// <seealso cref="System.IDisposable" />
14
24
publicsealedclassSortedKvStore:IDisposable
15
25
{
26
+
/// <summary>
27
+
/// The keys
28
+
/// </summary>
16
29
privatereadonlyIntArray_keys;
17
-
privatereadonlyIntArray_occupied;// 0/1 flags
30
+
31
+
/// <summary>
32
+
/// The occupied Array, 0/1 flags
33
+
/// </summary>
34
+
privatereadonlyIntArray_occupied;
35
+
36
+
/// <summary>
37
+
/// The values
38
+
/// </summary>
18
39
privatereadonlyIntArray_values;
19
40
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>
20
45
publicSortedKvStore(intinitialCapacity=16)
21
46
{
22
47
_keys=newIntArray(initialCapacity);
23
48
_values=newIntArray(initialCapacity);
24
49
_occupied=newIntArray(initialCapacity);
25
50
}
26
51
52
+
/// <summary>
53
+
/// Gets the number of active (occupied) key-value pairs stored.
54
+
/// </summary>
27
55
publicintCount{get;privateset;}
28
56
29
57
/// <summary>
30
-
/// Gets the keys.
58
+
/// Gets an enumerable collection of all keys currently in the store.
31
59
/// </summary>
32
60
/// <value>
33
-
/// The keys.
61
+
/// The keys of the key-value pairs.
34
62
/// </value>
35
63
publicIEnumerable<int>Keys
36
64
{
@@ -44,6 +72,19 @@ public IEnumerable<int> Keys
44
72
}
45
73
}
46
74
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>
47
88
publicintthis[intkey]
48
89
{
49
90
get
@@ -59,6 +100,11 @@ public int this[int key]
59
100
}
60
101
}
61
102
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>
62
108
publicvoidAdd(intkey,intvalue)
63
109
{
64
110
varidx=BinarySearch(key);
@@ -90,6 +136,12 @@ public void Add(int key, int value)
90
136
Count++;
91
137
}
92
138
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>
93
145
publicboolTryGet(intkey,outintvalue)
94
146
{
95
147
intleft=0,right=Count-1;
@@ -126,6 +178,10 @@ public bool TryGet(int key, out int value)
126
178
returnfalse;
127
179
}
128
180
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>
129
185
publicvoidRemove(intkey)
130
186
{
131
187
varidx=BinarySearch(key);
@@ -135,6 +191,12 @@ public void Remove(int key)
135
191
}
136
192
}
137
193
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>
138
200
publicboolTryRemove(intkey,outintindex)
139
201
{
140
202
index=BinarySearch(key);
@@ -148,6 +210,11 @@ public bool TryRemove(int key, out int index)
148
210
returnfalse;
149
211
}
150
212
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>
0 commit comments