xronos /Microsoft.Scripting/Runtime/CustomSymbolDictionary.cs

Language C# Lines 633
MD5 Hash 356adee8a407f4f8d0434650a73d3d97 Estimated Cost $11,764 (why?)
Repository https://bitbucket.org/stefanrusek/xronos View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 *
 * ***************************************************************************/

#if CODEPLEX_40
using System;
#else
using System; using Microsoft;
#endif
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
#if CODEPLEX_40
using System.Linq.Expressions;
#else
using Microsoft.Linq.Expressions;
#endif
using Microsoft.Contracts;
using Microsoft.Scripting.Utils;

namespace Microsoft.Scripting.Runtime {

    /// <summary>
    /// Abstract base class used for optimized thread-safe SymbolDictionaries. 
    /// 
    /// Implementers derive from this class and override the GetExtraKeys, TrySetExtraValue, 
    /// and TryGetExtraValue methods. When looking up a value first the extra keys will be 
    /// searched using the optimized Try*ExtraValue functions.  If the value isn't found there
    /// then the value is stored in the underlying .NET dictionary.
    /// 
    /// Implementors can optionally override the object key functionality to store object keys
    /// using their own mechanism.  By default object keys are stored in their own dictionary
    /// which is stored in the primary SymbolId dictionary under an invalid symbol id.
    /// </summary>
    public abstract class CustomSymbolDictionary : BaseSymbolDictionary, IDictionary, IDictionary<object, object>, IAttributesCollection {
        private Dictionary<SymbolId, object> _data;

        protected CustomSymbolDictionary() {
        }

        /// <summary>
        /// Gets a list of the extra keys that are cached by the the optimized implementation
        /// of the module.
        /// </summary>
        public abstract SymbolId[] GetExtraKeys();

        /// <summary>
        /// Try to set the extra value and return true if the specified key was found in the 
        /// list of extra values.
        /// </summary>
        protected internal abstract bool TrySetExtraValue(SymbolId key, object value);

        /// <summary>
        /// Try to get the extra value and returns true if the specified key was found in the
        /// list of extra values.  Returns true even if the value is Uninitialized.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1007:UseGenericsWhereAppropriate")]
        protected internal abstract bool TryGetExtraValue(SymbolId key, out object value);

        private void InitializeData() {
            Debug.Assert(_data == null);

            _data = new Dictionary<SymbolId, object>();
        }

        /// <summary>
        /// Field dictionaries are usually indexed using literal strings, which is handled using the Symbols.
        /// However, Python does allow non-string keys too. We handle this case by lazily creating an object-keyed dictionary,
        /// and keeping it in the symbol-indexed dictionary. Such access is slower, which is acceptable.
        /// </summary>
        private Dictionary<object, object> GetObjectKeysDictionary() {
            Dictionary<object, object> objData = GetObjectKeysDictionaryIfExists();
            if (objData == null) {
                if (_data == null) InitializeData();
                objData = new Dictionary<object, object>();
                _data.Add(ObjectKeys, objData);
            }
            return objData;
        }

        private Dictionary<object, object> GetObjectKeysDictionaryIfExists() {
            if (_data == null) return null;

            object objData;
            if (_data.TryGetValue(ObjectKeys, out objData))
                return (Dictionary<object, object>)objData;
            return null;
        }

        #region IDictionary<object, object> Members

        void IDictionary<object, object>.Add(object key, object value) {
            Debug.Assert(!(key is SymbolId));
            string strKey = key as string;
            if (strKey != null) {
                lock (this) {
                    if (_data == null) InitializeData();
                    SymbolId keyId = SymbolTable.StringToId(strKey);
                    if (TrySetExtraValue(keyId, value))
                        return;
                    _data.Add(keyId, value);
                }
            } else {
                AddObjectKey(key, value);
            }
        }

        [Confined]
        bool IDictionary<object, object>.ContainsKey(object key) {
            Debug.Assert(!(key is SymbolId));
            lock (this) {
                object dummy;
                return AsObjectKeyedDictionary().TryGetValue(key, out dummy);
            }
        }

        ICollection<object> IDictionary<object, object>.Keys {
            get {
                List<object> res = new List<object>();
                lock (this) if (_data != null) {
                        foreach (SymbolId x in _data.Keys) {
                            if (x == ObjectKeys) continue;
                            res.Add(SymbolTable.IdToString(x));
                        }
                    }

                foreach (SymbolId key in GetExtraKeys()) {
                    if (key.Id < 0) break;

                    object dummy;
                    if (TryGetExtraValue(key, out dummy) && dummy != Uninitialized.Instance) {
                        res.Add(SymbolTable.IdToString(key));
                    }
                }

                GetObjectKeys(res);
                return res;
            }
        }

