@@ -12,9 +12,13 @@ internal class SafeLazyDictionary<TValue> : IDictionary<string, TValue>
12
12
#if ! DNXCORE50
13
13
private static readonly string [ ] EmptyKeys = new string [ 0 ] ;
14
14
private static readonly TValue [ ] EmptyValues = new TValue [ 0 ] ;
15
- private static readonly KeyValuePair < string , TValue > [ ] EmptyEnumerator = new KeyValuePair < string , TValue > [ 0 ] ;
16
15
#endif
17
- public IDictionary < string , TValue > Store { get ; set ; }
16
+ private static readonly Enumerator EmptyEnumerator = new Enumerator ( ) ;
17
+ // Pre-box
18
+ private static readonly IEnumerator < KeyValuePair < string , TValue > > EmptyIEnumeratorType = EmptyEnumerator ;
19
+ private static readonly IEnumerator EmptyIEnumerator = EmptyEnumerator ;
20
+
21
+ private Dictionary < string , TValue > Store { get ; set ; }
18
22
19
23
public SafeLazyDictionary ( )
20
24
{
@@ -133,11 +137,14 @@ public void Clear()
133
137
134
138
public bool Contains ( KeyValuePair < string , TValue > item )
135
139
{
136
- if ( Store == null )
140
+ TValue value ;
141
+ if ( Store == null ||
142
+ ! Store . TryGetValue ( item . Key , out value ) ||
143
+ ! value . Equals ( item . Value ) )
137
144
{
138
145
return false ;
139
146
}
140
- return Store . Contains ( item ) ;
147
+ return true ;
141
148
}
142
149
143
150
public bool ContainsKey ( string key )
@@ -162,20 +169,7 @@ public void CopyTo(KeyValuePair<string, TValue>[] array, int arrayIndex)
162
169
arrayIndex ++ ;
163
170
}
164
171
}
165
-
166
- public IEnumerator < KeyValuePair < string , TValue > > GetEnumerator ( )
167
- {
168
- if ( Store == null )
169
- {
170
- #if DNXCORE50
171
- return ( ( IEnumerable < KeyValuePair < string , TValue > > ) Array . Empty < KeyValuePair < string , TValue > > ( ) ) . GetEnumerator ( ) ;
172
- #else
173
- return ( ( IEnumerable < KeyValuePair < string , TValue > > ) EmptyEnumerator ) . GetEnumerator ( ) ;
174
- #endif
175
- }
176
- return Store . GetEnumerator ( ) ;
177
- }
178
-
172
+
179
173
public bool Remove ( KeyValuePair < string , TValue > item )
180
174
{
181
175
if ( Store == null )
@@ -211,17 +205,83 @@ public bool TryGetValue(string key, out TValue value)
211
205
return Store . TryGetValue ( key , out value ) ;
212
206
}
213
207
208
+ public Enumerator GetEnumerator ( )
209
+ {
210
+ if ( Store == null )
211
+ {
212
+ return EmptyEnumerator ;
213
+ }
214
+ return new Enumerator ( Store . GetEnumerator ( ) ) ;
215
+ }
216
+
217
+ IEnumerator < KeyValuePair < string , TValue > > IEnumerable < KeyValuePair < string , TValue > > . GetEnumerator ( )
218
+ {
219
+ if ( Store == null )
220
+ {
221
+ return EmptyIEnumeratorType ;
222
+ }
223
+ return Store . GetEnumerator ( ) ;
224
+ }
214
225
IEnumerator IEnumerable . GetEnumerator ( )
215
226
{
216
227
if ( Store == null )
217
228
{
218
- #if DNXCORE50
219
- return Array . Empty < TValue > ( ) . GetEnumerator ( ) ;
220
- #else
221
- return EmptyEnumerator . GetEnumerator ( ) ;
222
- #endif
229
+ return EmptyIEnumerator ;
223
230
}
224
231
return Store . GetEnumerator ( ) ;
225
232
}
233
+
234
+ public struct Enumerator : IEnumerator < KeyValuePair < string , TValue > >
235
+ {
236
+ private Dictionary < string , TValue > . Enumerator _dictionaryEnumerator ;
237
+ private bool _notEmpty ;
238
+
239
+ internal Enumerator ( Dictionary < string , TValue > . Enumerator dictionaryEnumerator )
240
+ {
241
+ _dictionaryEnumerator = dictionaryEnumerator ;
242
+ _notEmpty = true ;
243
+ }
244
+
245
+ public bool MoveNext ( )
246
+ {
247
+ if ( _notEmpty )
248
+ {
249
+ return _dictionaryEnumerator . MoveNext ( ) ;
250
+ }
251
+ return false ;
252
+ }
253
+
254
+ public KeyValuePair < string , TValue > Current
255
+ {
256
+ get
257
+ {
258
+ if ( _notEmpty )
259
+ {
260
+ return _dictionaryEnumerator . Current ;
261
+ }
262
+ return default ( KeyValuePair < string , TValue > ) ;
263
+ }
264
+ }
265
+
266
+ public void Dispose ( )
267
+ {
268
+ }
269
+
270
+ object IEnumerator . Current
271
+ {
272
+ get
273
+ {
274
+ return Current ;
275
+ }
276
+ }
277
+
278
+ void IEnumerator . Reset ( )
279
+ {
280
+ if ( _notEmpty )
281
+ {
282
+ ( ( IEnumerator ) _dictionaryEnumerator ) . Reset ( ) ;
283
+ }
284
+ }
285
+ }
226
286
}
227
287
}
0 commit comments