Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ImmutableExtensions.cs
Go to the documentation of this file.
2using System.Linq;
3
5
6internal static class ImmutableExtensions
7{
8 private sealed class ListOfTWrapper<T> : IOrderedCollection<T>, IEnumerable<T>, IEnumerable
9 {
10 private readonly IList<T> _collection;
11
12 public int Count => _collection.Count;
13
14 public T this[int index] => _collection[index];
15
17 {
18 Requires.NotNull(collection, "collection");
20 }
21
23 {
24 return _collection.GetEnumerator();
25 }
26
31 }
32
34 {
35 private readonly IEnumerable<T> _sequence;
36
38
39 public int Count
40 {
41 get
42 {
43 if (_collection == null)
44 {
45 if (_sequence.TryGetCount(out var count))
46 {
47 return count;
48 }
49 _collection = _sequence.ToArray();
50 }
51 return _collection.Count;
52 }
53 }
54
55 public T this[int index]
56 {
57 get
58 {
59 if (_collection == null)
60 {
61 _collection = _sequence.ToArray();
62 }
63 return _collection[index];
64 }
65 }
66
68 {
69 Requires.NotNull(sequence, "sequence");
71 }
72
74 {
75 return _sequence.GetEnumerator();
76 }
77
82 }
83
84 internal static bool IsValueType<T>()
85 {
86 return typeof(T).IsValueType;
87 }
88
90 {
91 Requires.NotNull(sequence, "sequence");
93 {
94 return result;
95 }
97 {
98 return new ListOfTWrapper<T>(collection);
99 }
100 return new FallbackWrapper<T>(sequence);
101 }
102
103 internal static void ClearFastWhenEmpty<T>(this Stack<T> stack)
104 {
105 if (stack.Count > 0)
106 {
107 stack.Clear();
108 }
109 }
110
120
121 internal static bool TryGetCount<T>(this IEnumerable<T> sequence, out int count)
122 {
123 return ((IEnumerable)sequence).TryGetCount<T>(out count);
124 }
125
126 internal static bool TryGetCount<T>(this IEnumerable sequence, out int count)
127 {
129 {
130 count = collection.Count;
131 return true;
132 }
134 {
136 return true;
137 }
139 {
141 return true;
142 }
143 count = 0;
144 return false;
145 }
146
148 {
149 if (!sequence.TryGetCount(out var count))
150 {
151 List<T> list = sequence.ToList();
152 count = list.Count;
153 sequence = list;
154 }
155 return count;
156 }
157
158 internal static bool TryCopyTo<T>(this IEnumerable<T> sequence, T[] array, int arrayIndex)
159 {
160 if (sequence is IList<T>)
161 {
163 {
165 return true;
166 }
167 if (sequence.GetType() == typeof(T[]))
168 {
169 T[] array2 = (T[])sequence;
170 Array.Copy(array2, 0, array, arrayIndex, array2.Length);
171 return true;
172 }
174 {
176 return true;
177 }
178 }
179 return false;
180 }
181
182 internal static T[] ToArray<T>(this IEnumerable<T> sequence, int count)
183 {
184 Requires.NotNull(sequence, "sequence");
185 Requires.Range(count >= 0, "count");
186 if (count == 0)
187 {
188 return ImmutableArray<T>.Empty.array;
189 }
190 T[] array = new T[count];
191 if (!sequence.TryCopyTo(array, 0))
192 {
193 int num = 0;
194 foreach (T item in sequence)
195 {
196 Requires.Argument(num < count);
197 array[num++] = item;
198 }
199 Requires.Argument(num == count);
200 }
201 return array;
202 }
203}
static unsafe void Copy(Array sourceArray, Array destinationArray, int length)
Definition Array.cs:624
void CopyTo(KeyValuePair< TKey, TValue >[] array, int index)
static bool TryGetCount< T >(this IEnumerable< T > sequence, out int count)
static bool TryCopyTo< T >(this IEnumerable< T > sequence, T[] array, int arrayIndex)
static DisposableEnumeratorAdapter< T, TEnumerator > GetEnumerableDisposable< T, TEnumerator >(this IEnumerable< T > enumerable)
static IOrderedCollection< T > AsOrderedCollection< T >(this IEnumerable< T > sequence)
static int GetCount< T >(ref IEnumerable< T > sequence)
static T[] ToArray< T >(this IEnumerable< T > sequence, int count)
static void ClearFastWhenEmpty< T >(this Stack< T > stack)
static void Range(bool condition, string? parameterName, string? message=null)
Definition Requires.cs:40
static void Argument(bool condition, string? parameterName, string? message)
Definition Requires.cs:59
new IEnumerator< T > GetEnumerator()