        bool IDictionary<object, object>.Remove(object key) {
            Debug.Assert(!(key is SymbolId));

            string strKey = key as string;
            if (strKey != null) {
                lock (this) {
                    SymbolId fieldId = SymbolTable.StringToId(strKey);
                    if (TrySetExtraValue(fieldId, Uninitialized.Instance)) return true;

                    if (_data == null) return false;
                    return _data.Remove(fieldId);
                }
            }

            return RemoveObjectKey(key);
        }

        bool IDictionary<object, object>.TryGetValue(object key, out object value) {
            Debug.Assert(!(key is SymbolId));

            string strKey = key as string;
            if (strKey != null) {
                lock (this) {
                    SymbolId fieldId = SymbolTable.StringToId(strKey);

                    if (TryGetExtraValue(fieldId, out value) && value != Uninitialized.Instance) return true;

                    if (_data == null) return false;
                    return _data.TryGetValue(fieldId, out value);
                }
            }

            return TryGetObjectValue(key, out value);
        }

        ICollection<object> IDictionary<object, object>.Values {
            get {
                List<object> res = new List<object>();
                lock (this) {
                    if (_data != null) {
                        foreach (SymbolId x in _data.Keys) {
                            if (x == ObjectKeys) continue;
                            res.Add(_data[x]);
                        }
                    }
                }

                foreach (SymbolId key in GetExtraKeys()) {
                    if (key.Id < 0) break;

                    object value;
                    if (TryGetExtraValue(key, out value) && value != Uninitialized.Instance) {
                        res.Add(value);
                    }
                }

                GetObjectValues(res);
                return res;
            }
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1065:DoNotRaiseExceptionsInUnexpectedLocations")]
        public object this[object key] {
            get {
                Debug.Assert(!(key is SymbolId));

                string strKey = key as string;
                object res;
                if (strKey != null) {
                    lock (this) {
                        SymbolId id = SymbolTable.StringToId(strKey);

                        if (TryGetExtraValue(id, out res) && !(res is Uninitialized)) return res;

                        if (_data == null) {
                            throw new KeyNotFoundException(key.ToString());
                        }

                        return _data[id];
                    }
                }

                if (TryGetObjectValue(key, out res))
                    return res;

                throw new KeyNotFoundException(key.ToString());
            }
            set {
                Debug.Assert(!(key is SymbolId));

                string strKey = key as string;
                if (strKey != null) {
                    lock (this) {
                        SymbolId id = SymbolTable.StringToId(strKey);
                        if (TrySetExtraValue(id, value)) return;

                        if (_data == null) InitializeData();
                        _data[id] = value;
                    }
                } else {
                    AddObjectKey(key, value);
                }
            }
        }

        #endregion

        #region ICollection<KeyValuePair<string,object>> Members

        public void Add(KeyValuePair<object, object> item) {
            throw new NotImplementedException();
        }

        public void Clear() {
            lock (this) {
                foreach (SymbolId key in GetExtraKeys()) {
                    if (key.Id < 0) break;

                    TrySetExtraValue(key, Uninitialized.Instance);
                }
                _data = null;
            }
        }

        [Confined]
        public bool Contains(KeyValuePair<object, object> item) {
            throw new NotImplementedException();
        }

        public void CopyTo(KeyValuePair<object, object>[] array, int arrayIndex) {
            ContractUtils.RequiresNotNull(array, "array");
            ContractUtils.RequiresArrayRange(array, arrayIndex, Count, "araryIndex", "Count");

            foreach (KeyValuePair<object, object> kvp in ((IEnumerable<KeyValuePair<object, object>>)this)) {
                array[arrayIndex++] = kvp;
            }
        }

        public int Count {
            get {
                int count = GetObjectKeyCount();

                lock (this) {
                    if (_data != null) {
                        foreach (KeyValuePair<SymbolId, object> o in _data) {
                            if (o.Key == SymbolId.Invalid) break;
                            if (o.Key != ObjectKeys) count++;
                        }
                    }

                    foreach (SymbolId key in GetExtraKeys()) {
                        if (key.Id < 0) break;

                        object dummy;
                        if (TryGetExtraValue(key, out dummy) && dummy != Uninitialized.Instance) count++;
                    }
                }

                return count;
            }
        }

        public bool IsReadOnly {
            get { return false; }
        }

        public bool Remove(KeyValuePair<object, object> item) {
            throw new NotImplementedException();
        }

        #endregion

        #region IEnumerable<KeyValuePair<object,object>> Members

        [Pure]
        IEnumerator<KeyValuePair<object, object>> IEnumerable<KeyValuePair<object, object>>.GetEnumerator() {
            if (_data != null) {
                foreach (KeyValuePair<SymbolId, object> o in _data) {
                    if (o.Key == SymbolId.Invalid) break;
                    if (o.Key == ObjectKeys) continue;
                    yield return new KeyValuePair<object, object>(SymbolTable.IdToString(o.Key), o.Value);
                }
            }

            foreach (SymbolId o in GetExtraKeys()) {
                if (o.Id < 0) break;

                object val;
                if (TryGetExtraValue(o, out val) && val != Uninitialized.Instance) {
                    yield return new KeyValuePair<object, object>(SymbolTable.IdToString(o), val);
                }
            }

            IDictionaryEnumerator objItems = GetObjectItems();
            if (objItems != null) {
                while (objItems.MoveNext()) {
                    yield return new KeyValuePair<object, object>(objItems.Key, objItems.Value);
                }
            }
        }

        #endregion

        #region IEnumerable Members

        [Pure]
        public System.Collections.IEnumerator GetEnumerator() {
            List<object> l = new List<object>(this.Keys);
            for (int i = 0; i < l.Count; i++) {
                object baseVal = l[i];
                object nullVal = l[i] = BaseSymbolDictionary.ObjToNull(l[i]);
                if (baseVal != nullVal) {
                    // we've transformed null, stop looking
                    break;
                }
            }
            return l.GetEnumerator();
        }

        #endregion

        #region IAttributesDictionary Members

        public void Add(SymbolId name, object value) {
            lock (this) {
                if (TrySetExtraValue(name, value)) return;

                if (_data == null) InitializeData();
                _data.Add(name, value);
            }
        }

        public bool ContainsKey(SymbolId name) {
            object value;
            if (TryGetExtraValue(name, out value) && value != Uninitialized.Instance) return true;
            if (_data == null) return false;

            lock (this) return _data.ContainsKey(name);
        }

        public bool Remove(SymbolId name) {
            object value;
            if (TryGetExtraValue(name, out value)) {
                if (value == Uninitialized.Instance) return false;
                if (TrySetExtraValue(name, Uninitialized.Instance)) return true;
            }

            if (_data == null) return false;

            lock (this) return _data.Remove(name);
        }

        public bool TryGetValue(SymbolId name, out object value) {
            if (TryGetExtraValue(name, out value) && value != Uninitialized.Instance) return true;

            if (_data == null) return false;

            lock (this) return _data.TryGetValue(name, out value);
        }

        object IAttributesCollection.this[SymbolId name] {
            get {
                object res;
                if (TryGetExtraValue(name, out res) && res != Uninitialized.Instance) return res;

                lock (this) {
                    if (_data == null) throw new KeyNotFoundException(SymbolTable.IdToString(name));
                    return _data[name];
                }
            }
            set {
                if (TrySetExtraValue(name, value)) return;

                lock (this) {
                    if (_data == null) InitializeData();
                    _data[name] = value;
                }
            }
        }

        public IDictionary<SymbolId, object> SymbolAttributes {
            get {
                Dictionary<SymbolId, object> d = new Dictionary<SymbolId, object>();
                lock (this) {
                    if (_data != null) {
                        foreach (KeyValuePair<SymbolId, object> name in _data) {
                            if (name.Key == ObjectKeys) continue;
                            d.Add(name.Key, name.Value);
                        }
                    }
                    foreach (SymbolId extraKey in GetExtraKeys()) {
                        object value;
                        if (TryGetExtraValue(extraKey, out value) && !(value is Uninitialized))
                            d.Add(extraKey, value);
                    }
                }
                return d;
            }
        }

        /// <summary>
        /// Appends the object keys to the provided list.
        /// </summary>
        protected virtual void GetObjectKeys(List<object> res) {
            lock (this) {
                Dictionary<object, object> objData = GetObjectKeysDictionaryIfExists();
                if (objData != null) res.AddRange(objData.Keys);
            }
        }

        /// <summary>
        /// Appends the values stored under object keys to the provided list.
        /// </summary>
        protected virtual void GetObjectValues(List<object> res) {
            lock (this) {
                Dictionary<object, object> objData = GetObjectKeysDictionaryIfExists();
                if (objData != null) res.AddRange(objData.Values);
            }
        }

        /// <summary>
        /// Gets the count of object keys.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] // TODO: fix
        protected virtual int GetObjectKeyCount() {
            lock (this) {
                Dictionary<object, object> objData = GetObjectKeysDictionaryIfExists();
                if (objData != null) {
                    return objData.Count;
                }
            }
            return 0;
        }

        /// <summary>
        /// Gets an IDictionaryEnumerator for all of the object key/value pairs.
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] // TODO: fix
        public virtual IDictionaryEnumerator GetObjectItems() {
            lock (this) {
                Dictionary<object, object> objData = GetObjectKeysDictionaryIfExists();
                if (objData != null) {
                    return objData.GetEnumerator();
                }
            }
            return null;
        }

