< Summary

Information
Line coverage
0%
Covered lines: 0
Uncovered lines: 29
Coverable lines: 29
Total lines: 64
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

https://raw.githubusercontent.com/dotnet/runtime/811a7eabb75c42db53440e8ba3f60c07511cfd1f/src/libraries/Common/src/System/Collections/Generic/BidirectionalDictionary.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Diagnostics;
 5using System.Diagnostics.CodeAnalysis;
 6
 7namespace System.Collections.Generic
 8{
 9    internal sealed class BidirectionalDictionary<T1, T2> : IEnumerable<KeyValuePair<T1, T2>>
 10            where T1 : notnull
 11            where T2 : notnull
 12    {
 13        private readonly Dictionary<T1, T2> _forward;
 14        private readonly Dictionary<T2, T1> _backward;
 15
 016        public BidirectionalDictionary(int capacity)
 017        {
 018            _forward = new Dictionary<T1, T2>(capacity);
 019            _backward = new Dictionary<T2, T1>(capacity);
 020        }
 21
 22        public int Count
 23        {
 24            get
 025            {
 026                Debug.Assert(_forward.Count == _backward.Count, "both the dictionaries must have the same number of elem
 027                return _forward.Count;
 028            }
 29        }
 30
 31        public void Add(T1 item1, T2 item2)
 032        {
 033            Debug.Assert(!_backward.ContainsKey(item2), "No added item1 should ever have existing item2");
 034            _forward.Add(item1, item2);
 035            _backward.Add(item2, item1);
 036        }
 37
 38        public bool TryGetForward(T1 item1, [MaybeNullWhen(false)] out T2 item2)
 039        {
 040            return _forward.TryGetValue(item1, out item2);
 041        }
 42
 43        public bool TryGetBackward(T2 item2, [MaybeNullWhen(false)] out T1 item1)
 044        {
 045            return _backward.TryGetValue(item2, out item1);
 046        }
 47
 48        public Dictionary<T1, T2>.Enumerator GetEnumerator()
 049        {
 050            return _forward.GetEnumerator();
 051        }
 52
 53        IEnumerator<KeyValuePair<T1, T2>> IEnumerable<KeyValuePair<T1, T2>>.GetEnumerator()
 054        {
 055            return GetEnumerator();
 056        }
 57
 58        IEnumerator IEnumerable.GetEnumerator()
 059        {
 060            return GetEnumerator();
 061        }
 62    }
 63}
 64