< Summary

Class:BaseRefCountedCollection[T]
Assembly:BaseVariable
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/DataStore/Variables/BaseRefCountedCollection.cs
Covered lines:34
Uncovered lines:8
Coverable lines:42
Total lines:93
Line coverage:80.9% (34 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%110100%
GetRefCount(...)0%2.152066.67%
SetRefCounts(...)0%330100%
SetRefCount(...)0%330100%
IncreaseRefCount(...)0%330100%
DecreaseRefCount(...)0%440100%
Clear()0%220100%
Count()0%110100%
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
 44110    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
 88214    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
 325    public IEnumerable<KeyValuePair<T, int>> GetAllRefCounts() => dictionary;
 26
 27    public int GetRefCount(T key)
 28    {
 1229        if (!dictionary.ContainsKey(key))
 030            return 0;
 1231        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    {
 20045        if (dictionary.ContainsKey(key))
 446            dictionary[key] = count;
 47        else
 19648            dictionary.Add(key, count);
 49
 20050        OnRefCountUpdated?.Invoke(key, count);
 851    }
 52
 53    public void IncreaseRefCount(T key)
 54    {
 302255        if (dictionary.ContainsKey(key))
 456            dictionary[key]++;
 57        else
 301858            dictionary.Add(key, 1);
 59
 302260        OnRefCountUpdated?.Invoke(key, dictionary[key]);
 961    }
 62
 63    public void DecreaseRefCount(T key)
 64    {
 7465        if (!dictionary.ContainsKey(key))
 3366            return;
 67
 4168        int newCount = Math.Max(0, dictionary[key] - 1);
 4169        if (newCount == 0)
 3870            dictionary.Remove(key);
 71        else
 372            dictionary[key] = newCount;
 73
 4174        OnRefCountUpdated?.Invoke(key, newCount);
 875    }
 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
 488    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}