        /// <summary>
        /// Stores the specified value under the specified object key.
        /// </summary>
        public virtual void AddObjectKey(object name, object value) {
            lock (this) {
                Dictionary<object, object> objData = GetObjectKeysDictionary();
                objData[name] = value;
            }
        }

        public bool ContainsObjectKey(object name) {
            return AsObjectKeyedDictionary().ContainsKey(name);
        }

        /// <summary>
        /// Removes the specified object key from the dictionary.
        /// </summary>
        public virtual bool RemoveObjectKey(object name) {
            lock (this) {
                Dictionary<object, object> objData = GetObjectKeysDictionaryIfExists();
                if (objData == null) return false;
                return objData.Remove(name);
            }
        }

        /// <summary>
        /// Attemps to get the value stored under the specified object key.
        /// 
        /// Returns true if the key was found, false if not found.
        /// </summary>
        public virtual bool TryGetObjectValue(object name, out object value) {
            string strKey = name as string;
            if (strKey != null) {
                return ((IAttributesCollection)this).TryGetValue(SymbolTable.StringToId(strKey), out value);
            }

            lock (this) {
                Dictionary<object, object> objData = GetObjectKeysDictionaryIfExists();
                if (objData != null)
                    return objData.TryGetValue(name, out value);
            }
            value = null;
            return false;
        }

