-
Notifications
You must be signed in to change notification settings - Fork 311
Expand file tree
/
Copy pathEmptyDictionaryStorage.cs
More file actions
82 lines (65 loc) · 2.48 KB
/
EmptyDictionaryStorage.cs
File metadata and controls
82 lines (65 loc) · 2.48 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
#nullable enable
using System;
using System.Collections.Generic;
using IronPython.Runtime.Operations;
namespace IronPython.Runtime {
/// <summary>
/// Singleton used for dictionaries which contain no items.
/// </summary>
[Serializable]
internal class EmptyDictionaryStorage : DictionaryStorage {
public static EmptyDictionaryStorage Instance = new EmptyDictionaryStorage();
private EmptyDictionaryStorage() { }
public override void Add(ref DictionaryStorage storage, object? key, object? value) {
lock (this) {
if (storage == this) {
CommonDictionaryStorage newStorage = new CommonDictionaryStorage();
newStorage.AddNoLock(key, value);
storage = newStorage;
return;
}
}
// race, try again...
storage.Add(ref storage, key, value);
}
public override bool Remove(ref DictionaryStorage storage, object? key) {
return false;
}
public override DictionaryStorage AsMutable(ref DictionaryStorage storage) {
lock (this) {
if (storage == this) {
return storage = new CommonDictionaryStorage();
}
}
// race, try again...
return storage.AsMutable(ref storage);
}
public override void Clear(ref DictionaryStorage storage) { }
public override bool Contains(object? key) {
// make sure argument is valid, do not calculate hash
if (PythonContext.IsHashable(key)) {
return false;
}
throw PythonOps.TypeErrorForUnhashableObject(key);
}
public override bool TryGetValue(object? key, out object? value) {
value = null;
return false;
}
public override int Count {
get { return 0; }
}
public override List<KeyValuePair<object?, object?>> GetItems() {
return new List<KeyValuePair<object?, object?>>();
}
public override DictionaryStorage Clone() {
return this;
}
public override bool HasNonStringAttributes() {
return false;
}
}
}