< Summary

Information
Class: Chronicler.Serialization.OrderedStringMap<T>
Assembly: Chronicler
File(s): /home/runner/work/Chronicler/Chronicler/src/Chronicler/Serialization/OrderedStringMap.cs
Line coverage
100%
Covered lines: 29
Uncovered lines: 0
Coverable lines: 29
Total lines: 83
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Count()100%11100%
set_Item(...)100%11100%
TryGetValue(...)100%22100%
Remove(...)100%44100%
GetEnumerator()100%11100%
System.Collections.IEnumerable.GetEnumerator()100%11100%
Set(...)100%44100%

File(s)

/home/runner/work/Chronicler/Chronicler/src/Chronicler/Serialization/OrderedStringMap.cs

#LineLine coverage
 1//=======================================================================
 2// OrderedStringMap.cs
 3//=======================================================================
 4// MIT License, Copyright (c) 2024-present David Oravsky (mrdav30)
 5// See LICENSE file in the project root for full license information.
 6//=======================================================================
 7
 8using System;
 9using System.Collections;
 10using System.Collections.Generic;
 11
 12namespace Chronicler.Serialization;
 13
 14internal sealed class OrderedStringMap<TValue> : IEnumerable<KeyValuePair<string, TValue>>
 15{
 16    private readonly List<KeyValuePair<string, TValue>> _entries;
 17    private readonly Dictionary<string, int> _indexes;
 18
 179219    public OrderedStringMap(int capacity, IEqualityComparer<string> comparer)
 20    {
 179221        _entries = new List<KeyValuePair<string, TValue>>(capacity);
 179222        _indexes = new Dictionary<string, int>(capacity, comparer);
 179223    }
 24
 51125    public int Count => _entries.Count;
 26
 27    public TValue this[string key]
 28    {
 182829        set => Set(key, value);
 30    }
 31
 32    public bool TryGetValue(string key, out TValue value)
 33    {
 57534        if (!_indexes.TryGetValue(key, out int index))
 35        {
 17536            value = default!;
 17537            return false;
 38        }
 39
 40040        value = _entries[index].Value;
 40041        return true;
 42    }
 43
 44    public bool Remove(string key)
 45    {
 4346        if (!_indexes.TryGetValue(key, out int index))
 747            return false;
 48
 3649        _entries.RemoveAt(index);
 3650        _indexes.Remove(key);
 51
 10852        for (int i = index; i < _entries.Count; i++)
 1853            _indexes[_entries[i].Key] = i;
 54
 3655        return true;
 56    }
 57
 58    public IEnumerator<KeyValuePair<string, TValue>> GetEnumerator()
 59    {
 73860        return _entries.GetEnumerator();
 61    }
 62
 63    IEnumerator IEnumerable.GetEnumerator()
 64    {
 165        return GetEnumerator();
 66    }
 67
 68    private void Set(string key, TValue value)
 69    {
 182870        if (key == null)
 171            throw new ArgumentNullException(nameof(key));
 72
 182773        if (_indexes.TryGetValue(key, out int index))
 74        {
 6375            string existingKey = _entries[index].Key;
 6376            _entries[index] = new KeyValuePair<string, TValue>(existingKey, value);
 6377            return;
 78        }
 79
 176480        _indexes.Add(key, _entries.Count);
 176481        _entries.Add(new KeyValuePair<string, TValue>(key, value));
 176482    }
 83}