        public ICollection<object> Keys { get { return AsObjectKeyedDictionary().Keys; } }

        public IDictionary<object, object> AsObjectKeyedDictionary() {
            return this;
        }

        #endregion

        #region IDictionary Members

        [Pure]
        void IDictionary.Add(object key, object value) {
            AsObjectKeyedDictionary().Add(key, value);
        }

        [Pure]
        public bool Contains(object key) {
            return AsObjectKeyedDictionary().ContainsKey(key);
        }

        [Pure]
        IDictionaryEnumerator IDictionary.GetEnumerator() {
            List<IDictionaryEnumerator> enums = new List<IDictionaryEnumerator>();

            enums.Add(new ExtraKeyEnumerator(this));

            if (_data != null) enums.Add(new TransformDictionaryEnumerator(_data));

            IDictionaryEnumerator objItems = GetObjectItems();
            if (objItems != null) {
                enums.Add(objItems);
            }

            return new DictionaryUnionEnumerator(enums);
        }

        public bool IsFixedSize {
            get { return false; }
        }

        ICollection IDictionary.Keys {
            get { return new List<object>(AsObjectKeyedDictionary().Keys); }
        }

        void IDictionary.Remove(object key) {
            Debug.Assert(!(key is SymbolId));
            string strKey = key as string;
            if (strKey != null) {
                SymbolId id = SymbolTable.StringToId(strKey);
                if (TrySetExtraValue(id, Uninitialized.Instance)) return;

                lock (this) if (_data != null) _data.Remove(id);
            } else {
                RemoveObjectKey(key);
            }
        }

        ICollection IDictionary.Values {
            get {
                return new List<object>(AsObjectKeyedDictionary().Values);
            }
        }

        object IDictionary.this[object key] {
            get { return AsObjectKeyedDictionary()[key]; }
            set { AsObjectKeyedDictionary()[key] = value; }
        }

        #endregion

        public void CopyTo(Array array, int index) {
            throw Error.MethodOrOperatorNotImplemented();
        }

        public bool IsSynchronized {
            get {
                return true;
            }
        }

        public object SyncRoot {
            get {
                // TODO: Sync root shouldn't be this, it should be data.
                return this;
            }
        }
    }
}
Back to Top