6
6
using System . Collections ;
7
7
using System . Collections . Generic ;
8
8
using System . Diagnostics . CodeAnalysis ;
9
+ using Microsoft . CodeAnalysis . Collections . Internal ;
9
10
10
11
namespace Microsoft . CodeAnalysis . Collections
11
12
{
@@ -14,50 +15,26 @@ internal readonly partial struct ImmutableSegmentedDictionary<TKey, TValue>
14
15
public sealed partial class Builder : IDictionary < TKey , TValue > , IReadOnlyDictionary < TKey , TValue > , IDictionary
15
16
{
16
17
/// <summary>
17
- /// The immutable collection this builder is based on .
18
+ /// The private builder implementation .
18
19
/// </summary>
19
- private ImmutableSegmentedDictionary < TKey , TValue > _dictionary ;
20
-
21
- /// <summary>
22
- /// The current mutable collection this builder is operating on. This field is initialized to a copy of
23
- /// <see cref="_dictionary"/> the first time a change is made.
24
- /// </summary>
25
- private SegmentedDictionary < TKey , TValue > ? _mutableDictionary ;
20
+ private ValueBuilder _builder ;
26
21
27
22
internal Builder ( ImmutableSegmentedDictionary < TKey , TValue > dictionary )
28
- {
29
- _dictionary = dictionary ;
30
- }
23
+ => _builder = new ValueBuilder ( dictionary ) ;
31
24
32
25
public IEqualityComparer < TKey > KeyComparer
33
26
{
34
- get
35
- {
36
- return ReadOnlyDictionary . Comparer ;
37
- }
38
-
39
- set
40
- {
41
- if ( value is null )
42
- throw new ArgumentNullException ( nameof ( value ) ) ;
43
-
44
- if ( value != KeyComparer )
45
- {
46
- // Rewrite the mutable dictionary using a new comparer
47
- var valuesToAdd = ReadOnlyDictionary ;
48
- _mutableDictionary = new SegmentedDictionary < TKey , TValue > ( value ) ;
49
- AddRange ( valuesToAdd ) ;
50
- }
51
- }
27
+ get => _builder . KeyComparer ;
28
+ set => _builder . KeyComparer = value ;
52
29
}
53
30
54
- public int Count => ReadOnlyDictionary . Count ;
31
+ public int Count => _builder . Count ;
55
32
56
33
public KeyCollection Keys => new ( this ) ;
57
34
58
35
public ValueCollection Values => new ( this ) ;
59
36
60
- private SegmentedDictionary < TKey , TValue > ReadOnlyDictionary => _mutableDictionary ?? _dictionary . _dictionary ;
37
+ private SegmentedDictionary < TKey , TValue > ReadOnlyDictionary => _builder . ReadOnlyDictionary ;
61
38
62
39
IEnumerable < TKey > IReadOnlyDictionary < TKey , TValue > . Keys => Keys ;
63
40
@@ -67,189 +44,113 @@ public IEqualityComparer<TKey> KeyComparer
67
44
68
45
ICollection < TValue > IDictionary < TKey , TValue > . Values => Values ;
69
46
70
- bool ICollection < KeyValuePair < TKey , TValue > > . IsReadOnly => false ;
47
+ bool ICollection < KeyValuePair < TKey , TValue > > . IsReadOnly => ICollectionCalls < KeyValuePair < TKey , TValue > > . IsReadOnly ( ref _builder ) ;
71
48
72
49
ICollection IDictionary . Keys => Keys ;
73
50
74
51
ICollection IDictionary . Values => Values ;
75
52
76
- bool IDictionary . IsReadOnly => false ;
53
+ bool IDictionary . IsReadOnly => IDictionaryCalls . IsReadOnly ( ref _builder ) ;
77
54
78
- bool IDictionary . IsFixedSize => false ;
55
+ bool IDictionary . IsFixedSize => IDictionaryCalls . IsFixedSize ( ref _builder ) ;
79
56
80
57
object ICollection . SyncRoot => this ;
81
58
82
- bool ICollection . IsSynchronized => false ;
59
+ bool ICollection . IsSynchronized => ICollectionCalls . IsSynchronized ( ref _builder ) ;
83
60
84
61
public TValue this [ TKey key ]
85
62
{
86
- get => ReadOnlyDictionary [ key ] ;
87
- set => GetOrCreateMutableDictionary ( ) [ key ] = value ;
63
+ get => _builder [ key ] ;
64
+ set => _builder [ key ] = value ;
88
65
}
89
66
90
67
object ? IDictionary . this [ object key ]
91
68
{
92
- get => ( ( IDictionary ) ReadOnlyDictionary ) [ key ] ;
93
- set => ( ( IDictionary ) GetOrCreateMutableDictionary ( ) ) [ key ] = value ;
94
- }
95
-
96
- private SegmentedDictionary < TKey , TValue > GetOrCreateMutableDictionary ( )
97
- {
98
- return _mutableDictionary ??= new SegmentedDictionary < TKey , TValue > ( _dictionary . _dictionary , _dictionary . KeyComparer ) ;
69
+ get => IDictionaryCalls . GetItem ( ref _builder , key ) ;
70
+ set => IDictionaryCalls . SetItem ( ref _builder , key , value ) ;
99
71
}
100
72
101
73
public void Add ( TKey key , TValue value )
102
- {
103
- if ( Contains ( new KeyValuePair < TKey , TValue > ( key , value ) ) )
104
- return ;
105
-
106
- GetOrCreateMutableDictionary ( ) . Add ( key , value ) ;
107
- }
74
+ => _builder . Add ( key , value ) ;
108
75
109
76
public void Add ( KeyValuePair < TKey , TValue > item )
110
- => Add ( item . Key , item . Value ) ;
77
+ => _builder . Add ( item ) ;
111
78
112
79
public void AddRange ( IEnumerable < KeyValuePair < TKey , TValue > > items )
113
- {
114
- if ( items == null )
115
- throw new ArgumentNullException ( nameof ( items ) ) ;
116
-
117
- foreach ( var pair in items )
118
- Add ( pair . Key , pair . Value ) ;
119
- }
80
+ => _builder . AddRange ( items ) ;
120
81
121
82
public void Clear ( )
122
- {
123
- if ( ReadOnlyDictionary . Count != 0 )
124
- {
125
- if ( _mutableDictionary is null )
126
- _mutableDictionary = new SegmentedDictionary < TKey , TValue > ( KeyComparer ) ;
127
- else
128
- _mutableDictionary . Clear ( ) ;
129
- }
130
- }
83
+ => _builder . Clear ( ) ;
131
84
132
85
public bool Contains ( KeyValuePair < TKey , TValue > item )
133
- {
134
- return TryGetValue ( item . Key , out var value )
135
- && EqualityComparer < TValue > . Default . Equals ( value , item . Value ) ;
136
- }
86
+ => _builder . Contains ( item ) ;
137
87
138
88
public bool ContainsKey ( TKey key )
139
- => ReadOnlyDictionary . ContainsKey ( key ) ;
89
+ => _builder . ContainsKey ( key ) ;
140
90
141
91
public bool ContainsValue ( TValue value )
142
- {
143
- return _dictionary . ContainsValue ( value ) ;
144
- }
92
+ => _builder . ContainsValue ( value ) ;
145
93
146
94
public Enumerator GetEnumerator ( )
147
- => new ( GetOrCreateMutableDictionary ( ) , Enumerator . ReturnType . KeyValuePair ) ;
95
+ => _builder . GetEnumerator ( ) ;
148
96
149
97
public TValue ? GetValueOrDefault ( TKey key )
150
- {
151
- if ( TryGetValue ( key , out var value ) )
152
- return value ;
153
-
154
- return default ;
155
- }
98
+ => _builder . GetValueOrDefault ( key ) ;
156
99
157
100
public TValue GetValueOrDefault ( TKey key , TValue defaultValue )
158
- {
159
- if ( TryGetValue ( key , out var value ) )
160
- return value ;
161
-
162
- return defaultValue ;
163
- }
101
+ => _builder . GetValueOrDefault ( key , defaultValue ) ;
164
102
165
103
public bool Remove ( TKey key )
166
- {
167
- if ( _mutableDictionary is null && ! ContainsKey ( key ) )
168
- return false ;
169
-
170
- return GetOrCreateMutableDictionary ( ) . Remove ( key ) ;
171
- }
104
+ => _builder . Remove ( key ) ;
172
105
173
106
public bool Remove ( KeyValuePair < TKey , TValue > item )
174
- {
175
- if ( ! Contains ( item ) )
176
- {
177
- return false ;
178
- }
179
-
180
- GetOrCreateMutableDictionary ( ) . Remove ( item . Key ) ;
181
- return true ;
182
- }
107
+ => _builder . Remove ( item ) ;
183
108
184
109
public void RemoveRange ( IEnumerable < TKey > keys )
185
- {
186
- if ( keys is null )
187
- throw new ArgumentNullException ( nameof ( keys ) ) ;
188
-
189
- foreach ( var key in keys )
190
- {
191
- Remove ( key ) ;
192
- }
193
- }
110
+ => _builder . RemoveRange ( keys ) ;
194
111
195
112
public bool TryGetKey ( TKey equalKey , out TKey actualKey )
196
- {
197
- foreach ( var key in Keys )
198
- {
199
- if ( KeyComparer . Equals ( key , equalKey ) )
200
- {
201
- actualKey = key ;
202
- return true ;
203
- }
204
- }
205
-
206
- actualKey = equalKey ;
207
- return false ;
208
- }
113
+ => _builder . TryGetKey ( equalKey , out actualKey ) ;
209
114
210
115
#pragma warning disable CS8767 // Nullability of reference types in type of parameter doesn't match implicitly implemented member (possibly because of nullability attributes).
211
116
public bool TryGetValue ( TKey key , [ MaybeNullWhen ( false ) ] out TValue value )
212
117
#pragma warning restore CS8767 // Nullability of reference types in type of parameter doesn't match implicitly implemented member (possibly because of nullability attributes).
213
- => ReadOnlyDictionary . TryGetValue ( key , out value ) ;
118
+ => _builder . TryGetValue ( key , out value ) ;
214
119
215
120
public ImmutableSegmentedDictionary < TKey , TValue > ToImmutable ( )
216
- {
217
- _dictionary = new ImmutableSegmentedDictionary < TKey , TValue > ( ReadOnlyDictionary ) ;
218
- _mutableDictionary = null ;
219
- return _dictionary ;
220
- }
121
+ => _builder . ToImmutable ( ) ;
221
122
222
123
void ICollection < KeyValuePair < TKey , TValue > > . CopyTo ( KeyValuePair < TKey , TValue > [ ] array , int arrayIndex )
223
- => ( ( ICollection < KeyValuePair < TKey , TValue > > ) ReadOnlyDictionary ) . CopyTo ( array , arrayIndex ) ;
124
+ => ICollectionCalls < KeyValuePair < TKey , TValue > > . CopyTo ( ref _builder , array , arrayIndex ) ;
224
125
225
126
IEnumerator < KeyValuePair < TKey , TValue > > IEnumerable < KeyValuePair < TKey , TValue > > . GetEnumerator ( )
226
- => new Enumerator ( GetOrCreateMutableDictionary ( ) , Enumerator . ReturnType . KeyValuePair ) ;
127
+ => IEnumerableCalls < KeyValuePair < TKey , TValue > > . GetEnumerator ( ref _builder ) ;
227
128
228
129
IEnumerator IEnumerable . GetEnumerator ( )
229
- => new Enumerator ( GetOrCreateMutableDictionary ( ) , Enumerator . ReturnType . KeyValuePair ) ;
130
+ => IEnumerableCalls . GetEnumerator ( ref _builder ) ;
230
131
231
132
bool IDictionary . Contains ( object key )
232
- => ( ( IDictionary ) ReadOnlyDictionary ) . Contains ( key ) ;
133
+ => IDictionaryCalls . Contains ( ref _builder , key ) ;
233
134
234
135
void IDictionary . Add ( object key , object ? value )
235
- => ( ( IDictionary ) GetOrCreateMutableDictionary ( ) ) . Add ( key , value ) ;
136
+ => IDictionaryCalls . Add ( ref _builder , key , value ) ;
236
137
237
138
IDictionaryEnumerator IDictionary . GetEnumerator ( )
238
- => new Enumerator ( GetOrCreateMutableDictionary ( ) , Enumerator . ReturnType . DictionaryEntry ) ;
139
+ => IDictionaryCalls . GetEnumerator ( ref _builder ) ;
239
140
240
141
void IDictionary . Remove ( object key )
241
- => ( ( IDictionary ) GetOrCreateMutableDictionary ( ) ) . Remove ( key ) ;
142
+ => IDictionaryCalls . Remove ( ref _builder , key ) ;
242
143
243
144
void ICollection . CopyTo ( Array array , int index )
244
- => ( ( ICollection ) ReadOnlyDictionary ) . CopyTo ( array , index ) ;
145
+ => ICollectionCalls . CopyTo ( ref _builder , array , index ) ;
245
146
246
147
internal TestAccessor GetTestAccessor ( )
247
148
=> new TestAccessor ( this ) ;
248
149
249
150
internal readonly struct TestAccessor ( Builder instance )
250
151
{
251
152
internal SegmentedDictionary < TKey , TValue > GetOrCreateMutableDictionary ( )
252
- => instance . GetOrCreateMutableDictionary ( ) ;
153
+ => instance . _builder . GetOrCreateMutableDictionary ( ) ;
253
154
}
254
155
}
255
156
}
0 commit comments