openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Generated/Internal/ChangeTrackingDictionary.cs
164lines · modecode
| 1 | // <auto-generated/> |
| 2 | |
| 3 | #nullable disable |
| 4 | |
| 5 | using System; |
| 6 | using System.Collections; |
| 7 | using System.Collections.Generic; |
| 8 | |
| 9 | namespace OpenAI |
| 10 | { |
| 11 | internal class ChangeTrackingDictionary<TKey, TValue> : IDictionary<TKey, TValue>, IReadOnlyDictionary<TKey, TValue> where TKey : notnull |
| 12 | { |
| 13 | private IDictionary<TKey, TValue> _innerDictionary; |
| 14 | |
| 15 | public ChangeTrackingDictionary() |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | public ChangeTrackingDictionary(IDictionary<TKey, TValue> dictionary) |
| 20 | { |
| 21 | if (dictionary == null) |
| 22 | { |
| 23 | return; |
| 24 | } |
| 25 | _innerDictionary = new Dictionary<TKey, TValue>(dictionary); |
| 26 | } |
| 27 | |
| 28 | public ChangeTrackingDictionary(IReadOnlyDictionary<TKey, TValue> dictionary) |
| 29 | { |
| 30 | if (dictionary == null) |
| 31 | { |
| 32 | return; |
| 33 | } |
| 34 | _innerDictionary = new Dictionary<TKey, TValue>(); |
| 35 | foreach (var pair in dictionary) |
| 36 | { |
| 37 | _innerDictionary.Add(pair); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | public bool IsUndefined => _innerDictionary == null; |
| 42 | |
| 43 | public int Count => IsUndefined ? 0 : EnsureDictionary().Count; |
| 44 | |
| 45 | public bool IsReadOnly => IsUndefined ? false : EnsureDictionary().IsReadOnly; |
| 46 | |
| 47 | public ICollection<TKey> Keys => IsUndefined ? Array.Empty<TKey>() : EnsureDictionary().Keys; |
| 48 | |
| 49 | public ICollection<TValue> Values => IsUndefined ? Array.Empty<TValue>() : EnsureDictionary().Values; |
| 50 | |
| 51 | public TValue this[TKey key] |
| 52 | { |
| 53 | get |
| 54 | { |
| 55 | if (IsUndefined) |
| 56 | { |
| 57 | throw new KeyNotFoundException(nameof(key)); |
| 58 | } |
| 59 | return EnsureDictionary()[key]; |
| 60 | } |
| 61 | set |
| 62 | { |
| 63 | EnsureDictionary()[key] = value; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | IEnumerable<TKey> IReadOnlyDictionary<TKey, TValue>.Keys => Keys; |
| 68 | |
| 69 | IEnumerable<TValue> IReadOnlyDictionary<TKey, TValue>.Values => Values; |
| 70 | |
| 71 | public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() |
| 72 | { |
| 73 | if (IsUndefined) |
| 74 | { |
| 75 | IEnumerator<KeyValuePair<TKey, TValue>> enumerateEmpty() |
| 76 | { |
| 77 | yield break; |
| 78 | } |
| 79 | return enumerateEmpty(); |
| 80 | } |
| 81 | return EnsureDictionary().GetEnumerator(); |
| 82 | } |
| 83 | |
| 84 | IEnumerator IEnumerable.GetEnumerator() |
| 85 | { |
| 86 | return GetEnumerator(); |
| 87 | } |
| 88 | |
| 89 | public void Add(KeyValuePair<TKey, TValue> item) |
| 90 | { |
| 91 | EnsureDictionary().Add(item); |
| 92 | } |
| 93 | |
| 94 | public void Clear() |
| 95 | { |
| 96 | EnsureDictionary().Clear(); |
| 97 | } |
| 98 | |
| 99 | public bool Contains(KeyValuePair<TKey, TValue> item) |
| 100 | { |
| 101 | if (IsUndefined) |
| 102 | { |
| 103 | return false; |
| 104 | } |
| 105 | return EnsureDictionary().Contains(item); |
| 106 | } |
| 107 | |
| 108 | public void CopyTo(KeyValuePair<TKey, TValue>[] array, int index) |
| 109 | { |
| 110 | if (IsUndefined) |
| 111 | { |
| 112 | return; |
| 113 | } |
| 114 | EnsureDictionary().CopyTo(array, index); |
| 115 | } |
| 116 | |
| 117 | public bool Remove(KeyValuePair<TKey, TValue> item) |
| 118 | { |
| 119 | if (IsUndefined) |
| 120 | { |
| 121 | return false; |
| 122 | } |
| 123 | return EnsureDictionary().Remove(item); |
| 124 | } |
| 125 | |
| 126 | public void Add(TKey key, TValue value) |
| 127 | { |
| 128 | EnsureDictionary().Add(key, value); |
| 129 | } |
| 130 | |
| 131 | public bool ContainsKey(TKey key) |
| 132 | { |
| 133 | if (IsUndefined) |
| 134 | { |
| 135 | return false; |
| 136 | } |
| 137 | return EnsureDictionary().ContainsKey(key); |
| 138 | } |
| 139 | |
| 140 | public bool Remove(TKey key) |
| 141 | { |
| 142 | if (IsUndefined) |
| 143 | { |
| 144 | return false; |
| 145 | } |
| 146 | return EnsureDictionary().Remove(key); |
| 147 | } |
| 148 | |
| 149 | public bool TryGetValue(TKey key, out TValue value) |
| 150 | { |
| 151 | if (IsUndefined) |
| 152 | { |
| 153 | value = default; |
| 154 | return false; |
| 155 | } |
| 156 | return EnsureDictionary().TryGetValue(key, out value); |
| 157 | } |
| 158 | |
| 159 | public IDictionary<TKey, TValue> EnsureDictionary() |
| 160 | { |
| 161 | return _innerDictionary ??= new Dictionary<TKey, TValue>(); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |