• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CalionVarduk / LfrlAnvil / 9288918066

29 May 2024 03:27PM UTC coverage: 96.92%. Remained the same
9288918066

push

github

CalionVarduk
Global:
- Add docfx cake script;

14635 of 15454 branches covered (94.7%)

Branch coverage included in aggregate %.

41635 of 42604 relevant lines covered (97.73%)

35160.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.02
/src/LfrlAnvil.Collections/TwoWayDictionary.cs
1
using System.Collections;
2
using System.Collections.Generic;
3
using System.Diagnostics.CodeAnalysis;
4
using System.Diagnostics.Contracts;
5
using System.Linq;
6
using LfrlAnvil.Collections.Exceptions;
7
using LfrlAnvil.Collections.Internal;
8

9
namespace LfrlAnvil.Collections;
10

11
/// <inheritdoc cref="ITwoWayDictionary{T1,T2}" />
12
public class TwoWayDictionary<T1, T2> : ITwoWayDictionary<T1, T2>
13
    where T1 : notnull
14
    where T2 : notnull
15
{
16
    private readonly Dictionary<T1, T2> _forward;
17
    private readonly Dictionary<T2, T1> _reverse;
18

19
    /// <summary>
20
    /// Creates a new empty <see cref="TwoWayDictionary{T1,T2}"/> instance
21
    /// with <see cref="EqualityComparer{T}.Default"/> forward and reverse comparer.
22
    /// </summary>
23
    public TwoWayDictionary()
47✔
24
    {
25
        _forward = new Dictionary<T1, T2>();
47✔
26
        _reverse = new Dictionary<T2, T1>();
47✔
27
    }
47✔
28

29
    /// <summary>
30
    /// Creates a new empty <see cref="TwoWayDictionary{T1,T2}"/> instance.
31
    /// </summary>
32
    /// <param name="forwardComparer">Forward key equality comparer.</param>
33
    /// <param name="reverseComparer">Reverse key equality comparer.</param>
34
    public TwoWayDictionary(IEqualityComparer<T1> forwardComparer, IEqualityComparer<T2> reverseComparer)
1✔
35
    {
36
        _forward = new Dictionary<T1, T2>( forwardComparer );
1✔
37
        _reverse = new Dictionary<T2, T1>( reverseComparer );
1✔
38
    }
1✔
39

40
    /// <inheritdoc cref="ITwoWayDictionary{T1,T2}.Count" />
41
    public int Count => _forward.Count;
30✔
42

43
    /// <summary>
44
    /// Represents the <typeparamref name="T1"/> => <typeparamref name="T2"/> read-only dictionary.
45
    /// </summary>
46
    public IReadOnlyDictionary<T1, T2> Forward => _forward;
20✔
47

48
    /// <summary>
49
    /// Represents the <typeparamref name="T2"/> => <typeparamref name="T1"/> read-only dictionary.
50
    /// </summary>
51
    public IReadOnlyDictionary<T2, T1> Reverse => _reverse;
20✔
52

53
    /// <inheritdoc />
54
    public IEqualityComparer<T1> ForwardComparer => _forward.Comparer;
2✔
55

56
    /// <inheritdoc />
57
    public IEqualityComparer<T2> ReverseComparer => _reverse.Comparer;
2✔
58

59
    bool ICollection<Pair<T1, T2>>.IsReadOnly => (( ICollection<KeyValuePair<T1, T2>> )_forward).IsReadOnly;
×
60

61
    /// <inheritdoc />
62
    public bool TryAdd(T1 first, T2 second)
63
    {
64
        if ( _forward.ContainsKey( first ) || _reverse.ContainsKey( second ) )
4✔
65
            return false;
2✔
66

67
        _forward.Add( first, second );
2✔
68
        _reverse.Add( second, first );
2✔
69
        return true;
2✔
70
    }
71

72
    /// <inheritdoc />
73
    public void Add(T1 first, T2 second)
74
    {
75
        Ensure.False( _forward.ContainsKey( first ), Resources.KeyExistenceInForwardDictionary );
42✔
76
        Ensure.False( _reverse.ContainsKey( second ), Resources.KeyExistenceInReverseDictionary );
41✔
77
        _forward.Add( first, second );
40✔
78
        _reverse.Add( second, first );
40✔
79
    }
40✔
80

81
    /// <inheritdoc />
82
    public bool TryUpdateForward(T1 first, T2 second)
83
    {
84
        if ( _reverse.ContainsKey( second ) )
3✔
85
            return false;
1✔
86

87
        if ( ! _forward.TryGetValue( first, out var other ) )
2✔
88
            return false;
1✔
89

90
        _forward[first] = second;
1✔
91
        _reverse.Remove( other );
1✔
92
        _reverse.Add( second, first );
1✔
93
        return true;
1✔
94
    }
95

96
    /// <inheritdoc />
97
    public void UpdateForward(T1 first, T2 second)
98
    {
99
        Ensure.False( _reverse.ContainsKey( second ), Resources.KeyExistenceInReverseDictionary );
3✔
100
        var other = _forward[first];
2✔
101
        _forward[first] = second;
1✔
102
        _reverse.Remove( other );
1✔
103
        _reverse.Add( second, first );
1✔
104
    }
1✔
105

106
    /// <inheritdoc />
107
    public bool TryUpdateReverse(T2 second, T1 first)
108
    {
109
        if ( _forward.ContainsKey( first ) )
3✔
110
            return false;
1✔
111

112
        if ( ! _reverse.TryGetValue( second, out var other ) )
2✔
113
            return false;
1✔
114

115
        _reverse[second] = first;
1✔
116
        _forward.Remove( other );
1✔
117
        _forward.Add( first, second );
1✔
118
        return true;
1✔
119
    }
120

121
    /// <inheritdoc />
122
    public void UpdateReverse(T2 second, T1 first)
123
    {
124
        Ensure.False( _forward.ContainsKey( first ), Resources.KeyExistenceInForwardDictionary );
3✔
125
        var other = _reverse[second];
2✔
126
        _reverse[second] = first;
1✔
127
        _forward.Remove( other );
1✔
128
        _forward.Add( first, second );
1✔
129
    }
1✔
130

131
    /// <inheritdoc />
132
    public bool RemoveForward(T1 value)
133
    {
134
        return RemoveForward( value, out _ );
2✔
135
    }
136

137
    /// <inheritdoc />
138
    public bool RemoveReverse(T2 value)
139
    {
140
        return RemoveReverse( value, out _ );
2✔
141
    }
142

143
    /// <inheritdoc />
144
    public bool RemoveForward(T1 value, [MaybeNullWhen( false )] out T2 second)
145
    {
146
        if ( ! _forward.Remove( value, out second ) )
4✔
147
            return false;
2✔
148

149
        _reverse.Remove( second );
2✔
150
        return true;
2✔
151
    }
152

153
    /// <inheritdoc />
154
    public bool RemoveReverse(T2 value, [MaybeNullWhen( false )] out T1 first)
155
    {
156
        if ( ! _reverse.Remove( value, out first ) )
4✔
157
            return false;
2✔
158

159
        _forward.Remove( first );
2✔
160
        return true;
2✔
161
    }
162

163
    /// <inheritdoc />
164
    public void Clear()
165
    {
166
        _forward.Clear();
1✔
167
        _reverse.Clear();
1✔
168
    }
1✔
169

170
    /// <inheritdoc />
171
    [Pure]
172
    public bool Contains(T1 first, T2 second)
173
    {
174
        return _forward.TryGetValue( first, out var existingSecond ) && _reverse.Comparer.Equals( existingSecond, second );
10✔
175
    }
176

177
    /// <inheritdoc />
178
    [Pure]
179
    public bool Contains(Pair<T1, T2> item)
180
    {
181
        return Contains( item.First, item.Second );
6✔
182
    }
183

184
    /// <inheritdoc />
185
    [Pure]
186
    public IEnumerator<Pair<T1, T2>> GetEnumerator()
187
    {
188
        return _forward.Select( static kv => Pair.Create( kv.Key, kv.Value ) ).GetEnumerator();
8✔
189
    }
190

191
    [Pure]
192
    IEnumerator IEnumerable.GetEnumerator()
193
    {
194
        return GetEnumerator();
1✔
195
    }
196

197
    void ICollection<Pair<T1, T2>>.Add(Pair<T1, T2> item)
198
    {
199
        Add( item.First, item.Second );
6✔
200
    }
6✔
201

202
    bool ICollection<Pair<T1, T2>>.Remove(Pair<T1, T2> item)
203
    {
204
        if ( ! _forward.TryGetValue( item.First, out var second ) || ! _reverse.Comparer.Equals( second, item.Second ) )
4✔
205
            return false;
3✔
206

207
        _forward.Remove( item.First );
1✔
208
        _reverse.Remove( item.Second );
1✔
209
        return true;
1✔
210
    }
211

212
    void ICollection<Pair<T1, T2>>.CopyTo(Pair<T1, T2>[] array, int arrayIndex)
213
    {
214
        CollectionCopying.CopyTo( this, array, arrayIndex );
1✔
215
    }
1✔
216
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc