< Summary

Class:BaseRefCounter[TKey]
Assembly:BaseVariable
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/DataStore/Variables/BaseRefCounter.cs
Covered lines:22
Uncovered lines:11
Coverable lines:33
Total lines:114
Line coverage:66.6% (22 of 33)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
BaseRefCounter()0%110100%
Get()0%2100%
Get(...)0%6200%
AddRefCount(...)0%4.054085.71%
AddRefCount(...)0%330100%
RemoveRefCount(...)0%330100%
RemoveRefCount(...)0%550100%
ContainsKey(...)0%2100%
Clear()0%2100%
Count()0%110100%
GetEnumerator()0%2100%
GetEnumerator()0%2100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Collections.Generic;
 4using UnityEngine;
 5
 6public class BaseRefCounter<TKey> : IEnumerable<KeyValuePair<TKey, int>>
 7{
 8    public event Action<TKey> OnAdded;
 9    public event Action<TKey> OnRemoved;
 10
 248811    internal readonly Dictionary<TKey, int> dictionary = new Dictionary<TKey, int>();
 12
 013    public int this[TKey key] { get => dictionary[key]; set => dictionary[key] = value; }
 14
 15    public IEnumerable<KeyValuePair<TKey, int>> Get()
 16    {
 017        return dictionary;
 18    }
 19
 20    public int Get(TKey key)
 21    {
 022        if ( !dictionary.ContainsKey(key))
 023            return 0;
 24
 025        return dictionary[key];
 26    }
 27
 28    /// <summary>
 29    /// Adds one to the reference count for the given key.
 30    /// </summary>
 31    /// <param name="key">A given key to be ref counted.</param>
 32    public void AddRefCount(TKey key)
 33    {
 86734        if ( key == null )
 035            return;
 36
 86737        if ( !dictionary.ContainsKey(key) )
 38        {
 77539            dictionary.Add(key, 0);
 77540            OnAdded?.Invoke(key);
 41        }
 42
 86743        dictionary[key]++;
 86744    }
 45
 46    /// <summary>
 47    /// Adds one to the reference count for the given key.
 48    /// </summary>
 49    /// <param name="key">A given key to be ref counted.</param>
 50    public void AddRefCount(IEnumerable<TKey> keys)
 51    {
 553452        foreach ( TKey key in keys )
 53        {
 86754            AddRefCount(key);
 55        }
 190056    }
 57
 58    /// <summary>
 59    /// Adds one to the reference count for the given keys.
 60    /// </summary>
 61    /// <param name="keys">A given set of keys to be ref counted.</param>
 62    public void RemoveRefCount(IEnumerable<TKey> keys)
 63    {
 296864        foreach ( TKey key in keys )
 65        {
 34466            RemoveRefCount(key);
 67        }
 114068    }
 69
 70    /// <summary>
 71    /// Removes the reference count for the given key.
 72    /// </summary>
 73    /// <param name="key">A given key to be ref counted.</param>
 74    /// <returns>true if the element count reached zero and was removed.</returns>
 75    public bool RemoveRefCount(TKey key)
 76    {
 34477        if ( key == null || !dictionary.ContainsKey(key) )
 6178            return false;
 79
 28380        dictionary[key]--;
 81
 28382        if ( dictionary[key] != 0 )
 5183            return false;
 84
 23285        dictionary.Remove(key);
 23286        OnRemoved?.Invoke(key);
 23287        return true;
 88    }
 89
 90    public bool ContainsKey(TKey key)
 91    {
 092        return dictionary.ContainsKey(key);
 93    }
 94
 95    public void Clear()
 96    {
 097        dictionary.Clear();
 098    }
 99
 100    public int Count()
 101    {
 763102        return dictionary.Count;
 103    }
 104
 105    public IEnumerator<KeyValuePair<TKey, int>> GetEnumerator()
 106    {
 0107        return dictionary.GetEnumerator();
 108    }
 109
 110    IEnumerator IEnumerable.GetEnumerator()
 111    {
 0112        return dictionary.GetEnumerator();
 113    }
 114}