< Summary

Class:BaseRefCountedCollection[T]
Assembly:BaseVariable
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/DataStore/Variables/BaseRefCountedCollection.cs
Covered lines:32
Uncovered lines:10
Coverable lines:42
Total lines:93
Line coverage:76.1% (32 of 42)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BaseRefCountedCollection()0%110100%
BaseRefCountedCollection(...)0%12300%
GetAllRefCounts()0%2100%
GetRefCount(...)0%2.152066.67%
SetRefCounts(...)0%330100%
SetRefCount(...)0%330100%
IncreaseRefCount(...)0%330100%
DecreaseRefCount(...)0%440100%
Clear()0%220100%
Count()0%2100%
GetEnumerator()0%2100%
GetEnumerator()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/DataStore/Variables/BaseRefCountedCollection.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using System.Linq;
 5
 6public class BaseRefCountedCollection<T> : IEnumerable<KeyValuePair<T, int>>
 7{
 8    public event Action<T, int> OnRefCountUpdated;
 9
 69910    internal readonly Dictionary<T, int> dictionary = new Dictionary<T, int>();
 11
 712    public int this[T key] { get => dictionary[key]; set => dictionary[key] = value; }
 13
 139814    public BaseRefCountedCollection() { }
 15
 016    public BaseRefCountedCollection(IEnumerable<(T, int)> elements)
 17    {
 018        dictionary = new Dictionary<T, int>();
 019        foreach ((T key, int refCount) in elements)
 20        {
 021            dictionary.Add(key, refCount);
 22        }
 023    }
 24
 025    public IEnumerable<KeyValuePair<T, int>> GetAllRefCounts() => dictionary;
 26
 27    public int GetRefCount(T key)
 28    {
 929        if (!dictionary.ContainsKey(key))
 030            return 0;
 931        return dictionary[key];
 32    }
 33
 34    public void SetRefCounts(IEnumerable<(T, int)> elements)
 35    {
 136        dictionary.Clear();
 837        foreach ((T key, int value) in elements)
 38        {
 339            SetRefCount(key, value);
 40        }
 141    }
 42
 43    public void SetRefCount(T key, int count)
 44    {
 1345        if (dictionary.ContainsKey(key))
 446            dictionary[key] = count;
 47        else
 948            dictionary.Add(key, count);
 49
 1350        OnRefCountUpdated?.Invoke(key, count);
 751    }
 52
 53    public void IncreaseRefCount(T key)
 54    {
 1455        if (dictionary.ContainsKey(key))
 456            dictionary[key]++;
 57        else
 1058            dictionary.Add(key, 1);
 59
 1460        OnRefCountUpdated?.Invoke(key, dictionary[key]);
 261    }
 62
 63    public void DecreaseRefCount(T key)
 64    {
 2365        if (!dictionary.ContainsKey(key))
 666            return;
 67
 1768        int newCount = Math.Max(0, dictionary[key] - 1);
 1769        if (newCount == 0)
 1170            dictionary.Remove(key);
 71        else
 672            dictionary[key] = newCount;
 73
 1774        OnRefCountUpdated?.Invoke(key, newCount);
 275    }
 76
 77    public void Clear()
 78    {
 179        T[] keys = dictionary.Keys.ToArray();
 880        foreach (T key in keys)
 81        {
 382            SetRefCount(key, 0);
 83        }
 84
 185        dictionary.Clear();
 186    }
 87
 088    public int Count() => dictionary.Count;
 89
 090    public IEnumerator<KeyValuePair<T, int>> GetEnumerator() { return dictionary.GetEnumerator(); }
 91
 092    IEnumerator IEnumerable.GetEnumerator() { return dictionary.GetEnumerator(); }
 93}