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