< Summary

Information
Class: Chronicler.OrderedStringMap<T>
Assembly: Chronicler
File(s): /home/runner/work/Chronicler/Chronicler/src/Chronicler/Serialization/OrderedStringMap.cs
Line coverage
93%
Covered lines: 27
Uncovered lines: 2
Coverable lines: 29
Total lines: 76
Line coverage: 93.1%
Branch coverage
90%
Covered branches: 9
Total branches: 10
Branch coverage: 90%
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%210%
Set(...)75%4488.88%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4
 5namespace Chronicler;
 6
 7internal sealed class OrderedStringMap<TValue> : IEnumerable<KeyValuePair<string, TValue>>
 8{
 9    private readonly List<KeyValuePair<string, TValue>> _entries;
 10    private readonly Dictionary<string, int> _indexes;
 11
 35212    public OrderedStringMap(int capacity, IEqualityComparer<string> comparer)
 13    {
 35214        _entries = new List<KeyValuePair<string, TValue>>(capacity);
 35215        _indexes = new Dictionary<string, int>(capacity, comparer);
 35216    }
 17
 9618    public int Count => _entries.Count;
 19
 20    public TValue this[string key]
 21    {
 25622        set => Set(key, value);
 23    }
 24
 25    public bool TryGetValue(string key, out TValue value)
 26    {
 7727        if (!_indexes.TryGetValue(key, out int index))
 28        {
 2829            value = default!;
 2830            return false;
 31        }
 32
 4933        value = _entries[index].Value;
 4934        return true;
 35    }
 36
 37    public bool Remove(string key)
 38    {
 1139        if (!_indexes.TryGetValue(key, out int index))
 140            return false;
 41
 1042        _entries.RemoveAt(index);
 1043        _indexes.Remove(key);
 44
 3045        for (int i = index; i < _entries.Count; i++)
 546            _indexes[_entries[i].Key] = i;
 47
 1048        return true;
 49    }
 50
 51    public IEnumerator<KeyValuePair<string, TValue>> GetEnumerator()
 52    {
 15253        return _entries.GetEnumerator();
 54    }
 55
 56    IEnumerator IEnumerable.GetEnumerator()
 57    {
 058        return GetEnumerator();
 59    }
 60
 61    private void Set(string key, TValue value)
 62    {
 25663        if (key == null)
 064            throw new ArgumentNullException(nameof(key));
 65
 25666        if (_indexes.TryGetValue(key, out int index))
 67        {
 568            string existingKey = _entries[index].Key;
 569            _entries[index] = new KeyValuePair<string, TValue>(existingKey, value);
 570            return;
 71        }
 72
 25173        _indexes.Add(key, _entries.Count);
 25174        _entries.Add(new KeyValuePair<string, TValue>(key, value));
 25175    }
 76}