| | 1 | | namespace Towel.DataStructures; |
| | 2 | |
|
| | 3 | | /// <summary>A map between instances of two types. The polymorphism base for Map implementations in Towel.</summary> |
| | 4 | | /// <typeparam name="T">The generic type to be stored in this data structure.</typeparam> |
| | 5 | | /// <typeparam name="TKey">The type of keys used to look up items in this structure.</typeparam> |
| | 6 | | public interface IMap<T, TKey> : IDataStructure<T>, |
| | 7 | | DataStructure.ICountable, |
| | 8 | | DataStructure.IClearable, |
| | 9 | | DataStructure.IAuditable<TKey>, |
| | 10 | | DataStructure.IRemovable<TKey> |
| | 11 | | { |
| | 12 | | #region Properties |
| | 13 | |
|
| | 14 | | /// <summary>Allows indexed look-up of the structure. (Set does not replace the Add() method)</summary> |
| | 15 | | /// <param name="key">The "index" to access of the structure.</param> |
| | 16 | | /// <returns>The value at the index of the requested key.</returns> |
| | 17 | | T this[TKey key] { get; set; } |
| | 18 | |
|
| | 19 | | #endregion |
| | 20 | |
|
| | 21 | | #region Methods |
| | 22 | |
|
| | 23 | | /// <summary>Tries to get a value by key.</summary> |
| | 24 | | /// <param name="key">The key of the value to get.</param> |
| | 25 | | /// <returns> |
| | 26 | | /// - <see cref="bool"/> Success: true if the key was found or false if not.<br/> |
| | 27 | | /// - <see cref="Exception"/>? Exception: the exception that occured if the get failed.<br/> |
| | 28 | | /// - <typeparamref name="T"/>? Value: the value if the key was found or default if not. |
| | 29 | | /// </returns> |
| | 30 | | (bool Success, Exception? Exception, T? Value) TryGet(TKey key); |
| | 31 | |
|
| | 32 | | /// <summary>Sets value in the map.</summary> |
| | 33 | | /// <param name="key">The key of the value.</param> |
| | 34 | | /// <param name="value">The value to be set.</param> |
| | 35 | | /// <returns> |
| | 36 | | /// - <see cref="bool"/> Success: true if the key+value was set or false if not.<br/> |
| | 37 | | /// - <see cref="Exception"/>? Exception: the exception that occured if the set failed.<br/> |
| | 38 | | /// - <see cref="bool"/>? Existed: if the key existed prior to being set<br/> |
| | 39 | | /// - <typeparamref name="T"/>? OldValue: the previous value if the key existed prior to being set |
| | 40 | | /// </returns> |
| | 41 | | (bool Success, Exception? Exception, bool? Existed, T? OldValue) TrySet(TKey key, T value); |
| | 42 | |
|
| | 43 | | /// <summary>Tries to add a value to the map.</summary> |
| | 44 | | /// <param name="key">The key of the value.</param> |
| | 45 | | /// <param name="value">The value to be added.</param> |
| | 46 | | /// <returns> |
| | 47 | | /// - <see cref="bool"/> Success: true if the key+value was added or false if not<br/> |
| | 48 | | /// - <see cref="Exception"/>? Exception: the exception that occured if the add failed |
| | 49 | | /// </returns> |
| | 50 | | (bool Success, Exception? Exception) TryAdd(TKey key, T value); |
| | 51 | |
|
| | 52 | | /// <summary>Tries to update a value in the map the relative key exists.</summary> |
| | 53 | | /// <typeparam name="TRemovePredicate">The type of predicate determining if the pair should be removed.</typeparam> |
| | 54 | | /// <typeparam name="TUpdate">The type of function to update the value.</typeparam> |
| | 55 | | /// <param name="key">The key of the value to update.</param> |
| | 56 | | /// <param name="removePredicate">The predicate determining if the pair should be removed.</param> |
| | 57 | | /// <param name="update">The function to update the value relative to the key.</param> |
| | 58 | | /// <returns> |
| | 59 | | /// - <see cref="bool"/> Success: true if the key was found or false if not<br/> |
| | 60 | | /// - <see cref="Exception"/>? Exception: the exception that occured if the add failed<br/> |
| | 61 | | /// - <typeparamref name="T"/>? OldValue: the non-updated value if the key was found or default if not<br/> |
| | 62 | | /// - <typeparamref name="T"/>? NewValue: the updated value if the key was found or default if not |
| | 63 | | /// </returns> |
| | 64 | | (bool Success, Exception? Exception, bool? Removed, T? OldValue, T? NewValue) TryRemoveOrUpdate<TRemovePredicate, TUpd |
| | 65 | | where TRemovePredicate : struct, IFunc<T, bool> |
| | 66 | | where TUpdate : struct, IFunc<T, T>; |
| | 67 | |
|
| | 68 | | /// <summary>Tries to update a value in the map the relative key exists.</summary> |
| | 69 | | /// <typeparam name="TUpdate">The type of function to update the value.</typeparam> |
| | 70 | | /// <param name="key">The key of the value to update.</param> |
| | 71 | | /// <param name="update">The function to update the value relative to the key.</param> |
| | 72 | | /// <returns> |
| | 73 | | /// - <see cref="bool"/> Success: true if the key was found or false if not<br/> |
| | 74 | | /// - <see cref="Exception"/>? Exception: the exception that occured if the add failed<br/> |
| | 75 | | /// - <typeparamref name="T"/>? OldValue: the non-updated value if the key was found or default if not<br/> |
| | 76 | | /// - <typeparamref name="T"/>? NewValue: the updated value if the key was found or default if not |
| | 77 | | /// </returns> |
| | 78 | | (bool Success, Exception? Exception, T? OldValue, T? NewValue) TryUpdate<TUpdate>(TKey key, TUpdate update = default) |
| | 79 | | where TUpdate : struct, IFunc<T, T>; |
| | 80 | |
|
| | 81 | | /// <summary>Adds or updates the value at the given key.</summary> |
| | 82 | | /// <typeparam name="TUpdate">The type of function to update the value if present.</typeparam> |
| | 83 | | /// <param name="key">The key of the value to add or update.</param> |
| | 84 | | /// <param name="value">The value to add if not already present.</param> |
| | 85 | | /// <param name="update">The function to update the value if present.</param> |
| | 86 | | /// <returns> |
| | 87 | | /// - <see cref="bool"/> Success: true if the value was added or updated or false if not.<br/> |
| | 88 | | /// - <see cref="Exception"/>? Exception: the exception that occured if the add or update failed.<br/> |
| | 89 | | /// - <see cref="bool"/>? Existed: if the key existed prior to the add or update<br/> |
| | 90 | | /// - <typeparamref name="T"/>? OldValue: the old value if the key existed prior to the add or update |
| | 91 | | /// </returns> |
| | 92 | | (bool Success, Exception? Exception, bool? Existed, T? OldValue) TryAddOrUpdate<TUpdate>(TKey key, T value, TUpdate up |
| | 93 | | where TUpdate : struct, IFunc<T, T>; |
| | 94 | |
|
| | 95 | | /// <summary>Gets an enumerator that will traverse the keys of the map.</summary> |
| | 96 | | /// <returns>An enumerator that will traverse the keys of the map.</returns> |
| | 97 | | System.Collections.Generic.IEnumerable<TKey> GetKeys(); |
| | 98 | |
|
| | 99 | | /// <summary>Gets an array with all the keys in the map.</summary> |
| | 100 | | /// <returns>An array with all the keys in the map.</returns> |
| | 101 | | TKey[] KeysToArray(); |
| | 102 | |
|
| | 103 | | /// <summary>Performs a function on every key in a map.</summary> |
| | 104 | | /// <typeparam name="TStep">The type of the step function.</typeparam> |
| | 105 | | /// <param name="step">The step function to perform on every key.</param> |
| | 106 | | /// <returns>The status of traversal.</returns> |
| | 107 | | StepStatus KeysBreak<TStep>(TStep step = default) |
| | 108 | | where TStep : struct, IFunc<TKey, StepStatus>; |
| | 109 | |
|
| | 110 | | /// <summary>Gets an enumerator that will traverse the pairs of the map.</summary> |
| | 111 | | /// <returns>An enumerator that will traverse the pairs of the map.</returns> |
| | 112 | | System.Collections.Generic.IEnumerable<(T Value, TKey Key)> GetPairs(); |
| | 113 | |
|
| | 114 | | /// <summary>Gets an array with all the pairs in the map.</summary> |
| | 115 | | /// <returns>An array with all the pairs in the map.</returns> |
| | 116 | | (T Value, TKey Key)[] PairsToArray(); |
| | 117 | |
|
| | 118 | | /// <summary>Performs a function on every pair in a map.</summary> |
| | 119 | | /// <typeparam name="TStep">The type of the step function.</typeparam> |
| | 120 | | /// <param name="step">The step function to perform on every pair.</param> |
| | 121 | | /// <returns>The status of traversal.</returns> |
| | 122 | | StepStatus PairsBreak<TStep>(TStep step = default) |
| | 123 | | where TStep : struct, IFunc<(T Value, TKey Key), StepStatus>; |
| | 124 | |
|
| | 125 | | #endregion |
| | 126 | | } |
| | 127 | |
|
| | 128 | | /// <summary>Static Extension class for Map interface implementers.</summary> |
| | 129 | | public static class Map |
| | 130 | | { |
| | 131 | | #region Extensions Methods |
| | 132 | |
|
| | 133 | | /// <summary>Updates a value in the map the relative key exists.</summary> |
| | 134 | | /// <typeparam name="T">The type of values in the map.</typeparam> |
| | 135 | | /// <typeparam name="TKey">The type of keys in the map.</typeparam> |
| | 136 | | /// <param name="map">The map to update the value in.</param> |
| | 137 | | /// <param name="key">The key of the value to update.</param> |
| | 138 | | /// <param name="update">The function to update the value relative to the key.</param> |
| | 139 | | /// <returns> |
| | 140 | | /// (<typeparamref name="T"/> OldValue, <typeparamref name="T"/> NewValue)<br/> |
| | 141 | | /// - <typeparamref name="T"/> OldValue: the value relative tothe key before to the update<br/> |
| | 142 | | /// - <typeparamref name="T"/> NewValue: the value relative tothe key after to the update |
| | 143 | | /// </returns> |
| | 144 | | public static (T OldValue, T NewValue) Update<T, TKey>(this IMap<T, TKey> map, TKey key, Func<T, T> update) => |
| | 145 | | map.Update<T, TKey, SFunc<T, T>>(key, update); |
| | 146 | |
|
| | 147 | | /// <summary>Adds or updates the value at the given key.</summary> |
| | 148 | | /// <typeparam name="T">The type of values in the map.</typeparam> |
| | 149 | | /// <typeparam name="TKey">The type of keys in the map.</typeparam> |
| | 150 | | /// <param name="map">The map to add or update the value in.</param> |
| | 151 | | /// <param name="key">The key of the value to add or update.</param> |
| | 152 | | /// <param name="value">The value to add if not already present.</param> |
| | 153 | | /// <param name="update">The function to update the value if present.</param> |
| | 154 | | /// <returns> |
| | 155 | | /// (<see cref="bool"/> Existed, <typeparamref name="T"/> OldValue)<br/> |
| | 156 | | /// - <see cref="bool"/> Existed: true if the key was already in the map<br/> |
| | 157 | | /// - <typeparamref name="T"/> OldValue: the value relative tothe key before to the update if it existed or default |
| | 158 | | /// </returns> |
| | 159 | | public static (bool Existed, T? OldValue) AddOrUpdate<T, TKey>(this IMap<T, TKey> map, TKey key, T value, Func<T, T> u |
| | 160 | | map.AddOrUpdate<T, TKey, SFunc<T, T>>(key, value, update); |
| | 161 | |
|
| | 162 | | /// <summary>Updates a value in the map the relative key exists.</summary> |
| | 163 | | /// <typeparam name="T">The type of values in the map.</typeparam> |
| | 164 | | /// <typeparam name="TKey">The type of keys in the map.</typeparam> |
| | 165 | | /// <typeparam name="TUpdate">The type of function to update the value.</typeparam> |
| | 166 | | /// <param name="map">The map to update the value in.</param> |
| | 167 | | /// <param name="key">The key of the value to update.</param> |
| | 168 | | /// <param name="update">The function to update the value relative to the key.</param> |
| | 169 | | /// <returns> |
| | 170 | | /// (<typeparamref name="T"/> OldValue, <typeparamref name="T"/> NewValue)<br/> |
| | 171 | | /// - <typeparamref name="T"/> OldValue: the value relative tothe key before to the update<br/> |
| | 172 | | /// - <typeparamref name="T"/> NewValue: the value relative tothe key after to the update |
| | 173 | | /// </returns> |
| | 174 | | public static (T OldValue, T NewValue) Update<T, TKey, TUpdate>(this IMap<T, TKey> map, TKey key, TUpdate update = def |
| | 175 | | where TUpdate : struct, IFunc<T, T> |
| | 176 | | { |
| | 177 | | var (success, exception, oldValue, newValue) = map.TryUpdate(key, update); |
| | 178 | | if (!success) |
| | 179 | | { |
| | 180 | | throw exception ?? new ArgumentException($"{nameof(Update)} failed but the {nameof(exception)} is null"); |
| | 181 | | } |
| | 182 | | return (oldValue!, newValue!); |
| | 183 | | } |
| | 184 | |
|
| | 185 | | /// <summary>Adds or updates the value at the given key.</summary> |
| | 186 | | /// <typeparam name="T">The type of values in the map.</typeparam> |
| | 187 | | /// <typeparam name="TKey">The type of keys in the map.</typeparam> |
| | 188 | | /// <typeparam name="TUpdate">The type of function to update the value.</typeparam> |
| | 189 | | /// <param name="map">The map to add or update the value in.</param> |
| | 190 | | /// <param name="key">The key of the value to add or update.</param> |
| | 191 | | /// <param name="value">The value to add if not already present.</param> |
| | 192 | | /// <param name="update">The function to update the value if present.</param> |
| | 193 | | /// <returns> |
| | 194 | | /// - <see cref="bool"/> Existed: true if the key was already in the map<br/> |
| | 195 | | /// - <typeparamref name="T"/> OldValue: the value relative tothe key before to the update if it existed or default |
| | 196 | | /// </returns> |
| | 197 | | public static (bool Existed, T? OldValue) AddOrUpdate<T, TKey, TUpdate>(this IMap<T, TKey> map, TKey key, T value, TUp |
| | 198 | | where TUpdate : struct, IFunc<T, T> |
| | 199 | | { |
| | 200 | | var (success, exception, existed, oldValue) = map.TryAddOrUpdate(key, value, update); |
| | 201 | | if (!success) |
| | 202 | | { |
| | 203 | | throw exception ?? new ArgumentException($"{nameof(AddOrUpdate)} failed but the {nameof(exception)} is null"); |
| | 204 | | } |
| | 205 | | return (existed!.Value, oldValue); |
| | 206 | | } |
| | 207 | |
|
| | 208 | | /// <summary>Tries to update a value in the map the relative key exists.</summary> |
| | 209 | | /// <typeparam name="T">The type of values in the map.</typeparam> |
| | 210 | | /// <typeparam name="TKey">The type of keys in the map.</typeparam> |
| | 211 | | /// <param name="map">The map to update the value in.</param> |
| | 212 | | /// <param name="key">The key of the value to update.</param> |
| | 213 | | /// <param name="update">The function to update the value relative to the key.</param> |
| | 214 | | /// <returns> |
| | 215 | | /// - <see cref="bool"/> Success: true if the key was found or false if not<br/> |
| | 216 | | /// - <see cref="Exception"/>? Exception: the exception that occured if the update failed<br/> |
| | 217 | | /// - <typeparamref name="T"/>? OldValue: the value if the key was found or default if not<br/> |
| | 218 | | /// - <typeparamref name="T"/>? NewValue: the value if the key was found or default if not |
| | 219 | | /// </returns> |
| | 220 | | public static (bool Success, Exception? Exception, T? OldValue, T? NewValue) TryUpdate<T, TKey>(this IMap<T, TKey> map |
| | 221 | | { |
| | 222 | | if (update is null) throw new ArgumentNullException(nameof(update)); |
| | 223 | | return map.TryUpdate<SFunc<T, T>>(key, update); |
| | 224 | | } |
| | 225 | |
|
| | 226 | | /// <summary>Tries to add or update the value at the given key.</summary> |
| | 227 | | /// <typeparam name="T">The type of values in the map.</typeparam> |
| | 228 | | /// <typeparam name="TKey">The type of keys in the map.</typeparam> |
| | 229 | | /// <param name="map">The map to add or update the value in.</param> |
| | 230 | | /// <param name="key">The key of the value to add or update.</param> |
| | 231 | | /// <param name="value">The value to add if not already present.</param> |
| | 232 | | /// <param name="update">The function to update the value if present.</param> |
| | 233 | | /// <returns> |
| | 234 | | /// - <see cref="bool"/> Success: true if the value was added or updated or false if not.<br/> |
| | 235 | | /// - <see cref="Exception"/>? Exception: the exception that occured if the add or update failed<br/> |
| | 236 | | /// - <see cref="bool"/>? Existed: true if the key-value pair was added or updated or false<br/> |
| | 237 | | /// - <typeparamref name="T"/>? OldValue: the previous value if the key existed before the add or update operation |
| | 238 | | /// </returns> |
| | 239 | | public static (bool Success, Exception? Exception, bool? Existed, T? OldValue) TryAddOrUpdate<T, TKey>(this IMap<T, TK |
| | 240 | | { |
| | 241 | | if (update is null) throw new ArgumentNullException(nameof(update)); |
| | 242 | | return map.TryAddOrUpdate<SFunc<T, T>>(key, value, update); |
| | 243 | | } |
| | 244 | |
|
| | 245 | | /// <summary>Adds a value to a map by key.</summary> |
| | 246 | | /// <typeparam name="T">The type of values in the map.</typeparam> |
| | 247 | | /// <typeparam name="TKey">The type of keys in the map.</typeparam> |
| | 248 | | /// <param name="map">The map to add the value to.</param> |
| | 249 | | /// <param name="key">The key of the value to get.</param> |
| | 250 | | /// <param name="value">The value to add to the map.</param> |
| | 251 | | public static void Add<T, TKey>(this IMap<T, TKey> map, TKey key, T value) |
| | 252 | | { |
| | 253 | | var (success, exception) = map.TryAdd(key, value); |
| | 254 | | if (!success) |
| | 255 | | { |
| | 256 | | throw exception ?? new ArgumentException($"{nameof(Add)} failed but the {nameof(exception)} is null"); |
| | 257 | | } |
| | 258 | | } |
| | 259 | |
|
| | 260 | | /// <summary>Sets a value in a map relative to a key.</summary> |
| | 261 | | /// <typeparam name="T">The type of the value.</typeparam> |
| | 262 | | /// <typeparam name="TKey">The type of the key.</typeparam> |
| | 263 | | /// <param name="map">The map to set the value in.</param> |
| | 264 | | /// <param name="key">The key.</param> |
| | 265 | | /// <param name="value">The value.</param> |
| | 266 | | /// <returns> |
| | 267 | | /// (<see cref="bool"/> Existed, <typeparamref name="T"/> OldValue)<br/> |
| | 268 | | /// - <see cref="bool"/> Existed: true if the key was already in the map<br/> |
| | 269 | | /// - <typeparamref name="T"/> OldValue: the value relative tothe key before to the update if it existed or default |
| | 270 | | /// </returns> |
| | 271 | | public static (bool Existed, T? OldValue) Set<T, TKey>(this IMap<T, TKey> map, TKey key, T value) |
| | 272 | | { |
| | 273 | | var (success, exception, existed, oldValue) = map.TrySet(key, value); |
| | 274 | | if (!success) |
| | 275 | | { |
| | 276 | | throw exception ?? new ArgumentException($"{nameof(Set)} failed but the {nameof(exception)} is null"); |
| | 277 | | } |
| | 278 | | return (existed!.Value, oldValue); |
| | 279 | | } |
| | 280 | |
|
| | 281 | | /// <summary>Gets a value in a map relative to a key.</summary> |
| | 282 | | /// <typeparam name="T">The type of the value.</typeparam> |
| | 283 | | /// <typeparam name="TKey">The type of the key.</typeparam> |
| | 284 | | /// <param name="map">The map to set the value in.</param> |
| | 285 | | /// <param name="key">The key.</param> |
| | 286 | | /// <returns>The value relative to the key.</returns> |
| | 287 | | public static T Get<T, TKey>(this IMap<T, TKey> map, TKey key) |
| | 288 | | { |
| | 289 | | var (success, exception, value) = map.TryGet(key); |
| | 290 | | if (!success) |
| | 291 | | { |
| | 292 | | throw exception ?? new ArgumentException($"{nameof(Get)} failed but the {nameof(exception)} is null"); |
| | 293 | | } |
| | 294 | | return value!; |
| | 295 | | } |
| | 296 | |
|
| | 297 | | /// <summary>Performs a function on every key in a map.</summary> |
| | 298 | | /// <typeparam name="T">The type of values in the map.</typeparam> |
| | 299 | | /// <typeparam name="TKey">The type of keys in the map.</typeparam> |
| | 300 | | /// <param name="map">The map to traverse the keys of.</param> |
| | 301 | | /// <param name="step">The step function to perform on every key.</param> |
| | 302 | | public static void Keys<T, TKey>(this IMap<T, TKey> map, Action<TKey> step) |
| | 303 | | { |
| | 304 | | if (step is null) throw new ArgumentNullException(nameof(step)); |
| | 305 | | map.Keys<T, TKey, SAction<TKey>>(step); |
| | 306 | | } |
| | 307 | |
|
| | 308 | | /// <summary>Performs a function on every key in a map.</summary> |
| | 309 | | /// <typeparam name="T">The type of values in the map.</typeparam> |
| | 310 | | /// <typeparam name="TKey">The type of keys in the map.</typeparam> |
| | 311 | | /// <typeparam name="TStep">The type of step function to perform on every key.</typeparam> |
| | 312 | | /// <param name="map">The map to traverse the keys of.</param> |
| | 313 | | /// <param name="step">The step function to perform on every key.</param> |
| | 314 | | public static void Keys<T, TKey, TStep>(this IMap<T, TKey> map, TStep step = default) |
| | 315 | | where TStep : struct, IAction<TKey> => |
| | 316 | | map.KeysBreak<StepBreakFromAction<TKey, TStep>>(step); |
| | 317 | |
|
| | 318 | | /// <summary>Performs a function on every key in a map.</summary> |
| | 319 | | /// <typeparam name="T">The type of values in the map.</typeparam> |
| | 320 | | /// <typeparam name="TKey">The type of keys in the map.</typeparam> |
| | 321 | | /// <param name="map">The map to traverse the keys of.</param> |
| | 322 | | /// <param name="step">The step function to perform on every key.</param> |
| | 323 | | /// <returns>The status of the traversal.</returns> |
| | 324 | | public static StepStatus KeysBreak<T, TKey>(this IMap<T, TKey> map, Func<TKey, StepStatus> step) |
| | 325 | | { |
| | 326 | | if (step is null) throw new ArgumentNullException(nameof(step)); |
| | 327 | | return map.KeysBreak<SFunc<TKey, StepStatus>>(step); |
| | 328 | | } |
| | 329 | |
|
| | 330 | | /// <summary>Performs a function on every pair in a map.</summary> |
| | 331 | | /// <typeparam name="T">The type of values in the map.</typeparam> |
| | 332 | | /// <typeparam name="TKey">The type of keys in the map.</typeparam> |
| | 333 | | /// <param name="map">The map to traverse the pairs of.</param> |
| | 334 | | /// <param name="step">The step function to perform on every pair.</param> |
| | 335 | | public static void Pairs<T, TKey>(this IMap<T, TKey> map, Action<(T Value, TKey Key)> step) |
| | 336 | | { |
| | 337 | | if (step is null) throw new ArgumentNullException(nameof(step)); |
| | 338 | | map.Pairs<T, TKey, SAction<(T Value, TKey Key)>>(step); |
| | 339 | | } |
| | 340 | |
|
| | 341 | | /// <summary>Performs a function on every pair in a map.</summary> |
| | 342 | | /// <typeparam name="T">The type of values in the map.</typeparam> |
| | 343 | | /// <typeparam name="TKey">The type of keys in the map.</typeparam> |
| | 344 | | /// <typeparam name="TStep">The type of step function to perform on every pair.</typeparam> |
| | 345 | | /// <param name="map">The map to traverse the pairs of.</param> |
| | 346 | | /// <param name="step">The step function to perform on every pair.</param> |
| | 347 | | public static void Pairs<T, TKey, TStep>(this IMap<T, TKey> map, TStep step = default) |
| | 348 | | where TStep : struct, IAction<(T Value, TKey Key)> => |
| | 349 | | map.PairsBreak<StepBreakFromAction<(T Value, TKey Key), TStep>>(step); |
| | 350 | |
|
| | 351 | | /// <summary>Performs a function on every pair in a map.</summary> |
| | 352 | | /// <typeparam name="T">The type of values in the map.</typeparam> |
| | 353 | | /// <typeparam name="TKey">The type of keys in the map.</typeparam> |
| | 354 | | /// <param name="map">The map to traverse the pairs of.</param> |
| | 355 | | /// <param name="step">The step function to perform on every pair.</param> |
| | 356 | | /// <returns>The status of the traversal.</returns> |
| | 357 | | public static StepStatus PairsBreak<T, TKey>(this IMap<T, TKey> map, Func<(T Value, TKey Key), StepStatus> step) |
| | 358 | | { |
| | 359 | | if (step is null) throw new ArgumentNullException(nameof(step)); |
| | 360 | | return map.PairsBreak<SFunc<(T Value, TKey Key), StepStatus>>(step); |
| | 361 | | } |
| | 362 | |
|
| | 363 | | #endregion |
| | 364 | | } |
| | 365 | |
|
| | 366 | | /// <summary>Static helpers.</summary> |
| | 367 | | public static class MapHashLinked |
| | 368 | | { |
| | 369 | | #region Extension Methods |
| | 370 | |
|
| | 371 | | /// <summary>Constructs a new <see cref="MapHashLinked{T, K, TEquate, THash}"/>.</summary> |
| | 372 | | /// <typeparam name="T">The type of values stored in this data structure.</typeparam> |
| | 373 | | /// <typeparam name="TKey">The type of keys used to look up values.</typeparam> |
| | 374 | | /// <param name="equate">The function for comparing <typeparamref name="T"/> values for equality.</param> |
| | 375 | | /// <param name="hash">The function for hashing <typeparamref name="T"/> values.</param> |
| | 376 | | /// <returns>The new constructed <see cref="MapHashLinked{T, K, TEquate, THash}"/>.</returns> |
| | 377 | | public static MapHashLinked<T, TKey, SFunc<TKey, TKey, bool>, SFunc<TKey, int>> New<T, TKey>( |
| | 378 | | Func<TKey, TKey, bool>? equate = null, |
| | 379 | | Func<TKey, int>? hash = null) => |
| | 380 | | new(equate ?? Equate, hash ?? Hash); |
| | 381 | |
|
| | 382 | | #endregion |
| | 383 | | } |
| | 384 | |
|
| | 385 | | /// <summary>An unsorted structure of unique items.</summary> |
| | 386 | | /// <typeparam name="T">The generic type of the structure.</typeparam> |
| | 387 | | /// <typeparam name="TKey">The generic key type of this map.</typeparam> |
| | 388 | | /// <typeparam name="TEquate">The type of function for quality checking <typeparamref name="TKey"/> values.</typeparam> |
| | 389 | | /// <typeparam name="THash">The type of function for hashing <typeparamref name="TKey"/> values.</typeparam> |
| | 390 | | public class MapHashLinked<T, TKey, TEquate, THash> : IMap<T, TKey>, |
| | 391 | | ICloneable<MapHashLinked<T, TKey, TEquate, THash>>, |
| | 392 | | DataStructure.IEquating<TKey, TEquate>, |
| | 393 | | DataStructure.IHashing<TKey, THash> |
| | 394 | | where TEquate : struct, IFunc<TKey, TKey, bool> |
| | 395 | | where THash : struct, IFunc<TKey, int> |
| | 396 | | { |
| | 397 | | internal const float _maxLoadFactor = .7f; |
| | 398 | | internal const float _minLoadFactor = .3f; |
| | 399 | |
|
| | 400 | | internal TEquate _equate; |
| | 401 | | internal THash _hash; |
| | 402 | | internal Node?[] _table; |
| | 403 | | internal int _count; |
| | 404 | |
|
| | 405 | | #region Nested Types |
| | 406 | |
|
| | 407 | | internal class Node |
| | 408 | | { |
| | 409 | | internal TKey Key; |
| | 410 | | internal T Value; |
| | 411 | | internal Node? Next; |
| | 412 | |
|
| 602477 | 413 | | internal Node(T value, TKey key, Node? next = null) |
| 602477 | 414 | | { |
| 602477 | 415 | | Value = value; |
| 602477 | 416 | | Key = key; |
| 602477 | 417 | | Next = next; |
| 602477 | 418 | | } |
| | 419 | | } |
| | 420 | |
|
| | 421 | | #endregion |
| | 422 | |
|
| | 423 | | #region Constructors |
| | 424 | |
|
| | 425 | | /// <summary>Constructs a new <see cref="MapHashLinked{T, K, TEquate, THash}"/>.</summary> |
| | 426 | | /// <param name="equate">The function for quality checking <typeparamref name="TKey"/> values.</param> |
| | 427 | | /// <param name="hash">The function for hashing <typeparamref name="TKey"/> values.</param> |
| | 428 | | /// <param name="expectedCount">The expected count of the map.</param> |
| 430 | 429 | | public MapHashLinked( |
| 430 | 430 | | TEquate equate = default, |
| 430 | 431 | | THash hash = default, |
| 430 | 432 | | int? expectedCount = null) |
| 430 | 433 | | { |
| 430 | 434 | | if (expectedCount.HasValue && expectedCount.Value > 0) |
| 26 | 435 | | { |
| 26 | 436 | | int tableSize = (int)(expectedCount.Value * (1 / _maxLoadFactor)); |
| 49 | 437 | | while (!IsPrime(tableSize)) |
| 23 | 438 | | { |
| 23 | 439 | | tableSize++; |
| 23 | 440 | | } |
| 26 | 441 | | _table = new Node[tableSize]; |
| 26 | 442 | | } |
| | 443 | | else |
| 404 | 444 | | { |
| 404 | 445 | | _table = new Node[2]; |
| 404 | 446 | | } |
| 430 | 447 | | _equate = equate; |
| 430 | 448 | | _hash = hash; |
| 430 | 449 | | _count = 0; |
| 430 | 450 | | } |
| | 451 | |
|
| | 452 | | /// <summary>This constructor is for cloning purposes.</summary> |
| | 453 | | /// <param name="map">The map to clone.</param> |
| 1 | 454 | | internal MapHashLinked(MapHashLinked<T, TKey, TEquate, THash> map) |
| 1 | 455 | | { |
| 1 | 456 | | _equate = map._equate; |
| 1 | 457 | | _hash = map._hash; |
| 1 | 458 | | _table = (Node[])map._table.Clone(); |
| 1 | 459 | | _count = map._count; |
| 1 | 460 | | } |
| | 461 | |
|
| | 462 | | #endregion |
| | 463 | |
|
| | 464 | | #region Properties |
| | 465 | |
|
| | 466 | | /// <summary>The current size of the hashed table.</summary> |
| 0 | 467 | | public int TableSize => _table.Length; |
| | 468 | |
|
| | 469 | | /// <inheritdoc/> |
| 174 | 470 | | public int Count => _count; |
| | 471 | |
|
| | 472 | | /// <inheritdoc/> |
| 198 | 473 | | public THash Hash => _hash; |
| | 474 | |
|
| | 475 | | /// <inheritdoc/> |
| 198 | 476 | | public TEquate Equate => _equate; |
| | 477 | |
|
| | 478 | | /// <summary>Gets the value of a specified key.</summary> |
| | 479 | | /// <param name="key">The key to get the value of.</param> |
| | 480 | | /// <returns>The value of the key.</returns> |
| 601408 | 481 | | public T this[TKey key] { get => this.Get(key); set => this.Set(key, value); } |
| | 482 | |
|
| | 483 | | #endregion |
| | 484 | |
|
| | 485 | | #region Methods |
| | 486 | |
|
| 1700243 | 487 | | internal int GetLocation(TKey key) => (_hash.Invoke(key) & int.MaxValue) % _table.Length; |
| | 488 | |
|
| | 489 | | /// <inheritdoc/> |
| | 490 | | public (bool Success, Exception? Exception) TryAdd(TKey key, T value) |
| 400242 | 491 | | { |
| 400242 | 492 | | int location = GetLocation(key); |
| 1000090 | 493 | | for (Node? node = _table[location]; node is not null; node = node.Next) |
| 99810 | 494 | | { |
| 99810 | 495 | | if (_equate.Invoke(node.Key, key)) |
| 7 | 496 | | { |
| 7 | 497 | | return (false, new ArgumentException("Attempting to add a duplicate key to a map.", nameof(key))); |
| | 498 | | } |
| 99803 | 499 | | } |
| 400235 | 500 | | _table[location] = new Node(value: value, key: key, next: _table[location]); |
| 400235 | 501 | | _count++; |
| 400235 | 502 | | if (_count > _table.Length * _maxLoadFactor) |
| 143 | 503 | | { |
| 143 | 504 | | float tableSizeFloat = (_count * 2) * (1 / _maxLoadFactor); |
| 143 | 505 | | if (tableSizeFloat <= int.MaxValue) |
| 143 | 506 | | { |
| 143 | 507 | | int tableSize = (int)tableSizeFloat; |
| 426 | 508 | | while (!IsPrime(tableSize)) |
| 283 | 509 | | { |
| 283 | 510 | | tableSize++; |
| 283 | 511 | | } |
| 143 | 512 | | Resize(tableSize); |
| 143 | 513 | | } |
| 143 | 514 | | } |
| 400235 | 515 | | return (true, null); |
| 400242 | 516 | | } |
| | 517 | |
|
| | 518 | | /// <inheritdoc/> |
| | 519 | | public (bool Success, Exception? Exception, bool? Existed, T? OldValue) TryAddOrUpdate<TUpdate>(TKey key, T value, TUp |
| | 520 | | where TUpdate : struct, IFunc<T, T> |
| 3070 | 521 | | { |
| 3070 | 522 | | int location = GetLocation(key); |
| 6140 | 523 | | for (Node? node = _table[location]; node is not null; node = node.Next) |
| 2028 | 524 | | { |
| 2028 | 525 | | if (_equate.Invoke(node.Key, key)) |
| 2028 | 526 | | { |
| 2028 | 527 | | T oldValue = node.Value; |
| 2028 | 528 | | node.Value = update.Invoke(node.Value); |
| 2028 | 529 | | return (true, null, true, oldValue); |
| | 530 | | } |
| 0 | 531 | | } |
| 1042 | 532 | | _table[location] = new Node( |
| 1042 | 533 | | value: value, |
| 1042 | 534 | | key: key, |
| 1042 | 535 | | next: _table[location]); |
| 1042 | 536 | | _count++; |
| 1042 | 537 | | if (_count > _table.Length * _maxLoadFactor) |
| 12 | 538 | | { |
| 12 | 539 | | float tableSizeFloat = (_count * 2) * (1 / _maxLoadFactor); |
| 12 | 540 | | if (tableSizeFloat <= int.MaxValue) |
| 12 | 541 | | { |
| 12 | 542 | | int tableSize = (int)tableSizeFloat; |
| 39 | 543 | | while (!IsPrime(tableSize)) |
| 27 | 544 | | { |
| 27 | 545 | | tableSize++; |
| 27 | 546 | | } |
| 12 | 547 | | Resize(tableSize); |
| 12 | 548 | | } |
| 12 | 549 | | } |
| 1042 | 550 | | return (true, null, false, default); |
| 3070 | 551 | | } |
| | 552 | |
|
| | 553 | | /// <inheritdoc/> |
| | 554 | | public (bool Success, Exception? Exception, bool? Removed, T? OldValue, T? NewValue) TryRemoveOrUpdate<TRemovePredicat |
| | 555 | | where TRemovePredicate : struct, IFunc<T, bool> |
| | 556 | | where TUpdate : struct, IFunc<T, T> |
| 0 | 557 | | { |
| 0 | 558 | | int location = GetLocation(key); |
| | 559 | |
|
| 0 | 560 | | for (Node? node = _table[location], previous = null; node is not null; previous = node, node = node.Next) |
| 0 | 561 | | { |
| 0 | 562 | | if (_equate.Invoke(node.Key, key)) |
| 0 | 563 | | { |
| 0 | 564 | | if (removePredicate.Invoke(node.Value)) |
| 0 | 565 | | { |
| 0 | 566 | | if (previous is null) |
| 0 | 567 | | { |
| 0 | 568 | | _table[location] = node.Next; |
| 0 | 569 | | } |
| | 570 | | else |
| 0 | 571 | | { |
| 0 | 572 | | previous.Next = node.Next; |
| 0 | 573 | | } |
| 0 | 574 | | _count--; |
| 0 | 575 | | return (true, null, true, node.Value, default); |
| | 576 | | } |
| | 577 | | else |
| 0 | 578 | | { |
| 0 | 579 | | T oldValue = node.Value; |
| 0 | 580 | | node.Value = update.Invoke(node.Value); |
| 0 | 581 | | return (true, null, false, oldValue, node.Value); |
| | 582 | | } |
| | 583 | | } |
| 0 | 584 | | } |
| 0 | 585 | | return (false, new ArgumentException(paramName: nameof(key), message: "key not found"), default, default, default); |
| 0 | 586 | | } |
| | 587 | |
|
| | 588 | | /// <inheritdoc/> |
| | 589 | | public (bool Success, Exception? Exception, T? OldValue, T? NewValue) TryUpdate<TUpdate>(TKey key, TUpdate update = de |
| | 590 | | where TUpdate : struct, IFunc<T, T> |
| 62 | 591 | | { |
| 62 | 592 | | int location = GetLocation(key); |
| 124 | 593 | | for (Node? node = _table[location]; node is not null; node = node.Next) |
| 60 | 594 | | { |
| 60 | 595 | | if (_equate.Invoke(node.Key, key)) |
| 60 | 596 | | { |
| 60 | 597 | | T oldValue = node.Value; |
| 60 | 598 | | node.Value = update.Invoke(node.Value); |
| 60 | 599 | | return (true, null, oldValue, node.Value); |
| | 600 | | } |
| 0 | 601 | | } |
| 2 | 602 | | return (false, new ArgumentException(paramName: nameof(key), message: "key not found"), default, default); |
| 62 | 603 | | } |
| | 604 | |
|
| | 605 | | /// <inheritdoc/> |
| | 606 | | public (bool Success, Exception? Exception, T? Value) TryGet(TKey key) |
| 420879 | 607 | | { |
| 420879 | 608 | | int location = GetLocation(key); |
| 926052 | 609 | | for (Node? node = _table[location]; node is not null; node = node.Next) |
| 459909 | 610 | | { |
| 459909 | 611 | | if (_equate.Invoke(node.Key, key)) |
| 417762 | 612 | | { |
| 417762 | 613 | | return (true, null, node.Value); |
| | 614 | | } |
| 42147 | 615 | | } |
| 3117 | 616 | | return (false, new ArgumentException("Attempting to get a value from the map that has not been added.", nameof(key)) |
| 420879 | 617 | | } |
| | 618 | |
|
| | 619 | | /// <inheritdoc/> |
| | 620 | | public (bool Success, Exception? Exception, bool? Existed, T? OldValue) TrySet(TKey key, T value) |
| 203210 | 621 | | { |
| 203210 | 622 | | int location = GetLocation(key); |
| 507206 | 623 | | for (Node? node = _table[location]; node is not null; node = node.Next) |
| 52403 | 624 | | { |
| 52403 | 625 | | if (_equate.Invoke(node.Key, key)) |
| 2010 | 626 | | { |
| 2010 | 627 | | T oldValue = node.Value; |
| 2010 | 628 | | node.Value = value; |
| 2010 | 629 | | return (true, null, true, oldValue); |
| | 630 | | } |
| 50393 | 631 | | } |
| 201200 | 632 | | _table[location] = new Node( |
| 201200 | 633 | | value: value, |
| 201200 | 634 | | key: key, |
| 201200 | 635 | | next: _table[location]); |
| 201200 | 636 | | _count++; |
| 201200 | 637 | | if (_count > _table.Length * _maxLoadFactor) |
| 120 | 638 | | { |
| 120 | 639 | | float tableSizeFloat = (_count * 2) * (1 / _maxLoadFactor); |
| 120 | 640 | | if (tableSizeFloat <= int.MaxValue) |
| 120 | 641 | | { |
| 120 | 642 | | int tableSize = (int)tableSizeFloat; |
| 340 | 643 | | while (!IsPrime(tableSize)) |
| 220 | 644 | | { |
| 220 | 645 | | tableSize++; |
| 220 | 646 | | } |
| 120 | 647 | | Resize(tableSize); |
| 120 | 648 | | } |
| 120 | 649 | | } |
| 201200 | 650 | | return (true, null, false, default); |
| 203210 | 651 | | } |
| | 652 | |
|
| | 653 | | /// <inheritdoc/> |
| | 654 | | public (bool Success, Exception? Exception) TryRemove(TKey key) |
| 66672 | 655 | | { |
| 66672 | 656 | | var (success, exception) = TryRemoveWithoutTrim(key); |
| 66672 | 657 | | if (!success) |
| 0 | 658 | | { |
| 0 | 659 | | return (false, exception); |
| | 660 | | } |
| 66672 | 661 | | else if (_table.Length > 2 && _count < _table.Length * _minLoadFactor) |
| 3 | 662 | | { |
| 3 | 663 | | int tableSize = (int)(_count * (1 / _maxLoadFactor)); |
| 4 | 664 | | while (!IsPrime(tableSize)) |
| 1 | 665 | | { |
| 1 | 666 | | tableSize++; |
| 1 | 667 | | } |
| 3 | 668 | | Resize(tableSize); |
| 3 | 669 | | } |
| 66672 | 670 | | return (true, null); |
| 66672 | 671 | | } |
| | 672 | |
|
| | 673 | | /// <summary>Tries to remove a keyed value without shrinking the hash table.</summary> |
| | 674 | | /// <param name="key">The key of the value to remove.</param> |
| | 675 | | /// <returns>True if the removal was successful for false if not.</returns> |
| | 676 | | public (bool Success, Exception? Exception) TryRemoveWithoutTrim(TKey key) |
| 66672 | 677 | | { |
| 66672 | 678 | | int location = GetLocation(key); |
| 221961 | 679 | | for (Node? node = _table[location], previous = null; node is not null; previous = node, node = node.Next) |
| 73987 | 680 | | { |
| 73987 | 681 | | if (_equate.Invoke(node.Key, key)) |
| 66672 | 682 | | { |
| 66672 | 683 | | if (previous is null) |
| 60307 | 684 | | { |
| 60307 | 685 | | _table[location] = node.Next; |
| 60307 | 686 | | } |
| | 687 | | else |
| 6365 | 688 | | { |
| 6365 | 689 | | previous.Next = node.Next; |
| 6365 | 690 | | } |
| 66672 | 691 | | _count--; |
| 66672 | 692 | | return (true, null); |
| | 693 | | } |
| 7315 | 694 | | } |
| 0 | 695 | | return (false, new ArgumentException("Attempting to remove a key that is no in a map.", nameof(key))); |
| 66672 | 696 | | } |
| | 697 | |
|
| | 698 | | internal void Resize(int tableSize) |
| 278 | 699 | | { |
| 278 | 700 | | if (tableSize == _table.Length) |
| 0 | 701 | | { |
| 0 | 702 | | return; |
| | 703 | | } |
| | 704 | |
|
| 278 | 705 | | Node?[] temp = _table; |
| 278 | 706 | | _table = new Node[tableSize]; |
| | 707 | |
|
| 3759610 | 708 | | for (int i = 0; i < temp.Length; i++) |
| 1879527 | 709 | | { |
| 6015450 | 710 | | for (Node? node = temp[i]; node is not null; node = temp[i]) |
| 1128198 | 711 | | { |
| 1128198 | 712 | | temp[i] = node.Next; |
| | 713 | |
|
| 1128198 | 714 | | int hashCode = _hash.Invoke(node.Key); |
| 1128198 | 715 | | int location = (hashCode & int.MaxValue) % _table.Length; |
| | 716 | |
|
| 1128198 | 717 | | node.Next = _table[location]; |
| 1128198 | 718 | | _table[location] = node; |
| 1128198 | 719 | | } |
| 1879527 | 720 | | } |
| 278 | 721 | | } |
| | 722 | |
|
| | 723 | | /// <summary> |
| | 724 | | /// Trims the table to an appropriate size based on the current count.<br/> |
| | 725 | | /// Runtime: O(n), Ω(1) |
| | 726 | | /// </summary> |
| | 727 | | public void Trim() |
| 0 | 728 | | { |
| 0 | 729 | | int tableSize = _count; |
| 0 | 730 | | while (!IsPrime(tableSize)) |
| 0 | 731 | | { |
| 0 | 732 | | tableSize++; |
| 0 | 733 | | } |
| 0 | 734 | | Resize(tableSize); |
| 0 | 735 | | } |
| | 736 | |
|
| | 737 | | /// <inheritdoc/> |
| 1 | 738 | | public MapHashLinked<T, TKey, TEquate, THash> Clone() => new(this); |
| | 739 | |
|
| | 740 | | /// <inheritdoc/> |
| | 741 | | public bool Contains(TKey key) |
| 606108 | 742 | | { |
| 606108 | 743 | | int location = GetLocation(key); |
| 1385008 | 744 | | for (Node? node = _table[location]; node is not null; node = node.Next) |
| 624803 | 745 | | { |
| 624803 | 746 | | if (_equate.Invoke(node.Key, key)) |
| 538407 | 747 | | { |
| 538407 | 748 | | return true; |
| | 749 | | } |
| 86396 | 750 | | } |
| 67701 | 751 | | return false; |
| 606108 | 752 | | } |
| | 753 | |
|
| | 754 | | /// <inheritdoc/> |
| | 755 | | public void Clear() |
| 9 | 756 | | { |
| 9 | 757 | | _table = new Node[2]; |
| 9 | 758 | | _count = 0; |
| 9 | 759 | | } |
| | 760 | |
|
| | 761 | | /// <inheritdoc/> |
| | 762 | | public StepStatus StepperBreak<TStep>(TStep step = default) |
| | 763 | | where TStep : struct, IFunc<T, StepStatus> |
| 0 | 764 | | { |
| 0 | 765 | | for (int i = 0; i < _table.Length; i++) |
| 0 | 766 | | { |
| 0 | 767 | | for (Node? node = _table[i]; node is not null; node = node.Next) |
| 0 | 768 | | { |
| 0 | 769 | | if (step.Invoke(node.Value) is Break) |
| 0 | 770 | | { |
| 0 | 771 | | return Break; |
| | 772 | | } |
| 0 | 773 | | } |
| 0 | 774 | | } |
| 0 | 775 | | return Continue; |
| 0 | 776 | | } |
| | 777 | |
|
| | 778 | | /// <inheritdoc/> |
| | 779 | | public StepStatus KeysBreak<TStep>(TStep step = default) |
| | 780 | | where TStep : struct, IFunc<TKey, StepStatus> |
| 31 | 781 | | { |
| 282 | 782 | | for (int i = 0; i < _table.Length; i++) |
| 110 | 783 | | { |
| 314 | 784 | | for (Node? node = _table[i]; node is not null; node = node.Next) |
| 47 | 785 | | { |
| 47 | 786 | | if (step.Invoke(node.Key) is Break) |
| 0 | 787 | | { |
| 0 | 788 | | return Break; |
| | 789 | | } |
| 47 | 790 | | } |
| 110 | 791 | | } |
| 31 | 792 | | return Continue; |
| 31 | 793 | | } |
| | 794 | |
|
| | 795 | | /// <inheritdoc/> |
| | 796 | | public System.Collections.Generic.IEnumerable<TKey> GetKeys() |
| 3 | 797 | | { |
| 30 | 798 | | for (int i = 0; i < _table.Length; i++) |
| 12 | 799 | | { |
| 38 | 800 | | for (Node? node = _table[i]; node is not null; node = node.Next) |
| 7 | 801 | | { |
| 7 | 802 | | yield return node.Key; |
| 7 | 803 | | } |
| 12 | 804 | | } |
| 3 | 805 | | } |
| | 806 | |
|
| | 807 | | /// <inheritdoc/> |
| | 808 | | public StepStatus PairsBreak<TStep>(TStep step = default) |
| | 809 | | where TStep : struct, IFunc<(T Value, TKey Key), StepStatus> |
| 11 | 810 | | { |
| 180 | 811 | | for (int i = 0; i < _table.Length; i++) |
| 79 | 812 | | { |
| 224 | 813 | | for (Node? node = _table[i]; node is not null; node = node.Next) |
| 33 | 814 | | { |
| 33 | 815 | | if (step.Invoke((node.Value, node.Key)) is Break) |
| 0 | 816 | | { |
| 0 | 817 | | return Break; |
| | 818 | | } |
| 33 | 819 | | } |
| 79 | 820 | | } |
| 11 | 821 | | return Continue; |
| 11 | 822 | | } |
| | 823 | |
|
| | 824 | | /// <inheritdoc/> |
| | 825 | | public System.Collections.Generic.IEnumerable<(T Value, TKey Key)> GetPairs() |
| 27 | 826 | | { |
| 54864 | 827 | | for (int i = 0; i < _table.Length; i++) |
| 27405 | 828 | | { |
| 84858 | 829 | | for (Node? node = _table[i]; node is not null; node = node.Next) |
| 15024 | 830 | | { |
| 15024 | 831 | | yield return (node.Value, node.Key); |
| 15024 | 832 | | } |
| 27405 | 833 | | } |
| 27 | 834 | | } |
| | 835 | |
|
| 0 | 836 | | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => GetEnumerator(); |
| | 837 | |
|
| | 838 | | /// <inheritdoc/> |
| | 839 | | public System.Collections.Generic.IEnumerator<T> GetEnumerator() |
| 0 | 840 | | { |
| 0 | 841 | | for (int i = 0; i < _table.Length; i++) |
| 0 | 842 | | { |
| 0 | 843 | | for (Node? node = _table[i]; node is not null; node = node.Next) |
| 0 | 844 | | { |
| 0 | 845 | | yield return node.Value; |
| 0 | 846 | | } |
| 0 | 847 | | } |
| 0 | 848 | | } |
| | 849 | |
|
| | 850 | | /// <inheritdoc/> |
| | 851 | | public T[] ToArray() |
| 0 | 852 | | { |
| 0 | 853 | | T[] array = new T[_count]; |
| 0 | 854 | | for (int i = 0, index = 0; i < _table.Length; i++) |
| 0 | 855 | | { |
| 0 | 856 | | for (Node? node = _table[i]; node is not null; node = node.Next) |
| 0 | 857 | | { |
| 0 | 858 | | array[index++] = node.Value; |
| 0 | 859 | | } |
| 0 | 860 | | } |
| 0 | 861 | | return array; |
| 0 | 862 | | } |
| | 863 | |
|
| | 864 | | /// <inheritdoc/> |
| | 865 | | public TKey[] KeysToArray() |
| 1 | 866 | | { |
| 1 | 867 | | TKey[] array = new TKey[_count]; |
| 25 | 868 | | for (int i = 0, index = 0; i < _table.Length; i++) |
| 11 | 869 | | { |
| 32 | 870 | | for (Node? node = _table[i]; node is not null; node = node.Next) |
| 5 | 871 | | { |
| 5 | 872 | | array[index++] = node.Key; |
| 5 | 873 | | } |
| 11 | 874 | | } |
| 1 | 875 | | return array; |
| 1 | 876 | | } |
| | 877 | |
|
| | 878 | | /// <inheritdoc/> |
| | 879 | | public (T Value, TKey Key)[] PairsToArray() |
| 0 | 880 | | { |
| 0 | 881 | | (T Value, TKey Key)[] array = new (T Value, TKey Key)[_count]; |
| 0 | 882 | | for (int i = 0, index = 0; i < _table.Length; i++) |
| 0 | 883 | | { |
| 0 | 884 | | for (Node? node = _table[i]; node is not null; node = node.Next) |
| 0 | 885 | | { |
| 0 | 886 | | array[index++] = (node.Value, node.Key); |
| 0 | 887 | | } |
| 0 | 888 | | } |
| 0 | 889 | | return array; |
| 0 | 890 | | } |
| | 891 | |
|
| | 892 | | #endregion |
| | 893 | | } |