< Summary

Class:BaseRefCounter[TKey]
Assembly:BaseVariable
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/DataStore/Variables/BaseRefCounter.cs
Covered lines:21
Uncovered lines:12
Coverable lines:33
Total lines:114
Line coverage:63.6% (21 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%2100%
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
 152111    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    {
 76434        if ( key == null )
 035            return;
 36
 76437        if ( !dictionary.ContainsKey(key) )
 38        {
 67439            dictionary.Add(key, 0);
 67440            OnAdded?.Invoke(key);
 41        }
 42
 76443        dictionary[key]++;
 76444    }
 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    {
 418652        foreach ( TKey key in keys )
 53        {
 76454            AddRefCount(key);
 55        }
 132956    }
 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    {
 217264        foreach ( TKey key in keys )
 65        {
 30966            RemoveRefCount(key);
 67        }
 77768    }
 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    {
 30977        if ( key == null || !dictionary.ContainsKey(key) )
 5978            return false;
 79
 25080        dictionary[key]--;
 81
 25082        if ( dictionary[key] != 0 )
 5383            return false;
 84
 19785        dictionary.Remove(key);
 19786        OnRemoved?.Invoke(key);
 19787        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    {
 0102        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}