C# Unity 複数の値に対応する 1 つのキーを持つ辞書

C# Unity 複数の値に対応する 1 つのキーを持つ辞書

1。目的

C# では、複数の値に対応するキーを持つコンテナーは提供されません。自分でカプセル化する必要があります。実装方法は多数あります。リンク リストを使用して実装できます (リンク リストを領域に分割するだけです)。ただし、これはより複雑ですが、頻繁に挿入する方がリストを使用するよりも優れています。より良いものは長さを動的に拡張する必要がありません。機会があれば、リンクされたリストを使用して 1 つのキーをより効率的に取得する方法を共有できます。今日は、簡単で早くてわかりやすい方法を紹介します。

2.OneToManyDic

C# は、値に対応するキーを提供する辞書を使用し、それを展開できます (辞書の継承)

public class OneToManyDic<TKey, TValue> : Dictionary<TKey, List<TValue>> where TKey : notnull

削除中にリストが頻繁に作成されるのを避けるために、Queue キューを使用してリストをキャッシュします。

        private readonly Queue<List<TValue>> _cacheQueue = new Queue<List<TValue>>(); // 缓存list
        private readonly int _recyclingLimit = 120;
        public OneToManyDic()
        {
    
    

        }
        /// <summary>
        /// 设置最大缓存数量
        /// </summary>
        /// <param name="recyclingLimit">
        /// 1:防止数据量过大、所以超过recyclingLimit的数据还是走GC.
        /// 2:设置成0不控制数量,全部缓存
        /// </param>
        public OneToManyDic(int recyclingLimit)
        {
    
    
            _recyclingLimit = recyclingLimit;
        }

3. 完全なコード

public class OneToManyDic<TKey, TValue> : Dictionary<TKey, List<TValue>> where TKey : notnull
    {
    
    
        private readonly Queue<List<TValue>> _cacheQueue = new Queue<List<TValue>>(); // 缓存list
        private readonly int _recyclingLimit = 120;
        public OneToManyDic()
        {
    
    

        }
        /// <summary>
        /// 设置最大缓存数量
        /// </summary>
        /// <param name="recyclingLimit">
        /// 1:防止数据量过大、所以超过recyclingLimit的数据还是走GC.
        /// 2:设置成0不控制数量,全部缓存
        /// </param>
        public OneToManyDic(int recyclingLimit)
        {
    
    
            _recyclingLimit = recyclingLimit;
        }

        public bool Contains(TKey key, TValue value)
        {
    
    
            TryGetValue(key, out var list);

            return list != null && list.Contains(value);
        }

        public void Add(TKey key, TValue value)
        {
    
    
            if (!TryGetValue(key, out var list))
            {
    
    
                list = Fetch();
                list.Add(value);
                Add(key, list);
                return;
            }
            list.Add(value);
        }

        public TValue? First(TKey key)
        {
    
    
            return !TryGetValue(key, out var list) ? default : list.FirstOrDefault();
        }

        public bool RemoveValue(TKey key, TValue value)
        {
    
    
            if (!TryGetValue(key, out var list))
            {
    
    
                return true;
            }

            var isRemove = list.Remove(value);

            if (list.Count == 0)
            {
    
    
                isRemove = RemoveByKey(key);
            }

            return isRemove;
        }

        public bool RemoveByKey(TKey key)
        {
    
    
            if (!TryGetValue(key, out var list))
            {
    
    
                return false;
            }

            Remove(key);
            Recycle(list);
            return true;
        }

        public List<TValue>? GetValues(TKey key)
        {
    
    
            return TryGetValue(key, out var list) ? list : null;
        }

        public new void Clear()
        {
    
    
            foreach (var keyValuePair in this) Recycle(keyValuePair.Value);

            base.Clear();
        }

        private List<TValue> Fetch()
        {
    
    
            return _cacheQueue.Count <= 0 ? new List<TValue>() : _cacheQueue.Dequeue();
        }

        private void Recycle(List<TValue> list)
        {
    
    
            list.Clear();

            if (_recyclingLimit != 0 && _cacheQueue.Count > _recyclingLimit) return;

            _cacheQueue.Enqueue(list);
        }
    }

3. コードをテストする

OneToManyDic<int, string> dic = new OneToManyDic<int, string>();
dic.Add(1, "zzs");
dic.Add(1, "666");
dic.Add(2, "777");
dic.Add(2, "888");
dic.Add(2, "999");

Console.WriteLine(dic.Count);
Console.WriteLine(dic.GetValues(1)?.Count);
Console.WriteLine(dic.GetValues(2)?.Count);

Console.ReadKey();

おすすめ

転載: blog.csdn.net/zzzsss123333/article/details/132524280