Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FrugalList.cs « TextDataUtil « Util « Text « src - github.com/microsoft/vs-editor-api.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c6ac083e12618a14a637585c007f22f37b16074a (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//
//  Copyright (c) Microsoft Corporation. All rights reserved.
//  Licensed under the MIT License. See License.txt in the project root for license information.
//
#undef STATS

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;

namespace Microsoft.VisualStudio.Text.Utilities
{

#if STATS
    static class Stats
    {
        public static int[] sizes = new int[20];
    }
#endif

#pragma warning disable CA1710 // Identifiers should have correct suffix
    /// <summary>
    /// <para>
    /// This implementation is intended for lists that are usually empty or have a single element.
    /// The element type may be a struct or a class, but lists of structs are by far the most common
    /// in the editor. We store the head of the list in a local field and allocate an array for the tail of the
    /// list only if it the list has length greater than one. Thus singleton and empty lists require only
    /// a single object (not counting elements of the list), as compared to the BCL version of list, 
    /// which allocates two objects for a list with a single member.
    /// </para>
    /// <para>
    /// Do not use this implementation for lists that you know will have length greater than two; the
    /// platform list implementation will be more space efficient.
    /// </para>
    /// </summary>
    /// <typeparam name="T">The type of the list element.</typeparam>
    public class FrugalList<T> : IList<T>, IReadOnlyList<T>
#pragma warning restore CA1710 // Identifiers should have correct suffix
    {
        const int InitialTailSize = 2;                  // initial size of array list

        static List<T> UnitaryTail = new List<T>(0);    // marker that the FrugalList has length one.

        T head;          // first element of list
        List<T> tail;    // the balance

#if STATS
        ~FrugalList()
        {
            Stats.sizes[Math.Min(19, this.count)]++;
        }
#endif
        #region Construction
        // there is no constructor that takes a capacity because we are using 
        // the tail field to tell us something about the current count of elements
        // in the list. If you are tempted to provide a capacity, and it's less than two,
        // go ahead and use this list without a capacity. If it's greater than two, you should
        // be using a regular list, which will be more frugal.

        public FrugalList()
        {
        }

        public FrugalList(IList<T> elements)
        {
            if (elements == null)
            {
                throw new ArgumentNullException(nameof(elements));
            }
            switch (elements.Count)
            {
                case 0:
                    break;
                case 1:
                    this.head = elements[0];
                    this.tail = UnitaryTail;
                    break;
                default:
                    this.head = elements[0];
                    this.tail = new List<T>(InitialTailSize);
                    for (int i = 1; i < elements.Count; ++i)
                    {
                        this.tail.Add(elements[i]);
                    }
                    break;
            }
        }
        #endregion

        public int Count
        {
            get
            {
                if (this.tail == null)
                {
                    return 0;
                }
                else
                {
                    return 1 + this.tail.Count;
                }
            }
        }

        public void AddRange(IList<T> list)
        {
            if (list == null)
            {
                throw new ArgumentNullException(nameof(list));
            }

            for (int i = 0; i < list.Count; ++i)
            {
                Add(list[i]);
            }
        }

        public ReadOnlyCollection<T> AsReadOnly()
        {
            return new ReadOnlyCollection<T>(this);
        }

        public int RemoveAll(Predicate<T> match)
        {
            if (match == null)
            {
                throw new ArgumentNullException(nameof(match));
            }
            int removed = 0;
            for (int i = Count - 1; i >= 0; --i)
            {
                if (match(this[i]))
                {
                    removed++;
                    RemoveAt(i);
                }
            }
            return removed;
        }

        public void Add(T item)
        {
            if (Count == 0)
            {
                this.head = item;
                this.tail = UnitaryTail;
            }
            else
            {
                Debug.Assert(this.tail != null);
                if (this.tail == UnitaryTail)
                {
                    this.tail = new List<T>(InitialTailSize);
                }
                this.tail.Add(item);
            }
        }

        public int IndexOf(T item)
        {
            if (Count > 0)
            {
                if (EqualityComparer<T>.Default.Equals(this.head, item))
                {
                    return 0;
                }
                else
                {
                    int indexOf = this.tail.IndexOf(item);
                    return indexOf >= 0 ? indexOf + 1 : -1;
                }
            }
            else
            {
                return -1;
            }
        }

        public void Insert(int index, T item)
        {
            if (index < 0 || index > this.Count)
            {
                throw new ArgumentOutOfRangeException(nameof(index));
            }
            if (index == 0)
            {
                switch (Count)
                {
                    case 0:
                        this.tail = UnitaryTail;
                        break;
                    case 1:
                        this.tail = new List<T>(InitialTailSize);
                        this.tail.Add(this.head);
                        break;
                    default:
                        this.tail.Insert(0, this.head);
                        break;
                }
                this.head = item;
            }
            else
            {
                Debug.Assert(Count > 0);
                if (this.tail == UnitaryTail)
                {
                    this.tail = new List<T>(InitialTailSize);
                }
                this.tail.Insert(index - 1, item);
            }
        }

        public void RemoveAt(int index)
        {
            if (index < 0 || index >= Count)
            {
                throw new ArgumentOutOfRangeException(nameof(index));
            }

            int count = Count;
            if (index == 0)
            {
                if (count == 1)
                {
                    this.head = default(T);
                    this.tail = null;
                }
                else
                {
                    this.head = this.tail[0];
                    if (count == 2)
                    {
                        this.tail = UnitaryTail;
                    }
                    else
                    {
                        this.tail.RemoveAt(0);
                    }
                }
            }
            else if (count == 2)
            {
                Debug.Assert(index == 1);
                this.tail = UnitaryTail;
            }
            else
            {
                this.tail.RemoveAt(index - 1);
            }
        }

        // Analyzer has a bug where it is giving a false positive in this location.
#pragma warning disable CA1065 // Do not raise exceptions in unexpected locations
        public T this[int index]
        {
            get
            {
                if (index < 0 || index >= Count)
                {
                    throw new ArgumentOutOfRangeException(nameof(index));
                }

                if (index == 0)
                {
                    return this.head;
                }
                else
                {
                    return this.tail[index - 1];
                }
            }
            set
            {
                if (index < 0 || index >= Count)
                {
                    throw new ArgumentOutOfRangeException(nameof(index));
                }

                if (index == 0)
                {
                    this.head = value;
                }
                else
                {
                    this.tail[index - 1] = value;
                }
            }
        }
#pragma warning restore CA1065 // Do not raise exceptions in unexpected locations

        public void Clear()
        {
            this.head = default(T);
            this.tail = null;
        }

        public bool Contains(T item)
        {
            int count = Count;
            if (count > 0)
            {
                if (EqualityComparer<T>.Default.Equals(this.head, item))
                {
                    return true;
                }
                if (count > 1)
                {
                    return this.tail.Contains(item);
                }
            }
            return false;
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            if (array == null)
            {
                throw new ArgumentNullException(nameof(array));
            }
            int count = Count;
            if (count > 0)
            {
                // let array indexing do the index checks
                array[arrayIndex++] = this.head;
                if (count > 1)
                {
                    this.tail.CopyTo(array, arrayIndex);
                }
            }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public bool Remove(T item)
        {
            if (Count > 0)
            {
                if (EqualityComparer<T>.Default.Equals(this.head, item))
                {
                    RemoveAt(0);
                    return true;
                }
                else
                {
                    return this.tail.Remove(item);
                }
            }
            return false;
        }

        IEnumerator<T> IEnumerable<T>.GetEnumerator()
        {
            return new FrugalEnumerator(this);
        }

        public FrugalEnumerator GetEnumerator()
        {
            return new FrugalEnumerator(this);
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return new FrugalEnumerator(this);
        }

        public struct FrugalEnumerator : IEnumerator, IEnumerator<T>
        {
            FrugalList<T> list;
            int position;

            public FrugalEnumerator(FrugalList<T> list)
            {
                this.list = list;
                this.position = -1;
            }

            public T Current
            {
                get
                {
                    // if position is -1, then the behavior of Current is unspecified.
                    // for us it will throw an indexing exception.
                    return this.list[this.position];
                }
            }

            object IEnumerator.Current
            {
                get { return this.list[this.position]; }
            }

            public bool MoveNext()
            {
                if (this.position < this.list.Count - 1)
                {
                    this.position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            public void Reset()
            {
                this.position = -1;
            }

            public void Dispose()
            {
            }
        }
    }

}