| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | |
| | | 8 | | namespace System.Net.Http.Headers |
| | | 9 | | { |
| | | 10 | | internal sealed class UnvalidatedObjectCollection<T> : ObjectCollection<T> where T : class |
| | | 11 | | { |
| | | 12 | | public override void Validate(T item) |
| | | 13 | | { |
| | | 14 | | ArgumentNullException.ThrowIfNull(item); |
| | | 15 | | } |
| | | 16 | | } |
| | | 17 | | |
| | | 18 | | /// <summary>An <see cref="ICollection{T}"/> list that prohibits null elements and that is optimized for a small num |
| | | 19 | | [DebuggerDisplay("Count = {Count}")] |
| | | 20 | | [DebuggerTypeProxy(typeof(ObjectCollection<>.DebugView))] |
| | | 21 | | internal abstract class ObjectCollection<T> : ICollection<T> where T : class |
| | | 22 | | { |
| | | 23 | | private const int DefaultSize = 4; |
| | | 24 | | |
| | | 25 | | /// <summary>null, a T, or a T[].</summary> |
| | | 26 | | internal object? _items; |
| | | 27 | | /// <summary>Number of elements stored in the collection.</summary> |
| | | 28 | | internal int _size; |
| | | 29 | | |
| | 380565 | 30 | | public ObjectCollection() { } |
| | | 31 | | |
| | 199717 | 32 | | public int Count => _size; |
| | | 33 | | |
| | 0 | 34 | | public bool IsReadOnly => false; |
| | | 35 | | |
| | | 36 | | public abstract void Validate(T item); |
| | | 37 | | |
| | | 38 | | public void Add(T item) |
| | 516778 | 39 | | { |
| | 516778 | 40 | | Validate(item); |
| | 516778 | 41 | | Debug.Assert(item != null); |
| | | 42 | | |
| | 516778 | 43 | | if (_items is null) |
| | 126575 | 44 | | { |
| | | 45 | | // The collection is empty. Just store the new item directly. |
| | 126575 | 46 | | _items = item; |
| | 126575 | 47 | | _size = 1; |
| | 126575 | 48 | | } |
| | 390203 | 49 | | else if (_items is T existingItem) |
| | 63647 | 50 | | { |
| | | 51 | | // The collection has a single item stored directly. Upgrade to |
| | | 52 | | // an array, and store both the existing and new items. |
| | 63647 | 53 | | Debug.Assert(_size == 1); |
| | 63647 | 54 | | T[] items = new T[DefaultSize]; |
| | 63647 | 55 | | items[0] = existingItem; |
| | 63647 | 56 | | items[1] = item; |
| | 63647 | 57 | | _items = items; |
| | 63647 | 58 | | _size = 2; |
| | 63647 | 59 | | } |
| | | 60 | | else |
| | 326556 | 61 | | { |
| | 326556 | 62 | | T[] array = (T[])_items; |
| | 326556 | 63 | | int size = _size; |
| | 326556 | 64 | | if ((uint)size < (uint)array.Length) |
| | 310803 | 65 | | { |
| | | 66 | | // There's room in the existing array. Add the item. |
| | 310803 | 67 | | array[size] = item; |
| | 310803 | 68 | | } |
| | | 69 | | else |
| | 15753 | 70 | | { |
| | | 71 | | // We need to grow the array. Do so, and store the new item. |
| | 15753 | 72 | | Debug.Assert(_size > 0); |
| | 15753 | 73 | | Debug.Assert(_size == array.Length); |
| | | 74 | | |
| | 15753 | 75 | | var newItems = new T[array.Length * 2]; |
| | 15753 | 76 | | Array.Copy(array, newItems, size); |
| | 15753 | 77 | | _items = newItems; |
| | 15753 | 78 | | newItems[size] = item; |
| | 15753 | 79 | | } |
| | 326556 | 80 | | _size = size + 1; |
| | 326556 | 81 | | } |
| | 516778 | 82 | | } |
| | | 83 | | |
| | | 84 | | public void Clear() |
| | 0 | 85 | | { |
| | 0 | 86 | | _items = null; |
| | 0 | 87 | | _size = 0; |
| | 0 | 88 | | } |
| | | 89 | | |
| | | 90 | | public bool Contains(T item) => |
| | 0 | 91 | | _size <= 0 ? false : |
| | 0 | 92 | | _items is T o ? o.Equals(item) : |
| | 0 | 93 | | _items is T[] items && Array.IndexOf(items, item, 0, _size) >= 0; |
| | | 94 | | |
| | | 95 | | public void CopyTo(T[] array, int arrayIndex) |
| | 0 | 96 | | { |
| | 0 | 97 | | if (_items is T[] items) |
| | 0 | 98 | | { |
| | 0 | 99 | | Array.Copy(items, 0, array, arrayIndex, _size); |
| | 0 | 100 | | } |
| | | 101 | | else |
| | 0 | 102 | | { |
| | 0 | 103 | | Debug.Assert(_size == 0 || _size == 1); |
| | 0 | 104 | | if (array is null || _size > array.Length - arrayIndex) |
| | 0 | 105 | | { |
| | | 106 | | // Use Array.CopyTo to throw the right exceptions. |
| | 0 | 107 | | new T[] { (T)_items! }.CopyTo(array!, arrayIndex); |
| | 0 | 108 | | } |
| | 0 | 109 | | else if (_size == 1) |
| | 0 | 110 | | { |
| | 0 | 111 | | array[arrayIndex] = (T)_items!; |
| | 0 | 112 | | } |
| | 0 | 113 | | } |
| | 0 | 114 | | } |
| | | 115 | | |
| | | 116 | | public bool Remove(T item) |
| | 0 | 117 | | { |
| | 0 | 118 | | if (_items is T o) |
| | 0 | 119 | | { |
| | 0 | 120 | | if (o.Equals(item)) |
| | 0 | 121 | | { |
| | 0 | 122 | | _items = null; |
| | 0 | 123 | | _size = 0; |
| | 0 | 124 | | return true; |
| | | 125 | | } |
| | 0 | 126 | | } |
| | 0 | 127 | | else if (_items is T[] items) |
| | 0 | 128 | | { |
| | 0 | 129 | | int index = Array.IndexOf(items, item, 0, _size); |
| | 0 | 130 | | if (index >= 0) |
| | 0 | 131 | | { |
| | 0 | 132 | | _size--; |
| | 0 | 133 | | if (index < _size) |
| | 0 | 134 | | { |
| | 0 | 135 | | Array.Copy(items, index + 1, items, index, _size - index); |
| | 0 | 136 | | } |
| | 0 | 137 | | items[_size] = null!; |
| | | 138 | | |
| | 0 | 139 | | return true; |
| | | 140 | | } |
| | 0 | 141 | | } |
| | | 142 | | |
| | 0 | 143 | | return false; |
| | 0 | 144 | | } |
| | | 145 | | |
| | 199222 | 146 | | public Enumerator GetEnumerator() => new Enumerator(this); |
| | 0 | 147 | | IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator(); |
| | 0 | 148 | | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| | | 149 | | |
| | | 150 | | public struct Enumerator : IEnumerator<T> |
| | | 151 | | { |
| | | 152 | | private readonly ObjectCollection<T> _list; |
| | | 153 | | private int _index; |
| | | 154 | | private T _current; |
| | | 155 | | |
| | | 156 | | internal Enumerator(ObjectCollection<T> list) |
| | 199222 | 157 | | { |
| | 199222 | 158 | | _list = list; |
| | 199222 | 159 | | _index = 0; |
| | 199222 | 160 | | _current = default!; |
| | 199222 | 161 | | } |
| | | 162 | | |
| | 398444 | 163 | | public void Dispose() { } |
| | | 164 | | |
| | | 165 | | public bool MoveNext() |
| | 1019706 | 166 | | { |
| | 1019706 | 167 | | ObjectCollection<T> list = _list; |
| | | 168 | | |
| | 1019706 | 169 | | if ((uint)_index < (uint)list._size) |
| | 820484 | 170 | | { |
| | 820484 | 171 | | _current = list._items is T[] items ? items[_index] : (T)list._items!; |
| | 820484 | 172 | | _index++; |
| | 820484 | 173 | | return true; |
| | | 174 | | } |
| | | 175 | | |
| | 199222 | 176 | | _index = _list._size + 1; |
| | 199222 | 177 | | _current = default!; |
| | 199222 | 178 | | return false; |
| | 1019706 | 179 | | } |
| | | 180 | | |
| | 820484 | 181 | | public T Current => _current!; |
| | | 182 | | |
| | 0 | 183 | | object? IEnumerator.Current => _current; |
| | | 184 | | |
| | | 185 | | void IEnumerator.Reset() |
| | 0 | 186 | | { |
| | 0 | 187 | | _index = 0; |
| | 0 | 188 | | _current = default!; |
| | 0 | 189 | | } |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | internal sealed class DebugView |
| | | 193 | | { |
| | | 194 | | private readonly ObjectCollection<T> _collection; |
| | | 195 | | |
| | 0 | 196 | | public DebugView(ObjectCollection<T> collection) |
| | 0 | 197 | | { |
| | 0 | 198 | | ArgumentNullException.ThrowIfNull(collection); |
| | 0 | 199 | | _collection = collection; |
| | 0 | 200 | | } |
| | | 201 | | |
| | | 202 | | [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] |
| | | 203 | | public T[] Items |
| | | 204 | | { |
| | | 205 | | get |
| | 0 | 206 | | { |
| | 0 | 207 | | T[] items = new T[_collection.Count]; |
| | 0 | 208 | | _collection.CopyTo(items, 0); |
| | 0 | 209 | | return items; |
| | 0 | 210 | | } |
| | | 211 | | } |
| | | 212 | | } |
| | | 213 | | } |
| | | 214 | | } |