< Summary

Information
Class: MyNet.Collections.ObservableKeyedCollection<T1, T2>
Assembly: MyNet.Collections
File(s): https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Collections/ObservableKeyedCollection.cs
Tag: 342_31701152804
Line coverage
76%
Covered lines: 103
Uncovered lines: 31
Coverable lines: 134
Total lines: 453
Line coverage: 76.8%
Branch coverage
52%
Covered branches: 56
Total branches: 106
Branch coverage: 52.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
.ctor(...)100%11100%
.ctor(...)83.33%66100%
get_Dictionary()100%210%
get_IsDictionaryCreated()100%11100%
get_Item(...)100%22100%
Contains(...)50%2260%
TryGetValue(...)50%3250%
TryAdd(...)66.66%66100%
Remove(...)37.5%24837.5%
ChangeItemKey(...)100%210%
ChangeItemKey(...)50%111080%
ClearItems()50%22100%
InsertItem(...)80%1010100%
InsertItemInItems(...)100%210%
AddRange(...)50%141492.3%
RemoveRange(...)0%7280%
RemoveItem(...)50%22100%
SetItem(...)30%101083.33%
EnsureDictionaryIfNeeded()75%5466.66%
CreateDictionaryNow()50%22100%
ContainsItem(...)50%66100%
AddKeyInternal(...)50%4475%
RemoveKeyInternal(...)50%22100%
CreateDictionary()100%44100%
GetDictionaryStats()50%22100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Collections/ObservableKeyedCollection.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="ObservableKeyedCollection.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9using System.Linq;
 10
 11namespace MyNet.Collections;
 12
 13/// <summary>
 14/// A keyed observable collection that maintains an internal dictionary for fast O(1) key lookups.
 15/// Optimized for performance with automatic dictionary creation and capacity management.
 16/// </summary>
 17/// <typeparam name="TKey">The type of the key for items in the collection.</typeparam>
 18/// <typeparam name="T">The type of the items in the collection.</typeparam>
 19public abstract class ObservableKeyedCollection<TKey, T> : ObservableRangeCollection<T>
 20    where TKey : notnull
 21{
 22    private readonly int _dictionaryCreationThreshold;
 23    private Dictionary<TKey, T>? _dict;
 24
 25    /// <summary>
 26    /// Initializes a new instance of the <see cref="ObservableKeyedCollection{TKey, T}"/> class.
 27    /// </summary>
 28    protected ObservableKeyedCollection()
 629        : this([])
 30    {
 631    }
 32
 33    /// <summary>
 34    /// Initializes a new instance of the <see cref="ObservableKeyedCollection{TKey, T}"/> class with the specified comp
 35    /// </summary>
 36    /// <param name="comparer">The equality comparer used to compare keys.</param>
 37    /// <param name="dictionaryCreationThreshold">Minimum items before creating dictionary. 0 = always create.</param>
 38    protected ObservableKeyedCollection(IEqualityComparer<TKey> comparer, int dictionaryCreationThreshold = 0)
 6639        : this([], comparer, dictionaryCreationThreshold)
 40    {
 6641    }
 42
 43    /// <summary>
 44    /// Initializes a new instance of the <see cref="ObservableKeyedCollection{TKey, T}"/> class that contains elements 
 45    /// </summary>
 46    /// <param name="list">The list whose elements are copied to the new collection.</param>
 47    /// <param name="comparer">The optional comparer used to compare keys.</param>
 48    /// <param name="dictionaryCreationThreshold">Minimum items before creating dictionary. 0 = always create.</param>
 49    protected ObservableKeyedCollection(IEnumerable<T> list, IEqualityComparer<TKey>? comparer = null, int dictionaryCre
 7250        : base(list)
 51    {
 7252        Comparer = comparer ?? EqualityComparer<TKey>.Default;
 7253        _dictionaryCreationThreshold = dictionaryCreationThreshold;
 54
 55        // Pre-create dictionary if we have items above threshold OR if threshold is 0
 7256        if (_dictionaryCreationThreshold == 0 || Count > _dictionaryCreationThreshold)
 57        {
 6358            CreateDictionary();
 59        }
 7260    }
 61
 62    /// <summary>
 63    /// Gets the comparer used to compare keys.
 64    /// </summary>
 65    public IEqualityComparer<TKey> Comparer { get; }
 66
 67    /// <summary>
 68    /// Gets the internal dictionary used for fast key lookups, if it has been created.
 69    /// </summary>
 070    protected IDictionary<TKey, T>? Dictionary => _dict;
 71
 72    /// <summary>
 73    /// Gets a value indicating whether gets whether the dictionary has been created.
 74    /// </summary>
 1575    protected bool IsDictionaryCreated => _dict is not null;
 76
 77    /// <summary>
 78    /// Gets the item associated with the specified key, or default if the key is not present.
 79    /// </summary>
 80    /// <param name="key">The key of the item to get.</param>
 81    /// <returns>The item associated with the specified key, or default if not found.</returns>
 82    public T? this[TKey key]
 83    {
 84        get
 85        {
 3086            ArgumentNullException.ThrowIfNull(key);
 87
 88            // Fast path: Use dictionary if available
 3089            if (_dict is not null)
 90            {
 2791                return _dict.GetValueOrDefault(key);
 92            }
 93
 94            // Slow path: Linear search
 95            // Consider creating dictionary if we're searching frequently
 396            EnsureDictionaryIfNeeded();
 97
 398            return Items.FirstOrDefault(x => Comparer.Equals(GetKeyForItem(x), key));
 99        }
 100    }
 101
 102    /// <summary>
 103    /// Determines whether the collection contains an element with the specified key.
 104    /// </summary>
 105    /// <param name="key">The key to locate in the collection.</param>
 106    /// <returns>True if an element with the key exists; otherwise false.</returns>
 107    public bool Contains(TKey key)
 108    {
 30109        ArgumentNullException.ThrowIfNull(key);
 110
 30111        if (_dict is not null)
 112        {
 30113            return _dict.ContainsKey(key);
 114        }
 115
 0116        EnsureDictionaryIfNeeded();
 0117        return Items.Any(x => Comparer.Equals(GetKeyForItem(x), key));
 118    }
 119
 120    /// <summary>
 121    /// Attempts to get the value associated with the specified key.
 122    /// </summary>
 123    /// <param name="key">The key to locate.</param>
 124    /// <param name="value">The value if found.</param>
 125    /// <returns>True if the key was found; otherwise false.</returns>
 126    public bool TryGetValue(TKey key, out T? value)
 127    {
 6128        ArgumentNullException.ThrowIfNull(key);
 129
 6130        if (_dict is not null)
 131        {
 6132            return _dict.TryGetValue(key, out value);
 133        }
 134
 0135        EnsureDictionaryIfNeeded();
 136
 0137        value = Items.FirstOrDefault(x => Comparer.Equals(GetKeyForItem(x), key));
 0138        return value is not null;
 139    }
 140
 141    /// <summary>
 142    /// Attempts to add an item to the collection if its key is not already present.
 143    /// </summary>
 144    /// <param name="item">The item to add.</param>
 145    /// <returns>True if the item was added; false if the key was null or already exists.</returns>
 146    public bool TryAdd(T item)
 147    {
 9148        var key = GetKeyForItem(item);
 9149        if (key is null) return false;
 150
 9151        EnsureDictionaryIfNeeded();
 152
 9153        if (_dict?.ContainsKey(key) == true)
 6154            return false;
 155
 3156        Add(item);
 3157        return true;
 158    }
 159
 160    /// <summary>
 161    /// Removes the item with the specified key from the collection.
 162    /// </summary>
 163    /// <param name="key">The key of the item to remove.</param>
 164    /// <returns>True if the item was found and removed; otherwise false.</returns>
 165    public bool Remove(TKey key)
 166    {
 9167        ArgumentNullException.ThrowIfNull(key);
 168
 169        // Fast path with dictionary
 9170        if (_dict is not null)
 171        {
 9172            return _dict.TryGetValue(key, out var item) && Remove(item);
 173        }
 174
 175        // Slow path without dictionary
 0176        for (var i = 0; i < Items.Count; i++)
 177        {
 0178            if (!Comparer.Equals(GetKeyForItem(Items[i]), key)) continue;
 0179            RemoveItem(i);
 0180            return true;
 181        }
 182
 0183        return false;
 184    }
 185
 186    /// <summary>
 187    /// Changes the key associated with an existing item in the collection.
 188    /// </summary>
 189    /// <param name="item">The item whose key is changing.</param>
 190    /// <param name="newKey">The new key to associate with the item.</param>
 191    protected void ChangeItemKey(T item, TKey? newKey)
 0192        => ChangeItemKey(item, newKey, default);
 193
 194    /// <summary>
 195    /// Changes the key associated with an existing item in the collection.
 196    /// </summary>
 197    /// <param name="item">The item whose key is changing.</param>
 198    /// <param name="newKey">The new key to associate with the item.</param>
 199    /// <param name="oldKey">The old key to remove. If default, GetKeyForItem is used.</param>
 200    protected void ChangeItemKey(T item, TKey? newKey, TKey? oldKey)
 201    {
 3202        if (!ContainsItem(item))
 0203            return;
 204
 205        // Use provided oldKey or get it from item
 3206        oldKey ??= GetKeyForItem(item);
 207
 3208        if (Comparer.Equals(oldKey, newKey))
 0209            return;
 210
 3211        if (newKey is not null)
 212        {
 3213            AddKeyInternal(newKey, item);
 214        }
 215
 3216        if (oldKey is not null)
 217        {
 3218            RemoveKeyInternal(oldKey);
 219        }
 3220    }
 221
 222    /// <inheritdoc />
 223    protected override void ClearItems()
 224    {
 3225        _dict?.Clear();
 3226        base.ClearItems();
 3227    }
 228
 229    /// <summary>
 230    /// When implemented in a derived class, returns the key for the specified item.
 231    /// </summary>
 232    /// <param name="item">The item to extract the key from.</param>
 233    /// <returns>The key for the specified item, or null if no key is associated.</returns>
 234    protected abstract TKey? GetKeyForItem(T item);
 235
 236    protected override void InsertItem(int index, T item)
 237    {
 360238        var key = GetKeyForItem(item);
 239
 240        // Add to base collection first
 360241        base.InsertItem(index, item);
 242
 243        // Then handle dictionary
 363244        if (key is null) return;
 357245        switch (_dict)
 246        {
 247            // Check threshold AFTER item is added
 248            // Special case: if threshold is 0, always use dictionary
 24249            case null when
 24250                (_dictionaryCreationThreshold != 0 || Count <= 0) &&
 24251                Count <= _dictionaryCreationThreshold:
 21252                return;
 253            case null:
 3254                CreateDictionary();
 3255                break;
 256            default:
 333257                _dict.Add(key, item);
 258                break;
 259        }
 333260    }
 261
 262    /// <summary>
 263    /// Inserts an item directly into the underlying Items collection without dictionary handling.
 264    /// Use with caution - this bypasses key tracking.
 265    /// </summary>
 266    /// <param name="index">The position at which to insert the item.</param>
 267    /// <param name="item">The item to insert.</param>
 0268    protected void InsertItemInItems(int index, T item) => base.InsertItem(index, item);
 269
 270    /// <summary>
 271    /// Adds the elements of the specified collection to the end of the collection.
 272    /// Overridden to ensure dictionary is updated.
 273    /// </summary>
 274    /// <param name="items">The collection whose elements should be added.</param>
 275    public override void AddRange(IEnumerable<T> items)
 276    {
 9277        ArgumentNullException.ThrowIfNull(items);
 278
 9279        var initialCount = Count;
 280
 281        // Call parent AddRange which adds to Items directly
 9282        base.AddRange(items);
 283
 284        // Now update dictionary with all new items
 9285        if (Count <= initialCount) return;
 286
 287        // Check if we should create dictionary
 9288        if (_dict is null && (_dictionaryCreationThreshold == 0 || Count > _dictionaryCreationThreshold))
 289        {
 290            // Create dictionary and it will include all items
 0291            CreateDictionary();
 292        }
 9293        else if (_dict is not null)
 294        {
 295            // Dictionary exists, add only the new items
 296            // Items were added from initialCount to Count-1
 66297            for (var i = initialCount; i < Count; i++)
 298            {
 24299                var item = Items[i];
 24300                var key = GetKeyForItem(item);
 24301                if (key is not null)
 302                {
 24303                    _dict.TryAdd(key, item); // Use TryAdd to avoid exceptions on duplicates
 304                }
 305            }
 306        }
 9307    }
 308
 309    /// <summary>
 310    /// Removes the items with the specified keys from the collection.
 311    /// </summary>
 312    /// <param name="keys">The keys of the items to remove.</param>
 313    /// <returns>The number of items removed.</returns>
 314    public int RemoveRange(IEnumerable<TKey> keys)
 315    {
 0316        ArgumentNullException.ThrowIfNull(keys);
 317
 0318        var set = new HashSet<TKey>(keys, Comparer);
 319
 0320        var removedCount = 0;
 321
 322        // First, remove from dictionary
 0323        if (_dict is not null)
 324        {
 0325            removedCount += set.Count(key => _dict.Remove(key));
 326        }
 327
 328        // Then, remove from base items collection
 0329        for (var i = Items.Count - 1; i >= 0; i--)
 330        {
 0331            var key = GetKeyForItem(Items[i]);
 0332            if (key is null || !set.Contains(key)) continue;
 0333            RemoveItem(i);
 0334            removedCount++;
 335        }
 336
 0337        return removedCount;
 338    }
 339
 340    protected override void RemoveItem(int index)
 341    {
 6342        var key = GetKeyForItem(Items[index]);
 343
 6344        base.RemoveItem(index);
 345
 6346        if (key is not null)
 347        {
 6348            RemoveKeyInternal(key);
 349        }
 6350    }
 351
 352    protected override void SetItem(int index, T item)
 353    {
 3354        var newKey = GetKeyForItem(item);
 3355        var oldKey = GetKeyForItem(Items[index]);
 356
 3357        if (Comparer.Equals(oldKey, newKey))
 358        {
 0359            if (newKey is not null && _dict is not null)
 360            {
 0361                _dict[newKey] = item;
 362            }
 363        }
 364        else
 365        {
 3366            if (newKey is not null)
 367            {
 3368                EnsureDictionaryIfNeeded();
 3369                AddKeyInternal(newKey, item);
 370            }
 371
 3372            if (oldKey is not null)
 373            {
 3374                RemoveKeyInternal(oldKey);
 375            }
 376        }
 377
 3378        base.SetItem(index, item);
 3379    }
 380
 381    /// <summary>
 382    /// Ensures the dictionary is created if the collection size warrants it.
 383    /// </summary>
 384    private void EnsureDictionaryIfNeeded()
 385    {
 15386        if (_dict is null && Count >= _dictionaryCreationThreshold)
 387        {
 0388            CreateDictionary();
 389        }
 15390    }
 391
 392    /// <summary>
 393    /// Forces creation of the dictionary regardless of size.
 394    /// </summary>
 395    protected void CreateDictionaryNow()
 396    {
 3397        if (_dict is null)
 398        {
 3399            CreateDictionary();
 400        }
 3401    }
 402
 403    private bool ContainsItem(T item)
 404    {
 3405        var key = GetKeyForItem(item);
 406
 3407        return _dict is null || key is null
 3408            ? Items.Contains(item)
 3409            : _dict.TryGetValue(key, out var itemInDict) && EqualityComparer<T>.Default.Equals(itemInDict, item);
 410    }
 411
 412    /// <summary>
 413    /// Internal add key without additional locking (assumes already locked).
 414    /// </summary>
 415    private void AddKeyInternal(TKey key, T item)
 416    {
 6417        if (_dict is null)
 418        {
 0419            CreateDictionary();
 420        }
 421
 6422        _dict?.Add(key, item);
 6423    }
 424
 425    /// <summary>
 426    /// Internal remove key without additional locking (assumes already locked).
 427    /// </summary>
 12428    private void RemoveKeyInternal(TKey key) => _dict?.Remove(key);
 429
 430    private void CreateDictionary()
 431    {
 432        // Pre-allocate with current count + some headroom
 69433        var capacity = Math.Max(Count, 16);
 69434        _dict = new Dictionary<TKey, T>(capacity, Comparer);
 435
 180436        foreach (var item in Items)
 437        {
 21438            var key = GetKeyForItem(item);
 21439            if (key is not null)
 440            {
 441                // Use TryAdd to avoid exceptions if duplicate keys exist
 21442                _dict.TryAdd(key, item);
 443            }
 444        }
 69445    }
 446
 447    /// <summary>
 448    /// Gets statistics about the dictionary usage.
 449    /// </summary>
 450    protected (bool Created, int Count, int Capacity) GetDictionaryStats() =>
 3451        _dict is null ? (false, 0, 0) : (true, _dict.Count, _dict.EnsureCapacity(0));
 452}
 453